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 / ops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-05  |  61.3 KB  |  2,688 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.  * ops.c: implementation of various operators: do_shift, do_delete, do_tilde,
  11.  *          do_change, do_yank, do_put, do_join
  12.  */
  13.  
  14. #include "vim.h"
  15. #include "globals.h"
  16. #include "proto.h"
  17. #include "option.h"
  18. #include "ops.h"
  19.  
  20. /*
  21.  * Number of registers.
  22.  *      0 = unnamed register, for normal yanks and puts
  23.  *   1..9 = number registers, for deletes
  24.  * 10..35 = named registers
  25.  *     36 = delete register (-)
  26.  *     37 = GUI selection register (*). Only if USE_GUI defined
  27.  */
  28. #ifdef USE_GUI
  29. # define NUM_REGISTERS            38
  30. #else
  31. # define NUM_REGISTERS            37
  32. #endif
  33.  
  34. /*
  35.  * Symbolic names for some registers.
  36.  */
  37. #define DELETION_REGISTER        36
  38. #ifdef USE_GUI
  39. # define GUI_SELECTION_REGISTER    37
  40. #endif
  41.  
  42. /*
  43.  * Each yank buffer is an array of pointers to lines.
  44.  */
  45. static struct yankbuf
  46. {
  47.     char_u        **y_array;        /* pointer to array of line pointers */
  48.     linenr_t     y_size;         /* number of lines in y_array */
  49.     char_u        y_type;         /* MLINE, MCHAR or MBLOCK */
  50. } y_buf[NUM_REGISTERS];
  51.  
  52. static struct    yankbuf *y_current;            /* ptr to current yank buffer */
  53. static int        yankappend;                    /* TRUE when appending */
  54. static struct    yankbuf *y_previous = NULL; /* ptr to last written yank buffr */
  55.  
  56. /*
  57.  * structure used by block_prep, do_delete and do_yank for blockwise operators
  58.  */
  59. struct block_def
  60. {
  61.     int            startspaces;
  62.     int            endspaces;
  63.     int            textlen;
  64.     char_u        *textstart;
  65.     colnr_t        textcol;
  66. };
  67.  
  68. static void        get_yank_buffer __ARGS((int));
  69. static int        stuff_yank __ARGS((int, char_u *));
  70. static void        free_yank __ARGS((long));
  71. static void        free_yank_all __ARGS((void));
  72. static void        block_prep __ARGS((struct block_def *, linenr_t, int));
  73. static int        same_leader __ARGS((int, char_u *, int, char_u *));
  74. static int        fmt_end_block __ARGS((linenr_t, int *, char_u **));
  75.  
  76. /*
  77.  * do_shift - handle a shift operation
  78.  */
  79.     void
  80. do_shift(op, curs_top, amount)
  81.     int             op;
  82.     int                curs_top;
  83.     int                amount;
  84. {
  85.     register long    i;
  86.     int                first_char;
  87.  
  88.     if (u_save((linenr_t)(curwin->w_cursor.lnum - 1),
  89.                    (linenr_t)(curwin->w_cursor.lnum + op_line_count)) == FAIL)
  90.         return;
  91.     for (i = op_line_count; --i >= 0; )
  92.     {
  93.         first_char = *ml_get_curline();
  94.         if (first_char == NUL)                            /* empty line */
  95.             curwin->w_cursor.col = 0;
  96.         /*
  97.          * Don't move the line right if it starts with # and p_si is set.
  98.          */
  99.         else
  100. #if defined(SMARTINDENT) || defined(CINDENT)
  101.             if (first_char != '#' || (
  102. # ifdef SMARTINDENT
  103.                          !curbuf->b_p_si
  104. # endif
  105. # if defined(SMARTINDENT) && defined(CINDENT)
  106.                              &&
  107. # endif
  108. # ifdef CINDENT
  109.                          (!curbuf->b_p_cin || !in_cinkeys('#', ' ', TRUE))
  110. # endif
  111.                                         ))
  112. #endif
  113.         {
  114.             /* if (op_block_mode)
  115.                     shift the block, not the whole line
  116.             else */
  117.                 shift_line(op == LSHIFT, p_sr, amount);
  118.         }
  119.         ++curwin->w_cursor.lnum;
  120.     }
  121.  
  122.     if (curs_top)            /* put cursor on first line, for ">>" */
  123.     {
  124.         curwin->w_cursor.lnum -= op_line_count;
  125.         beginline(MAYBE);    /* shift_line() may have changed cursor.col */
  126.     }
  127.     else
  128.         --curwin->w_cursor.lnum;        /* put cursor on last line, for ":>" */
  129.     updateScreen(CURSUPD);
  130.  
  131.     if (op_line_count > p_report)
  132.        smsg((char_u *)"%ld line%s %ced %d time%s", op_line_count,
  133.                            plural(op_line_count), (op == RSHIFT) ? '>' : '<',
  134.                            amount, plural((long)amount));
  135. }
  136.  
  137. /*
  138.  * shift the current line one shiftwidth left (if left != 0) or right
  139.  * leaves cursor on first blank in the line
  140.  */
  141.     void
  142. shift_line(left, round, amount)
  143.     int left;
  144.     int    round;
  145.     int    amount;
  146. {
  147.     register int count;
  148.     register int i, j;
  149.     int p_sw = (int)curbuf->b_p_sw;
  150.  
  151.     count = get_indent();            /* get current indent */
  152.  
  153.     if (round)                        /* round off indent */
  154.     {
  155.         i = count / p_sw;            /* number of p_sw rounded down */
  156.         j = count % p_sw;            /* extra spaces */
  157.         if (j && left)                /* first remove extra spaces */
  158.             --amount;
  159.         if (left)
  160.         {
  161.             i -= amount;
  162.             if (i < 0)
  163.                 i = 0;
  164.         }
  165.         else
  166.             i += amount;
  167.         count = i * p_sw;
  168.     }
  169.     else                /* original vi indent */
  170.     {
  171.         if (left)
  172.         {
  173.             count -= p_sw * amount;
  174.             if (count < 0)
  175.                 count = 0;
  176.         }
  177.         else
  178.             count += p_sw * amount;
  179.     }
  180.     set_indent(count, TRUE);        /* set new indent */
  181. }
  182.  
  183. #if defined(LISPINDENT) || defined(CINDENT)
  184. /*
  185.  * do_reindent - handle reindenting a block of lines for C or lisp.
  186.  *
  187.  * mechanism copied from do_shift, above
  188.  */
  189.     void
  190. do_reindent(how)
  191.     int (*how) __ARGS((void));
  192. {
  193.     register long   i;
  194.     char_u            *l;
  195.     int                count;
  196.  
  197.     if (u_save((linenr_t)(curwin->w_cursor.lnum - 1),
  198.                     (linenr_t)(curwin->w_cursor.lnum + op_line_count)) == FAIL)
  199.         return;
  200.  
  201.     for (i = op_line_count; --i >= 0 && !got_int; )
  202.     {
  203.         /* it's a slow thing to do, so give feedback so there's no worry that
  204.          * the computer's just hung. */
  205.  
  206.         if ((i % 50 == 0 || i == op_line_count - 1) && op_line_count > p_report)
  207.             smsg((char_u *)"%ld line%s to indent... ", i, plural(i));
  208.  
  209.         /*
  210.          * Be vi-compatible: For lisp indenting the first line is not
  211.          * indented, unless there is only one line.
  212.          */
  213. #ifdef LISPINDENT
  214.         if (i != op_line_count - 1 || op_line_count == 1 ||
  215.                                                        how != get_lisp_indent)
  216. #endif
  217.         {
  218.             l = skipwhite(ml_get_curline());
  219.             if (*l == NUL)                    /* empty or blank line */
  220.                 count = 0;
  221.             else
  222.                 count = how();                /* get the indent for this line */
  223.             
  224.             set_indent(count, TRUE);
  225.         }
  226.         ++curwin->w_cursor.lnum;
  227.     }
  228.  
  229.     /* put cursor on first non-blank of indented line */
  230.     curwin->w_cursor.lnum -= op_line_count;
  231.     beginline(MAYBE);
  232.  
  233.     updateScreen(CURSUPD);
  234.  
  235.     if (op_line_count > p_report)
  236.     {
  237.         i = op_line_count - (i + 1);
  238.         smsg((char_u *)"%ld line%s indented ", i, plural(i));
  239.     }
  240. }
  241. #endif /* defined(LISPINDENT) || defined(CINDENT) */
  242.  
  243. /*
  244.  * check if character is name of yank buffer
  245.  * Note: There is no check for 0 (default register), caller should do this
  246.  */
  247.      int
  248. is_yank_buffer(c, writing)
  249.     int        c;
  250.     int        writing;        /* if TRUE check for writable buffers */
  251. {
  252.     if (c > '~')
  253.         return FALSE;
  254.     if (isalnum(c) || (!writing && vim_strchr((char_u *)".%:", c) != NULL) ||
  255.                                                           c == '"' || c == '-'
  256. #ifdef USE_GUI
  257.                                                    || (gui.in_use && c == '*')
  258. #endif
  259.                                                         )
  260.         return TRUE;
  261.     return FALSE;
  262. }
  263.  
  264. /*
  265.  * Set y_current and yankappend, according to the value of yankbuffer.
  266.  *
  267.  * If yankbuffer is 0 and writing, use buffer 0
  268.  * If yankbuffer is 0 and reading, use previous buffer
  269.  */
  270.     static void
  271. get_yank_buffer(writing)
  272.     int        writing;
  273. {
  274.     register int i;
  275.  
  276.     yankappend = FALSE;
  277.     if (((yankbuffer == 0 && !writing) || yankbuffer == '"') &&
  278.                                                            y_previous != NULL)
  279.     {
  280.         y_current = y_previous;
  281.         return;
  282.     }
  283.     i = yankbuffer;
  284.     if (isdigit(i))
  285.         i -= '0';
  286.     else if (islower(i))
  287.         i -= 'a' - 10;
  288.     else if (isupper(i))
  289.     {
  290.         i -= 'A' - 10;
  291.         yankappend = TRUE;
  292.     }
  293.     else if (yankbuffer == '-')
  294.         i = DELETION_REGISTER;
  295. #ifdef USE_GUI
  296.     else if (gui.in_use && yankbuffer == '*')
  297.         i = GUI_SELECTION_REGISTER;
  298. #endif
  299.     else                /* not 0-9, a-z, A-Z or '-': use buffer 0 */
  300.         i = 0;
  301.     y_current = &(y_buf[i]);
  302.     if (writing)        /* remember the buffer we write into for do_put() */
  303.         y_previous = y_current;
  304. }
  305.  
  306. /*
  307.  * return TRUE if the current yank buffer has type MLINE
  308.  */
  309.     int
  310. yank_buffer_mline()
  311. {
  312.     if (yankbuffer != 0 && !is_yank_buffer(yankbuffer, FALSE))
  313.         return FALSE;
  314.     get_yank_buffer(FALSE);
  315.     return (y_current->y_type == MLINE);
  316. }
  317.  
  318. /*
  319.  * start or stop recording into a yank buffer
  320.  *
  321.  * return FAIL for failure, OK otherwise
  322.  */
  323.     int
  324. do_record(c)
  325.     int c;
  326. {
  327.     char_u        *p;
  328.     static int    bufname;
  329.     int            retval;
  330.  
  331.     if (Recording == FALSE)         /* start recording */
  332.     {
  333.                         /* registers 0-9, a-z and " are allowed */
  334.         if (c > '~' || (!isalnum(c) && c != '"'))
  335.             retval = FAIL;
  336.         else
  337.         {
  338.             Recording = TRUE;
  339.             showmode();
  340.             bufname = c;
  341.             retval = OK;
  342.         }
  343.     }
  344.     else                            /* stop recording */
  345.     {
  346.         Recording = FALSE;
  347.         MSG("");
  348.         p = get_recorded();
  349.         if (p == NULL)
  350.             retval = FAIL;
  351.         else
  352.             retval = (stuff_yank(bufname, p));
  353.     }
  354.     return retval;
  355. }
  356.  
  357. /*
  358.  * stuff string 'p' into yank buffer 'bufname' (append if uppercase)
  359.  * 'p' is assumed to be alloced.
  360.  *
  361.  * return FAIL for failure, OK otherwise
  362.  */
  363.     static int
  364. stuff_yank(bufname, p)
  365.     int bufname;
  366.     char_u *p;
  367. {
  368.     char_u *lp;
  369.     char_u **pp;
  370.  
  371.     yankbuffer = bufname;
  372.                                             /* check for read-only buffer */
  373.     if (yankbuffer != 0 && !is_yank_buffer(yankbuffer, TRUE))
  374.         return FAIL;
  375.     get_yank_buffer(TRUE);
  376.     if (yankappend && y_current->y_array != NULL)
  377.     {
  378.         pp = &(y_current->y_array[y_current->y_size - 1]);
  379.         lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
  380.         if (lp == NULL)
  381.         {
  382.             vim_free(p);
  383.             return FAIL;
  384.         }
  385.         STRCPY(lp, *pp);
  386.         STRCAT(lp, p);
  387.         vim_free(p);
  388.         vim_free(*pp);
  389.         *pp = lp;
  390.     }
  391.     else
  392.     {
  393.         free_yank_all();
  394.         if ((y_current->y_array =
  395.                         (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
  396.         {
  397.             vim_free(p);
  398.             return FAIL;
  399.         }
  400.         y_current->y_array[0] = p;
  401.         y_current->y_size = 1;
  402.         y_current->y_type = MCHAR;    /* used to be MLINE, why? */
  403.     }
  404.     return OK;
  405. }
  406.  
  407. /*
  408.  * execute a yank buffer (register): copy it into the stuff buffer
  409.  *
  410.  * return FAIL for failure, OK otherwise
  411.  */
  412.     int
  413. do_execbuf(c, colon, addcr)
  414.     int c;
  415.     int    colon;                /* insert ':' before each line */
  416.     int addcr;                /* always add '\n' to end of line */
  417. {
  418.     static int    lastc = NUL;
  419.     long        i;
  420.     char_u        *p;
  421.     int            truncated;
  422.     int            retval;
  423.  
  424.  
  425.     if (c == '@')                    /* repeat previous one */
  426.         c = lastc;
  427.     if (c == '%' || !is_yank_buffer(c, FALSE))    /* check for valid buffer */
  428.         return FAIL;
  429.     lastc = c;
  430.  
  431.     if (c == ':')                    /* use last command line */
  432.     {
  433.         if (last_cmdline == NULL)
  434.         {
  435.             EMSG(e_nolastcmd);
  436.             return FAIL;
  437.         }
  438.         vim_free(new_last_cmdline);    /* don't keep the cmdline containing @: */
  439.         new_last_cmdline = NULL;
  440.         if (ins_typebuf((char_u *)"\n", FALSE, 0, TRUE) == FAIL)
  441.             return FAIL;
  442.         if (ins_typebuf(last_cmdline, FALSE, 0, TRUE) == FAIL)
  443.             return FAIL;
  444.         if (ins_typebuf((char_u *)":", FALSE, 0, TRUE) == FAIL)
  445.             return FAIL;
  446.     }
  447.     else if (c == '.')                /* use last inserted text */
  448.     {
  449.         p = get_last_insert();
  450.         if (p == NULL)
  451.         {
  452.             EMSG(e_noinstext);
  453.             return FAIL;
  454.         }
  455.         i = STRLEN(p);
  456.         if (i > 0 && p[i - 1] == ESC)    /* remove trailing ESC */
  457.         {
  458.             p[i - 1] = NUL;
  459.             truncated = TRUE;
  460.         }
  461.         else
  462.             truncated = FALSE;
  463.         retval = ins_typebuf(p, FALSE, 0, TRUE);
  464.         if (truncated)
  465.             p[i - 1] = ESC;
  466.         return retval;
  467.     }
  468.     else
  469.     {
  470.         yankbuffer = c;
  471.         get_yank_buffer(FALSE);
  472.         if (y_current->y_array == NULL)
  473.             return FAIL;
  474.  
  475.         /*
  476.          * Insert lines into typeahead buffer, from last one to first one.
  477.          */
  478.         for (i = y_current->y_size; --i >= 0; )
  479.         {
  480.         /* insert newline between lines and after last line if type is MLINE */
  481.             if (y_current->y_type == MLINE || i < y_current->y_size - 1
  482.                                                                      || addcr)
  483.             {
  484.                 if (ins_typebuf((char_u *)"\n", FALSE, 0, TRUE) == FAIL)
  485.                     return FAIL;
  486.             }
  487.             if (ins_typebuf(y_current->y_array[i], FALSE, 0, TRUE) == FAIL)
  488.                 return FAIL;
  489.             if (colon && ins_typebuf((char_u *)":", FALSE, 0, TRUE) == FAIL)
  490.                 return FAIL;
  491.         }
  492.         Exec_reg = TRUE;        /* disable the 'q' command */
  493.     }
  494.     return OK;
  495. }
  496.  
  497. /*
  498.  * Insert a yank buffer: copy it into the Read buffer.
  499.  * Used by CTRL-R command and middle mouse button in insert mode.
  500.  *
  501.  * return FAIL for failure, OK otherwise
  502.  */
  503.     int
  504. insertbuf(c)
  505.     int c;
  506. {
  507.     long    i;
  508.     int        retval = OK;
  509.  
  510.     /*
  511.      * It is possible to get into an endless loop by having CTRL-R a in
  512.      * register a and then, in insert mode, doing CTRL-R a.
  513.      * If you hit CTRL-C, the loop will be broken here.
  514.      */
  515.     mch_breakcheck();
  516.     if (got_int)
  517.         return FAIL;
  518.  
  519.     /* check for valid buffer */
  520.     if (c != NUL && !is_yank_buffer(c, FALSE))
  521.         return FAIL;
  522.  
  523. #ifdef USE_GUI
  524.     if (c == '*')
  525.         gui_get_selection();            /* may fill * register */
  526. #endif
  527.  
  528.     if (c == '.')                        /* insert last inserted text */
  529.         retval = stuff_inserted(NUL, 1L, TRUE);
  530.     else if (c == '%')                    /* insert file name */
  531.     {
  532.         if (check_fname() == FAIL)
  533.             return FAIL;
  534.         stuffReadbuff(curbuf->b_xfilename);
  535.     }
  536.     else if (c == ':')                    /* insert last command line */
  537.     {
  538.         if (last_cmdline == NULL)
  539.         {
  540.             EMSG(e_nolastcmd);
  541.             return FAIL;
  542.         }
  543.         stuffReadbuff(last_cmdline);
  544.     }
  545.     else                                /* name or number register */
  546.     {
  547.         yankbuffer = c;
  548.         get_yank_buffer(FALSE);
  549.         if (y_current->y_array == NULL)
  550.             retval = FAIL;
  551.         else
  552.         {
  553.  
  554.             for (i = 0; i < y_current->y_size; ++i)
  555.             {
  556.                 stuffReadbuff(y_current->y_array[i]);
  557.                 /* insert newline between lines and after last line if type is
  558.                  * MLINE */
  559.                 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
  560.                     stuffReadbuff((char_u *)"\n");
  561.             }
  562.         }
  563.     }
  564.  
  565.     return retval;
  566. }
  567.  
  568. /*
  569.  * paste a yank buffer into the command line.
  570.  * used by CTRL-R command in command-line mode
  571.  * insertbuf() can't be used here, because special characters from the
  572.  * register contents will be interpreted as commands.
  573.  *
  574.  * return FAIL for failure, OK otherwise
  575.  */
  576.     int
  577. cmdline_paste(c)
  578.     int c;
  579. {
  580.     long i;
  581.  
  582.     if (!is_yank_buffer(c, FALSE))        /* check for valid buffer */
  583.         return FAIL;
  584.  
  585. #ifdef USE_GUI
  586.     if (c == '*')
  587.         gui_get_selection();
  588. #endif
  589.  
  590.     if (c == '.')                        /* insert last inserted text */
  591.         return FAIL;                    /* Unimplemented */
  592.  
  593.     if (c == '%')                        /* insert file name */
  594.     {
  595.         if (check_fname() == FAIL)
  596.             return FAIL;
  597.         return put_on_cmdline(curbuf->b_xfilename, -1, TRUE);
  598.     }
  599.  
  600.     if (c == ':')                        /* insert last command line */
  601.     {
  602.         if (last_cmdline == NULL)
  603.             return FAIL;
  604.         return put_on_cmdline(last_cmdline, -1, TRUE);
  605.     }
  606.  
  607.     yankbuffer = c;
  608.     get_yank_buffer(FALSE);
  609.     if (y_current->y_array == NULL)
  610.         return FAIL;
  611.  
  612.     for (i = 0; i < y_current->y_size; ++i)
  613.     {
  614.         put_on_cmdline(y_current->y_array[i], -1, FALSE);
  615.  
  616.         /* insert ^M between lines and after last line if type is MLINE */
  617.         if (y_current->y_type == MLINE || i < y_current->y_size - 1)
  618.             put_on_cmdline((char_u *)"\r", 1, FALSE);
  619.     }
  620.     return OK;
  621. }
  622.  
  623. /*
  624.  * do_delete - handle a delete operation
  625.  */
  626.     void
  627. do_delete()
  628. {
  629.     register int        n;
  630.     linenr_t            lnum;
  631.     char_u                *ptr;
  632.     char_u                *newp, *oldp;
  633.     linenr_t            old_lcount = curbuf->b_ml.ml_line_count;
  634.     int                    did_yank = FALSE;
  635.     struct block_def    bd;
  636.  
  637.     if (curbuf->b_ml.ml_flags & ML_EMPTY)            /* nothing to do */
  638.         return;
  639.  
  640. /*
  641.  * Imitate the strange Vi behaviour: If the delete spans more than one line
  642.  * and op_motion_type == MCHAR and the result is a blank line, make the delete
  643.  * linewise. Don't do this for the change command.
  644.  */
  645.     if (op_motion_type == MCHAR && op_line_count > 1 && op_type == DELETE)
  646.     {
  647.         ptr = ml_get(curbuf->b_op_end.lnum) + curbuf->b_op_end.col +
  648.                                                                  op_inclusive;
  649.         ptr = skipwhite(ptr);
  650.         if (*ptr == NUL && inindent(0))
  651.             op_motion_type = MLINE;
  652.     }
  653.  
  654. /*
  655.  * Check for trying to delete (e.g. "D") in an empty line.
  656.  * Note: For change command it is ok.
  657.  */
  658.     if (op_motion_type == MCHAR && op_line_count == 1 &&
  659.                 op_type == DELETE && *ml_get(curbuf->b_op_start.lnum) == NUL)
  660.     {
  661.         beep_flush();
  662.         return;
  663.     }
  664.  
  665. /*
  666.  * Do a yank of whatever we're about to delete.
  667.  * If a yank buffer was specified, put the deleted text into that buffer
  668.  */
  669.     if (yankbuffer != 0)
  670.     {
  671.                                         /* check for read-only buffer */
  672.         if (!is_yank_buffer(yankbuffer, TRUE))
  673.         {
  674.             beep_flush();
  675.             return;
  676.         }
  677.         get_yank_buffer(TRUE);            /* yank into specified buffer */
  678.         if (do_yank(TRUE, FALSE) == OK)    /* yank without message */
  679.             did_yank = TRUE;
  680.     }
  681.  
  682. /*
  683.  * Put deleted text into register 1 and shift number buffers if
  684.  * the delete contains a line break, or when a yankbuffer has been specified!
  685.  */
  686.     if (yankbuffer != 0 || op_motion_type == MLINE || op_line_count > 1)
  687.     {
  688.         y_current = &y_buf[9];
  689.         free_yank_all();                /* free buffer nine */
  690.         for (n = 9; n > 1; --n)
  691.             y_buf[n] = y_buf[n - 1];
  692.         y_previous = y_current = &y_buf[1];
  693.         y_buf[1].y_array = NULL;        /* set buffer one to empty */
  694.         yankbuffer = 0;
  695.     }
  696.     else if (yankbuffer == 0)            /* yank into unnamed buffer */
  697.     {
  698.         yankbuffer = '-';                /* use special delete buffer */
  699.         get_yank_buffer(TRUE);
  700.         yankbuffer = 0;
  701.     }
  702.  
  703.     if (yankbuffer == 0 && do_yank(TRUE, FALSE) == OK)
  704.         did_yank = TRUE;
  705.  
  706. /*
  707.  * If there's too much stuff to fit in the yank buffer, then get a
  708.  * confirmation before doing the delete. This is crude, but simple. And it
  709.  * avoids doing a delete of something we can't put back if we want.
  710.  */
  711.     if (!did_yank)
  712.     {
  713.         if (ask_yesno((char_u *)"cannot yank; delete anyway", TRUE) != 'y')
  714.         {
  715.             emsg(e_abort);
  716.             return;
  717.         }
  718.     }
  719.  
  720. /*
  721.  * block mode delete
  722.  */
  723.     if (op_block_mode)
  724.     {
  725.         if (u_save((linenr_t)(curbuf->b_op_start.lnum - 1),
  726.                                (linenr_t)(curbuf->b_op_end.lnum + 1)) == FAIL)
  727.             return;
  728.  
  729.         for (lnum = curwin->w_cursor.lnum;
  730.                                curwin->w_cursor.lnum <= curbuf->b_op_end.lnum;
  731.                                                       ++curwin->w_cursor.lnum)
  732.         {
  733.             block_prep(&bd, curwin->w_cursor.lnum, TRUE);
  734.             if (bd.textlen == 0)        /* nothing to delete */
  735.                 continue;
  736.  
  737.         /*
  738.          * If we delete a TAB, it may be replaced by several characters.
  739.          * Thus the number of characters may increase!
  740.          */
  741.             n = bd.textlen - bd.startspaces - bd.endspaces;        /* number of chars deleted */
  742.             oldp = ml_get_curline();
  743.             newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
  744.             if (newp == NULL)
  745.                 continue;
  746.         /* copy up to deleted part */
  747.             vim_memmove(newp, oldp, (size_t)bd.textcol);
  748.         /* insert spaces */
  749.             copy_spaces(newp + bd.textcol, (size_t)(bd.startspaces + bd.endspaces));
  750.         /* copy the part after the deleted part */
  751.             oldp += bd.textcol + bd.textlen;
  752.             vim_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces,
  753.                                                       oldp, STRLEN(oldp) + 1);
  754.         /* replace the line */
  755.             ml_replace(curwin->w_cursor.lnum, newp, FALSE);
  756.         }
  757.  
  758.         curwin->w_cursor.lnum = lnum;
  759.         adjust_cursor();
  760.  
  761.         CHANGED;
  762.         updateScreen(VALID_TO_CURSCHAR);
  763.         op_line_count = 0;        /* no lines deleted */
  764.     }
  765.     else if (op_motion_type == MLINE)
  766.     {
  767.         if (op_type == CHANGE)
  768.         {
  769.                 /* Delete the lines except the first one.
  770.                  * Temporarily move the cursor to the next line.
  771.                  * Save the current line number, if the last line is deleted
  772.                  * it may be changed.
  773.                  */
  774.             if (op_line_count > 1)
  775.             {
  776.                 lnum = curwin->w_cursor.lnum;
  777.                 ++curwin->w_cursor.lnum;
  778.                 dellines((long)(op_line_count - 1), TRUE, TRUE);
  779.                 curwin->w_cursor.lnum = lnum;
  780.             }
  781.             if (u_save_cursor() == FAIL)
  782.                 return;
  783.             if (curbuf->b_p_ai)                /* don't delete indent */
  784.             {
  785.                 beginline(TRUE);            /* put cursor on first non-white */
  786.                 did_ai = TRUE;                /* delete the indent when ESC hit */
  787.             }
  788.             truncate_line(FALSE);
  789.             if (curwin->w_cursor.col > 0)
  790.                 --curwin->w_cursor.col;        /* put cursor on last char in line */
  791.         }
  792.         else
  793.         {
  794.             dellines(op_line_count, TRUE, TRUE);
  795.         }
  796.         u_clearline();    /* "U" command should not be possible after "dd" */
  797.         beginline(TRUE);
  798.     }
  799.     else if (op_line_count == 1)        /* delete characters within one line */
  800.     {
  801.         if (u_save_cursor() == FAIL)
  802.             return;
  803.             /* if 'cpoptions' contains '$', display '$' at end of change */
  804.         if (vim_strchr(p_cpo, CPO_DOLLAR) != NULL && op_type == CHANGE &&
  805.              curbuf->b_op_end.lnum == curwin->w_cursor.lnum && !op_is_VIsual)
  806.             display_dollar(curbuf->b_op_end.col - !op_inclusive);
  807.         n = curbuf->b_op_end.col - curbuf->b_op_start.col + 1 - !op_inclusive;
  808.         while (n-- > 0)
  809.             if (delchar(TRUE) == FAIL)
  810.                 break;
  811.     }
  812.     else                                /* delete characters between lines */
  813.     {
  814.         if (u_save_cursor() == FAIL)            /* save first line for undo */
  815.             return;
  816.         truncate_line(TRUE);            /* delete from cursor to end of line */
  817.  
  818.         curbuf->b_op_start = curwin->w_cursor;    /* remember curwin->w_cursor */
  819.         ++curwin->w_cursor.lnum;
  820.                                                 /* includes save for undo */
  821.         dellines((long)(op_line_count - 2), TRUE, TRUE);
  822.  
  823.         if (u_save_cursor() == FAIL)            /* save last line for undo */
  824.             return;
  825.         n = curbuf->b_op_end.col - !op_inclusive;
  826.         curwin->w_cursor.col = 0;
  827.         while (n-- >= 0)            /* delete from start of line until op_end */
  828.             if (delchar(TRUE) == FAIL)
  829.                 break;
  830.         curwin->w_cursor = curbuf->b_op_start;    /* restore curwin->w_cursor */
  831.         (void)do_join(FALSE, curs_rows() == OK);
  832.     }
  833.  
  834.     if ((op_motion_type == MCHAR && op_line_count == 1) || op_type == CHANGE)
  835.     {
  836.         if (dollar_vcol)
  837.             must_redraw = 0;        /* don't want a redraw now */
  838.         cursupdate();
  839.         if (!dollar_vcol)
  840.             updateline();
  841.     }
  842.     else if (!global_busy)            /* no need to update screen for :global */
  843.         updateScreen(CURSUPD);
  844.  
  845.     msgmore(curbuf->b_ml.ml_line_count - old_lcount);
  846.  
  847.         /* correct op_end for deleted text (for "']" command) */
  848.     if (op_block_mode)
  849.         curbuf->b_op_end.col = curbuf->b_op_start.col;
  850.     else
  851.         curbuf->b_op_end = curbuf->b_op_start;
  852. }
  853.  
  854. /*
  855.  * do_tilde - handle the (non-standard vi) tilde operator
  856.  */
  857.     void
  858. do_tilde()
  859. {
  860.     FPOS                pos;
  861.     struct block_def    bd;
  862.  
  863.     if (u_save((linenr_t)(curbuf->b_op_start.lnum - 1),
  864.                                (linenr_t)(curbuf->b_op_end.lnum + 1)) == FAIL)
  865.         return;
  866.  
  867.     pos = curbuf->b_op_start;
  868.     if (op_block_mode)                    /* Visual block mode */
  869.     {
  870.         for (; pos.lnum <= curbuf->b_op_end.lnum; ++pos.lnum)
  871.         {
  872.             block_prep(&bd, pos.lnum, FALSE);
  873.             pos.col = bd.textcol;
  874.             while (--bd.textlen >= 0)
  875.             {
  876.                 swapchar(&pos);
  877.                 if (inc(&pos) == -1)    /* at end of file */
  878.                     break;
  879.             }
  880.         }
  881.     }
  882.     else            /* not block mode */
  883.     {
  884.         if (op_motion_type == MLINE)
  885.         {
  886.                 pos.col = 0;
  887.                 curbuf->b_op_end.col = STRLEN(ml_get(curbuf->b_op_end.lnum));
  888.                 if (curbuf->b_op_end.col)
  889.                         --curbuf->b_op_end.col;
  890.         }
  891.         else if (!op_inclusive)
  892.             dec(&(curbuf->b_op_end));
  893.  
  894.         while (ltoreq(pos, curbuf->b_op_end))
  895.         {
  896.             swapchar(&pos);
  897.             if (inc(&pos) == -1)    /* at end of file */
  898.                 break;
  899.         }
  900.     }
  901.  
  902.     if (op_motion_type == MCHAR && op_line_count == 1 && !op_block_mode)
  903.     {
  904.         cursupdate();
  905.         updateline();
  906.     }
  907.     else
  908.         updateScreen(CURSUPD);
  909.  
  910.     if (op_line_count > p_report)
  911.             smsg((char_u *)"%ld line%s ~ed",
  912.                                         op_line_count, plural(op_line_count));
  913. }
  914.  
  915. /*
  916.  * If op_type == UPPER: make uppercase,
  917.  * if op_type == LOWER: make lowercase,
  918.  * else swap case of character at 'pos'
  919.  */
  920.     void
  921. swapchar(pos)
  922.     FPOS    *pos;
  923. {
  924.     int        c;
  925.  
  926.     c = gchar(pos);
  927.     if (islower(c) && op_type != LOWER)
  928.     {
  929.         pchar(*pos, toupper(c));
  930.         CHANGED;
  931.     }
  932.     else if (isupper(c) && op_type != UPPER)
  933.     {
  934.         pchar(*pos, tolower(c));
  935.         CHANGED;
  936.     }
  937. }
  938.  
  939. /*
  940.  * do_change - handle a change operation
  941.  * 
  942.  * return TRUE if edit() returns because of a CTRL-O command
  943.  */
  944.     int
  945. do_change()
  946. {
  947.     register colnr_t            l;
  948.  
  949.     l = curbuf->b_op_start.col;
  950.     if (op_motion_type == MLINE)
  951.     {
  952.         l = 0;
  953. #ifdef SMARTINDENT
  954.         if (curbuf->b_p_si)
  955.             can_si = TRUE;        /* It's like opening a new line, do si */
  956. #endif
  957.     }
  958.  
  959.     if (!op_empty)
  960.         do_delete();            /* delete the text and take care of undo */
  961.  
  962.     if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum))
  963.         inc_cursor();
  964.  
  965. #if defined(LISPINDENT) || defined(CINDENT)
  966.     if (op_motion_type == MLINE)
  967.     {
  968. # ifdef LISPINDENT
  969.         if (curbuf->b_p_lisp && curbuf->b_p_ai)
  970.             fixthisline(get_lisp_indent);
  971. # endif
  972. # if defined(LISPINDENT) && defined(CINDENT)
  973.         else
  974. # endif
  975. # ifdef CINDENT
  976.         if (curbuf->b_p_cin)
  977.             fixthisline(get_c_indent);
  978. # endif
  979.     }
  980. #endif
  981.  
  982.     op_type = NOP;            /* don't want op_type == CHANGED in Insert mode */
  983.     return edit(NUL, FALSE, (linenr_t)1);
  984. }
  985.  
  986. /*
  987.  * set all the yank buffers to empty (called from main())
  988.  */
  989.     void
  990. init_yank()
  991. {
  992.     register int i;
  993.  
  994.     for (i = 0; i < NUM_REGISTERS; ++i)
  995.         y_buf[i].y_array = NULL;
  996. }
  997.  
  998. /*
  999.  * Free "n" lines from the current yank buffer.
  1000.  * Called for normal freeing and in case of error.
  1001.  */
  1002.     static void
  1003. free_yank(n)
  1004.     long n;
  1005. {
  1006.     if (y_current->y_array != NULL)
  1007.     {
  1008.         register long i;
  1009.  
  1010.         for (i = n; --i >= 0; )
  1011.         {
  1012.             if ((i & 1023) == 1023)                    /* this may take a while */
  1013.             {
  1014.                 /*
  1015.                  * This message should never cause a hit-return message.
  1016.                  * Overwrite this message with any next message.
  1017.                  */
  1018.                 ++no_wait_return;
  1019.                 smsg((char_u *)"freeing %ld lines", i + 1);
  1020.                 --no_wait_return;
  1021.                 msg_didout = FALSE;
  1022.                 msg_col = 0;
  1023.             }
  1024.             vim_free(y_current->y_array[i]);
  1025.         }
  1026.         vim_free(y_current->y_array);
  1027.         y_current->y_array = NULL;
  1028.         if (n >= 1000)
  1029.             MSG("");
  1030.     }
  1031. }
  1032.  
  1033.     static void
  1034. free_yank_all()
  1035. {
  1036.         free_yank(y_current->y_size);
  1037. }
  1038.  
  1039. /*
  1040.  * Yank the text between curwin->w_cursor and startpos into a yank buffer.
  1041.  * If we are to append ("uppercase), we first yank into a new yank buffer and
  1042.  * then concatenate the old and the new one (so we keep the old one in case
  1043.  * of out-of-memory).
  1044.  *
  1045.  * return FAIL for failure, OK otherwise
  1046.  */
  1047.     int
  1048. do_yank(deleting, mess)
  1049.     int deleting;
  1050.     int mess;
  1051. {
  1052.     long                 i;                /* index in y_array[] */
  1053.     struct yankbuf        *curr;            /* copy of y_current */
  1054.     struct yankbuf        newbuf;         /* new yank buffer when appending */
  1055.     char_u                **new_ptr;
  1056.     register linenr_t    lnum;            /* current line number */
  1057.     long                 j;
  1058.     int                    yanktype = op_motion_type;
  1059.     long                yanklines = op_line_count;
  1060.     linenr_t            yankendlnum = curbuf->b_op_end.lnum;
  1061.  
  1062.     char_u                *pnew;
  1063.     struct block_def    bd;
  1064.  
  1065.                                     /* check for read-only buffer */
  1066.     if (yankbuffer != 0 && !is_yank_buffer(yankbuffer, TRUE))
  1067.     {
  1068.         beep_flush();
  1069.         return FAIL;
  1070.     }
  1071.     if (!deleting)                    /* do_delete() already set y_current */
  1072.         get_yank_buffer(TRUE);
  1073.  
  1074.     curr = y_current;
  1075.                                     /* append to existing contents */
  1076.     if (yankappend && y_current->y_array != NULL)
  1077.         y_current = &newbuf;
  1078.     else
  1079.         free_yank_all();        /* free previously yanked lines */
  1080.  
  1081. /*
  1082.  * If the cursor was in column 1 before and after the movement, and the
  1083.  * operator is not inclusive, the yank is always linewise.
  1084.  */
  1085.     if (op_motion_type == MCHAR && curbuf->b_op_start.col == 0 &&
  1086.                   !op_inclusive && curbuf->b_op_end.col == 0 && yanklines > 1)
  1087.     {
  1088.         yanktype = MLINE;
  1089.         --yankendlnum;
  1090.         --yanklines;
  1091.     }
  1092.  
  1093.     y_current->y_size = yanklines;
  1094.     y_current->y_type = yanktype;    /* set the yank buffer type */
  1095.     y_current->y_array = (char_u **)lalloc((long_u)(sizeof(char_u *) *
  1096.                                                             yanklines), TRUE);
  1097.  
  1098.     if (y_current->y_array == NULL)
  1099.     {
  1100.         y_current = curr;
  1101.         return FAIL;
  1102.     }
  1103.  
  1104.     i = 0;
  1105.     lnum = curbuf->b_op_start.lnum;
  1106.  
  1107. /*
  1108.  * Visual block mode
  1109.  */
  1110.     if (op_block_mode)
  1111.     {
  1112.         y_current->y_type = MBLOCK;    /* set the yank buffer type */
  1113.         for ( ; lnum <= yankendlnum; ++lnum)
  1114.         {
  1115.             block_prep(&bd, lnum, FALSE);
  1116.  
  1117.             if ((pnew = alloc(bd.startspaces + bd.endspaces +
  1118.                                           bd.textlen + 1)) == NULL)
  1119.                 goto fail;
  1120.             y_current->y_array[i++] = pnew;
  1121.  
  1122.             copy_spaces(pnew, (size_t)bd.startspaces);
  1123.             pnew += bd.startspaces;
  1124.  
  1125.             vim_memmove(pnew, bd.textstart, (size_t)bd.textlen);
  1126.             pnew += bd.textlen;
  1127.  
  1128.             copy_spaces(pnew, (size_t)bd.endspaces);
  1129.             pnew += bd.endspaces;
  1130.  
  1131.             *pnew = NUL;
  1132.         }
  1133.     }
  1134.     else
  1135.     {
  1136. /*
  1137.  * there are three parts for non-block mode:
  1138.  * 1. if yanktype != MLINE yank last part of the top line
  1139.  * 2. yank the lines between op_start and op_end, inclusive when
  1140.  *    yanktype == MLINE
  1141.  * 3. if yanktype != MLINE yank first part of the bot line
  1142.  */
  1143.         if (yanktype != MLINE)
  1144.         {
  1145.             if (yanklines == 1)        /* op_start and op_end on same line */
  1146.             {
  1147.                 j = curbuf->b_op_end.col - curbuf->b_op_start.col +
  1148.                                                             1 - !op_inclusive;
  1149.                 if ((y_current->y_array[0] = strnsave(ml_get(lnum) +
  1150.                                      curbuf->b_op_start.col, (int)j)) == NULL)
  1151.                 {
  1152. fail:
  1153.                     free_yank(i);    /* free the allocated lines */
  1154.                     y_current = curr;
  1155.                     return FAIL;
  1156.                 }
  1157.                 goto success;
  1158.             }
  1159.             if ((y_current->y_array[0] = strsave(ml_get(lnum++) +
  1160.                                              curbuf->b_op_start.col)) == NULL)
  1161.                 goto fail;
  1162.             ++i;
  1163.         }
  1164.  
  1165.         while (yanktype == MLINE ? (lnum <= yankendlnum) : (lnum < yankendlnum))
  1166.         {
  1167.             if ((y_current->y_array[i] = strsave(ml_get(lnum++))) == NULL)
  1168.                 goto fail;
  1169.             ++i;
  1170.         }
  1171.         if (yanktype != MLINE)
  1172.         {
  1173.             if ((y_current->y_array[i] = strnsave(ml_get(yankendlnum),
  1174.                            curbuf->b_op_end.col + 1 - !op_inclusive)) == NULL)
  1175.                 goto fail;
  1176.         }
  1177.     }
  1178.  
  1179. success:
  1180.     if (curr != y_current)        /* append the new block to the old block */
  1181.     {
  1182.         new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
  1183.                                    (curr->y_size + y_current->y_size)), TRUE);
  1184.         if (new_ptr == NULL)
  1185.             goto fail;
  1186.         for (j = 0; j < curr->y_size; ++j)
  1187.             new_ptr[j] = curr->y_array[j];
  1188.         vim_free(curr->y_array);
  1189.         curr->y_array = new_ptr;
  1190.  
  1191.         if (yanktype == MLINE)     /* MLINE overrides MCHAR and MBLOCK */
  1192.             curr->y_type = MLINE;
  1193.  
  1194.         /* concatenate the last line of the old block with the first line of
  1195.          * the new block */
  1196.         if (curr->y_type == MCHAR)
  1197.         {
  1198.             pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
  1199.                               + STRLEN(y_current->y_array[0]) + 1), TRUE);
  1200.             if (pnew == NULL)
  1201.             {
  1202.                     i = y_current->y_size - 1;
  1203.                     goto fail;
  1204.             }
  1205.             STRCPY(pnew, curr->y_array[--j]);
  1206.             STRCAT(pnew, y_current->y_array[0]);
  1207.             vim_free(curr->y_array[j]);
  1208.             vim_free(y_current->y_array[0]);
  1209.             curr->y_array[j++] = pnew;
  1210.             i = 1;
  1211.         }
  1212.         else
  1213.             i = 0;
  1214.         while (i < y_current->y_size)
  1215.             curr->y_array[j++] = y_current->y_array[i++];
  1216.         curr->y_size = j;
  1217.         vim_free(y_current->y_array);
  1218.         y_current = curr;
  1219.     }
  1220.     if (mess)                    /* Display message about yank? */
  1221.     {
  1222.         if (yanktype == MCHAR && !op_block_mode && yanklines == 1)
  1223.             yanklines = 0;
  1224.         if (yanklines > p_report)
  1225.         {
  1226.             cursupdate();        /* redisplay now, so message is not deleted */
  1227.             smsg((char_u *)"%ld line%s yanked", yanklines, plural(yanklines));
  1228.         }
  1229.     }
  1230.  
  1231.     return OK;
  1232. }
  1233.  
  1234. /*
  1235.  * put contents of register into the text
  1236.  * For ":put" command count == -1.
  1237.  */
  1238.     void
  1239. do_put(dir, count, fix_indent)
  1240.     int        dir;                /* BACKWARD for 'P', FORWARD for 'p' */
  1241.     long    count;
  1242.     int        fix_indent;            /* make indent look nice */
  1243. {
  1244.     char_u        *ptr;
  1245.     char_u        *newp, *oldp;
  1246.     int         yanklen;
  1247.     int            oldlen;
  1248.     int            totlen = 0;                    /* init for gcc */
  1249.     linenr_t    lnum;
  1250.     colnr_t        col;
  1251.     long         i;                            /* index in y_array[] */
  1252.     int         y_type;
  1253.     long         y_size;
  1254.     char_u        **y_array;
  1255.     long         nr_lines = 0;
  1256.     colnr_t        vcol;
  1257.     int            delcount;
  1258.     int            incr = 0;
  1259.     long        j;
  1260.     FPOS        new_cursor;
  1261.     int            indent;
  1262.     int            orig_indent = 0;            /* init for gcc */
  1263.     int            indent_diff = 0;            /* init for gcc */
  1264.     int            first_indent = TRUE;
  1265.     FPOS        old_pos;
  1266.     struct block_def bd;
  1267.     char_u        *insert_string = NULL;
  1268.  
  1269. #ifdef USE_GUI
  1270.     if (yankbuffer == '*')
  1271.         gui_get_selection();
  1272. #endif
  1273.  
  1274.     if (fix_indent)
  1275.         orig_indent = get_indent();
  1276.  
  1277.     curbuf->b_op_start = curwin->w_cursor;        /* default for "'[" command */
  1278.     if (dir == FORWARD)
  1279.         curbuf->b_op_start.col++;
  1280.     curbuf->b_op_end = curwin->w_cursor;        /* default for "']" command */
  1281.  
  1282.     /*
  1283.      * Using inserted text works differently, because the buffer includes
  1284.      * special characters (newlines, etc.).
  1285.      */
  1286.     if (yankbuffer == '.')
  1287.     {
  1288.         (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
  1289.                                     (count == -1 ? 'O' : 'i')), count, FALSE);
  1290.         return;
  1291.     }
  1292.  
  1293.     /*
  1294.      * For '%' (file name) and ':' (last command line) we have to create a
  1295.      * fake yank buffer.
  1296.      */
  1297.     if (yankbuffer == '%')                /* use file name */
  1298.     {
  1299.         if (check_fname() == FAIL)
  1300.             return;
  1301.         insert_string = curbuf->b_xfilename;
  1302.     }
  1303.     else if (yankbuffer == ':')            /* use last command line */
  1304.     {
  1305.         if (last_cmdline == NULL)
  1306.         {
  1307.             EMSG(e_nolastcmd);
  1308.             return;
  1309.         }
  1310.         insert_string = last_cmdline;
  1311.     }
  1312.  
  1313.     if (insert_string != NULL)
  1314.     {
  1315.         y_type = MCHAR;                    /* use fake one-line yank buffer */
  1316.         y_size = 1;
  1317.         y_array = &insert_string;
  1318.     }
  1319.     else
  1320.     {
  1321.         get_yank_buffer(FALSE);
  1322.  
  1323.         y_type = y_current->y_type;
  1324.         y_size = y_current->y_size;
  1325.         y_array = y_current->y_array;
  1326.     }
  1327.  
  1328.     if (count == -1)        /* :put command */
  1329.     {
  1330.         y_type = MLINE;
  1331.         count = 1;
  1332.     }
  1333.  
  1334.     if (y_size == 0 || y_array == NULL)
  1335.     {
  1336.         EMSG2("Nothing in register %s",
  1337.                     yankbuffer == 0 ? (char_u *)"\"" : transchar(yankbuffer));
  1338.         return;
  1339.     }
  1340.  
  1341.     if (y_type == MBLOCK)
  1342.     {
  1343.         lnum = curwin->w_cursor.lnum + y_size + 1;
  1344.         if (lnum > curbuf->b_ml.ml_line_count)
  1345.             lnum = curbuf->b_ml.ml_line_count + 1;
  1346.         if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
  1347.             return;
  1348.     }
  1349.     else if (u_save_cursor() == FAIL)
  1350.         return;
  1351.  
  1352.     yanklen = STRLEN(y_array[0]);
  1353.     CHANGED;
  1354.  
  1355.     lnum = curwin->w_cursor.lnum;
  1356.     col = curwin->w_cursor.col;
  1357.  
  1358. /*
  1359.  * block mode
  1360.  */
  1361.     if (y_type == MBLOCK)
  1362.     {
  1363.         if (dir == FORWARD && gchar_cursor() != NUL)
  1364.         {
  1365.             getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
  1366.             ++col;
  1367.             ++curwin->w_cursor.col;
  1368.         }
  1369.         else
  1370.             getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
  1371.         for (i = 0; i < y_size; ++i)
  1372.         {
  1373.             bd.startspaces = 0;
  1374.             bd.endspaces = 0;
  1375.             bd.textcol = 0;
  1376.             vcol = 0;
  1377.             delcount = 0;
  1378.  
  1379.         /* add a new line */
  1380.             if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
  1381.             {
  1382.                 ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
  1383.                                                            (colnr_t)1, FALSE);
  1384.                 ++nr_lines;
  1385.             }
  1386.             oldp = ml_get_curline();
  1387.             oldlen = STRLEN(oldp);
  1388.             for (ptr = oldp; vcol < col && *ptr; ++ptr)
  1389.             {
  1390.                 /* Count a tab for what it's worth (if list mode not on) */
  1391.                 incr = lbr_chartabsize(ptr, (colnr_t)vcol);
  1392.                 vcol += incr;
  1393.                 ++bd.textcol;
  1394.             }
  1395.             if (vcol < col)    /* line too short, padd with spaces */
  1396.             {
  1397.                 bd.startspaces = col - vcol;
  1398.             }
  1399.             else if (vcol > col)
  1400.             {
  1401.                 bd.endspaces = vcol - col;
  1402.                 bd.startspaces = incr - bd.endspaces;
  1403.                 --bd.textcol;
  1404.                 delcount = 1;
  1405.             }
  1406.             yanklen = STRLEN(y_array[i]);
  1407.             totlen = count * yanklen + bd.startspaces + bd.endspaces;
  1408.             newp = alloc_check((unsigned)totlen + oldlen + 1);
  1409.             if (newp == NULL)
  1410.                 break;
  1411.         /* copy part up to cursor to new line */
  1412.             ptr = newp;
  1413.             vim_memmove(ptr, oldp, (size_t)bd.textcol);
  1414.             ptr += bd.textcol;
  1415.         /* may insert some spaces before the new text */
  1416.             copy_spaces(ptr, (size_t)bd.startspaces);
  1417.             ptr += bd.startspaces;
  1418.         /* insert the new text */
  1419.             for (j = 0; j < count; ++j)
  1420.             {
  1421.                 vim_memmove(ptr, y_array[i], (size_t)yanklen);
  1422.                 ptr += yanklen;
  1423.             }
  1424.         /* may insert some spaces after the new text */
  1425.             copy_spaces(ptr, (size_t)bd.endspaces);
  1426.             ptr += bd.endspaces;
  1427.         /* move the text after the cursor to the end of the line. */
  1428.             vim_memmove(ptr, oldp + bd.textcol + delcount,
  1429.                                 (size_t)(oldlen - bd.textcol - delcount + 1));
  1430.             ml_replace(curwin->w_cursor.lnum, newp, FALSE);
  1431.  
  1432.             ++curwin->w_cursor.lnum;
  1433.             if (i == 0)
  1434.                 curwin->w_cursor.col += bd.startspaces;
  1435.         }
  1436.                                                 /* for "']" command */
  1437.         curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
  1438.         curbuf->b_op_end.col = bd.textcol + totlen - 1;
  1439.         curwin->w_cursor.lnum = lnum;
  1440.         cursupdate();
  1441.         updateScreen(VALID_TO_CURSCHAR);
  1442.     }
  1443.     else        /* not block mode */
  1444.     {
  1445.         if (y_type == MCHAR)
  1446.         {
  1447.     /* if type is MCHAR, FORWARD is the same as BACKWARD on the next char */
  1448.             if (dir == FORWARD && gchar_cursor() != NUL)
  1449.             {
  1450.                 ++col;
  1451.                 if (yanklen)
  1452.                 {
  1453.                     ++curwin->w_cursor.col;
  1454.                     ++curbuf->b_op_end.col;
  1455.                 }
  1456.             }
  1457.             new_cursor = curwin->w_cursor;
  1458.         }
  1459.         else if (dir == BACKWARD)
  1460.     /* if type is MLINE, BACKWARD is the same as FORWARD on the previous line */
  1461.             --lnum;
  1462.  
  1463. /*
  1464.  * simple case: insert into current line
  1465.  */
  1466.         if (y_type == MCHAR && y_size == 1)
  1467.         {
  1468.             totlen = count * yanklen;
  1469.             if (totlen)
  1470.             {
  1471.                 oldp = ml_get(lnum);
  1472.                 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
  1473.                 if (newp == NULL)
  1474.                     return;             /* alloc() will give error message */
  1475.                 vim_memmove(newp, oldp, (size_t)col);
  1476.                 ptr = newp + col;
  1477.                 for (i = 0; i < count; ++i)
  1478.                 {
  1479.                     vim_memmove(ptr, y_array[0], (size_t)yanklen);
  1480.                     ptr += yanklen;
  1481.                 }
  1482.                 vim_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
  1483.                 ml_replace(lnum, newp, FALSE);
  1484.                                           /* put cursor on last putted char */
  1485.                 curwin->w_cursor.col += (colnr_t)(totlen - 1);
  1486.             }
  1487.             curbuf->b_op_end = curwin->w_cursor;
  1488.             updateline();
  1489.         }
  1490.         else
  1491.         {
  1492.             while (--count >= 0)
  1493.             {
  1494.                 i = 0;
  1495.                 if (y_type == MCHAR)
  1496.                 {
  1497.                     /*
  1498.                      * Split the current line in two at the insert position.
  1499.                      * First insert y_array[size - 1] in front of second line.
  1500.                      * Then append y_array[0] to first line.
  1501.                      */
  1502.                     ptr = ml_get(lnum) + col;
  1503.                     totlen = STRLEN(y_array[y_size - 1]);
  1504.                     newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
  1505.                     if (newp == NULL)
  1506.                         goto error;
  1507.                     STRCPY(newp, y_array[y_size - 1]);
  1508.                     STRCAT(newp, ptr);
  1509.                         /* insert second line */
  1510.                     ml_append(lnum, newp, (colnr_t)0, FALSE);
  1511.                     vim_free(newp);
  1512.  
  1513.                     oldp = ml_get(lnum);
  1514.                     newp = alloc_check((unsigned)(col + yanklen + 1));
  1515.                     if (newp == NULL)
  1516.                         goto error;
  1517.                                             /* copy first part of line */
  1518.                     vim_memmove(newp, oldp, (size_t)col);
  1519.                                             /* append to first line */
  1520.                     vim_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
  1521.                     ml_replace(lnum, newp, FALSE);
  1522.  
  1523.                     curwin->w_cursor.lnum = lnum;
  1524.                     i = 1;
  1525.                 }
  1526.  
  1527.                 while (i < y_size)
  1528.                 {
  1529.                     if ((y_type != MCHAR || i < y_size - 1) &&
  1530.                         ml_append(lnum, y_array[i], (colnr_t)0, FALSE) == FAIL)
  1531.                             goto error;
  1532.                     lnum++;
  1533.                     i++;
  1534.                     if (fix_indent)
  1535.                     {
  1536.                         old_pos = curwin->w_cursor;
  1537.                         curwin->w_cursor.lnum = lnum;
  1538.                         ptr = ml_get(lnum);
  1539. #if defined(SMARTINDENT) || defined(CINDENT)
  1540.                         if (*ptr == '#'
  1541. # ifdef SMARTINDENT
  1542.                            && curbuf->b_p_si
  1543. # endif
  1544. # ifdef CINDENT
  1545.                            && curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
  1546. # endif
  1547.                                             )
  1548.                     
  1549.                             indent = 0;     /* Leave # lines at start */
  1550.                         else
  1551. #endif
  1552.                              if (*ptr == NUL)
  1553.                             indent = 0;     /* Ignore empty lines */
  1554.                         else if (first_indent)
  1555.                         {
  1556.                             indent_diff = orig_indent - get_indent();
  1557.                             indent = orig_indent;
  1558.                             first_indent = FALSE;
  1559.                         }
  1560.                         else if ((indent = get_indent() + indent_diff) < 0)
  1561.                             indent = 0;
  1562.                         set_indent(indent, TRUE);
  1563.                         curwin->w_cursor = old_pos;
  1564.                     }
  1565.                     ++nr_lines;
  1566.                 }
  1567.             }
  1568.  
  1569.             /* put '] at last inserted character */
  1570.             curbuf->b_op_end.lnum = lnum;
  1571.             col = STRLEN(y_array[y_size - 1]);
  1572.             if (col > 1)
  1573.                 curbuf->b_op_end.col = col - 1;
  1574.             else
  1575.                 curbuf->b_op_end.col = 0;
  1576.  
  1577.             if (y_type == MLINE)
  1578.             {
  1579.                 curwin->w_cursor.col = 0;
  1580.                 if (dir == FORWARD)
  1581.                 {
  1582.                     updateScreen(NOT_VALID);    /* recomp. curwin->w_botline */
  1583.                     ++curwin->w_cursor.lnum;
  1584.                 }
  1585.                     /* put cursor on first non-blank in last inserted line */
  1586.                 beginline(TRUE);
  1587.             }
  1588.             else        /* put cursor on first inserted character */
  1589.             {
  1590.                 curwin->w_cursor = new_cursor;
  1591.             }
  1592.  
  1593. error:
  1594.             if (y_type == MLINE)        /* for '[ */
  1595.             {
  1596.                 curbuf->b_op_start.col = 0;
  1597.                 if (dir == FORWARD)
  1598.                     curbuf->b_op_start.lnum++;
  1599.             }
  1600.             mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
  1601.                                                        MAXLNUM, nr_lines, 0L);
  1602.             updateScreen(CURSUPD);
  1603.         }
  1604.     }
  1605.  
  1606.     msgmore(nr_lines);
  1607.     curwin->w_set_curswant = TRUE;
  1608. }
  1609.  
  1610. /* Return the character name of the register with the given number */
  1611.     int
  1612. get_register_name(num)
  1613.     int num;
  1614. {
  1615.     if (num == -1)
  1616.         return '"';
  1617.     else if (num < 10)
  1618.         return num + '0';
  1619.     else if (num == DELETION_REGISTER)
  1620.         return '-';
  1621. #ifdef USE_GUI
  1622.     else if (num == GUI_SELECTION_REGISTER)
  1623.         return '*';
  1624. #endif
  1625.     else
  1626.         return num + 'a' - 10;
  1627. }
  1628.  
  1629. /*
  1630.  * display the contents of the yank buffers
  1631.  */
  1632.     void
  1633. do_dis(arg)
  1634.     char_u *arg;
  1635. {
  1636.     register int            i, n;
  1637.     register long            j;
  1638.     register char_u            *p;
  1639.     register struct yankbuf *yb;
  1640.     char_u name;
  1641.   
  1642.     if (arg != NULL && *arg == NUL)
  1643.         arg = NULL;
  1644.  
  1645.     set_highlight('t');        /* Highlight title */
  1646.     start_highlight();
  1647.     MSG_OUTSTR("\n--- Registers ---");
  1648.     stop_highlight();
  1649.     for (i = -1; i < NUM_REGISTERS; ++i)
  1650.     {
  1651.         if (i == -1)
  1652.         {
  1653.             if (y_previous != NULL)
  1654.                 yb = y_previous;
  1655.             else
  1656.                 yb = &(y_buf[0]);
  1657.         }
  1658.         else
  1659.             yb = &(y_buf[i]);
  1660.         name = get_register_name(i);
  1661.         if (yb->y_array != NULL && (arg == NULL ||
  1662.                                                vim_strchr(arg, name) != NULL))
  1663.         {
  1664.             msg_outchar('\n');
  1665.             msg_outchar('"');
  1666.             msg_outchar(name);
  1667.             MSG_OUTSTR("   ");
  1668.  
  1669.             n = (int)Columns - 6;
  1670.             for (j = 0; j < yb->y_size && n > 1; ++j)
  1671.             {
  1672.                 if (j)
  1673.                 {
  1674.                     MSG_OUTSTR("^J");
  1675.                     n -= 2;
  1676.                 }
  1677.                 for (p = yb->y_array[j]; *p && (n -= charsize(*p)) >= 0; ++p)
  1678.                     msg_outtrans_len(p, 1);
  1679.             }
  1680.             if (n > 1 && yb->y_type == MLINE)
  1681.                 MSG_OUTSTR("^J");
  1682.             flushbuf();                /* show one line at a time */
  1683.         }
  1684.     }
  1685.  
  1686.     /*
  1687.      * display last inserted text
  1688.      */
  1689.     if ((p = get_last_insert()) != NULL &&
  1690.         (arg == NULL || vim_strchr(arg, '.') != NULL))
  1691.     {
  1692.         MSG_OUTSTR("\n\".   ");
  1693.         dis_msg(p, TRUE);
  1694.     }
  1695.  
  1696.     /*
  1697.      * display last command line
  1698.      */
  1699.     if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL))
  1700.     {
  1701.         MSG_OUTSTR("\n\":   ");
  1702.         dis_msg(last_cmdline, FALSE);
  1703.     }
  1704.  
  1705.     /*
  1706.      * display current file name
  1707.      */
  1708.     if (curbuf->b_xfilename != NULL &&
  1709.                                 (arg == NULL || vim_strchr(arg, '%') != NULL))
  1710.     {
  1711.         MSG_OUTSTR("\n\"%   ");
  1712.         dis_msg(curbuf->b_xfilename, FALSE);
  1713.     }
  1714. }
  1715.  
  1716. /*
  1717.  * display a string for do_dis()
  1718.  * truncate at end of screen line
  1719.  */
  1720.     void
  1721. dis_msg(p, skip_esc)
  1722.     char_u        *p;
  1723.     int            skip_esc;            /* if TRUE, ignore trailing ESC */
  1724. {
  1725.     int        n;
  1726.  
  1727.     n = (int)Columns - 6;
  1728.     while (*p && !(*p == ESC && skip_esc && *(p + 1) == NUL) &&
  1729.                         (n -= charsize(*p)) >= 0)
  1730.         msg_outtrans_len(p++, 1);
  1731. }
  1732.  
  1733. /*
  1734.  * join 'count' lines (minimal 2), including u_save()
  1735.  */
  1736.     void
  1737. do_do_join(count, insert_space, redraw)
  1738.     long    count;
  1739.     int        insert_space;
  1740.     int        redraw;                    /* can redraw, curwin->w_col valid */
  1741. {
  1742.     if (u_save((linenr_t)(curwin->w_cursor.lnum - 1),
  1743.                     (linenr_t)(curwin->w_cursor.lnum + count)) == FAIL)
  1744.         return;
  1745.  
  1746.     if (count > 10)
  1747.         redraw = FALSE;                /* don't redraw each small change */
  1748.     while (--count > 0)
  1749.     {
  1750.         line_breakcheck();
  1751.         if (got_int || do_join(insert_space, redraw) == FAIL)
  1752.         {
  1753.             beep_flush();
  1754.             break;
  1755.         }
  1756.     }
  1757.     if (redraw)
  1758.         redraw_later(VALID_TO_CURSCHAR);
  1759.     else
  1760.         redraw_later(NOT_VALID);
  1761.     
  1762.     /*
  1763.      * Need to update the screen if the line where the cursor is became too
  1764.      * long to fit on the screen.
  1765.      */
  1766.     cursupdate();
  1767. }
  1768.  
  1769. /*
  1770.  * Join two lines at the cursor position.
  1771.  *
  1772.  * return FAIL for failure, OK ohterwise
  1773.  */
  1774.     int
  1775. do_join(insert_space, redraw)
  1776.     int            insert_space;
  1777.     int            redraw;        /* should only be TRUE when curwin->w_row valid */
  1778. {
  1779.     char_u        *curr;
  1780.     char_u        *next;
  1781.     char_u        *newp;
  1782.     int            endcurr1, endcurr2;
  1783.     int         currsize;        /* size of the current line */
  1784.     int         nextsize;        /* size of the next line */
  1785.     int            spaces;            /* number of spaces to insert */
  1786.     int            rows_to_del = 0;/* number of rows on screen to delete */
  1787.     linenr_t    t;
  1788.  
  1789.     if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
  1790.         return FAIL;            /* can't join on last line */
  1791.  
  1792.     if (redraw)
  1793.         rows_to_del = plines_m(curwin->w_cursor.lnum,
  1794.                                                    curwin->w_cursor.lnum + 1);
  1795.  
  1796.     curr = ml_get_curline();
  1797.     currsize = STRLEN(curr);
  1798.     endcurr1 = endcurr2 = NUL;
  1799.     if (currsize > 0)
  1800.     {
  1801.         endcurr1 = *(curr + currsize - 1);
  1802.         if (currsize > 1)
  1803.             endcurr2 = *(curr + currsize - 2);
  1804.     }
  1805.  
  1806.     next = ml_get((linenr_t)(curwin->w_cursor.lnum + 1));
  1807.     spaces = 0;
  1808.     if (insert_space)
  1809.     {
  1810.         next = skipwhite(next);
  1811.         spaces = 1;
  1812.         if (*next == ')' || currsize == 0)
  1813.             spaces = 0;
  1814.         else
  1815.         {
  1816.             if (endcurr1 == ' ' || endcurr1 == TAB)
  1817.             {
  1818.                 spaces = 0;
  1819.                 if (currsize > 1)
  1820.                     endcurr1 = endcurr2;
  1821.             }
  1822.             if (p_js && vim_strchr((char_u *)".!?", endcurr1) != NULL)
  1823.                 spaces = 2;
  1824.         }
  1825.     }
  1826.     nextsize = STRLEN(next);
  1827.  
  1828.     newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
  1829.     if (newp == NULL)
  1830.         return FAIL;
  1831.  
  1832.     /*
  1833.      * Insert the next line first, because we already have that pointer.
  1834.      * Curr has to be obtained again, because getting next will have
  1835.      * invalidated it.
  1836.      */
  1837.     vim_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
  1838.  
  1839.     curr = ml_get_curline();
  1840.     vim_memmove(newp, curr, (size_t)currsize);
  1841.  
  1842.     copy_spaces(newp + currsize, (size_t)spaces);
  1843.  
  1844.     ml_replace(curwin->w_cursor.lnum, newp, FALSE);
  1845.  
  1846.     /*
  1847.      * Delete the following line. To do this we move the cursor there
  1848.      * briefly, and then move it back. After dellines() the cursor may
  1849.      * have moved up (last line deleted), so the current lnum is kept in t.
  1850.      */
  1851.     t = curwin->w_cursor.lnum;
  1852.     ++curwin->w_cursor.lnum;
  1853.     dellines(1L, FALSE, FALSE);
  1854.     curwin->w_cursor.lnum = t;
  1855.  
  1856.     /*
  1857.      * the number of rows on the screen is reduced by the difference
  1858.      * in number of rows of the two old lines and the one new line
  1859.      */
  1860.     if (redraw)
  1861.     {
  1862.         rows_to_del -= plines(curwin->w_cursor.lnum);
  1863.         if (rows_to_del > 0)
  1864.             win_del_lines(curwin, curwin->w_cline_row + curwin->w_cline_height,
  1865.                                                      rows_to_del, TRUE, TRUE);
  1866.     }
  1867.  
  1868.      /*
  1869.      * go to first character of the joined line
  1870.      */
  1871.     if (currsize == 0)
  1872.         curwin->w_cursor.col = 0;
  1873.     else
  1874.     {
  1875.         curwin->w_cursor.col = currsize - 1;
  1876.         (void)oneright();
  1877.     }
  1878.     CHANGED;
  1879.  
  1880.     return OK;
  1881. }
  1882.  
  1883. /*
  1884.  * Return TRUE if the two comment leaders given are the same.  The cursor is
  1885.  * in the first line.  White-space is ignored.  Note that the whole of
  1886.  * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
  1887.  */
  1888.     static int
  1889. same_leader(leader1_len, leader1_flags, leader2_len, leader2_flags)
  1890.     int        leader1_len;
  1891.     char_u    *leader1_flags;
  1892.     int        leader2_len;
  1893.     char_u    *leader2_flags;
  1894. {
  1895.     int        idx1 = 0, idx2 = 0;
  1896.     char_u    *p;
  1897.     char_u    *line1;
  1898.     char_u    *line2;
  1899.  
  1900.     if (leader1_len == 0)
  1901.         return (leader2_len == 0);
  1902.  
  1903.     /*
  1904.      * If first leader has 'f' flag, the lines can be joined only if the
  1905.      * second line does not have a leader.
  1906.      * If first leader has 'e' flag, the lines can never be joined.
  1907.      * If fist leader has 's' flag, the lines can only be joined if there is
  1908.      * some text after it and the second line has the 'm' flag.
  1909.      */
  1910.     if (leader1_flags != NULL)
  1911.     {
  1912.         for (p = leader1_flags; *p && *p != ':'; ++p)
  1913.         {
  1914.             if (*p == COM_FIRST)
  1915.                 return (leader2_len == 0);
  1916.             if (*p == COM_END)
  1917.                 return FALSE;
  1918.             if (*p == COM_START)
  1919.             {
  1920.                 if (*(ml_get_curline() + leader1_len) == NUL)
  1921.                     return FALSE;
  1922.                 if (leader2_flags == NULL || leader2_len == 0)
  1923.                     return FALSE;
  1924.                 for (p = leader2_flags; *p && *p != ':'; ++p)
  1925.                     if (*p == COM_MIDDLE)
  1926.                         return TRUE;
  1927.                 return FALSE;
  1928.             }
  1929.         }
  1930.     }
  1931.  
  1932.     /*
  1933.      * Get current line and next line, compare the leaders.
  1934.      * The first line has to be saved, only one line can be locked at a time.
  1935.      */
  1936.     line1 = strsave(ml_get_curline());
  1937.     if (line1 != NULL)
  1938.     {
  1939.         for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
  1940.             ;
  1941.         line2 = ml_get(curwin->w_cursor.lnum + 1);
  1942.         for (idx2 = 0; idx2 < leader2_len; ++idx2)
  1943.         {
  1944.             if (!vim_iswhite(line2[idx2]))
  1945.             {
  1946.                 if (line1[idx1++] != line2[idx2])
  1947.                     break;
  1948.             }
  1949.             else
  1950.                 while (vim_iswhite(line1[idx1]))
  1951.                     ++idx1;
  1952.         }
  1953.         vim_free(line1);
  1954.     }
  1955.     return (idx2 == leader2_len && idx1 == leader1_len);
  1956. }
  1957.  
  1958. /*
  1959.  * implementation of the format operator 'Q'
  1960.  */
  1961.     void
  1962. do_format()
  1963. {
  1964.     long        old_line_count = curbuf->b_ml.ml_line_count;
  1965.     int            prev_is_blank = FALSE;
  1966.     int            is_end_block = TRUE;
  1967.     int            next_is_end_block;
  1968.     int            leader_len = 0;        /* init for gcc */
  1969.     int            next_leader_len;
  1970.     char_u        *leader_flags = NULL;
  1971.     char_u        *next_leader_flags;
  1972.     int            advance = TRUE;
  1973.     int            second_indent = -1;
  1974.     int            do_second_indent;
  1975.     int            first_par_line = TRUE;
  1976.  
  1977.     if (u_save((linenr_t)(curwin->w_cursor.lnum - 1),
  1978.                    (linenr_t)(curwin->w_cursor.lnum + op_line_count)) == FAIL)
  1979.         return;
  1980.  
  1981.     /* check for 'q' and '2' in 'formatoptions' */
  1982.     fo_do_comments = has_format_option(FO_Q_COMS);
  1983.     do_second_indent = has_format_option(FO_Q_SECOND);
  1984.  
  1985.     /*
  1986.      * get info about the previous and current line.
  1987.      */
  1988.     if (curwin->w_cursor.lnum > 1)
  1989.         is_end_block = fmt_end_block(curwin->w_cursor.lnum - 1,
  1990.                                         &next_leader_len, &next_leader_flags);
  1991.     next_is_end_block = fmt_end_block(curwin->w_cursor.lnum,
  1992.                                         &next_leader_len, &next_leader_flags);
  1993.  
  1994.     curwin->w_cursor.lnum--;
  1995.     while (--op_line_count >= 0)
  1996.     {
  1997.         /*
  1998.          * Advance to next block.
  1999.          */
  2000.         if (advance)
  2001.         {
  2002.             curwin->w_cursor.lnum++;
  2003.             prev_is_blank = is_end_block;
  2004.             is_end_block = next_is_end_block;
  2005.             leader_len = next_leader_len;
  2006.             leader_flags = next_leader_flags;
  2007.         }
  2008.  
  2009.         /*
  2010.          * The last line to be formatted.
  2011.          */
  2012.         if (op_line_count == 0)
  2013.         {
  2014.             next_is_end_block = TRUE;
  2015.             next_leader_len = 0;
  2016.             next_leader_flags = NULL;
  2017.         }
  2018.         else
  2019.             next_is_end_block = fmt_end_block(curwin->w_cursor.lnum + 1,
  2020.                                         &next_leader_len, &next_leader_flags);
  2021.         advance = TRUE;
  2022.  
  2023.         /*
  2024.          * For the first line of a paragraph, check indent of second line.
  2025.          * Don't do this for comments and empty lines.
  2026.          */
  2027.         if (first_par_line && do_second_indent &&
  2028.                 prev_is_blank && !is_end_block &&
  2029.                 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count &&
  2030.                 leader_len == 0 && next_leader_len == 0 &&
  2031.                 !lineempty(curwin->w_cursor.lnum + 1))
  2032.             second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
  2033.  
  2034.         /*
  2035.          * Skip end-of-block (blank) lines
  2036.          */
  2037.         if (is_end_block)
  2038.         {
  2039.         }
  2040.         /*
  2041.          * If we have got to the end of a paragraph, format it.
  2042.          */
  2043.         else if (next_is_end_block || !same_leader(leader_len, leader_flags,
  2044.                                           next_leader_len, next_leader_flags))
  2045.         {
  2046.             /* replace indent in first line with minimal number of tabs and
  2047.              * spaces, according to current options */
  2048.             set_indent(get_indent(), TRUE);
  2049.  
  2050.             /* put cursor on last non-space */
  2051.             coladvance(MAXCOL);
  2052.             while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
  2053.                 dec_cursor();
  2054.             curs_columns(FALSE);            /* update curwin->w_virtcol */
  2055.  
  2056.             /* do the formatting */
  2057.             State = INSERT;        /* for Opencmd() */
  2058.             insertchar(NUL, TRUE, second_indent);
  2059.             State = NORMAL;
  2060.             first_par_line = TRUE;
  2061.             second_indent = -1;
  2062.         }
  2063.         else
  2064.         {
  2065.             /*
  2066.              * Still in same paragraph, so join the lines together.
  2067.              * But first delete the comment leader from the second line.
  2068.              */
  2069.             advance = FALSE;
  2070.             curwin->w_cursor.lnum++;
  2071.             curwin->w_cursor.col = 0;
  2072.             while (next_leader_len--)
  2073.                 delchar(FALSE);
  2074.             curwin->w_cursor.lnum--;
  2075.             if (do_join(TRUE, FALSE) == FAIL)
  2076.             {
  2077.                 beep_flush();
  2078.                 break;
  2079.             }
  2080.             first_par_line = FALSE;
  2081.         }
  2082.     }
  2083.     fo_do_comments = FALSE;
  2084.     /*
  2085.      * Leave the cursor at the first non-blank of the last formatted line.
  2086.      * If the cursor was move one line back (e.g. with "Q}") go to the next
  2087.      * line, so "." will do the next lines.
  2088.      */
  2089.     if (op_end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
  2090.         ++curwin->w_cursor.lnum;
  2091.     beginline(TRUE);
  2092.     updateScreen(NOT_VALID);
  2093.     msgmore(curbuf->b_ml.ml_line_count - old_line_count);
  2094. }
  2095.  
  2096. /*
  2097.  * Blank lines, and lines containing only the comment leader, are left
  2098.  * untouched by the formatting.  The function returns TRUE in this
  2099.  * case.  It also returns TRUE when a line starts with the end of a comment
  2100.  * ('e' in comment flags), so that this line is skipped, and not joined to the
  2101.  * previous line.  A new paragraph starts after a blank line, or when the
  2102.  * comment leader changes -- webb.
  2103.  */
  2104.     static int
  2105. fmt_end_block(lnum, leader_len, leader_flags)
  2106.     linenr_t    lnum;
  2107.     int            *leader_len;
  2108.     char_u        **leader_flags;
  2109. {
  2110.     char_u        *flags = NULL;        /* init for GCC */
  2111.     char_u        *ptr;
  2112.  
  2113.     ptr = ml_get(lnum);
  2114.     *leader_len = get_leader_len(ptr, leader_flags);
  2115.  
  2116.     if (*leader_len > 0)
  2117.     {
  2118.         /*
  2119.          * Search for 'e' flag in comment leader flags.
  2120.          */
  2121.         flags = *leader_flags;
  2122.         while (*flags && *flags != ':' && *flags != COM_END)
  2123.             ++flags;
  2124.     }
  2125.  
  2126.     return (ptr[*leader_len] == NUL ||
  2127.             (*leader_len > 0 && *flags == COM_END) ||
  2128.              startPS(lnum, NUL, FALSE));
  2129. }
  2130.  
  2131. /*
  2132.  * prepare a few things for block mode yank/delete/tilde
  2133.  *
  2134.  * for delete:
  2135.  * - textlen includes the first/last char to be (partly) deleted
  2136.  * - start/endspaces is the number of columns that are taken by the
  2137.  *     first/last deleted char minus the number of columns that have to be deleted.
  2138.  * for yank and tilde:
  2139.  * - textlen includes the first/last char to be wholly yanked
  2140.  * - start/endspaces is the number of columns of the first/last yanked char
  2141.  *   that are to be yanked.
  2142.  */
  2143.     static void
  2144. block_prep(bd, lnum, is_del)
  2145.     struct block_def    *bd;
  2146.     linenr_t            lnum;
  2147.     int                    is_del;
  2148. {
  2149.     colnr_t        vcol;
  2150.     int            incr = 0;
  2151.     char_u        *pend;
  2152.     char_u        *pstart;
  2153.  
  2154.     bd->startspaces = 0;
  2155.     bd->endspaces = 0;
  2156.     bd->textlen = 0;
  2157.     bd->textcol = 0;
  2158.     vcol = 0;
  2159.     pstart = ml_get(lnum);
  2160.     while (vcol < op_start_vcol && *pstart)
  2161.     {
  2162.         /* Count a tab for what it's worth (if list mode not on) */
  2163.         incr = lbr_chartabsize(pstart, (colnr_t)vcol);
  2164.         vcol += incr;
  2165.         ++pstart;
  2166.         ++bd->textcol;
  2167.     }
  2168.     if (vcol < op_start_vcol)    /* line too short */
  2169.     {
  2170.         if (!is_del)
  2171.             bd->endspaces = op_end_vcol - op_start_vcol + 1;
  2172.     }
  2173.     else /* vcol >= op_start_vcol */
  2174.     {
  2175.         bd->startspaces = vcol - op_start_vcol;
  2176.         if (is_del && vcol > op_start_vcol)
  2177.             bd->startspaces = incr - bd->startspaces;
  2178.         pend = pstart;
  2179.         if (vcol > op_end_vcol)        /* it's all in one character */
  2180.         {
  2181.             bd->startspaces = op_end_vcol - op_start_vcol + 1;
  2182.             if (is_del)
  2183.                 bd->startspaces = incr - bd->startspaces;
  2184.         }
  2185.         else
  2186.         {
  2187.             while (vcol <= op_end_vcol && *pend)
  2188.             {
  2189.                 /* Count a tab for what it's worth (if list mode not on) */
  2190.                 incr = lbr_chartabsize(pend, (colnr_t)vcol);
  2191.                 vcol += incr;
  2192.                 ++pend;
  2193.             }
  2194.             if (vcol < op_end_vcol && !is_del)    /* line too short */
  2195.             {
  2196.                 bd->endspaces = op_end_vcol - vcol;
  2197.             }
  2198.             else if (vcol > op_end_vcol)
  2199.             {
  2200.                 bd->endspaces = vcol - op_end_vcol - 1;
  2201.                 if (!is_del && pend != pstart && bd->endspaces)
  2202.                     --pend;
  2203.             }
  2204.         }
  2205.         if (is_del && bd->startspaces)
  2206.         {
  2207.             --pstart;
  2208.             --bd->textcol;
  2209.         }
  2210.         bd->textlen = (int)(pend - pstart);
  2211.     }
  2212.     bd->textstart = pstart;
  2213. }
  2214.  
  2215. #define NUMBUFLEN 30
  2216.  
  2217. /*
  2218.  * add or subtract 'Prenum1' from a number in a line
  2219.  * 'command' is CTRL-A for add, CTRL-X for subtract
  2220.  *
  2221.  * return FAIL for failure, OK otherwise
  2222.  */
  2223.     int
  2224. do_addsub(command, Prenum1)
  2225.     int            command;
  2226.     linenr_t    Prenum1;
  2227. {
  2228.     register int     col;
  2229.     char_u            buf[NUMBUFLEN];
  2230.     int                hex;            /* 'X': hexadecimal; '0': octal */
  2231.     static int        hexupper = FALSE;    /* 0xABC */
  2232.     long            n;
  2233.     char_u            *ptr;
  2234.     int                i;
  2235.     int                c;
  2236.     int                zeros = 0;        /* number of leading zeros */
  2237.     int                digits = 0;        /* number of digits in the number */
  2238.  
  2239.     ptr = ml_get_curline();
  2240.     col = curwin->w_cursor.col;
  2241.  
  2242.         /* first check if we are on a hexadecimal number */
  2243.     while (col > 0 && isxdigit(ptr[col]))
  2244.         --col;
  2245.     if (col > 0 && (ptr[col] == 'X' || ptr[col] == 'x') &&
  2246.                         ptr[col - 1] == '0' && isxdigit(ptr[col + 1]))
  2247.         --col;        /* found hexadecimal number */
  2248.     else
  2249.     {
  2250.         /* first search forward and then backward for start of number */
  2251.         col = curwin->w_cursor.col;
  2252.  
  2253.         while (ptr[col] != NUL && !isdigit(ptr[col]))
  2254.             ++col;
  2255.  
  2256.         while (col > 0 && isdigit(ptr[col - 1]))
  2257.             --col;
  2258.     }
  2259.  
  2260.     if (isdigit(ptr[col]) && u_save_cursor() == OK)
  2261.     {
  2262.         ptr = ml_get_curline();                    /* get it again, because
  2263.                                                    u_save may have changed it */
  2264.         curwin->w_set_curswant = TRUE;
  2265.  
  2266.         hex = 0;                                /* default is decimal */
  2267.         if (ptr[col] == '0')                    /* could be hex or octal */
  2268.         {
  2269.             hex = TO_UPPER(ptr[col + 1]);        /* assume hexadecimal */
  2270.             if (hex != 'X' || !isxdigit(ptr[col + 2]))
  2271.             {
  2272.                 if (isdigit(hex))
  2273.                     hex = '0';                    /* octal */
  2274.                 else
  2275.                     hex = 0;                    /* 0 by itself is decimal */
  2276.             }
  2277.         }
  2278.  
  2279.         if (!hex && col > 0 && ptr[col - 1] == '-')
  2280.             --col;
  2281.  
  2282.         ptr += col;
  2283.         /*
  2284.          * we copy the number into a buffer because some versions of sscanf
  2285.          * cannot handle characters with the upper bit set, making some special
  2286.          * characters handled like digits.
  2287.          */
  2288.         for (i = 0; *ptr && !(*ptr & 0x80) && i < NUMBUFLEN - 1; ++i)
  2289.             buf[i] = *ptr++;
  2290.         buf[i] = NUL;
  2291.  
  2292.         if (hex == '0')
  2293.             sscanf((char *)buf, "%lo", &n);
  2294.         else if (hex)
  2295.             sscanf((char *)buf + 2, "%lx", &n);    /* "%X" doesn't work! */
  2296.         else
  2297.             n = atol((char *)buf);
  2298.  
  2299.         if (command == Ctrl('A'))
  2300.             n += Prenum1;
  2301.         else
  2302.             n -= Prenum1;
  2303.  
  2304.         if (hex == 'X')                    /* skip the '0x' */
  2305.             col += 2;
  2306.         else if (hex == '0')
  2307.             col++;                        /* skip the '0' */
  2308.         curwin->w_cursor.col = col;
  2309.  
  2310.         c = gchar_cursor();
  2311.         do                                /* delete the old number */
  2312.         {
  2313.             if (digits == 0 && c == '0')
  2314.                 ++zeros;                /* count the number of leading zeros */
  2315.             else
  2316.                 ++digits;                /* count the number of digits */
  2317.             if (isalpha(c))
  2318.             {
  2319.                 if (isupper(c))
  2320.                     hexupper = TRUE;
  2321.                 else
  2322.                     hexupper = FALSE;
  2323.             }
  2324.             (void)delchar(FALSE);
  2325.             c = gchar_cursor();
  2326.         }
  2327.         while (hex ? (hex == '0' ? c >= '0' && c <= '7' :
  2328.                                         isxdigit(c)) : isdigit(c));
  2329.  
  2330.         if (hex == 0)
  2331.             sprintf((char *)buf, "%ld", n);
  2332.         else
  2333.         {
  2334.             if (hex == '0')
  2335.                 sprintf((char *)buf, "%lo", n);
  2336.             else if (hex && hexupper)
  2337.                 sprintf((char *)buf, "%lX", n);
  2338.             else if (hex)
  2339.                 sprintf((char *)buf, "%lx", n);
  2340.             /* adjust number of zeros to the new number of digits, so the
  2341.              * total length of the number remains the same */
  2342.             if (zeros)
  2343.             {
  2344.                 zeros += digits - STRLEN(buf);
  2345.                 if (zeros > 0)
  2346.                 {
  2347.                     vim_memmove(buf + zeros, buf, STRLEN(buf) + 1);
  2348.                     for (col = 0; zeros > 0; --zeros)
  2349.                         buf[col++] = '0';
  2350.                 }
  2351.             }
  2352.         }
  2353.         ins_str(buf);                    /* insert the new number */
  2354.         --curwin->w_cursor.col;
  2355.         updateline();
  2356.         return OK;
  2357.     }
  2358.     else
  2359.     {
  2360.         beep_flush();
  2361.         return FAIL;
  2362.     }
  2363. }
  2364.  
  2365. #ifdef VIMINFO
  2366.     int
  2367. read_viminfo_register(line, fp, force)
  2368.     char_u    *line;
  2369.     FILE    *fp;
  2370.     int        force;
  2371. {
  2372.     int        eof;
  2373.     int        do_it = TRUE;
  2374.     int        size;
  2375.     int        limit;
  2376.     int        i;
  2377.     int        set_prev = FALSE;
  2378.     char_u    *str;
  2379.     char_u    **array = NULL;
  2380.  
  2381.     /* We only get here (hopefully) if line[0] == '"' */
  2382.     str = line + 1;
  2383.     if (*str == '"')
  2384.     {
  2385.         set_prev = TRUE;
  2386.         str++;
  2387.     }
  2388.     if (!isalnum(*str) && *str != '-')
  2389.     {
  2390.         if (viminfo_error("Illegal register name", line))
  2391.             return TRUE;        /* too many errors, pretend end-of-file */
  2392.         do_it = FALSE;
  2393.     }
  2394.     yankbuffer = *str++;
  2395.     get_yank_buffer(FALSE);
  2396.     yankbuffer = 0;
  2397.     if (!force && y_current->y_array != NULL)
  2398.         do_it = FALSE;
  2399.     size = 0;
  2400.     limit = 100;        /* Optimized for registers containing <= 100 lines */
  2401.     if (do_it)
  2402.     {
  2403.         if (set_prev)
  2404.             y_previous = y_current;
  2405.         vim_free(y_current->y_array);
  2406.         array = y_current->y_array =
  2407.                        (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
  2408.         str = skipwhite(str);
  2409.         if (STRNCMP(str, "CHAR", 4) == 0)
  2410.             y_current->y_type = MCHAR;
  2411.         else if (STRNCMP(str, "BLOCK", 5) == 0)
  2412.             y_current->y_type = MBLOCK;
  2413.         else
  2414.             y_current->y_type = MLINE;
  2415.     }
  2416.     while (!(eof = vim_fgets(line, LSIZE, fp)) && line[0] == TAB)
  2417.     {
  2418.         if (do_it)
  2419.         {
  2420.             if (size >= limit)
  2421.             {
  2422.                 y_current->y_array = (char_u **)
  2423.                               alloc((unsigned)(limit * 2 * sizeof(char_u *)));
  2424.                 for (i = 0; i < limit; i++)
  2425.                     y_current->y_array[i] = array[i];
  2426.                 vim_free(array);
  2427.                 limit *= 2;
  2428.                 array = y_current->y_array;
  2429.             }
  2430.             viminfo_readstring(line);
  2431.             str = strsave(line + 1);
  2432.             if (str != NULL)
  2433.                 array[size++] = str;
  2434.             else
  2435.                 do_it = FALSE;
  2436.         }
  2437.     }
  2438.     if (do_it)
  2439.     {
  2440.         if (size == 0)
  2441.         {
  2442.             vim_free(array);
  2443.             y_current->y_array = NULL;
  2444.         }
  2445.         else if (size < limit)
  2446.         {
  2447.             y_current->y_array =
  2448.                         (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
  2449.             for (i = 0; i < size; i++)
  2450.                 y_current->y_array[i] = array[i];
  2451.             vim_free(array);
  2452.         }
  2453.         y_current->y_size = size;
  2454.     }
  2455.     return eof;
  2456. }
  2457.  
  2458.     void
  2459. write_viminfo_registers(fp)
  2460.     FILE    *fp;
  2461. {
  2462.     int        i, j;
  2463.     char_u    *type;
  2464.     char_u    c;
  2465.     int        num_lines;
  2466.     int        max_num_lines;
  2467.  
  2468.     fprintf(fp, "\n# Registers:\n");
  2469.  
  2470.     max_num_lines = get_viminfo_parameter('"');
  2471.     if (max_num_lines == 0)
  2472.         return;
  2473.     for (i = 0; i < NUM_REGISTERS; i++)
  2474.     {
  2475.         if (y_buf[i].y_array == NULL)
  2476.             continue;
  2477. #ifdef USE_GUI
  2478.         /* Skip '*' register, we don't want it back next time */
  2479.         if (i == GUI_SELECTION_REGISTER)
  2480.             continue;
  2481. #endif
  2482.         switch (y_buf[i].y_type)
  2483.         {
  2484.             case MLINE:
  2485.                 type = (char_u *)"LINE";
  2486.                 break;
  2487.             case MCHAR:
  2488.                 type = (char_u *)"CHAR";
  2489.                 break;
  2490.             case MBLOCK:
  2491.                 type = (char_u *)"BLOCK";
  2492.                 break;
  2493.             default:
  2494.                 sprintf((char *)IObuff, "Unknown register type %d",
  2495.                     y_buf[i].y_type);
  2496.                 emsg(IObuff);
  2497.                 type = (char_u *)"LINE";
  2498.                 break;
  2499.         }
  2500.         if (y_previous == &y_buf[i])
  2501.             fprintf(fp, "\"");
  2502.         if (i == DELETION_REGISTER)
  2503.             c = '-';
  2504.         else if (i < 10)
  2505.             c = '0' + i;
  2506.         else
  2507.             c = 'a' + i - 10;
  2508.         fprintf(fp, "\"%c\t%s\n", c, type);
  2509.         num_lines = y_buf[i].y_size;
  2510.  
  2511.         /* If max_num_lines < 0, then we save ALL the lines in the register */
  2512.         if (max_num_lines > 0 && num_lines > max_num_lines)
  2513.             num_lines = max_num_lines;
  2514.         for (j = 0; j < num_lines; j++)
  2515.         {
  2516.             putc('\t', fp);
  2517.             viminfo_writestring(fp, y_buf[i].y_array[j]);
  2518.         }
  2519.     }
  2520. }
  2521. #endif /* VIMINFO */
  2522.  
  2523. #if defined(USE_GUI) || defined(PROTO)
  2524. /*
  2525.  * Text selection stuff that uses the GUI selection register '*'.  When using a
  2526.  * GUI this may be text from another window, otherwise it is the last text we
  2527.  * had highlighted with VIsual mode.  With mouse support, clicking the middle
  2528.  * button performs the paste, otherwise you will need to do <"*p>.
  2529.  */
  2530.  
  2531.     void
  2532. gui_free_selection()
  2533. {
  2534.     struct yankbuf *y_ptr = y_current;
  2535.  
  2536.     y_current = &y_buf[GUI_SELECTION_REGISTER];        /* '*' register */
  2537.     free_yank_all();
  2538.     y_current->y_size = 0;
  2539.     y_current = y_ptr;
  2540. }
  2541.  
  2542. /*
  2543.  * Get the selected text and put it in the gui text register '*'.
  2544.  */
  2545.     void
  2546. gui_get_selection()
  2547. {
  2548.     struct yankbuf *old_y_previous, *old_y_current;
  2549.     char_u    old_yankbuffer;
  2550.     FPOS    old_cursor, old_visual;
  2551.     int        old_op_type;
  2552.  
  2553.     if (gui.selection.owned)
  2554.     {
  2555.         if (y_buf[GUI_SELECTION_REGISTER].y_array != NULL)
  2556.             return;
  2557.  
  2558.         /* Get the text between gui.selection.start & gui.selection.end */
  2559.         old_y_previous = y_previous;
  2560.         old_y_current = y_current;
  2561.         old_yankbuffer = yankbuffer;
  2562.         old_cursor = curwin->w_cursor;
  2563.         old_visual = VIsual;
  2564.         old_op_type = op_type;
  2565.         yankbuffer = '*';
  2566.         op_type = YANK;
  2567.         do_pending_operator('y', NUL, FALSE, NULL, NULL, 0, TRUE, TRUE);
  2568.         y_previous = old_y_previous;
  2569.         y_current = old_y_current;
  2570.         yankbuffer = old_yankbuffer;
  2571.         curwin->w_cursor = old_cursor;
  2572.         VIsual = old_visual;
  2573.         op_type = old_op_type;
  2574.     }
  2575.     else
  2576.     {
  2577.         gui_free_selection();
  2578.  
  2579.         /* Try to get selected text from another window */
  2580.         gui_request_selection();
  2581.     }
  2582. }
  2583.  
  2584. /* Convert from the GUI selection string into the '*' register */
  2585.     void
  2586. gui_yank_selection(type, str, len)
  2587.     int        type;
  2588.     char_u    *str;
  2589.     long_u    len;
  2590. {
  2591.     struct yankbuf *y_ptr = &y_buf[GUI_SELECTION_REGISTER];    /* '*' register */
  2592.     int        lnum;
  2593.     int        start;
  2594.     int        i;
  2595.  
  2596.     gui_free_selection();
  2597.  
  2598.     /* Count the number of lines within the string */
  2599.     y_ptr->y_size = 1;
  2600.     for (i = 0; i < len; i++)
  2601.         if (str[i] == '\n')
  2602.             y_ptr->y_size++;
  2603.  
  2604.     if (type != MCHAR && i > 0 && str[i - 1] == '\n')
  2605.         y_ptr->y_size--;
  2606.  
  2607.     y_ptr->y_array = (char_u **)lalloc(y_ptr->y_size * sizeof(char_u *), TRUE);
  2608.     if (y_ptr->y_array == NULL)
  2609.         return;
  2610.     y_ptr->y_type = type;
  2611.     lnum = 0;
  2612.     start = 0;
  2613.     for (i = 0; i < len; i++)
  2614.     {
  2615.         if (str[i] == NUL)
  2616.             str[i] = '\n';
  2617.         else if (str[i] == '\n')
  2618.         {
  2619.             str[i] = NUL;
  2620.             if (type == MCHAR || i != len - 1)
  2621.             {
  2622.                 if ((y_ptr->y_array[lnum] = strsave(str + start)) == NULL)
  2623.                 {
  2624.                     y_ptr->y_size = lnum;
  2625.                     return;
  2626.                 }
  2627.                 lnum++;
  2628.                 start = i + 1;
  2629.             }
  2630.         }
  2631.     }
  2632.     if ((y_ptr->y_array[lnum] = alloc(i - start + 1)) == NULL)
  2633.         return;
  2634.     if (i - start > 0)
  2635.         STRNCPY(y_ptr->y_array[lnum], str + start, i - start);
  2636.     y_ptr->y_array[lnum][i - start] = NUL;
  2637.     y_ptr->y_size = lnum + 1;
  2638. }
  2639.  
  2640. /*
  2641.  * Convert the '*' register into a GUI selection string returned in *str with
  2642.  * length *len.
  2643.  */
  2644.     int
  2645. gui_convert_selection(str, len)
  2646.     char_u    **str;
  2647.     long_u    *len;
  2648. {
  2649.     struct yankbuf *y_ptr = &y_buf[GUI_SELECTION_REGISTER];    /* '*' register */
  2650.     char_u    *p;
  2651.     int        lnum;
  2652.     int        i, j;
  2653.  
  2654.     *str = NULL;
  2655.     *len = 0;
  2656.     if (y_ptr->y_array == NULL)
  2657.         return -1;
  2658.  
  2659.     for (i = 0; i < y_ptr->y_size; i++)
  2660.         *len += STRLEN(y_ptr->y_array[i]) + 1;
  2661.  
  2662.     /*
  2663.      * Don't want newline character at end of last line if we're in MCHAR mode.
  2664.      */
  2665.     if (y_ptr->y_type == MCHAR && *len > 1)
  2666.         (*len)--;
  2667.  
  2668.     p = *str = lalloc(*len, TRUE);
  2669.     if (p == NULL)
  2670.         return -1;
  2671.     lnum = 0;
  2672.     for (i = 0, j = 0; i < *len; i++, j++)
  2673.     {
  2674.         if (y_ptr->y_array[lnum][j] == '\n')
  2675.             p[i] = NUL;
  2676.         else if (y_ptr->y_array[lnum][j] == NUL)
  2677.         {
  2678.             p[i] = '\n';
  2679.             lnum++;
  2680.             j = -1;
  2681.         }
  2682.         else
  2683.             p[i] = y_ptr->y_array[lnum][j];
  2684.     }
  2685.     return y_ptr->y_type;
  2686. }
  2687. #endif /* USE_GUI || PROTO */
  2688.