/*{{{}}}*/
/*{{{  #includes*/
#ifdef CONFIG_H
#   include "config.h"
#endif

#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

#define FIELDEDIT_C
#define I_BUFFLOOP_C
#define I_DISPLAY_C
#define I_FILEC_C
#define I_FOLDING_C
#define I_GETTK_C
#define I_GETMSG_C
#define I_KEYTAB_C
#define I_LOOP_C
#define I_MAIN_C
#define I_MESSAGES_C
#define I_ORIEDT_C
#define I_PROMPT_C
#define I_SCREEN_C
#define I_SET_C
#define I_SIGNALS_C
#define I_STRING_C
#define I_VIRTUAL_C

#include "origami.h"
#include <lib/ori_add_lib.h>
/*}}}  */

/*{{{  variables*/
private unsigned char curr_c=' ';
private int ind;
private int txt_pos;
private int txt_len;
private int f_txt_offset=0;
private int high_lim= -1;
private highlight_typ high_type=no_highlight;
public unsigned char deleted_ch = ' ';
/*}}}  */

/*{{{  field_scr2txt*/
private int field_scr2txt(int o,int x,unsigned char const * const s)
{ int txt;

  txt=scr2txt(o,x,s);
  if (txt>txt_len)
     txt= -1;
  return(txt);
}
/*}}}  */
/*{{{  append_needed_spaces*/
private void append_needed_spaces(boolean add_one)
{ if (txt_pos==-1 || txt_pos==txt_len)
   { int i;
     unsigned char *x;

     if (txt_pos==-1)
        txt_pos=scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line);
     i=txt_pos-txt_len;
     if (add_one)
        i++;
     x=cur_dsp_line+txt_len;
     txt_len+=i;
     memset(x,(int)' ',(size_t)i);
     x[i]='\0';
     if (dsp.norm!=norm_dsp)
        bd.scr.cur_shift_w= -1;
   }
}
/*}}}  */
/*{{{  refresh_field*/
private void refresh_field(int froms,boolean clr2eoln,boolean force)
{ int x;

  if (froms<1)
     froms = 1;
  x_shift
   ( &bd.scr.cur_shift_w,
     &f_txt_offset,
     x=bd.scr.cur_shift_w,
     bd.scr.cursor.w,
     bd.f.enter_spaces,
     cur_dsp_line
   );
  if (force || x!=bd.scr.cur_shift_w)
   { froms=bd.scr.cur_shift_w+1;x=f_txt_offset; }
  else
     x=scr2txt(bd.f.enter_spaces,froms,cur_dsp_line);
  if (x>=txt_len)
   /*{{{  clr line from position*/
   { if (clr2eoln)
       clrbuffline(bd.scr.cursor.h,froms-bd.scr.cur_shift_w);
   }
   /*}}}  */
  else
   /*{{{  print line*/
   { int p,h1,h2;

     move_cursor_to(bd.scr.cursor.h+bd.scr.off.h,froms-bd.scr.cur_shift_w+bd.scr.off.w);
     /*{{{  get highlight positions*/
     if (high_lim>=0)
      { int l;

        l=(high_type==m_f_highlight)?bd.e.makefold_indent+1:bd.scr.cursor.w;
        if (l>high_lim)
         { h1=high_lim;h2=l-1; }
        else
         { h1=l-1;h2=high_lim+1;
           if (h1<h2)
              h1++;
         }
      }
     else
      { h1=(LINELEN+2)*max_dsp;
        h2=(LINELEN+3)*max_dsp;
      }
     /*}}}  */
     /*{{{  print the line*/
     p=prt_highlight_bin_text
        ( h1-froms+1,
          h2-froms+1,
          bd.f.enter_spaces+froms-1,
          bd.scr.txt_size.w-froms+bd.scr.cur_shift_w,
          True,
          cur_dsp_line+x
        );
     /*}}}  */
     /*{{{  maybe delete tail*/
     if (clr2eoln && p<=bd.scr.txt_size.w-froms+bd.scr.cur_shift_w)
        clrbuffline(bd.scr.cursor.h,froms-bd.scr.cur_shift_w+p);
     /*}}}  */
   }
   /*}}}  */
  if (bd.scr.cur_shift_w && soln_str)
   { move_cursor_to(bd.scr.cursor.h+bd.scr.off.h,1+bd.scr.off.w);
     oputc(soln_str);
   }
}
/*}}}  */
/*{{{  restore_cursor*/
private void restore_cursor(boolean force)
 { int x;
 
   /*{{{  get shift-width*/
   ori_assert(bd.scr.cur_shift_w<=LINELEN*max_dsp,"shift-check");
   x_shift
    ( &x,
      &f_txt_offset,
      (x=bd.scr.cur_shift_w),
      bd.scr.cursor.w,
      bd.f.enter_spaces,
      cur_dsp_line
    );
   /*}}}  */
   /*{{{  maybe special screen update (highlight or full-shift)*/
   if (force || x!=bd.scr.cur_shift_w || high_lim>=0)
    { refresh_field(x+1,True,True);
      title_op(CHGX);
    }
   /*}}}  */
   title_op(UPDTITLE);
   move_cursor_to
    ( bd.scr.cursor.h+bd.scr.off.h,
      (int)(1+(bd.scr.cursor.w-1-bd.scr.cur_shift_w)%bd.scr.txt_size.w)+bd.scr.off.w
    );
 }
/*}}}  */
/*{{{  restore_cursor_and_sleep*/
public void restore_cursor_and_sleep(int t)
 {
   if (!scr_off)
    { restore_cursor(False);
      oflush;
      if (t>=10)
         sleep(t/10);
      if ((t%=10))
         usleep((unsigned int)t*(unsigned long)100000);
    }
 }
/*}}}  */
/*{{{  field_move_cursor*/
private void field_move_cursor(int dist)
{ while (dist)
    if (dist>0)
     /*{{{  move right*/
     { if (txt_pos==-1 || txt_pos==txt_len)
        /*{{{  simple go right on screen*/
        { bd.scr.cursor.w+=dist*dsp.norm;
          txt_pos= -1;
          dist=0;
        }
        /*}}}  */
       else
        /*{{{  maybe check if tab*/
        { enum dsp_size dsp_lg;

          switch((dsp_lg=CHAR_DSP_SIZE(cur_dsp_line[txt_pos])))
           { case tab_dsp:
                bd.scr.cursor.w=((bd.scr.cursor.w+bd.f.enter_spaces+dsp.tab_size-1)/dsp.tab_size)*dsp.tab_size+1-bd.f.enter_spaces;
                break;
             case ictrl_dsp:
                dsp_lg=1;
             default:
                bd.scr.cursor.w+=dsp_lg;
                break;
           }
          txt_pos++;
          if (txt_pos>txt_len)
             txt_pos= -1;
          dist--;
        }
        /*}}}  */
     }
     /*}}}  */
    else
     /*{{{  move left*/
     { if (txt_pos==-1)
          txt_pos=scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line);
       txt_pos--;
       if (txt_pos<txt_len)
        /*{{{  check the given character too*/
        { enum dsp_size dsp_lg;

          switch((dsp_lg=CHAR_DSP_SIZE(cur_dsp_line[txt_pos])))
           { case tab_dsp:
                bd.scr.cursor.w=txt2scr(bd.f.enter_spaces,txt_pos,cur_dsp_line);
                break;
             case ictrl_dsp:
                dsp_lg=1;
             default:
                bd.scr.cursor.w-=dsp_lg;
                break;
           }
        }
        /*}}}  */
       else
        /*{{{  char is ' '!*/
          bd.scr.cursor.w-=dsp.norm;
        /*}}}  */
       dist++;
     }
     /*}}}  */
  /*{{{  maybe overflow-controll*/
  if (txt_pos>txt_len) txt_pos= -1;
  if (bd.scr.cursor.w<=0) { bd.scr.cursor.w=1;txt_pos=0; }
  if (bd.scr.cursor.w>LINELEN-bd.f.enter_spaces+1 )
     if (scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line)>LINELEN-bd.f.enter_spaces)
      { bd.scr.cursor.w=LINELEN-bd.f.enter_spaces+1;
        txt_pos=field_scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line);
        msg_message(M_LONG_LINE);
      }
  /*}}}  */
}
/*}}}  */
/*{{{  move_cursor*/
public void move_cursor(int dist)
{
  txt_len=ustrlen(cur_dsp_line);
  txt_pos=field_scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line);
  field_move_cursor(dist);
}
/*}}}  */
/*{{{  do_cc_compare*/
public void do_cc_compare(unsigned char const c,TOKEN const tok)
{
  unsigned int a;

  a=get_arg("do-c-compare");
  switch (tok)
   { case M_TEST_CHAR_SET:
        macro_tag=test_char_set(c,a);
        break;
     case M_TEST_CC:
     case M_TEST_L_CC:
     case M_TEST_H_CC:
        a=ocl_var[a].v;
     default:
        macro_tag=
           (tok==M_TEST_CHAR || tok==M_TEST_CC)      ? ((unsigned int)c==a) :
           (tok==M_TEST_CHAR_LOW || tok==M_TEST_L_CC)? ((unsigned int)c<a) :
                                                       ((unsigned int)c>a);
   }
}
/*}}}  */
/*{{{  do_str_compare*/
public void do_str_compare(unsigned char const * const str)
 { unsigned char arg[LINELEN+1];

   prompt_string(arg);
   macro_tag=!strncmp((char*)str,(char*)arg,ustrlen(arg));
 }
/*}}}  */
/*{{{  insert_*/
private void insert_(char const ch)
{
  /*{{{  errorcheck && prepare*/
  /*{{{  edit-position*/
  if
   (   (    bd.scr.cursor.w>bd.e.pre_scr_e_pos
         || ch!=' '
         || bd.scr.cursor.w<=ind*dsp.norm
         || bd.m.overwrite
       )
    && bd.scr.cursor.w<bd.e.first_scr_e_pos
   )
   { verbose_msg_message(M_ERR_PO);
     goto unchange_ret;
   }
  if (bd.scr.cursor.w>LINELEN-bd.f.enter_spaces)
     if (scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line)>=LINELEN-bd.f.enter_spaces)
      { msg_message(M_LONG_LINE);
        goto unchange_ret;
      }
  /*}}}  */
  append_needed_spaces(bd.m.overwrite);
  /*}}}  */
  if (!bd.m.overwrite)
   /*{{{  shift right*/
   {
     /*{{{  check long line*/
     if (   (txt_len>=LINELEN-bd.f.enter_spaces)
         || (bd.m.dialect.typ==F_C_TDS && txt_len>=TDS_LEN-bd.f.enter_spaces) )
      { trailing_spaces(cur_dsp_line);
        txt_len=ustrlen(cur_dsp_line);
        if (txt_len>=LINELEN-bd.f.enter_spaces)
         { msg_message(M_LONG_LINE);
           goto unchange_ret;
         }
        else if (bd.m.dialect.typ==F_C_TDS && txt_len>=TDS_LEN-bd.f.enter_spaces)
           msg_message(M_TDS_LONG_LINE);
        if (txt_pos>txt_len)
         { txt_pos= -1;
           append_needed_spaces(False);
         }
      }
     /*}}}  */
     /*{{{  insert the character at correct position in line*/
     ustrcpy(line_buffer,cur_dsp_line+txt_pos);
     ustrcpy(cur_dsp_line+txt_pos+1,line_buffer);
     txt_len++;
     /*}}}  */
   }
   /*}}}  */
  cur_dsp_line[txt_pos]=ch;
  refresh_field(bd.scr.cursor.w,bd.m.overwrite,False);
  if (bd.scr.cursor.w<=bd.e.pre_scr_e_pos)
   { bd.e.first_scr_e_pos+=dsp.norm;
     bd.e.pre_scr_e_pos+=dsp.norm;
   }
  field_move_cursor(1);
  return;

unchange_ret:
  try_unchange();
}
/*}}}  */
/*{{{  delete__*/
private boolean delete__(void)
{
  if (bd.scr.cursor.w <= ind*dsp.norm + 1)
     return(False);
  else
   { if
      /*{{{  incorrext edit pos*/
      (    bd.scr.cursor.w<=bd.e.first_scr_e_pos
        && bd.scr.cursor.w>bd.e.pre_scr_e_pos
      )
      /*}}}  */
      /*{{{  complain*/
      { try_unchange();
        verbose_msg_message(M_ERR_PO);
      }
      /*}}}  */
     else
      /*{{{  do it*/
      { append_needed_spaces(False);
        field_move_cursor(-1);
        deleted_ch=cur_dsp_line[txt_pos];
        /*{{{  do the delete*/
        { unsigned char *s;

          for (s=cur_dsp_line+txt_pos;(*s= *(s+1));s++);
          txt_len--;
        }
        /*}}}  */
        if (bd.scr.cursor.w<=bd.e.pre_scr_e_pos)
         { bd.e.first_scr_e_pos-=dsp.norm;
           bd.e.pre_scr_e_pos-=dsp.norm;
         }
        refresh_field(bd.scr.cursor.w,True,False);
      }
      /*}}}  */
     return(True);
   }
}
/*}}}  */
/*{{{  field_edit*/
public TOKEN field_edit(void)
{
  /*{{{  declarations*/
  TOKEN ch;
  boolean field_key;
  change_status old_status;
  boolean force_line_flush;
  /*}}}  */

  /*{{{  prepare editing field*/
  force_line_flush= !bd.m.full_shift;
  proc_to_edit_pos();
  if (bd.m.file_changed_status!=unchanged_file)
     bd.m.file_changed_status=changed_once;
  old_status=bd.m.file_changed_status;
  ind = bd.f.current->indent;
  ori_assert(bd.f.enter_spaces>=0,"fieldedit: enter_spaces>=0");
  txt_len=ustrlen(cur_dsp_line);
  if (txt_len > LINELEN-bd.f.enter_spaces)
     exit_origami(r_ocl_err,get_msg(M_LONG_LINE));
  txt_pos=field_scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line);
  if (txt_pos>=0)
     bd.scr.cursor.w=txt2scr(bd.f.enter_spaces,txt_pos,cur_dsp_line);
  /* check, if behind LINELEN, if other lines uses multichar displayed
   * characters, therefor:
   */ field_move_cursor(0);
  /*{{{  handle highlighting*/
  switch ((high_type=get_highlight(*bd.f.current)))
   { case c_f_highlight:
     case m_f_highlight:
        high_lim=(LINELEN+2)*max_dsp;
        bd.scr.cur_shift_w= -1;
        break;
     case c_m_highlight:
        high_lim=bd.e.makefold_indent;
        bd.scr.cur_shift_w= -1;
        break;
     case f_m_highlight:
        high_lim=0;
        bd.scr.cur_shift_w= -1;
        break;
     default:
        high_lim= -1;
   }
  /*}}}  */
  /*}}}  */
  do
   { title_op(CHGX);
     /*{{{  get the next operation*/
     curr_c=(txt_pos==-1 || txt_pos==txt_len) ? ' ' : cur_dsp_line[txt_pos];
     /*{{{  maybe user-signal*/
#     ifdef USER_SIGNAL
       user_signal_handle();
#     endif
     /*}}}  */
     do
      {
        /*{{{  handle signals*/
#        if defined(MGR)
           if (win_changed)
            { ch=O_REFRESH;
              break;
            }
#        endif
        if (sig_count)
         { push_macro(sig_macro);
           ocl_msg("pushing %d signals",sig_count);
           sig_count=0;
         }
        /*}}}  */
        /*{{{  show cursor, maybe shift remaining screen*/
        restore_cursor(force_line_flush);
        force_line_flush=False;
        if (restore_shift(bd.scr.cur_shift_w))
           restore_cursor(False);
        /*}}}  */
        ori_assert(bd.scr.cur_shift_w>=0,"negative shift");
        ch=edit_key();
      }
     while (!valid_key(ch));
     field_key = valid_field_key(ch);
     /*}}}  */
     if (field_key)
      { int old_x;

        /*{{{  store information for block-selections*/
        old_x=bd.scr.cursor.w;
        /*}}}  */
        /*{{{  handle command*/
        no_message();
        if (ch>=O_NOP)
         /*{{{  a fieldedit command*/
           switch (ch)
            {
              /*{{{  M_BEGIN_OF_LINE*/
              case M_BEGIN_OF_LINE: {
                int po=0;
                unsigned char *t=cur_dsp_line;

                while (*t==' ' || *t=='\t') {t++;po++;}
                macro_tag= *t ? (txt2scr(bd.f.enter_spaces,po,cur_dsp_line)==bd.scr.cursor.w)
                              : bd.scr.cursor.w==ind+1;
                break;
              }
              /*}}}  */
              /*{{{  M_TOP_OF_FOLD*/
              case M_TOP_OF_FOLD: {
                macro_tag=(bd.f.current==bd.f.head->next);
                break;
              }
              /*}}}  */
              /*{{{  M_BOT_OF_FOLD*/
              case M_BOT_OF_FOLD: {
                macro_tag=(bd.f.current==bd.f.tail);
                break;
              }
              /*}}}  */
              /*{{{  M_TEST_CHAR (LOW/HIGH/ ) _C SET*/
              case M_TEST_CHAR_SET:
              case M_TEST_CC:
              case M_TEST_L_CC:
              case M_TEST_H_CC:
              case M_TEST_CHAR_LOW:
              case M_TEST_CHAR_HIGH:
              case M_TEST_CHAR:
                 do_cc_compare(curr_c,ch);
                 break;
              /*}}}  */
              /*{{{  M_TEST_STR*/
              case M_TEST_STR:
                 do_str_compare((txt_pos==-1 || txt_pos==txt_len)?empty_text:(cur_dsp_line+txt_pos));
                 break;
              /*}}}  */
              /*{{{  M_STORE_C*/
              case M_STORE_C:
                 ocl_var[get_arg("store-char-var")].v=curr_c;
                 break;
              /*}}}  */
              /*{{{  M_END_OF_LINE*/
              case M_END_OF_LINE:
                 if (txt_len>ind)
                  { unsigned char *x;
                    int i=txt_len;

                    if (txt_pos==-1) { macro_tag=True;break; }
                    x=cur_dsp_line+(txt_len-1);
                    while (i-- && (*x==' ' || *x=='\t')) x--;
                    macro_tag=(x<(cur_dsp_line+txt_pos));
                  }
                 else
                    macro_tag=True;
                 break;
              /*}}}  */
              /*{{{  M_GO_COUNTER_X_POS*/
              case M_GO_COUNTER_X_POS:
               { int diff;

                 diff=  scr2txt(bd.f.enter_spaces,ocl_var[get_arg("goto-x-target")].v,cur_dsp_line)
                      - (txt_pos==-1?scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line):txt_pos);
                 field_move_cursor(diff);
                 break;
               }
              /*}}}  */
              /*{{{  M_POS_TO_COUNTER*/
              case M_POS_TO_COUNTER:
                 ocl_var[get_arg("store-pos-target")].v= bd.scr.cursor.w;
                 break;
              /*}}}  */
              /*{{{  O_FILE_C*/
              case O_FILE_C:
                 if (txt_pos!=-1 && (curr_c==' ' || curr_c=='\t'))
                  { unsigned char *s;

                    /*{{{  look for pathname*/
                    for (s=cur_dsp_line+txt_pos-1;;)
                       if (s==cur_dsp_line)
                          break;
                       else if (*--s==' ' || *s=='\t')
                        { s++;
                          break;
                        }
                    /*}}}  */
                    /*{{{  set 0 to indicate end of filename*/
                    cur_dsp_line[txt_pos]='\0';
                    /*}}}  */
                    if (!*do_file_c((char*)s))
                       try_unchange();
                    /*{{{  restore deleted tail of current line*/
                    if (txt_pos!=txt_len) cur_dsp_line[txt_pos]=curr_c;
                    /*}}}  */
                  }
                 break;
              /*}}}  */
              /*{{{  O_START_OF_LINE*/
              case O_START_OF_LINE:
               { if (txt_len>ind)
                  { unsigned char *x=cur_dsp_line;
                    int o=0;

                    while (*x==' ' || *x=='\t') { o++;x++; }
                    if (!*x) o=ind;
                    field_move_cursor(
                      o-(  (txt_pos==-1)
                         ? scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line)
                         : txt_pos));
                  }
                 else
                    field_move_cursor(ind+1-bd.scr.cursor.w);
                 break;
               }
              /*}}}  */
              /*{{{  O_END_OF_LINE*/
              case O_END_OF_LINE: {
                unsigned char *x=cur_dsp_line+(txt_len-1);
                int i=txt_len;

                if (txt_len)
                   while (*x==' ' || *x=='\t')
                    { x--;
                      i--;
                    }
                if (txt_pos==-1)
                   txt_pos=scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line);
                field_move_cursor((i>ind ? i : ind)-txt_pos);
                /*{{{  maybe correct the position*/
                if
                 (    bd.scr.cursor.w<bd.e.first_scr_e_pos
                   && bd.e.first_scr_e_pos<LINELEN-bd.f.enter_spaces
                 )
                   field_move_cursor((bd.e.first_scr_e_pos-bd.scr.cursor.w)/dsp.norm);
                /*}}}  */
                break;
              }
              /*}}}  */
              /*{{{  O_LEFT*/
              case O_LEFT:
                 field_move_cursor(-1);
                 break;
              /*}}}  */
              /*{{{  O_RIGHT*/
              case O_RIGHT:
                 field_move_cursor(1);
                 break;
              /*}}}  */
              /*{{{  O_DELETE*/
              case O_DELETE: {
                field_key=delete__();
                break;
              }
              /*}}}  */
              /*{{{  O_RETURN*/
              case O_RETURN:   /* cr */
                 switch (bd.m.select_mode)
                  { case fold_selection:
                    case unmark_fold_selection:
                    case lim_pseudo_fold_selection:
                    case lim_reg_selection:
                    case lim_block_selection:
                    case lim_nodrawn_selection:
                       msg_message(M_NOFOLDKEY);
                       break;
                    default:
                       /*{{{  do and break, if possible*/
                       ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"%d %d check cur_linetyp");
                       if
                        (    bd.scr.cursor.w>bd.e.pre_scr_e_pos
                          || (    !(bd.f.cur_line_typ&NOT_FOLD)
                               && bd.scr.cursor.w==bd.e.pre_scr_e_pos
                             )
                        )
                        { int first_c;
                          unsigned char *first_txt;

                          /*{{{  fill exact with spaces*/
                          /*{{{  cut trailing spaces*/
                          trailing_spaces(cur_dsp_line);
                          txt_len=ustrlen(cur_dsp_line);
                          if (txt_pos>=txt_len) txt_pos= -1;
                          /*}}}  */
                          append_needed_spaces(False);
                          /*}}}  */
                          /*{{{  first_c=first-non-space*/
                          for
                           ( first_c=0,first_txt=cur_dsp_line
                           ;    first_c<txt_pos
                             && (*first_txt==' ' || *first_txt=='\t')
                           ; first_c++,first_txt++
                           );
                          /*}}}  */
                          if
                           (   (    bd.f.current!=bd.f.tail
                                 || bd.scr.cursor.w==1
                               )
                            && (   txt_pos==txt_len
                                || (   !(bd.f.cur_line_typ&START_ENTER_FILED)
                                    && (   bd.f.cur_line_typ&NOT_FOLD
                                        || (    txt_pos==first_c
                                             && !(bd.f.cur_line_typ&START_ENTER_FOLD)
                                           )
                                        || bd.scr.cursor.w>=bd.e.first_scr_e_pos
                                       )
                                   )
                               )
                           )
                           /*{{{  do the return*/
                           { bd.e.split_line = txt_pos>first_c;
                             bd.e.part_line=proc_new_element();
                             if (!(bd.f.cur_line_typ&END_FOLD))
                                bd.e.part_line->indent = ind;
                             else
                                bd.e.part_line->indent =
                                    bd.f.current->x.fold.other_end->indent
                                   -bd.f.current->x.fold.other_end->x.fold.UU.U1.indent;
                             /*{{{  maybe splitt the line*/
                             if (bd.e.split_line)
                              { set_data
                                 ( bd.e.part_line,
                                   cur_dsp_line+txt_pos,
                                   bd.f.cur_line_typ&NOT_FOLD
                                 );
                                cur_dsp_line[txt_pos]='\0';
                                txt_len=txt_pos;
                                refresh_field(bd.scr.cursor.w,True,False);
                                field_move_cursor((first_c<=ind?ind:first_c)-txt_pos);
                              }
                             /*}}}  */
                             ustrcpy(line_buffer,cur_dsp_line+ind);
                             ustrcpy(line_buffer+((ind<first_c)?(first_c-ind):0),get_data(bd.e.part_line));
                             trailing_spaces(line_buffer);
                             set_data(bd.e.part_line,line_buffer,True);
                             field_key = False;
                             break;
                           }
                           /*}}}  */
                        }
                       /*}}}  */
                    unchange_err_po:
                       try_unchange();
                       verbose_msg_message(M_ERR_PO);
                       break;
                  }
                 break;
              /*}}}  */
              /*{{{  O_TOGGLE_CASE*/
              case O_TOGGLE_CASE:
                 if
                  (    bd.scr.cursor.w<bd.e.first_scr_e_pos
                    && bd.scr.cursor.w>bd.e.pre_scr_e_pos
                  )
                    goto unchange_err_po;
                 append_needed_spaces(True);
                 if (isalpha(curr_c))
                   cur_dsp_line[txt_pos]=islower(curr_c)?toupper(curr_c):tolower(curr_c);
                 refresh_field(bd.scr.cursor.w,False,False);
                 break;
              /*}}}  */
              /*{{{  O_A_VIEW*/
              case O_A_VIEW:
                 init_buffer.m.user_view=
                 init_buffer.m.read_only=
                 bd.m.user_view=
                 bd.m.read_only=True;
                 title_op(CHGTITLE);
                 break;
              /*}}}  */
              /*{{{  O_D_VIEW*/
              case O_D_VIEW:
                 init_buffer.m.user_view=
                 init_buffer.m.read_only=
                 bd.m.user_view=
                 bd.m.read_only=False;
                 title_op(CHGTITLE);
                 break;
              /*}}}  */
              /*{{{  O_A_OVER*/
              case O_A_OVER:
                 init_buffer.m.overwrite=True;
                 if (!bd.m.overwrite)
                  { bd.m.overwrite=True;
                    title_op(CHGTITLE);
                  }
                break;
              /*}}}  */
              /*{{{  O_D_OVER*/
              case O_D_OVER:
                 init_buffer.m.overwrite=False;
                 if (bd.m.overwrite)
                  { init_buffer.m.overwrite=bd.m.overwrite=False;
                    title_op(CHGTITLE);
                  }
                 break;
              /*}}}  */
              /*{{{  O_A_HASH*/
              case O_A_HASH:
                 if (!bd.m.hash_shift)
                  { bd.m.hash_shift=True;
                    title_op(CHGTITLE);
                  }
                 else
                    try_unchange();
                 init_buffer.m.hash_shift=True;
                 break;
              /*}}}  */
              /*{{{  O_D_HASH*/
              case O_D_HASH:
                 if (bd.m.hash_shift)
                  { bd.m.hash_shift=False;
                    title_op(CHGTITLE);
                  }
                 else
                    try_unchange();
                 init_buffer.m.hash_shift=False;
                 break;
              /*}}}  */
              /*{{{  default*/
              default: break;
              /*}}}  */
            }
         /*}}}  */
        else
           insert_(ch);
        /*}}}  */
        /*{{{  maybe return to higher level, to handle block selections*/
        switch (bd.m.select_mode)
         { case lim_block_selection:
           case block_selection:
              if (field_key && old_x!=bd.scr.cursor.w)
               { ch=O_PROMPT;
                 field_key=False;
               }
           default:
              break;
         }
        /*}}}  */
      }
   }
  while (field_key);
  if (bd.m.file_changed_status!=old_status)
   { trailing_spaces(cur_dsp_line);
     proc_from_edit_pos();
   }
  return ch;
}
/*}}}  */
