home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / amiga / vim46src.lha / vim-4.6 / src / edit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  87.3 KB  |  3,710 lines

  1. /* vi:set ts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved        by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  */
  8.  
  9. /*
  10.  * edit.c: functions for insert mode
  11.  */
  12.  
  13. #include "vim.h"
  14. #include "globals.h"
  15. #include "proto.h"
  16. #include "option.h"
  17. #include "ops.h"    /* for op_type */
  18.  
  19. #ifdef INSERT_EXPAND
  20. /*
  21.  * definitions used for CTRL-X submode
  22.  */
  23. #define CTRL_X_WANT_IDENT        0x100
  24.  
  25. #define CTRL_X_NOT_DEFINED_YET    (1)
  26. #define CTRL_X_SCROLL            (2)
  27. #define CTRL_X_WHOLE_LINE        (3)
  28. #define CTRL_X_FILES            (4)
  29. #define CTRL_X_TAGS                (5 + CTRL_X_WANT_IDENT)
  30. #define CTRL_X_PATH_PATTERNS    (6 + CTRL_X_WANT_IDENT)
  31. #define CTRL_X_PATH_DEFINES        (7 + CTRL_X_WANT_IDENT)
  32. #define CTRL_X_FINISHED            (8)
  33. #define CTRL_X_DICTIONARY        (9 + CTRL_X_WANT_IDENT)
  34.   
  35. struct Completion
  36. {
  37.     char_u                *str;
  38.     char_u                *fname;
  39.     int                    original;
  40.     struct Completion    *next;
  41.     struct Completion    *prev;
  42. };
  43.  
  44. struct Completion *first_match = NULL;
  45. struct Completion *curr_match = NULL;
  46.  
  47. static int add_completion __ARGS((char_u *str, int len, char_u *, int dir));
  48. static int make_cyclic __ARGS((void));
  49. static void complete_dictionaries __ARGS((char_u *, int));
  50. static void free_completions __ARGS((void));
  51. static int count_completions __ARGS((void));
  52. #endif /* INSERT_EXPAND */
  53.  
  54. #define BACKSPACE_CHAR                1
  55. #define BACKSPACE_WORD                2
  56. #define BACKSPACE_WORD_NOT_SPACE    3
  57. #define BACKSPACE_LINE                4
  58.  
  59. static void change_indent __ARGS((int type, int amount, int round));
  60. static void insert_special __ARGS((int, int));
  61. static void start_arrow __ARGS((FPOS *end_insert_pos));
  62. static void stop_arrow __ARGS((void));
  63. static void stop_insert __ARGS((FPOS *end_insert_pos));
  64. static int echeck_abbr __ARGS((int));
  65.  
  66. static FPOS     Insstart;        /* This is where the latest insert/append
  67.                                  * mode started. */
  68. static colnr_t    Insstart_textlen;    /* length of line when insert started */
  69. static colnr_t    Insstart_blank_vcol;    /* vcol for first inserted blank */
  70.  
  71. static char_u    *last_insert = NULL;
  72.                             /* the text of the previous insert */
  73. static int        last_insert_skip;
  74.                             /* number of chars in front of previous insert */
  75. static int        new_insert_skip;
  76.                             /* number of chars in front of the current insert */
  77. #ifdef INSERT_EXPAND
  78. static char_u    *original_text = NULL;
  79.                             /* Original text typed before completion */
  80. #endif
  81.  
  82. #ifdef CINDENT
  83. static int        can_cindent;    /* may do cindenting on this line */
  84. #endif
  85.  
  86. /*
  87.  * edit() returns TRUE if it returns because of a CTRL-O command
  88.  */
  89.     int
  90. edit(initstr, startln, count)
  91.     int            initstr;
  92.     int         startln;        /* if set, insert at start of line */
  93.     long         count;
  94. {
  95.     int             c;
  96.     int             cc;
  97.     char_u        *ptr;
  98.     linenr_t     lnum;
  99.     int          temp = 0;
  100.     int             mode;
  101.     int             lastc = 0;
  102.     colnr_t         mincol;
  103.     static linenr_t o_lnum = 0;
  104.     static int     o_eol = FALSE;
  105.     int             need_redraw = FALSE;
  106.     int             i;
  107.     int             did_backspace = TRUE;        /* previous char was backspace */
  108. #ifdef RIGHTLEFT
  109.     int            revins;                        /* reverse insert mode */
  110.     int            revinschars = 0;            /* how much to skip after edit */
  111.     int            revinslegal = 0;            /* was the last char 'legal'? */
  112.     int            revinsscol = -1;            /* start column of revins session */
  113. #endif
  114. #ifdef INSERT_EXPAND
  115.     FPOS         first_match_pos;
  116.     FPOS         last_match_pos;
  117.     FPOS        *complete_pos;
  118.     char_u        *complete_pat = NULL;
  119.     char_u        *tmp_ptr;
  120.     char_u        *mesg = NULL;                /* Message about completion */
  121.     int             started_completion = FALSE;
  122.     colnr_t         complete_col = 0;            /* init for gcc */
  123.     int             complete_direction;
  124.     int             done_dir = 0;                /* Found all matches in this
  125.                                              * direction */
  126.     int             num_matches;
  127.     char_u        **matches;
  128.     regexp        *prog;
  129.     int             save_sm = -1;                /* init for gcc */
  130.     int             save_p_scs;
  131. #endif
  132. #ifdef CINDENT
  133.     int             line_is_white = FALSE;        /* line is empty before insert */
  134. #endif
  135.     FPOS         tpos;
  136.  
  137. #ifdef USE_MOUSE
  138.     /*
  139.      * When doing a paste with the middle mouse button, Insstart is set to
  140.      * where the paste started.
  141.      */
  142.     if (where_paste_started.lnum != 0)
  143.         Insstart = where_paste_started;
  144.     else
  145. #endif
  146.     {
  147.         Insstart = curwin->w_cursor;
  148.         if (startln)
  149.             Insstart.col = 0;
  150.     }
  151.     Insstart_textlen = linetabsize(ml_get_curline());
  152.     Insstart_blank_vcol = MAXCOL;
  153.  
  154.     if (initstr != NUL && !restart_edit)
  155.     {
  156.         ResetRedobuff();
  157.         AppendNumberToRedobuff(count);
  158.         AppendCharToRedobuff(initstr);
  159.         if (initstr == 'g')                    /* "gI" command */
  160.             AppendCharToRedobuff('I');
  161.         else if (initstr == 'r')            /* "r<CR>" command */
  162.             count = 1;                        /* insert only one <CR> */
  163.     }
  164.  
  165.     if (initstr == 'R')
  166.         State = REPLACE;
  167.     else
  168.         State = INSERT;
  169.  
  170. #ifdef USE_MOUSE
  171.     setmouse();
  172. #endif
  173.     clear_showcmd();
  174. #ifdef RIGHTLEFT
  175.     revins = (State == INSERT && p_ri);    /* there is no reverse replace mode */
  176.     if (revins)
  177.         undisplay_dollar();
  178. #endif
  179.  
  180.     /*
  181.      * When CTRL-O . is used to repeat an insert, we get here with
  182.      * restart_edit non-zero, but something in the stuff buffer
  183.      */
  184.     if (restart_edit && stuff_empty())
  185.     {
  186. #ifdef USE_MOUSE
  187.         /*
  188.          * After a paste we consider text typed to be part of the insert for
  189.          * the pasted text. You can backspace over the paste text too.
  190.          */
  191.         if (where_paste_started.lnum)
  192.             arrow_used = FALSE;
  193.         else
  194. #endif
  195.             arrow_used = TRUE;
  196.         restart_edit = 0;
  197.         /*
  198.          * If the cursor was after the end-of-line before the CTRL-O
  199.          * and it is now at the end-of-line, put it after the end-of-line
  200.          * (this is not correct in very rare cases).
  201.          * Also do this if curswant is greater than the current virtual column.
  202.          * Eg after "^O$" or "^O80|".
  203.          */
  204.         if (((o_eol && curwin->w_cursor.lnum == o_lnum) ||
  205.                                 curwin->w_curswant > curwin->w_virtcol) &&
  206.                 *(ptr = ml_get_curline() + curwin->w_cursor.col)
  207.                                                                     != NUL &&
  208.                 *(ptr + 1) == NUL)
  209.             ++curwin->w_cursor.col;
  210.     }
  211.     else
  212.     {
  213.         arrow_used = FALSE;
  214.         o_eol = FALSE;
  215.     }
  216. #ifdef USE_MOUSE
  217.     where_paste_started.lnum = 0;
  218. #endif
  219. #ifdef CINDENT
  220.     can_cindent = TRUE;
  221. #endif
  222.  
  223.     /*
  224.      * If 'showmode' is set, show the current (insert/replace/..) mode.
  225.      * A warning message for changing a readonly file is given here, before
  226.      * actually changing anything.  It's put after the mode, if any.
  227.      */
  228.     i = 0;
  229.     if (p_smd)
  230.         i = showmode();
  231.  
  232.     if (!p_im)
  233.         change_warning(i + 1);
  234.  
  235. #ifdef DIGRAPHS
  236.     do_digraph(-1);                    /* clear digraphs */
  237. #endif
  238.  
  239. /*
  240.  * Get the current length of the redo buffer, those characters have to be
  241.  * skipped if we want to get to the inserted characters.
  242.  */
  243.     ptr = get_inserted();
  244.     new_insert_skip = STRLEN(ptr);
  245.     vim_free(ptr);
  246.  
  247.     old_indent = 0;
  248.  
  249.     for (;;)
  250.     {
  251. #ifdef RIGHTLEFT
  252.         if (!revinslegal)
  253.             revinsscol = -1;        /* reset on illegal motions */
  254.         else
  255.             revinslegal = 0;
  256. #endif
  257.         if (arrow_used)        /* don't repeat insert when arrow key used */
  258.             count = 0;
  259.  
  260.             /* set curwin->w_curswant for next K_DOWN or K_UP */
  261.         if (!arrow_used)
  262.             curwin->w_set_curswant = TRUE;
  263.  
  264.             /* Figure out where the cursor is based on curwin->w_cursor. */
  265.         mincol = curwin->w_col;
  266.         i = curwin->w_row;
  267.         cursupdate();
  268.  
  269.         /*
  270.          * When emsg() was called msg_scroll will have been set.
  271.          */
  272.         msg_scroll = FALSE;
  273.  
  274.         /*
  275.          * If we inserted a character at the last position of the last line in
  276.          * the window, scroll the window one line up. This avoids an extra
  277.          * redraw.
  278.          * This is detected when the cursor column is smaller after inserting
  279.          * something.
  280.          */
  281.         if (curwin->w_p_wrap && !did_backspace &&
  282.                           (int)curwin->w_col < (int)mincol - curbuf->b_p_ts &&
  283.                                i == curwin->w_winpos + curwin->w_height - 1 &&
  284.                                    curwin->w_cursor.lnum != curwin->w_topline)
  285.         {
  286.             ++curwin->w_topline;
  287.             updateScreen(VALID_TO_CURSCHAR);
  288.             cursupdate();
  289.             need_redraw = FALSE;
  290.         }
  291.         did_backspace = FALSE;
  292.  
  293.         /*
  294.          * redraw is postponed until after cursupdate() to make 'dollar'
  295.          * option work correctly.
  296.          */
  297.         if (need_redraw)
  298.         {
  299.             updateline();
  300.             need_redraw = FALSE;
  301.         }
  302.  
  303.         showruler(0);
  304.         setcursor();
  305.         emsg_on_display = FALSE;        /* may remove error message now */
  306.  
  307.         c = vgetc();
  308.  
  309. #ifdef RIGHTLEFT
  310.         if (p_hkmap && KeyTyped)
  311.             c = hkmap(c);                /* Hebrew mode mapping */
  312. #endif
  313.  
  314. #ifdef INSERT_EXPAND
  315.         if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
  316.         {
  317.             /* We have just entered ctrl-x mode and aren't quite sure which
  318.              * ctrl-x mode it will be yet.  Now we decide -- webb
  319.              */
  320.             switch (c)
  321.             {
  322.                 case Ctrl('E'):
  323.                 case Ctrl('Y'):
  324.                     ctrl_x_mode = CTRL_X_SCROLL;
  325.                     if (State == INSERT)
  326.                         edit_submode = (char_u *)" (insert) Scroll (^E/^Y)";
  327.                     else
  328.                         edit_submode = (char_u *)" (replace) Scroll (^E/^Y)";
  329.                     break;
  330.                 case Ctrl('L'):
  331.                     ctrl_x_mode = CTRL_X_WHOLE_LINE;
  332.                     edit_submode = (char_u *)" Whole line completion (^L/^N/^P)";
  333.                     break;
  334.                 case Ctrl('F'):
  335.                     ctrl_x_mode = CTRL_X_FILES;
  336.                     edit_submode = (char_u *)" File name completion (^F/^N/^P)";
  337.                     break;
  338.                 case Ctrl('K'):
  339.                     ctrl_x_mode = CTRL_X_DICTIONARY;
  340.                     edit_submode = (char_u *)" Dictionary completion (^K/^N/^P)";
  341.                     break;
  342.                 case Ctrl(']'):
  343.                     ctrl_x_mode = CTRL_X_TAGS;
  344.                     edit_submode = (char_u *)" Tag completion (^]/^N/^P)";
  345.                     break;
  346.                 case Ctrl('I'):
  347.                     ctrl_x_mode = CTRL_X_PATH_PATTERNS;
  348.                     edit_submode = (char_u *)" Path pattern completion (^N/^P)";
  349.                     break;
  350.                 case Ctrl('D'):
  351.                     ctrl_x_mode = CTRL_X_PATH_DEFINES;
  352.                     edit_submode = (char_u *)" Definition completion (^D/^N/^P)";
  353.                     break;
  354.                 default:
  355.                     ctrl_x_mode = 0;
  356.                     edit_submode = NULL;
  357.                     break;
  358.             }
  359.             showmode();
  360.         }
  361.         else if (ctrl_x_mode)
  362.         {
  363.             /* We we're already in ctrl-x mode, do we stay in it? */
  364.             if (!is_ctrl_x_key(c))
  365.             {
  366.                 if (ctrl_x_mode == CTRL_X_SCROLL)
  367.                     ctrl_x_mode = 0;
  368.                 else
  369.                     ctrl_x_mode = CTRL_X_FINISHED;
  370.                 edit_submode = NULL;
  371.             }
  372.             showmode();
  373.         }
  374.         if (started_completion || ctrl_x_mode == CTRL_X_FINISHED)
  375.         {
  376.             /* Show error message from attempted keyword completion (probably
  377.              * 'Pattern not found') until another key is hit, then go back to
  378.              * showing what mode we are in.
  379.              */
  380.             showmode();
  381.             if ((ctrl_x_mode == 0 && c != Ctrl('N') && c != Ctrl('P')) ||
  382.                                                 ctrl_x_mode == CTRL_X_FINISHED)
  383.             {
  384.                 /* Get here when we have finished typing a sequence of ^N and
  385.                  * ^P or other completion characters in CTRL-X mode. Free up
  386.                  * memory that was used, and make sure we can redo the insert
  387.                  * -- webb.
  388.                  */
  389.                 if (curr_match != NULL)
  390.                 {
  391.                     /*
  392.                      * If any of the original typed text has been changed,
  393.                      * eg when ignorecase is set, we must add back-spaces to
  394.                      * the redo buffer.  We add as few as necessary to delete
  395.                      * just the part of the original text that has changed
  396.                      * -- webb
  397.                      */
  398.                     ptr = curr_match->str;
  399.                     tmp_ptr = original_text;
  400.                     while (*tmp_ptr && *tmp_ptr == *ptr)
  401.                     {
  402.                         ++tmp_ptr;
  403.                         ++ptr;
  404.                     }
  405.                     for (temp = 0; tmp_ptr[temp]; ++temp)
  406.                         AppendCharToRedobuff(K_BS);
  407.                     if (*ptr)
  408.                         AppendToRedobuff(ptr);
  409.                 }
  410.                 /* Break line if it's too long */
  411.                 lnum = curwin->w_cursor.lnum;
  412.                 insertchar(NUL, FALSE, -1);
  413.                 if (lnum != curwin->w_cursor.lnum)
  414.                     updateScreen(CURSUPD);
  415.                 else
  416.                     need_redraw = TRUE;
  417.  
  418.                 vim_free(complete_pat);
  419.                 complete_pat = NULL;
  420.                 vim_free(original_text);
  421.                 original_text = NULL;
  422.                 free_completions();
  423.                 started_completion = FALSE;
  424.                 ctrl_x_mode = 0;
  425.                 p_sm = save_sm;
  426.                 if (edit_submode != NULL)
  427.                 {
  428.                     edit_submode = NULL;
  429.                     showmode();
  430.                 }
  431.             }
  432.         }
  433. #endif /* INSERT_EXPAND */
  434.  
  435.         if (c != Ctrl('D'))            /* remember to detect ^^D and 0^D */
  436.             lastc = c;
  437.  
  438. #ifdef DIGRAPHS
  439.         c = do_digraph(c);
  440. #endif /* DIGRAPHS */
  441.  
  442.         if (c == Ctrl('V') || c == Ctrl('Q'))
  443.         {
  444.             if (NextScreen != NULL)
  445.                 screen_outchar('^', curwin->w_winpos + curwin->w_row,
  446. #ifdef RIGHTLEFT
  447.                     curwin->w_p_rl ? (int)Columns - 1 - curwin->w_col :
  448. #endif
  449.                                                                curwin->w_col);
  450.             AppendToRedobuff((char_u *)"\026");    /* CTRL-V */
  451.             cursupdate();
  452.  
  453.             if (!add_to_showcmd(c, FALSE))
  454.                 setcursor();
  455.  
  456.             c = get_literal();
  457.             clear_showcmd();
  458.             insert_special(c, TRUE);
  459.             need_redraw = TRUE;
  460. #ifdef RIGHTLEFT
  461.             revinschars++;
  462.             revinslegal++;
  463. #endif
  464.             continue;
  465.         }
  466.  
  467. #ifdef CINDENT
  468.         if (curbuf->b_p_cin
  469. # ifdef INSERT_EXPAND
  470.                             && !ctrl_x_mode
  471. # endif
  472.                                                )
  473.         {
  474.             line_is_white = inindent(0);
  475.  
  476.             /* 
  477.              * A key name preceded by a bang means that this
  478.              * key wasn't destined to be inserted.  Skip ahead
  479.              * to the re-indenting if we find one.
  480.              */
  481.             if (in_cinkeys(c, '!', line_is_white))
  482.                 goto force_cindent;
  483.  
  484.             /* 
  485.              * A key name preceded by a star means that indenting
  486.              * has to be done before inserting the key.
  487.              */
  488.             if (can_cindent && in_cinkeys(c, '*', line_is_white))
  489.             {
  490.                 stop_arrow();
  491.  
  492.                 /* re-indent the current line */
  493.                 fixthisline(get_c_indent);
  494.  
  495.                 /* draw the changes on the screen later */
  496.                 need_redraw = TRUE;
  497.             }
  498.         }
  499. #endif /* CINDENT */
  500.  
  501. #ifdef RIGHTLEFT
  502.         if (curwin->w_p_rl)
  503.             switch (c)
  504.             {
  505.                 case K_LEFT:    c = K_RIGHT; break;
  506.                 case K_S_LEFT:    c = K_S_RIGHT; break;
  507.                 case K_RIGHT:    c = K_LEFT; break;
  508.                 case K_S_RIGHT: c = K_S_LEFT; break;
  509.             }
  510. #endif
  511.  
  512.         switch (c)        /* handle character in insert mode */
  513.         {
  514.               case K_INS:            /* toggle insert/replace mode */
  515.                 if (State == REPLACE)
  516.                     State = INSERT;
  517.                 else
  518.                     State = REPLACE;
  519.                 AppendCharToRedobuff(K_INS);
  520.                 showmode();
  521.                 break;
  522.  
  523. #ifdef INSERT_EXPAND
  524.               case Ctrl('X'):        /* Enter ctrl-x mode */
  525.                 /* We're not sure which ctrl-x mode it will be yet */
  526.                 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
  527.                 edit_submode = (char_u *)"^X mode (^E/^Y/^L/^]/^F/^I/^K/^D)";
  528.                 showmode();
  529.                 break;
  530. #endif /* INSERT_EXPAND */
  531.  
  532.               case Ctrl('O'):        /* execute one command */
  533.                 if (echeck_abbr(Ctrl('O') + ABBR_OFF))
  534.                     break;
  535.                   count = 0;
  536.                 if (State == INSERT)
  537.                     restart_edit = 'I';
  538.                 else
  539.                     restart_edit = 'R';
  540.                 o_lnum = curwin->w_cursor.lnum;
  541.                 o_eol = (gchar_cursor() == NUL);
  542.                 goto doESCkey;
  543.  
  544.               /* Hitting the help key in insert mode is like <ESC> <Help> */
  545.               case K_HELP:
  546.               case K_F1:
  547.                   stuffcharReadbuff(K_HELP);
  548.                 /*FALLTHROUGH*/
  549.  
  550.               case ESC:             /* an escape ends input mode */
  551.                 if (echeck_abbr(ESC + ABBR_OFF))
  552.                     break;
  553.                 /*FALLTHROUGH*/
  554.  
  555.               case Ctrl('C'):
  556. doESCkey:
  557.                 temp = curwin->w_cursor.col;
  558.                 if (!arrow_used)
  559.                 {
  560.                     /*
  561.                      * Don't append the ESC for "r<CR>".
  562.                      */
  563.                     if (initstr != 'r')
  564.                         AppendToRedobuff(ESC_STR);
  565.  
  566.                     /*
  567.                      * Repeating insert may take a long time.  Check for
  568.                      * interrupt now and then.
  569.                      */
  570.                     if (count)
  571.                     {
  572.                         line_breakcheck();
  573.                         if (got_int)
  574.                             count = 0;
  575.                     }
  576.  
  577.                     if (--count > 0)        /* repeat what was typed */
  578.                     {
  579.                         (void)start_redo_ins();
  580.                         continue;
  581.                     }
  582.                     stop_insert(&curwin->w_cursor);
  583.                     if (dollar_vcol)
  584.                     {
  585.                         dollar_vcol = 0;
  586.                         /* may have to redraw status line if this was the
  587.                          * first change, show "[+]" */
  588.                         if (curwin->w_redr_status == TRUE)
  589.                             must_redraw = NOT_VALID;
  590.                         else
  591.                             need_redraw = TRUE;
  592.                     }
  593.                 }
  594.                 if (need_redraw)
  595.                     updateline();
  596.  
  597.                 /* When an autoindent was removed, curswant stays after the
  598.                  * indent */
  599.                 if (!restart_edit && (colnr_t)temp == curwin->w_cursor.col)
  600.                     curwin->w_set_curswant = TRUE;
  601.  
  602.                 /*
  603.                  * The cursor should end up on the last inserted character.
  604.                  */
  605.                 if (curwin->w_cursor.col != 0 &&
  606.                           (!restart_edit || gchar_cursor() == NUL)
  607. #ifdef RIGHTLEFT
  608.                                       && !revins
  609. #endif
  610.                                                   )
  611.                     --curwin->w_cursor.col;
  612.                 if (State == REPLACE)
  613.                     replace_flush();    /* free replace stack */
  614.                 State = NORMAL;
  615. #ifdef USE_MOUSE
  616.                 setmouse();
  617. #endif
  618.                     /* inchar() may have deleted the "INSERT" message */
  619.                     /* for CTRL-O we display -- INSERT COMMAND -- */
  620.                 if (Recording || restart_edit)
  621.                     showmode();
  622.                 else if (p_smd)
  623.                     MSG("");
  624.                 old_indent = 0;
  625.  
  626.                 /*
  627.                  * This is the ONLY return from edit().
  628.                  */
  629.                 return (c == Ctrl('O'));
  630.  
  631.                   /*
  632.                  * Insert the previously inserted text.
  633.                  * For ^@ the trailing ESC will end the insert, unless there
  634.                  * is an error.
  635.                  */
  636.               case K_ZERO:
  637.               case NUL:
  638.               case Ctrl('A'):
  639.                 if (stuff_inserted(NUL, 1L, (c == Ctrl('A'))) == FAIL &&
  640.                                                                c != Ctrl('A'))
  641.                     goto doESCkey;            /* quit insert mode */
  642.                 break;
  643.  
  644.                   /*
  645.                  * insert the contents of a register
  646.                  */
  647.               case Ctrl('R'):
  648.                 if (NextScreen != NULL)
  649.                     screen_outchar('"', curwin->w_winpos + curwin->w_row,
  650. #ifdef RIGHTLEFT
  651.                         curwin->w_p_rl ? (int)Columns - 1 - curwin->w_col :
  652. #endif
  653.                                                             curwin->w_col);
  654.                 if (!add_to_showcmd(c, FALSE))
  655.                     setcursor();
  656.                     /* don't map the register name. This also prevents the
  657.                      * mode message to be deleted when ESC is hit */
  658.                 ++no_mapping;
  659. #ifdef HAVE_LANGMAP
  660.                 cc = vgetc();
  661.                 LANGMAP_ADJUST(cc, TRUE);
  662.                   if (insertbuf(cc) == FAIL)
  663. #else
  664.                   if (insertbuf(vgetc()) == FAIL)
  665. #endif
  666.                 {
  667.                     beep_flush();
  668.                     need_redraw = TRUE;        /* remove the '"' */
  669.                 }
  670.                 --no_mapping;
  671.                 clear_showcmd();
  672.                 break;
  673.  
  674. #ifdef RIGHTLEFT
  675.               case Ctrl('B'):            /* toggle reverse insert mode */
  676.                   p_ri = !p_ri;
  677.                 revins = (State == INSERT && p_ri);
  678.                 if (revins)
  679.                     undisplay_dollar();
  680.                 showmode();
  681.                 break;
  682.  
  683.               case Ctrl('_'):        /* toggle language: khmap and revins */
  684.                                     /* Move to end of reverse inserted text */
  685.                 if (revins && revinschars && revinsscol >= 0)
  686.                     while (gchar_cursor() != NUL && revinschars--)
  687.                         ++curwin->w_cursor.col;
  688.                 p_ri = !p_ri;
  689.                 revins = (State == INSERT && p_ri);
  690.                 if (revins)
  691.                 {
  692.                     revinsscol = curwin->w_cursor.col;
  693.                     revinslegal++;
  694.                     revinschars = 0;
  695.                     undisplay_dollar();
  696.                 }
  697.                 else
  698.                     revinsscol = -1;
  699.                 p_hkmap = curwin->w_p_rl ^ p_ri;    /* be consistent! */
  700.                 showmode();
  701.                 break;
  702. #endif
  703.  
  704.                 /*
  705.                  * If the cursor is on an indent, ^T/^D insert/delete one
  706.                  * shiftwidth.  Otherwise ^T/^D behave like a "<<" or ">>".
  707.                  * Always round the indent to 'shiftwith', this is compatible
  708.                  * with vi.  But vi only supports ^T and ^D after an
  709.                  * autoindent, we support it everywhere.
  710.                  */
  711.               case Ctrl('D'):         /* make indent one shiftwidth smaller */
  712. #ifdef INSERT_EXPAND
  713.                 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
  714.                     goto docomplete;
  715. #endif /* INSERT_EXPAND */
  716.                 /* FALLTHROUGH */
  717.               case Ctrl('T'):        /* make indent one shiftwidth greater */
  718.                 stop_arrow();
  719.                 AppendCharToRedobuff(c);
  720.  
  721.                 /*
  722.                  * 0^D and ^^D: remove all indent.
  723.                  */
  724.                 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
  725.                 {
  726.                     --curwin->w_cursor.col;
  727.                     (void)delchar(FALSE);            /* delete the '^' or '0' */
  728.                     /*
  729.                      * In Replace mode, restore the character that '^' or '0'
  730.                      * replaced.
  731.                      */
  732.                     if (State == REPLACE && (i = replace_pop()) >= 0)
  733.                     {
  734.                         State = INSERT;
  735.                         ins_char(i);
  736.                         --curwin->w_cursor.col;
  737.                         State = REPLACE;
  738.                     }
  739.                     if (lastc == '^')
  740.                         old_indent = get_indent();    /* remember curr. indent */
  741.                     change_indent(INDENT_SET, 0, TRUE);
  742.                 }
  743.                 else
  744.                     change_indent(c == Ctrl('D') ? INDENT_DEC : INDENT_INC,
  745.                                                                      0, TRUE);
  746.  
  747.                 did_ai = FALSE;
  748.                 did_si = FALSE;
  749.                 can_si = FALSE;
  750.                 can_si_back = FALSE;
  751. #ifdef CINDENT
  752.                 can_cindent = FALSE;        /* no cindenting after ^D or ^T */
  753. #endif
  754.                   goto redraw;
  755.  
  756.               case K_DEL:
  757.                 stop_arrow();
  758.                   if (gchar_cursor() == NUL)        /* delete newline */
  759.                 {
  760.                     temp = curwin->w_cursor.col;
  761.                     if (!p_bs ||                /* only if 'bs' set */
  762.                         u_save((linenr_t)(curwin->w_cursor.lnum - 1),
  763.                             (linenr_t)(curwin->w_cursor.lnum + 2)) == FAIL ||
  764.                                 do_join(FALSE, TRUE) == FAIL)
  765.                         beep_flush();
  766.                     else
  767.                         curwin->w_cursor.col = temp;
  768.                 }
  769.                 else if (delchar(FALSE) == FAIL)/* delete char under cursor */
  770.                     beep_flush();
  771.                 did_ai = FALSE;
  772.                 did_si = FALSE;
  773.                 can_si = FALSE;
  774.                 can_si_back = FALSE;
  775.                 AppendCharToRedobuff(c);
  776.                 goto redraw;
  777.  
  778.               case K_BS:
  779.               case Ctrl('H'):
  780.                 mode = BACKSPACE_CHAR;
  781. dodel:
  782.                 /* can't delete anything in an empty file */
  783.                 /* can't backup past first character in buffer */
  784.                 /* can't backup past starting point unless 'backspace' > 1 */
  785.                 /* can backup to a previous line if 'backspace' == 0 */
  786.                 if (bufempty() || (
  787. #ifdef RIGHTLEFT
  788.                         !revins &&
  789. #endif
  790.                         ((curwin->w_cursor.lnum == 1 &&
  791.                                     curwin->w_cursor.col <= 0) ||
  792.                         (p_bs < 2 && (arrow_used ||
  793.                             (curwin->w_cursor.lnum == Insstart.lnum &&
  794.                             curwin->w_cursor.col <= Insstart.col) ||
  795.                             (curwin->w_cursor.col <= 0 && p_bs == 0))))))
  796.                 {
  797.                     beep_flush();
  798.                     goto redraw;
  799.                 }
  800.  
  801.                 stop_arrow();
  802. #ifdef CINDENT
  803.                 if (inindent(0))
  804.                     can_cindent = FALSE;
  805. #endif
  806. #ifdef RIGHTLEFT
  807.                 if (revins)            /* put cursor after last inserted char */
  808.                     inc_cursor();
  809. #endif
  810.                 if (curwin->w_cursor.col <= 0)        /* delete newline! */
  811.                 {
  812.                     lnum = Insstart.lnum;
  813.                     if (curwin->w_cursor.lnum == Insstart.lnum
  814. #ifdef RIGHTLEFT
  815.                                     || revins
  816. #endif
  817.                                                 )
  818.                     {
  819.                         if (u_save((linenr_t)(curwin->w_cursor.lnum - 2),
  820.                                 (linenr_t)(curwin->w_cursor.lnum + 1)) == FAIL)
  821.                             goto redraw;
  822.                         --Insstart.lnum;
  823.                         Insstart.col = 0;
  824.                     }
  825.                     /*
  826.                      * In replace mode:
  827.                      * cc < 0: NL was inserted, delete it
  828.                      * cc >= 0: NL was replaced, put original characters back
  829.                      */
  830.                     cc = -1;
  831.                     if (State == REPLACE)
  832.                         cc = replace_pop();
  833.                 /* in replace mode, in the line we started replacing, we
  834.                                                         only move the cursor */
  835.                     if (State != REPLACE || curwin->w_cursor.lnum > lnum)
  836.                     {
  837.                         temp = gchar_cursor();        /* remember current char */
  838.                         --curwin->w_cursor.lnum;
  839.                         (void)do_join(FALSE, curs_rows() == OK);
  840.                         if (temp == NUL && gchar_cursor() != NUL)
  841.                             ++curwin->w_cursor.col;
  842.                         /*
  843.                          * in REPLACE mode we have to put back the text that
  844.                          * was replace by the NL. On the replace stack is
  845.                          * first a NUL-terminated sequence of characters that
  846.                          * were deleted and then the character that NL
  847.                          * replaced.
  848.                          */
  849.                         if (State == REPLACE)
  850.                         {
  851.                             /*
  852.                              * Do the next ins_char() in NORMAL state, to
  853.                              * prevent ins_char() from replacing characters and
  854.                              * avoiding showmatch().
  855.                              */
  856.                             State = NORMAL;
  857.                             /*
  858.                              * restore blanks deleted after cursor
  859.                              */
  860.                             while (cc > 0)
  861.                             {
  862.                                 temp = curwin->w_cursor.col;
  863.                                 ins_char(cc);
  864.                                 curwin->w_cursor.col = temp;
  865.                                 cc = replace_pop();
  866.                             }
  867.                             cc = replace_pop();
  868.                             if (cc > 0)
  869.                             {
  870.                                 ins_char(cc);
  871.                                 dec_cursor();
  872.                             }
  873.                             State = REPLACE;
  874.                         }
  875.                     }
  876.                     else
  877.                         dec_cursor();
  878.                     did_ai = FALSE;
  879.                 }
  880.                 else
  881.                 {
  882. #ifdef RIGHTLEFT
  883.                     if (revins)            /* put cursor on last inserted char */
  884.                         dec_cursor();
  885. #endif
  886.                     mincol = 0;
  887.                                                             /* keep indent */
  888.                     if (mode == BACKSPACE_LINE && curbuf->b_p_ai
  889. #ifdef RIGHTLEFT
  890.                             && !revins
  891. #endif
  892.                                         )
  893.                     {
  894.                         temp = curwin->w_cursor.col;
  895.                         beginline(TRUE);
  896.                         if (curwin->w_cursor.col < (colnr_t)temp)
  897.                             mincol = curwin->w_cursor.col;
  898.                         curwin->w_cursor.col = temp;
  899.                     }
  900.  
  901.                     /* delete upto starting point, start of line or previous
  902.                      * word */
  903.                     do
  904.                     {
  905. #ifdef RIGHTLEFT
  906.                         if (!revins)    /* put cursor on char to be deleted */
  907. #endif
  908.                             dec_cursor();
  909.  
  910.                                 /* start of word? */
  911.                         if (mode == BACKSPACE_WORD &&
  912.                                                 !vim_isspace(gchar_cursor()))
  913.                         {
  914.                             mode = BACKSPACE_WORD_NOT_SPACE;
  915.                             temp = iswordchar(gchar_cursor());
  916.                         }
  917.                                 /* end of word? */
  918.                         else if (mode == BACKSPACE_WORD_NOT_SPACE &&
  919.                                           (vim_isspace(cc = gchar_cursor()) ||
  920.                                                       iswordchar(cc) != temp))
  921.                         {
  922. #ifdef RIGHTLEFT
  923.                             if (!revins)
  924. #endif
  925.                                 inc_cursor();
  926. #ifdef RIGHTLEFT
  927.                             else if (State == REPLACE)
  928.                                 dec_cursor();
  929. #endif
  930.                             break;
  931.                         }
  932.                         if (State == REPLACE)
  933.                         {
  934.                             /*
  935.                              * cc < 0: replace stack empty, just move cursor
  936.                              * cc == 0: character was inserted, delete it
  937.                              * cc > 0: character was replace, put original back
  938.                              */
  939.                             cc = replace_pop();
  940.                             if (cc > 0)
  941.                                 pchar_cursor(cc);
  942.                             else if (cc == 0)
  943.                                 (void)delchar(FALSE);
  944.                         }
  945.                         else  /* State != REPLACE */
  946.                         {
  947.                             (void)delchar(FALSE);
  948. #ifdef RIGHTLEFT
  949.                             if (revinschars)
  950.                             {
  951.                                 revinschars--;
  952.                                 revinslegal++;
  953.                             }
  954.                             if (revins && gchar_cursor() == NUL)
  955.                                 break;
  956. #endif
  957.                         }
  958.                         /* Just a single backspace?: */
  959.                         if (mode == BACKSPACE_CHAR)
  960.                             break;
  961.                     } while (
  962. #ifdef RIGHTLEFT
  963.                             revins ||
  964. #endif
  965.                             (curwin->w_cursor.col > mincol &&
  966.                             (curwin->w_cursor.lnum != Insstart.lnum ||
  967.                             curwin->w_cursor.col != Insstart.col)));
  968.                     did_backspace = TRUE;
  969.                 }
  970.                 did_si = FALSE;
  971.                 can_si = FALSE;
  972.                 can_si_back = FALSE;
  973.                 if (curwin->w_cursor.col <= 1)
  974.                     did_ai = FALSE;
  975.                 /*
  976.                  * It's a little strange to put backspaces into the redo
  977.                  * buffer, but it makes auto-indent a lot easier to deal
  978.                  * with.
  979.                  */
  980.                 AppendCharToRedobuff(c);
  981. redraw:
  982.                 need_redraw = TRUE;
  983.                 break;
  984.  
  985.               case Ctrl('W'):        /* delete word before cursor */
  986.                   mode = BACKSPACE_WORD;
  987.                   goto dodel;
  988.  
  989.               case Ctrl('U'):        /* delete inserted text in current line */
  990.                 mode = BACKSPACE_LINE;
  991.                   goto dodel;
  992.  
  993. #ifdef USE_MOUSE
  994.               case K_LEFTMOUSE:
  995.               case K_LEFTDRAG:
  996.               case K_LEFTRELEASE:
  997.               case K_MIDDLEMOUSE:
  998.               case K_MIDDLEDRAG:
  999.               case K_MIDDLERELEASE:
  1000.               case K_RIGHTMOUSE:
  1001.               case K_RIGHTDRAG:
  1002.               case K_RIGHTRELEASE:
  1003. #ifdef USE_GUI
  1004.                 /* When GUI is active, also move/paste when 'mouse' is empty */
  1005.                 if (!gui.in_use)
  1006. #endif
  1007.                     if (!mouse_has(MOUSE_INSERT))
  1008.                         break;
  1009.  
  1010.                 undisplay_dollar();
  1011.                 tpos = curwin->w_cursor;
  1012.                 if (do_mouse(c, BACKWARD, 1L, FALSE))
  1013.                 {
  1014.                     start_arrow(&tpos);
  1015. # ifdef CINDENT
  1016.                     can_cindent = TRUE;
  1017. # endif
  1018.                 }
  1019.  
  1020.                 break;
  1021.  
  1022.               case K_IGNORE:
  1023.                 break;
  1024. #endif
  1025.  
  1026. #ifdef USE_GUI
  1027.               case K_SCROLLBAR:
  1028.                 undisplay_dollar();
  1029.                 tpos = curwin->w_cursor;
  1030.                 if (gui_do_scroll())
  1031.                 {
  1032.                     start_arrow(&tpos);
  1033. # ifdef CINDENT
  1034.                     can_cindent = TRUE;
  1035. # endif
  1036.                 }
  1037.                 break;
  1038.  
  1039.               case K_HORIZ_SCROLLBAR:
  1040.                 undisplay_dollar();
  1041.                 tpos = curwin->w_cursor;
  1042.                 if (gui_do_horiz_scroll())
  1043.                 {
  1044.                     start_arrow(&tpos);
  1045. #ifdef CINDENT
  1046.                     can_cindent = TRUE;
  1047. #endif
  1048.                 }
  1049.                 break;
  1050. #endif
  1051.  
  1052.               case K_LEFT:
  1053.                 undisplay_dollar();
  1054.                 tpos = curwin->w_cursor;
  1055.                   if (oneleft() == OK)
  1056.                 {
  1057.                     start_arrow(&tpos);
  1058. #ifdef RIGHTLEFT
  1059.                     /* If exit reversed string, position is fixed */
  1060.                     if (revinsscol != -1 &&
  1061.                                       (int)curwin->w_cursor.col >= revinsscol)
  1062.                         revinslegal++;
  1063.                     revinschars++;
  1064. #endif
  1065.                 }
  1066.  
  1067.                 /*
  1068.                  * if 'whichwrap' set for cursor in insert mode may go to
  1069.                  * previous line
  1070.                  */
  1071.                 else if (vim_strchr(p_ww, '[') != NULL &&
  1072.                                                     curwin->w_cursor.lnum > 1)
  1073.                 {
  1074.                     start_arrow(&tpos);
  1075.                     --(curwin->w_cursor.lnum);
  1076.                     coladvance(MAXCOL);
  1077.                     curwin->w_set_curswant = TRUE;    /* so we stay at the end */
  1078.                 }
  1079.                 else
  1080.                     beep_flush();
  1081.                 break;
  1082.  
  1083.               case K_HOME:
  1084.               case K_KHOME:
  1085.                 undisplay_dollar();
  1086.                 tpos = curwin->w_cursor;
  1087.                 if ((mod_mask & MOD_MASK_CTRL))
  1088.                     curwin->w_cursor.lnum = 1;
  1089.                   curwin->w_cursor.col = 0;
  1090.                 curwin->w_curswant = 0;
  1091.                 start_arrow(&tpos);
  1092.                 break;
  1093.  
  1094.               case K_END:
  1095.               case K_KEND:
  1096.                 undisplay_dollar();
  1097.                 tpos = curwin->w_cursor;
  1098.                 if ((mod_mask & MOD_MASK_CTRL))
  1099.                     curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
  1100.                 coladvance(MAXCOL);
  1101.                 curwin->w_curswant = MAXCOL;
  1102.                 start_arrow(&tpos);
  1103.                 break;
  1104.  
  1105.               case K_S_LEFT:
  1106.                 undisplay_dollar();
  1107.                   if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
  1108.                 {
  1109.                     start_arrow(&curwin->w_cursor);
  1110.                     (void)bck_word(1L, 0, FALSE);
  1111.                 }
  1112.                 else
  1113.                     beep_flush();
  1114.                 break;
  1115.  
  1116.               case K_RIGHT:
  1117.                 undisplay_dollar();
  1118.                 if (gchar_cursor() != NUL)
  1119.                 {
  1120.                     start_arrow(&curwin->w_cursor);
  1121.                     curwin->w_set_curswant = TRUE;
  1122.                     ++curwin->w_cursor.col;
  1123. #ifdef RIGHTLEFT
  1124.                     revinslegal++;
  1125.                     if (revinschars)
  1126.                         revinschars--;
  1127. #endif
  1128.                 }
  1129.                     /* if 'whichwrap' set for cursor in insert mode may go
  1130.                      * to next line */
  1131.                 else if (vim_strchr(p_ww, ']') != NULL &&
  1132.                            curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
  1133.                 {
  1134.                     start_arrow(&curwin->w_cursor);
  1135.                     curwin->w_set_curswant = TRUE;
  1136.                     ++curwin->w_cursor.lnum;
  1137.                     curwin->w_cursor.col = 0;
  1138.                 }
  1139.                 else
  1140.                     beep_flush();
  1141.                 break;
  1142.  
  1143.               case K_S_RIGHT:
  1144.                 undisplay_dollar();
  1145.                   if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count ||
  1146.                                                         gchar_cursor() != NUL)
  1147.                 {
  1148.                     start_arrow(&curwin->w_cursor);
  1149.                     (void)fwd_word(1L, 0, 0);
  1150.                 }
  1151.                 else
  1152.                     beep_flush();
  1153.                 break;
  1154.  
  1155.               case K_UP:
  1156.                 undisplay_dollar();
  1157.                 tpos = curwin->w_cursor;
  1158.                   if (cursor_up(1L) == OK)
  1159.                 {
  1160.                     start_arrow(&tpos);
  1161. #ifdef CINDENT
  1162.                     can_cindent = TRUE;
  1163. #endif
  1164.                 }
  1165.                 else
  1166.                     beep_flush();
  1167.                 break;
  1168.  
  1169.               case K_S_UP:
  1170.               case K_PAGEUP:
  1171.               case K_KPAGEUP:
  1172.                 undisplay_dollar();
  1173.                 tpos = curwin->w_cursor;
  1174.                   if (onepage(BACKWARD, 1L) == OK)
  1175.                 {
  1176.                     start_arrow(&tpos);
  1177. #ifdef CINDENT
  1178.                     can_cindent = TRUE;
  1179. #endif
  1180.                 }
  1181.                 else
  1182.                     beep_flush();
  1183.                 break;
  1184.  
  1185.               case K_DOWN:
  1186.                 undisplay_dollar();
  1187.                 tpos = curwin->w_cursor;
  1188.                   if (cursor_down(1L) == OK)
  1189.                 {
  1190.                     start_arrow(&tpos);
  1191. #ifdef CINDENT
  1192.                     can_cindent = TRUE;
  1193. #endif
  1194.                 }
  1195.                 else
  1196.                     beep_flush();
  1197.                 break;
  1198.  
  1199.               case K_S_DOWN:
  1200.               case K_PAGEDOWN:
  1201.               case K_KPAGEDOWN:
  1202.                 undisplay_dollar();
  1203.                 tpos = curwin->w_cursor;
  1204.                   if (onepage(FORWARD, 1L) == OK)
  1205.                 {
  1206.                     start_arrow(&tpos);
  1207. #ifdef CINDENT
  1208.                     can_cindent = TRUE;
  1209. #endif
  1210.                 }
  1211.                 else
  1212.                     beep_flush();
  1213.                 break;
  1214.  
  1215.               case TAB:                /* TAB or Complete patterns along path */
  1216. #ifdef INSERT_EXPAND
  1217.                 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
  1218.                     goto docomplete;
  1219. #endif /* INSERT_EXPAND */
  1220.  
  1221.                 if (Insstart_blank_vcol == MAXCOL &&
  1222.                                        curwin->w_cursor.lnum == Insstart.lnum)
  1223.                     Insstart_blank_vcol = curwin->w_virtcol;
  1224.                 if (echeck_abbr(TAB + ABBR_OFF))
  1225.                     break;
  1226.                 i = inindent(0);
  1227. #ifdef CINDENT
  1228.                 if (i)
  1229.                     can_cindent = FALSE;
  1230. #endif
  1231.                   if (!curbuf->b_p_et && !(p_sta && i))
  1232.                     goto normalchar;
  1233.  
  1234.                 stop_arrow();
  1235.                 did_ai = FALSE;
  1236.                 did_si = FALSE;
  1237.                 can_si = FALSE;
  1238.                 can_si_back = FALSE;
  1239.                 AppendToRedobuff((char_u *)"\t");
  1240.  
  1241.                 if (p_sta && i)                    /* insert tab in indent */
  1242.                 {
  1243.                     if (State == REPLACE)        /* delete a char first */
  1244.                     {
  1245.                         i = gchar_cursor();
  1246.                         if (i)
  1247.                         {
  1248.                             replace_push(i);
  1249.                             delchar(FALSE);
  1250.                         }
  1251.                     }
  1252.                     change_indent(INDENT_INC, 0, p_sr);
  1253.                     goto redraw;
  1254.                 }
  1255.  
  1256.                 /*
  1257.                  * p_et is set: expand a tab into spaces
  1258.                  */
  1259.                 temp = (int)curbuf->b_p_ts;
  1260.                 temp -= curwin->w_virtcol % temp;
  1261.  
  1262.                 /*
  1263.                  * insert the first space with ins_char(); it will delete one
  1264.                  * char in replace mode. Insert the rest with ins_str(); it
  1265.                  * will not delete any chars
  1266.                  */
  1267.                 ins_char(' ');
  1268.                 while (--temp)
  1269.                 {
  1270.                     ins_str((char_u *)" ");
  1271.                     if (State == REPLACE)        /* no char replaced */
  1272.                         replace_push(NUL);
  1273.                 }
  1274.                 goto redraw;
  1275.  
  1276.               case CR:
  1277.               case NL:
  1278.                 if (echeck_abbr(c + ABBR_OFF))
  1279.                     break;
  1280.                 stop_arrow();
  1281.                 if (State == REPLACE)
  1282.                     replace_push(NUL);
  1283. #ifdef RIGHTLEFT
  1284.                 /* NL in reverse insert will allways start in the end of
  1285.                  * current line. */
  1286.                 if (revins)
  1287.                     while (gchar_cursor() != NUL)
  1288.                         ++curwin->w_cursor.col;
  1289. #endif
  1290.  
  1291.                 AppendToRedobuff(NL_STR);
  1292.                 if (has_format_option(FO_RET_COMS))
  1293.                     fo_do_comments = TRUE;
  1294.                 i = Opencmd(FORWARD, TRUE, FALSE);
  1295.                 fo_do_comments = FALSE;
  1296. #ifdef CINDENT
  1297.                 can_cindent = TRUE;
  1298. #endif
  1299.                 if (!i)
  1300.                     goto doESCkey;        /* out of memory */
  1301.                 break;
  1302.  
  1303. #ifdef DIGRAPHS
  1304.               case Ctrl('K'):
  1305. #ifdef INSERT_EXPAND
  1306.                 if (ctrl_x_mode == CTRL_X_DICTIONARY)
  1307.                     goto docomplete;
  1308. #endif
  1309.                 if (NextScreen != NULL)
  1310.                     screen_outchar('?', curwin->w_winpos + curwin->w_row,
  1311. #ifdef RIGHTLEFT
  1312.                         curwin->w_p_rl ? (int)Columns - 1 - curwin->w_col :
  1313. #endif
  1314.                                                             curwin->w_col);
  1315.                 if (!add_to_showcmd(c, FALSE))
  1316.                     setcursor();
  1317.                     /* don't map the digraph chars. This also prevents the
  1318.                      * mode message to be deleted when ESC is hit */
  1319.                 ++no_mapping;
  1320.                 ++allow_keys;
  1321.                   c = vgetc();
  1322.                 --no_mapping;
  1323.                 --allow_keys;
  1324.                 if (IS_SPECIAL(c))        /* special key */
  1325.                 {
  1326.                     clear_showcmd();
  1327.                     insert_special(c, TRUE);
  1328.                     need_redraw = TRUE;
  1329.                     break;
  1330.                 }
  1331.                 if (c != ESC)
  1332.                 {
  1333.                     if (charsize(c) == 1 && NextScreen != NULL)
  1334.                          screen_outchar(c, curwin->w_winpos + curwin->w_row,
  1335. #ifdef RIGHTLEFT
  1336.                             curwin->w_p_rl ? (int)Columns - 1 - curwin->w_col :
  1337. #endif
  1338.                                                                 curwin->w_col);
  1339.                     if (!add_to_showcmd(c, FALSE))
  1340.                         setcursor();
  1341.                     ++no_mapping;
  1342.                     ++allow_keys;
  1343.                     cc = vgetc();
  1344.                     --no_mapping;
  1345.                     --allow_keys;
  1346.                     if (cc != ESC)
  1347.                     {
  1348.                         AppendToRedobuff((char_u *)"\026");    /* CTRL-V */
  1349.                         c = getdigraph(c, cc, TRUE);
  1350.                         clear_showcmd();
  1351.                         goto normalchar;
  1352.                     }
  1353.                 }
  1354.                 clear_showcmd();
  1355.                 need_redraw = TRUE;
  1356.                 break;
  1357. #else /* DIGRAPHS */
  1358. # ifdef INSERT_EXPAND
  1359.               case Ctrl('K'):
  1360.                 if (ctrl_x_mode != CTRL_X_DICTIONARY)
  1361.                     goto normalchar;
  1362.                 goto docomplete;
  1363. # endif /* INSERT_EXPAND */
  1364. #endif /* DIGRAPHS */
  1365.  
  1366. #ifdef INSERT_EXPAND
  1367.               case Ctrl(']'):            /* Tag name completion after ^X */
  1368.                 if (ctrl_x_mode != CTRL_X_TAGS)
  1369.                     goto normalchar;
  1370.                 goto docomplete;
  1371.  
  1372.               case Ctrl('F'):            /* File name completion after ^X */
  1373.                 if (ctrl_x_mode != CTRL_X_FILES)
  1374.                     goto normalchar;
  1375.                 goto docomplete;
  1376.  
  1377.               case Ctrl('L'):            /* Whole line completion after ^X */
  1378.                 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
  1379.                     goto normalchar;
  1380.                 /* FALLTHROUGH */
  1381.  
  1382.               case Ctrl('P'):            /* Do previous pattern completion */
  1383.               case Ctrl('N'):            /* Do next pattern completion */
  1384. docomplete:
  1385.                 if (c == Ctrl('P') || c == Ctrl('L'))
  1386.                     complete_direction = BACKWARD;
  1387.                 else
  1388.                     complete_direction = FORWARD;
  1389.                 mesg = NULL;            /* No message by default */
  1390.                 if (!started_completion)
  1391.                 {
  1392.                     /* First time we hit ^N or ^P (in a row, I mean) */
  1393.  
  1394.                     /* Turn off 'sm' so we don't show matches with ^X^L */
  1395.                     save_sm = p_sm;
  1396.                     p_sm = FALSE;
  1397.  
  1398.                     if (ctrl_x_mode == 0)
  1399.                     {
  1400.                         edit_submode = (char_u *)" Keyword completion (^P/^N)";
  1401.                         showmode();
  1402.                     }
  1403.                     did_ai = FALSE;
  1404.                     did_si = FALSE;
  1405.                     can_si = FALSE;
  1406.                     can_si_back = FALSE;
  1407.                     stop_arrow();
  1408.                     done_dir = 0;
  1409.                     first_match_pos = curwin->w_cursor;
  1410.                     ptr = tmp_ptr = ml_get(first_match_pos.lnum);
  1411.                     complete_col = first_match_pos.col;
  1412.                     temp = (int)complete_col - 1;
  1413.  
  1414.                     /* Work out completion pattern and original text -- webb */
  1415.                     if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
  1416.                     {
  1417.                         if (temp < 0 || !iswordchar(ptr[temp]))
  1418.                         {
  1419.                             /* Match any word of at least two chars */
  1420.                             complete_pat = strsave((char_u *)"\\<\\k\\k");
  1421.                             if (complete_pat == NULL)
  1422.                                 break;
  1423.                             tmp_ptr += complete_col;
  1424.                             temp = 0;
  1425.                         }
  1426.                         else
  1427.                         {
  1428.                             while (temp >= 0 && iswordchar(ptr[temp]))
  1429.                                 temp--;
  1430.                             tmp_ptr += ++temp;
  1431.                             if ((temp = (int)complete_col - temp) == 1)
  1432.                             {
  1433.                                 /* Only match word with at least two
  1434.                                  * chars -- webb
  1435.                                  */
  1436.                                 sprintf((char *)IObuff, "\\<%c\\k", *tmp_ptr);
  1437.                                 complete_pat = strsave(IObuff);
  1438.                                 if (complete_pat == NULL)
  1439.                                     break;
  1440.                             }
  1441.                             else
  1442.                             {
  1443.                                 complete_pat = alloc(temp + 3);
  1444.                                 if (complete_pat == NULL)
  1445.                                     break;
  1446.                                 sprintf((char *)complete_pat, "\\<%.*s", temp,
  1447.                                                                     tmp_ptr);
  1448.                             }
  1449.                         }
  1450.                     }
  1451.                     else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
  1452.                     {
  1453.                         tmp_ptr = skipwhite(ptr);
  1454.                         temp = (int)complete_col - (tmp_ptr - ptr);
  1455.                         complete_pat = strnsave(tmp_ptr, temp);
  1456.                         if (complete_pat == NULL)
  1457.                             break;
  1458.                     }
  1459.                     else if (ctrl_x_mode == CTRL_X_FILES)
  1460.                     {
  1461.                         while (temp >= 0 && isfilechar(ptr[temp]))
  1462.                             temp--;
  1463.                         tmp_ptr += ++temp;
  1464.                         temp = (int)complete_col - temp;
  1465.                         complete_pat = addstar(tmp_ptr, temp);
  1466.                         if (complete_pat == NULL)
  1467.                             break;
  1468.                     }
  1469.                     original_text = strnsave(tmp_ptr, temp);
  1470.                     if (original_text == NULL)
  1471.                     {
  1472.                         vim_free(complete_pat);
  1473.                         complete_pat = NULL;
  1474.                         break;
  1475.                     }
  1476.  
  1477.                     complete_col = tmp_ptr - ptr;
  1478.                     first_match_pos.col -= temp;
  1479.  
  1480.                     /* So that ^N can match word immediately after cursor */
  1481.                     if (ctrl_x_mode == 0)
  1482.                         dec(&first_match_pos);
  1483.  
  1484.                     last_match_pos = first_match_pos;
  1485.  
  1486.                     /* Get list of all completions now, if appropriate */
  1487.                     if (ctrl_x_mode == CTRL_X_PATH_PATTERNS ||
  1488.                         ctrl_x_mode == CTRL_X_PATH_DEFINES)
  1489.                     {
  1490.                         started_completion = TRUE;
  1491.                         find_pattern_in_path(complete_pat,
  1492.                                 (int)STRLEN(complete_pat), FALSE, FALSE,
  1493.                             (ctrl_x_mode == CTRL_X_PATH_DEFINES) ? FIND_DEFINE
  1494.                             : FIND_ANY, 1L, ACTION_EXPAND,
  1495.                             (linenr_t)1, (linenr_t)MAXLNUM);
  1496.  
  1497.                         if (make_cyclic() > 1)
  1498.                         {
  1499.                             sprintf((char *)IObuff, "There are %d matches",
  1500.                                 count_completions());
  1501.                             mesg = IObuff;
  1502.                         }
  1503.                     }
  1504.                     else if (ctrl_x_mode == CTRL_X_DICTIONARY)
  1505.                     {
  1506.                         started_completion = TRUE;
  1507.                         if (*p_dict == NUL)
  1508.                             mesg = (char_u *)"'dictionary' option is empty";
  1509.                         else
  1510.                         {
  1511.                             complete_dictionaries(complete_pat,
  1512.                                                           complete_direction);
  1513.                             if (make_cyclic() > 1)
  1514.                             {
  1515.                                 sprintf((char *)IObuff,
  1516.                                     "There are %d matching words",
  1517.                                     count_completions());
  1518.                                 mesg = IObuff;
  1519.                             }
  1520.                         }
  1521.                     }
  1522.                     else if (ctrl_x_mode == CTRL_X_TAGS)
  1523.                     {
  1524.                         started_completion = TRUE;
  1525.                             /* set reg_ic according to p_ic, p_scs and pat */
  1526.                         set_reg_ic(complete_pat);
  1527.                         prog = vim_regcomp(complete_pat);
  1528.                         if (prog != NULL &&
  1529.                             find_tags(NULL, prog, &num_matches, &matches,
  1530.                                        FALSE, FALSE) == OK && num_matches > 0)
  1531.                         {
  1532.                             for (i = 0; i < num_matches; i++)
  1533.                                 if (add_completion(matches[i], -1, NULL,
  1534.                                                         FORWARD) == RET_ERROR)
  1535.                                     break;
  1536.                             FreeWild(num_matches, matches);
  1537.                             vim_free(prog);
  1538.                             if (make_cyclic() > 1)
  1539.                             {
  1540.                                 sprintf((char *)IObuff,
  1541.                                     "There are %d matching tags",
  1542.                                     count_completions());
  1543.                                 mesg = IObuff;
  1544.                             }
  1545.                         }
  1546.                         else
  1547.                         {
  1548.                             vim_free(prog);
  1549.                             vim_free(complete_pat);
  1550.                             complete_pat = NULL;
  1551.                         }
  1552.                     }
  1553.                     else if (ctrl_x_mode == CTRL_X_FILES)
  1554.                     {
  1555.                         started_completion = TRUE;
  1556.                         expand_interactively = TRUE;
  1557.                         if (ExpandWildCards(1, &complete_pat, &num_matches,
  1558.                                                 &matches, FALSE, FALSE) == OK)
  1559.                         {
  1560.                             /*
  1561.                              * May change home directory back to "~".
  1562.                              */
  1563.                             tilde_replace(complete_pat, num_matches, matches);
  1564.                             for (i = 0; i < num_matches; i++)
  1565.                                 if (add_completion(matches[i], -1, NULL,
  1566.                                                         FORWARD) == RET_ERROR)
  1567.                                     break;
  1568.                             FreeWild(num_matches, matches);
  1569.                             if (make_cyclic() > 1)
  1570.                             {
  1571.                                 sprintf((char *)IObuff,
  1572.                                     "There are %d matching file names",
  1573.                                     count_completions());
  1574.                                 mesg = IObuff;
  1575.                             }
  1576.                         }
  1577.                         else
  1578.                         {
  1579.                             vim_free(complete_pat);
  1580.                             complete_pat = NULL;
  1581.                         }
  1582.                         expand_interactively = FALSE;
  1583.                     }
  1584.                 }
  1585.                 /*
  1586.                  * In insert mode: Delete the typed part.
  1587.                  * In replace mode: Put the old characters back, if any.
  1588.                  */
  1589.                 while (curwin->w_cursor.col > complete_col)
  1590.                 {
  1591.                     curwin->w_cursor.col--;
  1592.                     if (State == REPLACE)
  1593.                     {
  1594.                         if ((cc = replace_pop()) > 0)
  1595.                             pchar(curwin->w_cursor, cc);
  1596.                     }
  1597.                     else
  1598.                         delchar(FALSE);
  1599.                 }
  1600.                 complete_pos = NULL;
  1601.                 if (started_completion && curr_match == NULL &&
  1602.                                         (p_ws || done_dir == BOTH_DIRECTIONS))
  1603.                 {
  1604.                     edit_submode_extra = e_patnotf;
  1605.                     edit_submode_highl = 'e';
  1606.                 }
  1607.                 else if (curr_match != NULL && complete_direction == FORWARD &&
  1608.                                             curr_match->next != NULL)
  1609.                     curr_match = curr_match->next;
  1610.                 else if (curr_match != NULL && complete_direction == BACKWARD &&
  1611.                                             curr_match->prev != NULL)
  1612.                     curr_match = curr_match->prev;
  1613.                 else
  1614.                 {
  1615.                     complete_pos = (complete_direction == FORWARD) ?
  1616.                                         &last_match_pos : &first_match_pos;
  1617.                     /*
  1618.                      * If 'infercase' is set, don't use 'smartcase' here
  1619.                      */
  1620.                     save_p_scs = p_scs;
  1621.                     if (curbuf->b_p_inf)
  1622.                         p_scs = FALSE;
  1623.                     for (;;)
  1624.                     {
  1625.                         if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
  1626.                             temp = search_for_exact_line(complete_pos,
  1627.                                     complete_direction, complete_pat);
  1628.                         else
  1629.                             temp = searchit(complete_pos, complete_direction,
  1630.                                     complete_pat, 1L,
  1631.                                     SEARCH_KEEP + SEARCH_NFMSG, RE_LAST);
  1632.                         if (temp == FAIL)
  1633.                         {
  1634.                             if (!p_ws && done_dir != -complete_direction)
  1635.                             {
  1636.                                 /*
  1637.                                  * With nowrapscan, we haven't finished
  1638.                                  * looking in the other direction yet -- webb
  1639.                                  */
  1640.                                 temp = OK;
  1641.                                 done_dir = complete_direction;
  1642.                             }
  1643.                             else if (!p_ws)
  1644.                                 done_dir = BOTH_DIRECTIONS;
  1645.                             break;
  1646.                         }
  1647.                         if (!started_completion)
  1648.                         {
  1649.                             started_completion = TRUE;
  1650.                             first_match_pos = *complete_pos;
  1651.                             last_match_pos = *complete_pos;
  1652.                         }
  1653.                         else if (first_match_pos.lnum == last_match_pos.lnum &&
  1654.                           first_match_pos.col == last_match_pos.col)
  1655.                         {
  1656.                             /* We have found all the matches in this file */
  1657.                             temp = FAIL;
  1658.                             break;
  1659.                         }
  1660.                         ptr = ml_get_pos(complete_pos);
  1661.                         if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
  1662.                             temp = STRLEN(ptr);
  1663.                         else
  1664.                         {
  1665.                             tmp_ptr = ptr;
  1666.                             temp = 0;
  1667.                             while (*tmp_ptr != NUL && iswordchar(*tmp_ptr++))
  1668.                                 temp++;
  1669.                         }
  1670.                         if (add_completion_and_infercase(ptr, temp, NULL,
  1671.                                                   complete_direction) != FAIL)
  1672.                         {
  1673.                             temp = OK;
  1674.                             break;
  1675.                         }
  1676.                     }
  1677.                     p_scs = save_p_scs;
  1678.                 }
  1679.                 if (complete_pos != NULL && temp == FAIL)
  1680.                 {
  1681.                     int tot;
  1682.  
  1683.                     tot = count_completions();    /* Total num matches */
  1684.                     if (curr_match != NULL)
  1685.                         (void)make_cyclic();
  1686.                     if (tot > 1)
  1687.                     {
  1688.                         sprintf((char *)IObuff,
  1689.                             "All %d matches have now been found", tot);
  1690.                         mesg = IObuff;
  1691.                     }
  1692.                     else if (tot == 0)
  1693.                     {
  1694.                         edit_submode_extra = e_patnotf;
  1695.                         edit_submode_highl = 'e';
  1696.                     }
  1697.                 }
  1698.  
  1699.                 /* eat the ESC to avoid leaving insert mode */
  1700.                 if (got_int && !global_busy)
  1701.                 {
  1702.                     (void)vgetc();
  1703.                     got_int = FALSE;
  1704.                 }
  1705.  
  1706.                 /*
  1707.                  * When using match from another file, show the file name.
  1708.                  */
  1709.                 if (curr_match != NULL)
  1710.                     ptr = curr_match->str;
  1711.                 else            /* back to what has been typed */
  1712.                     ptr = original_text;
  1713.  
  1714.                 if (edit_submode_extra == NULL)
  1715.                 {
  1716.                     if (curr_match == NULL || curr_match->original)
  1717.                     {
  1718.                         edit_submode_extra = (char_u *)"Back at original";
  1719.                         edit_submode_highl = 'w';
  1720.                     }
  1721.                     else if (first_match != NULL &&
  1722.                             first_match->next != NULL &&
  1723.                             (first_match->next == first_match ||
  1724.                              first_match->next->original))
  1725.                     {
  1726.                         edit_submode_extra = (char_u *)"(the only match)";
  1727.                         edit_submode_highl = NUL;
  1728.                     }
  1729.                 }
  1730.  
  1731.                 /*
  1732.                  * Use ins_char() to insert the text, it is a bit slower than
  1733.                  * ins_str(), but it takes care of replace mode.
  1734.                  */
  1735.                 if (ptr != NULL)
  1736.                     while (*ptr)
  1737.                         ins_char(*ptr++);
  1738.  
  1739.                 started_completion = TRUE;
  1740.                 need_redraw = TRUE;
  1741.  
  1742.                 if (mesg != NULL)
  1743.                 {
  1744.                     if (dollar_vcol)
  1745.                         curs_columns(FALSE);
  1746.                     updateline();
  1747.                     need_redraw = FALSE;
  1748.                     (void)set_highlight('r');
  1749.                     msg_highlight = TRUE;
  1750.                     msg(mesg);
  1751.                     cursupdate();
  1752.                     setcursor();
  1753.                     flushbuf();
  1754.                     mch_delay(2000L, FALSE);
  1755.                 }
  1756.                 if (edit_submode_extra != NULL)
  1757.                 {
  1758.                     if (!p_smd)
  1759.                     {
  1760.                         if (edit_submode_highl)
  1761.                         {
  1762.                             set_highlight('r');        /* Highlight mode */
  1763.                             msg_highlight = TRUE;
  1764.                         }
  1765.                         msg(edit_submode_extra);
  1766.                     }
  1767.                     else
  1768.                         showmode();
  1769.                     edit_submode_extra = NULL;
  1770.                 }
  1771.  
  1772.                 /*
  1773.                  * If there is a file name for the match, overwrite any
  1774.                  * previous message, it's more interesting to know where the
  1775.                  * match comes from, except when using the dictionary.
  1776.                  * Truncate the file name to avoid a wait for return.
  1777.                  */
  1778.                 if (curr_match != NULL && curr_match->fname != NULL &&
  1779.                             (ctrl_x_mode != CTRL_X_DICTIONARY || mesg == NULL))
  1780.                 {
  1781.                     STRCPY(IObuff, "match in file ");
  1782.                     i = (strsize(curr_match->fname) + 16) - sc_col;
  1783.                     if (i <= 0)
  1784.                         i = 0;
  1785.                     else
  1786.                         STRCAT(IObuff, "<");
  1787.                     STRCAT(IObuff, curr_match->fname + i);
  1788.                     msg(IObuff);
  1789.                     redraw_cmdline = FALSE;        /* don't overwrite! */
  1790.                 }
  1791.  
  1792.                 break;
  1793. #endif /* INSERT_EXPAND */
  1794.  
  1795.               case Ctrl('Y'):                /* copy from previous line */
  1796. #ifdef INSERT_EXPAND
  1797.                 if (ctrl_x_mode == CTRL_X_SCROLL)
  1798.                 {
  1799.                     scrolldown_clamp();
  1800.                     updateScreen(VALID);
  1801.                     break;
  1802.                 }
  1803. #endif /* INSERT_EXPAND */
  1804.                 lnum = curwin->w_cursor.lnum - 1;
  1805.                 goto copychar;
  1806.  
  1807.               case Ctrl('E'):                /* copy from next line */
  1808. #ifdef INSERT_EXPAND
  1809.                 if (ctrl_x_mode == CTRL_X_SCROLL)
  1810.                 {
  1811.                     scrollup_clamp();
  1812.                     updateScreen(VALID);
  1813.                     break;
  1814.                 }
  1815. #endif /* INSERT_EXPAND */
  1816.                 lnum = curwin->w_cursor.lnum + 1;
  1817. copychar:
  1818.                 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
  1819.                 {
  1820.                     beep_flush();
  1821.                     break;
  1822.                 }
  1823.  
  1824.                 /* try to advance to the cursor column */
  1825.                 temp = 0;
  1826.                 ptr = ml_get(lnum);
  1827.                 while ((colnr_t)temp < curwin->w_virtcol && *ptr)
  1828.                     temp += lbr_chartabsize(ptr++, (colnr_t)temp);
  1829.  
  1830.                 if ((colnr_t)temp > curwin->w_virtcol)
  1831.                     --ptr;
  1832.                 if ((c = *ptr) == NUL)
  1833.                 {
  1834.                     beep_flush();
  1835.                     break;
  1836.                 }
  1837.  
  1838.                 /*FALLTHROUGH*/
  1839.               default:
  1840. normalchar:
  1841.                 /*
  1842.                  * do some very smart indenting when entering '{' or '}'
  1843.                  */
  1844.                 if (((did_si || can_si_back) && c == '{') ||
  1845.                                                          (can_si && c == '}'))
  1846.                 {
  1847.                     FPOS    *pos, old_pos;
  1848.  
  1849.                         /* for '}' set indent equal to indent of line
  1850.                          * containing matching '{'
  1851.                          */
  1852.                     if (c == '}' && (pos = findmatch('{')) != NULL)
  1853.                     {
  1854.                         old_pos = curwin->w_cursor;
  1855.                         /*
  1856.                          * If the matching '{' has a ')' immediately before it
  1857.                          * (ignoring white-space), then line up with the start
  1858.                          * of the line containing the matching '(' if there is
  1859.                          * one.  This handles the case where an
  1860.                          * "if (..\n..) {" statement continues over multiple
  1861.                          * lines -- webb
  1862.                          */
  1863.                         ptr = ml_get(pos->lnum);
  1864.                         i = pos->col;
  1865.                         if (i > 0)            /* skip blanks before '{' */
  1866.                             while (--i > 0 && vim_iswhite(ptr[i]))
  1867.                                 ;
  1868.                         curwin->w_cursor.lnum = pos->lnum;
  1869.                         curwin->w_cursor.col = i;
  1870.                         if (ptr[i] == ')' && (pos = findmatch('(')) != NULL)
  1871.                             curwin->w_cursor = *pos;
  1872.                         i = get_indent();
  1873.                         curwin->w_cursor = old_pos;
  1874.                         set_indent(i, TRUE);
  1875.                     }
  1876.                     else if (curwin->w_cursor.col > 0)
  1877.                     {
  1878.                         /*
  1879.                          * when inserting '{' after "O" reduce indent, but not
  1880.                          * more than indent of previous line
  1881.                          */
  1882.                         temp = TRUE;
  1883.                         if (c == '{' && can_si_back &&
  1884.                                                     curwin->w_cursor.lnum > 1)
  1885.                         {
  1886.                             old_pos = curwin->w_cursor;
  1887.                             i = get_indent();
  1888.                             while (curwin->w_cursor.lnum > 1)
  1889.                             {
  1890.                                 ptr = skipwhite(
  1891.                                            ml_get(--(curwin->w_cursor.lnum)));
  1892.                                 /* ignore empty lines and lines starting with
  1893.                                  * '#'.
  1894.                                  */
  1895.                                 if (*ptr != '#' && *ptr != NUL)
  1896.                                     break;
  1897.                             }
  1898.                             if (get_indent() >= i)
  1899.                                 temp = FALSE;
  1900.                             curwin->w_cursor = old_pos;
  1901.                         }
  1902.                         if (temp)
  1903.                             shift_line(TRUE, FALSE, 1);
  1904.                     }
  1905.                 }
  1906.                     /* set indent of '#' always to 0 */
  1907.                 if (curwin->w_cursor.col > 0 && can_si && c == '#')
  1908.                 {
  1909.                                 /* remember current indent for next line */
  1910.                     old_indent = get_indent();
  1911.                     set_indent(0, TRUE);
  1912.                 }
  1913.  
  1914.                 if (c == ' ')
  1915.                 {
  1916. #ifdef CINDENT
  1917.                     if (inindent(0))
  1918.                         can_cindent = FALSE;
  1919. #endif
  1920.                     if (Insstart_blank_vcol == MAXCOL &&
  1921.                                        curwin->w_cursor.lnum == Insstart.lnum)
  1922.                         Insstart_blank_vcol = curwin->w_virtcol;
  1923.                 }
  1924.  
  1925.                 if (iswordchar(c) || !echeck_abbr(c))
  1926.                 {
  1927.                     insert_special(c, FALSE);
  1928.                     need_redraw = TRUE;
  1929. #ifdef RIGHTLEFT
  1930.                     revinslegal++;
  1931.                     revinschars++;
  1932. #endif
  1933.                 }
  1934.                 break;
  1935.         }    /* end of switch (c) */
  1936.  
  1937. #ifdef CINDENT
  1938.         if (curbuf->b_p_cin && can_cindent
  1939. # ifdef INSERT_EXPAND
  1940.                                             && !ctrl_x_mode
  1941. # endif
  1942.                                                                )
  1943.         {
  1944. force_cindent:
  1945.             /*
  1946.              * Indent now if a key was typed that is in 'cinkeys'.
  1947.              */
  1948.             if (in_cinkeys(c, ' ', line_is_white))
  1949.             {
  1950.                 stop_arrow();
  1951.  
  1952.                 /* re-indent the current line */
  1953.                 fixthisline(get_c_indent);
  1954.  
  1955.                 /* draw the changes on the screen later */
  1956.                 need_redraw = TRUE;
  1957.             }
  1958.         }
  1959. #endif /* CINDENT */
  1960.  
  1961.     }    /* for (;;) */
  1962. }
  1963.  
  1964. /*
  1965.  * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
  1966.  * Keep the cursor on the same character.
  1967.  * type == INDENT_INC    increase indent (for CTRL-T or <Tab>)
  1968.  * type == INDENT_DEC    decrease indent (for CTRL-D)
  1969.  * type == INDENT_SET    set indent to "amount"
  1970.  * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
  1971.  */
  1972.     static void
  1973. change_indent(type, amount, round)
  1974.     int        type;
  1975.     int        amount;
  1976.     int        round;
  1977. {
  1978.     int            vcol;
  1979.     int            last_vcol;
  1980.     int            insstart_less;            /* reduction for Insstart.col */
  1981.     int            new_cursor_col;
  1982.     int            i;
  1983.     char_u        *ptr;
  1984.     int            save_p_list;
  1985.     int            start_col;
  1986.  
  1987.     /* for the following tricks we don't want list mode */
  1988.     save_p_list = curwin->w_p_list;
  1989.     if (save_p_list)
  1990.     {
  1991.         curwin->w_p_list = FALSE;
  1992.         curs_columns(FALSE);            /* recompute w_virtcol */
  1993.     }
  1994.     vcol = curwin->w_virtcol;
  1995.  
  1996.     /*
  1997.      * For Replace mode we need to fix the replace stack later, which is only
  1998.      * possible when the cursor is in the indent.  Remember the number of
  1999.      * characters before the cursor if it's possible.
  2000.      */
  2001.     start_col = curwin->w_cursor.col;
  2002.  
  2003.     /* determine offset from first non-blank */
  2004.     new_cursor_col = curwin->w_cursor.col;
  2005.     beginline(TRUE);
  2006.     new_cursor_col -= curwin->w_cursor.col;
  2007.  
  2008.     insstart_less = curwin->w_cursor.col;
  2009.  
  2010.     /*
  2011.      * If the cursor is in the indent, compute how many screen columns the
  2012.      * cursor is to the left of the first non-blank.
  2013.      */
  2014.     if (new_cursor_col < 0)
  2015.         vcol = get_indent() - vcol;
  2016.  
  2017.     if (new_cursor_col > 0)            /* can't fix replace stack */
  2018.         start_col = -1;
  2019.  
  2020.     /*
  2021.      * Set the new indent.  The cursor will be put on the first non-blank.
  2022.      */
  2023.     if (type == INDENT_SET)
  2024.         set_indent(amount, TRUE);
  2025.     else
  2026.         shift_line(type == INDENT_DEC, round, 1);
  2027.     insstart_less -= curwin->w_cursor.col;
  2028.  
  2029.     /*
  2030.      * Try to put cursor on same character.
  2031.      * If the cursor is at or after the first non-blank in the line,
  2032.      * compute the cursor column relative to the column of the first
  2033.      * non-blank character.
  2034.      * If we are not in insert mode, leave the cursor on the first non-blank.
  2035.      * If the cursor is before the first non-blank, position it relative
  2036.      * to the first non-blank, counted in screen columns.
  2037.      */
  2038.     if (new_cursor_col >= 0)
  2039.         new_cursor_col += curwin->w_cursor.col;
  2040.     else if (!(State & INSERT))
  2041.         new_cursor_col = curwin->w_cursor.col;
  2042.     else
  2043.     {
  2044.         /*
  2045.          * Compute the screen column where the cursor should be.
  2046.          */
  2047.         vcol = get_indent() - vcol;
  2048.         curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
  2049.  
  2050.         /*
  2051.          * Advance the cursor until we reach the right screen column.
  2052.          */
  2053.         vcol = last_vcol = 0;
  2054.         new_cursor_col = -1;
  2055.         ptr = ml_get_curline();
  2056.         while (vcol <= (int)curwin->w_virtcol)
  2057.         {
  2058.             last_vcol = vcol;
  2059.             ++new_cursor_col;
  2060.             vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_t)vcol);
  2061.         }
  2062.         vcol = last_vcol;
  2063.  
  2064.         /*
  2065.          * May need to insert spaces to be able to position the cursor on
  2066.          * the right screen column.
  2067.          */
  2068.         if (vcol != (int)curwin->w_virtcol)
  2069.         {
  2070.             curwin->w_cursor.col = new_cursor_col;
  2071.             i = (int)curwin->w_virtcol - vcol;
  2072.             ptr = alloc(i + 1);
  2073.             if (ptr != NULL)
  2074.             {
  2075.                 new_cursor_col += i;
  2076.                 ptr[i] = NUL;
  2077.                 while (--i >= 0)
  2078.                     ptr[i] = ' ';
  2079.                 ins_str(ptr);
  2080.                 vim_free(ptr);
  2081.             }
  2082.         }
  2083.  
  2084.         /*
  2085.          * When changing the indent while the cursor is in it, reset
  2086.          * Insstart_col to 0.
  2087.          */
  2088.         insstart_less = Insstart.col;
  2089.     }
  2090.  
  2091.     curwin->w_p_list = save_p_list;
  2092.  
  2093.     if (new_cursor_col <= 0)
  2094.         curwin->w_cursor.col = 0;
  2095.     else
  2096.         curwin->w_cursor.col = new_cursor_col;
  2097.     curwin->w_set_curswant = TRUE;
  2098.  
  2099.     /*
  2100.      * May have to adjust the start of the insert.
  2101.      */
  2102.     if ((State & INSERT) && curwin->w_cursor.lnum == Insstart.lnum &&
  2103.                                                             Insstart.col != 0)
  2104.     {
  2105.         if ((int)Insstart.col <= insstart_less)
  2106.             Insstart.col = 0;
  2107.         else
  2108.             Insstart.col -= insstart_less;
  2109.     }
  2110.  
  2111.     /*
  2112.      * May have to fix the replace stack, if it's possible.
  2113.      * If the number of characters before the cursor decreased, need to pop a
  2114.      * few characters from the replace stack.
  2115.      * If the number of characters before the cursor increased, need to push a
  2116.      * few NULs onto the replace stack.
  2117.      */
  2118.     if (State == REPLACE && start_col >= 0)
  2119.     {
  2120.         while (start_col > (int)curwin->w_cursor.col)
  2121.         {
  2122.             (void)replace_pop();
  2123.             --start_col;
  2124.         }
  2125.         while (start_col < (int)curwin->w_cursor.col)
  2126.         {
  2127.             replace_push(NUL);
  2128.             ++start_col;
  2129.         }
  2130.     }
  2131. }
  2132.  
  2133. #ifdef INSERT_EXPAND
  2134. /*
  2135.  * Is the character 'c' a valid key to keep us in the current ctrl-x mode?
  2136.  * -- webb
  2137.  */
  2138.     int
  2139. is_ctrl_x_key(c)
  2140.     int        c;
  2141. {
  2142.     switch (ctrl_x_mode)
  2143.     {
  2144.         case 0:                /* Not in any ctrl-x mode */
  2145.             break;
  2146.         case CTRL_X_NOT_DEFINED_YET:
  2147.             if (c == Ctrl('X') || c == Ctrl('Y') || c == Ctrl('E') ||
  2148.                     c == Ctrl('L') || c == Ctrl('F') || c == Ctrl(']') ||
  2149.                     c == Ctrl('I') || c == Ctrl('D') || c == Ctrl('P') ||
  2150.                     c == Ctrl('N'))
  2151.                 return TRUE;
  2152.             break;
  2153.         case CTRL_X_SCROLL:
  2154.             if (c == Ctrl('Y') || c == Ctrl('E'))
  2155.                 return TRUE;
  2156.             break;
  2157.         case CTRL_X_WHOLE_LINE:
  2158.             if (c == Ctrl('L') || c == Ctrl('P') || c == Ctrl('N'))
  2159.                 return TRUE;
  2160.             break;
  2161.         case CTRL_X_FILES:
  2162.             if (c == Ctrl('F') || c == Ctrl('P') || c == Ctrl('N'))
  2163.                 return TRUE;
  2164.             break;
  2165.         case CTRL_X_DICTIONARY:
  2166.             if (c == Ctrl('K') || c == Ctrl('P') || c == Ctrl('N'))
  2167.                 return TRUE;
  2168.             break;
  2169.         case CTRL_X_TAGS:
  2170.             if (c == Ctrl(']') || c == Ctrl('P') || c == Ctrl('N'))
  2171.                 return TRUE;
  2172.             break;
  2173.         case CTRL_X_PATH_PATTERNS:
  2174.             if (c == Ctrl('P') || c == Ctrl('N'))
  2175.                 return TRUE;
  2176.             break;
  2177.         case CTRL_X_PATH_DEFINES:
  2178.             if (c == Ctrl('D') || c == Ctrl('P') || c == Ctrl('N'))
  2179.                 return TRUE;
  2180.             break;
  2181.         default:
  2182.             emsg(e_internal);
  2183.             break;
  2184.     }
  2185.     return FALSE;
  2186. }
  2187.  
  2188. /*
  2189.  * This is like add_completion(), but if ic and inf are set, then the
  2190.  * case of the originally typed text is used, and the case of the completed
  2191.  * text is infered, ie this tries to work out what case you probably wanted
  2192.  * the rest of the word to be in -- webb
  2193.  */
  2194.     int
  2195. add_completion_and_infercase(str, len, fname, dir)
  2196.     char_u    *str;
  2197.     int        len;
  2198.     char_u    *fname;
  2199.     int        dir;
  2200. {
  2201.     int has_lower = FALSE;
  2202.     int was_letter = FALSE;
  2203.     int orig_len;
  2204.     int idx;
  2205.  
  2206.     if (p_ic && curbuf->b_p_inf && len < IOSIZE)
  2207.     {
  2208.         /* Infer case of completed part -- webb */
  2209.         orig_len = STRLEN(original_text);
  2210.  
  2211.         /* Use IObuff, str would change text in buffer! */
  2212.         STRNCPY(IObuff, str, len);
  2213.         IObuff[len] = NUL;
  2214.  
  2215.         /* Rule 1: Were any chars converted to lower? */
  2216.         for (idx = 0; idx < orig_len; ++idx)
  2217.         {
  2218.             if (islower(original_text[idx]))
  2219.             {
  2220.                 has_lower = TRUE;
  2221.                 if (isupper(IObuff[idx]))
  2222.                 {
  2223.                     /* Rule 1 is satisfied */
  2224.                     for (idx = orig_len; idx < len; ++idx)
  2225.                         IObuff[idx] = TO_LOWER(IObuff[idx]);
  2226.                     break;
  2227.                 }
  2228.             }
  2229.         }
  2230.  
  2231.         /*
  2232.          * Rule 2: No lower case, 2nd consecutive letter converted to
  2233.          * upper case.
  2234.          */
  2235.         if (!has_lower)
  2236.         {
  2237.             for (idx = 0; idx < orig_len; ++idx)
  2238.             {
  2239.                 if (was_letter && isupper(original_text[idx]) &&
  2240.                     islower(IObuff[idx]))
  2241.                 {
  2242.                     /* Rule 2 is satisfied */
  2243.                     for (idx = orig_len; idx < len; ++idx)
  2244.                         IObuff[idx] = TO_UPPER(IObuff[idx]);
  2245.                     break;
  2246.                 }
  2247.                 was_letter = isalpha(original_text[idx]);
  2248.             }
  2249.         }
  2250.  
  2251.         /* Copy the original case of the part we typed */
  2252.         STRNCPY(IObuff, original_text, orig_len);
  2253.  
  2254.         return add_completion(IObuff, len, fname, dir);
  2255.     }
  2256.     return add_completion(str, len, fname, dir);
  2257. }
  2258.  
  2259. /*
  2260.  * Add a match to the list of matches.
  2261.  * If the given string is already in the list of completions, then return
  2262.  * FAIL, otherwise add it to the list and return OK.  If there is an error,
  2263.  * maybe because alloc returns NULL, then RET_ERROR is returned -- webb.
  2264.  */
  2265.     static int
  2266. add_completion(str, len, fname, dir)
  2267.     char_u    *str;
  2268.     int        len;
  2269.     char_u    *fname;
  2270.     int        dir;
  2271. {
  2272.     struct Completion *match;
  2273.  
  2274.     mch_breakcheck();
  2275.     if (got_int)
  2276.         return RET_ERROR;
  2277.     if (len < 0)
  2278.         len = STRLEN(str);
  2279.  
  2280.     /*
  2281.      * If the same match is already present, don't add it.
  2282.      */
  2283.     if (first_match != NULL)
  2284.     {
  2285.         match = first_match;
  2286.         do
  2287.         {
  2288.             if (STRNCMP(match->str, str, (size_t)len) == 0 &&
  2289.                                                        match->str[len] == NUL)
  2290.                 return FAIL;
  2291.             match = match->next;
  2292.         } while (match != NULL && match != first_match);
  2293.     }
  2294.  
  2295.     /*
  2296.      * Allocate a new match structure.
  2297.      * Copy the values to the new match structure.
  2298.      */
  2299.     match = (struct Completion *)alloc((unsigned)sizeof(struct Completion));
  2300.     if (match == NULL)
  2301.         return RET_ERROR;
  2302.     if ((match->str = strnsave(str, len)) == NULL)
  2303.     {
  2304.         vim_free(match);
  2305.         return RET_ERROR;
  2306.     }
  2307.     if (fname != NULL)
  2308.         match->fname = strsave(fname);        /* ignore errors */
  2309.     else
  2310.         match->fname = NULL;
  2311.     match->original = FALSE;
  2312.  
  2313.     /*
  2314.      * Link the new match structure in the list of matches.
  2315.      */
  2316.     if (first_match == NULL)
  2317.     {
  2318.         first_match = curr_match = match;
  2319.         curr_match->next = curr_match->prev = NULL;
  2320.     }
  2321.     else
  2322.     {
  2323.         if (dir == FORWARD)
  2324.         {
  2325.             match->next = NULL;
  2326.             match->prev = curr_match;
  2327.             curr_match->next = match;
  2328.             curr_match = match;
  2329.         }
  2330.         else    /* BACKWARD */
  2331.         {
  2332.             match->prev = NULL;
  2333.             match->next = curr_match;
  2334.             curr_match->prev = match;
  2335.             first_match = curr_match = match;
  2336.         }
  2337.     }
  2338.  
  2339.     return OK;
  2340. }
  2341.  
  2342. /*
  2343.  * Make the completion list cyclic.  Add the original text at the end.
  2344.  * Return the number of matches (excluding the original).
  2345.  */
  2346.     static int
  2347. make_cyclic()
  2348. {
  2349.     struct Completion *match, *orig;
  2350.     int        count = 0;
  2351.  
  2352.     if (first_match != NULL)
  2353.     {
  2354.         /*
  2355.          * Find the end of the list.
  2356.          */
  2357.         match = first_match;
  2358.         count = 1;
  2359.         while (match->next != NULL)
  2360.         {
  2361.             match = match->next;
  2362.             ++count;
  2363.         }
  2364.  
  2365.         if (original_text != NULL)
  2366.         {
  2367.             /*
  2368.              * Allocate a new structure for the original text.
  2369.              * Copy the original text to the new structure.
  2370.              * Link it in the list at the end.
  2371.              */
  2372.             orig = (struct Completion *)alloc((unsigned)sizeof(
  2373.                                                           struct Completion));
  2374.             if (orig != NULL)
  2375.             {
  2376.                 if ((orig->str = strsave(original_text)) == NULL)
  2377.                     vim_free(orig);
  2378.                 else
  2379.                 {
  2380.                     orig->fname = NULL;
  2381.                     orig->original = TRUE;
  2382.                     orig->prev = match;
  2383.                     match->next = orig;
  2384.                     match = orig;
  2385.                     curr_match = orig;
  2386.                 }
  2387.             }
  2388.         }
  2389.         match->next = first_match;
  2390.         first_match->prev = match;
  2391.     }
  2392.     return count;
  2393. }
  2394.  
  2395. /*
  2396.  * Add any identifiers that match the given pattern to the list of
  2397.  * completions.
  2398.  */
  2399.     static void
  2400. complete_dictionaries(pat, dir)
  2401.     char_u    *pat;
  2402.     int        dir;
  2403. {
  2404.     struct Completion *save_curr_match = curr_match;
  2405.     char_u    *dict = p_dict;
  2406.     char_u    *ptr;
  2407.     char_u    *buf;
  2408.     char_u    *fname;
  2409.     int        at_start;
  2410.     FILE    *fp;
  2411.     struct regexp *prog = NULL;
  2412.  
  2413.     if ((buf = alloc(LSIZE)) == NULL)
  2414.         return;
  2415.     if (curr_match != NULL)
  2416.     {
  2417.         while (curr_match->next != NULL)
  2418.             curr_match = curr_match->next;
  2419.     }
  2420.     if (*dict != NUL)
  2421.     {
  2422.         (void)set_highlight('r');
  2423.         msg_highlight = TRUE;
  2424.         MSG("Please wait, searching dictionaries");
  2425.         set_reg_ic(pat);    /* set reg_ic according to p_ic, p_scs and pat */
  2426.         reg_magic = p_magic;
  2427.         prog = vim_regcomp(pat);
  2428.     }
  2429.     while (*dict != NUL && prog != NULL && !got_int)
  2430.     {
  2431.                                 /* copy one dictionary file name into buf */
  2432.         (void)copy_option_part(&dict, buf, LSIZE, ",");
  2433.  
  2434.         fp = fopen((char *)buf, "r");        /* open dictionary file */
  2435.  
  2436.         if (fp != NULL)
  2437.         {
  2438.             fname = strsave(buf);            /* keep name of file */
  2439.             /*
  2440.              * Read dictionary file line by line.
  2441.              * Check each line for a match.
  2442.              */
  2443.             while (!got_int && !vim_fgets(buf, LSIZE, fp))
  2444.             {
  2445.                 ptr = buf;
  2446.                 at_start = TRUE;
  2447.                 while (vim_regexec(prog, ptr, at_start))
  2448.                 {
  2449.                     at_start = FALSE;
  2450.                     ptr = prog->startp[0];
  2451.                     while (iswordchar(*ptr))
  2452.                         ++ptr;
  2453.                     if (add_completion_and_infercase(prog->startp[0],
  2454.                                  (int)(ptr - prog->startp[0]), fname, FORWARD)
  2455.                                                                  == RET_ERROR)
  2456.                         break;
  2457.                 }
  2458.                 line_breakcheck();
  2459.             }
  2460.             fclose(fp);
  2461.             vim_free(fname);
  2462.         }
  2463.     }
  2464.     vim_free(prog);
  2465.     if (save_curr_match != NULL)
  2466.         curr_match = save_curr_match;
  2467.     else if (dir == BACKWARD)
  2468.         curr_match = first_match;
  2469.     vim_free(buf);
  2470. }
  2471.  
  2472. /*
  2473.  * Free the list of completions
  2474.  */
  2475.     static void
  2476. free_completions()
  2477. {
  2478.     struct Completion *match;
  2479.  
  2480.     if (first_match == NULL)
  2481.         return;
  2482.     curr_match = first_match;
  2483.     do
  2484.     {
  2485.         match = curr_match;
  2486.         curr_match = curr_match->next;
  2487.         vim_free(match->str);
  2488.         vim_free(match->fname);
  2489.         vim_free(match);
  2490.     } while (curr_match != NULL && curr_match != first_match);
  2491.     first_match = curr_match = NULL;
  2492. }
  2493.  
  2494. /*
  2495.  * Return the number of items in the Completion list
  2496.  */
  2497.     static int
  2498. count_completions()
  2499. {
  2500.     struct Completion *match;
  2501.     int num = 0;
  2502.  
  2503.     if (first_match == NULL)
  2504.         return 0;
  2505.     match = first_match;
  2506.     do
  2507.     {
  2508.         if (!match->original)        /* original string doesn't count */
  2509.             num++;
  2510.         match = match->next;
  2511.     } while (match != NULL && match != first_match);
  2512.     return num;
  2513. }
  2514. #endif /* INSERT_EXPAND */
  2515.  
  2516. /*
  2517.  * Next character is interpreted literally.
  2518.  * A one, two or three digit decimal number is interpreted as its byte value.
  2519.  * If one or two digits are entered, the next character is given to vungetc().
  2520.  */
  2521.     int
  2522. get_literal()
  2523. {
  2524.     int             cc;
  2525.     int             nc;
  2526.     int             i;
  2527.  
  2528.     if (got_int)
  2529.         return Ctrl('C');
  2530.  
  2531. #ifdef USE_GUI
  2532.     /*
  2533.      * In GUI there is no point inserting the internal code for a special key.
  2534.      * It is more useful to insert the string "<KEY>" instead.  This would
  2535.      * probably be useful in a text window too, but it would not be
  2536.      * vi-compatible (maybe there should be an option for it?) -- webb
  2537.      */
  2538.     if (gui.in_use)
  2539.         ++allow_keys;
  2540. #endif
  2541.     ++no_mapping;            /* don't map the next key hits */
  2542.     cc = 0;
  2543.     for (i = 0; i < 3; ++i)
  2544.     {
  2545.         do
  2546.             nc = vgetc();
  2547.         while (nc == K_IGNORE || nc == K_SCROLLBAR || nc == K_HORIZ_SCROLLBAR);
  2548.         if (!(State & CMDLINE))
  2549.             add_to_showcmd(nc, FALSE);
  2550.         if (IS_SPECIAL(nc) || !isdigit(nc))
  2551.             break;
  2552.         cc = cc * 10 + nc - '0';
  2553.         if (cc > 255)
  2554.             cc = 255;            /* limit range to 0-255 */
  2555.         nc = 0;
  2556.     }
  2557.     if (i == 0)        /* no number entered */
  2558.     {
  2559.         if (nc == K_ZERO)    /* NUL is stored as NL */
  2560.         {
  2561.             cc = '\n';
  2562.             nc = 0;
  2563.         }
  2564.         else
  2565.         {
  2566.             cc = nc;
  2567.             nc = 0;
  2568.         }
  2569.     }
  2570.  
  2571.     if (cc == 0)        /* NUL is stored as NL */
  2572.         cc = '\n';
  2573.  
  2574.     --no_mapping;
  2575. #ifdef USE_GUI
  2576.     if (gui.in_use)
  2577.         --allow_keys;
  2578. #endif
  2579.     if (nc)
  2580.         vungetc(nc);
  2581.     got_int = FALSE;        /* CTRL-C typed after CTRL-V is not an interrupt */
  2582.     return cc;
  2583. }
  2584.  
  2585. /*
  2586.  * Insert character, taking care of special keys and mod_mask
  2587.  */
  2588.     static void
  2589. insert_special(c, allow_modmask)
  2590.     int        c;
  2591.     int        allow_modmask;
  2592. {
  2593.     char_u    *p;
  2594.     int        len;
  2595.  
  2596.     /*
  2597.      * Special function key, translate into "<Key>". Up to the last '>' is
  2598.      * inserted with ins_str(), so as not to replace characters in replace
  2599.      * mode.
  2600.      * Only use mod_mask for special keys, to avoid things like <S-Space>,
  2601.      * unless 'allow_modmask' is TRUE.
  2602.      */
  2603.     if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
  2604.     {
  2605.         p = get_special_key_name(c, mod_mask);
  2606.         len = STRLEN(p);
  2607.         c = p[len - 1];
  2608.         if (len > 2)
  2609.         {
  2610.             p[len - 1] = NUL;
  2611.             ins_str(p);
  2612.             AppendToRedobuff(p);
  2613.         }
  2614.     }
  2615.     insertchar(c, FALSE, -1);
  2616. }
  2617.  
  2618. /*
  2619.  * Special characters in this context are those that need processing other
  2620.  * than the simple insertion that can be performed here. This includes ESC
  2621.  * which terminates the insert, and CR/NL which need special processing to
  2622.  * open up a new line. This routine tries to optimize insertions performed by
  2623.  * the "redo", "undo" or "put" commands, so it needs to know when it should
  2624.  * stop and defer processing to the "normal" mechanism.
  2625.  */
  2626. #define ISSPECIAL(c)    ((c) < ' ' || (c) >= DEL)
  2627.  
  2628.     void
  2629. insertchar(c, force_formatting, second_indent)
  2630.     unsigned    c;
  2631.     int            force_formatting;        /* format line regardless of p_fo */
  2632.     int            second_indent;            /* indent for second line if >= 0 */
  2633. {
  2634.     int            haveto_redraw = FALSE;
  2635.     int            textwidth;
  2636.     colnr_t        leader_len;
  2637.     int            first_line = TRUE;
  2638.     int            fo_ins_blank;
  2639.     int            save_char = NUL;
  2640.  
  2641.     stop_arrow();
  2642.  
  2643.     /*
  2644.      * find out textwidth to be used:
  2645.      *    if 'textwidth' option is set, use it
  2646.      *    else if 'wrapmargin' option is set, use Columns - 'wrapmargin'
  2647.      *    if invalid value, use 0.
  2648.      *  Set default to window width (maximum 79) for "Q" command.
  2649.      */
  2650.     textwidth = curbuf->b_p_tw;
  2651.     if (textwidth == 0 && curbuf->b_p_wm)
  2652.         textwidth = Columns - curbuf->b_p_wm;
  2653.     if (textwidth < 0)
  2654.         textwidth = 0;
  2655.     if (force_formatting && textwidth == 0)
  2656.     {
  2657.         textwidth = Columns - 1;
  2658.         if (textwidth > 79)
  2659.             textwidth = 79;
  2660.     }
  2661.  
  2662.     fo_ins_blank = has_format_option(FO_INS_BLANK);
  2663.  
  2664.     /*
  2665.      * Try to break the line in two or more pieces when:
  2666.      * - Always do this if we have been called to do formatting only.
  2667.      * - Otherwise:
  2668.      *   - Don't do this if inserting a blank
  2669.      *   - Don't do this if an existing character is being replaced.
  2670.      *   - Do this if the cursor is not on the line where insert started
  2671.      *   or - 'formatoptions' doesn't have 'l' or the line was not too long
  2672.      *         before the insert.
  2673.      *      - 'formatoptions' doesn't have 'b' or a blank was inserted at or
  2674.      *        before 'textwidth'
  2675.      */
  2676.     if (force_formatting || (!vim_iswhite(c) &&
  2677.                              !(State == REPLACE && *ml_get_cursor() != NUL) &&
  2678.                                     (curwin->w_cursor.lnum != Insstart.lnum ||
  2679.                                           ((!has_format_option(FO_INS_LONG) ||
  2680.                                     Insstart_textlen <= (colnr_t)textwidth) &&
  2681.               (!fo_ins_blank || Insstart_blank_vcol <= (colnr_t)textwidth)))))
  2682.     {
  2683.         /*
  2684.          * When 'ai' is off we don't want a space under the cursor to be
  2685.          * deleted.  Replace it with an 'x' temporarily.
  2686.          */
  2687.         if (!curbuf->b_p_ai && vim_iswhite(gchar_cursor()))
  2688.         {
  2689.             save_char = gchar_cursor();
  2690.             pchar_cursor('x');
  2691.         }
  2692.         while (textwidth && curwin->w_virtcol >= (colnr_t)textwidth)
  2693.         {
  2694.             int        startcol;            /* Cursor column at entry */
  2695.             int        wantcol;            /* column at textwidth border */
  2696.             int        foundcol;            /* column for start of spaces */
  2697.             int        end_foundcol = 0;    /* column for start of word */
  2698.             colnr_t    len;
  2699.  
  2700.             if (!force_formatting && has_format_option(FO_WRAP_COMS))
  2701.                 fo_do_comments = TRUE;
  2702.  
  2703.             /* Don't break until after the comment leader */
  2704.             leader_len = get_leader_len(ml_get_curline(), NULL);
  2705.             if (!force_formatting && leader_len == 0 &&
  2706.                                                   !has_format_option(FO_WRAP))
  2707.  
  2708.             {
  2709.                 textwidth = 0;
  2710.                 break;
  2711.             }
  2712.             if ((startcol = curwin->w_cursor.col) == 0)
  2713.                 break;
  2714.                                         /* find column of textwidth border */
  2715.             coladvance((colnr_t)textwidth);
  2716.             wantcol = curwin->w_cursor.col;
  2717.  
  2718.             curwin->w_cursor.col = startcol - 1;
  2719.             foundcol = 0;
  2720.             /*
  2721.              * Find position to break at.
  2722.              * Stop at start of line.
  2723.              * Stop at first entered white when 'formatoptions' has 'v'
  2724.              */
  2725.             while (curwin->w_cursor.col > 0 &&
  2726.                           ((!fo_ins_blank && !has_format_option(FO_INS_VI)) ||
  2727.                                  curwin->w_cursor.lnum != Insstart.lnum ||
  2728.                                      curwin->w_cursor.col >= Insstart.col))
  2729.             {
  2730.                 if (vim_iswhite(gchar_cursor()))
  2731.                 {
  2732.                         /* remember position of blank just before text */
  2733.                     end_foundcol = curwin->w_cursor.col;
  2734.                     while (curwin->w_cursor.col > 0 &&
  2735.                                                   vim_iswhite(gchar_cursor()))
  2736.                         --curwin->w_cursor.col;
  2737.                     if (curwin->w_cursor.col == 0 &&
  2738.                                                   vim_iswhite(gchar_cursor()))
  2739.                         break;            /* only spaces in front of text */
  2740.                     /* Don't break until after the comment leader */
  2741.                     if (curwin->w_cursor.col < leader_len)
  2742.                         break;
  2743.                     foundcol = curwin->w_cursor.col + 1;
  2744.                     if (curwin->w_cursor.col < (colnr_t)wantcol)
  2745.                         break;
  2746.                 }
  2747.                 --curwin->w_cursor.col;
  2748.             }
  2749.  
  2750.             if (foundcol == 0)            /* no spaces, cannot break line */
  2751.             {
  2752.                 curwin->w_cursor.col = startcol;
  2753.                 break;
  2754.             }
  2755.  
  2756.             /*
  2757.              * offset between cursor position and line break is used by
  2758.              * replace stack functions
  2759.              */
  2760.             replace_offset = startcol - end_foundcol - 1;
  2761.  
  2762.             /*
  2763.              * adjust startcol for spaces that will be deleted and
  2764.              * characters that will remain on top line
  2765.              */
  2766.             curwin->w_cursor.col = foundcol;
  2767.             while (vim_iswhite(gchar_cursor()))
  2768.             {
  2769.                 ++curwin->w_cursor.col;
  2770.                 --startcol;
  2771.             }
  2772.             startcol -= foundcol;
  2773.             if (startcol < 0)
  2774.                 startcol = 0;
  2775.  
  2776.                 /* put cursor after pos. to break line */
  2777.             curwin->w_cursor.col = foundcol;
  2778.  
  2779.             Opencmd(FORWARD, FALSE, TRUE);
  2780.  
  2781.             replace_offset = 0;
  2782.             if (second_indent >= 0 && first_line)
  2783.                 set_indent(second_indent, TRUE);
  2784.             first_line = FALSE;
  2785.  
  2786.             /*
  2787.              * check if cursor is not past the NUL off the line, cindent may
  2788.              * have added or removed indent.
  2789.              */
  2790.             curwin->w_cursor.col += startcol;
  2791.             len = STRLEN(ml_get_curline());
  2792.             if (curwin->w_cursor.col > len)
  2793.                 curwin->w_cursor.col = len;
  2794.  
  2795.             curs_columns(FALSE);        /* update curwin->w_virtcol */
  2796.             haveto_redraw = TRUE;
  2797. #ifdef CINDENT
  2798.             can_cindent = TRUE;
  2799. #endif
  2800.         }
  2801.  
  2802.         if (save_char)                    /* put back space after cursor */
  2803.             pchar_cursor(save_char);
  2804.  
  2805.         if (c == NUL)                    /* formatting only */
  2806.             return;
  2807.         fo_do_comments = FALSE;
  2808.         if (haveto_redraw)
  2809.         {
  2810.             /*
  2811.              * If the cursor ended up just below the screen we scroll up here
  2812.              * to avoid a redraw of the whole screen in the most common cases.
  2813.              */
  2814.              if (curwin->w_cursor.lnum == curwin->w_botline &&
  2815.                                                         !curwin->w_empty_rows)
  2816.                 win_del_lines(curwin, 0, 1, TRUE, TRUE);
  2817.             updateScreen(CURSUPD);
  2818.         }
  2819.     }
  2820.     if (c == NUL)            /* only formatting was wanted */
  2821.         return;
  2822.  
  2823.     did_ai = FALSE;
  2824.     did_si = FALSE;
  2825.     can_si = FALSE;
  2826.     can_si_back = FALSE;
  2827.  
  2828.     /*
  2829.      * If there's any pending input, grab up to INPUT_BUFLEN at once.
  2830.      * This speeds up normal text input considerably.
  2831.      * Don't do this when 'cindent' is set, because we might need to re-indent
  2832.      * at a ':', or any other character.
  2833.      */
  2834. #define INPUT_BUFLEN 100
  2835.     if (!ISSPECIAL(c) && vpeekc() != NUL && State != REPLACE
  2836. #ifdef CINDENT
  2837.                                             && !curbuf->b_p_cin
  2838. #endif
  2839. #ifdef RIGHTLEFT
  2840.                                             && !p_ri
  2841. #endif
  2842.                                                                         )
  2843.     {
  2844.         char_u            p[INPUT_BUFLEN + 1];
  2845.         int             i;
  2846.  
  2847.         p[0] = c;
  2848.         i = 1;
  2849.         while ((c = vpeekc()) != NUL && !ISSPECIAL(c) && i < INPUT_BUFLEN &&
  2850.                                                             (textwidth == 0 ||
  2851.             (curwin->w_virtcol += charsize(p[i - 1])) < (colnr_t)textwidth) &&
  2852.                     !(!no_abbr && !iswordchar(c) && iswordchar(p[i - 1])))
  2853.         {
  2854. #ifdef RIGHTLEFT
  2855.             c = vgetc();
  2856.             if (p_hkmap && KeyTyped)
  2857.                 c = hkmap(c);                /* Hebrew mode mapping */
  2858.             p[i++] = c;
  2859. #else
  2860.              p[i++] = vgetc();
  2861. #endif
  2862.         }
  2863.             
  2864. #ifdef DIGRAPHS
  2865.         do_digraph(-1);                    /* clear digraphs */
  2866.         do_digraph(p[i-1]);                /* may be the start of a digraph */
  2867. #endif
  2868.         p[i] = '\0';
  2869.         ins_str(p);
  2870.         AppendToRedobuff(p);
  2871.     }
  2872.     else
  2873.     {
  2874.         ins_char(c);
  2875.         AppendCharToRedobuff(c);
  2876.     }
  2877. }
  2878.  
  2879. /*
  2880.  * start_arrow() is called when an arrow key is used in insert mode.
  2881.  * It resembles hitting the <ESC> key.
  2882.  */
  2883.     static void
  2884. start_arrow(end_insert_pos)
  2885.     FPOS    *end_insert_pos;
  2886. {
  2887.     if (!arrow_used)        /* something has been inserted */
  2888.     {
  2889.         AppendToRedobuff(ESC_STR);
  2890.         arrow_used = TRUE;        /* this means we stopped the current insert */
  2891.         stop_insert(end_insert_pos);
  2892.     }
  2893. }
  2894.  
  2895. /*
  2896.  * stop_arrow() is called before a change is made in insert mode.
  2897.  * If an arrow key has been used, start a new insertion.
  2898.  */
  2899.     static void
  2900. stop_arrow()
  2901. {
  2902.     if (arrow_used)
  2903.     {
  2904.         (void)u_save_cursor();                /* errors are ignored! */
  2905.         Insstart = curwin->w_cursor;    /* new insertion starts here */
  2906.         Insstart_textlen = linetabsize(ml_get_curline());
  2907.         ResetRedobuff();
  2908.         AppendToRedobuff((char_u *)"1i");    /* pretend we start an insertion */
  2909.         arrow_used = FALSE;
  2910.     }
  2911. }
  2912.  
  2913. /*
  2914.  * do a few things to stop inserting
  2915.  */
  2916.     static void
  2917. stop_insert(end_insert_pos)
  2918.     FPOS    *end_insert_pos;        /* where insert ended */
  2919. {
  2920.     stop_redo_ins();
  2921.  
  2922.     /*
  2923.      * save the inserted text for later redo with ^@
  2924.      */
  2925.     vim_free(last_insert);
  2926.     last_insert = get_inserted();
  2927.     last_insert_skip = new_insert_skip;
  2928.  
  2929.     /*
  2930.      * If we just did an auto-indent, remove the white space from the end of
  2931.      * the line, and put the cursor back.
  2932.      */
  2933.     if (did_ai && !arrow_used)
  2934.     {
  2935.         if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
  2936.             --curwin->w_cursor.col;
  2937.         while (vim_iswhite(gchar_cursor()))
  2938.             delchar(TRUE);
  2939.         if (gchar_cursor() != NUL)
  2940.             ++curwin->w_cursor.col;        /* put cursor back on the NUL */
  2941.         if (curwin->w_p_list)            /* the deletion is only seen in list
  2942.                                          * mode */
  2943.             updateline();
  2944.     }
  2945.     did_ai = FALSE;
  2946.     did_si = FALSE;
  2947.     can_si = FALSE;
  2948.     can_si_back = FALSE;
  2949.  
  2950.     /* set '[ and '] to the inserted text */
  2951.     curbuf->b_op_start = Insstart;
  2952.     curbuf->b_op_end = *end_insert_pos;
  2953. }
  2954.  
  2955. /*
  2956.  * Set the last inserted text to a single character.
  2957.  * Used for the replace command.
  2958.  */
  2959.     void
  2960. set_last_insert(c)
  2961.     int        c;
  2962. {
  2963.     vim_free(last_insert);
  2964.     last_insert = alloc(4);
  2965.     if (last_insert != NULL)
  2966.     {
  2967.         last_insert[0] = Ctrl('V');
  2968.         last_insert[1] = c;
  2969.         last_insert[2] = ESC;
  2970.         last_insert[3] = NUL;
  2971.             /* Use the CTRL-V only when not entering a digit */
  2972.         last_insert_skip = isdigit(c) ? 1 : 0;
  2973.     }
  2974. }
  2975.  
  2976. /*
  2977.  * move cursor to start of line
  2978.  * if flag == TRUE move to first non-white
  2979.  * if flag == MAYBE then move to first non-white if startofline is set,
  2980.  *        otherwise don't move at all.
  2981.  */
  2982.     void
  2983. beginline(flag)
  2984.     int            flag;
  2985. {
  2986.     if (flag == MAYBE && !p_sol)
  2987.         coladvance(curwin->w_curswant);
  2988.     else
  2989.     {
  2990.         curwin->w_cursor.col = 0;
  2991.         if (flag)
  2992.         {
  2993.             register char_u *ptr;
  2994.  
  2995.             for (ptr = ml_get_curline(); vim_iswhite(*ptr); ++ptr)
  2996.                 ++curwin->w_cursor.col;
  2997.         }
  2998.         curwin->w_set_curswant = TRUE;
  2999.     }
  3000. }
  3001.  
  3002. /*
  3003.  * oneright oneleft cursor_down cursor_up
  3004.  *
  3005.  * Move one char {right,left,down,up}.
  3006.  * Return OK when successful, FAIL when we hit a line of file boundary.
  3007.  */
  3008.  
  3009.     int
  3010. oneright()
  3011. {
  3012.     char_u *ptr;
  3013.  
  3014.     ptr = ml_get_cursor();
  3015.     if (*ptr++ == NUL || *ptr == NUL)
  3016.         return FAIL;
  3017.     curwin->w_set_curswant = TRUE;
  3018.     ++curwin->w_cursor.col;
  3019.     return OK;
  3020. }
  3021.  
  3022.     int
  3023. oneleft()
  3024. {
  3025.     if (curwin->w_cursor.col == 0)
  3026.         return FAIL;
  3027.     curwin->w_set_curswant = TRUE;
  3028.     --curwin->w_cursor.col;
  3029.     return OK;
  3030. }
  3031.  
  3032.     int
  3033. cursor_up(n)
  3034.     long n;
  3035. {
  3036.     if (n != 0 && curwin->w_cursor.lnum == 1)
  3037.         return FAIL;
  3038.     if (n >= curwin->w_cursor.lnum)
  3039.         curwin->w_cursor.lnum = 1;
  3040.     else
  3041.         curwin->w_cursor.lnum -= n;
  3042.  
  3043.     /* try to advance to the column we want to be at */
  3044.     coladvance(curwin->w_curswant);
  3045.  
  3046.     if (op_type == NOP)
  3047.         cursupdate();                /* make sure curwin->w_topline is valid */
  3048.  
  3049.     return OK;
  3050. }
  3051.  
  3052.     int
  3053. cursor_down(n)
  3054.     long n;
  3055. {
  3056.     if (n != 0 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
  3057.         return FAIL;
  3058.     curwin->w_cursor.lnum += n;
  3059.     if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
  3060.         curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
  3061.  
  3062.     /* try to advance to the column we want to be at */
  3063.     coladvance(curwin->w_curswant);
  3064.  
  3065.     if (op_type == NOP)
  3066.         cursupdate();                /* make sure curwin->w_topline is valid */
  3067.  
  3068.     return OK;
  3069. }
  3070.  
  3071. /*
  3072.  * screengo() --
  3073.  *
  3074.  * move 'dist' lines in direction 'dir', counting lines by *screen*
  3075.  * lines rather than lines in the file
  3076.  * 'dist' must be positive.
  3077.  *
  3078.  * return OK if able to move cursor, FAIL otherwise.
  3079.  */
  3080.  
  3081.     int
  3082. screengo(dir, dist)
  3083.     int        dir;
  3084.     long    dist;
  3085. {
  3086.     int        linelen = linetabsize(ml_get_curline());
  3087.     int        retval = OK;
  3088.     int        atend = FALSE;
  3089.     int        n;
  3090.  
  3091.     op_motion_type = MCHAR;
  3092.     op_inclusive = FALSE;
  3093.  
  3094.     /*
  3095.      * Instead of sticking at the last character of the line in the file we
  3096.      * try to stick in the last column of the screen
  3097.      */
  3098.     if (curwin->w_curswant == MAXCOL)
  3099.     {
  3100.         atend = TRUE;
  3101.         curwin->w_curswant = ((curwin->w_virtcol +
  3102.                        (curwin->w_p_nu ? 8 : 0)) / Columns + 1) * Columns - 1;
  3103.         if (curwin->w_p_nu && curwin->w_curswant > 8)
  3104.             curwin->w_curswant -= 8;
  3105.     }
  3106.     else
  3107.         while (curwin->w_curswant >= (colnr_t)(linelen + Columns))
  3108.             curwin->w_curswant -= Columns;
  3109.  
  3110.     while (dist--)
  3111.     {
  3112.         if (dir == BACKWARD)
  3113.         {
  3114.                                                 /* move back within line */
  3115.             if ((long)curwin->w_curswant >= Columns)
  3116.                 curwin->w_curswant -= Columns;
  3117.             else                                /* to previous line */
  3118.             {
  3119.                 if (curwin->w_cursor.lnum == 1)
  3120.                 {
  3121.                     retval = FAIL;
  3122.                     break;
  3123.                 }
  3124.                 --curwin->w_cursor.lnum;
  3125.                 linelen = linetabsize(ml_get_curline());
  3126.                 n = ((linelen + (curwin->w_p_nu ? 8 : 0) - 1) / Columns)
  3127.                                                                     * Columns;
  3128.                 if (curwin->w_p_nu &&
  3129.                                  (long)curwin->w_curswant >= Columns - 8 && n)
  3130.                     n -= Columns;
  3131.                 curwin->w_curswant += n;
  3132.             }
  3133.         }
  3134.         else /* dir == FORWARD */
  3135.         {
  3136.             n = ((linelen + (curwin->w_p_nu ? 8 : 0) - 1) / Columns) * Columns;
  3137.             if (curwin->w_p_nu && n > 8)
  3138.                 n -= 8;
  3139.                                                 /* move forward within line */
  3140.             if (curwin->w_curswant < (colnr_t)n)
  3141.                 curwin->w_curswant += Columns;
  3142.             else                                /* to next line */
  3143.             {
  3144.                 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
  3145.                 {
  3146.                     retval = FAIL;
  3147.                     break;
  3148.                 }
  3149.                 curwin->w_cursor.lnum++;
  3150.                 linelen = linetabsize(ml_get_curline());
  3151.                 curwin->w_curswant %= Columns;
  3152.             }
  3153.         }
  3154.     }
  3155.     coladvance(curwin->w_curswant);
  3156.     if (atend)
  3157.         curwin->w_curswant = MAXCOL;        /* stick in the last column */
  3158.     if (op_type == NOP)
  3159.         cursupdate();
  3160.     return retval;
  3161. }
  3162.  
  3163. /*
  3164.  * move screen 'count' pages up or down and update screen
  3165.  *
  3166.  * return FAIL for failure, OK otherwise
  3167.  */
  3168.     int
  3169. onepage(dir, count)
  3170.     int        dir;
  3171.     long    count;
  3172. {
  3173.     linenr_t        lp;
  3174.     long            n;
  3175.     int                off;
  3176.     int                retval = OK;
  3177.  
  3178.     if (curbuf->b_ml.ml_line_count == 1)    /* nothing to do */
  3179.     {
  3180.         beep_flush();
  3181.         return FAIL;
  3182.     }
  3183.  
  3184.     for ( ; count > 0; --count)
  3185.     {
  3186.         /*
  3187.          * It's an error to move a page up when the first line is already on
  3188.          * the screen.  It's an error to move a page down when the last line
  3189.          * is on the screen and the topline is 'scrolloff' lines from the
  3190.          * last line.
  3191.          */
  3192.         if (dir == FORWARD
  3193.                 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so) &&
  3194.                         curwin->w_botline > curbuf->b_ml.ml_line_count)
  3195.                 : (curwin->w_topline == 1))
  3196.         {
  3197.             beep_flush();
  3198.             retval = FAIL;
  3199.             break;
  3200.         }
  3201.         if (dir == FORWARD)
  3202.         {
  3203.                                         /* at end of file */
  3204.             if (curwin->w_botline > curbuf->b_ml.ml_line_count)
  3205.                 curwin->w_topline = curbuf->b_ml.ml_line_count;
  3206.             else
  3207.             {
  3208.                 /*
  3209.                  * When there are three or less lines on the screen, move them
  3210.                  * all to above the screen.
  3211.                  */
  3212.                 if (curwin->w_botline - curwin->w_topline <= 3)
  3213.                     off = 0;
  3214.                 /*
  3215.                  * Make sure at least w_botline gets onto the screen, also
  3216.                  * when 'scrolloff' is non-zero and with very long lines.
  3217.                  */
  3218.                 else if (plines(curwin->w_botline) +
  3219.                         plines(curwin->w_botline - 1) +
  3220.                         plines(curwin->w_botline - 2) >= curwin->w_height - 2)
  3221.                     off = 0;
  3222.                 else
  3223.                     off = 2;
  3224.                 curwin->w_topline = curwin->w_botline - off;
  3225.                 curwin->w_cursor.lnum = curwin->w_topline;
  3226.             }
  3227.             comp_Botline(curwin);
  3228.         }
  3229.         else    /* dir == BACKWARDS */
  3230.         {
  3231.             lp = curwin->w_topline;
  3232.             /*
  3233.              * If the first two lines on the screen are not too big, we keep
  3234.              * them on the screen.
  3235.              */
  3236.             if ((n = plines(lp)) > curwin->w_height / 2)
  3237.                 --lp;
  3238.             else if (lp < curbuf->b_ml.ml_line_count &&
  3239.                                     n + plines(lp + 1) < curwin->w_height / 2)
  3240.                 ++lp;
  3241.             curwin->w_cursor.lnum = lp;
  3242.             n = 0;
  3243.             while (n <= curwin->w_height && lp >= 1)
  3244.             {
  3245.                 n += plines(lp);
  3246.                 --lp;
  3247.             }
  3248.             if (n <= curwin->w_height)                /* at begin of file */
  3249.             {
  3250.                 curwin->w_topline = 1;
  3251.                 comp_Botline(curwin);
  3252.             }
  3253.             else if (lp >= curwin->w_topline - 2)    /* very long lines */
  3254.             {
  3255.                 --curwin->w_topline;
  3256.                 comp_Botline(curwin);
  3257.                 curwin->w_cursor.lnum = curwin->w_botline - 1;
  3258.             }
  3259.             else
  3260.             {
  3261.                 curwin->w_topline = lp + 2;
  3262.                 comp_Botline(curwin);
  3263.             }
  3264.         }
  3265.     }
  3266.     cursor_correct();
  3267.     beginline(MAYBE);
  3268.     /*
  3269.      * Avoid the screen jumping up and down when 'scrolloff' is non-zero.
  3270.      */
  3271.     if (dir == FORWARD && curwin->w_cursor.lnum < curwin->w_topline + p_so)
  3272.         scroll_cursor_top(1, FALSE);
  3273.     updateScreen(VALID);
  3274.     return retval;
  3275. }
  3276.  
  3277. /* #define KEEP_SCREEN_LINE */
  3278.  
  3279.     void
  3280. halfpage(flag, Prenum)
  3281.     int            flag;
  3282.     linenr_t    Prenum;
  3283. {
  3284.     long        scrolled = 0;
  3285.     int            i;
  3286.     int            n;
  3287.  
  3288.     if (Prenum)
  3289.         curwin->w_p_scroll = (Prenum > curwin->w_height) ?
  3290.                                                 curwin->w_height : Prenum;
  3291.     n = (curwin->w_p_scroll <= curwin->w_height) ?
  3292.                                     curwin->w_p_scroll : curwin->w_height;
  3293.  
  3294.     if (flag)        /* scroll down */
  3295.     {
  3296.         while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
  3297.         {
  3298.             i = plines(curwin->w_topline);
  3299.             n -= i;
  3300.             if (n < 0 && scrolled)
  3301.                 break;
  3302.             scrolled += i;
  3303.             ++curwin->w_topline;
  3304.             comp_Botline(curwin);        /* compute curwin->w_botline */
  3305. #ifndef KEEP_SCREEN_LINE
  3306.             if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
  3307.                 ++curwin->w_cursor.lnum;
  3308. #endif
  3309.         }
  3310. #ifndef KEEP_SCREEN_LINE
  3311.         /*
  3312.          * When hit bottom of the file: move cursor down.
  3313.          */
  3314.         if (n > 0)
  3315.         {
  3316.             curwin->w_cursor.lnum += n;
  3317.             if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
  3318.                 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
  3319.         }
  3320. #else
  3321.             /* try to put the cursor in the same screen line */
  3322.         while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
  3323.                              && curwin->w_cursor.lnum < curwin->w_botline - 1)
  3324.         {
  3325.             scrolled -= plines(curwin->w_cursor.lnum);
  3326.             if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
  3327.                 break;
  3328.             ++curwin->w_cursor.lnum;
  3329.         }
  3330. #endif
  3331.     }
  3332.     else            /* scroll up */
  3333.     {
  3334.         while (n > 0 && curwin->w_topline > 1)
  3335.         {
  3336.             i = plines(curwin->w_topline - 1);
  3337.             n -= i;
  3338.             if (n < 0 && scrolled)
  3339.                 break;
  3340.             scrolled += i;
  3341.             --curwin->w_topline;
  3342. #ifndef KEEP_SCREEN_LINE
  3343.             if (curwin->w_cursor.lnum > 1)
  3344.                 --curwin->w_cursor.lnum;
  3345. #endif
  3346.         }
  3347.         comp_Botline(curwin);        /* compute curwin->w_botline */
  3348. #ifndef KEEP_SCREEN_LINE
  3349.         /*
  3350.          * When hit top of the file: move cursor up.
  3351.          */
  3352.         if (n > 0)
  3353.         {
  3354.             if (curwin->w_cursor.lnum > (linenr_t)n)
  3355.                 curwin->w_cursor.lnum -= n;
  3356.             else
  3357.                 curwin->w_cursor.lnum = 1;
  3358.         }
  3359. #else
  3360.             /* try to put the cursor in the same screen line */
  3361.         scrolled += n;        /* move cursor when topline is 1 */
  3362.         while (curwin->w_cursor.lnum > curwin->w_topline &&
  3363.                  (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
  3364.         {
  3365.             scrolled -= plines(curwin->w_cursor.lnum - 1);
  3366.             if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
  3367.                 break;
  3368.             --curwin->w_cursor.lnum;
  3369.         }
  3370. #endif
  3371.     }
  3372.     cursor_correct();
  3373.     beginline(MAYBE);
  3374.     updateScreen(VALID);
  3375. }
  3376.  
  3377. /*
  3378.  * Stuff the last inserted text in the read buffer.
  3379.  * Last_insert actually is a copy of the redo buffer, so we
  3380.  * first have to remove the command.
  3381.  */
  3382.     int
  3383. stuff_inserted(c, count, no_esc)
  3384.     int        c;
  3385.     long    count;
  3386.     int        no_esc;
  3387. {
  3388.     char_u        *esc_ptr = NULL;
  3389.     char_u        *ptr;
  3390.  
  3391.     ptr = get_last_insert();
  3392.     if (ptr == NULL)
  3393.     {
  3394.         EMSG(e_noinstext);
  3395.         return FAIL;
  3396.     }
  3397.  
  3398.     if (c)
  3399.         stuffcharReadbuff(c);
  3400.     if (no_esc && (esc_ptr = (char_u *)vim_strrchr(ptr, 27)) != NULL)
  3401.         *esc_ptr = NUL;        /* remove the ESC */
  3402.  
  3403.     do
  3404.         stuffReadbuff(ptr);
  3405.     while (--count > 0);
  3406.  
  3407.     if (esc_ptr != NULL)
  3408.         *esc_ptr = 27;        /* put the ESC back */
  3409.  
  3410.     return OK;
  3411. }
  3412.  
  3413.     char_u *
  3414. get_last_insert()
  3415. {
  3416.     if (last_insert == NULL)
  3417.         return NULL;
  3418.     return last_insert + last_insert_skip;
  3419. }
  3420.  
  3421. /*
  3422.  * Check the word in front of the cursor for an abbreviation.
  3423.  * Called when the non-id character "c" has been entered.
  3424.  * When an abbreviation is recognized it is removed from the text and
  3425.  * the replacement string is inserted in typebuf[], followed by "c".
  3426.  */
  3427.     static int
  3428. echeck_abbr(c)
  3429.     int c;
  3430. {
  3431.     if (p_paste || no_abbr)            /* no abbreviations or in paste mode */
  3432.         return FALSE;
  3433.  
  3434.     return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
  3435.                 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
  3436. }
  3437.  
  3438. /*
  3439.  * replace-stack functions
  3440.  *
  3441.  * When replacing characters the replaced character is remembered
  3442.  * for each new character. This is used to re-insert the old text
  3443.  * when backspacing.
  3444.  *
  3445.  * replace_offset is normally 0, in which case replace_push will add a new
  3446.  * character at the end of the stack. If replace_offset is not 0, that many
  3447.  * characters will be left on the stack above the newly inserted character.
  3448.  */
  3449.  
  3450. char_u    *replace_stack = NULL;
  3451. long    replace_stack_nr = 0;        /* next entry in replace stack */
  3452. long    replace_stack_len = 0;        /* max. number of entries */
  3453.  
  3454.     void
  3455. replace_push(c)
  3456.     int        c;        /* character that is replaced (NUL is none) */
  3457. {
  3458.     char_u    *p;
  3459.  
  3460.     if (replace_stack_nr < replace_offset)        /* nothing to do */
  3461.         return;
  3462.     if (replace_stack_len <= replace_stack_nr)
  3463.     {
  3464.         replace_stack_len += 50;
  3465.         p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
  3466.         if (p == NULL)        /* out of memory */
  3467.         {
  3468.             replace_stack_len -= 50;
  3469.             return;
  3470.         }
  3471.         if (replace_stack != NULL)
  3472.         {
  3473.             vim_memmove(p, replace_stack,
  3474.                                  (size_t)(replace_stack_nr * sizeof(char_u)));
  3475.             vim_free(replace_stack);
  3476.         }
  3477.         replace_stack = p;
  3478.     }
  3479.     p = replace_stack + replace_stack_nr - replace_offset;
  3480.     if (replace_offset)
  3481.         vim_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
  3482.     *p = c;
  3483.     ++replace_stack_nr;
  3484. }
  3485.  
  3486. /*
  3487.  * pop one item from the replace stack
  3488.  * return -1 if stack empty
  3489.  * return 0 if no character was replaced
  3490.  * return replaced character otherwise
  3491.  */
  3492.     int
  3493. replace_pop()
  3494. {
  3495.     if (replace_stack_nr == 0)
  3496.         return -1;
  3497.     return (int)replace_stack[--replace_stack_nr];
  3498. }
  3499.  
  3500. /*
  3501.  * make the replace stack empty
  3502.  * (called when exiting replace mode)
  3503.  */
  3504.     void
  3505. replace_flush()
  3506. {
  3507.     vim_free(replace_stack);
  3508.     replace_stack = NULL;
  3509.     replace_stack_len = 0;
  3510.     replace_stack_nr = 0;
  3511. }
  3512.  
  3513. #if defined(LISPINDENT) || defined(CINDENT)
  3514. /*
  3515.  * Re-indent the current line, based on the current contents of it and the
  3516.  * surrounding lines. Fixing the cursor position seems really easy -- I'm very
  3517.  * confused what all the part that handles Control-T is doing that I'm not.
  3518.  * "get_the_indent" should be get_c_indent or get_lisp_indent.
  3519.  */
  3520.  
  3521.     void
  3522. fixthisline(get_the_indent)
  3523.     int (*get_the_indent) __ARGS((void));
  3524. {
  3525.     change_indent(INDENT_SET, get_the_indent(), FALSE);
  3526.     if (linewhite(curwin->w_cursor.lnum))
  3527.         did_ai = TRUE;        /* delete the indent if the line stays empty */
  3528. }
  3529. #endif /* defined(LISPINDENT) || defined(CINDENT) */
  3530.  
  3531. #ifdef CINDENT
  3532. /*
  3533.  * return TRUE if 'cinkeys' contains the key "keytyped",
  3534.  * when == '*':        Only if key is preceded with '*'    (indent before insert)
  3535.  * when == '!':        Only if key is prededed with '!'    (don't insert)
  3536.  * when == ' ':        Only if key is not preceded with '*'(indent afterwards)
  3537.  *
  3538.  * If line_is_empty is TRUE accept keys with '0' before them.
  3539.  */
  3540.     int
  3541. in_cinkeys(keytyped, when, line_is_empty)
  3542.     int            keytyped;
  3543.     int            when;
  3544.     int            line_is_empty;
  3545. {
  3546.     char_u    *look;
  3547.     int        try_match;
  3548.     char_u    *p;
  3549.  
  3550.     for (look = curbuf->b_p_cink; *look; )
  3551.     {
  3552.         /*
  3553.          * Find out if we want to try a match with this key, depending on
  3554.          * 'when' and a '*' or '!' before the key.
  3555.          */
  3556.         switch (when)
  3557.         {
  3558.             case '*': try_match = (*look == '*'); break;
  3559.             case '!': try_match = (*look == '!'); break;
  3560.              default: try_match = (*look != '*'); break;
  3561.         }
  3562.         if (*look == '*' || *look == '!')
  3563.             ++look;
  3564.  
  3565.         /*
  3566.          * If there is a '0', only accept a match if the line is empty.
  3567.          */
  3568.         if (*look == '0')
  3569.         {
  3570.             if (!line_is_empty)
  3571.                 try_match = FALSE;
  3572.             ++look;
  3573.         }
  3574.  
  3575.         /*
  3576.          * does it look like a control character?
  3577.          */
  3578.         if (*look == '^' && look[1] >= '@' && look[1] <= '_')
  3579.         {
  3580.             if (try_match && keytyped == Ctrl(look[1]))
  3581.                 return TRUE;
  3582.             look += 2;
  3583.         }
  3584.         /*
  3585.          * 'o' means "o" command, open forward.
  3586.          * 'O' means "O" command, open backward.
  3587.          */
  3588.         else if (*look == 'o')
  3589.         {
  3590.             if (try_match && keytyped == KEY_OPEN_FORW)
  3591.                 return TRUE;
  3592.             ++look;
  3593.         }
  3594.         else if (*look == 'O')
  3595.         {
  3596.             if (try_match && keytyped == KEY_OPEN_BACK)
  3597.                 return TRUE;
  3598.             ++look;
  3599.         }
  3600.  
  3601.         /*
  3602.          * 'e' means to check for "else" at start of line and just before the
  3603.          * cursor.
  3604.          */
  3605.         else if (*look == 'e')
  3606.         {
  3607.             if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
  3608.             {
  3609.                 p = ml_get_curline();
  3610.                 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
  3611.                         STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
  3612.                     return TRUE;
  3613.             }
  3614.             ++look;
  3615.         }
  3616.  
  3617.         /*
  3618.          * ':' only causes an indent if it is at the end of a label or case
  3619.          * statement.
  3620.          */
  3621.         else if (*look == ':')
  3622.         {
  3623.             if (try_match && keytyped == ':')
  3624.             {
  3625.                 p = ml_get_curline();
  3626.                 if (iscase(p) || islabel(30))
  3627.                     return TRUE;
  3628.             }
  3629.             ++look;
  3630.         }
  3631.  
  3632.  
  3633.         /*
  3634.          * Is it a key in <>, maybe?
  3635.          */
  3636.         else if (*look == '<')
  3637.         {
  3638.             if (try_match)
  3639.             {
  3640.                 /*
  3641.                  * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>
  3642.                  * and <!> so that people can re-indent on o, O, e, 0, <, >, *
  3643.                  * and ! keys if they really really want to.
  3644.                  */
  3645.                 if (vim_strchr((char_u *)"<>!*oOe0", look[1]) != NULL &&
  3646.                                                           keytyped == look[1])
  3647.                     return TRUE;
  3648.  
  3649.                 if (keytyped == get_special_key_code(look + 1))
  3650.                     return TRUE;
  3651.             }
  3652.             while (*look && *look != '>')
  3653.                 look++;
  3654.             while (*look == '>')
  3655.                 look++;
  3656.         }
  3657.  
  3658.         /*
  3659.          * ok, it's a boring generic character.
  3660.          */
  3661.         else
  3662.         {
  3663.             if (try_match && *look == keytyped)
  3664.                 return TRUE;
  3665.             ++look;
  3666.         }
  3667.  
  3668.         /*
  3669.          * Skip over ", ".
  3670.          */
  3671.         look = skip_to_option_part(look);
  3672.     }
  3673.     return FALSE;
  3674. }
  3675. #endif /* CINDENT */
  3676.  
  3677. #if defined(RIGHTLEFT) || defined(PROTO)
  3678. /*
  3679.  * Map Hebrew keyboard when in hkmap mode.
  3680.  */
  3681.     int
  3682. hkmap(c)
  3683.     int c;
  3684. {
  3685.     switch(c)
  3686.     {
  3687.         case '`':    return ';';
  3688.         case '/':    return '.';
  3689.         case '\'':    return ',';
  3690.         case 'q':    return '/';
  3691.         case 'w':    return '\'';
  3692.  
  3693.         /* Hebrew letters - set offset from 'a' */
  3694.         case ',':    c = '{'; break;
  3695.         case '.':    c = 'v'; break;
  3696.         case ';':    c = 't'; break;
  3697.         default: {
  3698.                     static char str[] = "zqbcxlsjphmkwonu ydafe rig";
  3699.  
  3700.                     if (c < 'a' || c > 'z')
  3701.                         return c;
  3702.                     c = str[c - 'a'];
  3703.                     break;
  3704.                 }
  3705.         }
  3706.  
  3707.     return c - 'a' + p_aleph;
  3708. }
  3709. #endif
  3710.