Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * ops.c: implementation of various operators: op_shift, op_delete, op_tilde, |
| 12 | * op_change, op_yank, do_put, do_join |
| 13 | */ |
| 14 | |
| 15 | #include "vim.h" |
| 16 | |
| 17 | /* |
| 18 | * Number of registers. |
| 19 | * 0 = unnamed register, for normal yanks and puts |
| 20 | * 1..9 = registers '1' to '9', for deletes |
| 21 | * 10..35 = registers 'a' to 'z' |
| 22 | * 36 = delete register '-' |
| 23 | * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined |
| 24 | * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined |
| 25 | */ |
| 26 | /* |
| 27 | * Symbolic names for some registers. |
| 28 | */ |
| 29 | #define DELETION_REGISTER 36 |
| 30 | #ifdef FEAT_CLIPBOARD |
| 31 | # define STAR_REGISTER 37 |
| 32 | # ifdef FEAT_X11 |
| 33 | # define PLUS_REGISTER 38 |
| 34 | # else |
| 35 | # define PLUS_REGISTER STAR_REGISTER /* there is only one */ |
| 36 | # endif |
| 37 | #endif |
| 38 | #ifdef FEAT_DND |
| 39 | # define TILDE_REGISTER (PLUS_REGISTER + 1) |
| 40 | #endif |
| 41 | |
| 42 | #ifdef FEAT_CLIPBOARD |
| 43 | # ifdef FEAT_DND |
| 44 | # define NUM_REGISTERS (TILDE_REGISTER + 1) |
| 45 | # else |
| 46 | # define NUM_REGISTERS (PLUS_REGISTER + 1) |
| 47 | # endif |
| 48 | #else |
| 49 | # define NUM_REGISTERS 37 |
| 50 | #endif |
| 51 | |
| 52 | /* |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 53 | * Each yank register has an array of pointers to lines. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 54 | */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 55 | typedef struct |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | { |
| 57 | char_u **y_array; /* pointer to array of line pointers */ |
| 58 | linenr_T y_size; /* number of lines in y_array */ |
| 59 | char_u y_type; /* MLINE, MCHAR or MBLOCK */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 60 | colnr_T y_width; /* only set if y_type == MBLOCK */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 61 | #ifdef FEAT_VIMINFO |
| 62 | time_t y_time_set; |
| 63 | #endif |
| 64 | } yankreg_T; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 66 | static yankreg_T y_regs[NUM_REGISTERS]; |
| 67 | |
| 68 | static yankreg_T *y_current; /* ptr to current yankreg */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | static int y_append; /* TRUE when appending */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 70 | static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * structure used by block_prep, op_delete and op_yank for blockwise operators |
| 74 | * also op_change, op_shift, op_insert, op_replace - AKelly |
| 75 | */ |
| 76 | struct block_def |
| 77 | { |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 78 | int startspaces; /* 'extra' cols before first char */ |
| 79 | int endspaces; /* 'extra' cols after last char */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | int textlen; /* chars in block */ |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 81 | char_u *textstart; /* pointer to 1st char (partially) in block */ |
| 82 | colnr_T textcol; /* index of chars (partially) in block */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | colnr_T start_vcol; /* start col of 1st char wholly inside block */ |
| 84 | colnr_T end_vcol; /* start col of 1st char wholly after block */ |
| 85 | #ifdef FEAT_VISUALEXTRA |
| 86 | int is_short; /* TRUE if line is too short to fit in block */ |
| 87 | int is_MAX; /* TRUE if curswant==MAXCOL when starting */ |
| 88 | int is_oneChar; /* TRUE if block within one character */ |
| 89 | int pre_whitesp; /* screen cols of ws before block */ |
| 90 | int pre_whitesp_c; /* chars of ws before block */ |
| 91 | colnr_T end_char_vcols; /* number of vcols of post-block char */ |
| 92 | #endif |
| 93 | colnr_T start_char_vcols; /* number of vcols of pre-block char */ |
| 94 | }; |
| 95 | |
| 96 | #ifdef FEAT_VISUALEXTRA |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 97 | static void shift_block(oparg_T *oap, int amount); |
| 98 | static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 100 | static int stuff_yank(int, char_u *); |
| 101 | static void put_reedit_in_typebuf(int silent); |
| 102 | static int put_in_typebuf(char_u *s, int esc, int colon, |
| 103 | int silent); |
| 104 | static void stuffescaped(char_u *arg, int literally); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 105 | #ifdef FEAT_MBYTE |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 106 | static void mb_adjust_opend(oparg_T *oap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 108 | static void free_yank(long); |
| 109 | static void free_yank_all(void); |
| 110 | static int yank_copy_line(struct block_def *bd, long y_idx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 112 | static void copy_yank_reg(yankreg_T *reg); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 113 | static void may_set_selection(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 114 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 115 | static void dis_msg(char_u *p, int skip_esc); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 116 | static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int); |
| 117 | static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 118 | #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 119 | static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 121 | static int ends_in_white(linenr_T lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 122 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 123 | static int same_leader(linenr_T lnum, int, char_u *, int, char_u *); |
| 124 | static int fmt_check_par(linenr_T, int *, char_u **, int do_comments); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 125 | #else |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 126 | static int fmt_check_par(linenr_T); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 129 | // Flags for third item in "opchars". |
| 130 | #define OPF_LINES 1 // operator always works on lines |
| 131 | #define OPF_CHANGE 2 // operator changes text |
| 132 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | /* |
| 134 | * The names of operators. |
| 135 | * IMPORTANT: Index must correspond with defines in vim.h!!! |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 136 | * The third field holds OPF_ flags. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 137 | */ |
| 138 | static char opchars[][3] = |
| 139 | { |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 140 | {NUL, NUL, 0}, // OP_NOP |
| 141 | {'d', NUL, OPF_CHANGE}, // OP_DELETE |
| 142 | {'y', NUL, 0}, // OP_YANK |
| 143 | {'c', NUL, OPF_CHANGE}, // OP_CHANGE |
| 144 | {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT |
| 145 | {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT |
| 146 | {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER |
| 147 | {'g', '~', OPF_CHANGE}, // OP_TILDE |
| 148 | {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT |
| 149 | {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT |
| 150 | {':', NUL, OPF_LINES}, // OP_COLON |
| 151 | {'g', 'U', OPF_CHANGE}, // OP_UPPER |
| 152 | {'g', 'u', OPF_CHANGE}, // OP_LOWER |
| 153 | {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN |
| 154 | {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS |
| 155 | {'g', '?', OPF_CHANGE}, // OP_ROT13 |
| 156 | {'r', NUL, OPF_CHANGE}, // OP_REPLACE |
| 157 | {'I', NUL, OPF_CHANGE}, // OP_INSERT |
| 158 | {'A', NUL, OPF_CHANGE}, // OP_APPEND |
| 159 | {'z', 'f', OPF_LINES}, // OP_FOLD |
| 160 | {'z', 'o', OPF_LINES}, // OP_FOLDOPEN |
| 161 | {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC |
| 162 | {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE |
| 163 | {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC |
| 164 | {'z', 'd', OPF_LINES}, // OP_FOLDDEL |
| 165 | {'z', 'D', OPF_LINES}, // OP_FOLDDELREC |
| 166 | {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2 |
| 167 | {'g', '@', OPF_CHANGE}, // OP_FUNCTION |
| 168 | {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD |
| 169 | {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /* |
| 173 | * Translate a command name into an operator type. |
| 174 | * Must only be called with a valid operator name! |
| 175 | */ |
| 176 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 177 | get_op_type(int char1, int char2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 178 | { |
| 179 | int i; |
| 180 | |
| 181 | if (char1 == 'r') /* ignore second character */ |
| 182 | return OP_REPLACE; |
| 183 | if (char1 == '~') /* when tilde is an operator */ |
| 184 | return OP_TILDE; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 185 | if (char1 == 'g' && char2 == Ctrl_A) /* add */ |
| 186 | return OP_NR_ADD; |
| 187 | if (char1 == 'g' && char2 == Ctrl_X) /* subtract */ |
| 188 | return OP_NR_SUB; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | for (i = 0; ; ++i) |
Bram Moolenaar | 2efb323 | 2017-12-19 12:27:23 +0100 | [diff] [blame] | 190 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 191 | if (opchars[i][0] == char1 && opchars[i][1] == char2) |
| 192 | break; |
Bram Moolenaar | 2efb323 | 2017-12-19 12:27:23 +0100 | [diff] [blame] | 193 | if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1)) |
| 194 | { |
| 195 | internal_error("get_op_type()"); |
| 196 | break; |
| 197 | } |
| 198 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 199 | return i; |
| 200 | } |
| 201 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | /* |
| 203 | * Return TRUE if operator "op" always works on whole lines. |
| 204 | */ |
| 205 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 206 | op_on_lines(int op) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 207 | { |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 208 | return opchars[op][2] & OPF_LINES; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Return TRUE if operator "op" changes text. |
| 213 | */ |
| 214 | int |
| 215 | op_is_change(int op) |
| 216 | { |
| 217 | return opchars[op][2] & OPF_CHANGE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 218 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Get first operator command character. |
| 222 | * Returns 'g' or 'z' if there is another command character. |
| 223 | */ |
| 224 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 225 | get_op_char(int optype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 226 | { |
| 227 | return opchars[optype][0]; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Get second operator command character. |
| 232 | */ |
| 233 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 234 | get_extra_op_char(int optype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | { |
| 236 | return opchars[optype][1]; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * op_shift - handle a shift operation |
| 241 | */ |
| 242 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 243 | op_shift(oparg_T *oap, int curs_top, int amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 244 | { |
| 245 | long i; |
| 246 | int first_char; |
| 247 | char_u *s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 248 | int block_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 249 | |
| 250 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 251 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 252 | return; |
| 253 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 254 | if (oap->block_mode) |
| 255 | block_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 256 | |
| 257 | for (i = oap->line_count; --i >= 0; ) |
| 258 | { |
| 259 | first_char = *ml_get_curline(); |
| 260 | if (first_char == NUL) /* empty line */ |
| 261 | curwin->w_cursor.col = 0; |
| 262 | #ifdef FEAT_VISUALEXTRA |
| 263 | else if (oap->block_mode) |
| 264 | shift_block(oap, amount); |
| 265 | #endif |
| 266 | else |
| 267 | /* Move the line right if it doesn't start with '#', 'smartindent' |
| 268 | * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */ |
| 269 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) |
| 270 | if (first_char != '#' || !preprocs_left()) |
| 271 | #endif |
| 272 | { |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 273 | shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | } |
| 275 | ++curwin->w_cursor.lnum; |
| 276 | } |
| 277 | |
| 278 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 279 | if (oap->block_mode) |
| 280 | { |
| 281 | curwin->w_cursor.lnum = oap->start.lnum; |
| 282 | curwin->w_cursor.col = block_col; |
| 283 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 284 | else if (curs_top) /* put cursor on first line, for ">>" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 285 | { |
| 286 | curwin->w_cursor.lnum = oap->start.lnum; |
| 287 | beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */ |
| 288 | } |
| 289 | else |
| 290 | --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */ |
| 291 | |
Bram Moolenaar | 54b2bfa | 2017-01-02 14:57:08 +0100 | [diff] [blame] | 292 | #ifdef FEAT_FOLDING |
| 293 | /* The cursor line is not in a closed fold */ |
| 294 | foldOpenCursor(); |
| 295 | #endif |
| 296 | |
| 297 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 298 | if (oap->line_count > p_report) |
| 299 | { |
| 300 | if (oap->op_type == OP_RSHIFT) |
| 301 | s = (char_u *)">"; |
| 302 | else |
| 303 | s = (char_u *)"<"; |
| 304 | if (oap->line_count == 1) |
| 305 | { |
| 306 | if (amount == 1) |
| 307 | sprintf((char *)IObuff, _("1 line %sed 1 time"), s); |
| 308 | else |
| 309 | sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount); |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | if (amount == 1) |
| 314 | sprintf((char *)IObuff, _("%ld lines %sed 1 time"), |
| 315 | oap->line_count, s); |
| 316 | else |
| 317 | sprintf((char *)IObuff, _("%ld lines %sed %d times"), |
| 318 | oap->line_count, s, amount); |
| 319 | } |
| 320 | msg(IObuff); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Set "'[" and "']" marks. |
| 325 | */ |
| 326 | curbuf->b_op_start = oap->start; |
| 327 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 328 | curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 329 | if (curbuf->b_op_end.col > 0) |
| 330 | --curbuf->b_op_end.col; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * shift the current line one shiftwidth left (if left != 0) or right |
| 335 | * leaves cursor on first blank in the line |
| 336 | */ |
| 337 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 338 | shift_line( |
| 339 | int left, |
| 340 | int round, |
| 341 | int amount, |
| 342 | int call_changed_bytes) /* call changed_bytes() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 343 | { |
| 344 | int count; |
| 345 | int i, j; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 346 | int p_sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 347 | |
| 348 | count = get_indent(); /* get current indent */ |
| 349 | |
| 350 | if (round) /* round off indent */ |
| 351 | { |
| 352 | i = count / p_sw; /* number of p_sw rounded down */ |
| 353 | j = count % p_sw; /* extra spaces */ |
| 354 | if (j && left) /* first remove extra spaces */ |
| 355 | --amount; |
| 356 | if (left) |
| 357 | { |
| 358 | i -= amount; |
| 359 | if (i < 0) |
| 360 | i = 0; |
| 361 | } |
| 362 | else |
| 363 | i += amount; |
| 364 | count = i * p_sw; |
| 365 | } |
| 366 | else /* original vi indent */ |
| 367 | { |
| 368 | if (left) |
| 369 | { |
| 370 | count -= p_sw * amount; |
| 371 | if (count < 0) |
| 372 | count = 0; |
| 373 | } |
| 374 | else |
| 375 | count += p_sw * amount; |
| 376 | } |
| 377 | |
| 378 | /* Set new indent */ |
| 379 | #ifdef FEAT_VREPLACE |
| 380 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 381 | change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 382 | else |
| 383 | #endif |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 384 | (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | #if defined(FEAT_VISUALEXTRA) || defined(PROTO) |
| 388 | /* |
| 389 | * Shift one line of the current block one shiftwidth right or left. |
| 390 | * Leaves cursor on first character in block. |
| 391 | */ |
| 392 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 393 | shift_block(oparg_T *oap, int amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 394 | { |
| 395 | int left = (oap->op_type == OP_LSHIFT); |
| 396 | int oldstate = State; |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 397 | int total; |
| 398 | char_u *newp, *oldp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 399 | int oldcol = curwin->w_cursor.col; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 400 | int p_sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 401 | int p_ts = (int)curbuf->b_p_ts; |
| 402 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 403 | int incr; |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 404 | colnr_T ws_vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 405 | int i = 0, j = 0; |
| 406 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 407 | #ifdef FEAT_RIGHTLEFT |
| 408 | int old_p_ri = p_ri; |
| 409 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 410 | p_ri = 0; /* don't want revins in indent */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 411 | #endif |
| 412 | |
| 413 | State = INSERT; /* don't want REPLACE for State */ |
| 414 | block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); |
| 415 | if (bd.is_short) |
| 416 | return; |
| 417 | |
| 418 | /* total is number of screen columns to be inserted/removed */ |
Bram Moolenaar | bae5a17 | 2017-08-06 15:42:06 +0200 | [diff] [blame] | 419 | total = (int)((unsigned)amount * (unsigned)p_sw); |
| 420 | if ((total / p_sw) != amount) |
| 421 | return; /* multiplication overflow */ |
| 422 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 423 | oldp = ml_get_curline(); |
| 424 | |
| 425 | if (!left) |
| 426 | { |
| 427 | /* |
| 428 | * 1. Get start vcol |
| 429 | * 2. Total ws vcols |
| 430 | * 3. Divvy into TABs & spp |
| 431 | * 4. Construct new string |
| 432 | */ |
| 433 | total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */ |
| 434 | ws_vcol = bd.start_vcol - bd.pre_whitesp; |
| 435 | if (bd.startspaces) |
| 436 | { |
| 437 | #ifdef FEAT_MBYTE |
| 438 | if (has_mbyte) |
Bram Moolenaar | 20b4f46 | 2016-03-05 17:25:39 +0100 | [diff] [blame] | 439 | { |
| 440 | if ((*mb_ptr2len)(bd.textstart) == 1) |
| 441 | ++bd.textstart; |
| 442 | else |
| 443 | { |
| 444 | ws_vcol = 0; |
| 445 | bd.startspaces = 0; |
| 446 | } |
| 447 | } |
Bram Moolenaar | be1138b | 2009-11-11 16:22:28 +0000 | [diff] [blame] | 448 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 449 | #endif |
Bram Moolenaar | be1138b | 2009-11-11 16:22:28 +0000 | [diff] [blame] | 450 | ++bd.textstart; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 451 | } |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 452 | for ( ; VIM_ISWHITE(*bd.textstart); ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 453 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 454 | /* TODO: is passing bd.textstart for start of the line OK? */ |
| 455 | incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart, |
| 456 | (colnr_T)(bd.start_vcol)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 457 | total += incr; |
| 458 | bd.start_vcol += incr; |
| 459 | } |
| 460 | /* OK, now total=all the VWS reqd, and textstart points at the 1st |
| 461 | * non-ws char in the block. */ |
| 462 | if (!curbuf->b_p_et) |
| 463 | i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */ |
| 464 | if (i) |
| 465 | j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */ |
| 466 | else |
| 467 | j = total; |
| 468 | /* if we're splitting a TAB, allow for it */ |
| 469 | bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0); |
| 470 | len = (int)STRLEN(bd.textstart) + 1; |
| 471 | newp = alloc_check((unsigned)(bd.textcol + i + j + len)); |
| 472 | if (newp == NULL) |
| 473 | return; |
| 474 | vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len)); |
| 475 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 476 | vim_memset(newp + bd.textcol, TAB, (size_t)i); |
| 477 | vim_memset(newp + bd.textcol + i, ' ', (size_t)j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 478 | /* the end */ |
| 479 | mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len); |
| 480 | } |
| 481 | else /* left */ |
| 482 | { |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 483 | colnr_T destination_col; /* column to which text in block will |
| 484 | be shifted */ |
| 485 | char_u *verbatim_copy_end; /* end of the part of the line which is |
| 486 | copied verbatim */ |
| 487 | colnr_T verbatim_copy_width;/* the (displayed) width of this part |
| 488 | of line */ |
| 489 | unsigned fill; /* nr of spaces that replace a TAB */ |
| 490 | unsigned new_line_len; /* the length of the line after the |
| 491 | block shift */ |
| 492 | size_t block_space_width; |
| 493 | size_t shift_amount; |
| 494 | char_u *non_white = bd.textstart; |
| 495 | colnr_T non_white_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 496 | |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 497 | /* |
| 498 | * Firstly, let's find the first non-whitespace character that is |
| 499 | * displayed after the block's start column and the character's column |
| 500 | * number. Also, let's calculate the width of all the whitespace |
| 501 | * characters that are displayed in the block and precede the searched |
| 502 | * non-whitespace character. |
| 503 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 504 | |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 505 | /* If "bd.startspaces" is set, "bd.textstart" points to the character, |
| 506 | * the part of which is displayed at the block's beginning. Let's start |
| 507 | * searching from the next character. */ |
| 508 | if (bd.startspaces) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 509 | MB_PTR_ADV(non_white); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 510 | |
| 511 | /* The character's column is in "bd.start_vcol". */ |
| 512 | non_white_col = bd.start_vcol; |
| 513 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 514 | while (VIM_ISWHITE(*non_white)) |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 515 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 516 | incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 517 | non_white_col += incr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 520 | block_space_width = non_white_col - oap->start_vcol; |
| 521 | /* We will shift by "total" or "block_space_width", whichever is less. |
| 522 | */ |
Bram Moolenaar | 92a990b | 2009-04-22 15:45:05 +0000 | [diff] [blame] | 523 | shift_amount = (block_space_width < (size_t)total |
| 524 | ? block_space_width : (size_t)total); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 525 | |
| 526 | /* The column to which we will shift the text. */ |
Bram Moolenaar | 92a990b | 2009-04-22 15:45:05 +0000 | [diff] [blame] | 527 | destination_col = (colnr_T)(non_white_col - shift_amount); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 528 | |
| 529 | /* Now let's find out how much of the beginning of the line we can |
| 530 | * reuse without modification. */ |
| 531 | verbatim_copy_end = bd.textstart; |
| 532 | verbatim_copy_width = bd.start_vcol; |
| 533 | |
| 534 | /* If "bd.startspaces" is set, "bd.textstart" points to the character |
| 535 | * preceding the block. We have to subtract its width to obtain its |
| 536 | * column number. */ |
| 537 | if (bd.startspaces) |
| 538 | verbatim_copy_width -= bd.start_char_vcols; |
| 539 | while (verbatim_copy_width < destination_col) |
| 540 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 541 | char_u *line = verbatim_copy_end; |
| 542 | |
| 543 | /* TODO: is passing verbatim_copy_end for start of the line OK? */ |
| 544 | incr = lbr_chartabsize(line, verbatim_copy_end, |
| 545 | verbatim_copy_width); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 546 | if (verbatim_copy_width + incr > destination_col) |
| 547 | break; |
| 548 | verbatim_copy_width += incr; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 549 | MB_PTR_ADV(verbatim_copy_end); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* If "destination_col" is different from the width of the initial |
| 553 | * part of the line that will be copied, it means we encountered a tab |
| 554 | * character, which we will have to partly replace with spaces. */ |
| 555 | fill = destination_col - verbatim_copy_width; |
| 556 | |
| 557 | /* The replacement line will consist of: |
| 558 | * - the beginning of the original line up to "verbatim_copy_end", |
| 559 | * - "fill" number of spaces, |
| 560 | * - the rest of the line, pointed to by non_white. */ |
| 561 | new_line_len = (unsigned)(verbatim_copy_end - oldp) |
| 562 | + fill |
| 563 | + (unsigned)STRLEN(non_white) + 1; |
| 564 | |
| 565 | newp = alloc_check(new_line_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 566 | if (newp == NULL) |
| 567 | return; |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 568 | mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp)); |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 569 | vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 570 | STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 571 | } |
| 572 | /* replace the line */ |
| 573 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 574 | changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol); |
| 575 | State = oldstate; |
| 576 | curwin->w_cursor.col = oldcol; |
| 577 | #ifdef FEAT_RIGHTLEFT |
| 578 | p_ri = old_p_ri; |
| 579 | #endif |
| 580 | } |
| 581 | #endif |
| 582 | |
| 583 | #ifdef FEAT_VISUALEXTRA |
| 584 | /* |
| 585 | * Insert string "s" (b_insert ? before : after) block :AKelly |
| 586 | * Caller must prepare for undo. |
| 587 | */ |
| 588 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 589 | block_insert( |
| 590 | oparg_T *oap, |
| 591 | char_u *s, |
| 592 | int b_insert, |
| 593 | struct block_def *bdp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 594 | { |
| 595 | int p_ts; |
| 596 | int count = 0; /* extra spaces to replace a cut TAB */ |
| 597 | int spaces = 0; /* non-zero if cutting a TAB */ |
| 598 | colnr_T offset; /* pointer along new line */ |
| 599 | unsigned s_len; /* STRLEN(s) */ |
| 600 | char_u *newp, *oldp; /* new, old lines */ |
| 601 | linenr_T lnum; /* loop var */ |
| 602 | int oldstate = State; |
| 603 | |
| 604 | State = INSERT; /* don't want REPLACE for State */ |
| 605 | s_len = (unsigned)STRLEN(s); |
| 606 | |
| 607 | for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++) |
| 608 | { |
| 609 | block_prep(oap, bdp, lnum, TRUE); |
| 610 | if (bdp->is_short && b_insert) |
| 611 | continue; /* OP_INSERT, line ends before block start */ |
| 612 | |
| 613 | oldp = ml_get(lnum); |
| 614 | |
| 615 | if (b_insert) |
| 616 | { |
| 617 | p_ts = bdp->start_char_vcols; |
| 618 | spaces = bdp->startspaces; |
| 619 | if (spaces != 0) |
| 620 | count = p_ts - 1; /* we're cutting a TAB */ |
| 621 | offset = bdp->textcol; |
| 622 | } |
| 623 | else /* append */ |
| 624 | { |
| 625 | p_ts = bdp->end_char_vcols; |
| 626 | if (!bdp->is_short) /* spaces = padding after block */ |
| 627 | { |
| 628 | spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0); |
| 629 | if (spaces != 0) |
| 630 | count = p_ts - 1; /* we're cutting a TAB */ |
| 631 | offset = bdp->textcol + bdp->textlen - (spaces != 0); |
| 632 | } |
| 633 | else /* spaces = padding to block edge */ |
| 634 | { |
| 635 | /* if $ used, just append to EOL (ie spaces==0) */ |
| 636 | if (!bdp->is_MAX) |
| 637 | spaces = (oap->end_vcol - bdp->end_vcol) + 1; |
| 638 | count = spaces; |
| 639 | offset = bdp->textcol + bdp->textlen; |
| 640 | } |
| 641 | } |
| 642 | |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 643 | #ifdef FEAT_MBYTE |
| 644 | if (has_mbyte && spaces > 0) |
| 645 | { |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 646 | int off; |
| 647 | |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 648 | /* Avoid starting halfway a multi-byte character. */ |
| 649 | if (b_insert) |
| 650 | { |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 651 | off = (*mb_head_off)(oldp, oldp + offset + spaces); |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 652 | } |
| 653 | else |
| 654 | { |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 655 | off = (*mb_off_next)(oldp, oldp + offset); |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 656 | offset += off; |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 657 | } |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 658 | spaces -= off; |
| 659 | count -= off; |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 660 | } |
| 661 | #endif |
| 662 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 663 | newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1); |
| 664 | if (newp == NULL) |
| 665 | continue; |
| 666 | |
| 667 | /* copy up to shifted part */ |
| 668 | mch_memmove(newp, oldp, (size_t)(offset)); |
| 669 | oldp += offset; |
| 670 | |
| 671 | /* insert pre-padding */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 672 | vim_memset(newp + offset, ' ', (size_t)spaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 673 | |
| 674 | /* copy the new text */ |
| 675 | mch_memmove(newp + offset + spaces, s, (size_t)s_len); |
| 676 | offset += s_len; |
| 677 | |
| 678 | if (spaces && !bdp->is_short) |
| 679 | { |
| 680 | /* insert post-padding */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 681 | vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 682 | /* We're splitting a TAB, don't copy it. */ |
| 683 | oldp++; |
| 684 | /* We allowed for that TAB, remember this now */ |
| 685 | count++; |
| 686 | } |
| 687 | |
| 688 | if (spaces > 0) |
| 689 | offset += count; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 690 | STRMOVE(newp + offset, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 691 | |
| 692 | ml_replace(lnum, newp, FALSE); |
| 693 | |
| 694 | if (lnum == oap->end.lnum) |
| 695 | { |
| 696 | /* Set "']" mark to the end of the block instead of the end of |
| 697 | * the insert in the first line. */ |
| 698 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 699 | curbuf->b_op_end.col = offset; |
| 700 | } |
| 701 | } /* for all lnum */ |
| 702 | |
| 703 | changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); |
| 704 | |
| 705 | State = oldstate; |
| 706 | } |
| 707 | #endif |
| 708 | |
| 709 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 710 | /* |
| 711 | * op_reindent - handle reindenting a block of lines. |
| 712 | */ |
| 713 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 714 | op_reindent(oparg_T *oap, int (*how)(void)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 715 | { |
| 716 | long i; |
| 717 | char_u *l; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 718 | int amount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 719 | linenr_T first_changed = 0; |
| 720 | linenr_T last_changed = 0; |
| 721 | linenr_T start_lnum = curwin->w_cursor.lnum; |
| 722 | |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 723 | /* Don't even try when 'modifiable' is off. */ |
| 724 | if (!curbuf->b_p_ma) |
| 725 | { |
| 726 | EMSG(_(e_modifiable)); |
| 727 | return; |
| 728 | } |
| 729 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 730 | for (i = oap->line_count; --i >= 0 && !got_int; ) |
| 731 | { |
| 732 | /* it's a slow thing to do, so give feedback so there's no worry that |
| 733 | * the computer's just hung. */ |
| 734 | |
| 735 | if (i > 1 |
| 736 | && (i % 50 == 0 || i == oap->line_count - 1) |
| 737 | && oap->line_count > p_report) |
| 738 | smsg((char_u *)_("%ld lines to indent... "), i); |
| 739 | |
| 740 | /* |
| 741 | * Be vi-compatible: For lisp indenting the first line is not |
| 742 | * indented, unless there is only one line. |
| 743 | */ |
| 744 | #ifdef FEAT_LISP |
| 745 | if (i != oap->line_count - 1 || oap->line_count == 1 |
| 746 | || how != get_lisp_indent) |
| 747 | #endif |
| 748 | { |
| 749 | l = skipwhite(ml_get_curline()); |
| 750 | if (*l == NUL) /* empty or blank line */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 751 | amount = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 752 | else |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 753 | amount = how(); /* get the indent for this line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 754 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 755 | if (amount >= 0 && set_indent(amount, SIN_UNDO)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 756 | { |
| 757 | /* did change the indent, call changed_lines() later */ |
| 758 | if (first_changed == 0) |
| 759 | first_changed = curwin->w_cursor.lnum; |
| 760 | last_changed = curwin->w_cursor.lnum; |
| 761 | } |
| 762 | } |
| 763 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 764 | curwin->w_cursor.col = 0; /* make sure it's valid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* put cursor on first non-blank of indented line */ |
| 768 | curwin->w_cursor.lnum = start_lnum; |
| 769 | beginline(BL_SOL | BL_FIX); |
| 770 | |
| 771 | /* Mark changed lines so that they will be redrawn. When Visual |
| 772 | * highlighting was present, need to continue until the last line. When |
| 773 | * there is no change still need to remove the Visual highlighting. */ |
| 774 | if (last_changed != 0) |
| 775 | changed_lines(first_changed, 0, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 776 | oap->is_VIsual ? start_lnum + oap->line_count : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 777 | last_changed + 1, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 778 | else if (oap->is_VIsual) |
| 779 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 780 | |
| 781 | if (oap->line_count > p_report) |
| 782 | { |
| 783 | i = oap->line_count - (i + 1); |
| 784 | if (i == 1) |
| 785 | MSG(_("1 line indented ")); |
| 786 | else |
| 787 | smsg((char_u *)_("%ld lines indented "), i); |
| 788 | } |
| 789 | /* set '[ and '] marks */ |
| 790 | curbuf->b_op_start = oap->start; |
| 791 | curbuf->b_op_end = oap->end; |
| 792 | } |
| 793 | #endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */ |
| 794 | |
| 795 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 796 | /* |
| 797 | * Keep the last expression line here, for repeating. |
| 798 | */ |
| 799 | static char_u *expr_line = NULL; |
| 800 | |
| 801 | /* |
| 802 | * Get an expression for the "\"=expr1" or "CTRL-R =expr1" |
| 803 | * Returns '=' when OK, NUL otherwise. |
| 804 | */ |
| 805 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 806 | get_expr_register(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 807 | { |
| 808 | char_u *new_line; |
| 809 | |
| 810 | new_line = getcmdline('=', 0L, 0); |
| 811 | if (new_line == NULL) |
| 812 | return NUL; |
| 813 | if (*new_line == NUL) /* use previous line */ |
| 814 | vim_free(new_line); |
| 815 | else |
| 816 | set_expr_line(new_line); |
| 817 | return '='; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Set the expression for the '=' register. |
| 822 | * Argument must be an allocated string. |
| 823 | */ |
| 824 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 825 | set_expr_line(char_u *new_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 826 | { |
| 827 | vim_free(expr_line); |
| 828 | expr_line = new_line; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Get the result of the '=' register expression. |
| 833 | * Returns a pointer to allocated memory, or NULL for failure. |
| 834 | */ |
| 835 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 836 | get_expr_line(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 837 | { |
| 838 | char_u *expr_copy; |
| 839 | char_u *rv; |
Bram Moolenaar | 21bffa7 | 2006-10-06 21:33:16 +0000 | [diff] [blame] | 840 | static int nested = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 841 | |
| 842 | if (expr_line == NULL) |
| 843 | return NULL; |
| 844 | |
| 845 | /* Make a copy of the expression, because evaluating it may cause it to be |
| 846 | * changed. */ |
| 847 | expr_copy = vim_strsave(expr_line); |
| 848 | if (expr_copy == NULL) |
| 849 | return NULL; |
| 850 | |
Bram Moolenaar | 21bffa7 | 2006-10-06 21:33:16 +0000 | [diff] [blame] | 851 | /* When we are invoked recursively limit the evaluation to 10 levels. |
| 852 | * Then return the string as-is. */ |
| 853 | if (nested >= 10) |
| 854 | return expr_copy; |
| 855 | |
| 856 | ++nested; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 857 | rv = eval_to_string(expr_copy, NULL, TRUE); |
Bram Moolenaar | 21bffa7 | 2006-10-06 21:33:16 +0000 | [diff] [blame] | 858 | --nested; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 859 | vim_free(expr_copy); |
| 860 | return rv; |
| 861 | } |
Bram Moolenaar | de934d7 | 2005-05-22 22:09:40 +0000 | [diff] [blame] | 862 | |
| 863 | /* |
| 864 | * Get the '=' register expression itself, without evaluating it. |
| 865 | */ |
| 866 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 867 | get_expr_line_src(void) |
Bram Moolenaar | de934d7 | 2005-05-22 22:09:40 +0000 | [diff] [blame] | 868 | { |
| 869 | if (expr_line == NULL) |
| 870 | return NULL; |
| 871 | return vim_strsave(expr_line); |
| 872 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 873 | #endif /* FEAT_EVAL */ |
| 874 | |
| 875 | /* |
| 876 | * Check if 'regname' is a valid name of a yank register. |
| 877 | * Note: There is no check for 0 (default register), caller should do this |
| 878 | */ |
| 879 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 880 | valid_yank_reg( |
| 881 | int regname, |
| 882 | int writing) /* if TRUE check for writable registers */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | { |
| 884 | if ( (regname > 0 && ASCII_ISALNUM(regname)) |
| 885 | || (!writing && vim_strchr((char_u *) |
| 886 | #ifdef FEAT_EVAL |
Bram Moolenaar | 3b3a949 | 2015-01-27 18:44:16 +0100 | [diff] [blame] | 887 | "/.%:=" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 888 | #else |
Bram Moolenaar | 3b3a949 | 2015-01-27 18:44:16 +0100 | [diff] [blame] | 889 | "/.%:" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 890 | #endif |
| 891 | , regname) != NULL) |
Bram Moolenaar | 3b3a949 | 2015-01-27 18:44:16 +0100 | [diff] [blame] | 892 | || regname == '#' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 893 | || regname == '"' |
| 894 | || regname == '-' |
| 895 | || regname == '_' |
| 896 | #ifdef FEAT_CLIPBOARD |
| 897 | || regname == '*' |
| 898 | || regname == '+' |
| 899 | #endif |
| 900 | #ifdef FEAT_DND |
| 901 | || (!writing && regname == '~') |
| 902 | #endif |
| 903 | ) |
| 904 | return TRUE; |
| 905 | return FALSE; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Set y_current and y_append, according to the value of "regname". |
| 910 | * Cannot handle the '_' register. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 911 | * Must only be called with a valid register name! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 912 | * |
| 913 | * If regname is 0 and writing, use register 0 |
| 914 | * If regname is 0 and reading, use previous register |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 915 | * |
| 916 | * Return TRUE when the register should be inserted literally (selection or |
| 917 | * clipboard). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 918 | */ |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 919 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 920 | get_yank_register(int regname, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 921 | { |
| 922 | int i; |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 923 | int ret = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | |
| 925 | y_append = FALSE; |
| 926 | if ((regname == 0 || regname == '"') && !writing && y_previous != NULL) |
| 927 | { |
| 928 | y_current = y_previous; |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 929 | return ret; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 930 | } |
| 931 | i = regname; |
| 932 | if (VIM_ISDIGIT(i)) |
| 933 | i -= '0'; |
| 934 | else if (ASCII_ISLOWER(i)) |
| 935 | i = CharOrdLow(i) + 10; |
| 936 | else if (ASCII_ISUPPER(i)) |
| 937 | { |
| 938 | i = CharOrdUp(i) + 10; |
| 939 | y_append = TRUE; |
| 940 | } |
| 941 | else if (regname == '-') |
| 942 | i = DELETION_REGISTER; |
| 943 | #ifdef FEAT_CLIPBOARD |
| 944 | /* When selection is not available, use register 0 instead of '*' */ |
| 945 | else if (clip_star.available && regname == '*') |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 946 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 947 | i = STAR_REGISTER; |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 948 | ret = TRUE; |
| 949 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 950 | /* When clipboard is not available, use register 0 instead of '+' */ |
| 951 | else if (clip_plus.available && regname == '+') |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 952 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 953 | i = PLUS_REGISTER; |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 954 | ret = TRUE; |
| 955 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 956 | #endif |
| 957 | #ifdef FEAT_DND |
| 958 | else if (!writing && regname == '~') |
| 959 | i = TILDE_REGISTER; |
| 960 | #endif |
| 961 | else /* not 0-9, a-z, A-Z or '-': use register 0 */ |
| 962 | i = 0; |
| 963 | y_current = &(y_regs[i]); |
| 964 | if (writing) /* remember the register we write into for do_put() */ |
| 965 | y_previous = y_current; |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 966 | return ret; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 969 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 970 | /* |
| 971 | * When "regname" is a clipboard register, obtain the selection. If it's not |
| 972 | * available return zero, otherwise return "regname". |
| 973 | */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 974 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 975 | may_get_selection(int regname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 976 | { |
| 977 | if (regname == '*') |
| 978 | { |
| 979 | if (!clip_star.available) |
| 980 | regname = 0; |
| 981 | else |
| 982 | clip_get_selection(&clip_star); |
| 983 | } |
| 984 | else if (regname == '+') |
| 985 | { |
| 986 | if (!clip_plus.available) |
| 987 | regname = 0; |
| 988 | else |
| 989 | clip_get_selection(&clip_plus); |
| 990 | } |
| 991 | return regname; |
| 992 | } |
| 993 | #endif |
| 994 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 995 | /* |
| 996 | * Obtain the contents of a "normal" register. The register is made empty. |
| 997 | * The returned pointer has allocated memory, use put_register() later. |
| 998 | */ |
| 999 | void * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1000 | get_register( |
| 1001 | int name, |
| 1002 | int copy) /* make a copy, if FALSE make register empty. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1003 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 1004 | yankreg_T *reg; |
| 1005 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1006 | |
| 1007 | #ifdef FEAT_CLIPBOARD |
| 1008 | /* When Visual area changed, may have to update selection. Obtain the |
| 1009 | * selection too. */ |
Bram Moolenaar | cdfd3e4 | 2007-11-08 09:35:50 +0000 | [diff] [blame] | 1010 | if (name == '*' && clip_star.available) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1011 | { |
Bram Moolenaar | c0885aa | 2012-07-10 16:49:23 +0200 | [diff] [blame] | 1012 | if (clip_isautosel_star()) |
| 1013 | clip_update_selection(&clip_star); |
| 1014 | may_get_selection(name); |
| 1015 | } |
| 1016 | if (name == '+' && clip_plus.available) |
| 1017 | { |
| 1018 | if (clip_isautosel_plus()) |
| 1019 | clip_update_selection(&clip_plus); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1020 | may_get_selection(name); |
| 1021 | } |
| 1022 | #endif |
| 1023 | |
| 1024 | get_yank_register(name, 0); |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 1025 | reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1026 | if (reg != NULL) |
| 1027 | { |
| 1028 | *reg = *y_current; |
| 1029 | if (copy) |
| 1030 | { |
| 1031 | /* If we run out of memory some or all of the lines are empty. */ |
| 1032 | if (reg->y_size == 0) |
| 1033 | reg->y_array = NULL; |
| 1034 | else |
| 1035 | reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *) |
| 1036 | * reg->y_size)); |
| 1037 | if (reg->y_array != NULL) |
| 1038 | { |
| 1039 | for (i = 0; i < reg->y_size; ++i) |
| 1040 | reg->y_array[i] = vim_strsave(y_current->y_array[i]); |
| 1041 | } |
| 1042 | } |
| 1043 | else |
| 1044 | y_current->y_array = NULL; |
| 1045 | } |
| 1046 | return (void *)reg; |
| 1047 | } |
| 1048 | |
| 1049 | /* |
Bram Moolenaar | 0a30746 | 2007-12-01 20:13:05 +0000 | [diff] [blame] | 1050 | * Put "reg" into register "name". Free any previous contents and "reg". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1051 | */ |
| 1052 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1053 | put_register(int name, void *reg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1054 | { |
| 1055 | get_yank_register(name, 0); |
| 1056 | free_yank_all(); |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 1057 | *y_current = *(yankreg_T *)reg; |
Bram Moolenaar | 0a30746 | 2007-12-01 20:13:05 +0000 | [diff] [blame] | 1058 | vim_free(reg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1059 | |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1060 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1061 | /* Send text written to clipboard register to the clipboard. */ |
| 1062 | may_set_selection(); |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1063 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1064 | } |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1065 | |
| 1066 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1067 | free_register(void *reg) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1068 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 1069 | yankreg_T tmp; |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1070 | |
| 1071 | tmp = *y_current; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 1072 | *y_current = *(yankreg_T *)reg; |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1073 | free_yank_all(); |
| 1074 | vim_free(reg); |
| 1075 | *y_current = tmp; |
| 1076 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1077 | |
| 1078 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 1079 | /* |
| 1080 | * return TRUE if the current yank register has type MLINE |
| 1081 | */ |
| 1082 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1083 | yank_register_mline(int regname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1084 | { |
| 1085 | if (regname != 0 && !valid_yank_reg(regname, FALSE)) |
| 1086 | return FALSE; |
| 1087 | if (regname == '_') /* black hole is always empty */ |
| 1088 | return FALSE; |
| 1089 | get_yank_register(regname, FALSE); |
| 1090 | return (y_current->y_type == MLINE); |
| 1091 | } |
| 1092 | #endif |
| 1093 | |
| 1094 | /* |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1095 | * Start or stop recording into a yank register. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | * |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1097 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | */ |
| 1099 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1100 | do_record(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1101 | { |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1102 | char_u *p; |
| 1103 | static int regname; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 1104 | yankreg_T *old_y_previous, *old_y_current; |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1105 | int retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1106 | |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1107 | if (reg_recording == 0) /* start recording */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1108 | { |
Bram Moolenaar | 8c87a2b | 2018-04-10 13:15:47 +0200 | [diff] [blame] | 1109 | /* registers 0-9, a-z and " are allowed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1110 | if (c < 0 || (!ASCII_ISALNUM(c) && c != '"')) |
| 1111 | retval = FAIL; |
| 1112 | else |
| 1113 | { |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1114 | reg_recording = c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1115 | showmode(); |
| 1116 | regname = c; |
| 1117 | retval = OK; |
| 1118 | } |
| 1119 | } |
| 1120 | else /* stop recording */ |
| 1121 | { |
| 1122 | /* |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1123 | * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this |
| 1124 | * needs to be removed again to put it in a register. exec_reg then |
| 1125 | * adds the escaping back later. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1126 | */ |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1127 | reg_recording = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1128 | MSG(""); |
| 1129 | p = get_recorded(); |
| 1130 | if (p == NULL) |
| 1131 | retval = FAIL; |
| 1132 | else |
| 1133 | { |
Bram Moolenaar | 81b8587 | 2007-03-04 20:22:01 +0000 | [diff] [blame] | 1134 | /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */ |
| 1135 | vim_unescape_csi(p); |
| 1136 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1137 | /* |
| 1138 | * We don't want to change the default register here, so save and |
| 1139 | * restore the current register name. |
| 1140 | */ |
| 1141 | old_y_previous = y_previous; |
| 1142 | old_y_current = y_current; |
| 1143 | |
| 1144 | retval = stuff_yank(regname, p); |
| 1145 | |
| 1146 | y_previous = old_y_previous; |
| 1147 | y_current = old_y_current; |
| 1148 | } |
| 1149 | } |
| 1150 | return retval; |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Stuff string "p" into yank register "regname" as a single line (append if |
| 1155 | * uppercase). "p" must have been alloced. |
| 1156 | * |
| 1157 | * return FAIL for failure, OK otherwise |
| 1158 | */ |
| 1159 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1160 | stuff_yank(int regname, char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | { |
| 1162 | char_u *lp; |
| 1163 | char_u **pp; |
| 1164 | |
| 1165 | /* check for read-only register */ |
| 1166 | if (regname != 0 && !valid_yank_reg(regname, TRUE)) |
| 1167 | { |
| 1168 | vim_free(p); |
| 1169 | return FAIL; |
| 1170 | } |
| 1171 | if (regname == '_') /* black hole: don't do anything */ |
| 1172 | { |
| 1173 | vim_free(p); |
| 1174 | return OK; |
| 1175 | } |
| 1176 | get_yank_register(regname, TRUE); |
| 1177 | if (y_append && y_current->y_array != NULL) |
| 1178 | { |
| 1179 | pp = &(y_current->y_array[y_current->y_size - 1]); |
| 1180 | lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE); |
| 1181 | if (lp == NULL) |
| 1182 | { |
| 1183 | vim_free(p); |
| 1184 | return FAIL; |
| 1185 | } |
| 1186 | STRCPY(lp, *pp); |
| 1187 | STRCAT(lp, p); |
| 1188 | vim_free(p); |
| 1189 | vim_free(*pp); |
| 1190 | *pp = lp; |
| 1191 | } |
| 1192 | else |
| 1193 | { |
| 1194 | free_yank_all(); |
| 1195 | if ((y_current->y_array = |
| 1196 | (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL) |
| 1197 | { |
| 1198 | vim_free(p); |
| 1199 | return FAIL; |
| 1200 | } |
| 1201 | y_current->y_array[0] = p; |
| 1202 | y_current->y_size = 1; |
| 1203 | y_current->y_type = MCHAR; /* used to be MLINE, why? */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 1204 | #ifdef FEAT_VIMINFO |
| 1205 | y_current->y_time_set = vim_time(); |
| 1206 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1207 | } |
| 1208 | return OK; |
| 1209 | } |
| 1210 | |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 1211 | static int execreg_lastc = NUL; |
| 1212 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1213 | /* |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 1214 | * Execute a yank register: copy it into the stuff buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1215 | * |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 1216 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1217 | */ |
| 1218 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1219 | do_execreg( |
| 1220 | int regname, |
| 1221 | int colon, /* insert ':' before each line */ |
| 1222 | int addcr, /* always add '\n' to end of line */ |
| 1223 | int silent) /* set "silent" flag in typeahead buffer */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1224 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1225 | long i; |
| 1226 | char_u *p; |
| 1227 | int retval = OK; |
| 1228 | int remap; |
| 1229 | |
| 1230 | if (regname == '@') /* repeat previous one */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1231 | { |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 1232 | if (execreg_lastc == NUL) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1233 | { |
| 1234 | EMSG(_("E748: No previously used register")); |
| 1235 | return FAIL; |
| 1236 | } |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 1237 | regname = execreg_lastc; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1238 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1239 | /* check for valid regname */ |
| 1240 | if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE)) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1241 | { |
| 1242 | emsg_invreg(regname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1243 | return FAIL; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1244 | } |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 1245 | execreg_lastc = regname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1246 | |
| 1247 | #ifdef FEAT_CLIPBOARD |
| 1248 | regname = may_get_selection(regname); |
| 1249 | #endif |
| 1250 | |
| 1251 | if (regname == '_') /* black hole: don't stuff anything */ |
| 1252 | return OK; |
| 1253 | |
| 1254 | #ifdef FEAT_CMDHIST |
| 1255 | if (regname == ':') /* use last command line */ |
| 1256 | { |
| 1257 | if (last_cmdline == NULL) |
| 1258 | { |
| 1259 | EMSG(_(e_nolastcmd)); |
| 1260 | return FAIL; |
| 1261 | } |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1262 | VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */ |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 1263 | /* Escape all control characters with a CTRL-V */ |
| 1264 | p = vim_strsave_escaped_ext(last_cmdline, |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 1265 | (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE); |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 1266 | if (p != NULL) |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 1267 | { |
| 1268 | /* When in Visual mode "'<,'>" will be prepended to the command. |
| 1269 | * Remove it when it's already there. */ |
| 1270 | if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0) |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1271 | retval = put_in_typebuf(p + 5, TRUE, TRUE, silent); |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 1272 | else |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1273 | retval = put_in_typebuf(p, TRUE, TRUE, silent); |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 1274 | } |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 1275 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1276 | } |
| 1277 | #endif |
| 1278 | #ifdef FEAT_EVAL |
| 1279 | else if (regname == '=') |
| 1280 | { |
| 1281 | p = get_expr_line(); |
| 1282 | if (p == NULL) |
| 1283 | return FAIL; |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1284 | retval = put_in_typebuf(p, TRUE, colon, silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1285 | vim_free(p); |
| 1286 | } |
| 1287 | #endif |
| 1288 | else if (regname == '.') /* use last inserted text */ |
| 1289 | { |
| 1290 | p = get_last_insert_save(); |
| 1291 | if (p == NULL) |
| 1292 | { |
| 1293 | EMSG(_(e_noinstext)); |
| 1294 | return FAIL; |
| 1295 | } |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1296 | retval = put_in_typebuf(p, FALSE, colon, silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1297 | vim_free(p); |
| 1298 | } |
| 1299 | else |
| 1300 | { |
| 1301 | get_yank_register(regname, FALSE); |
| 1302 | if (y_current->y_array == NULL) |
| 1303 | return FAIL; |
| 1304 | |
| 1305 | /* Disallow remaping for ":@r". */ |
| 1306 | remap = colon ? REMAP_NONE : REMAP_YES; |
| 1307 | |
| 1308 | /* |
| 1309 | * Insert lines into typeahead buffer, from last one to first one. |
| 1310 | */ |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1311 | put_reedit_in_typebuf(silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1312 | for (i = y_current->y_size; --i >= 0; ) |
| 1313 | { |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1314 | char_u *escaped; |
| 1315 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1316 | /* insert NL between lines and after last line if type is MLINE */ |
| 1317 | if (y_current->y_type == MLINE || i < y_current->y_size - 1 |
| 1318 | || addcr) |
| 1319 | { |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1320 | if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | return FAIL; |
| 1322 | } |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1323 | escaped = vim_strsave_escape_csi(y_current->y_array[i]); |
| 1324 | if (escaped == NULL) |
| 1325 | return FAIL; |
| 1326 | retval = ins_typebuf(escaped, remap, 0, TRUE, silent); |
| 1327 | vim_free(escaped); |
| 1328 | if (retval == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1329 | return FAIL; |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1330 | if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1331 | == FAIL) |
| 1332 | return FAIL; |
| 1333 | } |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1334 | reg_executing = regname == 0 ? '"' : regname; // disable "q" command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1335 | } |
| 1336 | return retval; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's |
| 1341 | * used only after other typeahead has been processed. |
| 1342 | */ |
| 1343 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1344 | put_reedit_in_typebuf(int silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1345 | { |
| 1346 | char_u buf[3]; |
| 1347 | |
| 1348 | if (restart_edit != NUL) |
| 1349 | { |
| 1350 | if (restart_edit == 'V') |
| 1351 | { |
| 1352 | buf[0] = 'g'; |
| 1353 | buf[1] = 'R'; |
| 1354 | buf[2] = NUL; |
| 1355 | } |
| 1356 | else |
| 1357 | { |
| 1358 | buf[0] = restart_edit == 'I' ? 'i' : restart_edit; |
| 1359 | buf[1] = NUL; |
| 1360 | } |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1361 | if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1362 | restart_edit = NUL; |
| 1363 | } |
| 1364 | } |
| 1365 | |
Bram Moolenaar | 38ef43b | 2010-01-27 16:31:13 +0100 | [diff] [blame] | 1366 | /* |
| 1367 | * Insert register contents "s" into the typeahead buffer, so that it will be |
| 1368 | * executed again. |
| 1369 | * When "esc" is TRUE it is to be taken literally: Escape CSI characters and |
| 1370 | * no remapping. |
| 1371 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1372 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1373 | put_in_typebuf( |
| 1374 | char_u *s, |
| 1375 | int esc, |
| 1376 | int colon, /* add ':' before the line */ |
| 1377 | int silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1378 | { |
| 1379 | int retval = OK; |
| 1380 | |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1381 | put_reedit_in_typebuf(silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1382 | if (colon) |
Bram Moolenaar | 38ef43b | 2010-01-27 16:31:13 +0100 | [diff] [blame] | 1383 | retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1384 | if (retval == OK) |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1385 | { |
| 1386 | char_u *p; |
| 1387 | |
| 1388 | if (esc) |
| 1389 | p = vim_strsave_escape_csi(s); |
| 1390 | else |
| 1391 | p = s; |
| 1392 | if (p == NULL) |
| 1393 | retval = FAIL; |
| 1394 | else |
Bram Moolenaar | 38ef43b | 2010-01-27 16:31:13 +0100 | [diff] [blame] | 1395 | retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES, |
| 1396 | 0, TRUE, silent); |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1397 | if (esc) |
| 1398 | vim_free(p); |
| 1399 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1400 | if (colon && retval == OK) |
Bram Moolenaar | 38ef43b | 2010-01-27 16:31:13 +0100 | [diff] [blame] | 1401 | retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1402 | return retval; |
| 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * Insert a yank register: copy it into the Read buffer. |
| 1407 | * Used by CTRL-R command and middle mouse button in insert mode. |
| 1408 | * |
| 1409 | * return FAIL for failure, OK otherwise |
| 1410 | */ |
| 1411 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1412 | insert_reg( |
| 1413 | int regname, |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 1414 | int literally_arg) /* insert literally, not as if typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1415 | { |
| 1416 | long i; |
| 1417 | int retval = OK; |
| 1418 | char_u *arg; |
| 1419 | int allocated; |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 1420 | int literally = literally_arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1421 | |
| 1422 | /* |
| 1423 | * It is possible to get into an endless loop by having CTRL-R a in |
| 1424 | * register a and then, in insert mode, doing CTRL-R a. |
| 1425 | * If you hit CTRL-C, the loop will be broken here. |
| 1426 | */ |
| 1427 | ui_breakcheck(); |
| 1428 | if (got_int) |
| 1429 | return FAIL; |
| 1430 | |
| 1431 | /* check for valid regname */ |
| 1432 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
| 1433 | return FAIL; |
| 1434 | |
| 1435 | #ifdef FEAT_CLIPBOARD |
| 1436 | regname = may_get_selection(regname); |
| 1437 | #endif |
| 1438 | |
| 1439 | if (regname == '.') /* insert last inserted text */ |
| 1440 | retval = stuff_inserted(NUL, 1L, TRUE); |
| 1441 | else if (get_spec_reg(regname, &arg, &allocated, TRUE)) |
| 1442 | { |
| 1443 | if (arg == NULL) |
| 1444 | return FAIL; |
| 1445 | stuffescaped(arg, literally); |
| 1446 | if (allocated) |
| 1447 | vim_free(arg); |
| 1448 | } |
| 1449 | else /* name or number register */ |
| 1450 | { |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 1451 | if (get_yank_register(regname, FALSE)) |
| 1452 | literally = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1453 | if (y_current->y_array == NULL) |
| 1454 | retval = FAIL; |
| 1455 | else |
| 1456 | { |
| 1457 | for (i = 0; i < y_current->y_size; ++i) |
| 1458 | { |
| 1459 | stuffescaped(y_current->y_array[i], literally); |
| 1460 | /* |
| 1461 | * Insert a newline between lines and after last line if |
| 1462 | * y_type is MLINE. |
| 1463 | */ |
| 1464 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 1465 | stuffcharReadbuff('\n'); |
| 1466 | } |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | return retval; |
| 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | * Stuff a string into the typeahead buffer, such that edit() will insert it |
| 1475 | * literally ("literally" TRUE) or interpret is as typed characters. |
| 1476 | */ |
| 1477 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1478 | stuffescaped(char_u *arg, int literally) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1479 | { |
| 1480 | int c; |
| 1481 | char_u *start; |
| 1482 | |
| 1483 | while (*arg != NUL) |
| 1484 | { |
| 1485 | /* Stuff a sequence of normal ASCII characters, that's fast. Also |
| 1486 | * stuff K_SPECIAL to get the effect of a special key when "literally" |
| 1487 | * is TRUE. */ |
| 1488 | start = arg; |
| 1489 | while ((*arg >= ' ' |
| 1490 | #ifndef EBCDIC |
| 1491 | && *arg < DEL /* EBCDIC: chars above space are normal */ |
| 1492 | #endif |
| 1493 | ) |
| 1494 | || (*arg == K_SPECIAL && !literally)) |
| 1495 | ++arg; |
| 1496 | if (arg > start) |
| 1497 | stuffReadbuffLen(start, (long)(arg - start)); |
| 1498 | |
| 1499 | /* stuff a single special character */ |
| 1500 | if (*arg != NUL) |
| 1501 | { |
| 1502 | #ifdef FEAT_MBYTE |
| 1503 | if (has_mbyte) |
Bram Moolenaar | 3b1c485 | 2010-07-31 17:59:29 +0200 | [diff] [blame] | 1504 | c = mb_cptr2char_adv(&arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1505 | else |
| 1506 | #endif |
| 1507 | c = *arg++; |
| 1508 | if (literally && ((c < ' ' && c != TAB) || c == DEL)) |
| 1509 | stuffcharReadbuff(Ctrl_V); |
| 1510 | stuffcharReadbuff(c); |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | /* |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1516 | * If "regname" is a special register, return TRUE and store a pointer to its |
| 1517 | * value in "argp". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1518 | */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 1519 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1520 | get_spec_reg( |
| 1521 | int regname, |
| 1522 | char_u **argp, |
| 1523 | int *allocated, /* return: TRUE when value was allocated */ |
| 1524 | int errmsg) /* give error message when failing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1525 | { |
| 1526 | int cnt; |
| 1527 | |
| 1528 | *argp = NULL; |
| 1529 | *allocated = FALSE; |
| 1530 | switch (regname) |
| 1531 | { |
| 1532 | case '%': /* file name */ |
| 1533 | if (errmsg) |
| 1534 | check_fname(); /* will give emsg if not set */ |
| 1535 | *argp = curbuf->b_fname; |
| 1536 | return TRUE; |
| 1537 | |
| 1538 | case '#': /* alternate file name */ |
| 1539 | *argp = getaltfname(errmsg); /* may give emsg if not set */ |
| 1540 | return TRUE; |
| 1541 | |
| 1542 | #ifdef FEAT_EVAL |
| 1543 | case '=': /* result of expression */ |
| 1544 | *argp = get_expr_line(); |
| 1545 | *allocated = TRUE; |
| 1546 | return TRUE; |
| 1547 | #endif |
| 1548 | |
| 1549 | case ':': /* last command line */ |
| 1550 | if (last_cmdline == NULL && errmsg) |
| 1551 | EMSG(_(e_nolastcmd)); |
| 1552 | *argp = last_cmdline; |
| 1553 | return TRUE; |
| 1554 | |
| 1555 | case '/': /* last search-pattern */ |
| 1556 | if (last_search_pat() == NULL && errmsg) |
| 1557 | EMSG(_(e_noprevre)); |
| 1558 | *argp = last_search_pat(); |
| 1559 | return TRUE; |
| 1560 | |
| 1561 | case '.': /* last inserted text */ |
| 1562 | *argp = get_last_insert_save(); |
| 1563 | *allocated = TRUE; |
| 1564 | if (*argp == NULL && errmsg) |
| 1565 | EMSG(_(e_noinstext)); |
| 1566 | return TRUE; |
| 1567 | |
| 1568 | #ifdef FEAT_SEARCHPATH |
| 1569 | case Ctrl_F: /* Filename under cursor */ |
| 1570 | case Ctrl_P: /* Path under cursor, expand via "path" */ |
| 1571 | if (!errmsg) |
| 1572 | return FALSE; |
| 1573 | *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 1574 | | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1575 | *allocated = TRUE; |
| 1576 | return TRUE; |
| 1577 | #endif |
| 1578 | |
| 1579 | case Ctrl_W: /* word under cursor */ |
| 1580 | case Ctrl_A: /* WORD (mnemonic All) under cursor */ |
| 1581 | if (!errmsg) |
| 1582 | return FALSE; |
| 1583 | cnt = find_ident_under_cursor(argp, regname == Ctrl_W |
| 1584 | ? (FIND_IDENT|FIND_STRING) : FIND_STRING); |
| 1585 | *argp = cnt ? vim_strnsave(*argp, cnt) : NULL; |
| 1586 | *allocated = TRUE; |
| 1587 | return TRUE; |
| 1588 | |
Bram Moolenaar | e2c8d83 | 2018-05-01 19:24:03 +0200 | [diff] [blame] | 1589 | case Ctrl_L: /* Line under cursor */ |
| 1590 | if (!errmsg) |
| 1591 | return FALSE; |
| 1592 | |
| 1593 | *argp = ml_get_buf(curwin->w_buffer, |
| 1594 | curwin->w_cursor.lnum, FALSE); |
| 1595 | return TRUE; |
| 1596 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1597 | case '_': /* black hole: always empty */ |
| 1598 | *argp = (char_u *)""; |
| 1599 | return TRUE; |
| 1600 | } |
| 1601 | |
| 1602 | return FALSE; |
| 1603 | } |
| 1604 | |
| 1605 | /* |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 1606 | * Paste a yank register into the command line. |
| 1607 | * Only for non-special registers. |
| 1608 | * Used by CTRL-R command in command-line mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1609 | * insert_reg() can't be used here, because special characters from the |
| 1610 | * register contents will be interpreted as commands. |
| 1611 | * |
| 1612 | * return FAIL for failure, OK otherwise |
| 1613 | */ |
| 1614 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1615 | cmdline_paste_reg( |
| 1616 | int regname, |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 1617 | int literally_arg, /* Insert text literally instead of "as typed" */ |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1618 | int remcr) /* don't add CR characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1619 | { |
| 1620 | long i; |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 1621 | int literally = literally_arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1622 | |
Bram Moolenaar | 3324d0a | 2018-03-06 19:51:13 +0100 | [diff] [blame] | 1623 | if (get_yank_register(regname, FALSE)) |
| 1624 | literally = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1625 | if (y_current->y_array == NULL) |
| 1626 | return FAIL; |
| 1627 | |
| 1628 | for (i = 0; i < y_current->y_size; ++i) |
| 1629 | { |
| 1630 | cmdline_paste_str(y_current->y_array[i], literally); |
| 1631 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1632 | /* Insert ^M between lines and after last line if type is MLINE. |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 1633 | * Don't do this when "remcr" is TRUE. */ |
| 1634 | if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1635 | cmdline_paste_str((char_u *)"\r", literally); |
| 1636 | |
| 1637 | /* Check for CTRL-C, in case someone tries to paste a few thousand |
| 1638 | * lines and gets bored. */ |
| 1639 | ui_breakcheck(); |
| 1640 | if (got_int) |
| 1641 | return FAIL; |
| 1642 | } |
| 1643 | return OK; |
| 1644 | } |
| 1645 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1646 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 1647 | /* |
| 1648 | * Adjust the register name pointed to with "rp" for the clipboard being |
| 1649 | * used always and the clipboard being available. |
| 1650 | */ |
| 1651 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1652 | adjust_clip_reg(int *rp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1653 | { |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 1654 | /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard', |
| 1655 | * use '*' or '+' reg, respectively. "unnamedplus" prevails. */ |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 1656 | if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0)) |
| 1657 | { |
| 1658 | if (clip_unnamed != 0) |
| 1659 | *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available) |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 1660 | ? '+' : '*'; |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 1661 | else |
| 1662 | *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available) |
| 1663 | ? '+' : '*'; |
| 1664 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1665 | if (!clip_star.available && *rp == '*') |
| 1666 | *rp = 0; |
| 1667 | if (!clip_plus.available && *rp == '+') |
| 1668 | *rp = 0; |
| 1669 | } |
| 1670 | #endif |
| 1671 | |
| 1672 | /* |
Bram Moolenaar | a189184 | 2017-02-04 21:34:31 +0100 | [diff] [blame] | 1673 | * Shift the delete registers: "9 is cleared, "8 becomes "9, etc. |
| 1674 | */ |
| 1675 | void |
| 1676 | shift_delete_registers() |
| 1677 | { |
| 1678 | int n; |
| 1679 | |
| 1680 | y_current = &y_regs[9]; |
| 1681 | free_yank_all(); /* free register nine */ |
| 1682 | for (n = 9; n > 1; --n) |
| 1683 | y_regs[n] = y_regs[n - 1]; |
Bram Moolenaar | 18d90b9 | 2017-06-27 15:39:14 +0200 | [diff] [blame] | 1684 | y_current = &y_regs[1]; |
| 1685 | if (!y_append) |
| 1686 | y_previous = y_current; |
Bram Moolenaar | a189184 | 2017-02-04 21:34:31 +0100 | [diff] [blame] | 1687 | y_regs[1].y_array = NULL; /* set register one to empty */ |
| 1688 | } |
| 1689 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1690 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1691 | static void |
| 1692 | yank_do_autocmd(oparg_T *oap, yankreg_T *reg) |
| 1693 | { |
| 1694 | static int recursive = FALSE; |
| 1695 | dict_T *v_event; |
| 1696 | list_T *list; |
| 1697 | int n; |
| 1698 | char_u buf[NUMBUFLEN + 2]; |
| 1699 | long reglen = 0; |
| 1700 | |
| 1701 | if (recursive) |
| 1702 | return; |
| 1703 | |
| 1704 | v_event = get_vim_var_dict(VV_EVENT); |
| 1705 | |
| 1706 | list = list_alloc(); |
Bram Moolenaar | a0221df | 2018-02-11 15:07:22 +0100 | [diff] [blame] | 1707 | if (list == NULL) |
| 1708 | return; |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1709 | for (n = 0; n < reg->y_size; n++) |
| 1710 | list_append_string(list, reg->y_array[n], -1); |
| 1711 | list->lv_lock = VAR_FIXED; |
| 1712 | dict_add_list(v_event, "regcontents", list); |
| 1713 | |
| 1714 | buf[0] = (char_u)oap->regname; |
| 1715 | buf[1] = NUL; |
| 1716 | dict_add_nr_str(v_event, "regname", 0, buf); |
| 1717 | |
| 1718 | buf[0] = get_op_char(oap->op_type); |
| 1719 | buf[1] = get_extra_op_char(oap->op_type); |
| 1720 | buf[2] = NUL; |
| 1721 | dict_add_nr_str(v_event, "operator", 0, buf); |
| 1722 | |
| 1723 | buf[0] = NUL; |
| 1724 | buf[1] = NUL; |
| 1725 | switch (get_reg_type(oap->regname, ®len)) |
| 1726 | { |
| 1727 | case MLINE: buf[0] = 'V'; break; |
| 1728 | case MCHAR: buf[0] = 'v'; break; |
| 1729 | case MBLOCK: |
| 1730 | vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V, |
| 1731 | reglen + 1); |
| 1732 | break; |
| 1733 | } |
| 1734 | dict_add_nr_str(v_event, "regtype", 0, buf); |
| 1735 | |
| 1736 | /* Lock the dictionary and its keys */ |
| 1737 | dict_set_items_ro(v_event); |
| 1738 | |
| 1739 | recursive = TRUE; |
| 1740 | textlock++; |
| 1741 | apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf); |
| 1742 | textlock--; |
| 1743 | recursive = FALSE; |
| 1744 | |
| 1745 | /* Empty the dictionary, v:event is still valid */ |
| 1746 | dict_free_contents(v_event); |
| 1747 | hash_init(&v_event->dv_hashtab); |
| 1748 | } |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 1749 | #endif |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1750 | |
Bram Moolenaar | a189184 | 2017-02-04 21:34:31 +0100 | [diff] [blame] | 1751 | /* |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1752 | * Handle a delete operation. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1753 | * |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1754 | * Return FAIL if undo failed, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1755 | */ |
| 1756 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1757 | op_delete(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1758 | { |
| 1759 | int n; |
| 1760 | linenr_T lnum; |
| 1761 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1762 | char_u *newp, *oldp; |
| 1763 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1764 | linenr_T old_lcount = curbuf->b_ml.ml_line_count; |
| 1765 | int did_yank = FALSE; |
Bram Moolenaar | 7c82130 | 2012-09-05 14:18:45 +0200 | [diff] [blame] | 1766 | int orig_regname = oap->regname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1767 | |
| 1768 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */ |
| 1769 | return OK; |
| 1770 | |
| 1771 | /* Nothing to delete, return here. Do prepare undo, for op_change(). */ |
| 1772 | if (oap->empty) |
| 1773 | return u_save_cursor(); |
| 1774 | |
| 1775 | if (!curbuf->b_p_ma) |
| 1776 | { |
| 1777 | EMSG(_(e_modifiable)); |
| 1778 | return FAIL; |
| 1779 | } |
| 1780 | |
| 1781 | #ifdef FEAT_CLIPBOARD |
| 1782 | adjust_clip_reg(&oap->regname); |
| 1783 | #endif |
| 1784 | |
| 1785 | #ifdef FEAT_MBYTE |
| 1786 | if (has_mbyte) |
| 1787 | mb_adjust_opend(oap); |
| 1788 | #endif |
| 1789 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1790 | /* |
| 1791 | * Imitate the strange Vi behaviour: If the delete spans more than one |
| 1792 | * line and motion_type == MCHAR and the result is a blank line, make the |
| 1793 | * delete linewise. Don't do this for the change command or Visual mode. |
| 1794 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1795 | if ( oap->motion_type == MCHAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1796 | && !oap->is_VIsual |
Bram Moolenaar | ec2dad6 | 2005-01-02 11:36:03 +0000 | [diff] [blame] | 1797 | && !oap->block_mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1798 | && oap->line_count > 1 |
Bram Moolenaar | 64a7230 | 2012-01-10 13:46:22 +0100 | [diff] [blame] | 1799 | && oap->motion_force == NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1800 | && oap->op_type == OP_DELETE) |
| 1801 | { |
Bram Moolenaar | 44286ca | 2011-07-15 17:51:34 +0200 | [diff] [blame] | 1802 | ptr = ml_get(oap->end.lnum) + oap->end.col; |
| 1803 | if (*ptr != NUL) |
| 1804 | ptr += oap->inclusive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1805 | ptr = skipwhite(ptr); |
| 1806 | if (*ptr == NUL && inindent(0)) |
| 1807 | oap->motion_type = MLINE; |
| 1808 | } |
| 1809 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1810 | /* |
| 1811 | * Check for trying to delete (e.g. "D") in an empty line. |
| 1812 | * Note: For the change operator it is ok. |
| 1813 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1814 | if ( oap->motion_type == MCHAR |
| 1815 | && oap->line_count == 1 |
| 1816 | && oap->op_type == OP_DELETE |
| 1817 | && *ml_get(oap->start.lnum) == NUL) |
| 1818 | { |
| 1819 | /* |
Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 1820 | * It's an error to operate on an empty region, when 'E' included in |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1821 | * 'cpoptions' (Vi compatible). |
| 1822 | */ |
Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 1823 | #ifdef FEAT_VIRTUALEDIT |
| 1824 | if (virtual_op) |
| 1825 | /* Virtual editing: Nothing gets deleted, but we set the '[ and '] |
| 1826 | * marks as if it happened. */ |
| 1827 | goto setmarks; |
| 1828 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1829 | if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL) |
| 1830 | beep_flush(); |
| 1831 | return OK; |
| 1832 | } |
| 1833 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1834 | /* |
| 1835 | * Do a yank of whatever we're about to delete. |
| 1836 | * If a yank register was specified, put the deleted text into that |
| 1837 | * register. For the black hole register '_' don't yank anything. |
| 1838 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1839 | if (oap->regname != '_') |
| 1840 | { |
| 1841 | if (oap->regname != 0) |
| 1842 | { |
| 1843 | /* check for read-only register */ |
| 1844 | if (!valid_yank_reg(oap->regname, TRUE)) |
| 1845 | { |
| 1846 | beep_flush(); |
| 1847 | return OK; |
| 1848 | } |
| 1849 | get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */ |
| 1850 | if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */ |
| 1851 | did_yank = TRUE; |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * Put deleted text into register 1 and shift number registers if the |
| 1856 | * delete contains a line break, or when a regname has been specified. |
Bram Moolenaar | 7c82130 | 2012-09-05 14:18:45 +0200 | [diff] [blame] | 1857 | * Use the register name from before adjust_clip_reg() may have |
| 1858 | * changed it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1859 | */ |
Bram Moolenaar | 7c82130 | 2012-09-05 14:18:45 +0200 | [diff] [blame] | 1860 | if (orig_regname != 0 || oap->motion_type == MLINE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1861 | || oap->line_count > 1 || oap->use_reg_one) |
| 1862 | { |
Bram Moolenaar | a189184 | 2017-02-04 21:34:31 +0100 | [diff] [blame] | 1863 | shift_delete_registers(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1864 | if (op_yank(oap, TRUE, FALSE) == OK) |
| 1865 | did_yank = TRUE; |
| 1866 | } |
| 1867 | |
Bram Moolenaar | 84298db | 2012-04-20 13:46:08 +0200 | [diff] [blame] | 1868 | /* Yank into small delete register when no named register specified |
| 1869 | * and the delete is within one line. */ |
| 1870 | if (( |
| 1871 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1872 | ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') || |
| 1873 | ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') || |
Bram Moolenaar | 84298db | 2012-04-20 13:46:08 +0200 | [diff] [blame] | 1874 | #endif |
| 1875 | oap->regname == 0) && oap->motion_type != MLINE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1876 | && oap->line_count == 1) |
| 1877 | { |
| 1878 | oap->regname = '-'; |
| 1879 | get_yank_register(oap->regname, TRUE); |
| 1880 | if (op_yank(oap, TRUE, FALSE) == OK) |
| 1881 | did_yank = TRUE; |
| 1882 | oap->regname = 0; |
| 1883 | } |
| 1884 | |
| 1885 | /* |
| 1886 | * If there's too much stuff to fit in the yank register, then get a |
| 1887 | * confirmation before doing the delete. This is crude, but simple. |
| 1888 | * And it avoids doing a delete of something we can't put back if we |
| 1889 | * want. |
| 1890 | */ |
| 1891 | if (!did_yank) |
| 1892 | { |
| 1893 | int msg_silent_save = msg_silent; |
| 1894 | |
| 1895 | msg_silent = 0; /* must display the prompt */ |
| 1896 | n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE); |
| 1897 | msg_silent = msg_silent_save; |
| 1898 | if (n != 'y') |
| 1899 | { |
| 1900 | EMSG(_(e_abort)); |
| 1901 | return FAIL; |
| 1902 | } |
| 1903 | } |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1904 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1905 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1906 | if (did_yank && has_textyankpost()) |
| 1907 | yank_do_autocmd(oap, y_current); |
| 1908 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1911 | /* |
| 1912 | * block mode delete |
| 1913 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1914 | if (oap->block_mode) |
| 1915 | { |
| 1916 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 1917 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 1918 | return FAIL; |
| 1919 | |
| 1920 | for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum) |
| 1921 | { |
| 1922 | block_prep(oap, &bd, lnum, TRUE); |
| 1923 | if (bd.textlen == 0) /* nothing to delete */ |
| 1924 | continue; |
| 1925 | |
| 1926 | /* Adjust cursor position for tab replaced by spaces and 'lbr'. */ |
| 1927 | if (lnum == curwin->w_cursor.lnum) |
| 1928 | { |
| 1929 | curwin->w_cursor.col = bd.textcol + bd.startspaces; |
| 1930 | # ifdef FEAT_VIRTUALEDIT |
| 1931 | curwin->w_cursor.coladd = 0; |
| 1932 | # endif |
| 1933 | } |
| 1934 | |
| 1935 | /* n == number of chars deleted |
| 1936 | * If we delete a TAB, it may be replaced by several characters. |
| 1937 | * Thus the number of characters may increase! |
| 1938 | */ |
| 1939 | n = bd.textlen - bd.startspaces - bd.endspaces; |
| 1940 | oldp = ml_get(lnum); |
| 1941 | newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n); |
| 1942 | if (newp == NULL) |
| 1943 | continue; |
| 1944 | /* copy up to deleted part */ |
| 1945 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 1946 | /* insert spaces */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 1947 | vim_memset(newp + bd.textcol, ' ', |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1948 | (size_t)(bd.startspaces + bd.endspaces)); |
| 1949 | /* copy the part after the deleted part */ |
| 1950 | oldp += bd.textcol + bd.textlen; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 1951 | STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1952 | /* replace the line */ |
| 1953 | ml_replace(lnum, newp, FALSE); |
| 1954 | } |
| 1955 | |
| 1956 | check_cursor_col(); |
| 1957 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 1958 | oap->end.lnum + 1, 0L); |
| 1959 | oap->line_count = 0; /* no lines deleted */ |
| 1960 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1961 | else if (oap->motion_type == MLINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1962 | { |
| 1963 | if (oap->op_type == OP_CHANGE) |
| 1964 | { |
| 1965 | /* Delete the lines except the first one. Temporarily move the |
| 1966 | * cursor to the next line. Save the current line number, if the |
| 1967 | * last line is deleted it may be changed. |
| 1968 | */ |
| 1969 | if (oap->line_count > 1) |
| 1970 | { |
| 1971 | lnum = curwin->w_cursor.lnum; |
| 1972 | ++curwin->w_cursor.lnum; |
| 1973 | del_lines((long)(oap->line_count - 1), TRUE); |
| 1974 | curwin->w_cursor.lnum = lnum; |
| 1975 | } |
| 1976 | if (u_save_cursor() == FAIL) |
| 1977 | return FAIL; |
| 1978 | if (curbuf->b_p_ai) /* don't delete indent */ |
| 1979 | { |
| 1980 | beginline(BL_WHITE); /* cursor on first non-white */ |
| 1981 | did_ai = TRUE; /* delete the indent when ESC hit */ |
| 1982 | ai_col = curwin->w_cursor.col; |
| 1983 | } |
| 1984 | else |
| 1985 | beginline(0); /* cursor in column 0 */ |
| 1986 | truncate_line(FALSE); /* delete the rest of the line */ |
| 1987 | /* leave cursor past last char in line */ |
| 1988 | if (oap->line_count > 1) |
| 1989 | u_clearline(); /* "U" command not possible after "2cc" */ |
| 1990 | } |
| 1991 | else |
| 1992 | { |
| 1993 | del_lines(oap->line_count, TRUE); |
| 1994 | beginline(BL_WHITE | BL_FIX); |
| 1995 | u_clearline(); /* "U" command not possible after "dd" */ |
| 1996 | } |
| 1997 | } |
| 1998 | else |
| 1999 | { |
| 2000 | #ifdef FEAT_VIRTUALEDIT |
| 2001 | if (virtual_op) |
| 2002 | { |
| 2003 | int endcol = 0; |
| 2004 | |
| 2005 | /* For virtualedit: break the tabs that are partly included. */ |
| 2006 | if (gchar_pos(&oap->start) == '\t') |
| 2007 | { |
| 2008 | if (u_save_cursor() == FAIL) /* save first line for undo */ |
| 2009 | return FAIL; |
| 2010 | if (oap->line_count == 1) |
| 2011 | endcol = getviscol2(oap->end.col, oap->end.coladd); |
| 2012 | coladvance_force(getviscol2(oap->start.col, oap->start.coladd)); |
| 2013 | oap->start = curwin->w_cursor; |
| 2014 | if (oap->line_count == 1) |
| 2015 | { |
| 2016 | coladvance(endcol); |
| 2017 | oap->end.col = curwin->w_cursor.col; |
| 2018 | oap->end.coladd = curwin->w_cursor.coladd; |
| 2019 | curwin->w_cursor = oap->start; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | /* Break a tab only when it's included in the area. */ |
| 2024 | if (gchar_pos(&oap->end) == '\t' |
| 2025 | && (int)oap->end.coladd < oap->inclusive) |
| 2026 | { |
| 2027 | /* save last line for undo */ |
| 2028 | if (u_save((linenr_T)(oap->end.lnum - 1), |
| 2029 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 2030 | return FAIL; |
| 2031 | curwin->w_cursor = oap->end; |
| 2032 | coladvance_force(getviscol2(oap->end.col, oap->end.coladd)); |
| 2033 | oap->end = curwin->w_cursor; |
| 2034 | curwin->w_cursor = oap->start; |
| 2035 | } |
| 2036 | } |
| 2037 | #endif |
| 2038 | |
| 2039 | if (oap->line_count == 1) /* delete characters within one line */ |
| 2040 | { |
| 2041 | if (u_save_cursor() == FAIL) /* save line for undo */ |
| 2042 | return FAIL; |
| 2043 | |
| 2044 | /* if 'cpoptions' contains '$', display '$' at end of change */ |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 2045 | if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2046 | && oap->op_type == OP_CHANGE |
| 2047 | && oap->end.lnum == curwin->w_cursor.lnum |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 2048 | && !oap->is_VIsual) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2049 | display_dollar(oap->end.col - !oap->inclusive); |
| 2050 | |
| 2051 | n = oap->end.col - oap->start.col + 1 - !oap->inclusive; |
| 2052 | |
| 2053 | #ifdef FEAT_VIRTUALEDIT |
| 2054 | if (virtual_op) |
| 2055 | { |
| 2056 | /* fix up things for virtualedit-delete: |
| 2057 | * break the tabs which are going to get in our way |
| 2058 | */ |
| 2059 | char_u *curline = ml_get_curline(); |
| 2060 | int len = (int)STRLEN(curline); |
| 2061 | |
| 2062 | if (oap->end.coladd != 0 |
| 2063 | && (int)oap->end.col >= len - 1 |
| 2064 | && !(oap->start.coladd && (int)oap->end.col >= len - 1)) |
| 2065 | n++; |
| 2066 | /* Delete at least one char (e.g, when on a control char). */ |
| 2067 | if (n == 0 && oap->start.coladd != oap->end.coladd) |
| 2068 | n = 1; |
| 2069 | |
| 2070 | /* When deleted a char in the line, reset coladd. */ |
| 2071 | if (gchar_cursor() != NUL) |
| 2072 | curwin->w_cursor.coladd = 0; |
| 2073 | } |
| 2074 | #endif |
Bram Moolenaar | d009e86 | 2015-06-09 20:20:03 +0200 | [diff] [blame] | 2075 | (void)del_bytes((long)n, !virtual_op, |
| 2076 | oap->op_type == OP_DELETE && !oap->is_VIsual); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2077 | } |
| 2078 | else /* delete characters between lines */ |
| 2079 | { |
| 2080 | pos_T curpos; |
| 2081 | |
| 2082 | /* save deleted and changed lines for undo */ |
| 2083 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 2084 | (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL) |
| 2085 | return FAIL; |
| 2086 | |
| 2087 | truncate_line(TRUE); /* delete from cursor to end of line */ |
| 2088 | |
| 2089 | curpos = curwin->w_cursor; /* remember curwin->w_cursor */ |
| 2090 | ++curwin->w_cursor.lnum; |
| 2091 | del_lines((long)(oap->line_count - 2), FALSE); |
| 2092 | |
Bram Moolenaar | d009e86 | 2015-06-09 20:20:03 +0200 | [diff] [blame] | 2093 | /* delete from start of line until op_end */ |
Bram Moolenaar | 44286ca | 2011-07-15 17:51:34 +0200 | [diff] [blame] | 2094 | n = (oap->end.col + 1 - !oap->inclusive); |
Bram Moolenaar | d009e86 | 2015-06-09 20:20:03 +0200 | [diff] [blame] | 2095 | curwin->w_cursor.col = 0; |
| 2096 | (void)del_bytes((long)n, !virtual_op, |
| 2097 | oap->op_type == OP_DELETE && !oap->is_VIsual); |
| 2098 | curwin->w_cursor = curpos; /* restore curwin->w_cursor */ |
| 2099 | (void)do_join(2, FALSE, FALSE, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | msgmore(curbuf->b_ml.ml_line_count - old_lcount); |
| 2104 | |
Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 2105 | #ifdef FEAT_VIRTUALEDIT |
| 2106 | setmarks: |
| 2107 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2108 | if (oap->block_mode) |
| 2109 | { |
| 2110 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 2111 | curbuf->b_op_end.col = oap->start.col; |
| 2112 | } |
| 2113 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2114 | curbuf->b_op_end = oap->start; |
| 2115 | curbuf->b_op_start = oap->start; |
| 2116 | |
| 2117 | return OK; |
| 2118 | } |
| 2119 | |
| 2120 | #ifdef FEAT_MBYTE |
| 2121 | /* |
| 2122 | * Adjust end of operating area for ending on a multi-byte character. |
| 2123 | * Used for deletion. |
| 2124 | */ |
| 2125 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2126 | mb_adjust_opend(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2127 | { |
| 2128 | char_u *p; |
| 2129 | |
| 2130 | if (oap->inclusive) |
| 2131 | { |
| 2132 | p = ml_get(oap->end.lnum); |
| 2133 | oap->end.col += mb_tail_off(p, p + oap->end.col); |
| 2134 | } |
| 2135 | } |
| 2136 | #endif |
| 2137 | |
| 2138 | #if defined(FEAT_VISUALEXTRA) || defined(PROTO) |
| 2139 | /* |
| 2140 | * Replace a whole area with one character. |
| 2141 | */ |
| 2142 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2143 | op_replace(oparg_T *oap, int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2144 | { |
| 2145 | int n, numc; |
| 2146 | #ifdef FEAT_MBYTE |
| 2147 | int num_chars; |
| 2148 | #endif |
| 2149 | char_u *newp, *oldp; |
| 2150 | size_t oldlen; |
| 2151 | struct block_def bd; |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2152 | char_u *after_p = NULL; |
Bram Moolenaar | f12519d | 2018-02-06 22:52:49 +0100 | [diff] [blame] | 2153 | int had_ctrl_v_cr = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2154 | |
| 2155 | if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty) |
| 2156 | return OK; /* nothing to do */ |
| 2157 | |
Bram Moolenaar | f12519d | 2018-02-06 22:52:49 +0100 | [diff] [blame] | 2158 | if (c == REPLACE_CR_NCHAR) |
| 2159 | { |
| 2160 | had_ctrl_v_cr = TRUE; |
| 2161 | c = CAR; |
| 2162 | } |
| 2163 | else if (c == REPLACE_NL_NCHAR) |
| 2164 | { |
| 2165 | had_ctrl_v_cr = TRUE; |
| 2166 | c = NL; |
| 2167 | } |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2168 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2169 | #ifdef FEAT_MBYTE |
| 2170 | if (has_mbyte) |
| 2171 | mb_adjust_opend(oap); |
| 2172 | #endif |
| 2173 | |
| 2174 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 2175 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 2176 | return FAIL; |
| 2177 | |
| 2178 | /* |
| 2179 | * block mode replace |
| 2180 | */ |
| 2181 | if (oap->block_mode) |
| 2182 | { |
| 2183 | bd.is_MAX = (curwin->w_curswant == MAXCOL); |
| 2184 | for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum) |
| 2185 | { |
Bram Moolenaar | a1381de | 2009-11-03 15:44:21 +0000 | [diff] [blame] | 2186 | curwin->w_cursor.col = 0; /* make sure cursor position is valid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2187 | block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); |
| 2188 | if (bd.textlen == 0 && (!virtual_op || bd.is_MAX)) |
| 2189 | continue; /* nothing to replace */ |
| 2190 | |
| 2191 | /* n == number of extra chars required |
| 2192 | * If we split a TAB, it may be replaced by several characters. |
| 2193 | * Thus the number of characters may increase! |
| 2194 | */ |
| 2195 | #ifdef FEAT_VIRTUALEDIT |
| 2196 | /* If the range starts in virtual space, count the initial |
| 2197 | * coladd offset as part of "startspaces" */ |
| 2198 | if (virtual_op && bd.is_short && *bd.textstart == NUL) |
| 2199 | { |
| 2200 | pos_T vpos; |
| 2201 | |
Bram Moolenaar | a1381de | 2009-11-03 15:44:21 +0000 | [diff] [blame] | 2202 | vpos.lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2203 | getvpos(&vpos, oap->start_vcol); |
| 2204 | bd.startspaces += vpos.coladd; |
| 2205 | n = bd.startspaces; |
| 2206 | } |
| 2207 | else |
| 2208 | #endif |
| 2209 | /* allow for pre spaces */ |
| 2210 | n = (bd.startspaces ? bd.start_char_vcols - 1 : 0); |
| 2211 | |
| 2212 | /* allow for post spp */ |
| 2213 | n += (bd.endspaces |
| 2214 | #ifdef FEAT_VIRTUALEDIT |
| 2215 | && !bd.is_oneChar |
| 2216 | #endif |
| 2217 | && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0; |
| 2218 | /* Figure out how many characters to replace. */ |
| 2219 | numc = oap->end_vcol - oap->start_vcol + 1; |
| 2220 | if (bd.is_short && (!virtual_op || bd.is_MAX)) |
| 2221 | numc -= (oap->end_vcol - bd.end_vcol) + 1; |
| 2222 | |
| 2223 | #ifdef FEAT_MBYTE |
| 2224 | /* A double-wide character can be replaced only up to half the |
| 2225 | * times. */ |
| 2226 | if ((*mb_char2cells)(c) > 1) |
| 2227 | { |
| 2228 | if ((numc & 1) && !bd.is_short) |
| 2229 | { |
| 2230 | ++bd.endspaces; |
| 2231 | ++n; |
| 2232 | } |
| 2233 | numc = numc / 2; |
| 2234 | } |
| 2235 | |
| 2236 | /* Compute bytes needed, move character count to num_chars. */ |
| 2237 | num_chars = numc; |
| 2238 | numc *= (*mb_char2len)(c); |
| 2239 | #endif |
| 2240 | /* oldlen includes textlen, so don't double count */ |
| 2241 | n += numc - bd.textlen; |
| 2242 | |
| 2243 | oldp = ml_get_curline(); |
| 2244 | oldlen = STRLEN(oldp); |
| 2245 | newp = alloc_check((unsigned)oldlen + 1 + n); |
| 2246 | if (newp == NULL) |
| 2247 | continue; |
| 2248 | vim_memset(newp, NUL, (size_t)(oldlen + 1 + n)); |
| 2249 | /* copy up to deleted part */ |
| 2250 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 2251 | oldp += bd.textcol + bd.textlen; |
| 2252 | /* insert pre-spaces */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 2253 | vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2254 | /* insert replacement chars CHECK FOR ALLOCATED SPACE */ |
Bram Moolenaar | f12519d | 2018-02-06 22:52:49 +0100 | [diff] [blame] | 2255 | /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR |
| 2256 | * literally. */ |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2257 | if (had_ctrl_v_cr || (c != '\r' && c != '\n')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | { |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2259 | #ifdef FEAT_MBYTE |
| 2260 | if (has_mbyte) |
| 2261 | { |
| 2262 | n = (int)STRLEN(newp); |
| 2263 | while (--num_chars >= 0) |
| 2264 | n += (*mb_char2bytes)(c, newp + n); |
| 2265 | } |
| 2266 | else |
| 2267 | #endif |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 2268 | vim_memset(newp + STRLEN(newp), c, (size_t)numc); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2269 | if (!bd.is_short) |
| 2270 | { |
| 2271 | /* insert post-spaces */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 2272 | vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2273 | /* copy the part after the changed part */ |
| 2274 | STRMOVE(newp + STRLEN(newp), oldp); |
| 2275 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2276 | } |
| 2277 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2278 | { |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2279 | /* Replacing with \r or \n means splitting the line. */ |
Bram Moolenaar | efe06f4 | 2013-11-11 23:17:39 +0100 | [diff] [blame] | 2280 | after_p = alloc_check( |
| 2281 | (unsigned)(oldlen + 1 + n - STRLEN(newp))); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2282 | if (after_p != NULL) |
| 2283 | STRMOVE(after_p, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2284 | } |
| 2285 | /* replace the line */ |
| 2286 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2287 | if (after_p != NULL) |
| 2288 | { |
| 2289 | ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE); |
| 2290 | appended_lines_mark(curwin->w_cursor.lnum, 1L); |
| 2291 | oap->end.lnum++; |
| 2292 | vim_free(after_p); |
| 2293 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | else |
| 2297 | { |
| 2298 | /* |
| 2299 | * MCHAR and MLINE motion replace. |
| 2300 | */ |
| 2301 | if (oap->motion_type == MLINE) |
| 2302 | { |
| 2303 | oap->start.col = 0; |
| 2304 | curwin->w_cursor.col = 0; |
| 2305 | oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 2306 | if (oap->end.col) |
| 2307 | --oap->end.col; |
| 2308 | } |
| 2309 | else if (!oap->inclusive) |
| 2310 | dec(&(oap->end)); |
| 2311 | |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2312 | while (LTOREQ_POS(curwin->w_cursor, oap->end)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2313 | { |
| 2314 | n = gchar_cursor(); |
| 2315 | if (n != NUL) |
| 2316 | { |
| 2317 | #ifdef FEAT_MBYTE |
| 2318 | if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1) |
| 2319 | { |
| 2320 | /* This is slow, but it handles replacing a single-byte |
| 2321 | * with a multi-byte and the other way around. */ |
Bram Moolenaar | db81395 | 2013-03-07 18:50:57 +0100 | [diff] [blame] | 2322 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 2323 | oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2324 | n = State; |
| 2325 | State = REPLACE; |
| 2326 | ins_char(c); |
| 2327 | State = n; |
| 2328 | /* Backup to the replaced character. */ |
| 2329 | dec_cursor(); |
| 2330 | } |
| 2331 | else |
| 2332 | #endif |
| 2333 | { |
| 2334 | #ifdef FEAT_VIRTUALEDIT |
| 2335 | if (n == TAB) |
| 2336 | { |
| 2337 | int end_vcol = 0; |
| 2338 | |
| 2339 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 2340 | { |
| 2341 | /* oap->end has to be recalculated when |
| 2342 | * the tab breaks */ |
| 2343 | end_vcol = getviscol2(oap->end.col, |
| 2344 | oap->end.coladd); |
| 2345 | } |
| 2346 | coladvance_force(getviscol()); |
| 2347 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 2348 | getvpos(&oap->end, end_vcol); |
| 2349 | } |
| 2350 | #endif |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2351 | PCHAR(curwin->w_cursor, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2352 | } |
| 2353 | } |
| 2354 | #ifdef FEAT_VIRTUALEDIT |
| 2355 | else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum) |
| 2356 | { |
| 2357 | int virtcols = oap->end.coladd; |
| 2358 | |
| 2359 | if (curwin->w_cursor.lnum == oap->start.lnum |
| 2360 | && oap->start.col == oap->end.col && oap->start.coladd) |
| 2361 | virtcols -= oap->start.coladd; |
| 2362 | |
| 2363 | /* oap->end has been trimmed so it's effectively inclusive; |
| 2364 | * as a result an extra +1 must be counted so we don't |
| 2365 | * trample the NUL byte. */ |
| 2366 | coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1); |
| 2367 | curwin->w_cursor.col -= (virtcols + 1); |
| 2368 | for (; virtcols >= 0; virtcols--) |
| 2369 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2370 | PCHAR(curwin->w_cursor, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2371 | if (inc(&curwin->w_cursor) == -1) |
| 2372 | break; |
| 2373 | } |
| 2374 | } |
| 2375 | #endif |
| 2376 | |
| 2377 | /* Advance to next character, stop at the end of the file. */ |
| 2378 | if (inc_cursor() == -1) |
| 2379 | break; |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | curwin->w_cursor = oap->start; |
| 2384 | check_cursor(); |
| 2385 | changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L); |
| 2386 | |
| 2387 | /* Set "'[" and "']" marks. */ |
| 2388 | curbuf->b_op_start = oap->start; |
| 2389 | curbuf->b_op_end = oap->end; |
| 2390 | |
| 2391 | return OK; |
| 2392 | } |
| 2393 | #endif |
| 2394 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 2395 | static int swapchars(int op_type, pos_T *pos, int length); |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2396 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2397 | /* |
| 2398 | * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?". |
| 2399 | */ |
| 2400 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2401 | op_tilde(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2402 | { |
| 2403 | pos_T pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2404 | struct block_def bd; |
Bram Moolenaar | e60c58d | 2008-02-06 13:44:43 +0000 | [diff] [blame] | 2405 | int did_change = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2406 | |
| 2407 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 2408 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 2409 | return; |
| 2410 | |
| 2411 | pos = oap->start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2412 | if (oap->block_mode) /* Visual block mode */ |
| 2413 | { |
| 2414 | for (; pos.lnum <= oap->end.lnum; ++pos.lnum) |
| 2415 | { |
Bram Moolenaar | 555b3d5 | 2008-12-03 12:38:36 +0000 | [diff] [blame] | 2416 | int one_change; |
| 2417 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2418 | block_prep(oap, &bd, pos.lnum, FALSE); |
| 2419 | pos.col = bd.textcol; |
Bram Moolenaar | 555b3d5 | 2008-12-03 12:38:36 +0000 | [diff] [blame] | 2420 | one_change = swapchars(oap->op_type, &pos, bd.textlen); |
| 2421 | did_change |= one_change; |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2422 | |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 2423 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2424 | if (netbeans_active() && one_change) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2425 | { |
| 2426 | char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 2427 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2428 | netbeans_removed(curbuf, pos.lnum, bd.textcol, |
| 2429 | (long)bd.textlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2430 | netbeans_inserted(curbuf, pos.lnum, bd.textcol, |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2431 | &ptr[bd.textcol], bd.textlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2432 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 2433 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2434 | } |
| 2435 | if (did_change) |
| 2436 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
| 2437 | } |
| 2438 | else /* not block mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2439 | { |
| 2440 | if (oap->motion_type == MLINE) |
| 2441 | { |
| 2442 | oap->start.col = 0; |
| 2443 | pos.col = 0; |
| 2444 | oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 2445 | if (oap->end.col) |
| 2446 | --oap->end.col; |
| 2447 | } |
| 2448 | else if (!oap->inclusive) |
| 2449 | dec(&(oap->end)); |
| 2450 | |
Bram Moolenaar | e60c58d | 2008-02-06 13:44:43 +0000 | [diff] [blame] | 2451 | if (pos.lnum == oap->end.lnum) |
| 2452 | did_change = swapchars(oap->op_type, &pos, |
| 2453 | oap->end.col - pos.col + 1); |
| 2454 | else |
| 2455 | for (;;) |
| 2456 | { |
| 2457 | did_change |= swapchars(oap->op_type, &pos, |
| 2458 | pos.lnum == oap->end.lnum ? oap->end.col + 1: |
| 2459 | (int)STRLEN(ml_get_pos(&pos))); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2460 | if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1) |
Bram Moolenaar | e60c58d | 2008-02-06 13:44:43 +0000 | [diff] [blame] | 2461 | break; |
| 2462 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2463 | if (did_change) |
| 2464 | { |
| 2465 | changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, |
| 2466 | 0L); |
| 2467 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2468 | if (netbeans_active() && did_change) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2469 | { |
| 2470 | char_u *ptr; |
| 2471 | int count; |
| 2472 | |
| 2473 | pos = oap->start; |
| 2474 | while (pos.lnum < oap->end.lnum) |
| 2475 | { |
| 2476 | ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2477 | count = (int)STRLEN(ptr) - pos.col; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2478 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2479 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2480 | &ptr[pos.col], count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2481 | pos.col = 0; |
| 2482 | pos.lnum++; |
| 2483 | } |
| 2484 | ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 2485 | count = oap->end.col - pos.col + 1; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2486 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2487 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2488 | &ptr[pos.col], count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2489 | } |
| 2490 | #endif |
| 2491 | } |
| 2492 | } |
| 2493 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2494 | if (!did_change && oap->is_VIsual) |
| 2495 | /* No change: need to remove the Visual selection */ |
| 2496 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2497 | |
| 2498 | /* |
| 2499 | * Set '[ and '] marks. |
| 2500 | */ |
| 2501 | curbuf->b_op_start = oap->start; |
| 2502 | curbuf->b_op_end = oap->end; |
| 2503 | |
| 2504 | if (oap->line_count > p_report) |
| 2505 | { |
| 2506 | if (oap->line_count == 1) |
| 2507 | MSG(_("1 line changed")); |
| 2508 | else |
| 2509 | smsg((char_u *)_("%ld lines changed"), oap->line_count); |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | /* |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2514 | * Invoke swapchar() on "length" bytes at position "pos". |
| 2515 | * "pos" is advanced to just after the changed characters. |
| 2516 | * "length" is rounded up to include the whole last multi-byte character. |
| 2517 | * Also works correctly when the number of bytes changes. |
| 2518 | * Returns TRUE if some character was changed. |
| 2519 | */ |
| 2520 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2521 | swapchars(int op_type, pos_T *pos, int length) |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2522 | { |
| 2523 | int todo; |
| 2524 | int did_change = 0; |
| 2525 | |
| 2526 | for (todo = length; todo > 0; --todo) |
| 2527 | { |
| 2528 | # ifdef FEAT_MBYTE |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2529 | if (has_mbyte) |
Bram Moolenaar | f17968b | 2013-08-09 19:48:40 +0200 | [diff] [blame] | 2530 | { |
| 2531 | int len = (*mb_ptr2len)(ml_get_pos(pos)); |
| 2532 | |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2533 | /* we're counting bytes, not characters */ |
Bram Moolenaar | f17968b | 2013-08-09 19:48:40 +0200 | [diff] [blame] | 2534 | if (len > 0) |
| 2535 | todo -= len - 1; |
| 2536 | } |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2537 | # endif |
| 2538 | did_change |= swapchar(op_type, pos); |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2539 | if (inc(pos) == -1) /* at end of file */ |
| 2540 | break; |
| 2541 | } |
| 2542 | return did_change; |
| 2543 | } |
| 2544 | |
| 2545 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2546 | * If op_type == OP_UPPER: make uppercase, |
| 2547 | * if op_type == OP_LOWER: make lowercase, |
| 2548 | * if op_type == OP_ROT13: do rot13 encoding, |
| 2549 | * else swap case of character at 'pos' |
| 2550 | * returns TRUE when something actually changed. |
| 2551 | */ |
| 2552 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2553 | swapchar(int op_type, pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2554 | { |
| 2555 | int c; |
| 2556 | int nc; |
| 2557 | |
| 2558 | c = gchar_pos(pos); |
| 2559 | |
| 2560 | /* Only do rot13 encoding for ASCII characters. */ |
| 2561 | if (c >= 0x80 && op_type == OP_ROT13) |
| 2562 | return FALSE; |
| 2563 | |
| 2564 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2565 | if (op_type == OP_UPPER && c == 0xdf |
| 2566 | && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0)) |
Bram Moolenaar | 6f16eb8 | 2005-08-23 21:02:42 +0000 | [diff] [blame] | 2567 | { |
| 2568 | pos_T sp = curwin->w_cursor; |
| 2569 | |
| 2570 | /* Special handling of German sharp s: change to "SS". */ |
| 2571 | curwin->w_cursor = *pos; |
| 2572 | del_char(FALSE); |
| 2573 | ins_char('S'); |
| 2574 | ins_char('S'); |
| 2575 | curwin->w_cursor = sp; |
| 2576 | inc(pos); |
| 2577 | } |
| 2578 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2579 | if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */ |
| 2580 | return FALSE; |
| 2581 | #endif |
| 2582 | nc = c; |
| 2583 | if (MB_ISLOWER(c)) |
| 2584 | { |
| 2585 | if (op_type == OP_ROT13) |
| 2586 | nc = ROT13(c, 'a'); |
| 2587 | else if (op_type != OP_LOWER) |
| 2588 | nc = MB_TOUPPER(c); |
| 2589 | } |
| 2590 | else if (MB_ISUPPER(c)) |
| 2591 | { |
| 2592 | if (op_type == OP_ROT13) |
| 2593 | nc = ROT13(c, 'A'); |
| 2594 | else if (op_type != OP_UPPER) |
| 2595 | nc = MB_TOLOWER(c); |
| 2596 | } |
| 2597 | if (nc != c) |
| 2598 | { |
| 2599 | #ifdef FEAT_MBYTE |
| 2600 | if (enc_utf8 && (c >= 0x80 || nc >= 0x80)) |
| 2601 | { |
| 2602 | pos_T sp = curwin->w_cursor; |
| 2603 | |
| 2604 | curwin->w_cursor = *pos; |
Bram Moolenaar | d1cb65e | 2010-08-01 14:22:48 +0200 | [diff] [blame] | 2605 | /* don't use del_char(), it also removes composing chars */ |
| 2606 | del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2607 | ins_char(nc); |
| 2608 | curwin->w_cursor = sp; |
| 2609 | } |
| 2610 | else |
| 2611 | #endif |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2612 | PCHAR(*pos, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2613 | return TRUE; |
| 2614 | } |
| 2615 | return FALSE; |
| 2616 | } |
| 2617 | |
| 2618 | #if defined(FEAT_VISUALEXTRA) || defined(PROTO) |
| 2619 | /* |
| 2620 | * op_insert - Insert and append operators for Visual mode. |
| 2621 | */ |
| 2622 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2623 | op_insert(oparg_T *oap, long count1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2624 | { |
| 2625 | long ins_len, pre_textlen = 0; |
| 2626 | char_u *firstline, *ins_text; |
Bram Moolenaar | 2254a8a | 2017-09-03 14:03:43 +0200 | [diff] [blame] | 2627 | colnr_T ind_pre = 0, ind_post; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2628 | struct block_def bd; |
| 2629 | int i; |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2630 | pos_T t1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2631 | |
| 2632 | /* edit() changes this - record it for OP_APPEND */ |
| 2633 | bd.is_MAX = (curwin->w_curswant == MAXCOL); |
| 2634 | |
| 2635 | /* vis block is still marked. Get rid of it now. */ |
| 2636 | curwin->w_cursor.lnum = oap->start.lnum; |
| 2637 | update_screen(INVERTED); |
| 2638 | |
| 2639 | if (oap->block_mode) |
| 2640 | { |
| 2641 | #ifdef FEAT_VIRTUALEDIT |
| 2642 | /* When 'virtualedit' is used, need to insert the extra spaces before |
| 2643 | * doing block_prep(). When only "block" is used, virtual edit is |
| 2644 | * already disabled, but still need it when calling |
| 2645 | * coladvance_force(). */ |
| 2646 | if (curwin->w_cursor.coladd > 0) |
| 2647 | { |
| 2648 | int old_ve_flags = ve_flags; |
| 2649 | |
| 2650 | ve_flags = VE_ALL; |
| 2651 | if (u_save_cursor() == FAIL) |
| 2652 | return; |
| 2653 | coladvance_force(oap->op_type == OP_APPEND |
| 2654 | ? oap->end_vcol + 1 : getviscol()); |
| 2655 | if (oap->op_type == OP_APPEND) |
| 2656 | --curwin->w_cursor.col; |
| 2657 | ve_flags = old_ve_flags; |
| 2658 | } |
| 2659 | #endif |
| 2660 | /* Get the info about the block before entering the text */ |
| 2661 | block_prep(oap, &bd, oap->start.lnum, TRUE); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 2662 | /* Get indent information */ |
| 2663 | ind_pre = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2664 | firstline = ml_get(oap->start.lnum) + bd.textcol; |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 2665 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2666 | if (oap->op_type == OP_APPEND) |
| 2667 | firstline += bd.textlen; |
| 2668 | pre_textlen = (long)STRLEN(firstline); |
| 2669 | } |
| 2670 | |
| 2671 | if (oap->op_type == OP_APPEND) |
| 2672 | { |
| 2673 | if (oap->block_mode |
| 2674 | #ifdef FEAT_VIRTUALEDIT |
| 2675 | && curwin->w_cursor.coladd == 0 |
| 2676 | #endif |
| 2677 | ) |
| 2678 | { |
| 2679 | /* Move the cursor to the character right of the block. */ |
| 2680 | curwin->w_set_curswant = TRUE; |
| 2681 | while (*ml_get_cursor() != NUL |
| 2682 | && (curwin->w_cursor.col < bd.textcol + bd.textlen)) |
| 2683 | ++curwin->w_cursor.col; |
| 2684 | if (bd.is_short && !bd.is_MAX) |
| 2685 | { |
| 2686 | /* First line was too short, make it longer and adjust the |
| 2687 | * values in "bd". */ |
| 2688 | if (u_save_cursor() == FAIL) |
| 2689 | return; |
| 2690 | for (i = 0; i < bd.endspaces; ++i) |
| 2691 | ins_char(' '); |
| 2692 | bd.textlen += bd.endspaces; |
| 2693 | } |
| 2694 | } |
| 2695 | else |
| 2696 | { |
| 2697 | curwin->w_cursor = oap->end; |
Bram Moolenaar | 6a584dc | 2006-06-20 18:29:34 +0000 | [diff] [blame] | 2698 | check_cursor_col(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2699 | |
| 2700 | /* Works just like an 'i'nsert on the next character. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2701 | if (!LINEEMPTY(curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2702 | && oap->start_vcol != oap->end_vcol) |
| 2703 | inc_cursor(); |
| 2704 | } |
| 2705 | } |
| 2706 | |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2707 | t1 = oap->start; |
Bram Moolenaar | 23fa81d | 2017-02-01 21:50:21 +0100 | [diff] [blame] | 2708 | (void)edit(NUL, FALSE, (linenr_T)count1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2709 | |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2710 | /* When a tab was inserted, and the characters in front of the tab |
| 2711 | * have been converted to a tab as well, the column of the cursor |
| 2712 | * might have actually been reduced, so need to adjust here. */ |
| 2713 | if (t1.lnum == curbuf->b_op_start_orig.lnum |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2714 | && LT_POS(curbuf->b_op_start_orig, t1)) |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2715 | oap->start = curbuf->b_op_start_orig; |
| 2716 | |
Bram Moolenaar | aacbb00 | 2008-01-03 15:34:40 +0000 | [diff] [blame] | 2717 | /* If user has moved off this line, we don't know what to do, so do |
| 2718 | * nothing. |
| 2719 | * Also don't repeat the insert when Insert mode ended with CTRL-C. */ |
| 2720 | if (curwin->w_cursor.lnum != oap->start.lnum || got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | |
| 2723 | if (oap->block_mode) |
| 2724 | { |
| 2725 | struct block_def bd2; |
Bram Moolenaar | 8c87a2b | 2018-04-10 13:15:47 +0200 | [diff] [blame] | 2726 | int did_indent = FALSE; |
Bram Moolenaar | 35e802e | 2018-04-30 17:21:03 +0200 | [diff] [blame] | 2727 | size_t len; |
| 2728 | int add; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2729 | |
Bram Moolenaar | 4ec86dd | 2017-09-02 23:28:54 +0200 | [diff] [blame] | 2730 | /* If indent kicked in, the firstline might have changed |
| 2731 | * but only do that, if the indent actually increased. */ |
| 2732 | ind_post = (colnr_T)getwhitecols_curline(); |
| 2733 | if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre) |
| 2734 | { |
| 2735 | bd.textcol += ind_post - ind_pre; |
| 2736 | bd.start_vcol += ind_post - ind_pre; |
Bram Moolenaar | 8c87a2b | 2018-04-10 13:15:47 +0200 | [diff] [blame] | 2737 | did_indent = TRUE; |
Bram Moolenaar | 4ec86dd | 2017-09-02 23:28:54 +0200 | [diff] [blame] | 2738 | } |
| 2739 | |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2740 | /* The user may have moved the cursor before inserting something, try |
Bram Moolenaar | 8c87a2b | 2018-04-10 13:15:47 +0200 | [diff] [blame] | 2741 | * to adjust the block for that. But only do it, if the difference |
| 2742 | * does not come from indent kicking in. */ |
| 2743 | if (oap->start.lnum == curbuf->b_op_start_orig.lnum |
| 2744 | && !bd.is_MAX && !did_indent) |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2745 | { |
| 2746 | if (oap->op_type == OP_INSERT |
Bram Moolenaar | 4c9a949 | 2014-03-19 18:57:54 +0100 | [diff] [blame] | 2747 | && oap->start.col |
| 2748 | #ifdef FEAT_VIRTUALEDIT |
| 2749 | + oap->start.coladd |
| 2750 | #endif |
| 2751 | != curbuf->b_op_start_orig.col |
| 2752 | #ifdef FEAT_VIRTUALEDIT |
| 2753 | + curbuf->b_op_start_orig.coladd |
| 2754 | #endif |
| 2755 | ) |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2756 | { |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2757 | int t = getviscol2(curbuf->b_op_start_orig.col, |
| 2758 | curbuf->b_op_start_orig.coladd); |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 2759 | oap->start.col = curbuf->b_op_start_orig.col; |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2760 | pre_textlen -= t - oap->start_vcol; |
| 2761 | oap->start_vcol = t; |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2762 | } |
| 2763 | else if (oap->op_type == OP_APPEND |
Bram Moolenaar | 4c9a949 | 2014-03-19 18:57:54 +0100 | [diff] [blame] | 2764 | && oap->end.col |
| 2765 | #ifdef FEAT_VIRTUALEDIT |
| 2766 | + oap->end.coladd |
| 2767 | #endif |
| 2768 | >= curbuf->b_op_start_orig.col |
| 2769 | #ifdef FEAT_VIRTUALEDIT |
| 2770 | + curbuf->b_op_start_orig.coladd |
| 2771 | #endif |
| 2772 | ) |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2773 | { |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2774 | int t = getviscol2(curbuf->b_op_start_orig.col, |
| 2775 | curbuf->b_op_start_orig.coladd); |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 2776 | oap->start.col = curbuf->b_op_start_orig.col; |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2777 | /* reset pre_textlen to the value of OP_INSERT */ |
| 2778 | pre_textlen += bd.textlen; |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2779 | pre_textlen -= t - oap->start_vcol; |
| 2780 | oap->start_vcol = t; |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2781 | oap->op_type = OP_INSERT; |
| 2782 | } |
| 2783 | } |
| 2784 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2785 | /* |
| 2786 | * Spaces and tabs in the indent may have changed to other spaces and |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 2787 | * tabs. Get the starting column again and correct the length. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2788 | * Don't do this when "$" used, end-of-line will have changed. |
| 2789 | */ |
| 2790 | block_prep(oap, &bd2, oap->start.lnum, TRUE); |
| 2791 | if (!bd.is_MAX || bd2.textlen < bd.textlen) |
| 2792 | { |
| 2793 | if (oap->op_type == OP_APPEND) |
| 2794 | { |
| 2795 | pre_textlen += bd2.textlen - bd.textlen; |
| 2796 | if (bd2.endspaces) |
| 2797 | --bd2.textlen; |
| 2798 | } |
| 2799 | bd.textcol = bd2.textcol; |
| 2800 | bd.textlen = bd2.textlen; |
| 2801 | } |
| 2802 | |
| 2803 | /* |
| 2804 | * Subsequent calls to ml_get() flush the firstline data - take a |
| 2805 | * copy of the required string. |
| 2806 | */ |
Bram Moolenaar | 35e802e | 2018-04-30 17:21:03 +0200 | [diff] [blame] | 2807 | firstline = ml_get(oap->start.lnum); |
| 2808 | len = STRLEN(firstline); |
| 2809 | add = bd.textcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2810 | if (oap->op_type == OP_APPEND) |
Bram Moolenaar | 35e802e | 2018-04-30 17:21:03 +0200 | [diff] [blame] | 2811 | add += bd.textlen; |
| 2812 | if ((size_t)add > len) |
| 2813 | firstline += len; // short line, point to the NUL |
| 2814 | else |
| 2815 | firstline += add; |
Bram Moolenaar | 5e4b9e9 | 2012-03-23 14:16:23 +0100 | [diff] [blame] | 2816 | if (pre_textlen >= 0 |
| 2817 | && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2818 | { |
| 2819 | ins_text = vim_strnsave(firstline, (int)ins_len); |
| 2820 | if (ins_text != NULL) |
| 2821 | { |
| 2822 | /* block handled here */ |
| 2823 | if (u_save(oap->start.lnum, |
| 2824 | (linenr_T)(oap->end.lnum + 1)) == OK) |
| 2825 | block_insert(oap, ins_text, (oap->op_type == OP_INSERT), |
| 2826 | &bd); |
| 2827 | |
| 2828 | curwin->w_cursor.col = oap->start.col; |
| 2829 | check_cursor(); |
| 2830 | vim_free(ins_text); |
| 2831 | } |
| 2832 | } |
| 2833 | } |
| 2834 | } |
| 2835 | #endif |
| 2836 | |
| 2837 | /* |
| 2838 | * op_change - handle a change operation |
| 2839 | * |
| 2840 | * return TRUE if edit() returns because of a CTRL-O command |
| 2841 | */ |
| 2842 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2843 | op_change(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2844 | { |
| 2845 | colnr_T l; |
| 2846 | int retval; |
| 2847 | #ifdef FEAT_VISUALEXTRA |
| 2848 | long offset; |
| 2849 | linenr_T linenr; |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 2850 | long ins_len; |
| 2851 | long pre_textlen = 0; |
| 2852 | long pre_indent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2853 | char_u *firstline; |
| 2854 | char_u *ins_text, *newp, *oldp; |
| 2855 | struct block_def bd; |
| 2856 | #endif |
| 2857 | |
| 2858 | l = oap->start.col; |
| 2859 | if (oap->motion_type == MLINE) |
| 2860 | { |
| 2861 | l = 0; |
| 2862 | #ifdef FEAT_SMARTINDENT |
| 2863 | if (!p_paste && curbuf->b_p_si |
| 2864 | # ifdef FEAT_CINDENT |
| 2865 | && !curbuf->b_p_cin |
| 2866 | # endif |
| 2867 | ) |
| 2868 | can_si = TRUE; /* It's like opening a new line, do si */ |
| 2869 | #endif |
| 2870 | } |
| 2871 | |
| 2872 | /* First delete the text in the region. In an empty buffer only need to |
| 2873 | * save for undo */ |
| 2874 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 2875 | { |
| 2876 | if (u_save_cursor() == FAIL) |
| 2877 | return FALSE; |
| 2878 | } |
| 2879 | else if (op_delete(oap) == FAIL) |
| 2880 | return FALSE; |
| 2881 | |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2882 | if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2883 | && !virtual_op) |
| 2884 | inc_cursor(); |
| 2885 | |
| 2886 | #ifdef FEAT_VISUALEXTRA |
| 2887 | /* check for still on same line (<CR> in inserted text meaningless) */ |
| 2888 | /* skip blank lines too */ |
| 2889 | if (oap->block_mode) |
| 2890 | { |
| 2891 | # ifdef FEAT_VIRTUALEDIT |
| 2892 | /* Add spaces before getting the current line length. */ |
| 2893 | if (virtual_op && (curwin->w_cursor.coladd > 0 |
| 2894 | || gchar_cursor() == NUL)) |
| 2895 | coladvance_force(getviscol()); |
| 2896 | # endif |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 2897 | firstline = ml_get(oap->start.lnum); |
| 2898 | pre_textlen = (long)STRLEN(firstline); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 2899 | pre_indent = (long)getwhitecols(firstline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2900 | bd.textcol = curwin->w_cursor.col; |
| 2901 | } |
| 2902 | #endif |
| 2903 | |
| 2904 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
| 2905 | if (oap->motion_type == MLINE) |
| 2906 | fix_indent(); |
| 2907 | #endif |
| 2908 | |
| 2909 | retval = edit(NUL, FALSE, (linenr_T)1); |
| 2910 | |
| 2911 | #ifdef FEAT_VISUALEXTRA |
| 2912 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2913 | * In Visual block mode, handle copying the new text to all lines of the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2914 | * block. |
Bram Moolenaar | aacbb00 | 2008-01-03 15:34:40 +0000 | [diff] [blame] | 2915 | * Don't repeat the insert when Insert mode ended with CTRL-C. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2916 | */ |
Bram Moolenaar | aacbb00 | 2008-01-03 15:34:40 +0000 | [diff] [blame] | 2917 | if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2918 | { |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 2919 | /* Auto-indenting may have changed the indent. If the cursor was past |
| 2920 | * the indent, exclude that indent change from the inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2921 | firstline = ml_get(oap->start.lnum); |
Bram Moolenaar | b8dc4d4 | 2007-09-25 12:20:19 +0000 | [diff] [blame] | 2922 | if (bd.textcol > (colnr_T)pre_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2923 | { |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 2924 | long new_indent = (long)getwhitecols(firstline); |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 2925 | |
| 2926 | pre_textlen += new_indent - pre_indent; |
| 2927 | bd.textcol += new_indent - pre_indent; |
| 2928 | } |
| 2929 | |
| 2930 | ins_len = (long)STRLEN(firstline) - pre_textlen; |
| 2931 | if (ins_len > 0) |
| 2932 | { |
| 2933 | /* Subsequent calls to ml_get() flush the firstline data - take a |
| 2934 | * copy of the inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2935 | if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL) |
| 2936 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2937 | vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2938 | for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum; |
| 2939 | linenr++) |
| 2940 | { |
| 2941 | block_prep(oap, &bd, linenr, TRUE); |
| 2942 | if (!bd.is_short || virtual_op) |
| 2943 | { |
| 2944 | # ifdef FEAT_VIRTUALEDIT |
| 2945 | pos_T vpos; |
| 2946 | |
| 2947 | /* If the block starts in virtual space, count the |
| 2948 | * initial coladd offset as part of "startspaces" */ |
| 2949 | if (bd.is_short) |
| 2950 | { |
Bram Moolenaar | a1381de | 2009-11-03 15:44:21 +0000 | [diff] [blame] | 2951 | vpos.lnum = linenr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2952 | (void)getvpos(&vpos, oap->start_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2953 | } |
| 2954 | else |
| 2955 | vpos.coladd = 0; |
| 2956 | # endif |
| 2957 | oldp = ml_get(linenr); |
| 2958 | newp = alloc_check((unsigned)(STRLEN(oldp) |
| 2959 | # ifdef FEAT_VIRTUALEDIT |
| 2960 | + vpos.coladd |
| 2961 | # endif |
| 2962 | + ins_len + 1)); |
| 2963 | if (newp == NULL) |
| 2964 | continue; |
| 2965 | /* copy up to block start */ |
| 2966 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 2967 | offset = bd.textcol; |
| 2968 | # ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 2969 | vim_memset(newp + offset, ' ', (size_t)vpos.coladd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2970 | offset += vpos.coladd; |
| 2971 | # endif |
| 2972 | mch_memmove(newp + offset, ins_text, (size_t)ins_len); |
| 2973 | offset += ins_len; |
| 2974 | oldp += bd.textcol; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 2975 | STRMOVE(newp + offset, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2976 | ml_replace(linenr, newp, FALSE); |
| 2977 | } |
| 2978 | } |
| 2979 | check_cursor(); |
| 2980 | |
| 2981 | changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); |
| 2982 | } |
| 2983 | vim_free(ins_text); |
| 2984 | } |
| 2985 | } |
| 2986 | #endif |
| 2987 | |
| 2988 | return retval; |
| 2989 | } |
| 2990 | |
| 2991 | /* |
| 2992 | * set all the yank registers to empty (called from main()) |
| 2993 | */ |
| 2994 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2995 | init_yank(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2996 | { |
| 2997 | int i; |
| 2998 | |
| 2999 | for (i = 0; i < NUM_REGISTERS; ++i) |
| 3000 | y_regs[i].y_array = NULL; |
| 3001 | } |
| 3002 | |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 3003 | #if defined(EXITFREE) || defined(PROTO) |
| 3004 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3005 | clear_registers(void) |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 3006 | { |
| 3007 | int i; |
| 3008 | |
| 3009 | for (i = 0; i < NUM_REGISTERS; ++i) |
| 3010 | { |
| 3011 | y_current = &y_regs[i]; |
| 3012 | if (y_current->y_array != NULL) |
| 3013 | free_yank_all(); |
| 3014 | } |
| 3015 | } |
| 3016 | #endif |
| 3017 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3018 | /* |
| 3019 | * Free "n" lines from the current yank register. |
| 3020 | * Called for normal freeing and in case of error. |
| 3021 | */ |
| 3022 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3023 | free_yank(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3024 | { |
| 3025 | if (y_current->y_array != NULL) |
| 3026 | { |
| 3027 | long i; |
| 3028 | |
| 3029 | for (i = n; --i >= 0; ) |
| 3030 | { |
| 3031 | #ifdef AMIGA /* only for very slow machines */ |
| 3032 | if ((i & 1023) == 1023) /* this may take a while */ |
| 3033 | { |
| 3034 | /* |
| 3035 | * This message should never cause a hit-return message. |
| 3036 | * Overwrite this message with any next message. |
| 3037 | */ |
| 3038 | ++no_wait_return; |
| 3039 | smsg((char_u *)_("freeing %ld lines"), i + 1); |
| 3040 | --no_wait_return; |
| 3041 | msg_didout = FALSE; |
| 3042 | msg_col = 0; |
| 3043 | } |
| 3044 | #endif |
| 3045 | vim_free(y_current->y_array[i]); |
| 3046 | } |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3047 | VIM_CLEAR(y_current->y_array); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3048 | #ifdef AMIGA |
| 3049 | if (n >= 1000) |
| 3050 | MSG(""); |
| 3051 | #endif |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3056 | free_yank_all(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3057 | { |
| 3058 | free_yank(y_current->y_size); |
| 3059 | } |
| 3060 | |
| 3061 | /* |
| 3062 | * Yank the text between "oap->start" and "oap->end" into a yank register. |
| 3063 | * If we are to append (uppercase register), we first yank into a new yank |
| 3064 | * register and then concatenate the old and the new one (so we keep the old |
| 3065 | * one in case of out-of-memory). |
| 3066 | * |
Bram Moolenaar | 1a4a75c | 2013-07-28 16:03:06 +0200 | [diff] [blame] | 3067 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3068 | */ |
| 3069 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3070 | op_yank(oparg_T *oap, int deleting, int mess) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3071 | { |
| 3072 | long y_idx; /* index in y_array[] */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 3073 | yankreg_T *curr; /* copy of y_current */ |
| 3074 | yankreg_T newreg; /* new yank register when appending */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3075 | char_u **new_ptr; |
| 3076 | linenr_T lnum; /* current line number */ |
| 3077 | long j; |
| 3078 | int yanktype = oap->motion_type; |
| 3079 | long yanklines = oap->line_count; |
| 3080 | linenr_T yankendlnum = oap->end.lnum; |
| 3081 | char_u *p; |
| 3082 | char_u *pnew; |
| 3083 | struct block_def bd; |
Bram Moolenaar | 9c52c3a | 2010-12-08 14:23:15 +0100 | [diff] [blame] | 3084 | #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 3085 | int did_star = FALSE; |
Bram Moolenaar | 9c52c3a | 2010-12-08 14:23:15 +0100 | [diff] [blame] | 3086 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3087 | |
| 3088 | /* check for read-only register */ |
| 3089 | if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE)) |
| 3090 | { |
| 3091 | beep_flush(); |
| 3092 | return FAIL; |
| 3093 | } |
| 3094 | if (oap->regname == '_') /* black hole: nothing to do */ |
| 3095 | return OK; |
| 3096 | |
| 3097 | #ifdef FEAT_CLIPBOARD |
| 3098 | if (!clip_star.available && oap->regname == '*') |
| 3099 | oap->regname = 0; |
| 3100 | else if (!clip_plus.available && oap->regname == '+') |
| 3101 | oap->regname = 0; |
| 3102 | #endif |
| 3103 | |
| 3104 | if (!deleting) /* op_delete() already set y_current */ |
| 3105 | get_yank_register(oap->regname, TRUE); |
| 3106 | |
| 3107 | curr = y_current; |
| 3108 | /* append to existing contents */ |
| 3109 | if (y_append && y_current->y_array != NULL) |
| 3110 | y_current = &newreg; |
| 3111 | else |
| 3112 | free_yank_all(); /* free previously yanked lines */ |
| 3113 | |
Bram Moolenaar | 2c7a29c | 2005-12-12 22:02:31 +0000 | [diff] [blame] | 3114 | /* |
| 3115 | * If the cursor was in column 1 before and after the movement, and the |
| 3116 | * operator is not inclusive, the yank is always linewise. |
| 3117 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3118 | if ( oap->motion_type == MCHAR |
| 3119 | && oap->start.col == 0 |
| 3120 | && !oap->inclusive |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3121 | && (!oap->is_VIsual || *p_sel == 'o') |
Bram Moolenaar | ec2dad6 | 2005-01-02 11:36:03 +0000 | [diff] [blame] | 3122 | && !oap->block_mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3123 | && oap->end.col == 0 |
| 3124 | && yanklines > 1) |
| 3125 | { |
| 3126 | yanktype = MLINE; |
| 3127 | --yankendlnum; |
| 3128 | --yanklines; |
| 3129 | } |
| 3130 | |
| 3131 | y_current->y_size = yanklines; |
| 3132 | y_current->y_type = yanktype; /* set the yank register type */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3133 | y_current->y_width = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3134 | y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) * |
| 3135 | yanklines), TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3136 | if (y_current->y_array == NULL) |
| 3137 | { |
| 3138 | y_current = curr; |
| 3139 | return FAIL; |
| 3140 | } |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 3141 | #ifdef FEAT_VIMINFO |
| 3142 | y_current->y_time_set = vim_time(); |
| 3143 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3144 | |
| 3145 | y_idx = 0; |
| 3146 | lnum = oap->start.lnum; |
| 3147 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3148 | if (oap->block_mode) |
| 3149 | { |
| 3150 | /* Visual block mode */ |
| 3151 | y_current->y_type = MBLOCK; /* set the yank register type */ |
| 3152 | y_current->y_width = oap->end_vcol - oap->start_vcol; |
| 3153 | |
| 3154 | if (curwin->w_curswant == MAXCOL && y_current->y_width > 0) |
| 3155 | y_current->y_width--; |
| 3156 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3157 | |
| 3158 | for ( ; lnum <= yankendlnum; lnum++, y_idx++) |
| 3159 | { |
| 3160 | switch (y_current->y_type) |
| 3161 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3162 | case MBLOCK: |
| 3163 | block_prep(oap, &bd, lnum, FALSE); |
| 3164 | if (yank_copy_line(&bd, y_idx) == FAIL) |
| 3165 | goto fail; |
| 3166 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3167 | |
| 3168 | case MLINE: |
| 3169 | if ((y_current->y_array[y_idx] = |
| 3170 | vim_strsave(ml_get(lnum))) == NULL) |
| 3171 | goto fail; |
| 3172 | break; |
| 3173 | |
| 3174 | case MCHAR: |
| 3175 | { |
| 3176 | colnr_T startcol = 0, endcol = MAXCOL; |
| 3177 | #ifdef FEAT_VIRTUALEDIT |
| 3178 | int is_oneChar = FALSE; |
| 3179 | colnr_T cs, ce; |
| 3180 | #endif |
| 3181 | p = ml_get(lnum); |
| 3182 | bd.startspaces = 0; |
| 3183 | bd.endspaces = 0; |
| 3184 | |
| 3185 | if (lnum == oap->start.lnum) |
| 3186 | { |
| 3187 | startcol = oap->start.col; |
| 3188 | #ifdef FEAT_VIRTUALEDIT |
| 3189 | if (virtual_op) |
| 3190 | { |
| 3191 | getvcol(curwin, &oap->start, &cs, NULL, &ce); |
| 3192 | if (ce != cs && oap->start.coladd > 0) |
| 3193 | { |
| 3194 | /* Part of a tab selected -- but don't |
| 3195 | * double-count it. */ |
| 3196 | bd.startspaces = (ce - cs + 1) |
| 3197 | - oap->start.coladd; |
| 3198 | startcol++; |
| 3199 | } |
| 3200 | } |
| 3201 | #endif |
| 3202 | } |
| 3203 | |
| 3204 | if (lnum == oap->end.lnum) |
| 3205 | { |
| 3206 | endcol = oap->end.col; |
| 3207 | #ifdef FEAT_VIRTUALEDIT |
| 3208 | if (virtual_op) |
| 3209 | { |
| 3210 | getvcol(curwin, &oap->end, &cs, NULL, &ce); |
| 3211 | if (p[endcol] == NUL || (cs + oap->end.coladd < ce |
| 3212 | # ifdef FEAT_MBYTE |
| 3213 | /* Don't add space for double-wide |
| 3214 | * char; endcol will be on last byte |
| 3215 | * of multi-byte char. */ |
| 3216 | && (*mb_head_off)(p, p + endcol) == 0 |
| 3217 | # endif |
| 3218 | )) |
| 3219 | { |
| 3220 | if (oap->start.lnum == oap->end.lnum |
| 3221 | && oap->start.col == oap->end.col) |
| 3222 | { |
| 3223 | /* Special case: inside a single char */ |
| 3224 | is_oneChar = TRUE; |
| 3225 | bd.startspaces = oap->end.coladd |
| 3226 | - oap->start.coladd + oap->inclusive; |
| 3227 | endcol = startcol; |
| 3228 | } |
| 3229 | else |
| 3230 | { |
| 3231 | bd.endspaces = oap->end.coladd |
| 3232 | + oap->inclusive; |
| 3233 | endcol -= oap->inclusive; |
| 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | #endif |
| 3238 | } |
Bram Moolenaar | 18e00d2 | 2012-05-18 12:49:40 +0200 | [diff] [blame] | 3239 | if (endcol == MAXCOL) |
| 3240 | endcol = (colnr_T)STRLEN(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3241 | if (startcol > endcol |
| 3242 | #ifdef FEAT_VIRTUALEDIT |
| 3243 | || is_oneChar |
| 3244 | #endif |
| 3245 | ) |
| 3246 | bd.textlen = 0; |
| 3247 | else |
| 3248 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3249 | bd.textlen = endcol - startcol + oap->inclusive; |
| 3250 | } |
| 3251 | bd.textstart = p + startcol; |
| 3252 | if (yank_copy_line(&bd, y_idx) == FAIL) |
| 3253 | goto fail; |
| 3254 | break; |
| 3255 | } |
| 3256 | /* NOTREACHED */ |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | if (curr != y_current) /* append the new block to the old block */ |
| 3261 | { |
| 3262 | new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) * |
| 3263 | (curr->y_size + y_current->y_size)), TRUE); |
| 3264 | if (new_ptr == NULL) |
| 3265 | goto fail; |
| 3266 | for (j = 0; j < curr->y_size; ++j) |
| 3267 | new_ptr[j] = curr->y_array[j]; |
| 3268 | vim_free(curr->y_array); |
| 3269 | curr->y_array = new_ptr; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 3270 | #ifdef FEAT_VIMINFO |
| 3271 | curr->y_time_set = vim_time(); |
| 3272 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3273 | |
| 3274 | if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */ |
| 3275 | curr->y_type = MLINE; |
| 3276 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 3277 | /* Concatenate the last line of the old block with the first line of |
| 3278 | * the new block, unless being Vi compatible. */ |
| 3279 | if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3280 | { |
| 3281 | pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1]) |
| 3282 | + STRLEN(y_current->y_array[0]) + 1), TRUE); |
| 3283 | if (pnew == NULL) |
| 3284 | { |
| 3285 | y_idx = y_current->y_size - 1; |
| 3286 | goto fail; |
| 3287 | } |
| 3288 | STRCPY(pnew, curr->y_array[--j]); |
| 3289 | STRCAT(pnew, y_current->y_array[0]); |
| 3290 | vim_free(curr->y_array[j]); |
| 3291 | vim_free(y_current->y_array[0]); |
| 3292 | curr->y_array[j++] = pnew; |
| 3293 | y_idx = 1; |
| 3294 | } |
| 3295 | else |
| 3296 | y_idx = 0; |
| 3297 | while (y_idx < y_current->y_size) |
| 3298 | curr->y_array[j++] = y_current->y_array[y_idx++]; |
| 3299 | curr->y_size = j; |
| 3300 | vim_free(y_current->y_array); |
| 3301 | y_current = curr; |
| 3302 | } |
Bram Moolenaar | 7ec8343 | 2014-06-17 18:16:11 +0200 | [diff] [blame] | 3303 | if (curwin->w_p_rnu) |
| 3304 | redraw_later(SOME_VALID); /* cursor moved to start */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3305 | if (mess) /* Display message about yank? */ |
| 3306 | { |
| 3307 | if (yanktype == MCHAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3308 | && !oap->block_mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3309 | && yanklines == 1) |
| 3310 | yanklines = 0; |
| 3311 | /* Some versions of Vi use ">=" here, some don't... */ |
| 3312 | if (yanklines > p_report) |
| 3313 | { |
Bram Moolenaar | e45deb7 | 2017-07-16 17:56:16 +0200 | [diff] [blame] | 3314 | char namebuf[100]; |
| 3315 | |
| 3316 | if (oap->regname == NUL) |
| 3317 | *namebuf = NUL; |
| 3318 | else |
| 3319 | vim_snprintf(namebuf, sizeof(namebuf), |
Bram Moolenaar | 60d0e97 | 2017-07-16 20:54:34 +0200 | [diff] [blame] | 3320 | _(" into \"%c"), oap->regname); |
Bram Moolenaar | e45deb7 | 2017-07-16 17:56:16 +0200 | [diff] [blame] | 3321 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3322 | /* redisplay now, so message is not deleted */ |
| 3323 | update_topline_redraw(); |
| 3324 | if (yanklines == 1) |
Bram Moolenaar | e2cc970 | 2005-03-15 22:43:58 +0000 | [diff] [blame] | 3325 | { |
Bram Moolenaar | e2cc970 | 2005-03-15 22:43:58 +0000 | [diff] [blame] | 3326 | if (oap->block_mode) |
Bram Moolenaar | e45deb7 | 2017-07-16 17:56:16 +0200 | [diff] [blame] | 3327 | smsg((char_u *)_("block of 1 line yanked%s"), namebuf); |
Bram Moolenaar | e2cc970 | 2005-03-15 22:43:58 +0000 | [diff] [blame] | 3328 | else |
Bram Moolenaar | e45deb7 | 2017-07-16 17:56:16 +0200 | [diff] [blame] | 3329 | smsg((char_u *)_("1 line yanked%s"), namebuf); |
Bram Moolenaar | e2cc970 | 2005-03-15 22:43:58 +0000 | [diff] [blame] | 3330 | } |
Bram Moolenaar | e2cc970 | 2005-03-15 22:43:58 +0000 | [diff] [blame] | 3331 | else if (oap->block_mode) |
Bram Moolenaar | e45deb7 | 2017-07-16 17:56:16 +0200 | [diff] [blame] | 3332 | smsg((char_u *)_("block of %ld lines yanked%s"), |
| 3333 | yanklines, namebuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3334 | else |
Bram Moolenaar | e45deb7 | 2017-07-16 17:56:16 +0200 | [diff] [blame] | 3335 | smsg((char_u *)_("%ld lines yanked%s"), yanklines, |
| 3336 | namebuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | |
| 3340 | /* |
| 3341 | * Set "'[" and "']" marks. |
| 3342 | */ |
| 3343 | curbuf->b_op_start = oap->start; |
| 3344 | curbuf->b_op_end = oap->end; |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 3345 | if (yanktype == MLINE && !oap->block_mode) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 3346 | { |
| 3347 | curbuf->b_op_start.col = 0; |
| 3348 | curbuf->b_op_end.col = MAXCOL; |
| 3349 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3350 | |
| 3351 | #ifdef FEAT_CLIPBOARD |
| 3352 | /* |
| 3353 | * If we were yanking to the '*' register, send result to clipboard. |
| 3354 | * If no register was specified, and "unnamed" in 'clipboard', make a copy |
| 3355 | * to the '*' register. |
| 3356 | */ |
| 3357 | if (clip_star.available |
| 3358 | && (curr == &(y_regs[STAR_REGISTER]) |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 3359 | || (!deleting && oap->regname == 0 |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 3360 | && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3361 | { |
| 3362 | if (curr != &(y_regs[STAR_REGISTER])) |
| 3363 | /* Copy the text from register 0 to the clipboard register. */ |
| 3364 | copy_yank_reg(&(y_regs[STAR_REGISTER])); |
| 3365 | |
| 3366 | clip_own_selection(&clip_star); |
| 3367 | clip_gen_set_selection(&clip_star); |
Bram Moolenaar | 9c52c3a | 2010-12-08 14:23:15 +0100 | [diff] [blame] | 3368 | # ifdef FEAT_X11 |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 3369 | did_star = TRUE; |
Bram Moolenaar | 9c52c3a | 2010-12-08 14:23:15 +0100 | [diff] [blame] | 3370 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | # ifdef FEAT_X11 |
| 3374 | /* |
| 3375 | * If we were yanking to the '+' register, send result to selection. |
| 3376 | * Also copy to the '*' register, in case auto-select is off. |
| 3377 | */ |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 3378 | if (clip_plus.available |
| 3379 | && (curr == &(y_regs[PLUS_REGISTER]) |
| 3380 | || (!deleting && oap->regname == 0 |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 3381 | && ((clip_unnamed | clip_unnamed_saved) & |
| 3382 | CLIP_UNNAMED_PLUS)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3383 | { |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 3384 | if (curr != &(y_regs[PLUS_REGISTER])) |
| 3385 | /* Copy the text from register 0 to the clipboard register. */ |
| 3386 | copy_yank_reg(&(y_regs[PLUS_REGISTER])); |
| 3387 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3388 | clip_own_selection(&clip_plus); |
| 3389 | clip_gen_set_selection(&clip_plus); |
Bram Moolenaar | 8bfe07b | 2017-10-15 21:47:05 +0200 | [diff] [blame] | 3390 | if (!clip_isautosel_star() && !clip_isautosel_plus() |
| 3391 | && !did_star && curr == &(y_regs[PLUS_REGISTER])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3392 | { |
| 3393 | copy_yank_reg(&(y_regs[STAR_REGISTER])); |
| 3394 | clip_own_selection(&clip_star); |
| 3395 | clip_gen_set_selection(&clip_star); |
| 3396 | } |
| 3397 | } |
| 3398 | # endif |
| 3399 | #endif |
| 3400 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 3401 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 3402 | if (!deleting && has_textyankpost()) |
| 3403 | yank_do_autocmd(oap, y_current); |
| 3404 | #endif |
| 3405 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3406 | return OK; |
| 3407 | |
| 3408 | fail: /* free the allocated lines */ |
| 3409 | free_yank(y_idx + 1); |
| 3410 | y_current = curr; |
| 3411 | return FAIL; |
| 3412 | } |
| 3413 | |
| 3414 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3415 | yank_copy_line(struct block_def *bd, long y_idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3416 | { |
| 3417 | char_u *pnew; |
| 3418 | |
| 3419 | if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1)) |
| 3420 | == NULL) |
| 3421 | return FAIL; |
| 3422 | y_current->y_array[y_idx] = pnew; |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3423 | vim_memset(pnew, ' ', (size_t)bd->startspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3424 | pnew += bd->startspaces; |
| 3425 | mch_memmove(pnew, bd->textstart, (size_t)bd->textlen); |
| 3426 | pnew += bd->textlen; |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3427 | vim_memset(pnew, ' ', (size_t)bd->endspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3428 | pnew += bd->endspaces; |
| 3429 | *pnew = NUL; |
| 3430 | return OK; |
| 3431 | } |
| 3432 | |
| 3433 | #ifdef FEAT_CLIPBOARD |
| 3434 | /* |
| 3435 | * Make a copy of the y_current register to register "reg". |
| 3436 | */ |
| 3437 | static void |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 3438 | copy_yank_reg(yankreg_T *reg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3439 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 3440 | yankreg_T *curr = y_current; |
| 3441 | long j; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3442 | |
| 3443 | y_current = reg; |
| 3444 | free_yank_all(); |
| 3445 | *y_current = *curr; |
| 3446 | y_current->y_array = (char_u **)lalloc_clear( |
| 3447 | (long_u)(sizeof(char_u *) * y_current->y_size), TRUE); |
| 3448 | if (y_current->y_array == NULL) |
| 3449 | y_current->y_size = 0; |
| 3450 | else |
| 3451 | for (j = 0; j < y_current->y_size; ++j) |
| 3452 | if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL) |
| 3453 | { |
| 3454 | free_yank(j); |
| 3455 | y_current->y_size = 0; |
| 3456 | break; |
| 3457 | } |
| 3458 | y_current = curr; |
| 3459 | } |
| 3460 | #endif |
| 3461 | |
| 3462 | /* |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 3463 | * Put contents of register "regname" into the text. |
| 3464 | * Caller must check "regname" to be valid! |
| 3465 | * "flags": PUT_FIXINDENT make indent look nice |
| 3466 | * PUT_CURSEND leave cursor after end of new text |
| 3467 | * PUT_LINE force linewise put (":put") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3468 | */ |
| 3469 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3470 | do_put( |
| 3471 | int regname, |
| 3472 | int dir, /* BACKWARD for 'P', FORWARD for 'p' */ |
| 3473 | long count, |
| 3474 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3475 | { |
| 3476 | char_u *ptr; |
| 3477 | char_u *newp, *oldp; |
| 3478 | int yanklen; |
| 3479 | int totlen = 0; /* init for gcc */ |
| 3480 | linenr_T lnum; |
| 3481 | colnr_T col; |
| 3482 | long i; /* index in y_array[] */ |
| 3483 | int y_type; |
| 3484 | long y_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3485 | int oldlen; |
| 3486 | long y_width = 0; |
| 3487 | colnr_T vcol; |
| 3488 | int delcount; |
| 3489 | int incr = 0; |
| 3490 | long j; |
| 3491 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3492 | char_u **y_array = NULL; |
| 3493 | long nr_lines = 0; |
| 3494 | pos_T new_cursor; |
| 3495 | int indent; |
| 3496 | int orig_indent = 0; /* init for gcc */ |
| 3497 | int indent_diff = 0; /* init for gcc */ |
| 3498 | int first_indent = TRUE; |
| 3499 | int lendiff = 0; |
| 3500 | pos_T old_pos; |
| 3501 | char_u *insert_string = NULL; |
| 3502 | int allocated = FALSE; |
| 3503 | long cnt; |
| 3504 | |
| 3505 | #ifdef FEAT_CLIPBOARD |
| 3506 | /* Adjust register name for "unnamed" in 'clipboard'. */ |
| 3507 | adjust_clip_reg(®name); |
| 3508 | (void)may_get_selection(regname); |
| 3509 | #endif |
| 3510 | |
| 3511 | if (flags & PUT_FIXINDENT) |
| 3512 | orig_indent = get_indent(); |
| 3513 | |
| 3514 | curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ |
| 3515 | curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ |
| 3516 | |
| 3517 | /* |
| 3518 | * Using inserted text works differently, because the register includes |
| 3519 | * special characters (newlines, etc.). |
| 3520 | */ |
| 3521 | if (regname == '.') |
| 3522 | { |
Bram Moolenaar | f8eb9c5 | 2017-01-02 17:31:24 +0100 | [diff] [blame] | 3523 | if (VIsual_active) |
| 3524 | stuffcharReadbuff(VIsual_mode); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3525 | (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') : |
| 3526 | (count == -1 ? 'O' : 'i')), count, FALSE); |
| 3527 | /* Putting the text is done later, so can't really move the cursor to |
| 3528 | * the next character. Use "l" to simulate it. */ |
| 3529 | if ((flags & PUT_CURSEND) && gchar_cursor() != NUL) |
| 3530 | stuffcharReadbuff('l'); |
| 3531 | return; |
| 3532 | } |
| 3533 | |
| 3534 | /* |
| 3535 | * For special registers '%' (file name), '#' (alternate file name) and |
| 3536 | * ':' (last command line), etc. we have to create a fake yank register. |
| 3537 | */ |
| 3538 | if (get_spec_reg(regname, &insert_string, &allocated, TRUE)) |
| 3539 | { |
| 3540 | if (insert_string == NULL) |
| 3541 | return; |
| 3542 | } |
| 3543 | |
Bram Moolenaar | 27356ad | 2012-12-12 16:11:36 +0100 | [diff] [blame] | 3544 | /* Autocommands may be executed when saving lines for undo, which may make |
| 3545 | * y_array invalid. Start undo now to avoid that. */ |
| 3546 | u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1); |
Bram Moolenaar | 27356ad | 2012-12-12 16:11:36 +0100 | [diff] [blame] | 3547 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3548 | if (insert_string != NULL) |
| 3549 | { |
| 3550 | y_type = MCHAR; |
| 3551 | #ifdef FEAT_EVAL |
| 3552 | if (regname == '=') |
| 3553 | { |
| 3554 | /* For the = register we need to split the string at NL |
Bram Moolenaar | a554a19 | 2011-09-21 17:33:53 +0200 | [diff] [blame] | 3555 | * characters. |
| 3556 | * Loop twice: count the number of lines and save them. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3557 | for (;;) |
| 3558 | { |
| 3559 | y_size = 0; |
| 3560 | ptr = insert_string; |
| 3561 | while (ptr != NULL) |
| 3562 | { |
| 3563 | if (y_array != NULL) |
| 3564 | y_array[y_size] = ptr; |
| 3565 | ++y_size; |
| 3566 | ptr = vim_strchr(ptr, '\n'); |
| 3567 | if (ptr != NULL) |
| 3568 | { |
| 3569 | if (y_array != NULL) |
| 3570 | *ptr = NUL; |
| 3571 | ++ptr; |
Bram Moolenaar | a554a19 | 2011-09-21 17:33:53 +0200 | [diff] [blame] | 3572 | /* A trailing '\n' makes the register linewise. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3573 | if (*ptr == NUL) |
| 3574 | { |
| 3575 | y_type = MLINE; |
| 3576 | break; |
| 3577 | } |
| 3578 | } |
| 3579 | } |
| 3580 | if (y_array != NULL) |
| 3581 | break; |
| 3582 | y_array = (char_u **)alloc((unsigned) |
| 3583 | (y_size * sizeof(char_u *))); |
| 3584 | if (y_array == NULL) |
| 3585 | goto end; |
| 3586 | } |
| 3587 | } |
| 3588 | else |
| 3589 | #endif |
| 3590 | { |
| 3591 | y_size = 1; /* use fake one-line yank register */ |
| 3592 | y_array = &insert_string; |
| 3593 | } |
| 3594 | } |
| 3595 | else |
| 3596 | { |
| 3597 | get_yank_register(regname, FALSE); |
| 3598 | |
| 3599 | y_type = y_current->y_type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3600 | y_width = y_current->y_width; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3601 | y_size = y_current->y_size; |
| 3602 | y_array = y_current->y_array; |
| 3603 | } |
| 3604 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | if (y_type == MLINE) |
| 3606 | { |
| 3607 | if (flags & PUT_LINE_SPLIT) |
| 3608 | { |
Bram Moolenaar | c004bc2 | 2015-06-19 15:17:55 +0200 | [diff] [blame] | 3609 | char_u *p; |
| 3610 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3611 | /* "p" or "P" in Visual mode: split the lines to put the text in |
| 3612 | * between. */ |
| 3613 | if (u_save_cursor() == FAIL) |
| 3614 | goto end; |
Bram Moolenaar | c004bc2 | 2015-06-19 15:17:55 +0200 | [diff] [blame] | 3615 | p = ml_get_cursor(); |
| 3616 | if (dir == FORWARD && *p != NUL) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 3617 | MB_PTR_ADV(p); |
Bram Moolenaar | c004bc2 | 2015-06-19 15:17:55 +0200 | [diff] [blame] | 3618 | ptr = vim_strsave(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3619 | if (ptr == NULL) |
| 3620 | goto end; |
| 3621 | ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE); |
| 3622 | vim_free(ptr); |
| 3623 | |
Bram Moolenaar | c004bc2 | 2015-06-19 15:17:55 +0200 | [diff] [blame] | 3624 | oldp = ml_get_curline(); |
| 3625 | p = oldp + curwin->w_cursor.col; |
| 3626 | if (dir == FORWARD && *p != NUL) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 3627 | MB_PTR_ADV(p); |
Bram Moolenaar | c004bc2 | 2015-06-19 15:17:55 +0200 | [diff] [blame] | 3628 | ptr = vim_strnsave(oldp, p - oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3629 | if (ptr == NULL) |
| 3630 | goto end; |
| 3631 | ml_replace(curwin->w_cursor.lnum, ptr, FALSE); |
| 3632 | ++nr_lines; |
| 3633 | dir = FORWARD; |
| 3634 | } |
| 3635 | if (flags & PUT_LINE_FORWARD) |
| 3636 | { |
| 3637 | /* Must be "p" for a Visual block, put lines below the block. */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 3638 | curwin->w_cursor = curbuf->b_visual.vi_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3639 | dir = FORWARD; |
| 3640 | } |
| 3641 | curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ |
| 3642 | curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ |
| 3643 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3644 | |
| 3645 | if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */ |
| 3646 | y_type = MLINE; |
| 3647 | |
| 3648 | if (y_size == 0 || y_array == NULL) |
| 3649 | { |
| 3650 | EMSG2(_("E353: Nothing in register %s"), |
| 3651 | regname == 0 ? (char_u *)"\"" : transchar(regname)); |
| 3652 | goto end; |
| 3653 | } |
| 3654 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3655 | if (y_type == MBLOCK) |
| 3656 | { |
| 3657 | lnum = curwin->w_cursor.lnum + y_size + 1; |
| 3658 | if (lnum > curbuf->b_ml.ml_line_count) |
| 3659 | lnum = curbuf->b_ml.ml_line_count + 1; |
| 3660 | if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL) |
| 3661 | goto end; |
| 3662 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 3663 | else if (y_type == MLINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3664 | { |
| 3665 | lnum = curwin->w_cursor.lnum; |
| 3666 | #ifdef FEAT_FOLDING |
| 3667 | /* Correct line number for closed fold. Don't move the cursor yet, |
| 3668 | * u_save() uses it. */ |
| 3669 | if (dir == BACKWARD) |
| 3670 | (void)hasFolding(lnum, &lnum, NULL); |
| 3671 | else |
| 3672 | (void)hasFolding(lnum, NULL, &lnum); |
| 3673 | #endif |
| 3674 | if (dir == FORWARD) |
| 3675 | ++lnum; |
Bram Moolenaar | 10315b1 | 2013-06-29 17:19:28 +0200 | [diff] [blame] | 3676 | /* In an empty buffer the empty line is going to be replaced, include |
| 3677 | * it in the saved lines. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3678 | if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3679 | goto end; |
| 3680 | #ifdef FEAT_FOLDING |
| 3681 | if (dir == FORWARD) |
| 3682 | curwin->w_cursor.lnum = lnum - 1; |
| 3683 | else |
| 3684 | curwin->w_cursor.lnum = lnum; |
| 3685 | curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */ |
| 3686 | #endif |
| 3687 | } |
| 3688 | else if (u_save_cursor() == FAIL) |
| 3689 | goto end; |
| 3690 | |
| 3691 | yanklen = (int)STRLEN(y_array[0]); |
| 3692 | |
| 3693 | #ifdef FEAT_VIRTUALEDIT |
| 3694 | if (ve_flags == VE_ALL && y_type == MCHAR) |
| 3695 | { |
| 3696 | if (gchar_cursor() == TAB) |
| 3697 | { |
| 3698 | /* Don't need to insert spaces when "p" on the last position of a |
| 3699 | * tab or "P" on the first position. */ |
| 3700 | if (dir == FORWARD |
| 3701 | ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1 |
| 3702 | : curwin->w_cursor.coladd > 0) |
| 3703 | coladvance_force(getviscol()); |
| 3704 | else |
| 3705 | curwin->w_cursor.coladd = 0; |
| 3706 | } |
| 3707 | else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL) |
| 3708 | coladvance_force(getviscol() + (dir == FORWARD)); |
| 3709 | } |
| 3710 | #endif |
| 3711 | |
| 3712 | lnum = curwin->w_cursor.lnum; |
| 3713 | col = curwin->w_cursor.col; |
| 3714 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3715 | /* |
| 3716 | * Block mode |
| 3717 | */ |
| 3718 | if (y_type == MBLOCK) |
| 3719 | { |
Bram Moolenaar | c812996 | 2017-01-22 20:04:51 +0100 | [diff] [blame] | 3720 | int c = gchar_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3721 | colnr_T endcol2 = 0; |
| 3722 | |
| 3723 | if (dir == FORWARD && c != NUL) |
| 3724 | { |
| 3725 | #ifdef FEAT_VIRTUALEDIT |
| 3726 | if (ve_flags == VE_ALL) |
| 3727 | getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); |
| 3728 | else |
| 3729 | #endif |
| 3730 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); |
| 3731 | |
| 3732 | #ifdef FEAT_MBYTE |
| 3733 | if (has_mbyte) |
| 3734 | /* move to start of next multi-byte character */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3735 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3736 | else |
| 3737 | #endif |
| 3738 | #ifdef FEAT_VIRTUALEDIT |
| 3739 | if (c != TAB || ve_flags != VE_ALL) |
| 3740 | #endif |
| 3741 | ++curwin->w_cursor.col; |
| 3742 | ++col; |
| 3743 | } |
| 3744 | else |
| 3745 | getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); |
| 3746 | |
| 3747 | #ifdef FEAT_VIRTUALEDIT |
| 3748 | col += curwin->w_cursor.coladd; |
Bram Moolenaar | e649ef0 | 2007-06-28 20:18:51 +0000 | [diff] [blame] | 3749 | if (ve_flags == VE_ALL |
| 3750 | && (curwin->w_cursor.coladd > 0 |
| 3751 | || endcol2 == curwin->w_cursor.col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3752 | { |
| 3753 | if (dir == FORWARD && c == NUL) |
| 3754 | ++col; |
| 3755 | if (dir != FORWARD && c != NUL) |
| 3756 | ++curwin->w_cursor.col; |
| 3757 | if (c == TAB) |
| 3758 | { |
| 3759 | if (dir == BACKWARD && curwin->w_cursor.col) |
| 3760 | curwin->w_cursor.col--; |
| 3761 | if (dir == FORWARD && col - 1 == endcol2) |
| 3762 | curwin->w_cursor.col++; |
| 3763 | } |
| 3764 | } |
| 3765 | curwin->w_cursor.coladd = 0; |
| 3766 | #endif |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 3767 | bd.textcol = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3768 | for (i = 0; i < y_size; ++i) |
| 3769 | { |
| 3770 | int spaces; |
| 3771 | char shortline; |
| 3772 | |
| 3773 | bd.startspaces = 0; |
| 3774 | bd.endspaces = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3775 | vcol = 0; |
| 3776 | delcount = 0; |
| 3777 | |
| 3778 | /* add a new line */ |
| 3779 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 3780 | { |
| 3781 | if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"", |
| 3782 | (colnr_T)1, FALSE) == FAIL) |
| 3783 | break; |
| 3784 | ++nr_lines; |
| 3785 | } |
| 3786 | /* get the old line and advance to the position to insert at */ |
| 3787 | oldp = ml_get_curline(); |
| 3788 | oldlen = (int)STRLEN(oldp); |
| 3789 | for (ptr = oldp; vcol < col && *ptr; ) |
| 3790 | { |
| 3791 | /* Count a tab for what it's worth (if list mode not on) */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 3792 | incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3793 | vcol += incr; |
| 3794 | } |
| 3795 | bd.textcol = (colnr_T)(ptr - oldp); |
| 3796 | |
| 3797 | shortline = (vcol < col) || (vcol == col && !*ptr) ; |
| 3798 | |
| 3799 | if (vcol < col) /* line too short, padd with spaces */ |
| 3800 | bd.startspaces = col - vcol; |
| 3801 | else if (vcol > col) |
| 3802 | { |
| 3803 | bd.endspaces = vcol - col; |
| 3804 | bd.startspaces = incr - bd.endspaces; |
| 3805 | --bd.textcol; |
| 3806 | delcount = 1; |
| 3807 | #ifdef FEAT_MBYTE |
| 3808 | if (has_mbyte) |
| 3809 | bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol); |
| 3810 | #endif |
| 3811 | if (oldp[bd.textcol] != TAB) |
| 3812 | { |
| 3813 | /* Only a Tab can be split into spaces. Other |
| 3814 | * characters will have to be moved to after the |
| 3815 | * block, causing misalignment. */ |
| 3816 | delcount = 0; |
| 3817 | bd.endspaces = 0; |
| 3818 | } |
| 3819 | } |
| 3820 | |
| 3821 | yanklen = (int)STRLEN(y_array[i]); |
| 3822 | |
| 3823 | /* calculate number of spaces required to fill right side of block*/ |
| 3824 | spaces = y_width + 1; |
| 3825 | for (j = 0; j < yanklen; j++) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 3826 | spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3827 | if (spaces < 0) |
| 3828 | spaces = 0; |
| 3829 | |
| 3830 | /* insert the new text */ |
| 3831 | totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces; |
| 3832 | newp = alloc_check((unsigned)totlen + oldlen + 1); |
| 3833 | if (newp == NULL) |
| 3834 | break; |
| 3835 | /* copy part up to cursor to new line */ |
| 3836 | ptr = newp; |
| 3837 | mch_memmove(ptr, oldp, (size_t)bd.textcol); |
| 3838 | ptr += bd.textcol; |
| 3839 | /* may insert some spaces before the new text */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3840 | vim_memset(ptr, ' ', (size_t)bd.startspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3841 | ptr += bd.startspaces; |
| 3842 | /* insert the new text */ |
| 3843 | for (j = 0; j < count; ++j) |
| 3844 | { |
| 3845 | mch_memmove(ptr, y_array[i], (size_t)yanklen); |
| 3846 | ptr += yanklen; |
| 3847 | |
| 3848 | /* insert block's trailing spaces only if there's text behind */ |
| 3849 | if ((j < count - 1 || !shortline) && spaces) |
| 3850 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3851 | vim_memset(ptr, ' ', (size_t)spaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3852 | ptr += spaces; |
| 3853 | } |
| 3854 | } |
| 3855 | /* may insert some spaces after the new text */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3856 | vim_memset(ptr, ' ', (size_t)bd.endspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3857 | ptr += bd.endspaces; |
| 3858 | /* move the text after the cursor to the end of the line. */ |
| 3859 | mch_memmove(ptr, oldp + bd.textcol + delcount, |
| 3860 | (size_t)(oldlen - bd.textcol - delcount + 1)); |
| 3861 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 3862 | |
| 3863 | ++curwin->w_cursor.lnum; |
| 3864 | if (i == 0) |
| 3865 | curwin->w_cursor.col += bd.startspaces; |
| 3866 | } |
| 3867 | |
| 3868 | changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines); |
| 3869 | |
| 3870 | /* Set '[ mark. */ |
| 3871 | curbuf->b_op_start = curwin->w_cursor; |
| 3872 | curbuf->b_op_start.lnum = lnum; |
| 3873 | |
| 3874 | /* adjust '] mark */ |
| 3875 | curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1; |
| 3876 | curbuf->b_op_end.col = bd.textcol + totlen - 1; |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 3877 | # ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3878 | curbuf->b_op_end.coladd = 0; |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 3879 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3880 | if (flags & PUT_CURSEND) |
| 3881 | { |
Bram Moolenaar | 12dec75 | 2006-07-23 20:37:09 +0000 | [diff] [blame] | 3882 | colnr_T len; |
| 3883 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3884 | curwin->w_cursor = curbuf->b_op_end; |
| 3885 | curwin->w_cursor.col++; |
Bram Moolenaar | 12dec75 | 2006-07-23 20:37:09 +0000 | [diff] [blame] | 3886 | |
| 3887 | /* in Insert mode we might be after the NUL, correct for that */ |
| 3888 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 3889 | if (curwin->w_cursor.col > len) |
| 3890 | curwin->w_cursor.col = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3891 | } |
| 3892 | else |
| 3893 | curwin->w_cursor.lnum = lnum; |
| 3894 | } |
| 3895 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3896 | { |
| 3897 | /* |
| 3898 | * Character or Line mode |
| 3899 | */ |
| 3900 | if (y_type == MCHAR) |
| 3901 | { |
| 3902 | /* if type is MCHAR, FORWARD is the same as BACKWARD on the next |
| 3903 | * char */ |
| 3904 | if (dir == FORWARD && gchar_cursor() != NUL) |
| 3905 | { |
| 3906 | #ifdef FEAT_MBYTE |
| 3907 | if (has_mbyte) |
| 3908 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3909 | int bytelen = (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3910 | |
| 3911 | /* put it on the next of the multi-byte character. */ |
| 3912 | col += bytelen; |
| 3913 | if (yanklen) |
| 3914 | { |
| 3915 | curwin->w_cursor.col += bytelen; |
| 3916 | curbuf->b_op_end.col += bytelen; |
| 3917 | } |
| 3918 | } |
| 3919 | else |
| 3920 | #endif |
| 3921 | { |
| 3922 | ++col; |
| 3923 | if (yanklen) |
| 3924 | { |
| 3925 | ++curwin->w_cursor.col; |
| 3926 | ++curbuf->b_op_end.col; |
| 3927 | } |
| 3928 | } |
| 3929 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3930 | curbuf->b_op_start = curwin->w_cursor; |
| 3931 | } |
| 3932 | /* |
| 3933 | * Line mode: BACKWARD is the same as FORWARD on the previous line |
| 3934 | */ |
| 3935 | else if (dir == BACKWARD) |
| 3936 | --lnum; |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 3937 | new_cursor = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3938 | |
| 3939 | /* |
| 3940 | * simple case: insert into current line |
| 3941 | */ |
| 3942 | if (y_type == MCHAR && y_size == 1) |
| 3943 | { |
Bram Moolenaar | 6a717f1 | 2017-01-24 20:47:50 +0100 | [diff] [blame] | 3944 | linenr_T end_lnum = 0; /* init for gcc */ |
Bram Moolenaar | 9957a10 | 2017-01-23 21:53:53 +0100 | [diff] [blame] | 3945 | |
Bram Moolenaar | 941c12d | 2017-01-24 19:55:43 +0100 | [diff] [blame] | 3946 | if (VIsual_active) |
| 3947 | { |
Bram Moolenaar | 6a717f1 | 2017-01-24 20:47:50 +0100 | [diff] [blame] | 3948 | end_lnum = curbuf->b_visual.vi_end.lnum; |
| 3949 | if (end_lnum < curbuf->b_visual.vi_start.lnum) |
| 3950 | end_lnum = curbuf->b_visual.vi_start.lnum; |
Bram Moolenaar | 941c12d | 2017-01-24 19:55:43 +0100 | [diff] [blame] | 3951 | } |
Bram Moolenaar | 9957a10 | 2017-01-23 21:53:53 +0100 | [diff] [blame] | 3952 | |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3953 | do { |
| 3954 | totlen = count * yanklen; |
| 3955 | if (totlen > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3956 | { |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3957 | oldp = ml_get(lnum); |
Bram Moolenaar | 941c12d | 2017-01-24 19:55:43 +0100 | [diff] [blame] | 3958 | if (VIsual_active && col > (int)STRLEN(oldp)) |
| 3959 | { |
| 3960 | lnum++; |
| 3961 | continue; |
| 3962 | } |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3963 | newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1)); |
| 3964 | if (newp == NULL) |
| 3965 | goto end; /* alloc() gave an error message */ |
| 3966 | mch_memmove(newp, oldp, (size_t)col); |
| 3967 | ptr = newp + col; |
| 3968 | for (i = 0; i < count; ++i) |
| 3969 | { |
| 3970 | mch_memmove(ptr, y_array[0], (size_t)yanklen); |
| 3971 | ptr += yanklen; |
| 3972 | } |
| 3973 | STRMOVE(ptr, oldp + col); |
| 3974 | ml_replace(lnum, newp, FALSE); |
| 3975 | /* Place cursor on last putted char. */ |
| 3976 | if (lnum == curwin->w_cursor.lnum) |
Bram Moolenaar | 1e42f7a | 2013-11-21 13:24:41 +0100 | [diff] [blame] | 3977 | { |
| 3978 | /* make sure curwin->w_virtcol is updated */ |
| 3979 | changed_cline_bef_curs(); |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3980 | curwin->w_cursor.col += (colnr_T)(totlen - 1); |
Bram Moolenaar | 1e42f7a | 2013-11-21 13:24:41 +0100 | [diff] [blame] | 3981 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3982 | } |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3983 | if (VIsual_active) |
| 3984 | lnum++; |
Bram Moolenaar | 6a717f1 | 2017-01-24 20:47:50 +0100 | [diff] [blame] | 3985 | } while (VIsual_active && lnum <= end_lnum); |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3986 | |
Bram Moolenaar | 06e7ce1 | 2014-11-19 17:35:39 +0100 | [diff] [blame] | 3987 | if (VIsual_active) /* reset lnum to the last visual line */ |
| 3988 | lnum--; |
| 3989 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3990 | curbuf->b_op_end = curwin->w_cursor; |
| 3991 | /* For "CTRL-O p" in Insert mode, put cursor after last char */ |
| 3992 | if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND))) |
| 3993 | ++curwin->w_cursor.col; |
| 3994 | changed_bytes(lnum, col); |
| 3995 | } |
| 3996 | else |
| 3997 | { |
| 3998 | /* |
| 3999 | * Insert at least one line. When y_type is MCHAR, break the first |
| 4000 | * line in two. |
| 4001 | */ |
| 4002 | for (cnt = 1; cnt <= count; ++cnt) |
| 4003 | { |
| 4004 | i = 0; |
| 4005 | if (y_type == MCHAR) |
| 4006 | { |
| 4007 | /* |
| 4008 | * Split the current line in two at the insert position. |
| 4009 | * First insert y_array[size - 1] in front of second line. |
| 4010 | * Then append y_array[0] to first line. |
| 4011 | */ |
| 4012 | lnum = new_cursor.lnum; |
| 4013 | ptr = ml_get(lnum) + col; |
| 4014 | totlen = (int)STRLEN(y_array[y_size - 1]); |
| 4015 | newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1)); |
| 4016 | if (newp == NULL) |
| 4017 | goto error; |
| 4018 | STRCPY(newp, y_array[y_size - 1]); |
| 4019 | STRCAT(newp, ptr); |
| 4020 | /* insert second line */ |
| 4021 | ml_append(lnum, newp, (colnr_T)0, FALSE); |
| 4022 | vim_free(newp); |
| 4023 | |
| 4024 | oldp = ml_get(lnum); |
| 4025 | newp = alloc_check((unsigned)(col + yanklen + 1)); |
| 4026 | if (newp == NULL) |
| 4027 | goto error; |
| 4028 | /* copy first part of line */ |
| 4029 | mch_memmove(newp, oldp, (size_t)col); |
| 4030 | /* append to first line */ |
| 4031 | mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1)); |
| 4032 | ml_replace(lnum, newp, FALSE); |
| 4033 | |
| 4034 | curwin->w_cursor.lnum = lnum; |
| 4035 | i = 1; |
| 4036 | } |
| 4037 | |
| 4038 | for (; i < y_size; ++i) |
| 4039 | { |
| 4040 | if ((y_type != MCHAR || i < y_size - 1) |
| 4041 | && ml_append(lnum, y_array[i], (colnr_T)0, FALSE) |
| 4042 | == FAIL) |
| 4043 | goto error; |
| 4044 | lnum++; |
| 4045 | ++nr_lines; |
| 4046 | if (flags & PUT_FIXINDENT) |
| 4047 | { |
| 4048 | old_pos = curwin->w_cursor; |
| 4049 | curwin->w_cursor.lnum = lnum; |
| 4050 | ptr = ml_get(lnum); |
| 4051 | if (cnt == count && i == y_size - 1) |
| 4052 | lendiff = (int)STRLEN(ptr); |
| 4053 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) |
| 4054 | if (*ptr == '#' && preprocs_left()) |
| 4055 | indent = 0; /* Leave # lines at start */ |
| 4056 | else |
| 4057 | #endif |
| 4058 | if (*ptr == NUL) |
| 4059 | indent = 0; /* Ignore empty lines */ |
| 4060 | else if (first_indent) |
| 4061 | { |
| 4062 | indent_diff = orig_indent - get_indent(); |
| 4063 | indent = orig_indent; |
| 4064 | first_indent = FALSE; |
| 4065 | } |
| 4066 | else if ((indent = get_indent() + indent_diff) < 0) |
| 4067 | indent = 0; |
| 4068 | (void)set_indent(indent, 0); |
| 4069 | curwin->w_cursor = old_pos; |
| 4070 | /* remember how many chars were removed */ |
| 4071 | if (cnt == count && i == y_size - 1) |
| 4072 | lendiff -= (int)STRLEN(ml_get(lnum)); |
| 4073 | } |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | error: |
| 4078 | /* Adjust marks. */ |
| 4079 | if (y_type == MLINE) |
| 4080 | { |
| 4081 | curbuf->b_op_start.col = 0; |
| 4082 | if (dir == FORWARD) |
| 4083 | curbuf->b_op_start.lnum++; |
| 4084 | } |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 4085 | /* Skip mark_adjust when adding lines after the last one, there |
Bram Moolenaar | f58a847 | 2017-03-05 18:03:04 +0100 | [diff] [blame] | 4086 | * can't be marks there. But still needed in diff mode. */ |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 4087 | if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines |
Bram Moolenaar | f58a847 | 2017-03-05 18:03:04 +0100 | [diff] [blame] | 4088 | < curbuf->b_ml.ml_line_count |
| 4089 | #ifdef FEAT_DIFF |
| 4090 | || curwin->w_p_diff |
| 4091 | #endif |
| 4092 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 4093 | mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4094 | (linenr_T)MAXLNUM, nr_lines, 0L); |
| 4095 | |
| 4096 | /* note changed text for displaying and folding */ |
| 4097 | if (y_type == MCHAR) |
| 4098 | changed_lines(curwin->w_cursor.lnum, col, |
| 4099 | curwin->w_cursor.lnum + 1, nr_lines); |
| 4100 | else |
| 4101 | changed_lines(curbuf->b_op_start.lnum, 0, |
| 4102 | curbuf->b_op_start.lnum, nr_lines); |
| 4103 | |
| 4104 | /* put '] mark at last inserted character */ |
| 4105 | curbuf->b_op_end.lnum = lnum; |
| 4106 | /* correct length for change in indent */ |
| 4107 | col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff; |
| 4108 | if (col > 1) |
| 4109 | curbuf->b_op_end.col = col - 1; |
| 4110 | else |
| 4111 | curbuf->b_op_end.col = 0; |
| 4112 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4113 | if (flags & PUT_CURSLINE) |
| 4114 | { |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 4115 | /* ":put": put cursor on last inserted line */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4116 | curwin->w_cursor.lnum = lnum; |
| 4117 | beginline(BL_WHITE | BL_FIX); |
| 4118 | } |
| 4119 | else if (flags & PUT_CURSEND) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4120 | { |
| 4121 | /* put cursor after inserted text */ |
| 4122 | if (y_type == MLINE) |
| 4123 | { |
| 4124 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 4125 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 4126 | else |
| 4127 | curwin->w_cursor.lnum = lnum + 1; |
| 4128 | curwin->w_cursor.col = 0; |
| 4129 | } |
| 4130 | else |
| 4131 | { |
| 4132 | curwin->w_cursor.lnum = lnum; |
| 4133 | curwin->w_cursor.col = col; |
| 4134 | } |
| 4135 | } |
| 4136 | else if (y_type == MLINE) |
| 4137 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4138 | /* put cursor on first non-blank in first inserted line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4139 | curwin->w_cursor.col = 0; |
| 4140 | if (dir == FORWARD) |
| 4141 | ++curwin->w_cursor.lnum; |
| 4142 | beginline(BL_WHITE | BL_FIX); |
| 4143 | } |
| 4144 | else /* put cursor on first inserted character */ |
| 4145 | curwin->w_cursor = new_cursor; |
| 4146 | } |
| 4147 | } |
| 4148 | |
| 4149 | msgmore(nr_lines); |
| 4150 | curwin->w_set_curswant = TRUE; |
| 4151 | |
| 4152 | end: |
| 4153 | if (allocated) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4154 | vim_free(insert_string); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 4155 | if (regname == '=') |
| 4156 | vim_free(y_array); |
| 4157 | |
Bram Moolenaar | 033d888 | 2013-09-25 23:24:57 +0200 | [diff] [blame] | 4158 | VIsual_active = FALSE; |
Bram Moolenaar | 033d888 | 2013-09-25 23:24:57 +0200 | [diff] [blame] | 4159 | |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 4160 | /* If the cursor is past the end of the line put it at the end. */ |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4161 | adjust_cursor_eol(); |
| 4162 | } |
| 4163 | |
| 4164 | /* |
| 4165 | * When the cursor is on the NUL past the end of the line and it should not be |
| 4166 | * there move it left. |
| 4167 | */ |
| 4168 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4169 | adjust_cursor_eol(void) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4170 | { |
| 4171 | if (curwin->w_cursor.col > 0 |
| 4172 | && gchar_cursor() == NUL |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 4173 | #ifdef FEAT_VIRTUALEDIT |
| 4174 | && (ve_flags & VE_ONEMORE) == 0 |
| 4175 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4176 | && !(restart_edit || (State & INSERT))) |
| 4177 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4178 | /* Put the cursor on the last character in the line. */ |
Bram Moolenaar | a5fac54 | 2005-10-12 20:58:49 +0000 | [diff] [blame] | 4179 | dec_cursor(); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4180 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4181 | #ifdef FEAT_VIRTUALEDIT |
| 4182 | if (ve_flags == VE_ALL) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4183 | { |
| 4184 | colnr_T scol, ecol; |
| 4185 | |
| 4186 | /* Coladd is set to the width of the last character. */ |
| 4187 | getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol); |
| 4188 | curwin->w_cursor.coladd = ecol - scol + 1; |
| 4189 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4190 | #endif |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO) |
| 4195 | /* |
| 4196 | * Return TRUE if lines starting with '#' should be left aligned. |
| 4197 | */ |
| 4198 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4199 | preprocs_left(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4200 | { |
| 4201 | return |
| 4202 | # ifdef FEAT_SMARTINDENT |
| 4203 | # ifdef FEAT_CINDENT |
| 4204 | (curbuf->b_p_si && !curbuf->b_p_cin) || |
| 4205 | # else |
| 4206 | curbuf->b_p_si |
| 4207 | # endif |
| 4208 | # endif |
| 4209 | # ifdef FEAT_CINDENT |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4210 | (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE) |
| 4211 | && curbuf->b_ind_hash_comment == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4212 | # endif |
| 4213 | ; |
| 4214 | } |
| 4215 | #endif |
| 4216 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 4217 | /* |
| 4218 | * Return the character name of the register with the given number. |
| 4219 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4220 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4221 | get_register_name(int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4222 | { |
| 4223 | if (num == -1) |
| 4224 | return '"'; |
| 4225 | else if (num < 10) |
| 4226 | return num + '0'; |
| 4227 | else if (num == DELETION_REGISTER) |
| 4228 | return '-'; |
| 4229 | #ifdef FEAT_CLIPBOARD |
| 4230 | else if (num == STAR_REGISTER) |
| 4231 | return '*'; |
| 4232 | else if (num == PLUS_REGISTER) |
| 4233 | return '+'; |
| 4234 | #endif |
| 4235 | else |
| 4236 | { |
| 4237 | #ifdef EBCDIC |
| 4238 | int i; |
| 4239 | |
| 4240 | /* EBCDIC is really braindead ... */ |
| 4241 | i = 'a' + (num - 10); |
| 4242 | if (i > 'i') |
| 4243 | i += 7; |
| 4244 | if (i > 'r') |
| 4245 | i += 8; |
| 4246 | return i; |
| 4247 | #else |
| 4248 | return num + 'a' - 10; |
| 4249 | #endif |
| 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | /* |
| 4254 | * ":dis" and ":registers": Display the contents of the yank registers. |
| 4255 | */ |
| 4256 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4257 | ex_display(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4258 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 4259 | int i, n; |
| 4260 | long j; |
| 4261 | char_u *p; |
| 4262 | yankreg_T *yb; |
| 4263 | int name; |
| 4264 | int attr; |
| 4265 | char_u *arg = eap->arg; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4266 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 4267 | int clen; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4268 | #else |
| 4269 | # define clen 1 |
| 4270 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4271 | |
| 4272 | if (arg != NULL && *arg == NUL) |
| 4273 | arg = NULL; |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4274 | attr = HL_ATTR(HLF_8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4275 | |
| 4276 | /* Highlight title */ |
| 4277 | MSG_PUTS_TITLE(_("\n--- Registers ---")); |
| 4278 | for (i = -1; i < NUM_REGISTERS && !got_int; ++i) |
| 4279 | { |
| 4280 | name = get_register_name(i); |
Bram Moolenaar | 0818b87 | 2010-11-24 14:28:58 +0100 | [diff] [blame] | 4281 | if (arg != NULL && vim_strchr(arg, name) == NULL |
| 4282 | #ifdef ONE_CLIPBOARD |
| 4283 | /* Star register and plus register contain the same thing. */ |
| 4284 | && (name != '*' || vim_strchr(arg, '+') == NULL) |
| 4285 | #endif |
| 4286 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4287 | continue; /* did not ask for this register */ |
| 4288 | |
| 4289 | #ifdef FEAT_CLIPBOARD |
| 4290 | /* Adjust register name for "unnamed" in 'clipboard'. |
| 4291 | * When it's a clipboard register, fill it with the current contents |
| 4292 | * of the clipboard. */ |
| 4293 | adjust_clip_reg(&name); |
| 4294 | (void)may_get_selection(name); |
| 4295 | #endif |
| 4296 | |
| 4297 | if (i == -1) |
| 4298 | { |
| 4299 | if (y_previous != NULL) |
| 4300 | yb = y_previous; |
| 4301 | else |
| 4302 | yb = &(y_regs[0]); |
| 4303 | } |
| 4304 | else |
| 4305 | yb = &(y_regs[i]); |
Bram Moolenaar | cde547a | 2009-11-17 11:43:06 +0000 | [diff] [blame] | 4306 | |
| 4307 | #ifdef FEAT_EVAL |
| 4308 | if (name == MB_TOLOWER(redir_reg) |
| 4309 | || (redir_reg == '"' && yb == y_previous)) |
| 4310 | continue; /* do not list register being written to, the |
| 4311 | * pointer can be freed */ |
| 4312 | #endif |
| 4313 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4314 | if (yb->y_array != NULL) |
| 4315 | { |
| 4316 | msg_putchar('\n'); |
| 4317 | msg_putchar('"'); |
| 4318 | msg_putchar(name); |
| 4319 | MSG_PUTS(" "); |
| 4320 | |
| 4321 | n = (int)Columns - 6; |
| 4322 | for (j = 0; j < yb->y_size && n > 1; ++j) |
| 4323 | { |
| 4324 | if (j) |
| 4325 | { |
| 4326 | MSG_PUTS_ATTR("^J", attr); |
| 4327 | n -= 2; |
| 4328 | } |
| 4329 | for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p) |
| 4330 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4331 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4332 | clen = (*mb_ptr2len)(p); |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4333 | #endif |
| 4334 | msg_outtrans_len(p, clen); |
| 4335 | #ifdef FEAT_MBYTE |
| 4336 | p += clen - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4337 | #endif |
| 4338 | } |
| 4339 | } |
| 4340 | if (n > 1 && yb->y_type == MLINE) |
| 4341 | MSG_PUTS_ATTR("^J", attr); |
| 4342 | out_flush(); /* show one line at a time */ |
| 4343 | } |
| 4344 | ui_breakcheck(); |
| 4345 | } |
| 4346 | |
| 4347 | /* |
| 4348 | * display last inserted text |
| 4349 | */ |
| 4350 | if ((p = get_last_insert()) != NULL |
| 4351 | && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int) |
| 4352 | { |
| 4353 | MSG_PUTS("\n\". "); |
| 4354 | dis_msg(p, TRUE); |
| 4355 | } |
| 4356 | |
| 4357 | /* |
| 4358 | * display last command line |
| 4359 | */ |
| 4360 | if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL) |
| 4361 | && !got_int) |
| 4362 | { |
| 4363 | MSG_PUTS("\n\": "); |
| 4364 | dis_msg(last_cmdline, FALSE); |
| 4365 | } |
| 4366 | |
| 4367 | /* |
| 4368 | * display current file name |
| 4369 | */ |
| 4370 | if (curbuf->b_fname != NULL |
| 4371 | && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) |
| 4372 | { |
| 4373 | MSG_PUTS("\n\"% "); |
| 4374 | dis_msg(curbuf->b_fname, FALSE); |
| 4375 | } |
| 4376 | |
| 4377 | /* |
| 4378 | * display alternate file name |
| 4379 | */ |
| 4380 | if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) |
| 4381 | { |
| 4382 | char_u *fname; |
| 4383 | linenr_T dummy; |
| 4384 | |
| 4385 | if (buflist_name_nr(0, &fname, &dummy) != FAIL) |
| 4386 | { |
| 4387 | MSG_PUTS("\n\"# "); |
| 4388 | dis_msg(fname, FALSE); |
| 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | /* |
| 4393 | * display last search pattern |
| 4394 | */ |
| 4395 | if (last_search_pat() != NULL |
| 4396 | && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int) |
| 4397 | { |
| 4398 | MSG_PUTS("\n\"/ "); |
| 4399 | dis_msg(last_search_pat(), FALSE); |
| 4400 | } |
| 4401 | |
| 4402 | #ifdef FEAT_EVAL |
| 4403 | /* |
| 4404 | * display last used expression |
| 4405 | */ |
| 4406 | if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL) |
| 4407 | && !got_int) |
| 4408 | { |
| 4409 | MSG_PUTS("\n\"= "); |
| 4410 | dis_msg(expr_line, FALSE); |
| 4411 | } |
| 4412 | #endif |
| 4413 | } |
| 4414 | |
| 4415 | /* |
| 4416 | * display a string for do_dis() |
| 4417 | * truncate at end of screen line |
| 4418 | */ |
| 4419 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4420 | dis_msg( |
| 4421 | char_u *p, |
| 4422 | int skip_esc) /* if TRUE, ignore trailing ESC */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4423 | { |
| 4424 | int n; |
| 4425 | #ifdef FEAT_MBYTE |
| 4426 | int l; |
| 4427 | #endif |
| 4428 | |
| 4429 | n = (int)Columns - 6; |
| 4430 | while (*p != NUL |
| 4431 | && !(*p == ESC && skip_esc && *(p + 1) == NUL) |
| 4432 | && (n -= ptr2cells(p)) >= 0) |
| 4433 | { |
| 4434 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4435 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4436 | { |
| 4437 | msg_outtrans_len(p, l); |
| 4438 | p += l; |
| 4439 | } |
| 4440 | else |
| 4441 | #endif |
| 4442 | msg_outtrans_len(p++, 1); |
| 4443 | } |
| 4444 | ui_breakcheck(); |
| 4445 | } |
| 4446 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4447 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4448 | /* |
| 4449 | * If "process" is TRUE and the line begins with a comment leader (possibly |
| 4450 | * after some white space), return a pointer to the text after it. Put a boolean |
| 4451 | * value indicating whether the line ends with an unclosed comment in |
| 4452 | * "is_comment". |
| 4453 | * line - line to be processed, |
| 4454 | * process - if FALSE, will only check whether the line ends with an unclosed |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4455 | * comment, |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4456 | * include_space - whether to also skip space following the comment leader, |
| 4457 | * is_comment - will indicate whether the current line ends with an unclosed |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4458 | * comment. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4459 | */ |
Bram Moolenaar | 025a6b7 | 2017-03-12 20:37:21 +0100 | [diff] [blame] | 4460 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4461 | skip_comment( |
| 4462 | char_u *line, |
| 4463 | int process, |
| 4464 | int include_space, |
| 4465 | int *is_comment) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4466 | { |
| 4467 | char_u *comment_flags = NULL; |
| 4468 | int lead_len; |
| 4469 | int leader_offset = get_last_leader_offset(line, &comment_flags); |
| 4470 | |
| 4471 | *is_comment = FALSE; |
| 4472 | if (leader_offset != -1) |
| 4473 | { |
| 4474 | /* Let's check whether the line ends with an unclosed comment. |
| 4475 | * If the last comment leader has COM_END in flags, there's no comment. |
| 4476 | */ |
| 4477 | while (*comment_flags) |
| 4478 | { |
| 4479 | if (*comment_flags == COM_END |
| 4480 | || *comment_flags == ':') |
| 4481 | break; |
| 4482 | ++comment_flags; |
| 4483 | } |
| 4484 | if (*comment_flags != COM_END) |
| 4485 | *is_comment = TRUE; |
| 4486 | } |
| 4487 | |
| 4488 | if (process == FALSE) |
| 4489 | return line; |
| 4490 | |
| 4491 | lead_len = get_leader_len(line, &comment_flags, FALSE, include_space); |
| 4492 | |
| 4493 | if (lead_len == 0) |
| 4494 | return line; |
| 4495 | |
| 4496 | /* Find: |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4497 | * - COM_END, |
| 4498 | * - colon, |
| 4499 | * whichever comes first. |
| 4500 | */ |
| 4501 | while (*comment_flags) |
| 4502 | { |
Bram Moolenaar | e04a48f | 2012-06-13 14:01:41 +0200 | [diff] [blame] | 4503 | if (*comment_flags == COM_END |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4504 | || *comment_flags == ':') |
| 4505 | { |
| 4506 | break; |
| 4507 | } |
| 4508 | ++comment_flags; |
| 4509 | } |
| 4510 | |
| 4511 | /* If we found a colon, it means that we are not processing a line |
Bram Moolenaar | e04a48f | 2012-06-13 14:01:41 +0200 | [diff] [blame] | 4512 | * starting with a closing part of a three-part comment. That's good, |
| 4513 | * because we don't want to remove those as this would be annoying. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4514 | */ |
| 4515 | if (*comment_flags == ':' || *comment_flags == NUL) |
| 4516 | line += lead_len; |
| 4517 | |
| 4518 | return line; |
| 4519 | } |
| 4520 | #endif |
| 4521 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4522 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4523 | * Join 'count' lines (minimal 2) at cursor position. |
| 4524 | * When "save_undo" is TRUE save lines for undo first. |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 4525 | * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment |
| 4526 | * leaders should not be removed. |
| 4527 | * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected |
| 4528 | * to set those marks. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4529 | * |
Bram Moolenaar | 10c5695 | 2007-05-10 18:38:52 +0000 | [diff] [blame] | 4530 | * return FAIL for failure, OK otherwise |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4531 | */ |
| 4532 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4533 | do_join( |
| 4534 | long count, |
| 4535 | int insert_space, |
| 4536 | int save_undo, |
| 4537 | int use_formatoptions UNUSED, |
| 4538 | int setmark) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4539 | { |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4540 | char_u *curr = NULL; |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 4541 | char_u *curr_start = NULL; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4542 | char_u *cend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4543 | char_u *newp; |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 4544 | char_u *spaces; /* number of spaces inserted before a line */ |
Bram Moolenaar | d43848c | 2010-07-14 14:28:26 +0200 | [diff] [blame] | 4545 | int endcurr1 = NUL; |
| 4546 | int endcurr2 = NUL; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4547 | int currsize = 0; /* size of the current line */ |
| 4548 | int sumsize = 0; /* size of the long new line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4549 | linenr_T t; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4550 | colnr_T col = 0; |
| 4551 | int ret = OK; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4552 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
Bram Moolenaar | 802053f | 2012-06-06 23:08:38 +0200 | [diff] [blame] | 4553 | int *comments = NULL; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4554 | int remove_comments = (use_formatoptions == TRUE) |
| 4555 | && has_format_option(FO_REMOVE_COMS); |
| 4556 | int prev_was_comment; |
| 4557 | #endif |
| 4558 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4559 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4560 | if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 4561 | (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL) |
| 4562 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4563 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4564 | /* Allocate an array to store the number of spaces inserted before each |
| 4565 | * line. We will use it to pre-compute the length of the new line and the |
| 4566 | * proper placement of each original line in the new one. */ |
| 4567 | spaces = lalloc_clear((long_u)count, TRUE); |
| 4568 | if (spaces == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4569 | return FAIL; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4570 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4571 | if (remove_comments) |
| 4572 | { |
| 4573 | comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE); |
| 4574 | if (comments == NULL) |
| 4575 | { |
| 4576 | vim_free(spaces); |
| 4577 | return FAIL; |
| 4578 | } |
| 4579 | } |
| 4580 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4581 | |
| 4582 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4583 | * Don't move anything, just compute the final line length |
| 4584 | * and setup the array of space strings lengths |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4585 | */ |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4586 | for (t = 0; t < count; ++t) |
| 4587 | { |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 4588 | curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t)); |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 4589 | if (t == 0 && setmark) |
Bram Moolenaar | f92d8a2 | 2014-02-11 19:33:07 +0100 | [diff] [blame] | 4590 | { |
| 4591 | /* Set the '[ mark. */ |
| 4592 | curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum; |
| 4593 | curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr); |
| 4594 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4595 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4596 | if (remove_comments) |
| 4597 | { |
| 4598 | /* We don't want to remove the comment leader if the |
| 4599 | * previous line is not a comment. */ |
| 4600 | if (t > 0 && prev_was_comment) |
| 4601 | { |
| 4602 | |
| 4603 | char_u *new_curr = skip_comment(curr, TRUE, insert_space, |
| 4604 | &prev_was_comment); |
Bram Moolenaar | 27ba088 | 2012-06-07 21:09:39 +0200 | [diff] [blame] | 4605 | comments[t] = (int)(new_curr - curr); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4606 | curr = new_curr; |
| 4607 | } |
| 4608 | else |
| 4609 | curr = skip_comment(curr, FALSE, insert_space, |
| 4610 | &prev_was_comment); |
| 4611 | } |
| 4612 | #endif |
| 4613 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4614 | if (insert_space && t > 0) |
| 4615 | { |
| 4616 | curr = skipwhite(curr); |
| 4617 | if (*curr != ')' && currsize != 0 && endcurr1 != TAB |
| 4618 | #ifdef FEAT_MBYTE |
| 4619 | && (!has_format_option(FO_MBYTE_JOIN) |
| 4620 | || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100)) |
| 4621 | && (!has_format_option(FO_MBYTE_JOIN2) |
| 4622 | || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100) |
| 4623 | #endif |
| 4624 | ) |
| 4625 | { |
| 4626 | /* don't add a space if the line is ending in a space */ |
| 4627 | if (endcurr1 == ' ') |
| 4628 | endcurr1 = endcurr2; |
| 4629 | else |
| 4630 | ++spaces[t]; |
| 4631 | /* extra space when 'joinspaces' set and line ends in '.' */ |
| 4632 | if ( p_js |
| 4633 | && (endcurr1 == '.' |
| 4634 | || (vim_strchr(p_cpo, CPO_JOINSP) == NULL |
| 4635 | && (endcurr1 == '?' || endcurr1 == '!')))) |
| 4636 | ++spaces[t]; |
| 4637 | } |
| 4638 | } |
| 4639 | currsize = (int)STRLEN(curr); |
| 4640 | sumsize += currsize + spaces[t]; |
| 4641 | endcurr1 = endcurr2 = NUL; |
| 4642 | if (insert_space && currsize > 0) |
| 4643 | { |
| 4644 | #ifdef FEAT_MBYTE |
| 4645 | if (has_mbyte) |
| 4646 | { |
| 4647 | cend = curr + currsize; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4648 | MB_PTR_BACK(curr, cend); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4649 | endcurr1 = (*mb_ptr2char)(cend); |
| 4650 | if (cend > curr) |
| 4651 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4652 | MB_PTR_BACK(curr, cend); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4653 | endcurr2 = (*mb_ptr2char)(cend); |
| 4654 | } |
| 4655 | } |
| 4656 | else |
| 4657 | #endif |
| 4658 | { |
| 4659 | endcurr1 = *(curr + currsize - 1); |
| 4660 | if (currsize > 1) |
| 4661 | endcurr2 = *(curr + currsize - 2); |
| 4662 | } |
| 4663 | } |
| 4664 | line_breakcheck(); |
| 4665 | if (got_int) |
| 4666 | { |
| 4667 | ret = FAIL; |
| 4668 | goto theend; |
| 4669 | } |
| 4670 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4671 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4672 | /* store the column position before last line */ |
| 4673 | col = sumsize - currsize - spaces[count - 1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4674 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4675 | /* allocate the space for the new line */ |
| 4676 | newp = alloc_check((unsigned)(sumsize + 1)); |
| 4677 | cend = newp + sumsize; |
| 4678 | *cend = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4679 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4680 | /* |
| 4681 | * Move affected lines to the new long one. |
| 4682 | * |
| 4683 | * Move marks from each deleted line to the joined line, adjusting the |
| 4684 | * column. This is not Vi compatible, but Vi deletes the marks, thus that |
| 4685 | * should not really be a problem. |
| 4686 | */ |
| 4687 | for (t = count - 1; ; --t) |
| 4688 | { |
| 4689 | cend -= currsize; |
| 4690 | mch_memmove(cend, curr, (size_t)currsize); |
| 4691 | if (spaces[t] > 0) |
| 4692 | { |
| 4693 | cend -= spaces[t]; |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 4694 | vim_memset(cend, ' ', (size_t)(spaces[t])); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4695 | } |
| 4696 | mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t, |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 4697 | (long)(cend - newp + spaces[t] - (curr - curr_start))); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4698 | if (t == 0) |
| 4699 | break; |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 4700 | curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1)); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4701 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4702 | if (remove_comments) |
| 4703 | curr += comments[t - 1]; |
| 4704 | #endif |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4705 | if (insert_space && t > 1) |
| 4706 | curr = skipwhite(curr); |
| 4707 | currsize = (int)STRLEN(curr); |
| 4708 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4709 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 4710 | |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 4711 | if (setmark) |
| 4712 | { |
| 4713 | /* Set the '] mark. */ |
| 4714 | curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum; |
| 4715 | curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp); |
| 4716 | } |
Bram Moolenaar | f92d8a2 | 2014-02-11 19:33:07 +0100 | [diff] [blame] | 4717 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4718 | /* Only report the change in the first line here, del_lines() will report |
| 4719 | * the deleted line. */ |
| 4720 | changed_lines(curwin->w_cursor.lnum, currsize, |
| 4721 | curwin->w_cursor.lnum + 1, 0L); |
| 4722 | |
| 4723 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4724 | * Delete following lines. To do this we move the cursor there |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4725 | * briefly, and then move it back. After del_lines() the cursor may |
| 4726 | * have moved up (last line deleted), so the current lnum is kept in t. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4727 | */ |
| 4728 | t = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4729 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4730 | del_lines(count - 1, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4731 | curwin->w_cursor.lnum = t; |
| 4732 | |
| 4733 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4734 | * Set the cursor column: |
| 4735 | * Vi compatible: use the column of the first join |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4736 | * vim: use the column of the last join |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4737 | */ |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4738 | curwin->w_cursor.col = |
| 4739 | (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4740 | check_cursor_col(); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4741 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4742 | #ifdef FEAT_VIRTUALEDIT |
| 4743 | curwin->w_cursor.coladd = 0; |
| 4744 | #endif |
| 4745 | curwin->w_set_curswant = TRUE; |
| 4746 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4747 | theend: |
| 4748 | vim_free(spaces); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4749 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4750 | if (remove_comments) |
| 4751 | vim_free(comments); |
| 4752 | #endif |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4753 | return ret; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | #ifdef FEAT_COMMENTS |
| 4757 | /* |
| 4758 | * Return TRUE if the two comment leaders given are the same. "lnum" is |
| 4759 | * the first line. White-space is ignored. Note that the whole of |
| 4760 | * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb |
| 4761 | */ |
| 4762 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4763 | same_leader( |
| 4764 | linenr_T lnum, |
| 4765 | int leader1_len, |
| 4766 | char_u *leader1_flags, |
| 4767 | int leader2_len, |
| 4768 | char_u *leader2_flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4769 | { |
| 4770 | int idx1 = 0, idx2 = 0; |
| 4771 | char_u *p; |
| 4772 | char_u *line1; |
| 4773 | char_u *line2; |
| 4774 | |
| 4775 | if (leader1_len == 0) |
| 4776 | return (leader2_len == 0); |
| 4777 | |
| 4778 | /* |
| 4779 | * If first leader has 'f' flag, the lines can be joined only if the |
| 4780 | * second line does not have a leader. |
| 4781 | * If first leader has 'e' flag, the lines can never be joined. |
| 4782 | * If fist leader has 's' flag, the lines can only be joined if there is |
| 4783 | * some text after it and the second line has the 'm' flag. |
| 4784 | */ |
| 4785 | if (leader1_flags != NULL) |
| 4786 | { |
| 4787 | for (p = leader1_flags; *p && *p != ':'; ++p) |
| 4788 | { |
| 4789 | if (*p == COM_FIRST) |
| 4790 | return (leader2_len == 0); |
| 4791 | if (*p == COM_END) |
| 4792 | return FALSE; |
| 4793 | if (*p == COM_START) |
| 4794 | { |
| 4795 | if (*(ml_get(lnum) + leader1_len) == NUL) |
| 4796 | return FALSE; |
| 4797 | if (leader2_flags == NULL || leader2_len == 0) |
| 4798 | return FALSE; |
| 4799 | for (p = leader2_flags; *p && *p != ':'; ++p) |
| 4800 | if (*p == COM_MIDDLE) |
| 4801 | return TRUE; |
| 4802 | return FALSE; |
| 4803 | } |
| 4804 | } |
| 4805 | } |
| 4806 | |
| 4807 | /* |
| 4808 | * Get current line and next line, compare the leaders. |
| 4809 | * The first line has to be saved, only one line can be locked at a time. |
| 4810 | */ |
| 4811 | line1 = vim_strsave(ml_get(lnum)); |
| 4812 | if (line1 != NULL) |
| 4813 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 4814 | for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4815 | ; |
| 4816 | line2 = ml_get(lnum + 1); |
| 4817 | for (idx2 = 0; idx2 < leader2_len; ++idx2) |
| 4818 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 4819 | if (!VIM_ISWHITE(line2[idx2])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4820 | { |
| 4821 | if (line1[idx1++] != line2[idx2]) |
| 4822 | break; |
| 4823 | } |
| 4824 | else |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 4825 | while (VIM_ISWHITE(line1[idx1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | ++idx1; |
| 4827 | } |
| 4828 | vim_free(line1); |
| 4829 | } |
| 4830 | return (idx2 == leader2_len && idx1 == leader1_len); |
| 4831 | } |
| 4832 | #endif |
| 4833 | |
| 4834 | /* |
Bram Moolenaar | 66accae | 2012-01-10 13:44:27 +0100 | [diff] [blame] | 4835 | * Implementation of the format operator 'gq'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4836 | */ |
| 4837 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4838 | op_format( |
| 4839 | oparg_T *oap, |
| 4840 | int keep_cursor) /* keep cursor on same text char */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4841 | { |
| 4842 | long old_line_count = curbuf->b_ml.ml_line_count; |
| 4843 | |
| 4844 | /* Place the cursor where the "gq" or "gw" command was given, so that "u" |
| 4845 | * can put it back there. */ |
| 4846 | curwin->w_cursor = oap->cursor_start; |
| 4847 | |
| 4848 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 4849 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 4850 | return; |
| 4851 | curwin->w_cursor = oap->start; |
| 4852 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4853 | if (oap->is_VIsual) |
| 4854 | /* When there is no change: need to remove the Visual selection */ |
| 4855 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4856 | |
| 4857 | /* Set '[ mark at the start of the formatted area */ |
| 4858 | curbuf->b_op_start = oap->start; |
| 4859 | |
| 4860 | /* For "gw" remember the cursor position and put it back below (adjusted |
| 4861 | * for joined and split lines). */ |
| 4862 | if (keep_cursor) |
| 4863 | saved_cursor = oap->cursor_start; |
| 4864 | |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 4865 | format_lines(oap->line_count, keep_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4866 | |
| 4867 | /* |
| 4868 | * Leave the cursor at the first non-blank of the last formatted line. |
| 4869 | * If the cursor was moved one line back (e.g. with "Q}") go to the next |
| 4870 | * line, so "." will do the next lines. |
| 4871 | */ |
| 4872 | if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 4873 | ++curwin->w_cursor.lnum; |
| 4874 | beginline(BL_WHITE | BL_FIX); |
| 4875 | old_line_count = curbuf->b_ml.ml_line_count - old_line_count; |
| 4876 | msgmore(old_line_count); |
| 4877 | |
| 4878 | /* put '] mark on the end of the formatted area */ |
| 4879 | curbuf->b_op_end = curwin->w_cursor; |
| 4880 | |
| 4881 | if (keep_cursor) |
| 4882 | { |
| 4883 | curwin->w_cursor = saved_cursor; |
| 4884 | saved_cursor.lnum = 0; |
| 4885 | } |
| 4886 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4887 | if (oap->is_VIsual) |
| 4888 | { |
| 4889 | win_T *wp; |
| 4890 | |
| 4891 | FOR_ALL_WINDOWS(wp) |
| 4892 | { |
| 4893 | if (wp->w_old_cursor_lnum != 0) |
| 4894 | { |
| 4895 | /* When lines have been inserted or deleted, adjust the end of |
| 4896 | * the Visual area to be redrawn. */ |
| 4897 | if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum) |
| 4898 | wp->w_old_cursor_lnum += old_line_count; |
| 4899 | else |
| 4900 | wp->w_old_visual_lnum += old_line_count; |
| 4901 | } |
| 4902 | } |
| 4903 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4906 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 4907 | /* |
| 4908 | * Implementation of the format operator 'gq' for when using 'formatexpr'. |
| 4909 | */ |
| 4910 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4911 | op_formatexpr(oparg_T *oap) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4912 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4913 | if (oap->is_VIsual) |
| 4914 | /* When there is no change: need to remove the Visual selection */ |
| 4915 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4916 | |
Bram Moolenaar | 700303e | 2010-07-11 17:35:50 +0200 | [diff] [blame] | 4917 | if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0) |
| 4918 | /* As documented: when 'formatexpr' returns non-zero fall back to |
| 4919 | * internal formatting. */ |
| 4920 | op_format(oap, FALSE); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4924 | fex_format( |
| 4925 | linenr_T lnum, |
| 4926 | long count, |
| 4927 | int c) /* character to be inserted */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4928 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4929 | int use_sandbox = was_set_insecurely((char_u *)"formatexpr", |
| 4930 | OPT_LOCAL); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4931 | int r; |
Bram Moolenaar | d77f9d5 | 2016-09-04 15:13:39 +0200 | [diff] [blame] | 4932 | char_u *fex; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4933 | |
| 4934 | /* |
| 4935 | * Set v:lnum to the first line number and v:count to the number of lines. |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4936 | * Set v:char to the character to be inserted (can be NUL). |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4937 | */ |
| 4938 | set_vim_var_nr(VV_LNUM, lnum); |
| 4939 | set_vim_var_nr(VV_COUNT, count); |
Bram Moolenaar | da9591e | 2009-09-30 13:17:02 +0000 | [diff] [blame] | 4940 | set_vim_var_char(c); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4941 | |
Bram Moolenaar | d77f9d5 | 2016-09-04 15:13:39 +0200 | [diff] [blame] | 4942 | /* Make a copy, the option could be changed while calling it. */ |
| 4943 | fex = vim_strsave(curbuf->b_p_fex); |
| 4944 | if (fex == NULL) |
| 4945 | return 0; |
| 4946 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4947 | /* |
| 4948 | * Evaluate the function. |
| 4949 | */ |
| 4950 | if (use_sandbox) |
| 4951 | ++sandbox; |
Bram Moolenaar | d77f9d5 | 2016-09-04 15:13:39 +0200 | [diff] [blame] | 4952 | r = (int)eval_to_number(fex); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4953 | if (use_sandbox) |
| 4954 | --sandbox; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4955 | |
| 4956 | set_vim_var_string(VV_CHAR, NULL, -1); |
Bram Moolenaar | d77f9d5 | 2016-09-04 15:13:39 +0200 | [diff] [blame] | 4957 | vim_free(fex); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4958 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4959 | return r; |
| 4960 | } |
| 4961 | #endif |
| 4962 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4963 | /* |
| 4964 | * Format "line_count" lines, starting at the cursor position. |
| 4965 | * When "line_count" is negative, format until the end of the paragraph. |
| 4966 | * Lines after the cursor line are saved for undo, caller must have saved the |
| 4967 | * first line. |
| 4968 | */ |
| 4969 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4970 | format_lines( |
| 4971 | linenr_T line_count, |
| 4972 | int avoid_fex) /* don't use 'formatexpr' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4973 | { |
| 4974 | int max_len; |
| 4975 | int is_not_par; /* current line not part of parag. */ |
| 4976 | int next_is_not_par; /* next line not part of paragraph */ |
| 4977 | int is_end_par; /* at end of paragraph */ |
| 4978 | int prev_is_end_par = FALSE;/* prev. line not part of parag. */ |
| 4979 | int next_is_start_par = FALSE; |
| 4980 | #ifdef FEAT_COMMENTS |
| 4981 | int leader_len = 0; /* leader len of current line */ |
| 4982 | int next_leader_len; /* leader len of next line */ |
| 4983 | char_u *leader_flags = NULL; /* flags for leader of current line */ |
| 4984 | char_u *next_leader_flags; /* flags for leader of next line */ |
| 4985 | int do_comments; /* format comments */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4986 | int do_comments_list = 0; /* format comments with 'n' or '2' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4987 | #endif |
| 4988 | int advance = TRUE; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4989 | int second_indent = -1; /* indent for second line (comment |
| 4990 | * aware) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4991 | int do_second_indent; |
| 4992 | int do_number_indent; |
| 4993 | int do_trail_white; |
| 4994 | int first_par_line = TRUE; |
| 4995 | int smd_save; |
| 4996 | long count; |
| 4997 | int need_set_indent = TRUE; /* set indent of next paragraph */ |
| 4998 | int force_format = FALSE; |
| 4999 | int old_State = State; |
| 5000 | |
| 5001 | /* length of a line to force formatting: 3 * 'tw' */ |
| 5002 | max_len = comp_textwidth(TRUE) * 3; |
| 5003 | |
| 5004 | /* check for 'q', '2' and '1' in 'formatoptions' */ |
| 5005 | #ifdef FEAT_COMMENTS |
| 5006 | do_comments = has_format_option(FO_Q_COMS); |
| 5007 | #endif |
| 5008 | do_second_indent = has_format_option(FO_Q_SECOND); |
| 5009 | do_number_indent = has_format_option(FO_Q_NUMBER); |
| 5010 | do_trail_white = has_format_option(FO_WHITE_PAR); |
| 5011 | |
| 5012 | /* |
| 5013 | * Get info about the previous and current line. |
| 5014 | */ |
| 5015 | if (curwin->w_cursor.lnum > 1) |
| 5016 | is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1 |
| 5017 | #ifdef FEAT_COMMENTS |
| 5018 | , &leader_len, &leader_flags, do_comments |
| 5019 | #endif |
| 5020 | ); |
| 5021 | else |
| 5022 | is_not_par = TRUE; |
| 5023 | next_is_not_par = fmt_check_par(curwin->w_cursor.lnum |
| 5024 | #ifdef FEAT_COMMENTS |
| 5025 | , &next_leader_len, &next_leader_flags, do_comments |
| 5026 | #endif |
| 5027 | ); |
| 5028 | is_end_par = (is_not_par || next_is_not_par); |
| 5029 | if (!is_end_par && do_trail_white) |
| 5030 | is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1); |
| 5031 | |
| 5032 | curwin->w_cursor.lnum--; |
| 5033 | for (count = line_count; count != 0 && !got_int; --count) |
| 5034 | { |
| 5035 | /* |
| 5036 | * Advance to next paragraph. |
| 5037 | */ |
| 5038 | if (advance) |
| 5039 | { |
| 5040 | curwin->w_cursor.lnum++; |
| 5041 | prev_is_end_par = is_end_par; |
| 5042 | is_not_par = next_is_not_par; |
| 5043 | #ifdef FEAT_COMMENTS |
| 5044 | leader_len = next_leader_len; |
| 5045 | leader_flags = next_leader_flags; |
| 5046 | #endif |
| 5047 | } |
| 5048 | |
| 5049 | /* |
| 5050 | * The last line to be formatted. |
| 5051 | */ |
| 5052 | if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count) |
| 5053 | { |
| 5054 | next_is_not_par = TRUE; |
| 5055 | #ifdef FEAT_COMMENTS |
| 5056 | next_leader_len = 0; |
| 5057 | next_leader_flags = NULL; |
| 5058 | #endif |
| 5059 | } |
| 5060 | else |
| 5061 | { |
| 5062 | next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1 |
| 5063 | #ifdef FEAT_COMMENTS |
| 5064 | , &next_leader_len, &next_leader_flags, do_comments |
| 5065 | #endif |
| 5066 | ); |
| 5067 | if (do_number_indent) |
| 5068 | next_is_start_par = |
| 5069 | (get_number_indent(curwin->w_cursor.lnum + 1) > 0); |
| 5070 | } |
| 5071 | advance = TRUE; |
| 5072 | is_end_par = (is_not_par || next_is_not_par || next_is_start_par); |
| 5073 | if (!is_end_par && do_trail_white) |
| 5074 | is_end_par = !ends_in_white(curwin->w_cursor.lnum); |
| 5075 | |
| 5076 | /* |
| 5077 | * Skip lines that are not in a paragraph. |
| 5078 | */ |
| 5079 | if (is_not_par) |
| 5080 | { |
| 5081 | if (line_count < 0) |
| 5082 | break; |
| 5083 | } |
| 5084 | else |
| 5085 | { |
| 5086 | /* |
| 5087 | * For the first line of a paragraph, check indent of second line. |
| 5088 | * Don't do this for comments and empty lines. |
| 5089 | */ |
| 5090 | if (first_par_line |
| 5091 | && (do_second_indent || do_number_indent) |
| 5092 | && prev_is_end_par |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5093 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5094 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 5095 | if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5096 | { |
| 5097 | #ifdef FEAT_COMMENTS |
| 5098 | if (leader_len == 0 && next_leader_len == 0) |
| 5099 | { |
| 5100 | /* no comment found */ |
| 5101 | #endif |
| 5102 | second_indent = |
| 5103 | get_indent_lnum(curwin->w_cursor.lnum + 1); |
| 5104 | #ifdef FEAT_COMMENTS |
| 5105 | } |
| 5106 | else |
| 5107 | { |
| 5108 | second_indent = next_leader_len; |
| 5109 | do_comments_list = 1; |
| 5110 | } |
| 5111 | #endif |
| 5112 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5113 | else if (do_number_indent) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5114 | { |
| 5115 | #ifdef FEAT_COMMENTS |
| 5116 | if (leader_len == 0 && next_leader_len == 0) |
| 5117 | { |
| 5118 | /* no comment found */ |
| 5119 | #endif |
| 5120 | second_indent = |
| 5121 | get_number_indent(curwin->w_cursor.lnum); |
| 5122 | #ifdef FEAT_COMMENTS |
| 5123 | } |
| 5124 | else |
| 5125 | { |
| 5126 | /* get_number_indent() is now "comment aware"... */ |
| 5127 | second_indent = |
| 5128 | get_number_indent(curwin->w_cursor.lnum); |
| 5129 | do_comments_list = 1; |
| 5130 | } |
| 5131 | #endif |
| 5132 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | /* |
| 5136 | * When the comment leader changes, it's the end of the paragraph. |
| 5137 | */ |
| 5138 | if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count |
| 5139 | #ifdef FEAT_COMMENTS |
| 5140 | || !same_leader(curwin->w_cursor.lnum, |
| 5141 | leader_len, leader_flags, |
| 5142 | next_leader_len, next_leader_flags) |
| 5143 | #endif |
| 5144 | ) |
| 5145 | is_end_par = TRUE; |
| 5146 | |
| 5147 | /* |
| 5148 | * If we have got to the end of a paragraph, or the line is |
| 5149 | * getting long, format it. |
| 5150 | */ |
| 5151 | if (is_end_par || force_format) |
| 5152 | { |
| 5153 | if (need_set_indent) |
| 5154 | /* replace indent in first line with minimal number of |
| 5155 | * tabs and spaces, according to current options */ |
| 5156 | (void)set_indent(get_indent(), SIN_CHANGED); |
| 5157 | |
| 5158 | /* put cursor on last non-space */ |
| 5159 | State = NORMAL; /* don't go past end-of-line */ |
| 5160 | coladvance((colnr_T)MAXCOL); |
| 5161 | while (curwin->w_cursor.col && vim_isspace(gchar_cursor())) |
| 5162 | dec_cursor(); |
| 5163 | |
| 5164 | /* do the formatting, without 'showmode' */ |
| 5165 | State = INSERT; /* for open_line() */ |
| 5166 | smd_save = p_smd; |
| 5167 | p_smd = FALSE; |
| 5168 | insertchar(NUL, INSCHAR_FORMAT |
| 5169 | #ifdef FEAT_COMMENTS |
| 5170 | + (do_comments ? INSCHAR_DO_COM : 0) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5171 | + (do_comments && do_comments_list |
| 5172 | ? INSCHAR_COM_LIST : 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5173 | #endif |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 5174 | + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5175 | State = old_State; |
| 5176 | p_smd = smd_save; |
| 5177 | second_indent = -1; |
| 5178 | /* at end of par.: need to set indent of next par. */ |
| 5179 | need_set_indent = is_end_par; |
| 5180 | if (is_end_par) |
| 5181 | { |
| 5182 | /* When called with a negative line count, break at the |
| 5183 | * end of the paragraph. */ |
| 5184 | if (line_count < 0) |
| 5185 | break; |
| 5186 | first_par_line = TRUE; |
| 5187 | } |
| 5188 | force_format = FALSE; |
| 5189 | } |
| 5190 | |
| 5191 | /* |
| 5192 | * When still in same paragraph, join the lines together. But |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 5193 | * first delete the leader from the second line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5194 | */ |
| 5195 | if (!is_end_par) |
| 5196 | { |
| 5197 | advance = FALSE; |
| 5198 | curwin->w_cursor.lnum++; |
| 5199 | curwin->w_cursor.col = 0; |
| 5200 | if (line_count < 0 && u_save_cursor() == FAIL) |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 5201 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5202 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5203 | if (next_leader_len > 0) |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 5204 | { |
| 5205 | (void)del_bytes((long)next_leader_len, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5206 | mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L, |
| 5207 | (long)-next_leader_len); |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 5208 | } else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5209 | #endif |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 5210 | if (second_indent > 0) /* the "leader" for FO_Q_SECOND */ |
| 5211 | { |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 5212 | int indent = getwhitecols_curline(); |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 5213 | |
| 5214 | if (indent > 0) |
| 5215 | { |
| 5216 | (void)del_bytes(indent, FALSE, FALSE); |
| 5217 | mark_col_adjust(curwin->w_cursor.lnum, |
| 5218 | (colnr_T)0, 0L, (long)-indent); |
Bram Moolenaar | 92c2db8 | 2013-11-02 23:59:35 +0100 | [diff] [blame] | 5219 | } |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 5220 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5221 | curwin->w_cursor.lnum--; |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 5222 | if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5223 | { |
| 5224 | beep_flush(); |
| 5225 | break; |
| 5226 | } |
| 5227 | first_par_line = FALSE; |
| 5228 | /* If the line is getting long, format it next time */ |
| 5229 | if (STRLEN(ml_get_curline()) > (size_t)max_len) |
| 5230 | force_format = TRUE; |
| 5231 | else |
| 5232 | force_format = FALSE; |
| 5233 | } |
| 5234 | } |
| 5235 | line_breakcheck(); |
| 5236 | } |
| 5237 | } |
| 5238 | |
| 5239 | /* |
| 5240 | * Return TRUE if line "lnum" ends in a white character. |
| 5241 | */ |
| 5242 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5243 | ends_in_white(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5244 | { |
| 5245 | char_u *s = ml_get(lnum); |
| 5246 | size_t l; |
| 5247 | |
| 5248 | if (*s == NUL) |
| 5249 | return FALSE; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 5250 | /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5251 | * invocation may call function multiple times". */ |
| 5252 | l = STRLEN(s) - 1; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 5253 | return VIM_ISWHITE(s[l]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
| 5256 | /* |
| 5257 | * Blank lines, and lines containing only the comment leader, are left |
| 5258 | * untouched by the formatting. The function returns TRUE in this |
| 5259 | * case. It also returns TRUE when a line starts with the end of a comment |
| 5260 | * ('e' in comment flags), so that this line is skipped, and not joined to the |
| 5261 | * previous line. A new paragraph starts after a blank line, or when the |
| 5262 | * comment leader changes -- webb. |
| 5263 | */ |
| 5264 | #ifdef FEAT_COMMENTS |
| 5265 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5266 | fmt_check_par( |
| 5267 | linenr_T lnum, |
| 5268 | int *leader_len, |
| 5269 | char_u **leader_flags, |
| 5270 | int do_comments) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5271 | { |
| 5272 | char_u *flags = NULL; /* init for GCC */ |
| 5273 | char_u *ptr; |
| 5274 | |
| 5275 | ptr = ml_get(lnum); |
| 5276 | if (do_comments) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 5277 | *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5278 | else |
| 5279 | *leader_len = 0; |
| 5280 | |
| 5281 | if (*leader_len > 0) |
| 5282 | { |
| 5283 | /* |
| 5284 | * Search for 'e' flag in comment leader flags. |
| 5285 | */ |
| 5286 | flags = *leader_flags; |
| 5287 | while (*flags && *flags != ':' && *flags != COM_END) |
| 5288 | ++flags; |
| 5289 | } |
| 5290 | |
| 5291 | return (*skipwhite(ptr + *leader_len) == NUL |
| 5292 | || (*leader_len > 0 && *flags == COM_END) |
| 5293 | || startPS(lnum, NUL, FALSE)); |
| 5294 | } |
| 5295 | #else |
| 5296 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5297 | fmt_check_par(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5298 | { |
| 5299 | return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE)); |
| 5300 | } |
| 5301 | #endif |
| 5302 | |
| 5303 | /* |
| 5304 | * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the |
| 5305 | * previous line is in the same paragraph. Used for auto-formatting. |
| 5306 | */ |
| 5307 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5308 | paragraph_start(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5309 | { |
| 5310 | char_u *p; |
| 5311 | #ifdef FEAT_COMMENTS |
| 5312 | int leader_len = 0; /* leader len of current line */ |
| 5313 | char_u *leader_flags = NULL; /* flags for leader of current line */ |
| 5314 | int next_leader_len; /* leader len of next line */ |
| 5315 | char_u *next_leader_flags; /* flags for leader of next line */ |
| 5316 | int do_comments; /* format comments */ |
| 5317 | #endif |
| 5318 | |
| 5319 | if (lnum <= 1) |
| 5320 | return TRUE; /* start of the file */ |
| 5321 | |
| 5322 | p = ml_get(lnum - 1); |
| 5323 | if (*p == NUL) |
| 5324 | return TRUE; /* after empty line */ |
| 5325 | |
| 5326 | #ifdef FEAT_COMMENTS |
| 5327 | do_comments = has_format_option(FO_Q_COMS); |
| 5328 | #endif |
| 5329 | if (fmt_check_par(lnum - 1 |
| 5330 | #ifdef FEAT_COMMENTS |
| 5331 | , &leader_len, &leader_flags, do_comments |
| 5332 | #endif |
| 5333 | )) |
| 5334 | return TRUE; /* after non-paragraph line */ |
| 5335 | |
| 5336 | if (fmt_check_par(lnum |
| 5337 | #ifdef FEAT_COMMENTS |
| 5338 | , &next_leader_len, &next_leader_flags, do_comments |
| 5339 | #endif |
| 5340 | )) |
| 5341 | return TRUE; /* "lnum" is not a paragraph line */ |
| 5342 | |
| 5343 | if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1)) |
| 5344 | return TRUE; /* missing trailing space in previous line. */ |
| 5345 | |
| 5346 | if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0)) |
| 5347 | return TRUE; /* numbered item starts in "lnum". */ |
| 5348 | |
| 5349 | #ifdef FEAT_COMMENTS |
| 5350 | if (!same_leader(lnum - 1, leader_len, leader_flags, |
| 5351 | next_leader_len, next_leader_flags)) |
| 5352 | return TRUE; /* change of comment leader. */ |
| 5353 | #endif |
| 5354 | |
| 5355 | return FALSE; |
| 5356 | } |
| 5357 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5358 | /* |
| 5359 | * prepare a few things for block mode yank/delete/tilde |
| 5360 | * |
| 5361 | * for delete: |
| 5362 | * - textlen includes the first/last char to be (partly) deleted |
| 5363 | * - start/endspaces is the number of columns that are taken by the |
| 5364 | * first/last deleted char minus the number of columns that have to be |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 5365 | * deleted. |
| 5366 | * for yank and tilde: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5367 | * - textlen includes the first/last char to be wholly yanked |
| 5368 | * - start/endspaces is the number of columns of the first/last yanked char |
| 5369 | * that are to be yanked. |
| 5370 | */ |
| 5371 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5372 | block_prep( |
| 5373 | oparg_T *oap, |
| 5374 | struct block_def *bdp, |
| 5375 | linenr_T lnum, |
| 5376 | int is_del) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5377 | { |
| 5378 | int incr = 0; |
| 5379 | char_u *pend; |
| 5380 | char_u *pstart; |
| 5381 | char_u *line; |
| 5382 | char_u *prev_pstart; |
| 5383 | char_u *prev_pend; |
| 5384 | |
| 5385 | bdp->startspaces = 0; |
| 5386 | bdp->endspaces = 0; |
| 5387 | bdp->textlen = 0; |
| 5388 | bdp->start_vcol = 0; |
| 5389 | bdp->end_vcol = 0; |
| 5390 | #ifdef FEAT_VISUALEXTRA |
| 5391 | bdp->is_short = FALSE; |
| 5392 | bdp->is_oneChar = FALSE; |
| 5393 | bdp->pre_whitesp = 0; |
| 5394 | bdp->pre_whitesp_c = 0; |
| 5395 | bdp->end_char_vcols = 0; |
| 5396 | #endif |
| 5397 | bdp->start_char_vcols = 0; |
| 5398 | |
| 5399 | line = ml_get(lnum); |
| 5400 | pstart = line; |
| 5401 | prev_pstart = line; |
| 5402 | while (bdp->start_vcol < oap->start_vcol && *pstart) |
| 5403 | { |
| 5404 | /* Count a tab for what it's worth (if list mode not on) */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 5405 | incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5406 | bdp->start_vcol += incr; |
| 5407 | #ifdef FEAT_VISUALEXTRA |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 5408 | if (VIM_ISWHITE(*pstart)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5409 | { |
| 5410 | bdp->pre_whitesp += incr; |
| 5411 | bdp->pre_whitesp_c++; |
| 5412 | } |
| 5413 | else |
| 5414 | { |
| 5415 | bdp->pre_whitesp = 0; |
| 5416 | bdp->pre_whitesp_c = 0; |
| 5417 | } |
| 5418 | #endif |
| 5419 | prev_pstart = pstart; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5420 | MB_PTR_ADV(pstart); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5421 | } |
| 5422 | bdp->start_char_vcols = incr; |
| 5423 | if (bdp->start_vcol < oap->start_vcol) /* line too short */ |
| 5424 | { |
| 5425 | bdp->end_vcol = bdp->start_vcol; |
| 5426 | #ifdef FEAT_VISUALEXTRA |
| 5427 | bdp->is_short = TRUE; |
| 5428 | #endif |
| 5429 | if (!is_del || oap->op_type == OP_APPEND) |
| 5430 | bdp->endspaces = oap->end_vcol - oap->start_vcol + 1; |
| 5431 | } |
| 5432 | else |
| 5433 | { |
| 5434 | /* notice: this converts partly selected Multibyte characters to |
| 5435 | * spaces, too. */ |
| 5436 | bdp->startspaces = bdp->start_vcol - oap->start_vcol; |
| 5437 | if (is_del && bdp->startspaces) |
| 5438 | bdp->startspaces = bdp->start_char_vcols - bdp->startspaces; |
| 5439 | pend = pstart; |
| 5440 | bdp->end_vcol = bdp->start_vcol; |
| 5441 | if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */ |
| 5442 | { |
| 5443 | #ifdef FEAT_VISUALEXTRA |
| 5444 | bdp->is_oneChar = TRUE; |
| 5445 | #endif |
| 5446 | if (oap->op_type == OP_INSERT) |
| 5447 | bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; |
| 5448 | else if (oap->op_type == OP_APPEND) |
| 5449 | { |
| 5450 | bdp->startspaces += oap->end_vcol - oap->start_vcol + 1; |
| 5451 | bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; |
| 5452 | } |
| 5453 | else |
| 5454 | { |
| 5455 | bdp->startspaces = oap->end_vcol - oap->start_vcol + 1; |
| 5456 | if (is_del && oap->op_type != OP_LSHIFT) |
| 5457 | { |
| 5458 | /* just putting the sum of those two into |
| 5459 | * bdp->startspaces doesn't work for Visual replace, |
| 5460 | * so we have to split the tab in two */ |
| 5461 | bdp->startspaces = bdp->start_char_vcols |
| 5462 | - (bdp->start_vcol - oap->start_vcol); |
| 5463 | bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; |
| 5464 | } |
| 5465 | } |
| 5466 | } |
| 5467 | else |
| 5468 | { |
| 5469 | prev_pend = pend; |
| 5470 | while (bdp->end_vcol <= oap->end_vcol && *pend != NUL) |
| 5471 | { |
| 5472 | /* Count a tab for what it's worth (if list mode not on) */ |
| 5473 | prev_pend = pend; |
Bram Moolenaar | 1dc9233 | 2015-01-27 13:22:20 +0100 | [diff] [blame] | 5474 | incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5475 | bdp->end_vcol += incr; |
| 5476 | } |
| 5477 | if (bdp->end_vcol <= oap->end_vcol |
| 5478 | && (!is_del |
| 5479 | || oap->op_type == OP_APPEND |
| 5480 | || oap->op_type == OP_REPLACE)) /* line too short */ |
| 5481 | { |
| 5482 | #ifdef FEAT_VISUALEXTRA |
| 5483 | bdp->is_short = TRUE; |
| 5484 | #endif |
| 5485 | /* Alternative: include spaces to fill up the block. |
| 5486 | * Disadvantage: can lead to trailing spaces when the line is |
| 5487 | * short where the text is put */ |
| 5488 | /* if (!is_del || oap->op_type == OP_APPEND) */ |
| 5489 | if (oap->op_type == OP_APPEND || virtual_op) |
| 5490 | bdp->endspaces = oap->end_vcol - bdp->end_vcol |
Bram Moolenaar | 2c7a29c | 2005-12-12 22:02:31 +0000 | [diff] [blame] | 5491 | + oap->inclusive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5492 | else |
| 5493 | bdp->endspaces = 0; /* replace doesn't add characters */ |
| 5494 | } |
| 5495 | else if (bdp->end_vcol > oap->end_vcol) |
| 5496 | { |
| 5497 | bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; |
| 5498 | if (!is_del && bdp->endspaces) |
| 5499 | { |
| 5500 | bdp->endspaces = incr - bdp->endspaces; |
| 5501 | if (pend != pstart) |
| 5502 | pend = prev_pend; |
| 5503 | } |
| 5504 | } |
| 5505 | } |
| 5506 | #ifdef FEAT_VISUALEXTRA |
| 5507 | bdp->end_char_vcols = incr; |
| 5508 | #endif |
| 5509 | if (is_del && bdp->startspaces) |
| 5510 | pstart = prev_pstart; |
| 5511 | bdp->textlen = (int)(pend - pstart); |
| 5512 | } |
| 5513 | bdp->textcol = (colnr_T) (pstart - line); |
| 5514 | bdp->textstart = pstart; |
| 5515 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5516 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5517 | /* |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5518 | * Handle the add/subtract operator. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5519 | */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5520 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5521 | op_addsub( |
| 5522 | oparg_T *oap, |
| 5523 | linenr_T Prenum1, /* Amount of add/subtract */ |
| 5524 | int g_cmd) /* was g<c-a>/g<c-x> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5525 | { |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5526 | pos_T pos; |
| 5527 | struct block_def bd; |
| 5528 | int change_cnt = 0; |
| 5529 | linenr_T amount = Prenum1; |
| 5530 | |
| 5531 | if (!VIsual_active) |
| 5532 | { |
| 5533 | pos = curwin->w_cursor; |
| 5534 | if (u_save_cursor() == FAIL) |
| 5535 | return; |
| 5536 | change_cnt = do_addsub(oap->op_type, &pos, 0, amount); |
| 5537 | if (change_cnt) |
| 5538 | changed_lines(pos.lnum, 0, pos.lnum + 1, 0L); |
| 5539 | } |
| 5540 | else |
| 5541 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5542 | int one_change; |
| 5543 | int length; |
| 5544 | pos_T startpos; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5545 | |
| 5546 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 5547 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 5548 | return; |
| 5549 | |
| 5550 | pos = oap->start; |
| 5551 | for (; pos.lnum <= oap->end.lnum; ++pos.lnum) |
| 5552 | { |
| 5553 | if (oap->block_mode) /* Visual block mode */ |
| 5554 | { |
| 5555 | block_prep(oap, &bd, pos.lnum, FALSE); |
| 5556 | pos.col = bd.textcol; |
| 5557 | length = bd.textlen; |
| 5558 | } |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5559 | else if (oap->motion_type == MLINE) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5560 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5561 | curwin->w_cursor.col = 0; |
| 5562 | pos.col = 0; |
| 5563 | length = (colnr_T)STRLEN(ml_get(pos.lnum)); |
| 5564 | } |
| 5565 | else /* oap->motion_type == MCHAR */ |
| 5566 | { |
Bram Moolenaar | 5fe6bdf | 2017-12-05 17:22:12 +0100 | [diff] [blame] | 5567 | if (pos.lnum == oap->start.lnum && !oap->inclusive) |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5568 | dec(&(oap->end)); |
| 5569 | length = (colnr_T)STRLEN(ml_get(pos.lnum)); |
| 5570 | pos.col = 0; |
| 5571 | if (pos.lnum == oap->start.lnum) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5572 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5573 | pos.col += oap->start.col; |
| 5574 | length -= oap->start.col; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5575 | } |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5576 | if (pos.lnum == oap->end.lnum) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5577 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5578 | length = (int)STRLEN(ml_get(oap->end.lnum)); |
| 5579 | if (oap->end.col >= length) |
| 5580 | oap->end.col = length - 1; |
| 5581 | length = oap->end.col - pos.col + 1; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5582 | } |
| 5583 | } |
| 5584 | one_change = do_addsub(oap->op_type, &pos, length, amount); |
| 5585 | if (one_change) |
| 5586 | { |
| 5587 | /* Remember the start position of the first change. */ |
| 5588 | if (change_cnt == 0) |
| 5589 | startpos = curbuf->b_op_start; |
| 5590 | ++change_cnt; |
| 5591 | } |
| 5592 | |
| 5593 | #ifdef FEAT_NETBEANS_INTG |
| 5594 | if (netbeans_active() && one_change) |
| 5595 | { |
| 5596 | char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 5597 | |
| 5598 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)length); |
| 5599 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
| 5600 | &ptr[pos.col], length); |
| 5601 | } |
| 5602 | #endif |
| 5603 | if (g_cmd && one_change) |
| 5604 | amount += Prenum1; |
| 5605 | } |
| 5606 | if (change_cnt) |
| 5607 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
| 5608 | |
| 5609 | if (!change_cnt && oap->is_VIsual) |
| 5610 | /* No change: need to remove the Visual selection */ |
| 5611 | redraw_curbuf_later(INVERTED); |
| 5612 | |
| 5613 | /* Set '[ mark if something changed. Keep the last end |
| 5614 | * position from do_addsub(). */ |
| 5615 | if (change_cnt > 0) |
| 5616 | curbuf->b_op_start = startpos; |
| 5617 | |
| 5618 | if (change_cnt > p_report) |
| 5619 | { |
| 5620 | if (change_cnt == 1) |
| 5621 | MSG(_("1 line changed")); |
| 5622 | else |
| 5623 | smsg((char_u *)_("%ld lines changed"), change_cnt); |
| 5624 | } |
| 5625 | } |
| 5626 | } |
| 5627 | |
| 5628 | /* |
| 5629 | * Add or subtract 'Prenum1' from a number in a line |
| 5630 | * op_type is OP_NR_ADD or OP_NR_SUB |
| 5631 | * |
| 5632 | * Returns TRUE if some character was changed. |
| 5633 | */ |
| 5634 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5635 | do_addsub( |
| 5636 | int op_type, |
| 5637 | pos_T *pos, |
| 5638 | int length, |
| 5639 | linenr_T Prenum1) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5640 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5641 | int col; |
| 5642 | char_u *buf1; |
| 5643 | char_u buf2[NUMBUFLEN]; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5644 | int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5645 | static int hexupper = FALSE; /* 0xABC */ |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 5646 | uvarnumber_T n; |
| 5647 | uvarnumber_T oldn; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5648 | char_u *ptr; |
| 5649 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5650 | int todel; |
| 5651 | int dohex; |
| 5652 | int dooct; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5653 | int dobin; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5654 | int doalp; |
| 5655 | int firstdigit; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5656 | int subtract; |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5657 | int negative = FALSE; |
Bram Moolenaar | 9bb1930 | 2015-07-03 12:44:07 +0200 | [diff] [blame] | 5658 | int was_positive = TRUE; |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5659 | int visual = VIsual_active; |
Bram Moolenaar | 3ec3261 | 2015-07-12 15:02:38 +0200 | [diff] [blame] | 5660 | int did_change = FALSE; |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5661 | pos_T save_cursor = curwin->w_cursor; |
Bram Moolenaar | 5d1bc78 | 2015-07-17 13:03:48 +0200 | [diff] [blame] | 5662 | int maxlen = 0; |
Bram Moolenaar | a52dfae | 2016-01-10 20:21:57 +0100 | [diff] [blame] | 5663 | pos_T startpos; |
| 5664 | pos_T endpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5665 | |
| 5666 | dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */ |
| 5667 | dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */ |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5668 | dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5669 | doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */ |
| 5670 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5671 | curwin->w_cursor = *pos; |
| 5672 | ptr = ml_get(pos->lnum); |
| 5673 | col = pos->col; |
| 5674 | |
| 5675 | if (*ptr == NUL) |
| 5676 | goto theend; |
| 5677 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5678 | /* |
| 5679 | * First check if we are on a hexadecimal number, after the "0x". |
| 5680 | */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5681 | if (!VIsual_active) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5682 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5683 | if (dobin) |
| 5684 | while (col > 0 && vim_isbdigit(ptr[col])) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5685 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5686 | --col; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5687 | #ifdef FEAT_MBYTE |
| 5688 | if (has_mbyte) |
| 5689 | col -= (*mb_head_off)(ptr, ptr + col); |
| 5690 | #endif |
| 5691 | } |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5692 | |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5693 | if (dohex) |
| 5694 | while (col > 0 && vim_isxdigit(ptr[col])) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5695 | { |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5696 | --col; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5697 | #ifdef FEAT_MBYTE |
| 5698 | if (has_mbyte) |
| 5699 | col -= (*mb_head_off)(ptr, ptr + col); |
| 5700 | #endif |
| 5701 | } |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5702 | |
| 5703 | if ( dobin |
| 5704 | && dohex |
| 5705 | && ! ((col > 0 |
| 5706 | && (ptr[col] == 'X' |
| 5707 | || ptr[col] == 'x') |
| 5708 | && ptr[col - 1] == '0' |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5709 | #ifdef FEAT_MBYTE |
| 5710 | && (!has_mbyte || |
| 5711 | !(*mb_head_off)(ptr, ptr + col - 1)) |
| 5712 | #endif |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5713 | && vim_isxdigit(ptr[col + 1])))) |
| 5714 | { |
| 5715 | |
| 5716 | /* In case of binary/hexadecimal pattern overlap match, rescan */ |
| 5717 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5718 | col = pos->col; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5719 | |
| 5720 | while (col > 0 && vim_isdigit(ptr[col])) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5721 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5722 | col--; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5723 | #ifdef FEAT_MBYTE |
| 5724 | if (has_mbyte) |
| 5725 | col -= (*mb_head_off)(ptr, ptr + col); |
| 5726 | #endif |
| 5727 | } |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | if (( dohex |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5731 | && col > 0 |
| 5732 | && (ptr[col] == 'X' |
| 5733 | || ptr[col] == 'x') |
| 5734 | && ptr[col - 1] == '0' |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5735 | #ifdef FEAT_MBYTE |
| 5736 | && (!has_mbyte || |
| 5737 | !(*mb_head_off)(ptr, ptr + col - 1)) |
| 5738 | #endif |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5739 | && vim_isxdigit(ptr[col + 1])) || |
| 5740 | ( dobin |
| 5741 | && col > 0 |
| 5742 | && (ptr[col] == 'B' |
| 5743 | || ptr[col] == 'b') |
| 5744 | && ptr[col - 1] == '0' |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5745 | #ifdef FEAT_MBYTE |
| 5746 | && (!has_mbyte || |
| 5747 | !(*mb_head_off)(ptr, ptr + col - 1)) |
| 5748 | #endif |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5749 | && vim_isbdigit(ptr[col + 1]))) |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5750 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5751 | /* Found hexadecimal or binary number, move to its start. */ |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5752 | --col; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5753 | #ifdef FEAT_MBYTE |
| 5754 | if (has_mbyte) |
| 5755 | col -= (*mb_head_off)(ptr, ptr + col); |
| 5756 | #endif |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5757 | } |
| 5758 | else |
| 5759 | { |
| 5760 | /* |
| 5761 | * Search forward and then backward to find the start of number. |
| 5762 | */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5763 | col = pos->col; |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5764 | |
| 5765 | while (ptr[col] != NUL |
| 5766 | && !vim_isdigit(ptr[col]) |
| 5767 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5768 | col += MB_PTR2LEN(ptr + col); |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5769 | |
| 5770 | while (col > 0 |
| 5771 | && vim_isdigit(ptr[col - 1]) |
| 5772 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5773 | { |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5774 | --col; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5775 | #ifdef FEAT_MBYTE |
| 5776 | if (has_mbyte) |
| 5777 | col -= (*mb_head_off)(ptr, ptr + col); |
| 5778 | #endif |
| 5779 | } |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5780 | } |
| 5781 | } |
| 5782 | |
Bram Moolenaar | 5d1bc78 | 2015-07-17 13:03:48 +0200 | [diff] [blame] | 5783 | if (visual) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5784 | { |
| 5785 | while (ptr[col] != NUL && length > 0 |
| 5786 | && !vim_isdigit(ptr[col]) |
| 5787 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
| 5788 | { |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5789 | int mb_len = MB_PTR2LEN(ptr + col); |
| 5790 | |
| 5791 | col += mb_len; |
| 5792 | length -= mb_len; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | if (length == 0) |
| 5796 | goto theend; |
| 5797 | |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5798 | if (col > pos->col && ptr[col - 1] == '-' |
| 5799 | #ifdef FEAT_MBYTE |
| 5800 | && (!has_mbyte || |
| 5801 | !(*mb_head_off)(ptr, ptr + col - 1)) |
| 5802 | #endif |
| 5803 | ) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5804 | { |
| 5805 | negative = TRUE; |
| 5806 | was_positive = FALSE; |
| 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | /* |
| 5811 | * If a number was found, and saving for undo works, replace the number. |
| 5812 | */ |
| 5813 | firstdigit = ptr[col]; |
| 5814 | if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit))) |
| 5815 | { |
| 5816 | beep_flush(); |
| 5817 | goto theend; |
| 5818 | } |
| 5819 | |
| 5820 | if (doalp && ASCII_ISALPHA(firstdigit)) |
| 5821 | { |
| 5822 | /* decrement or increment alphabetic character */ |
| 5823 | if (op_type == OP_NR_SUB) |
| 5824 | { |
| 5825 | if (CharOrd(firstdigit) < Prenum1) |
| 5826 | { |
| 5827 | if (isupper(firstdigit)) |
| 5828 | firstdigit = 'A'; |
| 5829 | else |
| 5830 | firstdigit = 'a'; |
| 5831 | } |
| 5832 | else |
| 5833 | #ifdef EBCDIC |
| 5834 | firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1); |
| 5835 | #else |
| 5836 | firstdigit -= Prenum1; |
| 5837 | #endif |
| 5838 | } |
| 5839 | else |
| 5840 | { |
| 5841 | if (26 - CharOrd(firstdigit) - 1 < Prenum1) |
| 5842 | { |
| 5843 | if (isupper(firstdigit)) |
| 5844 | firstdigit = 'Z'; |
| 5845 | else |
| 5846 | firstdigit = 'z'; |
| 5847 | } |
| 5848 | else |
| 5849 | #ifdef EBCDIC |
| 5850 | firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1); |
| 5851 | #else |
| 5852 | firstdigit += Prenum1; |
| 5853 | #endif |
| 5854 | } |
| 5855 | curwin->w_cursor.col = col; |
| 5856 | if (!did_change) |
| 5857 | startpos = curwin->w_cursor; |
| 5858 | did_change = TRUE; |
| 5859 | (void)del_char(FALSE); |
| 5860 | ins_char(firstdigit); |
| 5861 | endpos = curwin->w_cursor; |
| 5862 | curwin->w_cursor.col = col; |
| 5863 | } |
| 5864 | else |
| 5865 | { |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 5866 | if (col > 0 && ptr[col - 1] == '-' |
| 5867 | #ifdef FEAT_MBYTE |
| 5868 | && (!has_mbyte || |
| 5869 | !(*mb_head_off)(ptr, ptr + col - 1)) |
| 5870 | #endif |
| 5871 | && !visual) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5872 | { |
| 5873 | /* negative number */ |
| 5874 | --col; |
| 5875 | negative = TRUE; |
| 5876 | } |
| 5877 | /* get the number value (unsigned) */ |
| 5878 | if (visual && VIsual_mode != 'V') |
| 5879 | maxlen = (curbuf->b_visual.vi_curswant == MAXCOL |
| 5880 | ? (int)STRLEN(ptr) - col |
| 5881 | : length); |
| 5882 | |
| 5883 | vim_str2nr(ptr + col, &pre, &length, |
| 5884 | 0 + (dobin ? STR2NR_BIN : 0) |
| 5885 | + (dooct ? STR2NR_OCT : 0) |
| 5886 | + (dohex ? STR2NR_HEX : 0), |
| 5887 | NULL, &n, maxlen); |
| 5888 | |
| 5889 | /* ignore leading '-' for hex and octal and bin numbers */ |
| 5890 | if (pre && negative) |
| 5891 | { |
| 5892 | ++col; |
| 5893 | --length; |
| 5894 | negative = FALSE; |
| 5895 | } |
| 5896 | /* add or subtract */ |
| 5897 | subtract = FALSE; |
| 5898 | if (op_type == OP_NR_SUB) |
| 5899 | subtract ^= TRUE; |
| 5900 | if (negative) |
| 5901 | subtract ^= TRUE; |
| 5902 | |
| 5903 | oldn = n; |
| 5904 | if (subtract) |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 5905 | n -= (uvarnumber_T)Prenum1; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5906 | else |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 5907 | n += (uvarnumber_T)Prenum1; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5908 | /* handle wraparound for decimal numbers */ |
| 5909 | if (!pre) |
| 5910 | { |
| 5911 | if (subtract) |
| 5912 | { |
| 5913 | if (n > oldn) |
| 5914 | { |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 5915 | n = 1 + (n ^ (uvarnumber_T)-1); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5916 | negative ^= TRUE; |
| 5917 | } |
| 5918 | } |
| 5919 | else |
| 5920 | { |
| 5921 | /* add */ |
| 5922 | if (n < oldn) |
| 5923 | { |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 5924 | n = (n ^ (uvarnumber_T)-1); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5925 | negative ^= TRUE; |
| 5926 | } |
| 5927 | } |
| 5928 | if (n == 0) |
| 5929 | negative = FALSE; |
| 5930 | } |
| 5931 | |
| 5932 | if (visual && !was_positive && !negative && col > 0) |
| 5933 | { |
| 5934 | /* need to remove the '-' */ |
| 5935 | col--; |
| 5936 | length++; |
| 5937 | } |
| 5938 | |
| 5939 | /* |
| 5940 | * Delete the old number. |
| 5941 | */ |
| 5942 | curwin->w_cursor.col = col; |
| 5943 | if (!did_change) |
| 5944 | startpos = curwin->w_cursor; |
| 5945 | did_change = TRUE; |
| 5946 | todel = length; |
| 5947 | c = gchar_cursor(); |
| 5948 | /* |
| 5949 | * Don't include the '-' in the length, only the length of the |
| 5950 | * part after it is kept the same. |
| 5951 | */ |
| 5952 | if (c == '-') |
| 5953 | --length; |
| 5954 | while (todel-- > 0) |
| 5955 | { |
| 5956 | if (c < 0x100 && isalpha(c)) |
| 5957 | { |
| 5958 | if (isupper(c)) |
| 5959 | hexupper = TRUE; |
| 5960 | else |
| 5961 | hexupper = FALSE; |
| 5962 | } |
| 5963 | /* del_char() will mark line needing displaying */ |
| 5964 | (void)del_char(FALSE); |
| 5965 | c = gchar_cursor(); |
| 5966 | } |
| 5967 | |
| 5968 | /* |
| 5969 | * Prepare the leading characters in buf1[]. |
| 5970 | * When there are many leading zeros it could be very long. |
| 5971 | * Allocate a bit too much. |
| 5972 | */ |
| 5973 | buf1 = alloc((unsigned)length + NUMBUFLEN); |
| 5974 | if (buf1 == NULL) |
| 5975 | goto theend; |
| 5976 | ptr = buf1; |
Bram Moolenaar | dc633cf | 2016-04-23 14:33:19 +0200 | [diff] [blame] | 5977 | if (negative && (!visual || was_positive)) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5978 | { |
| 5979 | *ptr++ = '-'; |
| 5980 | } |
| 5981 | if (pre) |
| 5982 | { |
| 5983 | *ptr++ = '0'; |
| 5984 | --length; |
| 5985 | } |
| 5986 | if (pre == 'b' || pre == 'B' || |
| 5987 | pre == 'x' || pre == 'X') |
| 5988 | { |
| 5989 | *ptr++ = pre; |
| 5990 | --length; |
| 5991 | } |
| 5992 | |
| 5993 | /* |
| 5994 | * Put the number characters in buf2[]. |
| 5995 | */ |
| 5996 | if (pre == 'b' || pre == 'B') |
| 5997 | { |
| 5998 | int i; |
| 5999 | int bit = 0; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6000 | int bits = sizeof(uvarnumber_T) * 8; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 6001 | |
| 6002 | /* leading zeros */ |
| 6003 | for (bit = bits; bit > 0; bit--) |
| 6004 | if ((n >> (bit - 1)) & 0x1) break; |
| 6005 | |
| 6006 | for (i = 0; bit > 0; bit--) |
| 6007 | buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0'; |
| 6008 | |
| 6009 | buf2[i] = '\0'; |
| 6010 | } |
| 6011 | else if (pre == 0) |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 6012 | vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", |
| 6013 | (long long unsigned)n); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 6014 | else if (pre == '0') |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 6015 | vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", |
| 6016 | (long long unsigned)n); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 6017 | else if (pre && hexupper) |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 6018 | vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", |
| 6019 | (long long unsigned)n); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 6020 | else |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 6021 | vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", |
| 6022 | (long long unsigned)n); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 6023 | length -= (int)STRLEN(buf2); |
| 6024 | |
| 6025 | /* |
| 6026 | * Adjust number of zeros to the new number of digits, so the |
| 6027 | * total length of the number remains the same. |
| 6028 | * Don't do this when |
| 6029 | * the result may look like an octal number. |
| 6030 | */ |
| 6031 | if (firstdigit == '0' && !(dooct && pre == 0)) |
| 6032 | while (length-- > 0) |
| 6033 | *ptr++ = '0'; |
| 6034 | *ptr = NUL; |
| 6035 | STRCAT(buf1, buf2); |
| 6036 | ins_str(buf1); /* insert the new number */ |
| 6037 | vim_free(buf1); |
| 6038 | endpos = curwin->w_cursor; |
| 6039 | if (did_change && curwin->w_cursor.col) |
| 6040 | --curwin->w_cursor.col; |
| 6041 | } |
| 6042 | |
Bram Moolenaar | a52dfae | 2016-01-10 20:21:57 +0100 | [diff] [blame] | 6043 | if (did_change) |
| 6044 | { |
| 6045 | /* set the '[ and '] marks */ |
| 6046 | curbuf->b_op_start = startpos; |
| 6047 | curbuf->b_op_end = endpos; |
| 6048 | if (curbuf->b_op_end.col > 0) |
| 6049 | --curbuf->b_op_end.col; |
| 6050 | } |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 6051 | |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 6052 | theend: |
| 6053 | if (visual) |
| 6054 | curwin->w_cursor = save_cursor; |
Bram Moolenaar | 8e08125 | 2016-03-21 23:13:32 +0100 | [diff] [blame] | 6055 | else if (did_change) |
| 6056 | curwin->w_set_curswant = TRUE; |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 6057 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 6058 | return did_change; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | #ifdef FEAT_VIMINFO |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6062 | |
| 6063 | static yankreg_T *y_read_regs = NULL; |
| 6064 | |
| 6065 | #define REG_PREVIOUS 1 |
| 6066 | #define REG_EXEC 2 |
| 6067 | |
| 6068 | /* |
| 6069 | * Prepare for reading viminfo registers when writing viminfo later. |
| 6070 | */ |
| 6071 | void |
Bram Moolenaar | cf08946 | 2016-06-12 21:18:43 +0200 | [diff] [blame] | 6072 | prepare_viminfo_registers(void) |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6073 | { |
| 6074 | y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS |
| 6075 | * (int)sizeof(yankreg_T)); |
| 6076 | } |
| 6077 | |
| 6078 | void |
Bram Moolenaar | cf08946 | 2016-06-12 21:18:43 +0200 | [diff] [blame] | 6079 | finish_viminfo_registers(void) |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6080 | { |
| 6081 | int i; |
| 6082 | int j; |
| 6083 | |
| 6084 | if (y_read_regs != NULL) |
| 6085 | { |
| 6086 | for (i = 0; i < NUM_REGISTERS; ++i) |
| 6087 | if (y_read_regs[i].y_array != NULL) |
| 6088 | { |
| 6089 | for (j = 0; j < y_read_regs[i].y_size; j++) |
| 6090 | vim_free(y_read_regs[i].y_array[j]); |
| 6091 | vim_free(y_read_regs[i].y_array); |
| 6092 | } |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6093 | VIM_CLEAR(y_read_regs); |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6094 | } |
| 6095 | } |
| 6096 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6097 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6098 | read_viminfo_register(vir_T *virp, int force) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6099 | { |
| 6100 | int eof; |
| 6101 | int do_it = TRUE; |
| 6102 | int size; |
| 6103 | int limit; |
| 6104 | int i; |
| 6105 | int set_prev = FALSE; |
| 6106 | char_u *str; |
| 6107 | char_u **array = NULL; |
Bram Moolenaar | 7cbc703 | 2015-01-18 14:08:56 +0100 | [diff] [blame] | 6108 | int new_type = MCHAR; /* init to shut up compiler */ |
| 6109 | colnr_T new_width = 0; /* init to shut up compiler */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6110 | |
| 6111 | /* We only get here (hopefully) if line[0] == '"' */ |
| 6112 | str = virp->vir_line + 1; |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 6113 | |
| 6114 | /* If the line starts with "" this is the y_previous register. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6115 | if (*str == '"') |
| 6116 | { |
| 6117 | set_prev = TRUE; |
| 6118 | str++; |
| 6119 | } |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 6120 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6121 | if (!ASCII_ISALNUM(*str) && *str != '-') |
| 6122 | { |
| 6123 | if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line)) |
| 6124 | return TRUE; /* too many errors, pretend end-of-file */ |
| 6125 | do_it = FALSE; |
| 6126 | } |
| 6127 | get_yank_register(*str++, FALSE); |
| 6128 | if (!force && y_current->y_array != NULL) |
| 6129 | do_it = FALSE; |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 6130 | |
| 6131 | if (*str == '@') |
| 6132 | { |
| 6133 | /* "x@: register x used for @@ */ |
| 6134 | if (force || execreg_lastc == NUL) |
| 6135 | execreg_lastc = str[-1]; |
| 6136 | } |
| 6137 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6138 | size = 0; |
| 6139 | limit = 100; /* Optimized for registers containing <= 100 lines */ |
| 6140 | if (do_it) |
| 6141 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6142 | /* |
| 6143 | * Build the new register in array[]. |
| 6144 | * y_array is kept as-is until done. |
| 6145 | * The "do_it" flag is reset when something is wrong, in which case |
| 6146 | * array[] needs to be freed. |
| 6147 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6148 | if (set_prev) |
| 6149 | y_previous = y_current; |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6150 | array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *))); |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 6151 | str = skipwhite(skiptowhite(str)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6152 | if (STRNCMP(str, "CHAR", 4) == 0) |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6153 | new_type = MCHAR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6154 | else if (STRNCMP(str, "BLOCK", 5) == 0) |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6155 | new_type = MBLOCK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6156 | else |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6157 | new_type = MLINE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6158 | /* get the block width; if it's missing we get a zero, which is OK */ |
| 6159 | str = skipwhite(skiptowhite(str)); |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6160 | new_width = getdigits(&str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | while (!(eof = viminfo_readline(virp)) |
| 6164 | && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<')) |
| 6165 | { |
| 6166 | if (do_it) |
| 6167 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6168 | if (size == limit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6169 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6170 | char_u **new_array = (char_u **) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6171 | alloc((unsigned)(limit * 2 * sizeof(char_u *))); |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6172 | |
| 6173 | if (new_array == NULL) |
| 6174 | { |
| 6175 | do_it = FALSE; |
| 6176 | break; |
| 6177 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6178 | for (i = 0; i < limit; i++) |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6179 | new_array[i] = array[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6180 | vim_free(array); |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6181 | array = new_array; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6182 | limit *= 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6183 | } |
| 6184 | str = viminfo_readstring(virp, 1, TRUE); |
| 6185 | if (str != NULL) |
| 6186 | array[size++] = str; |
| 6187 | else |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6188 | /* error, don't store the result */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6189 | do_it = FALSE; |
| 6190 | } |
| 6191 | } |
Bram Moolenaar | 7cbc703 | 2015-01-18 14:08:56 +0100 | [diff] [blame] | 6192 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6193 | if (do_it) |
| 6194 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6195 | /* free y_array[] */ |
| 6196 | for (i = 0; i < y_current->y_size; i++) |
| 6197 | vim_free(y_current->y_array[i]); |
| 6198 | vim_free(y_current->y_array); |
| 6199 | |
| 6200 | y_current->y_type = new_type; |
| 6201 | y_current->y_width = new_width; |
| 6202 | y_current->y_size = size; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6203 | y_current->y_time_set = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6204 | if (size == 0) |
| 6205 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6206 | y_current->y_array = NULL; |
| 6207 | } |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6208 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6209 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6210 | /* Move the lines from array[] to y_array[]. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6211 | y_current->y_array = |
| 6212 | (char_u **)alloc((unsigned)(size * sizeof(char_u *))); |
| 6213 | for (i = 0; i < size; i++) |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6214 | { |
| 6215 | if (y_current->y_array == NULL) |
| 6216 | vim_free(array[i]); |
| 6217 | else |
| 6218 | y_current->y_array[i] = array[i]; |
| 6219 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6220 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6221 | } |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 6222 | else |
| 6223 | { |
| 6224 | /* Free array[] if it was filled. */ |
| 6225 | for (i = 0; i < size; i++) |
| 6226 | vim_free(array[i]); |
| 6227 | } |
| 6228 | vim_free(array); |
| 6229 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6230 | return eof; |
| 6231 | } |
| 6232 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6233 | /* |
| 6234 | * Accept a new style register line from the viminfo, store it when it's new. |
| 6235 | */ |
| 6236 | void |
| 6237 | handle_viminfo_register(garray_T *values, int force) |
| 6238 | { |
| 6239 | bval_T *vp = (bval_T *)values->ga_data; |
| 6240 | int flags; |
| 6241 | int name; |
| 6242 | int type; |
| 6243 | int linecount; |
| 6244 | int width; |
| 6245 | time_t timestamp; |
| 6246 | yankreg_T *y_ptr; |
| 6247 | int i; |
| 6248 | |
| 6249 | /* Check the format: |
| 6250 | * |{bartype},{flags},{name},{type}, |
| 6251 | * {linecount},{width},{timestamp},"line1","line2" |
| 6252 | */ |
| 6253 | if (values->ga_len < 6 |
| 6254 | || vp[0].bv_type != BVAL_NR |
| 6255 | || vp[1].bv_type != BVAL_NR |
| 6256 | || vp[2].bv_type != BVAL_NR |
| 6257 | || vp[3].bv_type != BVAL_NR |
| 6258 | || vp[4].bv_type != BVAL_NR |
| 6259 | || vp[5].bv_type != BVAL_NR) |
| 6260 | return; |
| 6261 | flags = vp[0].bv_nr; |
| 6262 | name = vp[1].bv_nr; |
Bram Moolenaar | 67e3720 | 2016-06-14 21:32:28 +0200 | [diff] [blame] | 6263 | if (name < 0 || name >= NUM_REGISTERS) |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6264 | return; |
| 6265 | type = vp[2].bv_nr; |
| 6266 | if (type != MCHAR && type != MLINE && type != MBLOCK) |
| 6267 | return; |
| 6268 | linecount = vp[3].bv_nr; |
| 6269 | if (values->ga_len < 6 + linecount) |
| 6270 | return; |
| 6271 | width = vp[4].bv_nr; |
| 6272 | if (width < 0) |
| 6273 | return; |
| 6274 | |
| 6275 | if (y_read_regs != NULL) |
| 6276 | /* Reading viminfo for merging and writing. Store the register |
| 6277 | * content, don't update the current registers. */ |
| 6278 | y_ptr = &y_read_regs[name]; |
| 6279 | else |
| 6280 | y_ptr = &y_regs[name]; |
| 6281 | |
| 6282 | /* Do not overwrite unless forced or the timestamp is newer. */ |
| 6283 | timestamp = (time_t)vp[5].bv_nr; |
| 6284 | if (y_ptr->y_array != NULL && !force |
| 6285 | && (timestamp == 0 || y_ptr->y_time_set > timestamp)) |
| 6286 | return; |
| 6287 | |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 6288 | if (y_ptr->y_array != NULL) |
| 6289 | for (i = 0; i < y_ptr->y_size; i++) |
| 6290 | vim_free(y_ptr->y_array[i]); |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6291 | vim_free(y_ptr->y_array); |
| 6292 | |
| 6293 | if (y_read_regs == NULL) |
| 6294 | { |
| 6295 | if (flags & REG_PREVIOUS) |
| 6296 | y_previous = y_ptr; |
| 6297 | if ((flags & REG_EXEC) && (force || execreg_lastc == NUL)) |
| 6298 | execreg_lastc = get_register_name(name); |
| 6299 | } |
| 6300 | y_ptr->y_type = type; |
| 6301 | y_ptr->y_width = width; |
| 6302 | y_ptr->y_size = linecount; |
| 6303 | y_ptr->y_time_set = timestamp; |
| 6304 | if (linecount == 0) |
| 6305 | y_ptr->y_array = NULL; |
| 6306 | else |
| 6307 | { |
| 6308 | y_ptr->y_array = |
| 6309 | (char_u **)alloc((unsigned)(linecount * sizeof(char_u *))); |
| 6310 | for (i = 0; i < linecount; i++) |
| 6311 | { |
| 6312 | if (vp[i + 6].bv_allocated) |
| 6313 | { |
| 6314 | y_ptr->y_array[i] = vp[i + 6].bv_string; |
| 6315 | vp[i + 6].bv_string = NULL; |
| 6316 | } |
| 6317 | else |
| 6318 | y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string); |
| 6319 | } |
| 6320 | } |
| 6321 | } |
| 6322 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6323 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6324 | write_viminfo_registers(FILE *fp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6325 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6326 | int i, j; |
| 6327 | char_u *type; |
| 6328 | char_u c; |
| 6329 | int num_lines; |
| 6330 | int max_num_lines; |
| 6331 | int max_kbyte; |
| 6332 | long len; |
| 6333 | yankreg_T *y_ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6334 | |
Bram Moolenaar | 6440447 | 2010-06-26 06:24:45 +0200 | [diff] [blame] | 6335 | fputs(_("\n# Registers:\n"), fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6336 | |
| 6337 | /* Get '<' value, use old '"' value if '<' is not found. */ |
| 6338 | max_num_lines = get_viminfo_parameter('<'); |
| 6339 | if (max_num_lines < 0) |
| 6340 | max_num_lines = get_viminfo_parameter('"'); |
| 6341 | if (max_num_lines == 0) |
| 6342 | return; |
| 6343 | max_kbyte = get_viminfo_parameter('s'); |
| 6344 | if (max_kbyte == 0) |
| 6345 | return; |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 6346 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6347 | for (i = 0; i < NUM_REGISTERS; i++) |
| 6348 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6349 | #ifdef FEAT_CLIPBOARD |
| 6350 | /* Skip '*'/'+' register, we don't want them back next time */ |
| 6351 | if (i == STAR_REGISTER || i == PLUS_REGISTER) |
| 6352 | continue; |
| 6353 | #endif |
| 6354 | #ifdef FEAT_DND |
| 6355 | /* Neither do we want the '~' register */ |
| 6356 | if (i == TILDE_REGISTER) |
| 6357 | continue; |
| 6358 | #endif |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6359 | /* When reading viminfo for merging and writing: Use the register from |
| 6360 | * viminfo if it's newer. */ |
| 6361 | if (y_read_regs != NULL |
| 6362 | && y_read_regs[i].y_array != NULL |
| 6363 | && (y_regs[i].y_array == NULL || |
| 6364 | y_read_regs[i].y_time_set > y_regs[i].y_time_set)) |
| 6365 | y_ptr = &y_read_regs[i]; |
| 6366 | else if (y_regs[i].y_array == NULL) |
| 6367 | continue; |
| 6368 | else |
| 6369 | y_ptr = &y_regs[i]; |
| 6370 | |
Bram Moolenaar | d7ee7ce | 2005-01-03 21:02:03 +0000 | [diff] [blame] | 6371 | /* Skip empty registers. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6372 | num_lines = y_ptr->y_size; |
Bram Moolenaar | d7ee7ce | 2005-01-03 21:02:03 +0000 | [diff] [blame] | 6373 | if (num_lines == 0 |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6374 | || (num_lines == 1 && y_ptr->y_type == MCHAR |
| 6375 | && *y_ptr->y_array[0] == NUL)) |
Bram Moolenaar | d7ee7ce | 2005-01-03 21:02:03 +0000 | [diff] [blame] | 6376 | continue; |
| 6377 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6378 | if (max_kbyte > 0) |
| 6379 | { |
| 6380 | /* Skip register if there is more text than the maximum size. */ |
| 6381 | len = 0; |
| 6382 | for (j = 0; j < num_lines; j++) |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6383 | len += (long)STRLEN(y_ptr->y_array[j]) + 1L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6384 | if (len > (long)max_kbyte * 1024L) |
| 6385 | continue; |
| 6386 | } |
| 6387 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6388 | switch (y_ptr->y_type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6389 | { |
| 6390 | case MLINE: |
| 6391 | type = (char_u *)"LINE"; |
| 6392 | break; |
| 6393 | case MCHAR: |
| 6394 | type = (char_u *)"CHAR"; |
| 6395 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6396 | case MBLOCK: |
| 6397 | type = (char_u *)"BLOCK"; |
| 6398 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6399 | default: |
| 6400 | sprintf((char *)IObuff, _("E574: Unknown register type %d"), |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6401 | y_ptr->y_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6402 | emsg(IObuff); |
| 6403 | type = (char_u *)"LINE"; |
| 6404 | break; |
| 6405 | } |
| 6406 | if (y_previous == &y_regs[i]) |
| 6407 | fprintf(fp, "\""); |
| 6408 | c = get_register_name(i); |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 6409 | fprintf(fp, "\"%c", c); |
| 6410 | if (c == execreg_lastc) |
| 6411 | fprintf(fp, "@"); |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6412 | fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6413 | |
| 6414 | /* If max_num_lines < 0, then we save ALL the lines in the register */ |
| 6415 | if (max_num_lines > 0 && num_lines > max_num_lines) |
| 6416 | num_lines = max_num_lines; |
| 6417 | for (j = 0; j < num_lines; j++) |
| 6418 | { |
| 6419 | putc('\t', fp); |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6420 | viminfo_writestring(fp, y_ptr->y_array[j]); |
| 6421 | } |
| 6422 | |
| 6423 | { |
| 6424 | int flags = 0; |
| 6425 | int remaining; |
| 6426 | |
| 6427 | /* New style with a bar line. Format: |
| 6428 | * |{bartype},{flags},{name},{type}, |
| 6429 | * {linecount},{width},{timestamp},"line1","line2" |
| 6430 | * flags: REG_PREVIOUS - register is y_previous |
Bram Moolenaar | f12519d | 2018-02-06 22:52:49 +0100 | [diff] [blame] | 6431 | * REG_EXEC - used for @@ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6432 | */ |
| 6433 | if (y_previous == &y_regs[i]) |
| 6434 | flags |= REG_PREVIOUS; |
| 6435 | if (c == execreg_lastc) |
| 6436 | flags |= REG_EXEC; |
| 6437 | fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags, |
| 6438 | i, y_ptr->y_type, num_lines, (int)y_ptr->y_width, |
| 6439 | (long)y_ptr->y_time_set); |
| 6440 | /* 11 chars for type/flags/name/type, 3 * 20 for numbers */ |
| 6441 | remaining = LSIZE - 71; |
| 6442 | for (j = 0; j < num_lines; j++) |
| 6443 | { |
| 6444 | putc(',', fp); |
| 6445 | --remaining; |
| 6446 | remaining = barline_writestring(fp, y_ptr->y_array[j], |
| 6447 | remaining); |
| 6448 | } |
| 6449 | putc('\n', fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6450 | } |
| 6451 | } |
| 6452 | } |
| 6453 | #endif /* FEAT_VIMINFO */ |
| 6454 | |
| 6455 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 6456 | /* |
| 6457 | * SELECTION / PRIMARY ('*') |
| 6458 | * |
| 6459 | * Text selection stuff that uses the GUI selection register '*'. When using a |
| 6460 | * GUI this may be text from another window, otherwise it is the last text we |
| 6461 | * had highlighted with VIsual mode. With mouse support, clicking the middle |
| 6462 | * button performs the paste, otherwise you will need to do <"*p>. " |
| 6463 | * If not under X, it is synonymous with the clipboard register '+'. |
| 6464 | * |
| 6465 | * X CLIPBOARD ('+') |
| 6466 | * |
| 6467 | * Text selection stuff that uses the GUI clipboard register '+'. |
| 6468 | * Under X, this matches the standard cut/paste buffer CLIPBOARD selection. |
| 6469 | * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed", |
| 6470 | * otherwise you will need to do <"+p>. " |
| 6471 | * If not under X, it is synonymous with the selection register '*'. |
| 6472 | */ |
| 6473 | |
| 6474 | /* |
| 6475 | * Routine to export any final X selection we had to the environment |
Bram Moolenaar | f58a847 | 2017-03-05 18:03:04 +0100 | [diff] [blame] | 6476 | * so that the text is still available after Vim has exited. X selections |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6477 | * only exist while the owning application exists, so we write to the |
| 6478 | * permanent (while X runs) store CUT_BUFFER0. |
| 6479 | * Dump the CLIPBOARD selection if we own it (it's logically the more |
| 6480 | * 'permanent' of the two), otherwise the PRIMARY one. |
| 6481 | * For now, use a hard-coded sanity limit of 1Mb of data. |
| 6482 | */ |
Bram Moolenaar | a6b7a08 | 2016-08-10 20:53:05 +0200 | [diff] [blame] | 6483 | #if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6484 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6485 | x11_export_final_selection(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6486 | { |
| 6487 | Display *dpy; |
| 6488 | char_u *str = NULL; |
| 6489 | long_u len = 0; |
| 6490 | int motion_type = -1; |
| 6491 | |
| 6492 | # ifdef FEAT_GUI |
| 6493 | if (gui.in_use) |
| 6494 | dpy = X_DISPLAY; |
| 6495 | else |
| 6496 | # endif |
| 6497 | # ifdef FEAT_XCLIPBOARD |
| 6498 | dpy = xterm_dpy; |
| 6499 | # else |
| 6500 | return; |
| 6501 | # endif |
| 6502 | |
| 6503 | /* Get selection to export */ |
| 6504 | if (clip_plus.owned) |
| 6505 | motion_type = clip_convert_selection(&str, &len, &clip_plus); |
| 6506 | else if (clip_star.owned) |
| 6507 | motion_type = clip_convert_selection(&str, &len, &clip_star); |
| 6508 | |
| 6509 | /* Check it's OK */ |
| 6510 | if (dpy != NULL && str != NULL && motion_type >= 0 |
| 6511 | && len < 1024*1024 && len > 0) |
| 6512 | { |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6513 | #ifdef FEAT_MBYTE |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6514 | int ok = TRUE; |
| 6515 | |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6516 | /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from |
| 6517 | * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit |
| 6518 | * encoding conversion usually doesn't work, so keep the text as-is. |
| 6519 | */ |
| 6520 | if (has_mbyte) |
| 6521 | { |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6522 | vimconv_T vc; |
| 6523 | |
| 6524 | vc.vc_type = CONV_NONE; |
| 6525 | if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK) |
| 6526 | { |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 6527 | int intlen = len; |
| 6528 | char_u *conv_str; |
Bram Moolenaar | 331dafd | 2009-11-25 11:38:30 +0000 | [diff] [blame] | 6529 | |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6530 | vc.vc_fail = TRUE; |
Bram Moolenaar | 331dafd | 2009-11-25 11:38:30 +0000 | [diff] [blame] | 6531 | conv_str = string_convert(&vc, str, &intlen); |
| 6532 | len = intlen; |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6533 | if (conv_str != NULL) |
| 6534 | { |
| 6535 | vim_free(str); |
| 6536 | str = conv_str; |
| 6537 | } |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6538 | else |
| 6539 | { |
| 6540 | ok = FALSE; |
| 6541 | } |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6542 | convert_setup(&vc, NULL, NULL); |
| 6543 | } |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6544 | else |
| 6545 | { |
| 6546 | ok = FALSE; |
| 6547 | } |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6548 | } |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6549 | |
| 6550 | /* Do not store the string if conversion failed. Better to use any |
| 6551 | * other selection than garbled text. */ |
| 6552 | if (ok) |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6553 | #endif |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6554 | { |
| 6555 | XStoreBuffer(dpy, (char *)str, (int)len, 0); |
| 6556 | XFlush(dpy); |
| 6557 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6558 | } |
| 6559 | |
| 6560 | vim_free(str); |
| 6561 | } |
| 6562 | #endif |
| 6563 | |
| 6564 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6565 | clip_free_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6566 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6567 | yankreg_T *y_ptr = y_current; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6568 | |
| 6569 | if (cbd == &clip_plus) |
| 6570 | y_current = &y_regs[PLUS_REGISTER]; |
| 6571 | else |
| 6572 | y_current = &y_regs[STAR_REGISTER]; |
| 6573 | free_yank_all(); |
| 6574 | y_current->y_size = 0; |
| 6575 | y_current = y_ptr; |
| 6576 | } |
| 6577 | |
| 6578 | /* |
| 6579 | * Get the selected text and put it in the gui selection register '*' or '+'. |
| 6580 | */ |
| 6581 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6582 | clip_get_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6583 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6584 | yankreg_T *old_y_previous, *old_y_current; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6585 | pos_T old_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6586 | pos_T old_visual; |
| 6587 | int old_visual_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6588 | colnr_T old_curswant; |
| 6589 | int old_set_curswant; |
| 6590 | pos_T old_op_start, old_op_end; |
| 6591 | oparg_T oa; |
| 6592 | cmdarg_T ca; |
| 6593 | |
| 6594 | if (cbd->owned) |
| 6595 | { |
| 6596 | if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL) |
| 6597 | || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL)) |
| 6598 | return; |
| 6599 | |
| 6600 | /* Get the text between clip_star.start & clip_star.end */ |
| 6601 | old_y_previous = y_previous; |
| 6602 | old_y_current = y_current; |
| 6603 | old_cursor = curwin->w_cursor; |
| 6604 | old_curswant = curwin->w_curswant; |
| 6605 | old_set_curswant = curwin->w_set_curswant; |
| 6606 | old_op_start = curbuf->b_op_start; |
| 6607 | old_op_end = curbuf->b_op_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6608 | old_visual = VIsual; |
| 6609 | old_visual_mode = VIsual_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6610 | clear_oparg(&oa); |
| 6611 | oa.regname = (cbd == &clip_plus ? '+' : '*'); |
| 6612 | oa.op_type = OP_YANK; |
| 6613 | vim_memset(&ca, 0, sizeof(ca)); |
| 6614 | ca.oap = &oa; |
| 6615 | ca.cmdchar = 'y'; |
| 6616 | ca.count1 = 1; |
| 6617 | ca.retval = CA_NO_ADJ_OP_END; |
| 6618 | do_pending_operator(&ca, 0, TRUE); |
| 6619 | y_previous = old_y_previous; |
| 6620 | y_current = old_y_current; |
| 6621 | curwin->w_cursor = old_cursor; |
Bram Moolenaar | 5c8837f | 2006-02-25 21:52:33 +0000 | [diff] [blame] | 6622 | changed_cline_bef_curs(); /* need to update w_virtcol et al */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6623 | curwin->w_curswant = old_curswant; |
| 6624 | curwin->w_set_curswant = old_set_curswant; |
| 6625 | curbuf->b_op_start = old_op_start; |
| 6626 | curbuf->b_op_end = old_op_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6627 | VIsual = old_visual; |
| 6628 | VIsual_mode = old_visual_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6629 | } |
Bram Moolenaar | 3fcfa35 | 2017-03-29 19:20:41 +0200 | [diff] [blame] | 6630 | else if (!is_clipboard_needs_update()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6631 | { |
| 6632 | clip_free_selection(cbd); |
| 6633 | |
| 6634 | /* Try to get selected text from another window */ |
| 6635 | clip_gen_request_selection(cbd); |
| 6636 | } |
| 6637 | } |
| 6638 | |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 6639 | /* |
| 6640 | * Convert from the GUI selection string into the '*'/'+' register. |
| 6641 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6642 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6643 | clip_yank_selection( |
| 6644 | int type, |
| 6645 | char_u *str, |
| 6646 | long len, |
| 6647 | VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6648 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6649 | yankreg_T *y_ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6650 | |
| 6651 | if (cbd == &clip_plus) |
| 6652 | y_ptr = &y_regs[PLUS_REGISTER]; |
| 6653 | else |
| 6654 | y_ptr = &y_regs[STAR_REGISTER]; |
| 6655 | |
| 6656 | clip_free_selection(cbd); |
| 6657 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6658 | str_to_reg(y_ptr, type, str, len, 0L, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6659 | } |
| 6660 | |
| 6661 | /* |
| 6662 | * Convert the '*'/'+' register into a GUI selection string returned in *str |
| 6663 | * with length *len. |
| 6664 | * Returns the motion type, or -1 for failure. |
| 6665 | */ |
| 6666 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6667 | clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6668 | { |
| 6669 | char_u *p; |
| 6670 | int lnum; |
| 6671 | int i, j; |
| 6672 | int_u eolsize; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6673 | yankreg_T *y_ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6674 | |
| 6675 | if (cbd == &clip_plus) |
| 6676 | y_ptr = &y_regs[PLUS_REGISTER]; |
| 6677 | else |
| 6678 | y_ptr = &y_regs[STAR_REGISTER]; |
| 6679 | |
| 6680 | #ifdef USE_CRNL |
| 6681 | eolsize = 2; |
| 6682 | #else |
| 6683 | eolsize = 1; |
| 6684 | #endif |
| 6685 | |
| 6686 | *str = NULL; |
| 6687 | *len = 0; |
| 6688 | if (y_ptr->y_array == NULL) |
| 6689 | return -1; |
| 6690 | |
| 6691 | for (i = 0; i < y_ptr->y_size; i++) |
| 6692 | *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize; |
| 6693 | |
| 6694 | /* |
| 6695 | * Don't want newline character at end of last line if we're in MCHAR mode. |
| 6696 | */ |
| 6697 | if (y_ptr->y_type == MCHAR && *len >= eolsize) |
| 6698 | *len -= eolsize; |
| 6699 | |
| 6700 | p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */ |
| 6701 | if (p == NULL) |
| 6702 | return -1; |
| 6703 | lnum = 0; |
| 6704 | for (i = 0, j = 0; i < (int)*len; i++, j++) |
| 6705 | { |
| 6706 | if (y_ptr->y_array[lnum][j] == '\n') |
| 6707 | p[i] = NUL; |
| 6708 | else if (y_ptr->y_array[lnum][j] == NUL) |
| 6709 | { |
| 6710 | #ifdef USE_CRNL |
| 6711 | p[i++] = '\r'; |
| 6712 | #endif |
| 6713 | #ifdef USE_CR |
| 6714 | p[i] = '\r'; |
| 6715 | #else |
| 6716 | p[i] = '\n'; |
| 6717 | #endif |
| 6718 | lnum++; |
| 6719 | j = -1; |
| 6720 | } |
| 6721 | else |
| 6722 | p[i] = y_ptr->y_array[lnum][j]; |
| 6723 | } |
| 6724 | return y_ptr->y_type; |
| 6725 | } |
| 6726 | |
| 6727 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6728 | /* |
| 6729 | * If we have written to a clipboard register, send the text to the clipboard. |
| 6730 | */ |
| 6731 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6732 | may_set_selection(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6733 | { |
| 6734 | if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available) |
| 6735 | { |
| 6736 | clip_own_selection(&clip_star); |
| 6737 | clip_gen_set_selection(&clip_star); |
| 6738 | } |
| 6739 | else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available) |
| 6740 | { |
| 6741 | clip_own_selection(&clip_plus); |
| 6742 | clip_gen_set_selection(&clip_plus); |
| 6743 | } |
| 6744 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6745 | |
| 6746 | #endif /* FEAT_CLIPBOARD || PROTO */ |
| 6747 | |
| 6748 | |
| 6749 | #if defined(FEAT_DND) || defined(PROTO) |
| 6750 | /* |
| 6751 | * Replace the contents of the '~' register with str. |
| 6752 | */ |
| 6753 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6754 | dnd_yank_drag_data(char_u *str, long len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6755 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6756 | yankreg_T *curr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6757 | |
| 6758 | curr = y_current; |
| 6759 | y_current = &y_regs[TILDE_REGISTER]; |
| 6760 | free_yank_all(); |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6761 | str_to_reg(y_current, MCHAR, str, len, 0L, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6762 | y_current = curr; |
| 6763 | } |
| 6764 | #endif |
| 6765 | |
| 6766 | |
| 6767 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6768 | /* |
| 6769 | * Return the type of a register. |
| 6770 | * Used for getregtype() |
| 6771 | * Returns MAUTO for error. |
| 6772 | */ |
| 6773 | char_u |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6774 | get_reg_type(int regname, long *reglen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6775 | { |
| 6776 | switch (regname) |
| 6777 | { |
| 6778 | case '%': /* file name */ |
| 6779 | case '#': /* alternate file name */ |
| 6780 | case '=': /* expression */ |
| 6781 | case ':': /* last command line */ |
| 6782 | case '/': /* last search-pattern */ |
| 6783 | case '.': /* last inserted text */ |
| 6784 | #ifdef FEAT_SEARCHPATH |
| 6785 | case Ctrl_F: /* Filename under cursor */ |
| 6786 | case Ctrl_P: /* Path under cursor, expand via "path" */ |
| 6787 | #endif |
| 6788 | case Ctrl_W: /* word under cursor */ |
| 6789 | case Ctrl_A: /* WORD (mnemonic All) under cursor */ |
| 6790 | case '_': /* black hole: always empty */ |
| 6791 | return MCHAR; |
| 6792 | } |
| 6793 | |
| 6794 | #ifdef FEAT_CLIPBOARD |
| 6795 | regname = may_get_selection(regname); |
| 6796 | #endif |
| 6797 | |
Bram Moolenaar | 32b9201 | 2014-01-14 12:33:36 +0100 | [diff] [blame] | 6798 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6799 | return MAUTO; |
Bram Moolenaar | 32b9201 | 2014-01-14 12:33:36 +0100 | [diff] [blame] | 6800 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6801 | get_yank_register(regname, FALSE); |
| 6802 | |
| 6803 | if (y_current->y_array != NULL) |
| 6804 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6805 | if (reglen != NULL && y_current->y_type == MBLOCK) |
| 6806 | *reglen = y_current->y_width; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6807 | return y_current->y_type; |
| 6808 | } |
| 6809 | return MAUTO; |
| 6810 | } |
| 6811 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 6812 | static char_u *getreg_wrap_one_line(char_u *s, int flags); |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6813 | |
| 6814 | /* |
| 6815 | * When "flags" has GREG_LIST return a list with text "s". |
| 6816 | * Otherwise just return "s". |
| 6817 | */ |
| 6818 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6819 | getreg_wrap_one_line(char_u *s, int flags) |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6820 | { |
| 6821 | if (flags & GREG_LIST) |
| 6822 | { |
| 6823 | list_T *list = list_alloc(); |
| 6824 | |
| 6825 | if (list != NULL) |
| 6826 | { |
| 6827 | if (list_append_string(list, NULL, -1) == FAIL) |
| 6828 | { |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 6829 | list_free(list); |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6830 | return NULL; |
| 6831 | } |
| 6832 | list->lv_first->li_tv.vval.v_string = s; |
| 6833 | } |
| 6834 | return (char_u *)list; |
| 6835 | } |
| 6836 | return s; |
| 6837 | } |
| 6838 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6839 | /* |
| 6840 | * Return the contents of a register as a single allocated string. |
| 6841 | * Used for "@r" in expressions and for getreg(). |
| 6842 | * Returns NULL for error. |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6843 | * Flags: |
| 6844 | * GREG_NO_EXPR Do not allow expression register |
| 6845 | * GREG_EXPR_SRC For the expression register: return expression itself, |
| 6846 | * not the result of its evaluation. |
| 6847 | * GREG_LIST Return a list of lines in place of a single string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6848 | */ |
| 6849 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6850 | get_reg_contents(int regname, int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6851 | { |
| 6852 | long i; |
| 6853 | char_u *retval; |
| 6854 | int allocated; |
| 6855 | long len; |
| 6856 | |
| 6857 | /* Don't allow using an expression register inside an expression */ |
| 6858 | if (regname == '=') |
| 6859 | { |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6860 | if (flags & GREG_NO_EXPR) |
| 6861 | return NULL; |
| 6862 | if (flags & GREG_EXPR_SRC) |
| 6863 | return getreg_wrap_one_line(get_expr_line_src(), flags); |
| 6864 | return getreg_wrap_one_line(get_expr_line(), flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
| 6867 | if (regname == '@') /* "@@" is used for unnamed register */ |
| 6868 | regname = '"'; |
| 6869 | |
| 6870 | /* check for valid regname */ |
| 6871 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
| 6872 | return NULL; |
| 6873 | |
| 6874 | #ifdef FEAT_CLIPBOARD |
| 6875 | regname = may_get_selection(regname); |
| 6876 | #endif |
| 6877 | |
| 6878 | if (get_spec_reg(regname, &retval, &allocated, FALSE)) |
| 6879 | { |
| 6880 | if (retval == NULL) |
| 6881 | return NULL; |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6882 | if (allocated) |
| 6883 | return getreg_wrap_one_line(retval, flags); |
| 6884 | return getreg_wrap_one_line(vim_strsave(retval), flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6885 | } |
| 6886 | |
| 6887 | get_yank_register(regname, FALSE); |
| 6888 | if (y_current->y_array == NULL) |
| 6889 | return NULL; |
| 6890 | |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6891 | if (flags & GREG_LIST) |
| 6892 | { |
| 6893 | list_T *list = list_alloc(); |
| 6894 | int error = FALSE; |
| 6895 | |
| 6896 | if (list == NULL) |
| 6897 | return NULL; |
| 6898 | for (i = 0; i < y_current->y_size; ++i) |
| 6899 | if (list_append_string(list, y_current->y_array[i], -1) == FAIL) |
| 6900 | error = TRUE; |
| 6901 | if (error) |
| 6902 | { |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 6903 | list_free(list); |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6904 | return NULL; |
| 6905 | } |
| 6906 | return (char_u *)list; |
| 6907 | } |
| 6908 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6909 | /* |
| 6910 | * Compute length of resulting string. |
| 6911 | */ |
| 6912 | len = 0; |
| 6913 | for (i = 0; i < y_current->y_size; ++i) |
| 6914 | { |
| 6915 | len += (long)STRLEN(y_current->y_array[i]); |
| 6916 | /* |
| 6917 | * Insert a newline between lines and after last line if |
| 6918 | * y_type is MLINE. |
| 6919 | */ |
| 6920 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 6921 | ++len; |
| 6922 | } |
| 6923 | |
| 6924 | retval = lalloc(len + 1, TRUE); |
| 6925 | |
| 6926 | /* |
| 6927 | * Copy the lines of the yank register into the string. |
| 6928 | */ |
| 6929 | if (retval != NULL) |
| 6930 | { |
| 6931 | len = 0; |
| 6932 | for (i = 0; i < y_current->y_size; ++i) |
| 6933 | { |
| 6934 | STRCPY(retval + len, y_current->y_array[i]); |
| 6935 | len += (long)STRLEN(retval + len); |
| 6936 | |
| 6937 | /* |
| 6938 | * Insert a NL between lines and after the last line if y_type is |
| 6939 | * MLINE. |
| 6940 | */ |
| 6941 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 6942 | retval[len++] = '\n'; |
| 6943 | } |
| 6944 | retval[len] = NUL; |
| 6945 | } |
| 6946 | |
| 6947 | return retval; |
| 6948 | } |
| 6949 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6950 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6951 | init_write_reg( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6952 | int name, |
| 6953 | yankreg_T **old_y_previous, |
| 6954 | yankreg_T **old_y_current, |
| 6955 | int must_append, |
| 6956 | int *yank_type UNUSED) |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6957 | { |
| 6958 | if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */ |
| 6959 | { |
| 6960 | emsg_invreg(name); |
| 6961 | return FAIL; |
| 6962 | } |
| 6963 | |
| 6964 | /* Don't want to change the current (unnamed) register */ |
| 6965 | *old_y_previous = y_previous; |
| 6966 | *old_y_current = y_current; |
| 6967 | |
| 6968 | get_yank_register(name, TRUE); |
| 6969 | if (!y_append && !must_append) |
| 6970 | free_yank_all(); |
| 6971 | return OK; |
| 6972 | } |
| 6973 | |
| 6974 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6975 | finish_write_reg( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6976 | int name, |
| 6977 | yankreg_T *old_y_previous, |
| 6978 | yankreg_T *old_y_current) |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6979 | { |
| 6980 | # ifdef FEAT_CLIPBOARD |
| 6981 | /* Send text of clipboard register to the clipboard. */ |
| 6982 | may_set_selection(); |
| 6983 | # endif |
| 6984 | |
| 6985 | /* ':let @" = "val"' should change the meaning of the "" register */ |
| 6986 | if (name != '"') |
| 6987 | y_previous = old_y_previous; |
| 6988 | y_current = old_y_current; |
| 6989 | } |
| 6990 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6991 | /* |
| 6992 | * Store string "str" in register "name". |
| 6993 | * "maxlen" is the maximum number of bytes to use, -1 for all bytes. |
| 6994 | * If "must_append" is TRUE, always append to the register. Otherwise append |
| 6995 | * if "name" is an uppercase letter. |
| 6996 | * Note: "maxlen" and "must_append" don't work for the "/" register. |
| 6997 | * Careful: 'str' is modified, you may have to use a copy! |
| 6998 | * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise. |
| 6999 | */ |
| 7000 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7001 | write_reg_contents( |
| 7002 | int name, |
| 7003 | char_u *str, |
| 7004 | int maxlen, |
| 7005 | int must_append) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7006 | { |
| 7007 | write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L); |
| 7008 | } |
| 7009 | |
| 7010 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7011 | write_reg_contents_lst( |
| 7012 | int name, |
| 7013 | char_u **strings, |
| 7014 | int maxlen UNUSED, |
| 7015 | int must_append, |
| 7016 | int yank_type, |
| 7017 | long block_len) |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7018 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 7019 | yankreg_T *old_y_previous, *old_y_current; |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7020 | |
| 7021 | if (name == '/' |
| 7022 | #ifdef FEAT_EVAL |
| 7023 | || name == '=' |
| 7024 | #endif |
| 7025 | ) |
| 7026 | { |
| 7027 | char_u *s; |
| 7028 | |
| 7029 | if (strings[0] == NULL) |
| 7030 | s = (char_u *)""; |
| 7031 | else if (strings[1] != NULL) |
| 7032 | { |
| 7033 | EMSG(_("E883: search pattern and expression register may not " |
| 7034 | "contain two or more lines")); |
| 7035 | return; |
| 7036 | } |
| 7037 | else |
| 7038 | s = strings[0]; |
| 7039 | write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len); |
| 7040 | return; |
| 7041 | } |
| 7042 | |
| 7043 | if (name == '_') /* black hole: nothing to do */ |
| 7044 | return; |
| 7045 | |
| 7046 | if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, |
| 7047 | &yank_type) == FAIL) |
| 7048 | return; |
| 7049 | |
| 7050 | str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE); |
| 7051 | |
| 7052 | finish_write_reg(name, old_y_previous, old_y_current); |
| 7053 | } |
| 7054 | |
| 7055 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7056 | write_reg_contents_ex( |
| 7057 | int name, |
| 7058 | char_u *str, |
| 7059 | int maxlen, |
| 7060 | int must_append, |
| 7061 | int yank_type, |
| 7062 | long block_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7063 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 7064 | yankreg_T *old_y_previous, *old_y_current; |
| 7065 | long len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7066 | |
Bram Moolenaar | e756604 | 2005-06-17 22:00:15 +0000 | [diff] [blame] | 7067 | if (maxlen >= 0) |
| 7068 | len = maxlen; |
| 7069 | else |
| 7070 | len = (long)STRLEN(str); |
| 7071 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7072 | /* Special case: '/' search pattern */ |
| 7073 | if (name == '/') |
| 7074 | { |
| 7075 | set_last_search_pat(str, RE_SEARCH, TRUE, TRUE); |
| 7076 | return; |
| 7077 | } |
| 7078 | |
Bram Moolenaar | 3b3a949 | 2015-01-27 18:44:16 +0100 | [diff] [blame] | 7079 | if (name == '#') |
| 7080 | { |
| 7081 | buf_T *buf; |
| 7082 | |
| 7083 | if (VIM_ISDIGIT(*str)) |
| 7084 | { |
| 7085 | int num = atoi((char *)str); |
| 7086 | |
| 7087 | buf = buflist_findnr(num); |
| 7088 | if (buf == NULL) |
| 7089 | EMSGN(_(e_nobufnr), (long)num); |
| 7090 | } |
| 7091 | else |
| 7092 | buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str), |
| 7093 | TRUE, FALSE, FALSE)); |
| 7094 | if (buf == NULL) |
| 7095 | return; |
| 7096 | curwin->w_alt_fnum = buf->b_fnum; |
| 7097 | return; |
| 7098 | } |
| 7099 | |
Bram Moolenaar | e756604 | 2005-06-17 22:00:15 +0000 | [diff] [blame] | 7100 | #ifdef FEAT_EVAL |
| 7101 | if (name == '=') |
| 7102 | { |
| 7103 | char_u *p, *s; |
| 7104 | |
| 7105 | p = vim_strnsave(str, (int)len); |
| 7106 | if (p == NULL) |
| 7107 | return; |
| 7108 | if (must_append) |
| 7109 | { |
| 7110 | s = concat_str(get_expr_line_src(), p); |
| 7111 | vim_free(p); |
| 7112 | p = s; |
Bram Moolenaar | e756604 | 2005-06-17 22:00:15 +0000 | [diff] [blame] | 7113 | } |
| 7114 | set_expr_line(p); |
| 7115 | return; |
| 7116 | } |
| 7117 | #endif |
| 7118 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7119 | if (name == '_') /* black hole: nothing to do */ |
| 7120 | return; |
| 7121 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7122 | if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, |
| 7123 | &yank_type) == FAIL) |
| 7124 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7125 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7126 | str_to_reg(y_current, yank_type, str, len, block_len, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7127 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7128 | finish_write_reg(name, old_y_previous, old_y_current); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7129 | } |
| 7130 | #endif /* FEAT_EVAL */ |
| 7131 | |
| 7132 | #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) |
| 7133 | /* |
| 7134 | * Put a string into a register. When the register is not empty, the string |
| 7135 | * is appended. |
| 7136 | */ |
| 7137 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7138 | str_to_reg( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 7139 | yankreg_T *y_ptr, /* pointer to yank register */ |
| 7140 | int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */ |
| 7141 | char_u *str, /* string to put in register */ |
| 7142 | long len, /* length of string */ |
| 7143 | long blocklen, /* width of Visual block */ |
| 7144 | int str_list) /* TRUE if str is char_u ** */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7145 | { |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 7146 | int type; /* MCHAR, MLINE or MBLOCK */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7147 | int lnum; |
| 7148 | long start; |
| 7149 | long i; |
| 7150 | int extra; |
| 7151 | int newlines; /* number of lines added */ |
| 7152 | int extraline = 0; /* extra line at the end */ |
| 7153 | int append = FALSE; /* append to last line in register */ |
| 7154 | char_u *s; |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7155 | char_u **ss; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7156 | char_u **pp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7157 | long maxlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7158 | |
Bram Moolenaar | cde547a | 2009-11-17 11:43:06 +0000 | [diff] [blame] | 7159 | if (y_ptr->y_array == NULL) /* NULL means empty register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7160 | y_ptr->y_size = 0; |
| 7161 | |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 7162 | if (yank_type == MAUTO) |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7163 | type = ((str_list || (len > 0 && (str[len - 1] == NL |
| 7164 | || str[len - 1] == CAR))) |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 7165 | ? MLINE : MCHAR); |
| 7166 | else |
| 7167 | type = yank_type; |
| 7168 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7169 | /* |
| 7170 | * Count the number of lines within the string |
| 7171 | */ |
| 7172 | newlines = 0; |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7173 | if (str_list) |
| 7174 | { |
| 7175 | for (ss = (char_u **) str; *ss != NULL; ++ss) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7176 | ++newlines; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7177 | } |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7178 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7179 | { |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7180 | for (i = 0; i < len; i++) |
| 7181 | if (str[i] == '\n') |
| 7182 | ++newlines; |
| 7183 | if (type == MCHAR || len == 0 || str[len - 1] != '\n') |
| 7184 | { |
| 7185 | extraline = 1; |
| 7186 | ++newlines; /* count extra newline at the end */ |
| 7187 | } |
| 7188 | if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR) |
| 7189 | { |
| 7190 | append = TRUE; |
| 7191 | --newlines; /* uncount newline when appending first line */ |
| 7192 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7193 | } |
| 7194 | |
Bram Moolenaar | 659c94d | 2015-05-04 20:19:21 +0200 | [diff] [blame] | 7195 | /* Without any lines make the register empty. */ |
| 7196 | if (y_ptr->y_size + newlines == 0) |
| 7197 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7198 | VIM_CLEAR(y_ptr->y_array); |
Bram Moolenaar | 659c94d | 2015-05-04 20:19:21 +0200 | [diff] [blame] | 7199 | return; |
| 7200 | } |
| 7201 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7202 | /* |
| 7203 | * Allocate an array to hold the pointers to the new register lines. |
| 7204 | * If the register was not empty, move the existing lines to the new array. |
| 7205 | */ |
| 7206 | pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines) |
| 7207 | * sizeof(char_u *), TRUE); |
| 7208 | if (pp == NULL) /* out of memory */ |
| 7209 | return; |
| 7210 | for (lnum = 0; lnum < y_ptr->y_size; ++lnum) |
| 7211 | pp[lnum] = y_ptr->y_array[lnum]; |
| 7212 | vim_free(y_ptr->y_array); |
| 7213 | y_ptr->y_array = pp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7214 | maxlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7215 | |
| 7216 | /* |
| 7217 | * Find the end of each line and save it into the array. |
| 7218 | */ |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7219 | if (str_list) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7220 | { |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7221 | for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum) |
| 7222 | { |
Bram Moolenaar | 121f9bd | 2014-04-29 15:55:43 +0200 | [diff] [blame] | 7223 | i = (long)STRLEN(*ss); |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7224 | pp[lnum] = vim_strnsave(*ss, i); |
| 7225 | if (i > maxlen) |
| 7226 | maxlen = i; |
| 7227 | } |
| 7228 | } |
| 7229 | else |
| 7230 | { |
| 7231 | for (start = 0; start < len + extraline; start += i + 1) |
| 7232 | { |
| 7233 | for (i = start; i < len; ++i) /* find the end of the line */ |
| 7234 | if (str[i] == '\n') |
| 7235 | break; |
| 7236 | i -= start; /* i is now length of line */ |
| 7237 | if (i > maxlen) |
| 7238 | maxlen = i; |
| 7239 | if (append) |
| 7240 | { |
| 7241 | --lnum; |
| 7242 | extra = (int)STRLEN(y_ptr->y_array[lnum]); |
| 7243 | } |
| 7244 | else |
| 7245 | extra = 0; |
| 7246 | s = alloc((unsigned)(i + extra + 1)); |
| 7247 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7248 | break; |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 7249 | if (extra) |
| 7250 | mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra); |
| 7251 | if (append) |
| 7252 | vim_free(y_ptr->y_array[lnum]); |
| 7253 | if (i) |
| 7254 | mch_memmove(s + extra, str + start, (size_t)i); |
| 7255 | extra += i; |
| 7256 | s[extra] = NUL; |
| 7257 | y_ptr->y_array[lnum++] = s; |
| 7258 | while (--extra >= 0) |
| 7259 | { |
| 7260 | if (*s == NUL) |
| 7261 | *s = '\n'; /* replace NUL with newline */ |
| 7262 | ++s; |
| 7263 | } |
| 7264 | append = FALSE; /* only first line is appended */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7265 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7266 | } |
| 7267 | y_ptr->y_type = type; |
| 7268 | y_ptr->y_size = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7269 | if (type == MBLOCK) |
| 7270 | y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen); |
| 7271 | else |
| 7272 | y_ptr->y_width = 0; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 7273 | #ifdef FEAT_VIMINFO |
| 7274 | y_ptr->y_time_set = vim_time(); |
| 7275 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7276 | } |
| 7277 | #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */ |
| 7278 | |
| 7279 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7280 | clear_oparg(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7281 | { |
| 7282 | vim_memset(oap, 0, sizeof(oparg_T)); |
| 7283 | } |
| 7284 | |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7285 | static varnumber_T line_count_info(char_u *line, varnumber_T *wc, varnumber_T *cc, varnumber_T limit, int eol_size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7286 | |
| 7287 | /* |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7288 | * Count the number of bytes, characters and "words" in a line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7289 | * |
| 7290 | * "Words" are counted by looking for boundaries between non-space and |
| 7291 | * space characters. (it seems to produce results that match 'wc'.) |
| 7292 | * |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7293 | * Return value is byte count; word count for the line is added to "*wc". |
| 7294 | * Char count is added to "*cc". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7295 | * |
| 7296 | * The function will only examine the first "limit" characters in the |
| 7297 | * line, stopping if it encounters an end-of-line (NUL byte). In that |
| 7298 | * case, eol_size will be added to the character count to account for |
| 7299 | * the size of the EOL character. |
| 7300 | */ |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7301 | static varnumber_T |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7302 | line_count_info( |
| 7303 | char_u *line, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7304 | varnumber_T *wc, |
| 7305 | varnumber_T *cc, |
| 7306 | varnumber_T limit, |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7307 | int eol_size) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7308 | { |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7309 | varnumber_T i; |
| 7310 | varnumber_T words = 0; |
| 7311 | varnumber_T chars = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7312 | int is_word = 0; |
| 7313 | |
Bram Moolenaar | 88b1ba1 | 2012-06-29 13:34:19 +0200 | [diff] [blame] | 7314 | for (i = 0; i < limit && line[i] != NUL; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7315 | { |
| 7316 | if (is_word) |
| 7317 | { |
| 7318 | if (vim_isspace(line[i])) |
| 7319 | { |
| 7320 | words++; |
| 7321 | is_word = 0; |
| 7322 | } |
| 7323 | } |
| 7324 | else if (!vim_isspace(line[i])) |
| 7325 | is_word = 1; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7326 | ++chars; |
| 7327 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7328 | i += (*mb_ptr2len)(line + i); |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7329 | #else |
| 7330 | ++i; |
| 7331 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7332 | } |
| 7333 | |
| 7334 | if (is_word) |
| 7335 | words++; |
| 7336 | *wc += words; |
| 7337 | |
| 7338 | /* Add eol_size if the end of line was reached before hitting limit. */ |
Bram Moolenaar | 1cb7e09 | 2011-08-10 12:11:01 +0200 | [diff] [blame] | 7339 | if (i < limit && line[i] == NUL) |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7340 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7341 | i += eol_size; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7342 | chars += eol_size; |
| 7343 | } |
| 7344 | *cc += chars; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7345 | return i; |
| 7346 | } |
| 7347 | |
| 7348 | /* |
| 7349 | * Give some info about the position of the cursor (for "g CTRL-G"). |
| 7350 | * In Visual mode, give some info about the selected region. (In this case, |
| 7351 | * the *_count_cursor variables store running totals for the selection.) |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7352 | * When "dict" is not NULL store the info there instead of showing it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7353 | */ |
| 7354 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7355 | cursor_pos_info(dict_T *dict) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7356 | { |
| 7357 | char_u *p; |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 7358 | char_u buf1[50]; |
| 7359 | char_u buf2[40]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7360 | linenr_T lnum; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7361 | varnumber_T byte_count = 0; |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7362 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7363 | varnumber_T bom_count = 0; |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7364 | #endif |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7365 | varnumber_T byte_count_cursor = 0; |
| 7366 | varnumber_T char_count = 0; |
| 7367 | varnumber_T char_count_cursor = 0; |
| 7368 | varnumber_T word_count = 0; |
| 7369 | varnumber_T word_count_cursor = 0; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7370 | int eol_size; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7371 | varnumber_T last_check = 100000L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7372 | long line_count_selected = 0; |
| 7373 | pos_T min_pos, max_pos; |
| 7374 | oparg_T oparg; |
| 7375 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7376 | |
| 7377 | /* |
| 7378 | * Compute the length of the file in characters. |
| 7379 | */ |
| 7380 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 7381 | { |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7382 | if (dict == NULL) |
| 7383 | { |
| 7384 | MSG(_(no_lines_msg)); |
| 7385 | return; |
| 7386 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7387 | } |
| 7388 | else |
| 7389 | { |
| 7390 | if (get_fileformat(curbuf) == EOL_DOS) |
| 7391 | eol_size = 2; |
| 7392 | else |
| 7393 | eol_size = 1; |
| 7394 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7395 | if (VIsual_active) |
| 7396 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 7397 | if (LT_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7398 | { |
| 7399 | min_pos = VIsual; |
| 7400 | max_pos = curwin->w_cursor; |
| 7401 | } |
| 7402 | else |
| 7403 | { |
| 7404 | min_pos = curwin->w_cursor; |
| 7405 | max_pos = VIsual; |
| 7406 | } |
| 7407 | if (*p_sel == 'e' && max_pos.col > 0) |
| 7408 | --max_pos.col; |
| 7409 | |
| 7410 | if (VIsual_mode == Ctrl_V) |
| 7411 | { |
Bram Moolenaar | 81d0007 | 2009-04-29 15:41:40 +0000 | [diff] [blame] | 7412 | #ifdef FEAT_LINEBREAK |
| 7413 | char_u * saved_sbr = p_sbr; |
| 7414 | |
| 7415 | /* Make 'sbr' empty for a moment to get the correct size. */ |
| 7416 | p_sbr = empty_option; |
| 7417 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7418 | oparg.is_VIsual = 1; |
| 7419 | oparg.block_mode = TRUE; |
| 7420 | oparg.op_type = OP_NOP; |
| 7421 | getvcols(curwin, &min_pos, &max_pos, |
Bram Moolenaar | 5c8837f | 2006-02-25 21:52:33 +0000 | [diff] [blame] | 7422 | &oparg.start_vcol, &oparg.end_vcol); |
Bram Moolenaar | 81d0007 | 2009-04-29 15:41:40 +0000 | [diff] [blame] | 7423 | #ifdef FEAT_LINEBREAK |
| 7424 | p_sbr = saved_sbr; |
| 7425 | #endif |
Bram Moolenaar | 5c8837f | 2006-02-25 21:52:33 +0000 | [diff] [blame] | 7426 | if (curwin->w_curswant == MAXCOL) |
| 7427 | oparg.end_vcol = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7428 | /* Swap the start, end vcol if needed */ |
| 7429 | if (oparg.end_vcol < oparg.start_vcol) |
| 7430 | { |
| 7431 | oparg.end_vcol += oparg.start_vcol; |
| 7432 | oparg.start_vcol = oparg.end_vcol - oparg.start_vcol; |
| 7433 | oparg.end_vcol -= oparg.start_vcol; |
| 7434 | } |
| 7435 | } |
| 7436 | line_count_selected = max_pos.lnum - min_pos.lnum + 1; |
| 7437 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7438 | |
| 7439 | for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) |
| 7440 | { |
| 7441 | /* Check for a CTRL-C every 100000 characters. */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7442 | if (byte_count > last_check) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7443 | { |
| 7444 | ui_breakcheck(); |
| 7445 | if (got_int) |
| 7446 | return; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7447 | last_check = byte_count + 100000L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7448 | } |
| 7449 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7450 | /* Do extra processing for VIsual mode. */ |
| 7451 | if (VIsual_active |
| 7452 | && lnum >= min_pos.lnum && lnum <= max_pos.lnum) |
| 7453 | { |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7454 | char_u *s = NULL; |
| 7455 | long len = 0L; |
| 7456 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7457 | switch (VIsual_mode) |
| 7458 | { |
| 7459 | case Ctrl_V: |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7460 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7461 | virtual_op = virtual_active(); |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7462 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7463 | block_prep(&oparg, &bd, lnum, 0); |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7464 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7465 | virtual_op = MAYBE; |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7466 | #endif |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7467 | s = bd.textstart; |
| 7468 | len = (long)bd.textlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7469 | break; |
| 7470 | case 'V': |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7471 | s = ml_get(lnum); |
| 7472 | len = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7473 | break; |
| 7474 | case 'v': |
| 7475 | { |
| 7476 | colnr_T start_col = (lnum == min_pos.lnum) |
| 7477 | ? min_pos.col : 0; |
| 7478 | colnr_T end_col = (lnum == max_pos.lnum) |
| 7479 | ? max_pos.col - start_col + 1 : MAXCOL; |
| 7480 | |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7481 | s = ml_get(lnum) + start_col; |
| 7482 | len = end_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7483 | } |
| 7484 | break; |
| 7485 | } |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7486 | if (s != NULL) |
| 7487 | { |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7488 | byte_count_cursor += line_count_info(s, &word_count_cursor, |
| 7489 | &char_count_cursor, len, eol_size); |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7490 | if (lnum == curbuf->b_ml.ml_line_count |
| 7491 | && !curbuf->b_p_eol |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 7492 | && (curbuf->b_p_bin || !curbuf->b_p_fixeol) |
Bram Moolenaar | ec2dad6 | 2005-01-02 11:36:03 +0000 | [diff] [blame] | 7493 | && (long)STRLEN(s) < len) |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7494 | byte_count_cursor -= eol_size; |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7495 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7496 | } |
| 7497 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7498 | { |
| 7499 | /* In non-visual mode, check for the line the cursor is on */ |
| 7500 | if (lnum == curwin->w_cursor.lnum) |
| 7501 | { |
| 7502 | word_count_cursor += word_count; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7503 | char_count_cursor += char_count; |
| 7504 | byte_count_cursor = byte_count + |
| 7505 | line_count_info(ml_get(lnum), |
| 7506 | &word_count_cursor, &char_count_cursor, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7507 | (varnumber_T)(curwin->w_cursor.col + 1), |
| 7508 | eol_size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7509 | } |
| 7510 | } |
| 7511 | /* Add to the running totals */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7512 | byte_count += line_count_info(ml_get(lnum), &word_count, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7513 | &char_count, (varnumber_T)MAXCOL, |
| 7514 | eol_size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7515 | } |
| 7516 | |
| 7517 | /* Correction for when last line doesn't have an EOL. */ |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 7518 | if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol)) |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7519 | byte_count -= eol_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7520 | |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7521 | if (dict == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7522 | { |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7523 | if (VIsual_active) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7524 | { |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7525 | if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL) |
| 7526 | { |
| 7527 | getvcols(curwin, &min_pos, &max_pos, &min_pos.col, |
| 7528 | &max_pos.col); |
| 7529 | vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "), |
| 7530 | (long)(oparg.end_vcol - oparg.start_vcol + 1)); |
| 7531 | } |
| 7532 | else |
| 7533 | buf1[0] = NUL; |
| 7534 | |
| 7535 | if (char_count_cursor == byte_count_cursor |
| 7536 | && char_count == byte_count) |
| 7537 | vim_snprintf((char *)IObuff, IOSIZE, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7538 | _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"), |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7539 | buf1, line_count_selected, |
| 7540 | (long)curbuf->b_ml.ml_line_count, |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 7541 | (long long)word_count_cursor, |
| 7542 | (long long)word_count, |
| 7543 | (long long)byte_count_cursor, |
| 7544 | (long long)byte_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7545 | else |
| 7546 | vim_snprintf((char *)IObuff, IOSIZE, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7547 | _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"), |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7548 | buf1, line_count_selected, |
| 7549 | (long)curbuf->b_ml.ml_line_count, |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 7550 | (long long)word_count_cursor, |
| 7551 | (long long)word_count, |
| 7552 | (long long)char_count_cursor, |
| 7553 | (long long)char_count, |
| 7554 | (long long)byte_count_cursor, |
| 7555 | (long long)byte_count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7556 | } |
| 7557 | else |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7558 | { |
| 7559 | p = ml_get_curline(); |
| 7560 | validate_virtcol(); |
| 7561 | col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1, |
| 7562 | (int)curwin->w_virtcol + 1); |
| 7563 | col_print(buf2, sizeof(buf2), (int)STRLEN(p), |
| 7564 | linetabsize(p)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7565 | |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7566 | if (char_count_cursor == byte_count_cursor |
| 7567 | && char_count == byte_count) |
| 7568 | vim_snprintf((char *)IObuff, IOSIZE, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7569 | _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"), |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7570 | (char *)buf1, (char *)buf2, |
| 7571 | (long)curwin->w_cursor.lnum, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7572 | (long)curbuf->b_ml.ml_line_count, |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 7573 | (long long)word_count_cursor, (long long)word_count, |
| 7574 | (long long)byte_count_cursor, (long long)byte_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7575 | else |
| 7576 | vim_snprintf((char *)IObuff, IOSIZE, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7577 | _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"), |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7578 | (char *)buf1, (char *)buf2, |
| 7579 | (long)curwin->w_cursor.lnum, |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7580 | (long)curbuf->b_ml.ml_line_count, |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 7581 | (long long)word_count_cursor, (long long)word_count, |
| 7582 | (long long)char_count_cursor, (long long)char_count, |
| 7583 | (long long)byte_count_cursor, (long long)byte_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7584 | } |
| 7585 | } |
| 7586 | |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7587 | #ifdef FEAT_MBYTE |
| 7588 | bom_count = bomb_size(); |
| 7589 | if (bom_count > 0) |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7590 | vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE, |
Bram Moolenaar | 672afb9 | 2018-04-08 16:34:22 +0200 | [diff] [blame] | 7591 | _("(+%lld for BOM)"), (long long)bom_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7592 | #endif |
| 7593 | if (dict == NULL) |
| 7594 | { |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7595 | /* Don't shorten this message, the user asked for it. */ |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7596 | p = p_shm; |
| 7597 | p_shm = (char_u *)""; |
| 7598 | msg(IObuff); |
| 7599 | p_shm = p; |
| 7600 | } |
| 7601 | } |
Bram Moolenaar | 718272a | 2016-01-03 22:56:45 +0100 | [diff] [blame] | 7602 | #if defined(FEAT_EVAL) |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7603 | if (dict != NULL) |
| 7604 | { |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7605 | dict_add_nr_str(dict, "words", word_count, NULL); |
| 7606 | dict_add_nr_str(dict, "chars", char_count, NULL); |
| 7607 | dict_add_nr_str(dict, "bytes", byte_count |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7608 | # ifdef FEAT_MBYTE |
| 7609 | + bom_count |
| 7610 | # endif |
| 7611 | , NULL); |
| 7612 | dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes", |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7613 | byte_count_cursor, NULL); |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7614 | dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars", |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7615 | char_count_cursor, NULL); |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7616 | dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words", |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 7617 | word_count_cursor, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7618 | } |
Bram Moolenaar | 718272a | 2016-01-03 22:56:45 +0100 | [diff] [blame] | 7619 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7620 | } |