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

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

#define BUFFLOOP_C
#define I_DISPLAY_C
#define I_FIELDEDIT_C
#define I_FINDS_C
#define I_FOLDHELP_C
#define I_FOLDFILING_C
#define I_FOLDING_C
#define I_FOLDFILING_C
#define I_GETMSG_C
#define I_GETTK_C
#define I_KEYBOARD_C
#define I_KEYTAB_C
#define I_LOOP_C
#define I_MAIN_C
#define I_MISC_C
#define I_MESSAGES_C
#define I_ORIEDT_C
#define I_PROMPT_C
#define I_READFOLDS_C
#define I_SCREEN_C
#define I_SHELL_C
#define I_SIGNALS_C
#define I_STRING_C
#define I_VIRTUAL_C
#define I_WRITEF_C

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

/*{{{  variables*/
public element *deleted_lines[UNDEL_LINES];
public int delete_ptr=0;
#ifdef REGEXP
  public reg_types reg_type=no_reg;
#endif
/*}}}  */

/*{{{  try_unchange*/
public void try_unchange(void)
{
  if (bd.m.file_changed_status==changed_once)
   { bd.m.file_changed_status=unchanged_file;
     title_op(CHGTITLE);
   }
}
/*}}}  */
/*{{{  buffer_loop*/
/*{{{  ref_shifted_line*/
private void ref_shifted_line(void)
{
  if (bd.scr.cur_shift_w!=bd.scr.full_shift_w)
   /*{{{  line is shifted left, unshift it*/
   { write_dsp_line(bd.f.current,bd.scr.cursor.h);
     bd.scr.cur_shift_w=bd.scr.full_shift_w;
   }
   /*}}}  */
}
/*}}}  */

TOKEN buffer_loop(TOKEN ch,boolean active_buffer)
{
  if (bd.m.upd_sel_delayed && !scr_off)
     upd_highlight(bd.f.real_head,bd.f.real_tail,True);
  switch (ch)
   {
     /*{{{  O_NOP: a normal editor loop*/
     case O_NOP:
      { boolean quit;

        title_op(CHGTITLE);
        /*{{{  set ocl vars for screen and screen position..*/
        ocl_var[var_cu_b_id].v=bd.scr.id;
        ocl_var[var_cu_buff].v=bd.scr.list_no;
        ocl_var[var_scr_h].v=bd.scr.txt_size.h;
        ocl_var[var_off_h].v=bd.scr.off.h;
        ocl_var[var_scr_w].v=bd.scr.txt_size.w;
        ocl_var[var_off_w].v=bd.scr.off.w;
        ocl_var[var_file_no].v=bd.e.file_no;
        /*}}}  */
        for (quit=False,ch=O_NOP;!quit;)
         {
           /*{{{  variables*/
           int old_x;
           int scr_key;
           /*}}}  */

           /*{{{  save screen-cursor x-position to ocl-var*/
           ocl_var[cy_ind].v=bd.scr.cursor.h;
           old_x=bd.scr.cursor.w;
           /*}}}  */
           /*{{{  maybe change window title*/
#           ifdef WINDOW_TITLE_CHANGE
              if (wt_buff_id!=bd.scr.id && !scr_off)
               { chg_w_title((char*)0);
                 wt_buff_id=bd.scr.id;
               }
#           endif
           /*}}}  */
           /*{{{  maybe warn about multiple edited buffer*/
           if (mult_edit)
            { ori_assert(bd.scr.cur_shift_w<=LINELEN*max_dsp,"shift-check");
              title_op(PRTTITLE);
              message(get_msg(M_MULT_EDIT,mult_edit+1));
              mult_edit=0;
              if (!scr_off) warn_delay();
            }
           /*}}}  */
           /*{{{  maybe full redraw screen if long message or interrupt_restore*/
           if (interrupt_restore || (dirty && ch!=O_PROMPT))
             { B_invalid();
               auto_flush();
             }
           /*}}}  */
           /*{{{  handle abort*/
           if (aborted)
            { aborted = False;
              if (!run_abort_mac)
               { stop_macros(True);
                 call_number_macro(abort_macro);
                 run_abort_mac=True;
               }
              else
                 run_abort_mac=False;
              if (!ocl_screen_on())
               { while (!ocl_screen_on());
                 auto_flush();
               }
              msg_message(M_ABORTED);
            }
           /*}}}  */
           /*{{{  get textline, call fieldedit, save textline*/
           copyin(cur_dsp_line, bd.f.current, False);
           bd.f.cur_line_typ=get_linetyp(*bd.f.current);
           /*{{{  get next editor-token*/
           ch=field_edit();
           /*}}}  */
           ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
           if (!(bd.f.cur_line_typ&END_FOLD))
              copyout(cur_dsp_line, bd.f.current);
           /*}}}  */
           /*{{{  maybe execute command here*/
           ori_assert(bd.scr.cur_shift_w<=LINELEN*max_dsp,"shift-check");
           switch ((scr_key=valid_screen_key(ch)))
            {
              /*{{{  0 : error, no [restricted]fold key*/
              no_restricted_fold_key:
                 upd_highlight(bd.f.real_head,bd.f.real_tail,True);
              case 0:
                 msg_message(M_NOFOLDKEY);
                 break;
              /*}}}  */
              /*{{{  +/- 1 : (un) limited fold key*/
              case -1:
               /*{{{  reset highlight marks*/
               { sel_mod old_h;

                 old_h=bd.m.select_mode;
                 bd.m.select_mode=no_selection;
                 upd_highlight(bd.f.real_head,bd.f.real_tail,True);
                 bd.m.select_mode=old_h;
               }
               /*}}}  */
              case 1:
               /*{{{  handle action*/
                 if (ch!=O_PROMPT) no_message();
                 if (ch>=O_NOP)
                  /*{{{  handle loop command*/
                  { switch (ch) {
                     /*{{{  default: no action*/
                     no_highlight_redraw:
                     default:
                        scr_key= -2;
                        break;
                     /*}}}  */
                     /*{{{  ops, handled partial in main loop*/
                     case M_SHOW_HELP:
                        set_area(ocl_var[get_arg("area-size")].v);
                        ch=O_REFRESH;
                     case O_A_NOPARSE:
                     case O_D_NOPARSE:
                     case O_A_AUTO_SAVE:
                     case O_D_AUTO_SAVE:
                     case O_DESCRIBE_BINDINGS:
                     case O_DSP:
                     case O_HELP:
                     case O_OPEN_BUFFER:
                     case O_OPEN_W_BUFFER:
                     case O_OPEN_LIST_BUFFER:
                     case O_OPEN_LIST_W_BUFFER:
                     case O_GROW_BUFFER:
                     case O_GROW_W_BUFFER:
                     case O_SHRINK_BUFFER:
                     case O_SHRINK_W_BUFFER:
                     case O_AUTO_SAVE:
                     case O_FLUSH:
                     case O_REFRESH:
                     case O_TITLE_HIDE:
                     case O_TITLE_SHOW:
                     case M_LAYOUT:
                     case M_GO_BUFFER:
                         quit=True;
                         goto no_highlight_redraw;
                     /*}}}  */
#                    ifdef REGEXP
                      /*{{{  reg exp handling*/
                      /*{{{  O_D_REGEXP*/
                        case O_D_REGEXP:
                           if (reg_type)
                            { ocl_var[var_ocl_arg].v=(reg_type==basic_reg)?1:2;
                              reg_type=no_reg;
                              title_op(CHGTITLE);
                            }
                           else
                              ocl_var[var_ocl_arg].v=0;
                           break;
                      /*}}}  */
                      /*{{{  O_A_B_REGEXP*/
                        case O_A_B_REGEXP:
                           if (reg_type!=basic_reg)
                            { reg_type=basic_reg;
                              ocl_var[var_ocl_arg].v=1;
                              title_op(CHGTITLE);
                            }
                           break;
                      /*}}}  */
                      /*{{{  O_A_REGEXP*/
                        case O_A_X_REGEXP:
                           if (reg_type!=ext_reg)
                            { reg_type=ext_reg;
                              ocl_var[var_ocl_arg].v=2;
                              title_op(CHGTITLE);
                            }
                           break;
                      /*}}}  */
                      /*}}}  */
#                    else
                      /*{{{  reg exp not here*/
                        case O_D_REGEXP:
                        case O_A_B_REGEXP:
                        case O_A_X_REGEXP:
                           ocl_var[var_ocl_arg].v=0;
                           message(M_NOT_HERE);
                           break;
                      /*}}}  */
#                    endif
                     /*{{{  M_EDIT_HISTORY*/
                     case M_EDIT_HISTORY:
                      { unsigned char prompt[LINELEN + 1];
                        unsigned char ret[LINELEN + 1];
                        histories old_default;
                        unsigned char const *init_text;
                        int init_pos;

                        /*{{{  get initialization for the prompt*/
                        switch (get_arg("hist edit init"))
                         { case 0:
                           default:
                              init_text=0;
                              init_pos= -1;
                              break;
                           case 1:
                              init_text=cur_dsp_line;
                              init_pos=bd.scr.cursor.w-2;
                              break;
                           case 2:
                              init_text=(bd.f.real_tail==bd.f.real_head)
                                         ? 0
                                         : get_data(bd.f.real_tail);
                              init_pos= -1;
                              break;
                         }
                        /*}}}  */
                        old_default=overwrite_history_id;
                        overwrite_history_id=(histories)get_arg("history");
                        prompt_string(prompt);
                        readprompt
                         ( ret,
                           prompt,
                           LINELEN,
                           overwrite_history_id,
                           init_text,
                           init_pos
                         );
                        overwrite_history_id=old_default;
                        break;
                      }
                     /*}}}  */
                     /*{{{  M_WRITE_HELP*/
                     case M_WRITE_HELP:
                      { int x;
                        int y;
                        unsigned char pr[LINELEN+1];

                        x=ocl_var[get_arg("area-x")].v;
                        y=ocl_var[get_arg("area-y")].v;
                        prompt_string(pr);
                        write_area(x,y,pr);
                        break;
                      }
                     /*}}}  */
                     /*{{{  M_CHANGE_FOLD*/
                     case M_CHANGE_FOLD:
                      { int t=get_arg("fold-type");

                        if (chg_foldtype(ocl_var[t].v,ocl_var[get_arg("fold-contents")].v))
                         { unchange_error_po:

                           try_unchange();

                           error_po:

                           verbose_msg_message(M_ERR_PO);
                         }
                        goto no_highlight_redraw;
                      }
                     /*}}}  */
                     /*{{{  O_ATTACH_FILE*/
                     case O_ATTACH_FILE:
                        attach_file();
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_A_POSI*/
                     case O_A_POSI:
                        if (!xy_show)
                         { xy_show=True;
                           title_op(CHGTITLE);
                         }
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_A_ECHO*/
                     case O_A_ECHO:
                        switch_echo(True);
                        message((unsigned char*)STR_E_A);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_A_TIME*/
                     case O_A_TIME:
                        switch_time(True);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_A_VERBOSE*/
                     case O_A_VERBOSE:
                        verbose=True;
                        message((unsigned char*)STR_V_A);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_A_FULLSHIFT*/
                     case O_A_FULLSHIFT:
                        init_buffer.m.full_shift=bd.m.full_shift=True;
                        message((unsigned char*)STR_F_A);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_BOT_OF_FOLD*/
                     case O_BOT_OF_FOLD:
                        ref_shifted_line();
                        skip_to(bd.f.tail);
                        break;
                     /*}}}  */
                     /*{{{  O_BREAK*/
                     case O_BREAK:
                        break;
                     /*}}}  */
                     /*{{{  O_CHANGE_MARK*/
                     case O_CHANGE_MARK:
                        chg_marks();
                        break;
                     /*}}}  */
                     /*{{{  O_CLOSE_FOLD*/
                     case O_CLOSE_FOLD:
                        close_fold();
                        break;
                     /*}}}  */
                     /*{{{  O_COPY*/
                     case O_COPY:
                      { element  *copy_ptr;

                        ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                        if
                         (    (bd.f.cur_line_typ&(NOT_FOLD|START_FOLD|START_FILED))
                           && bd.f.current!=bd.f.tail
                           && (copy_ptr=copy_a_line())
                         )
                         { ref_shifted_line();
                           undel_after(copy_ptr);
                         }
                        else
                           goto unchange_error_po;
                        break;
                      }
                     /*}}}  */
                     /*{{{  O_COPY_PICK*/
                     case O_COPY_PICK:
                        ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                        if (bd.f.cur_line_typ&(NOT_FOLD|START_FOLD|START_FILED))
                           append_to_pick(copy_a_line());
                        else
                           goto error_po;
                        break;
                     /*}}}  */
                     /*{{{  O_CREATE_FOLD*/
                     case O_CREATE_FOLD:
                        switch (bd.m.select_mode)
                         { case no_selection:
                            /*{{{  start fold or highlight*/
                              switch (ocl_var[var_mod_beh].v)
                               { case fold_selection-1:
                                 case unmark_fold_selection-1:
                                  /*{{{  start folding*/
                                    ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                                    if (bd.f.cur_line_typ&(START_ENTER_FOLD|START_ENTER_FILED))
                                       goto unchange_error_po;
                                    else
                                     { start_make_fold(ocl_var[var_mod_beh].v+1);
                                       title_op
                                        ( bd.f.count_comment_line
                                           ? CHGXY
                                           : INCY
                                        );
                                     }
                                    break;
                                  /*}}}  */
                                 case lim_pseudo_fold_selection-1:
                                  /*{{{  lim_pseudo-fold-selection*/
                                    bd.m.select_mode=lim_pseudo_fold_selection;
                                    goto do_start_select;
                                  /*}}}  */
                                 case lim_reg_selection-1:
                                  /*{{{  limited region selection*/
                                    bd.m.select_mode=lim_reg_selection;
                                    goto do_start_select;
                                  /*}}}  */
                                 case lim_block_selection-1:
                                  /*{{{  limited block selection*/
                                    bd.m.select_mode=lim_block_selection;
                                    goto do_start_select;
                                  /*}}}  */
                                 case lim_nodrawn_selection-1:
                                  /*{{{  limited nodrawn selection*/
                                    bd.m.select_mode=lim_nodrawn_selection;
                                    goto do_start_select;
                                  /*}}}  */
                                 case -(lim_pseudo_fold_selection-1):
                                  /*{{{  pseudo-fold-selection*/
                                    bd.m.select_mode=pseudo_fold_selection;
                                    goto do_start_select;
                                  /*}}}  */
                                 case -(lim_reg_selection-1):
                                  /*{{{  unlimited region selection*/
                                    bd.m.select_mode=reg_selection;
                                    goto do_start_select;
                                  /*}}}  */
                                 case -(lim_block_selection-1):
                                  /*{{{  unlimited block selection*/
                                    bd.m.select_mode=block_selection;
                                    goto do_start_select;
                                  /*}}}  */
                                 case -(lim_nodrawn_selection-1):
                                  /*{{{  unlimited nodrawn selection*/
                                    bd.m.select_mode=nodrawn_selection;
                                    goto do_start_select;
                                  /*}}}  */
                                 do_start_select:
                                   /*{{{  start highlighting*/
                                   bd.f.select_ptr=bd.f.current;
                                   bd.e.makefold_indent=bd.scr.cursor.w-1;
                                   upd_highlight(bd.f.current,bd.f.current,False);
                                   if
                                    (    bd.m.select_mode==lim_pseudo_fold_selection
                                      || bd.m.select_mode==pseudo_fold_selection
                                    ) move_down();
                                   break;
                                   /*}}}  */
                                 default:
                                    break;
                               }
                              break;
                            /*}}}  */
                           case fold_selection:
                           case unmark_fold_selection:
                            /*{{{  try to make a fold*/
                              make_fold();
                              if (bd.m.select_mode==no_selection)
                                 title_op(CHGXY);
                              break;
                            /*}}}  */
                           default:
                            /*{{{  end highlighting*/
                              bd.m.select_mode=no_selection;
                              upd_highlight(bd.f.real_head,bd.f.real_tail,True);
                              restore(1);
                              break;
                            /*}}}  */
                         }
                        break;
                     /*}}}  */
                     /*{{{  O_DEL_LINE*/
                     case O_DEL_LINE:
                      { element *x;
                        int op=delete_ptr;
                      
                        ref_shifted_line();
                        if (++delete_ptr==UNDEL_LINES) delete_ptr=0;
                        x=deleted_lines[delete_ptr];
                        remove_line(&deleted_lines[delete_ptr]);
                        if (x==deleted_lines[delete_ptr])
                         { delete_ptr=op;
                      
                     unchange_file:
                      
                           try_unchange();
                         }
                        break;
                      }
                     /*}}}  */
                     /*{{{  O_DELETE*/
                     case O_DELETE:
                        ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                        if
                         (    (bd.f.cur_line_typ&NOT_FOLD)
                           && bd.f.current != bd.f.tail
                           && bd.f.current != bd.f.head->next
                           && test_linetyp
                               ( *(bd.f.current->prec),
                                 (   NOT_FOLD
                                   | START_FILED
                                   | START_FOLD
                                   | START_OPEN_FOLD
                                   | START_ENTER_FOLD
                                 )
                               )
                         )
                         { title_op(CHGXY);
                           ustrcpy(line_buffer,get_data(bd.f.current));
                           trailing_spaces(line_buffer);
                           bd.f.cur_line_typ=get_linetyp(*bd.f.current);
                           move_up();
                           copyin(cur_dsp_line, bd.f.current, False);
                           trailing_spaces(cur_dsp_line);
                           bd.scr.cursor.w=txt2scr(bd.f.enter_spaces,
                                                   ustrlen(cur_dsp_line),
                                                   cur_dsp_line);
                           if (bd.scr.cursor.w==1)
                              bd.scr.cursor.w=bd.f.current->indent+1;
                           if
                            (    (ustrlen(line_buffer)+ustrlen(cur_dsp_line))
                              <= LINELEN-bd.f.enter_spaces
                            )
                            { ustrcpy(cur_dsp_line,get_data(bd.f.current));
                              ustrcat(cur_dsp_line,line_buffer);
                              set_data
                               ( bd.f.current,cur_dsp_line,
                                 test_linetyp(*bd.f.current,NOT_FOLD)
                               );
                              ori_assert((cur_dsp_line[0]==0)||((cur_dsp_line[ustrlen(cur_dsp_line)-1]!='\t')&&(cur_dsp_line[ustrlen(cur_dsp_line)-1]!=' ')),"delete-join: trailling space");
                              write_dsp_line(bd.f.current, bd.scr.cursor.h);
                              bd.f.cur_line_typ=get_linetyp(*bd.f.current);
                              move_down();
                              bd.e.part_line = 0;
                              remove_line(&bd.e.part_line);
                              proc_dispose(bd.e.part_line);
                              bd.f.cur_line_typ=get_linetyp(*bd.f.current);
                              move_up();
                              break;
                            }
                           else
                              msg_message(M_LONG_LINE);
                         }
                        else
                           verbose_msg_message(M_ERR_PO);
                        goto unchange_file;
                     /*}}}  */
                     /*{{{  O_D_POSI*/
                     case O_D_POSI:
                        if (xy_show)
                         { xy_show=False;
                           title_op(CHGTITLE);
                         }
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_D_ECHO*/
                     case O_D_ECHO:
                        switch_echo(False);
                        message((unsigned char*)STR_E_D);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_D_TIME*/
                     case O_D_TIME:
                        ocl_var[var_ocl_arg].v=switch_time(False);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_D_VERBOSE*/
                     case O_D_VERBOSE:
                        verbose=False;
                        message((unsigned char*)STR_V_D);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_D_FULLSHIFT*/
                     case O_D_FULLSHIFT:
                        ocl_var[var_ocl_arg].v=bd.m.full_shift?1:0;
                        init_buffer.m.full_shift=bd.m.full_shift=False;
                        message((unsigned char*)STR_F_D);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_DOWN*/
                     case O_DOWN:
                        ref_shifted_line();
                        move_down();
                        break;
                     /*}}}  */
                     /*{{{  O_ENTER_FOLD*/
                     case O_ENTER_FOLD:
                        if (scr_key<=0) goto no_restricted_fold_key;
                        enter_fold((unsigned char*)0);
                        goto chg_file_no;
                     /*}}}  */
                     /*{{{  O_ENTER_NO_FILE*/
                     case O_ENTER_NO_FILE:
                        if (scr_key<=0) goto no_restricted_fold_key;
                        enter_list_file();
                        goto chg_file_no;
                     /*}}}  */
                     /*{{{  O_EXIT_FOLD*/
                     case O_EXIT_FOLD:
                        if (scr_key<=0) goto no_restricted_fold_key;
                        bd.scr.cursor.w = 1;
                        exit_fold();
                        goto chg_file_no;
                     /*}}}  */
                     /*{{{  O_FILE_FOLD*/
                     case O_FILE_FOLD:
                        file_fold((unsigned char*)0,(FILE*)0);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_FILTER_OPs*/
                     case O_FILTER_BUFFER:
                     case O_FILTER_LINE:
                        if (scr_key<=0)
                           goto no_restricted_fold_key;
                        filter_op(ch==O_FILTER_BUFFER);
                        break;
                     /*}}}  */
                     /*{{{  O_FIND/O_FIND_REVERSE*/
                     case O_FIND:
                     case O_FIND_REVERSE:
                        ref_shifted_line();
                        get_search(False,False,True);
                        if (*item_to_look_for != '\0' && !aborted)
                         { if
                            ( !find_item
                                ( (ch==O_FIND_REVERSE),
                                  True,
                                  True
                                  re_args(reg_type)
                                )
                            )
                              msg_message(M_FAILED);
                           title_op(CHGXY);
                         }
                        break;
                     /*}}}  */
                     /*{{{  M/O_FINISH*/
                     case M_FINISH:
                     case O_FINISH:
                        check_time_event();
                        save_file(True);
                        ori_assert(bd.scr.cur_shift_w<=LINELEN*max_dsp,"shift-check");
                        title_op(PRTTITLE);
                        if (bd.m.file_changed_status==unchanged_file)
                           quit=True;
                        else
                           auto_flush();
                        break;
                     /*}}}  */
                     /*{{{  O_FIRST_LINE_TO_FOLD_H*/
                     case O_FIRST_LINE_TO_FOLD_H:
                        ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                        if (!(bd.f.cur_line_typ&(START_FOLD|START_OPEN_FOLD)))
                           goto unchange_error_po;
                        set_data(bd.f.current,get_data(bd.f.current->x.fold.data),False);
                        write_dsp_line(bd.f.current, bd.scr.cursor.h);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_FOLD_INFO*/
                     case O_FOLD_INFO:
                        print_buffer[0]='[';
                        /*{{{  position and current char*/
                        { int txt_pos;
                          int curr_c;

                          copyin(cur_dsp_line, bd.f.current, False);
                          txt_pos=scr2txt(bd.f.enter_spaces,bd.scr.cursor.w,cur_dsp_line);
                          curr_c=(txt_pos>=ustrlen(cur_dsp_line))?' ':cur_dsp_line[txt_pos];
                          ustrcpy(print_buffer+1,
                                  get_msg(F_INFO_1,
                                          cur_line_no(),
                                          bd.scr.cursor.w+bd.f.enter_spaces,
                                          curr_c,
                                          curr_c,
                                          curr_c,
                                          curr_c,
                                          bd.f.entered,
                                          bd.f.enter_spaces));
                        }
                        /*}}}  */
                        /*{{{  filename and type/contents*/
                        { unsigned char *f=(unsigned char*)empty_text;

                          ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                          switch (bd.f.cur_line_typ)
                           { case END_FOLD:
                                if (test_linetyp(*(bd.f.current->x.fold.other_end),START_ENTER_FILED))
                                   f=get_data(bd.f.current);
                                else if (0)
                             case START_FILED:
                             case START_ENTER_FILED:
                                 { f=get_data(bd.f.current->x.fold.other_end);
                                   add_history(file_history,f);
                                 }
                             case START_FOLD:
                             case START_OPEN_FOLD:
                             case START_ENTER_FOLD:
                              /*{{{  get string*/
                                ustrcat
                                 ( print_buffer,
                                   get_msg
                                    ( F_INFO_2,
                                      bd.f.current->x.fold.UU.U1.type,
                                      bd.f.current->x.fold.UU.U1.contents,
                                      f
                                    )
                                 );
                                trailing_spaces(print_buffer);
                              /*}}}  */
                              /*{{{  set ocl-arg to coding type/contents*/
                                ocl_var[var_ocl_arg].v=
                                    ((bd.f.current->x.fold.UU.U1.type&127)<<8)
                                  + bd.f.current->x.fold.UU.U1.contents;
                                if (bd.f.current->x.fold.UU.U1.type&128)
                                   ocl_var[var_ocl_arg].v= -ocl_var[var_ocl_arg].v;
                              /*}}}  */
                             default:
                                break;
                           }
                        }
                        /*}}}  */
                        ustrcat(print_buffer,(unsigned char*)"]");
                        message(print_buffer);
                        ch=O_PROMPT;
                        break;
                     /*}}}  */
                     /*{{{  O_GOTO_LINE*/
                     case O_GOTO_LINE:
                        goto_line(-1,(element*)0,False);
                        break;
                     /*}}}  */
                     /*{{{  M_GOTO_MARK*/
                     case M_GOTO_MARK:
                        goto_line
                         ( 0,
                           ocl_var[get_arg("go-mark-arg")].l,
                           ocl_var[var_mod_beh].v
                         );
                        break;
                     /*}}}  */
                     /*{{{  O_INSERT_FILE*/
                     case O_INSERT_FILE:
                        ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                        if (bd.f.cur_line_typ&(START_ENTER_FOLD|START_ENTER_FILED))
                           goto unchange_error_po;
                        else
                         { struct lg_dumb lang_dump;

                           save_lang(&lang_dump);
                           insert_file
                            ( (element *)0,
                              bd.f.current->prec,
                              bd.f.current,
                              False,
                              bd.f.real_head,
                              (boolean*)0,
                              (boolean*)0,
                              (FILE*)0
                            );
                           reset_lang(&lang_dump);
                           bd.m.read_only=False;
                           title_op(CHGXY);
                           restore_element(bd.scr.cursor.h);
                         }
                        break;
                     /*}}}  */
                     /*{{{  O_ITS_REVERSE*/
                     case O_ITS_REVERSE:
                        ref_shifted_line();
                        its_search(True);
                        break;
                     /*}}}  */
                     /*{{{  O_ITS_SEARCH*/
                     case O_ITS_SEARCH:
                        ref_shifted_line();
                        its_search(False);
                        break;
                     /*}}}  */
                     /*{{{  O_LANGUAGE*/
                     case O_LANGUAGE:
                        proc_language();
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_MOVE*/
                     case O_MOVE:
                      { static element *move_ptr=0;

                        if (move_ptr)
                         { ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                           if (bd.f.cur_line_typ&(START_ENTER_FOLD|START_ENTER_FILED))
                              goto unchange_error_po;
                           else
                            { ref_shifted_line();
                              undel_before(move_ptr);
                              move_ptr = 0;
                            }
                         }
                        else
                           remove_line(&move_ptr);
                        ocl_var[var_move_count].v=move_ptr?1:0;
                        break;
                      }
                     /*}}}  */
                     /*{{{  O_OPEN_FOLD*/
                     case O_OPEN_FOLD:
                        open_fold();
                        break;
                     /*}}}  */
                     /*{{{  O_OPEN_NEW_FILE*/
                     case O_OPEN_NEW_FILE:
                        if (scr_key<=0) goto no_restricted_fold_key;
                        open_file(False);
                        goto chg_file_no;
                     /*}}}  */
                     /*{{{  O_OPEN_LIST_FILE*/
                     case O_OPEN_LIST_FILE:
                        if (scr_key<=0) goto no_restricted_fold_key;
                        open_list_file();
                        goto chg_file_no;
                     /*}}}  */
                     /*{{{  O_PAGE_UP/DOWN*/
                     case O_PAGE_DOWN:
                      { int i;

                        ref_shifted_line();
                        i=bd.scr.txt_size.h-bd.scr.cursor.h+1;
                        /*{{{  skip i lines*/
                        while (i-- && bd.f.current != bd.f.tail)
                           bd.f.current = bd.f.current->next;
                        /*}}}  */
                        goto o_p_refresh;
                     case O_PAGE_UP:
                        ref_shifted_line();
                        i=bd.scr.txt_size.h+bd.scr.cursor.h-3;
                        /*{{{  skip i lines*/
                        while (i-- && bd.f.current->prec != bd.f.head)
                           bd.f.current = bd.f.current->prec;
                        /*}}}  */
                     o_p_refresh:
                        title_op(CHGXY);
                        if (bd.m.select_mode!=no_selection)
                           upd_highlight(bd.f.real_head,bd.f.real_tail,True);
                        restore_element(1);
                        break;
                      }
                     /*}}}  */
                     /*{{{  O_PICK*/
                     case O_PICK:
                      { element  *pick_ptr;

                        pick_ptr = 0;
                        remove_line(&pick_ptr);
                        if (pick_ptr)
                           append_to_pick(pick_ptr);
                        else
                           try_unchange();
                        goto no_highlight_redraw;
                      }
                     /*}}}  */
                     /*{{{  O_PIPE_FROM_COMMAND*/
                     case O_PIPE_FROM_COMMAND:
                        ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                        if (bd.f.cur_line_typ&(START_ENTER_FOLD|START_ENTER_FILED))
                           goto unchange_error_po;
                        else
                         { pipe_from_command();
                           restore_element(bd.scr.cursor.h);
                         }
                        break;
                     /*}}}  */
                     /*{{{  O_PIPE_TO_COMMAND*/
                     case O_PIPE_TO_COMMAND:
                        if (bd.f.tail == bd.f.real_tail)
                           pipe_to_command();
                        else
                           msg_message(M_NO_TOP);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_PROMPT*/
                     case O_PROMPT:
                        write_dsp_line(bd.f.current,bd.scr.cursor.h);
                        if (executing_macro) oflush;
                        break;
                     /*}}}  */
                     /*{{{  O_PUT_PICK*/
                     case O_PUT_PICK:
                        ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                        if (bd.f.cur_line_typ&(START_ENTER_FOLD|START_ENTER_FILED))
                           goto unchange_error_po;
                        else if (pick_head != pick_tail)
                           undel_pick_before();
                        else
                         { msg_message(M_NO_PICK);
                           goto unchange_file;
                         }
                        break;
                     /*}}}  */
                     /*{{{  O_QUERY_REPLACE*/
                     case O_QUERY_REPLACE:
                        if (scr_key<=0) goto no_restricted_fold_key;
                        ref_shifted_line();
                        query_replace(False);
                        break;
                     /*}}}  */
                     /*{{{  O_QUIT*/
                     case O_QUIT:
                        check_time_event();
                        ori_assert(bd.scr.cur_shift_w<=LINELEN*max_dsp,"shift-check");
                        title_op(PRTTITLE);
                        quit = (bd.m.file_changed_status!=unchanged_file ?
                                     yes(get_msg(M_CHANGED_LEAVE)) : True);
                        break;
                     /*}}}  */
                     /*{{{  O_REMOVE_FOLD*/
                     case O_REMOVE_FOLD:
                        unfold();
                        break;
                     /*}}}  */
                     /*{{{  O_RENAME_FILE*/
                     case O_RENAME_FILE:
                        if (bd.f.real_tail!=bd.f.real_head)
                         { unsigned char file[_POSIX_PATH_MAX+1];

                           fileprompt(file);
                           if (file[0])
                            { set_data(bd.f.real_tail,file,False);
                              set_data(bd.f.real_head->next,file,False);
                              add_edit_file((char*)0,cur_line_no(),True);
                              bd.e.file_no=add_edit_file((char*)file,cur_line_no(),True);
                              bd.m.dir_edit=is_dir((FILE*)0,(char*)file);
#                             ifdef WINDOW_TITLE_CHANGE
                                 wt_buff_id= -1;
#                             endif
                              title_op(CHGTITLE);
                              restore_element(bd.scr.cursor.h);
                              warn_mult_edit();
                     chg_file_no:
                              ocl_var[var_file_no].v=bd.e.file_no;
                              break;
                            }
                         }
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_REPLACE*/
                     case O_REPLACE:
                        if (scr_key<=0) goto no_restricted_fold_key;
                        do_replace();
                        break;
                     /*}}}  */
                     /*{{{  O_RETURN*/
                     case O_RETURN:
                        switch (bd.m.select_mode)
                         { case no_selection:
                           case pseudo_fold_selection:
                           case reg_selection:
                           case block_selection:
                              ref_shifted_line();
                              if (bd.e.split_line)
                                 undel_after(bd.e.part_line);
                              else
                               { undel_before(bd.e.part_line);
                                 bd.f.cur_line_typ=get_linetyp(*bd.f.current);
                                 move_down();
                               }
                           default:
                              break;
                         }
                        break;
                     /*}}}  */
                     /*{{{  O_SAVE_FILE*/
                     case O_SAVE_FILE:
                        save_file(False);
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_SHELL_COMMAND*/
                     case O_SHELL_COMMAND:
                      { unsigned char cmd[LINELEN + 1];

                        s_readprompt(cmd, get_msg(M_SH_COMMAND), LINELEN,shell_history);
                        if (*cmd)
                         { TOKEN x;

                           put_vars();
                           reset_keyboard();
                           reset_terminal();
                           force_sh=ocl_var[var_force_sh].v;
                           ocl_var[var_ocl_arg].v=subshell((char*)cmd);
                           init_terminal();
                           init_keyboard();
                           B_valid();
                           msg_message(M_END);
                           do
                            { x=hide_key(); }
                           while (x!=O_RETURN && x!=O_BREAK);
                           B_invalid();
                           auto_flush();
                         }
                        goto no_highlight_redraw;
                      }
                     /*}}}  */
                     /*{{{  O_SHELL*/
                     case O_SHELL:
                        msg_message(M_SHELL_CREATED);
                        put_vars();
                        reset_keyboard();
                        reset_terminal();
                        force_sh=ocl_var[var_force_sh].v;
                        subshell((char*)0);
                        init_terminal();
                        init_keyboard();
                        B_invalid();
                        auto_flush();
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_SUSPEND*/
                     case O_SUSPEND:
                        suspend();
                        goto no_highlight_redraw;
                     /*}}}  */
                     /*{{{  O_TOP_OF_FOLD*/
                     case O_TOP_OF_FOLD:
                        ref_shifted_line();
                        skip_to(bd.f.head->next);
                        break;
                     /*}}}  */
                     /*{{{  O_UNDEL_LINE*/
                     case O_UNDEL_LINE:
                        ori_assert(bd.f.cur_line_typ==get_linetyp(*bd.f.current),"check cur_linetyp");
                        if (bd.f.cur_line_typ&(START_ENTER_FOLD|START_ENTER_FILED))
                           goto unchange_error_po;
                        else if (deleted_lines[delete_ptr])
                         { ref_shifted_line();
                           undel_before(deleted_lines[delete_ptr]);
                           deleted_lines[delete_ptr] = 0;
                           if (--delete_ptr<0)
                              delete_ptr=UNDEL_LINES-1;
                         }
                        else
                         { msg_message(M_NO_UNDEL);
                           goto unchange_file;
                         }
                        break;
                     /*}}}  */
                     /*{{{  O_UP*/
                     case O_UP:
                        ref_shifted_line();
                        move_up();
                        break;
                     /*}}}  */
                     /*{{{  O_WRITE_FILE*/
                     case O_WRITE_FILE:
                        write_file();
                        goto no_highlight_redraw;
                     /*}}}  */
                    }
                  }
                  /*}}}  */
               /*}}}  */
               /*{{{  maybe reset highlight marks*/
                 if (scr_key<0)
                  { ori_assert(bd.scr.cur_shift_w<=LINELEN*max_dsp,"shift-check");
                    upd_highlight(bd.f.real_head,bd.f.real_tail,True);
                    ori_assert(bd.scr.cur_shift_w<=LINELEN*max_dsp,"shift-check");
                    if (scr_key>-2)
                       restore(1);
                  }
               /*}}}  */
                 break;
              /*}}}  */
            }
           if
            /*{{{  block-selection changed*/
            (    scr_key>=1
              && old_x!=bd.scr.cursor.w
              && (    bd.m.select_mode==lim_block_selection
                   || bd.m.select_mode==block_selection
                 )
            )
            /*}}}  */
            /*{{{  redraw display for new marks*/
              restore(1);
            /*}}}  */
           /*}}}  */
         }
        break;
      }
     /*}}}  */
     /*{{{  O_AUTO_SAVE*/
     case O_AUTO_SAVE:
        auto_write();
        break;
     /*}}}  */
     /*{{{  O_REFRESH/O_FLUSH*/
     case O_REFRESH:
        if (active_buffer)
         { bd.scr.cur_shift_w=0;
           title_op(CHGXY);
           restore_element(bd.scr.txt_size.h / 2);
#          ifdef WINDOW_TITLE_CHANGE
              if (!scr_off)
                 wt_buff_id= -1;
#          endif
           break;
         }
     case O_FLUSH:
        if (bd.scr.txt_size.h)
           restore_element(bd.scr.cursor.h);
        if (!active_buffer)
           title_op(SMALLTITLE);
        break;
     /*}}}  */
     default:
        ch=O_NOP;
        break;
   }
  return(ch);
}
/*}}}  */
