Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * 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 | /* |
| 53 | * Each yank register is an array of pointers to lines. |
| 54 | */ |
| 55 | static struct yankreg |
| 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 | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | } y_regs[NUM_REGISTERS]; |
| 62 | |
| 63 | static struct yankreg *y_current; /* ptr to current yankreg */ |
| 64 | static int y_append; /* TRUE when appending */ |
| 65 | static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */ |
| 66 | |
| 67 | /* |
| 68 | * structure used by block_prep, op_delete and op_yank for blockwise operators |
| 69 | * also op_change, op_shift, op_insert, op_replace - AKelly |
| 70 | */ |
| 71 | struct block_def |
| 72 | { |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 73 | int startspaces; /* 'extra' cols before first char */ |
| 74 | int endspaces; /* 'extra' cols after last char */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | int textlen; /* chars in block */ |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 76 | char_u *textstart; /* pointer to 1st char (partially) in block */ |
| 77 | colnr_T textcol; /* index of chars (partially) in block */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 78 | colnr_T start_vcol; /* start col of 1st char wholly inside block */ |
| 79 | colnr_T end_vcol; /* start col of 1st char wholly after block */ |
| 80 | #ifdef FEAT_VISUALEXTRA |
| 81 | int is_short; /* TRUE if line is too short to fit in block */ |
| 82 | int is_MAX; /* TRUE if curswant==MAXCOL when starting */ |
| 83 | int is_oneChar; /* TRUE if block within one character */ |
| 84 | int pre_whitesp; /* screen cols of ws before block */ |
| 85 | int pre_whitesp_c; /* chars of ws before block */ |
| 86 | colnr_T end_char_vcols; /* number of vcols of post-block char */ |
| 87 | #endif |
| 88 | colnr_T start_char_vcols; /* number of vcols of pre-block char */ |
| 89 | }; |
| 90 | |
| 91 | #ifdef FEAT_VISUALEXTRA |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 92 | static void shift_block(oparg_T *oap, int amount); |
| 93 | 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] | 94 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 95 | static int stuff_yank(int, char_u *); |
| 96 | static void put_reedit_in_typebuf(int silent); |
| 97 | static int put_in_typebuf(char_u *s, int esc, int colon, |
| 98 | int silent); |
| 99 | static void stuffescaped(char_u *arg, int literally); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 100 | #ifdef FEAT_MBYTE |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 101 | static void mb_adjust_opend(oparg_T *oap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 102 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 103 | static void free_yank(long); |
| 104 | static void free_yank_all(void); |
| 105 | static int yank_copy_line(struct block_def *bd, long y_idx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 107 | static void copy_yank_reg(struct yankreg *reg); |
| 108 | static void may_set_selection(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 109 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 110 | static void dis_msg(char_u *p, int skip_esc); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 111 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 112 | static char_u *skip_comment(char_u *line, int process, int include_space, int *is_comment); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 113 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 114 | static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int); |
| 115 | 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] | 116 | #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 117 | static void str_to_reg(struct yankreg *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] | 118 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 119 | static int ends_in_white(linenr_T lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 121 | static int same_leader(linenr_T lnum, int, char_u *, int, char_u *); |
| 122 | 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] | 123 | #else |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 124 | static int fmt_check_par(linenr_T); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 125 | #endif |
| 126 | |
| 127 | /* |
| 128 | * The names of operators. |
| 129 | * IMPORTANT: Index must correspond with defines in vim.h!!! |
| 130 | * The third field indicates whether the operator always works on lines. |
| 131 | */ |
| 132 | static char opchars[][3] = |
| 133 | { |
| 134 | {NUL, NUL, FALSE}, /* OP_NOP */ |
| 135 | {'d', NUL, FALSE}, /* OP_DELETE */ |
| 136 | {'y', NUL, FALSE}, /* OP_YANK */ |
| 137 | {'c', NUL, FALSE}, /* OP_CHANGE */ |
| 138 | {'<', NUL, TRUE}, /* OP_LSHIFT */ |
| 139 | {'>', NUL, TRUE}, /* OP_RSHIFT */ |
| 140 | {'!', NUL, TRUE}, /* OP_FILTER */ |
| 141 | {'g', '~', FALSE}, /* OP_TILDE */ |
| 142 | {'=', NUL, TRUE}, /* OP_INDENT */ |
| 143 | {'g', 'q', TRUE}, /* OP_FORMAT */ |
| 144 | {':', NUL, TRUE}, /* OP_COLON */ |
| 145 | {'g', 'U', FALSE}, /* OP_UPPER */ |
| 146 | {'g', 'u', FALSE}, /* OP_LOWER */ |
| 147 | {'J', NUL, TRUE}, /* DO_JOIN */ |
| 148 | {'g', 'J', TRUE}, /* DO_JOIN_NS */ |
| 149 | {'g', '?', FALSE}, /* OP_ROT13 */ |
| 150 | {'r', NUL, FALSE}, /* OP_REPLACE */ |
| 151 | {'I', NUL, FALSE}, /* OP_INSERT */ |
| 152 | {'A', NUL, FALSE}, /* OP_APPEND */ |
| 153 | {'z', 'f', TRUE}, /* OP_FOLD */ |
| 154 | {'z', 'o', TRUE}, /* OP_FOLDOPEN */ |
| 155 | {'z', 'O', TRUE}, /* OP_FOLDOPENREC */ |
| 156 | {'z', 'c', TRUE}, /* OP_FOLDCLOSE */ |
| 157 | {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */ |
| 158 | {'z', 'd', TRUE}, /* OP_FOLDDEL */ |
| 159 | {'z', 'D', TRUE}, /* OP_FOLDDELREC */ |
| 160 | {'g', 'w', TRUE}, /* OP_FORMAT2 */ |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 161 | {'g', '@', FALSE}, /* OP_FUNCTION */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 162 | {Ctrl_A, NUL, FALSE}, /* OP_NR_ADD */ |
| 163 | {Ctrl_X, NUL, FALSE}, /* OP_NR_SUB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /* |
| 167 | * Translate a command name into an operator type. |
| 168 | * Must only be called with a valid operator name! |
| 169 | */ |
| 170 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 171 | get_op_type(int char1, int char2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 172 | { |
| 173 | int i; |
| 174 | |
| 175 | if (char1 == 'r') /* ignore second character */ |
| 176 | return OP_REPLACE; |
| 177 | if (char1 == '~') /* when tilde is an operator */ |
| 178 | return OP_TILDE; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 179 | if (char1 == 'g' && char2 == Ctrl_A) /* add */ |
| 180 | return OP_NR_ADD; |
| 181 | if (char1 == 'g' && char2 == Ctrl_X) /* subtract */ |
| 182 | return OP_NR_SUB; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 183 | for (i = 0; ; ++i) |
| 184 | if (opchars[i][0] == char1 && opchars[i][1] == char2) |
| 185 | break; |
| 186 | return i; |
| 187 | } |
| 188 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | /* |
| 190 | * Return TRUE if operator "op" always works on whole lines. |
| 191 | */ |
| 192 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 193 | op_on_lines(int op) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 194 | { |
| 195 | return opchars[op][2]; |
| 196 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * Get first operator command character. |
| 200 | * Returns 'g' or 'z' if there is another command character. |
| 201 | */ |
| 202 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 203 | get_op_char(int optype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 204 | { |
| 205 | return opchars[optype][0]; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Get second operator command character. |
| 210 | */ |
| 211 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 212 | get_extra_op_char(int optype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 213 | { |
| 214 | return opchars[optype][1]; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * op_shift - handle a shift operation |
| 219 | */ |
| 220 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 221 | op_shift(oparg_T *oap, int curs_top, int amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 222 | { |
| 223 | long i; |
| 224 | int first_char; |
| 225 | char_u *s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 226 | int block_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 227 | |
| 228 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 229 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 230 | return; |
| 231 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 232 | if (oap->block_mode) |
| 233 | block_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 234 | |
| 235 | for (i = oap->line_count; --i >= 0; ) |
| 236 | { |
| 237 | first_char = *ml_get_curline(); |
| 238 | if (first_char == NUL) /* empty line */ |
| 239 | curwin->w_cursor.col = 0; |
| 240 | #ifdef FEAT_VISUALEXTRA |
| 241 | else if (oap->block_mode) |
| 242 | shift_block(oap, amount); |
| 243 | #endif |
| 244 | else |
| 245 | /* Move the line right if it doesn't start with '#', 'smartindent' |
| 246 | * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */ |
| 247 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) |
| 248 | if (first_char != '#' || !preprocs_left()) |
| 249 | #endif |
| 250 | { |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 251 | shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 252 | } |
| 253 | ++curwin->w_cursor.lnum; |
| 254 | } |
| 255 | |
| 256 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
Bram Moolenaar | 2b79bfd | 2013-07-13 16:34:32 +0200 | [diff] [blame] | 257 | #ifdef FEAT_FOLDING |
| 258 | /* The cursor line is not in a closed fold */ |
| 259 | foldOpenCursor(); |
| 260 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 261 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 262 | if (oap->block_mode) |
| 263 | { |
| 264 | curwin->w_cursor.lnum = oap->start.lnum; |
| 265 | curwin->w_cursor.col = block_col; |
| 266 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 267 | else if (curs_top) /* put cursor on first line, for ">>" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 268 | { |
| 269 | curwin->w_cursor.lnum = oap->start.lnum; |
| 270 | beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */ |
| 271 | } |
| 272 | else |
| 273 | --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */ |
| 274 | |
| 275 | if (oap->line_count > p_report) |
| 276 | { |
| 277 | if (oap->op_type == OP_RSHIFT) |
| 278 | s = (char_u *)">"; |
| 279 | else |
| 280 | s = (char_u *)"<"; |
| 281 | if (oap->line_count == 1) |
| 282 | { |
| 283 | if (amount == 1) |
| 284 | sprintf((char *)IObuff, _("1 line %sed 1 time"), s); |
| 285 | else |
| 286 | sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | if (amount == 1) |
| 291 | sprintf((char *)IObuff, _("%ld lines %sed 1 time"), |
| 292 | oap->line_count, s); |
| 293 | else |
| 294 | sprintf((char *)IObuff, _("%ld lines %sed %d times"), |
| 295 | oap->line_count, s, amount); |
| 296 | } |
| 297 | msg(IObuff); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Set "'[" and "']" marks. |
| 302 | */ |
| 303 | curbuf->b_op_start = oap->start; |
| 304 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 305 | curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 306 | if (curbuf->b_op_end.col > 0) |
| 307 | --curbuf->b_op_end.col; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * shift the current line one shiftwidth left (if left != 0) or right |
| 312 | * leaves cursor on first blank in the line |
| 313 | */ |
| 314 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 315 | shift_line( |
| 316 | int left, |
| 317 | int round, |
| 318 | int amount, |
| 319 | int call_changed_bytes) /* call changed_bytes() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 320 | { |
| 321 | int count; |
| 322 | int i, j; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 323 | int p_sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 324 | |
| 325 | count = get_indent(); /* get current indent */ |
| 326 | |
| 327 | if (round) /* round off indent */ |
| 328 | { |
| 329 | i = count / p_sw; /* number of p_sw rounded down */ |
| 330 | j = count % p_sw; /* extra spaces */ |
| 331 | if (j && left) /* first remove extra spaces */ |
| 332 | --amount; |
| 333 | if (left) |
| 334 | { |
| 335 | i -= amount; |
| 336 | if (i < 0) |
| 337 | i = 0; |
| 338 | } |
| 339 | else |
| 340 | i += amount; |
| 341 | count = i * p_sw; |
| 342 | } |
| 343 | else /* original vi indent */ |
| 344 | { |
| 345 | if (left) |
| 346 | { |
| 347 | count -= p_sw * amount; |
| 348 | if (count < 0) |
| 349 | count = 0; |
| 350 | } |
| 351 | else |
| 352 | count += p_sw * amount; |
| 353 | } |
| 354 | |
| 355 | /* Set new indent */ |
| 356 | #ifdef FEAT_VREPLACE |
| 357 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 358 | change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 359 | else |
| 360 | #endif |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 361 | (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | #if defined(FEAT_VISUALEXTRA) || defined(PROTO) |
| 365 | /* |
| 366 | * Shift one line of the current block one shiftwidth right or left. |
| 367 | * Leaves cursor on first character in block. |
| 368 | */ |
| 369 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 370 | shift_block(oparg_T *oap, int amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 371 | { |
| 372 | int left = (oap->op_type == OP_LSHIFT); |
| 373 | int oldstate = State; |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 374 | int total; |
| 375 | char_u *newp, *oldp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 376 | int oldcol = curwin->w_cursor.col; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 377 | int p_sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 378 | int p_ts = (int)curbuf->b_p_ts; |
| 379 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 380 | int incr; |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 381 | colnr_T ws_vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 382 | int i = 0, j = 0; |
| 383 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 384 | #ifdef FEAT_RIGHTLEFT |
| 385 | int old_p_ri = p_ri; |
| 386 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 387 | p_ri = 0; /* don't want revins in indent */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 388 | #endif |
| 389 | |
| 390 | State = INSERT; /* don't want REPLACE for State */ |
| 391 | block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); |
| 392 | if (bd.is_short) |
| 393 | return; |
| 394 | |
| 395 | /* total is number of screen columns to be inserted/removed */ |
| 396 | total = amount * p_sw; |
| 397 | oldp = ml_get_curline(); |
| 398 | |
| 399 | if (!left) |
| 400 | { |
| 401 | /* |
| 402 | * 1. Get start vcol |
| 403 | * 2. Total ws vcols |
| 404 | * 3. Divvy into TABs & spp |
| 405 | * 4. Construct new string |
| 406 | */ |
| 407 | total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */ |
| 408 | ws_vcol = bd.start_vcol - bd.pre_whitesp; |
| 409 | if (bd.startspaces) |
| 410 | { |
| 411 | #ifdef FEAT_MBYTE |
| 412 | if (has_mbyte) |
Bram Moolenaar | 20b4f46 | 2016-03-05 17:25:39 +0100 | [diff] [blame] | 413 | { |
| 414 | if ((*mb_ptr2len)(bd.textstart) == 1) |
| 415 | ++bd.textstart; |
| 416 | else |
| 417 | { |
| 418 | ws_vcol = 0; |
| 419 | bd.startspaces = 0; |
| 420 | } |
| 421 | } |
Bram Moolenaar | be1138b | 2009-11-11 16:22:28 +0000 | [diff] [blame] | 422 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 423 | #endif |
Bram Moolenaar | be1138b | 2009-11-11 16:22:28 +0000 | [diff] [blame] | 424 | ++bd.textstart; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 425 | } |
| 426 | for ( ; vim_iswhite(*bd.textstart); ) |
| 427 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 428 | /* TODO: is passing bd.textstart for start of the line OK? */ |
| 429 | incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart, |
| 430 | (colnr_T)(bd.start_vcol)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 431 | total += incr; |
| 432 | bd.start_vcol += incr; |
| 433 | } |
| 434 | /* OK, now total=all the VWS reqd, and textstart points at the 1st |
| 435 | * non-ws char in the block. */ |
| 436 | if (!curbuf->b_p_et) |
| 437 | i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */ |
| 438 | if (i) |
| 439 | j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */ |
| 440 | else |
| 441 | j = total; |
| 442 | /* if we're splitting a TAB, allow for it */ |
| 443 | bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0); |
| 444 | len = (int)STRLEN(bd.textstart) + 1; |
| 445 | newp = alloc_check((unsigned)(bd.textcol + i + j + len)); |
| 446 | if (newp == NULL) |
| 447 | return; |
| 448 | vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len)); |
| 449 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 450 | vim_memset(newp + bd.textcol, TAB, (size_t)i); |
| 451 | vim_memset(newp + bd.textcol + i, ' ', (size_t)j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 452 | /* the end */ |
| 453 | mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len); |
| 454 | } |
| 455 | else /* left */ |
| 456 | { |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 457 | colnr_T destination_col; /* column to which text in block will |
| 458 | be shifted */ |
| 459 | char_u *verbatim_copy_end; /* end of the part of the line which is |
| 460 | copied verbatim */ |
| 461 | colnr_T verbatim_copy_width;/* the (displayed) width of this part |
| 462 | of line */ |
| 463 | unsigned fill; /* nr of spaces that replace a TAB */ |
| 464 | unsigned new_line_len; /* the length of the line after the |
| 465 | block shift */ |
| 466 | size_t block_space_width; |
| 467 | size_t shift_amount; |
| 468 | char_u *non_white = bd.textstart; |
| 469 | colnr_T non_white_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 470 | |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 471 | /* |
| 472 | * Firstly, let's find the first non-whitespace character that is |
| 473 | * displayed after the block's start column and the character's column |
| 474 | * number. Also, let's calculate the width of all the whitespace |
| 475 | * characters that are displayed in the block and precede the searched |
| 476 | * non-whitespace character. |
| 477 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 478 | |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 479 | /* If "bd.startspaces" is set, "bd.textstart" points to the character, |
| 480 | * the part of which is displayed at the block's beginning. Let's start |
| 481 | * searching from the next character. */ |
| 482 | if (bd.startspaces) |
| 483 | mb_ptr_adv(non_white); |
| 484 | |
| 485 | /* The character's column is in "bd.start_vcol". */ |
| 486 | non_white_col = bd.start_vcol; |
| 487 | |
| 488 | while (vim_iswhite(*non_white)) |
| 489 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 490 | incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 491 | non_white_col += incr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 494 | block_space_width = non_white_col - oap->start_vcol; |
| 495 | /* We will shift by "total" or "block_space_width", whichever is less. |
| 496 | */ |
Bram Moolenaar | 92a990b | 2009-04-22 15:45:05 +0000 | [diff] [blame] | 497 | shift_amount = (block_space_width < (size_t)total |
| 498 | ? block_space_width : (size_t)total); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 499 | |
| 500 | /* The column to which we will shift the text. */ |
Bram Moolenaar | 92a990b | 2009-04-22 15:45:05 +0000 | [diff] [blame] | 501 | destination_col = (colnr_T)(non_white_col - shift_amount); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 502 | |
| 503 | /* Now let's find out how much of the beginning of the line we can |
| 504 | * reuse without modification. */ |
| 505 | verbatim_copy_end = bd.textstart; |
| 506 | verbatim_copy_width = bd.start_vcol; |
| 507 | |
| 508 | /* If "bd.startspaces" is set, "bd.textstart" points to the character |
| 509 | * preceding the block. We have to subtract its width to obtain its |
| 510 | * column number. */ |
| 511 | if (bd.startspaces) |
| 512 | verbatim_copy_width -= bd.start_char_vcols; |
| 513 | while (verbatim_copy_width < destination_col) |
| 514 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 515 | char_u *line = verbatim_copy_end; |
| 516 | |
| 517 | /* TODO: is passing verbatim_copy_end for start of the line OK? */ |
| 518 | incr = lbr_chartabsize(line, verbatim_copy_end, |
| 519 | verbatim_copy_width); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 520 | if (verbatim_copy_width + incr > destination_col) |
| 521 | break; |
| 522 | verbatim_copy_width += incr; |
| 523 | mb_ptr_adv(verbatim_copy_end); |
| 524 | } |
| 525 | |
| 526 | /* If "destination_col" is different from the width of the initial |
| 527 | * part of the line that will be copied, it means we encountered a tab |
| 528 | * character, which we will have to partly replace with spaces. */ |
| 529 | fill = destination_col - verbatim_copy_width; |
| 530 | |
| 531 | /* The replacement line will consist of: |
| 532 | * - the beginning of the original line up to "verbatim_copy_end", |
| 533 | * - "fill" number of spaces, |
| 534 | * - the rest of the line, pointed to by non_white. */ |
| 535 | new_line_len = (unsigned)(verbatim_copy_end - oldp) |
| 536 | + fill |
| 537 | + (unsigned)STRLEN(non_white) + 1; |
| 538 | |
| 539 | newp = alloc_check(new_line_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 540 | if (newp == NULL) |
| 541 | return; |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 542 | mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp)); |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 543 | vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 544 | STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 545 | } |
| 546 | /* replace the line */ |
| 547 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 548 | changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol); |
| 549 | State = oldstate; |
| 550 | curwin->w_cursor.col = oldcol; |
| 551 | #ifdef FEAT_RIGHTLEFT |
| 552 | p_ri = old_p_ri; |
| 553 | #endif |
| 554 | } |
| 555 | #endif |
| 556 | |
| 557 | #ifdef FEAT_VISUALEXTRA |
| 558 | /* |
| 559 | * Insert string "s" (b_insert ? before : after) block :AKelly |
| 560 | * Caller must prepare for undo. |
| 561 | */ |
| 562 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 563 | block_insert( |
| 564 | oparg_T *oap, |
| 565 | char_u *s, |
| 566 | int b_insert, |
| 567 | struct block_def *bdp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 568 | { |
| 569 | int p_ts; |
| 570 | int count = 0; /* extra spaces to replace a cut TAB */ |
| 571 | int spaces = 0; /* non-zero if cutting a TAB */ |
| 572 | colnr_T offset; /* pointer along new line */ |
| 573 | unsigned s_len; /* STRLEN(s) */ |
| 574 | char_u *newp, *oldp; /* new, old lines */ |
| 575 | linenr_T lnum; /* loop var */ |
| 576 | int oldstate = State; |
| 577 | |
| 578 | State = INSERT; /* don't want REPLACE for State */ |
| 579 | s_len = (unsigned)STRLEN(s); |
| 580 | |
| 581 | for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++) |
| 582 | { |
| 583 | block_prep(oap, bdp, lnum, TRUE); |
| 584 | if (bdp->is_short && b_insert) |
| 585 | continue; /* OP_INSERT, line ends before block start */ |
| 586 | |
| 587 | oldp = ml_get(lnum); |
| 588 | |
| 589 | if (b_insert) |
| 590 | { |
| 591 | p_ts = bdp->start_char_vcols; |
| 592 | spaces = bdp->startspaces; |
| 593 | if (spaces != 0) |
| 594 | count = p_ts - 1; /* we're cutting a TAB */ |
| 595 | offset = bdp->textcol; |
| 596 | } |
| 597 | else /* append */ |
| 598 | { |
| 599 | p_ts = bdp->end_char_vcols; |
| 600 | if (!bdp->is_short) /* spaces = padding after block */ |
| 601 | { |
| 602 | spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0); |
| 603 | if (spaces != 0) |
| 604 | count = p_ts - 1; /* we're cutting a TAB */ |
| 605 | offset = bdp->textcol + bdp->textlen - (spaces != 0); |
| 606 | } |
| 607 | else /* spaces = padding to block edge */ |
| 608 | { |
| 609 | /* if $ used, just append to EOL (ie spaces==0) */ |
| 610 | if (!bdp->is_MAX) |
| 611 | spaces = (oap->end_vcol - bdp->end_vcol) + 1; |
| 612 | count = spaces; |
| 613 | offset = bdp->textcol + bdp->textlen; |
| 614 | } |
| 615 | } |
| 616 | |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 617 | #ifdef FEAT_MBYTE |
| 618 | if (has_mbyte && spaces > 0) |
| 619 | { |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 620 | int off; |
| 621 | |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 622 | /* Avoid starting halfway a multi-byte character. */ |
| 623 | if (b_insert) |
| 624 | { |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 625 | off = (*mb_head_off)(oldp, oldp + offset + spaces); |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 626 | } |
| 627 | else |
| 628 | { |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 629 | off = (*mb_off_next)(oldp, oldp + offset); |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 630 | offset += off; |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 631 | } |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 632 | spaces -= off; |
| 633 | count -= off; |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 634 | } |
| 635 | #endif |
| 636 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 637 | newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1); |
| 638 | if (newp == NULL) |
| 639 | continue; |
| 640 | |
| 641 | /* copy up to shifted part */ |
| 642 | mch_memmove(newp, oldp, (size_t)(offset)); |
| 643 | oldp += offset; |
| 644 | |
| 645 | /* insert pre-padding */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 646 | vim_memset(newp + offset, ' ', (size_t)spaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 647 | |
| 648 | /* copy the new text */ |
| 649 | mch_memmove(newp + offset + spaces, s, (size_t)s_len); |
| 650 | offset += s_len; |
| 651 | |
| 652 | if (spaces && !bdp->is_short) |
| 653 | { |
| 654 | /* insert post-padding */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 655 | vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 656 | /* We're splitting a TAB, don't copy it. */ |
| 657 | oldp++; |
| 658 | /* We allowed for that TAB, remember this now */ |
| 659 | count++; |
| 660 | } |
| 661 | |
| 662 | if (spaces > 0) |
| 663 | offset += count; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 664 | STRMOVE(newp + offset, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 665 | |
| 666 | ml_replace(lnum, newp, FALSE); |
| 667 | |
| 668 | if (lnum == oap->end.lnum) |
| 669 | { |
| 670 | /* Set "']" mark to the end of the block instead of the end of |
| 671 | * the insert in the first line. */ |
| 672 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 673 | curbuf->b_op_end.col = offset; |
| 674 | } |
| 675 | } /* for all lnum */ |
| 676 | |
| 677 | changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); |
| 678 | |
| 679 | State = oldstate; |
| 680 | } |
| 681 | #endif |
| 682 | |
| 683 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 684 | /* |
| 685 | * op_reindent - handle reindenting a block of lines. |
| 686 | */ |
| 687 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 688 | op_reindent(oparg_T *oap, int (*how)(void)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 689 | { |
| 690 | long i; |
| 691 | char_u *l; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 692 | int amount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 693 | linenr_T first_changed = 0; |
| 694 | linenr_T last_changed = 0; |
| 695 | linenr_T start_lnum = curwin->w_cursor.lnum; |
| 696 | |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 697 | /* Don't even try when 'modifiable' is off. */ |
| 698 | if (!curbuf->b_p_ma) |
| 699 | { |
| 700 | EMSG(_(e_modifiable)); |
| 701 | return; |
| 702 | } |
| 703 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 704 | for (i = oap->line_count; --i >= 0 && !got_int; ) |
| 705 | { |
| 706 | /* it's a slow thing to do, so give feedback so there's no worry that |
| 707 | * the computer's just hung. */ |
| 708 | |
| 709 | if (i > 1 |
| 710 | && (i % 50 == 0 || i == oap->line_count - 1) |
| 711 | && oap->line_count > p_report) |
| 712 | smsg((char_u *)_("%ld lines to indent... "), i); |
| 713 | |
| 714 | /* |
| 715 | * Be vi-compatible: For lisp indenting the first line is not |
| 716 | * indented, unless there is only one line. |
| 717 | */ |
| 718 | #ifdef FEAT_LISP |
| 719 | if (i != oap->line_count - 1 || oap->line_count == 1 |
| 720 | || how != get_lisp_indent) |
| 721 | #endif |
| 722 | { |
| 723 | l = skipwhite(ml_get_curline()); |
| 724 | if (*l == NUL) /* empty or blank line */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 725 | amount = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 726 | else |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 727 | amount = how(); /* get the indent for this line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 728 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 729 | if (amount >= 0 && set_indent(amount, SIN_UNDO)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 730 | { |
| 731 | /* did change the indent, call changed_lines() later */ |
| 732 | if (first_changed == 0) |
| 733 | first_changed = curwin->w_cursor.lnum; |
| 734 | last_changed = curwin->w_cursor.lnum; |
| 735 | } |
| 736 | } |
| 737 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 738 | curwin->w_cursor.col = 0; /* make sure it's valid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* put cursor on first non-blank of indented line */ |
| 742 | curwin->w_cursor.lnum = start_lnum; |
| 743 | beginline(BL_SOL | BL_FIX); |
| 744 | |
| 745 | /* Mark changed lines so that they will be redrawn. When Visual |
| 746 | * highlighting was present, need to continue until the last line. When |
| 747 | * there is no change still need to remove the Visual highlighting. */ |
| 748 | if (last_changed != 0) |
| 749 | changed_lines(first_changed, 0, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 750 | oap->is_VIsual ? start_lnum + oap->line_count : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 751 | last_changed + 1, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 752 | else if (oap->is_VIsual) |
| 753 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 754 | |
| 755 | if (oap->line_count > p_report) |
| 756 | { |
| 757 | i = oap->line_count - (i + 1); |
| 758 | if (i == 1) |
| 759 | MSG(_("1 line indented ")); |
| 760 | else |
| 761 | smsg((char_u *)_("%ld lines indented "), i); |
| 762 | } |
| 763 | /* set '[ and '] marks */ |
| 764 | curbuf->b_op_start = oap->start; |
| 765 | curbuf->b_op_end = oap->end; |
| 766 | } |
| 767 | #endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */ |
| 768 | |
| 769 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 770 | /* |
| 771 | * Keep the last expression line here, for repeating. |
| 772 | */ |
| 773 | static char_u *expr_line = NULL; |
| 774 | |
| 775 | /* |
| 776 | * Get an expression for the "\"=expr1" or "CTRL-R =expr1" |
| 777 | * Returns '=' when OK, NUL otherwise. |
| 778 | */ |
| 779 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 780 | get_expr_register(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 781 | { |
| 782 | char_u *new_line; |
| 783 | |
| 784 | new_line = getcmdline('=', 0L, 0); |
| 785 | if (new_line == NULL) |
| 786 | return NUL; |
| 787 | if (*new_line == NUL) /* use previous line */ |
| 788 | vim_free(new_line); |
| 789 | else |
| 790 | set_expr_line(new_line); |
| 791 | return '='; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * Set the expression for the '=' register. |
| 796 | * Argument must be an allocated string. |
| 797 | */ |
| 798 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 799 | set_expr_line(char_u *new_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 800 | { |
| 801 | vim_free(expr_line); |
| 802 | expr_line = new_line; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Get the result of the '=' register expression. |
| 807 | * Returns a pointer to allocated memory, or NULL for failure. |
| 808 | */ |
| 809 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 810 | get_expr_line(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 811 | { |
| 812 | char_u *expr_copy; |
| 813 | char_u *rv; |
Bram Moolenaar | 21bffa7 | 2006-10-06 21:33:16 +0000 | [diff] [blame] | 814 | static int nested = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 815 | |
| 816 | if (expr_line == NULL) |
| 817 | return NULL; |
| 818 | |
| 819 | /* Make a copy of the expression, because evaluating it may cause it to be |
| 820 | * changed. */ |
| 821 | expr_copy = vim_strsave(expr_line); |
| 822 | if (expr_copy == NULL) |
| 823 | return NULL; |
| 824 | |
Bram Moolenaar | 21bffa7 | 2006-10-06 21:33:16 +0000 | [diff] [blame] | 825 | /* When we are invoked recursively limit the evaluation to 10 levels. |
| 826 | * Then return the string as-is. */ |
| 827 | if (nested >= 10) |
| 828 | return expr_copy; |
| 829 | |
| 830 | ++nested; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 831 | rv = eval_to_string(expr_copy, NULL, TRUE); |
Bram Moolenaar | 21bffa7 | 2006-10-06 21:33:16 +0000 | [diff] [blame] | 832 | --nested; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 833 | vim_free(expr_copy); |
| 834 | return rv; |
| 835 | } |
Bram Moolenaar | de934d7 | 2005-05-22 22:09:40 +0000 | [diff] [blame] | 836 | |
| 837 | /* |
| 838 | * Get the '=' register expression itself, without evaluating it. |
| 839 | */ |
| 840 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 841 | get_expr_line_src(void) |
Bram Moolenaar | de934d7 | 2005-05-22 22:09:40 +0000 | [diff] [blame] | 842 | { |
| 843 | if (expr_line == NULL) |
| 844 | return NULL; |
| 845 | return vim_strsave(expr_line); |
| 846 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 847 | #endif /* FEAT_EVAL */ |
| 848 | |
| 849 | /* |
| 850 | * Check if 'regname' is a valid name of a yank register. |
| 851 | * Note: There is no check for 0 (default register), caller should do this |
| 852 | */ |
| 853 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 854 | valid_yank_reg( |
| 855 | int regname, |
| 856 | int writing) /* if TRUE check for writable registers */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 857 | { |
| 858 | if ( (regname > 0 && ASCII_ISALNUM(regname)) |
| 859 | || (!writing && vim_strchr((char_u *) |
| 860 | #ifdef FEAT_EVAL |
Bram Moolenaar | 3b3a949 | 2015-01-27 18:44:16 +0100 | [diff] [blame] | 861 | "/.%:=" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 862 | #else |
Bram Moolenaar | 3b3a949 | 2015-01-27 18:44:16 +0100 | [diff] [blame] | 863 | "/.%:" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 864 | #endif |
| 865 | , regname) != NULL) |
Bram Moolenaar | 3b3a949 | 2015-01-27 18:44:16 +0100 | [diff] [blame] | 866 | || regname == '#' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 867 | || regname == '"' |
| 868 | || regname == '-' |
| 869 | || regname == '_' |
| 870 | #ifdef FEAT_CLIPBOARD |
| 871 | || regname == '*' |
| 872 | || regname == '+' |
| 873 | #endif |
| 874 | #ifdef FEAT_DND |
| 875 | || (!writing && regname == '~') |
| 876 | #endif |
| 877 | ) |
| 878 | return TRUE; |
| 879 | return FALSE; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Set y_current and y_append, according to the value of "regname". |
| 884 | * Cannot handle the '_' register. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 885 | * Must only be called with a valid register name! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 886 | * |
| 887 | * If regname is 0 and writing, use register 0 |
| 888 | * If regname is 0 and reading, use previous register |
| 889 | */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 890 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 891 | get_yank_register(int regname, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 892 | { |
| 893 | int i; |
| 894 | |
| 895 | y_append = FALSE; |
| 896 | if ((regname == 0 || regname == '"') && !writing && y_previous != NULL) |
| 897 | { |
| 898 | y_current = y_previous; |
| 899 | return; |
| 900 | } |
| 901 | i = regname; |
| 902 | if (VIM_ISDIGIT(i)) |
| 903 | i -= '0'; |
| 904 | else if (ASCII_ISLOWER(i)) |
| 905 | i = CharOrdLow(i) + 10; |
| 906 | else if (ASCII_ISUPPER(i)) |
| 907 | { |
| 908 | i = CharOrdUp(i) + 10; |
| 909 | y_append = TRUE; |
| 910 | } |
| 911 | else if (regname == '-') |
| 912 | i = DELETION_REGISTER; |
| 913 | #ifdef FEAT_CLIPBOARD |
| 914 | /* When selection is not available, use register 0 instead of '*' */ |
| 915 | else if (clip_star.available && regname == '*') |
| 916 | i = STAR_REGISTER; |
| 917 | /* When clipboard is not available, use register 0 instead of '+' */ |
| 918 | else if (clip_plus.available && regname == '+') |
| 919 | i = PLUS_REGISTER; |
| 920 | #endif |
| 921 | #ifdef FEAT_DND |
| 922 | else if (!writing && regname == '~') |
| 923 | i = TILDE_REGISTER; |
| 924 | #endif |
| 925 | else /* not 0-9, a-z, A-Z or '-': use register 0 */ |
| 926 | i = 0; |
| 927 | y_current = &(y_regs[i]); |
| 928 | if (writing) /* remember the register we write into for do_put() */ |
| 929 | y_previous = y_current; |
| 930 | } |
| 931 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 932 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 933 | /* |
| 934 | * When "regname" is a clipboard register, obtain the selection. If it's not |
| 935 | * available return zero, otherwise return "regname". |
| 936 | */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 937 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 938 | may_get_selection(int regname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 939 | { |
| 940 | if (regname == '*') |
| 941 | { |
| 942 | if (!clip_star.available) |
| 943 | regname = 0; |
| 944 | else |
| 945 | clip_get_selection(&clip_star); |
| 946 | } |
| 947 | else if (regname == '+') |
| 948 | { |
| 949 | if (!clip_plus.available) |
| 950 | regname = 0; |
| 951 | else |
| 952 | clip_get_selection(&clip_plus); |
| 953 | } |
| 954 | return regname; |
| 955 | } |
| 956 | #endif |
| 957 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 958 | /* |
| 959 | * Obtain the contents of a "normal" register. The register is made empty. |
| 960 | * The returned pointer has allocated memory, use put_register() later. |
| 961 | */ |
| 962 | void * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 963 | get_register( |
| 964 | int name, |
| 965 | int copy) /* make a copy, if FALSE make register empty. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 966 | { |
Bram Moolenaar | 0a30746 | 2007-12-01 20:13:05 +0000 | [diff] [blame] | 967 | struct yankreg *reg; |
| 968 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 969 | |
| 970 | #ifdef FEAT_CLIPBOARD |
| 971 | /* When Visual area changed, may have to update selection. Obtain the |
| 972 | * selection too. */ |
Bram Moolenaar | cdfd3e4 | 2007-11-08 09:35:50 +0000 | [diff] [blame] | 973 | if (name == '*' && clip_star.available) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 974 | { |
Bram Moolenaar | c0885aa | 2012-07-10 16:49:23 +0200 | [diff] [blame] | 975 | if (clip_isautosel_star()) |
| 976 | clip_update_selection(&clip_star); |
| 977 | may_get_selection(name); |
| 978 | } |
| 979 | if (name == '+' && clip_plus.available) |
| 980 | { |
| 981 | if (clip_isautosel_plus()) |
| 982 | clip_update_selection(&clip_plus); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 983 | may_get_selection(name); |
| 984 | } |
| 985 | #endif |
| 986 | |
| 987 | get_yank_register(name, 0); |
| 988 | reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg)); |
| 989 | if (reg != NULL) |
| 990 | { |
| 991 | *reg = *y_current; |
| 992 | if (copy) |
| 993 | { |
| 994 | /* If we run out of memory some or all of the lines are empty. */ |
| 995 | if (reg->y_size == 0) |
| 996 | reg->y_array = NULL; |
| 997 | else |
| 998 | reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *) |
| 999 | * reg->y_size)); |
| 1000 | if (reg->y_array != NULL) |
| 1001 | { |
| 1002 | for (i = 0; i < reg->y_size; ++i) |
| 1003 | reg->y_array[i] = vim_strsave(y_current->y_array[i]); |
| 1004 | } |
| 1005 | } |
| 1006 | else |
| 1007 | y_current->y_array = NULL; |
| 1008 | } |
| 1009 | return (void *)reg; |
| 1010 | } |
| 1011 | |
| 1012 | /* |
Bram Moolenaar | 0a30746 | 2007-12-01 20:13:05 +0000 | [diff] [blame] | 1013 | * Put "reg" into register "name". Free any previous contents and "reg". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1014 | */ |
| 1015 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1016 | put_register(int name, void *reg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1017 | { |
| 1018 | get_yank_register(name, 0); |
| 1019 | free_yank_all(); |
| 1020 | *y_current = *(struct yankreg *)reg; |
Bram Moolenaar | 0a30746 | 2007-12-01 20:13:05 +0000 | [diff] [blame] | 1021 | vim_free(reg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1022 | |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1023 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1024 | /* Send text written to clipboard register to the clipboard. */ |
| 1025 | may_set_selection(); |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1026 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1027 | } |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1028 | |
| 1029 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1030 | free_register(void *reg) |
Bram Moolenaar | 1a0316c | 2013-03-13 17:50:25 +0100 | [diff] [blame] | 1031 | { |
| 1032 | struct yankreg tmp; |
| 1033 | |
| 1034 | tmp = *y_current; |
| 1035 | *y_current = *(struct yankreg *)reg; |
| 1036 | free_yank_all(); |
| 1037 | vim_free(reg); |
| 1038 | *y_current = tmp; |
| 1039 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1040 | |
| 1041 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 1042 | /* |
| 1043 | * return TRUE if the current yank register has type MLINE |
| 1044 | */ |
| 1045 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1046 | yank_register_mline(int regname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1047 | { |
| 1048 | if (regname != 0 && !valid_yank_reg(regname, FALSE)) |
| 1049 | return FALSE; |
| 1050 | if (regname == '_') /* black hole is always empty */ |
| 1051 | return FALSE; |
| 1052 | get_yank_register(regname, FALSE); |
| 1053 | return (y_current->y_type == MLINE); |
| 1054 | } |
| 1055 | #endif |
| 1056 | |
| 1057 | /* |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1058 | * Start or stop recording into a yank register. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1059 | * |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1060 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1061 | */ |
| 1062 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1063 | do_record(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1064 | { |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1065 | char_u *p; |
| 1066 | static int regname; |
| 1067 | struct yankreg *old_y_previous, *old_y_current; |
| 1068 | int retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (Recording == FALSE) /* start recording */ |
| 1071 | { |
| 1072 | /* registers 0-9, a-z and " are allowed */ |
| 1073 | if (c < 0 || (!ASCII_ISALNUM(c) && c != '"')) |
| 1074 | retval = FAIL; |
| 1075 | else |
| 1076 | { |
Bram Moolenaar | a0ed84a | 2015-11-19 17:56:13 +0100 | [diff] [blame] | 1077 | Recording = c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1078 | showmode(); |
| 1079 | regname = c; |
| 1080 | retval = OK; |
| 1081 | } |
| 1082 | } |
| 1083 | else /* stop recording */ |
| 1084 | { |
| 1085 | /* |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1086 | * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this |
| 1087 | * needs to be removed again to put it in a register. exec_reg then |
| 1088 | * adds the escaping back later. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1089 | */ |
| 1090 | Recording = FALSE; |
| 1091 | MSG(""); |
| 1092 | p = get_recorded(); |
| 1093 | if (p == NULL) |
| 1094 | retval = FAIL; |
| 1095 | else |
| 1096 | { |
Bram Moolenaar | 81b8587 | 2007-03-04 20:22:01 +0000 | [diff] [blame] | 1097 | /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */ |
| 1098 | vim_unescape_csi(p); |
| 1099 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1100 | /* |
| 1101 | * We don't want to change the default register here, so save and |
| 1102 | * restore the current register name. |
| 1103 | */ |
| 1104 | old_y_previous = y_previous; |
| 1105 | old_y_current = y_current; |
| 1106 | |
| 1107 | retval = stuff_yank(regname, p); |
| 1108 | |
| 1109 | y_previous = old_y_previous; |
| 1110 | y_current = old_y_current; |
| 1111 | } |
| 1112 | } |
| 1113 | return retval; |
| 1114 | } |
| 1115 | |
| 1116 | /* |
| 1117 | * Stuff string "p" into yank register "regname" as a single line (append if |
| 1118 | * uppercase). "p" must have been alloced. |
| 1119 | * |
| 1120 | * return FAIL for failure, OK otherwise |
| 1121 | */ |
| 1122 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1123 | stuff_yank(int regname, char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1124 | { |
| 1125 | char_u *lp; |
| 1126 | char_u **pp; |
| 1127 | |
| 1128 | /* check for read-only register */ |
| 1129 | if (regname != 0 && !valid_yank_reg(regname, TRUE)) |
| 1130 | { |
| 1131 | vim_free(p); |
| 1132 | return FAIL; |
| 1133 | } |
| 1134 | if (regname == '_') /* black hole: don't do anything */ |
| 1135 | { |
| 1136 | vim_free(p); |
| 1137 | return OK; |
| 1138 | } |
| 1139 | get_yank_register(regname, TRUE); |
| 1140 | if (y_append && y_current->y_array != NULL) |
| 1141 | { |
| 1142 | pp = &(y_current->y_array[y_current->y_size - 1]); |
| 1143 | lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE); |
| 1144 | if (lp == NULL) |
| 1145 | { |
| 1146 | vim_free(p); |
| 1147 | return FAIL; |
| 1148 | } |
| 1149 | STRCPY(lp, *pp); |
| 1150 | STRCAT(lp, p); |
| 1151 | vim_free(p); |
| 1152 | vim_free(*pp); |
| 1153 | *pp = lp; |
| 1154 | } |
| 1155 | else |
| 1156 | { |
| 1157 | free_yank_all(); |
| 1158 | if ((y_current->y_array = |
| 1159 | (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL) |
| 1160 | { |
| 1161 | vim_free(p); |
| 1162 | return FAIL; |
| 1163 | } |
| 1164 | y_current->y_array[0] = p; |
| 1165 | y_current->y_size = 1; |
| 1166 | y_current->y_type = MCHAR; /* used to be MLINE, why? */ |
| 1167 | } |
| 1168 | return OK; |
| 1169 | } |
| 1170 | |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 1171 | static int execreg_lastc = NUL; |
| 1172 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1173 | /* |
| 1174 | * execute a yank register: copy it into the stuff buffer |
| 1175 | * |
| 1176 | * return FAIL for failure, OK otherwise |
| 1177 | */ |
| 1178 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1179 | do_execreg( |
| 1180 | int regname, |
| 1181 | int colon, /* insert ':' before each line */ |
| 1182 | int addcr, /* always add '\n' to end of line */ |
| 1183 | int silent) /* set "silent" flag in typeahead buffer */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1184 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1185 | long i; |
| 1186 | char_u *p; |
| 1187 | int retval = OK; |
| 1188 | int remap; |
| 1189 | |
| 1190 | if (regname == '@') /* repeat previous one */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1191 | { |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 1192 | if (execreg_lastc == NUL) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1193 | { |
| 1194 | EMSG(_("E748: No previously used register")); |
| 1195 | return FAIL; |
| 1196 | } |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 1197 | regname = execreg_lastc; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1198 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1199 | /* check for valid regname */ |
| 1200 | if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE)) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1201 | { |
| 1202 | emsg_invreg(regname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1203 | return FAIL; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1204 | } |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 1205 | execreg_lastc = regname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1206 | |
| 1207 | #ifdef FEAT_CLIPBOARD |
| 1208 | regname = may_get_selection(regname); |
| 1209 | #endif |
| 1210 | |
| 1211 | if (regname == '_') /* black hole: don't stuff anything */ |
| 1212 | return OK; |
| 1213 | |
| 1214 | #ifdef FEAT_CMDHIST |
| 1215 | if (regname == ':') /* use last command line */ |
| 1216 | { |
| 1217 | if (last_cmdline == NULL) |
| 1218 | { |
| 1219 | EMSG(_(e_nolastcmd)); |
| 1220 | return FAIL; |
| 1221 | } |
| 1222 | vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */ |
| 1223 | new_last_cmdline = NULL; |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 1224 | /* Escape all control characters with a CTRL-V */ |
| 1225 | p = vim_strsave_escaped_ext(last_cmdline, |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 1226 | (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] | 1227 | if (p != NULL) |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 1228 | { |
| 1229 | /* When in Visual mode "'<,'>" will be prepended to the command. |
| 1230 | * Remove it when it's already there. */ |
| 1231 | if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0) |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1232 | retval = put_in_typebuf(p + 5, TRUE, TRUE, silent); |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 1233 | else |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1234 | retval = put_in_typebuf(p, TRUE, TRUE, silent); |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 1235 | } |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 1236 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | } |
| 1238 | #endif |
| 1239 | #ifdef FEAT_EVAL |
| 1240 | else if (regname == '=') |
| 1241 | { |
| 1242 | p = get_expr_line(); |
| 1243 | if (p == NULL) |
| 1244 | return FAIL; |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1245 | retval = put_in_typebuf(p, TRUE, colon, silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1246 | vim_free(p); |
| 1247 | } |
| 1248 | #endif |
| 1249 | else if (regname == '.') /* use last inserted text */ |
| 1250 | { |
| 1251 | p = get_last_insert_save(); |
| 1252 | if (p == NULL) |
| 1253 | { |
| 1254 | EMSG(_(e_noinstext)); |
| 1255 | return FAIL; |
| 1256 | } |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1257 | retval = put_in_typebuf(p, FALSE, colon, silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1258 | vim_free(p); |
| 1259 | } |
| 1260 | else |
| 1261 | { |
| 1262 | get_yank_register(regname, FALSE); |
| 1263 | if (y_current->y_array == NULL) |
| 1264 | return FAIL; |
| 1265 | |
| 1266 | /* Disallow remaping for ":@r". */ |
| 1267 | remap = colon ? REMAP_NONE : REMAP_YES; |
| 1268 | |
| 1269 | /* |
| 1270 | * Insert lines into typeahead buffer, from last one to first one. |
| 1271 | */ |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1272 | put_reedit_in_typebuf(silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1273 | for (i = y_current->y_size; --i >= 0; ) |
| 1274 | { |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1275 | char_u *escaped; |
| 1276 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1277 | /* insert NL between lines and after last line if type is MLINE */ |
| 1278 | if (y_current->y_type == MLINE || i < y_current->y_size - 1 |
| 1279 | || addcr) |
| 1280 | { |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1281 | if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1282 | return FAIL; |
| 1283 | } |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1284 | escaped = vim_strsave_escape_csi(y_current->y_array[i]); |
| 1285 | if (escaped == NULL) |
| 1286 | return FAIL; |
| 1287 | retval = ins_typebuf(escaped, remap, 0, TRUE, silent); |
| 1288 | vim_free(escaped); |
| 1289 | if (retval == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1290 | return FAIL; |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1291 | if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1292 | == FAIL) |
| 1293 | return FAIL; |
| 1294 | } |
| 1295 | Exec_reg = TRUE; /* disable the 'q' command */ |
| 1296 | } |
| 1297 | return retval; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's |
| 1302 | * used only after other typeahead has been processed. |
| 1303 | */ |
| 1304 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1305 | put_reedit_in_typebuf(int silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1306 | { |
| 1307 | char_u buf[3]; |
| 1308 | |
| 1309 | if (restart_edit != NUL) |
| 1310 | { |
| 1311 | if (restart_edit == 'V') |
| 1312 | { |
| 1313 | buf[0] = 'g'; |
| 1314 | buf[1] = 'R'; |
| 1315 | buf[2] = NUL; |
| 1316 | } |
| 1317 | else |
| 1318 | { |
| 1319 | buf[0] = restart_edit == 'I' ? 'i' : restart_edit; |
| 1320 | buf[1] = NUL; |
| 1321 | } |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1322 | if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1323 | restart_edit = NUL; |
| 1324 | } |
| 1325 | } |
| 1326 | |
Bram Moolenaar | 38ef43b | 2010-01-27 16:31:13 +0100 | [diff] [blame] | 1327 | /* |
| 1328 | * Insert register contents "s" into the typeahead buffer, so that it will be |
| 1329 | * executed again. |
| 1330 | * When "esc" is TRUE it is to be taken literally: Escape CSI characters and |
| 1331 | * no remapping. |
| 1332 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1333 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1334 | put_in_typebuf( |
| 1335 | char_u *s, |
| 1336 | int esc, |
| 1337 | int colon, /* add ':' before the line */ |
| 1338 | int silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1339 | { |
| 1340 | int retval = OK; |
| 1341 | |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 1342 | put_reedit_in_typebuf(silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1343 | if (colon) |
Bram Moolenaar | 38ef43b | 2010-01-27 16:31:13 +0100 | [diff] [blame] | 1344 | retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1345 | if (retval == OK) |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1346 | { |
| 1347 | char_u *p; |
| 1348 | |
| 1349 | if (esc) |
| 1350 | p = vim_strsave_escape_csi(s); |
| 1351 | else |
| 1352 | p = s; |
| 1353 | if (p == NULL) |
| 1354 | retval = FAIL; |
| 1355 | else |
Bram Moolenaar | 38ef43b | 2010-01-27 16:31:13 +0100 | [diff] [blame] | 1356 | retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES, |
| 1357 | 0, TRUE, silent); |
Bram Moolenaar | 7f6ca07 | 2007-02-27 16:21:38 +0000 | [diff] [blame] | 1358 | if (esc) |
| 1359 | vim_free(p); |
| 1360 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | if (colon && retval == OK) |
Bram Moolenaar | 38ef43b | 2010-01-27 16:31:13 +0100 | [diff] [blame] | 1362 | retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1363 | return retval; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * Insert a yank register: copy it into the Read buffer. |
| 1368 | * Used by CTRL-R command and middle mouse button in insert mode. |
| 1369 | * |
| 1370 | * return FAIL for failure, OK otherwise |
| 1371 | */ |
| 1372 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1373 | insert_reg( |
| 1374 | int regname, |
| 1375 | int literally) /* insert literally, not as if typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1376 | { |
| 1377 | long i; |
| 1378 | int retval = OK; |
| 1379 | char_u *arg; |
| 1380 | int allocated; |
| 1381 | |
| 1382 | /* |
| 1383 | * It is possible to get into an endless loop by having CTRL-R a in |
| 1384 | * register a and then, in insert mode, doing CTRL-R a. |
| 1385 | * If you hit CTRL-C, the loop will be broken here. |
| 1386 | */ |
| 1387 | ui_breakcheck(); |
| 1388 | if (got_int) |
| 1389 | return FAIL; |
| 1390 | |
| 1391 | /* check for valid regname */ |
| 1392 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
| 1393 | return FAIL; |
| 1394 | |
| 1395 | #ifdef FEAT_CLIPBOARD |
| 1396 | regname = may_get_selection(regname); |
| 1397 | #endif |
| 1398 | |
| 1399 | if (regname == '.') /* insert last inserted text */ |
| 1400 | retval = stuff_inserted(NUL, 1L, TRUE); |
| 1401 | else if (get_spec_reg(regname, &arg, &allocated, TRUE)) |
| 1402 | { |
| 1403 | if (arg == NULL) |
| 1404 | return FAIL; |
| 1405 | stuffescaped(arg, literally); |
| 1406 | if (allocated) |
| 1407 | vim_free(arg); |
| 1408 | } |
| 1409 | else /* name or number register */ |
| 1410 | { |
| 1411 | get_yank_register(regname, FALSE); |
| 1412 | if (y_current->y_array == NULL) |
| 1413 | retval = FAIL; |
| 1414 | else |
| 1415 | { |
| 1416 | for (i = 0; i < y_current->y_size; ++i) |
| 1417 | { |
| 1418 | stuffescaped(y_current->y_array[i], literally); |
| 1419 | /* |
| 1420 | * Insert a newline between lines and after last line if |
| 1421 | * y_type is MLINE. |
| 1422 | */ |
| 1423 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 1424 | stuffcharReadbuff('\n'); |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | return retval; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * Stuff a string into the typeahead buffer, such that edit() will insert it |
| 1434 | * literally ("literally" TRUE) or interpret is as typed characters. |
| 1435 | */ |
| 1436 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1437 | stuffescaped(char_u *arg, int literally) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1438 | { |
| 1439 | int c; |
| 1440 | char_u *start; |
| 1441 | |
| 1442 | while (*arg != NUL) |
| 1443 | { |
| 1444 | /* Stuff a sequence of normal ASCII characters, that's fast. Also |
| 1445 | * stuff K_SPECIAL to get the effect of a special key when "literally" |
| 1446 | * is TRUE. */ |
| 1447 | start = arg; |
| 1448 | while ((*arg >= ' ' |
| 1449 | #ifndef EBCDIC |
| 1450 | && *arg < DEL /* EBCDIC: chars above space are normal */ |
| 1451 | #endif |
| 1452 | ) |
| 1453 | || (*arg == K_SPECIAL && !literally)) |
| 1454 | ++arg; |
| 1455 | if (arg > start) |
| 1456 | stuffReadbuffLen(start, (long)(arg - start)); |
| 1457 | |
| 1458 | /* stuff a single special character */ |
| 1459 | if (*arg != NUL) |
| 1460 | { |
| 1461 | #ifdef FEAT_MBYTE |
| 1462 | if (has_mbyte) |
Bram Moolenaar | 3b1c485 | 2010-07-31 17:59:29 +0200 | [diff] [blame] | 1463 | c = mb_cptr2char_adv(&arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1464 | else |
| 1465 | #endif |
| 1466 | c = *arg++; |
| 1467 | if (literally && ((c < ' ' && c != TAB) || c == DEL)) |
| 1468 | stuffcharReadbuff(Ctrl_V); |
| 1469 | stuffcharReadbuff(c); |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | /* |
Bram Moolenaar | d55de22 | 2007-05-06 13:38:48 +0000 | [diff] [blame] | 1475 | * If "regname" is a special register, return TRUE and store a pointer to its |
| 1476 | * value in "argp". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1477 | */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 1478 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1479 | get_spec_reg( |
| 1480 | int regname, |
| 1481 | char_u **argp, |
| 1482 | int *allocated, /* return: TRUE when value was allocated */ |
| 1483 | int errmsg) /* give error message when failing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1484 | { |
| 1485 | int cnt; |
| 1486 | |
| 1487 | *argp = NULL; |
| 1488 | *allocated = FALSE; |
| 1489 | switch (regname) |
| 1490 | { |
| 1491 | case '%': /* file name */ |
| 1492 | if (errmsg) |
| 1493 | check_fname(); /* will give emsg if not set */ |
| 1494 | *argp = curbuf->b_fname; |
| 1495 | return TRUE; |
| 1496 | |
| 1497 | case '#': /* alternate file name */ |
| 1498 | *argp = getaltfname(errmsg); /* may give emsg if not set */ |
| 1499 | return TRUE; |
| 1500 | |
| 1501 | #ifdef FEAT_EVAL |
| 1502 | case '=': /* result of expression */ |
| 1503 | *argp = get_expr_line(); |
| 1504 | *allocated = TRUE; |
| 1505 | return TRUE; |
| 1506 | #endif |
| 1507 | |
| 1508 | case ':': /* last command line */ |
| 1509 | if (last_cmdline == NULL && errmsg) |
| 1510 | EMSG(_(e_nolastcmd)); |
| 1511 | *argp = last_cmdline; |
| 1512 | return TRUE; |
| 1513 | |
| 1514 | case '/': /* last search-pattern */ |
| 1515 | if (last_search_pat() == NULL && errmsg) |
| 1516 | EMSG(_(e_noprevre)); |
| 1517 | *argp = last_search_pat(); |
| 1518 | return TRUE; |
| 1519 | |
| 1520 | case '.': /* last inserted text */ |
| 1521 | *argp = get_last_insert_save(); |
| 1522 | *allocated = TRUE; |
| 1523 | if (*argp == NULL && errmsg) |
| 1524 | EMSG(_(e_noinstext)); |
| 1525 | return TRUE; |
| 1526 | |
| 1527 | #ifdef FEAT_SEARCHPATH |
| 1528 | case Ctrl_F: /* Filename under cursor */ |
| 1529 | case Ctrl_P: /* Path under cursor, expand via "path" */ |
| 1530 | if (!errmsg) |
| 1531 | return FALSE; |
| 1532 | *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 1533 | | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1534 | *allocated = TRUE; |
| 1535 | return TRUE; |
| 1536 | #endif |
| 1537 | |
| 1538 | case Ctrl_W: /* word under cursor */ |
| 1539 | case Ctrl_A: /* WORD (mnemonic All) under cursor */ |
| 1540 | if (!errmsg) |
| 1541 | return FALSE; |
| 1542 | cnt = find_ident_under_cursor(argp, regname == Ctrl_W |
| 1543 | ? (FIND_IDENT|FIND_STRING) : FIND_STRING); |
| 1544 | *argp = cnt ? vim_strnsave(*argp, cnt) : NULL; |
| 1545 | *allocated = TRUE; |
| 1546 | return TRUE; |
| 1547 | |
| 1548 | case '_': /* black hole: always empty */ |
| 1549 | *argp = (char_u *)""; |
| 1550 | return TRUE; |
| 1551 | } |
| 1552 | |
| 1553 | return FALSE; |
| 1554 | } |
| 1555 | |
| 1556 | /* |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 1557 | * Paste a yank register into the command line. |
| 1558 | * Only for non-special registers. |
| 1559 | * Used by CTRL-R command in command-line mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1560 | * insert_reg() can't be used here, because special characters from the |
| 1561 | * register contents will be interpreted as commands. |
| 1562 | * |
| 1563 | * return FAIL for failure, OK otherwise |
| 1564 | */ |
| 1565 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1566 | cmdline_paste_reg( |
| 1567 | int regname, |
| 1568 | int literally, /* Insert text literally instead of "as typed" */ |
| 1569 | int remcr) /* don't add CR characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1570 | { |
| 1571 | long i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1572 | |
| 1573 | get_yank_register(regname, FALSE); |
| 1574 | if (y_current->y_array == NULL) |
| 1575 | return FAIL; |
| 1576 | |
| 1577 | for (i = 0; i < y_current->y_size; ++i) |
| 1578 | { |
| 1579 | cmdline_paste_str(y_current->y_array[i], literally); |
| 1580 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1581 | /* Insert ^M between lines and after last line if type is MLINE. |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 1582 | * Don't do this when "remcr" is TRUE. */ |
| 1583 | 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] | 1584 | cmdline_paste_str((char_u *)"\r", literally); |
| 1585 | |
| 1586 | /* Check for CTRL-C, in case someone tries to paste a few thousand |
| 1587 | * lines and gets bored. */ |
| 1588 | ui_breakcheck(); |
| 1589 | if (got_int) |
| 1590 | return FAIL; |
| 1591 | } |
| 1592 | return OK; |
| 1593 | } |
| 1594 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1595 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 1596 | /* |
| 1597 | * Adjust the register name pointed to with "rp" for the clipboard being |
| 1598 | * used always and the clipboard being available. |
| 1599 | */ |
| 1600 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1601 | adjust_clip_reg(int *rp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1602 | { |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 1603 | /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard', |
| 1604 | * use '*' or '+' reg, respectively. "unnamedplus" prevails. */ |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 1605 | if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0)) |
| 1606 | { |
| 1607 | if (clip_unnamed != 0) |
| 1608 | *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available) |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 1609 | ? '+' : '*'; |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 1610 | else |
| 1611 | *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available) |
| 1612 | ? '+' : '*'; |
| 1613 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1614 | if (!clip_star.available && *rp == '*') |
| 1615 | *rp = 0; |
| 1616 | if (!clip_plus.available && *rp == '+') |
| 1617 | *rp = 0; |
| 1618 | } |
| 1619 | #endif |
| 1620 | |
| 1621 | /* |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1622 | * Handle a delete operation. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1623 | * |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1624 | * Return FAIL if undo failed, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1625 | */ |
| 1626 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1627 | op_delete(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1628 | { |
| 1629 | int n; |
| 1630 | linenr_T lnum; |
| 1631 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1632 | char_u *newp, *oldp; |
| 1633 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1634 | linenr_T old_lcount = curbuf->b_ml.ml_line_count; |
| 1635 | int did_yank = FALSE; |
Bram Moolenaar | 7c82130 | 2012-09-05 14:18:45 +0200 | [diff] [blame] | 1636 | int orig_regname = oap->regname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1637 | |
| 1638 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */ |
| 1639 | return OK; |
| 1640 | |
| 1641 | /* Nothing to delete, return here. Do prepare undo, for op_change(). */ |
| 1642 | if (oap->empty) |
| 1643 | return u_save_cursor(); |
| 1644 | |
| 1645 | if (!curbuf->b_p_ma) |
| 1646 | { |
| 1647 | EMSG(_(e_modifiable)); |
| 1648 | return FAIL; |
| 1649 | } |
| 1650 | |
| 1651 | #ifdef FEAT_CLIPBOARD |
| 1652 | adjust_clip_reg(&oap->regname); |
| 1653 | #endif |
| 1654 | |
| 1655 | #ifdef FEAT_MBYTE |
| 1656 | if (has_mbyte) |
| 1657 | mb_adjust_opend(oap); |
| 1658 | #endif |
| 1659 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1660 | /* |
| 1661 | * Imitate the strange Vi behaviour: If the delete spans more than one |
| 1662 | * line and motion_type == MCHAR and the result is a blank line, make the |
| 1663 | * delete linewise. Don't do this for the change command or Visual mode. |
| 1664 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1665 | if ( oap->motion_type == MCHAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1666 | && !oap->is_VIsual |
Bram Moolenaar | ec2dad6 | 2005-01-02 11:36:03 +0000 | [diff] [blame] | 1667 | && !oap->block_mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1668 | && oap->line_count > 1 |
Bram Moolenaar | 64a7230 | 2012-01-10 13:46:22 +0100 | [diff] [blame] | 1669 | && oap->motion_force == NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1670 | && oap->op_type == OP_DELETE) |
| 1671 | { |
Bram Moolenaar | 44286ca | 2011-07-15 17:51:34 +0200 | [diff] [blame] | 1672 | ptr = ml_get(oap->end.lnum) + oap->end.col; |
| 1673 | if (*ptr != NUL) |
| 1674 | ptr += oap->inclusive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1675 | ptr = skipwhite(ptr); |
| 1676 | if (*ptr == NUL && inindent(0)) |
| 1677 | oap->motion_type = MLINE; |
| 1678 | } |
| 1679 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1680 | /* |
| 1681 | * Check for trying to delete (e.g. "D") in an empty line. |
| 1682 | * Note: For the change operator it is ok. |
| 1683 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1684 | if ( oap->motion_type == MCHAR |
| 1685 | && oap->line_count == 1 |
| 1686 | && oap->op_type == OP_DELETE |
| 1687 | && *ml_get(oap->start.lnum) == NUL) |
| 1688 | { |
| 1689 | /* |
Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 1690 | * 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] | 1691 | * 'cpoptions' (Vi compatible). |
| 1692 | */ |
Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 1693 | #ifdef FEAT_VIRTUALEDIT |
| 1694 | if (virtual_op) |
| 1695 | /* Virtual editing: Nothing gets deleted, but we set the '[ and '] |
| 1696 | * marks as if it happened. */ |
| 1697 | goto setmarks; |
| 1698 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1699 | if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL) |
| 1700 | beep_flush(); |
| 1701 | return OK; |
| 1702 | } |
| 1703 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1704 | /* |
| 1705 | * Do a yank of whatever we're about to delete. |
| 1706 | * If a yank register was specified, put the deleted text into that |
| 1707 | * register. For the black hole register '_' don't yank anything. |
| 1708 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1709 | if (oap->regname != '_') |
| 1710 | { |
| 1711 | if (oap->regname != 0) |
| 1712 | { |
| 1713 | /* check for read-only register */ |
| 1714 | if (!valid_yank_reg(oap->regname, TRUE)) |
| 1715 | { |
| 1716 | beep_flush(); |
| 1717 | return OK; |
| 1718 | } |
| 1719 | get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */ |
| 1720 | if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */ |
| 1721 | did_yank = TRUE; |
| 1722 | } |
| 1723 | |
| 1724 | /* |
| 1725 | * Put deleted text into register 1 and shift number registers if the |
| 1726 | * delete contains a line break, or when a regname has been specified. |
Bram Moolenaar | 7c82130 | 2012-09-05 14:18:45 +0200 | [diff] [blame] | 1727 | * Use the register name from before adjust_clip_reg() may have |
| 1728 | * changed it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1729 | */ |
Bram Moolenaar | 7c82130 | 2012-09-05 14:18:45 +0200 | [diff] [blame] | 1730 | if (orig_regname != 0 || oap->motion_type == MLINE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1731 | || oap->line_count > 1 || oap->use_reg_one) |
| 1732 | { |
| 1733 | y_current = &y_regs[9]; |
| 1734 | free_yank_all(); /* free register nine */ |
| 1735 | for (n = 9; n > 1; --n) |
| 1736 | y_regs[n] = y_regs[n - 1]; |
| 1737 | y_previous = y_current = &y_regs[1]; |
| 1738 | y_regs[1].y_array = NULL; /* set register one to empty */ |
| 1739 | if (op_yank(oap, TRUE, FALSE) == OK) |
| 1740 | did_yank = TRUE; |
| 1741 | } |
| 1742 | |
Bram Moolenaar | 84298db | 2012-04-20 13:46:08 +0200 | [diff] [blame] | 1743 | /* Yank into small delete register when no named register specified |
| 1744 | * and the delete is within one line. */ |
| 1745 | if (( |
| 1746 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1747 | ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') || |
| 1748 | ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') || |
Bram Moolenaar | 84298db | 2012-04-20 13:46:08 +0200 | [diff] [blame] | 1749 | #endif |
| 1750 | oap->regname == 0) && oap->motion_type != MLINE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1751 | && oap->line_count == 1) |
| 1752 | { |
| 1753 | oap->regname = '-'; |
| 1754 | get_yank_register(oap->regname, TRUE); |
| 1755 | if (op_yank(oap, TRUE, FALSE) == OK) |
| 1756 | did_yank = TRUE; |
| 1757 | oap->regname = 0; |
| 1758 | } |
| 1759 | |
| 1760 | /* |
| 1761 | * If there's too much stuff to fit in the yank register, then get a |
| 1762 | * confirmation before doing the delete. This is crude, but simple. |
| 1763 | * And it avoids doing a delete of something we can't put back if we |
| 1764 | * want. |
| 1765 | */ |
| 1766 | if (!did_yank) |
| 1767 | { |
| 1768 | int msg_silent_save = msg_silent; |
| 1769 | |
| 1770 | msg_silent = 0; /* must display the prompt */ |
| 1771 | n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE); |
| 1772 | msg_silent = msg_silent_save; |
| 1773 | if (n != 'y') |
| 1774 | { |
| 1775 | EMSG(_(e_abort)); |
| 1776 | return FAIL; |
| 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 1781 | /* |
| 1782 | * block mode delete |
| 1783 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1784 | if (oap->block_mode) |
| 1785 | { |
| 1786 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 1787 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 1788 | return FAIL; |
| 1789 | |
| 1790 | for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum) |
| 1791 | { |
| 1792 | block_prep(oap, &bd, lnum, TRUE); |
| 1793 | if (bd.textlen == 0) /* nothing to delete */ |
| 1794 | continue; |
| 1795 | |
| 1796 | /* Adjust cursor position for tab replaced by spaces and 'lbr'. */ |
| 1797 | if (lnum == curwin->w_cursor.lnum) |
| 1798 | { |
| 1799 | curwin->w_cursor.col = bd.textcol + bd.startspaces; |
| 1800 | # ifdef FEAT_VIRTUALEDIT |
| 1801 | curwin->w_cursor.coladd = 0; |
| 1802 | # endif |
| 1803 | } |
| 1804 | |
| 1805 | /* n == number of chars deleted |
| 1806 | * If we delete a TAB, it may be replaced by several characters. |
| 1807 | * Thus the number of characters may increase! |
| 1808 | */ |
| 1809 | n = bd.textlen - bd.startspaces - bd.endspaces; |
| 1810 | oldp = ml_get(lnum); |
| 1811 | newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n); |
| 1812 | if (newp == NULL) |
| 1813 | continue; |
| 1814 | /* copy up to deleted part */ |
| 1815 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 1816 | /* insert spaces */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 1817 | vim_memset(newp + bd.textcol, ' ', |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1818 | (size_t)(bd.startspaces + bd.endspaces)); |
| 1819 | /* copy the part after the deleted part */ |
| 1820 | oldp += bd.textcol + bd.textlen; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 1821 | STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1822 | /* replace the line */ |
| 1823 | ml_replace(lnum, newp, FALSE); |
| 1824 | } |
| 1825 | |
| 1826 | check_cursor_col(); |
| 1827 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 1828 | oap->end.lnum + 1, 0L); |
| 1829 | oap->line_count = 0; /* no lines deleted */ |
| 1830 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1831 | else if (oap->motion_type == MLINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1832 | { |
| 1833 | if (oap->op_type == OP_CHANGE) |
| 1834 | { |
| 1835 | /* Delete the lines except the first one. Temporarily move the |
| 1836 | * cursor to the next line. Save the current line number, if the |
| 1837 | * last line is deleted it may be changed. |
| 1838 | */ |
| 1839 | if (oap->line_count > 1) |
| 1840 | { |
| 1841 | lnum = curwin->w_cursor.lnum; |
| 1842 | ++curwin->w_cursor.lnum; |
| 1843 | del_lines((long)(oap->line_count - 1), TRUE); |
| 1844 | curwin->w_cursor.lnum = lnum; |
| 1845 | } |
| 1846 | if (u_save_cursor() == FAIL) |
| 1847 | return FAIL; |
| 1848 | if (curbuf->b_p_ai) /* don't delete indent */ |
| 1849 | { |
| 1850 | beginline(BL_WHITE); /* cursor on first non-white */ |
| 1851 | did_ai = TRUE; /* delete the indent when ESC hit */ |
| 1852 | ai_col = curwin->w_cursor.col; |
| 1853 | } |
| 1854 | else |
| 1855 | beginline(0); /* cursor in column 0 */ |
| 1856 | truncate_line(FALSE); /* delete the rest of the line */ |
| 1857 | /* leave cursor past last char in line */ |
| 1858 | if (oap->line_count > 1) |
| 1859 | u_clearline(); /* "U" command not possible after "2cc" */ |
| 1860 | } |
| 1861 | else |
| 1862 | { |
| 1863 | del_lines(oap->line_count, TRUE); |
| 1864 | beginline(BL_WHITE | BL_FIX); |
| 1865 | u_clearline(); /* "U" command not possible after "dd" */ |
| 1866 | } |
| 1867 | } |
| 1868 | else |
| 1869 | { |
| 1870 | #ifdef FEAT_VIRTUALEDIT |
| 1871 | if (virtual_op) |
| 1872 | { |
| 1873 | int endcol = 0; |
| 1874 | |
| 1875 | /* For virtualedit: break the tabs that are partly included. */ |
| 1876 | if (gchar_pos(&oap->start) == '\t') |
| 1877 | { |
| 1878 | if (u_save_cursor() == FAIL) /* save first line for undo */ |
| 1879 | return FAIL; |
| 1880 | if (oap->line_count == 1) |
| 1881 | endcol = getviscol2(oap->end.col, oap->end.coladd); |
| 1882 | coladvance_force(getviscol2(oap->start.col, oap->start.coladd)); |
| 1883 | oap->start = curwin->w_cursor; |
| 1884 | if (oap->line_count == 1) |
| 1885 | { |
| 1886 | coladvance(endcol); |
| 1887 | oap->end.col = curwin->w_cursor.col; |
| 1888 | oap->end.coladd = curwin->w_cursor.coladd; |
| 1889 | curwin->w_cursor = oap->start; |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | /* Break a tab only when it's included in the area. */ |
| 1894 | if (gchar_pos(&oap->end) == '\t' |
| 1895 | && (int)oap->end.coladd < oap->inclusive) |
| 1896 | { |
| 1897 | /* save last line for undo */ |
| 1898 | if (u_save((linenr_T)(oap->end.lnum - 1), |
| 1899 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 1900 | return FAIL; |
| 1901 | curwin->w_cursor = oap->end; |
| 1902 | coladvance_force(getviscol2(oap->end.col, oap->end.coladd)); |
| 1903 | oap->end = curwin->w_cursor; |
| 1904 | curwin->w_cursor = oap->start; |
| 1905 | } |
| 1906 | } |
| 1907 | #endif |
| 1908 | |
| 1909 | if (oap->line_count == 1) /* delete characters within one line */ |
| 1910 | { |
| 1911 | if (u_save_cursor() == FAIL) /* save line for undo */ |
| 1912 | return FAIL; |
| 1913 | |
| 1914 | /* if 'cpoptions' contains '$', display '$' at end of change */ |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1915 | if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1916 | && oap->op_type == OP_CHANGE |
| 1917 | && oap->end.lnum == curwin->w_cursor.lnum |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1918 | && !oap->is_VIsual) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1919 | display_dollar(oap->end.col - !oap->inclusive); |
| 1920 | |
| 1921 | n = oap->end.col - oap->start.col + 1 - !oap->inclusive; |
| 1922 | |
| 1923 | #ifdef FEAT_VIRTUALEDIT |
| 1924 | if (virtual_op) |
| 1925 | { |
| 1926 | /* fix up things for virtualedit-delete: |
| 1927 | * break the tabs which are going to get in our way |
| 1928 | */ |
| 1929 | char_u *curline = ml_get_curline(); |
| 1930 | int len = (int)STRLEN(curline); |
| 1931 | |
| 1932 | if (oap->end.coladd != 0 |
| 1933 | && (int)oap->end.col >= len - 1 |
| 1934 | && !(oap->start.coladd && (int)oap->end.col >= len - 1)) |
| 1935 | n++; |
| 1936 | /* Delete at least one char (e.g, when on a control char). */ |
| 1937 | if (n == 0 && oap->start.coladd != oap->end.coladd) |
| 1938 | n = 1; |
| 1939 | |
| 1940 | /* When deleted a char in the line, reset coladd. */ |
| 1941 | if (gchar_cursor() != NUL) |
| 1942 | curwin->w_cursor.coladd = 0; |
| 1943 | } |
| 1944 | #endif |
Bram Moolenaar | d009e86 | 2015-06-09 20:20:03 +0200 | [diff] [blame] | 1945 | (void)del_bytes((long)n, !virtual_op, |
| 1946 | oap->op_type == OP_DELETE && !oap->is_VIsual); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1947 | } |
| 1948 | else /* delete characters between lines */ |
| 1949 | { |
| 1950 | pos_T curpos; |
| 1951 | |
| 1952 | /* save deleted and changed lines for undo */ |
| 1953 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 1954 | (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL) |
| 1955 | return FAIL; |
| 1956 | |
| 1957 | truncate_line(TRUE); /* delete from cursor to end of line */ |
| 1958 | |
| 1959 | curpos = curwin->w_cursor; /* remember curwin->w_cursor */ |
| 1960 | ++curwin->w_cursor.lnum; |
| 1961 | del_lines((long)(oap->line_count - 2), FALSE); |
| 1962 | |
Bram Moolenaar | d009e86 | 2015-06-09 20:20:03 +0200 | [diff] [blame] | 1963 | /* delete from start of line until op_end */ |
Bram Moolenaar | 44286ca | 2011-07-15 17:51:34 +0200 | [diff] [blame] | 1964 | n = (oap->end.col + 1 - !oap->inclusive); |
Bram Moolenaar | d009e86 | 2015-06-09 20:20:03 +0200 | [diff] [blame] | 1965 | curwin->w_cursor.col = 0; |
| 1966 | (void)del_bytes((long)n, !virtual_op, |
| 1967 | oap->op_type == OP_DELETE && !oap->is_VIsual); |
| 1968 | curwin->w_cursor = curpos; /* restore curwin->w_cursor */ |
| 1969 | (void)do_join(2, FALSE, FALSE, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | msgmore(curbuf->b_ml.ml_line_count - old_lcount); |
| 1974 | |
Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 1975 | #ifdef FEAT_VIRTUALEDIT |
| 1976 | setmarks: |
| 1977 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1978 | if (oap->block_mode) |
| 1979 | { |
| 1980 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 1981 | curbuf->b_op_end.col = oap->start.col; |
| 1982 | } |
| 1983 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1984 | curbuf->b_op_end = oap->start; |
| 1985 | curbuf->b_op_start = oap->start; |
| 1986 | |
| 1987 | return OK; |
| 1988 | } |
| 1989 | |
| 1990 | #ifdef FEAT_MBYTE |
| 1991 | /* |
| 1992 | * Adjust end of operating area for ending on a multi-byte character. |
| 1993 | * Used for deletion. |
| 1994 | */ |
| 1995 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1996 | mb_adjust_opend(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1997 | { |
| 1998 | char_u *p; |
| 1999 | |
| 2000 | if (oap->inclusive) |
| 2001 | { |
| 2002 | p = ml_get(oap->end.lnum); |
| 2003 | oap->end.col += mb_tail_off(p, p + oap->end.col); |
| 2004 | } |
| 2005 | } |
| 2006 | #endif |
| 2007 | |
| 2008 | #if defined(FEAT_VISUALEXTRA) || defined(PROTO) |
| 2009 | /* |
| 2010 | * Replace a whole area with one character. |
| 2011 | */ |
| 2012 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2013 | op_replace(oparg_T *oap, int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2014 | { |
| 2015 | int n, numc; |
| 2016 | #ifdef FEAT_MBYTE |
| 2017 | int num_chars; |
| 2018 | #endif |
| 2019 | char_u *newp, *oldp; |
| 2020 | size_t oldlen; |
| 2021 | struct block_def bd; |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2022 | char_u *after_p = NULL; |
| 2023 | int had_ctrl_v_cr = (c == -1 || c == -2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2024 | |
| 2025 | if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty) |
| 2026 | return OK; /* nothing to do */ |
| 2027 | |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2028 | if (had_ctrl_v_cr) |
| 2029 | c = (c == -1 ? '\r' : '\n'); |
| 2030 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2031 | #ifdef FEAT_MBYTE |
| 2032 | if (has_mbyte) |
| 2033 | mb_adjust_opend(oap); |
| 2034 | #endif |
| 2035 | |
| 2036 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 2037 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 2038 | return FAIL; |
| 2039 | |
| 2040 | /* |
| 2041 | * block mode replace |
| 2042 | */ |
| 2043 | if (oap->block_mode) |
| 2044 | { |
| 2045 | bd.is_MAX = (curwin->w_curswant == MAXCOL); |
| 2046 | for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum) |
| 2047 | { |
Bram Moolenaar | a1381de | 2009-11-03 15:44:21 +0000 | [diff] [blame] | 2048 | curwin->w_cursor.col = 0; /* make sure cursor position is valid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2049 | block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); |
| 2050 | if (bd.textlen == 0 && (!virtual_op || bd.is_MAX)) |
| 2051 | continue; /* nothing to replace */ |
| 2052 | |
| 2053 | /* n == number of extra chars required |
| 2054 | * If we split a TAB, it may be replaced by several characters. |
| 2055 | * Thus the number of characters may increase! |
| 2056 | */ |
| 2057 | #ifdef FEAT_VIRTUALEDIT |
| 2058 | /* If the range starts in virtual space, count the initial |
| 2059 | * coladd offset as part of "startspaces" */ |
| 2060 | if (virtual_op && bd.is_short && *bd.textstart == NUL) |
| 2061 | { |
| 2062 | pos_T vpos; |
| 2063 | |
Bram Moolenaar | a1381de | 2009-11-03 15:44:21 +0000 | [diff] [blame] | 2064 | vpos.lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | getvpos(&vpos, oap->start_vcol); |
| 2066 | bd.startspaces += vpos.coladd; |
| 2067 | n = bd.startspaces; |
| 2068 | } |
| 2069 | else |
| 2070 | #endif |
| 2071 | /* allow for pre spaces */ |
| 2072 | n = (bd.startspaces ? bd.start_char_vcols - 1 : 0); |
| 2073 | |
| 2074 | /* allow for post spp */ |
| 2075 | n += (bd.endspaces |
| 2076 | #ifdef FEAT_VIRTUALEDIT |
| 2077 | && !bd.is_oneChar |
| 2078 | #endif |
| 2079 | && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0; |
| 2080 | /* Figure out how many characters to replace. */ |
| 2081 | numc = oap->end_vcol - oap->start_vcol + 1; |
| 2082 | if (bd.is_short && (!virtual_op || bd.is_MAX)) |
| 2083 | numc -= (oap->end_vcol - bd.end_vcol) + 1; |
| 2084 | |
| 2085 | #ifdef FEAT_MBYTE |
| 2086 | /* A double-wide character can be replaced only up to half the |
| 2087 | * times. */ |
| 2088 | if ((*mb_char2cells)(c) > 1) |
| 2089 | { |
| 2090 | if ((numc & 1) && !bd.is_short) |
| 2091 | { |
| 2092 | ++bd.endspaces; |
| 2093 | ++n; |
| 2094 | } |
| 2095 | numc = numc / 2; |
| 2096 | } |
| 2097 | |
| 2098 | /* Compute bytes needed, move character count to num_chars. */ |
| 2099 | num_chars = numc; |
| 2100 | numc *= (*mb_char2len)(c); |
| 2101 | #endif |
| 2102 | /* oldlen includes textlen, so don't double count */ |
| 2103 | n += numc - bd.textlen; |
| 2104 | |
| 2105 | oldp = ml_get_curline(); |
| 2106 | oldlen = STRLEN(oldp); |
| 2107 | newp = alloc_check((unsigned)oldlen + 1 + n); |
| 2108 | if (newp == NULL) |
| 2109 | continue; |
| 2110 | vim_memset(newp, NUL, (size_t)(oldlen + 1 + n)); |
| 2111 | /* copy up to deleted part */ |
| 2112 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 2113 | oldp += bd.textcol + bd.textlen; |
| 2114 | /* insert pre-spaces */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 2115 | vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2116 | /* insert replacement chars CHECK FOR ALLOCATED SPACE */ |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2117 | /* -1/-2 is used for entering CR literally. */ |
| 2118 | if (had_ctrl_v_cr || (c != '\r' && c != '\n')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2119 | { |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2120 | #ifdef FEAT_MBYTE |
| 2121 | if (has_mbyte) |
| 2122 | { |
| 2123 | n = (int)STRLEN(newp); |
| 2124 | while (--num_chars >= 0) |
| 2125 | n += (*mb_char2bytes)(c, newp + n); |
| 2126 | } |
| 2127 | else |
| 2128 | #endif |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 2129 | vim_memset(newp + STRLEN(newp), c, (size_t)numc); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2130 | if (!bd.is_short) |
| 2131 | { |
| 2132 | /* insert post-spaces */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 2133 | vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2134 | /* copy the part after the changed part */ |
| 2135 | STRMOVE(newp + STRLEN(newp), oldp); |
| 2136 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2137 | } |
| 2138 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2139 | { |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2140 | /* Replacing with \r or \n means splitting the line. */ |
Bram Moolenaar | efe06f4 | 2013-11-11 23:17:39 +0100 | [diff] [blame] | 2141 | after_p = alloc_check( |
| 2142 | (unsigned)(oldlen + 1 + n - STRLEN(newp))); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2143 | if (after_p != NULL) |
| 2144 | STRMOVE(after_p, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2145 | } |
| 2146 | /* replace the line */ |
| 2147 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 2148 | if (after_p != NULL) |
| 2149 | { |
| 2150 | ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE); |
| 2151 | appended_lines_mark(curwin->w_cursor.lnum, 1L); |
| 2152 | oap->end.lnum++; |
| 2153 | vim_free(after_p); |
| 2154 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | else |
| 2158 | { |
| 2159 | /* |
| 2160 | * MCHAR and MLINE motion replace. |
| 2161 | */ |
| 2162 | if (oap->motion_type == MLINE) |
| 2163 | { |
| 2164 | oap->start.col = 0; |
| 2165 | curwin->w_cursor.col = 0; |
| 2166 | oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 2167 | if (oap->end.col) |
| 2168 | --oap->end.col; |
| 2169 | } |
| 2170 | else if (!oap->inclusive) |
| 2171 | dec(&(oap->end)); |
| 2172 | |
| 2173 | while (ltoreq(curwin->w_cursor, oap->end)) |
| 2174 | { |
| 2175 | n = gchar_cursor(); |
| 2176 | if (n != NUL) |
| 2177 | { |
| 2178 | #ifdef FEAT_MBYTE |
| 2179 | if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1) |
| 2180 | { |
| 2181 | /* This is slow, but it handles replacing a single-byte |
| 2182 | * with a multi-byte and the other way around. */ |
Bram Moolenaar | db81395 | 2013-03-07 18:50:57 +0100 | [diff] [blame] | 2183 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 2184 | oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2185 | n = State; |
| 2186 | State = REPLACE; |
| 2187 | ins_char(c); |
| 2188 | State = n; |
| 2189 | /* Backup to the replaced character. */ |
| 2190 | dec_cursor(); |
| 2191 | } |
| 2192 | else |
| 2193 | #endif |
| 2194 | { |
| 2195 | #ifdef FEAT_VIRTUALEDIT |
| 2196 | if (n == TAB) |
| 2197 | { |
| 2198 | int end_vcol = 0; |
| 2199 | |
| 2200 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 2201 | { |
| 2202 | /* oap->end has to be recalculated when |
| 2203 | * the tab breaks */ |
| 2204 | end_vcol = getviscol2(oap->end.col, |
| 2205 | oap->end.coladd); |
| 2206 | } |
| 2207 | coladvance_force(getviscol()); |
| 2208 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 2209 | getvpos(&oap->end, end_vcol); |
| 2210 | } |
| 2211 | #endif |
| 2212 | pchar(curwin->w_cursor, c); |
| 2213 | } |
| 2214 | } |
| 2215 | #ifdef FEAT_VIRTUALEDIT |
| 2216 | else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum) |
| 2217 | { |
| 2218 | int virtcols = oap->end.coladd; |
| 2219 | |
| 2220 | if (curwin->w_cursor.lnum == oap->start.lnum |
| 2221 | && oap->start.col == oap->end.col && oap->start.coladd) |
| 2222 | virtcols -= oap->start.coladd; |
| 2223 | |
| 2224 | /* oap->end has been trimmed so it's effectively inclusive; |
| 2225 | * as a result an extra +1 must be counted so we don't |
| 2226 | * trample the NUL byte. */ |
| 2227 | coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1); |
| 2228 | curwin->w_cursor.col -= (virtcols + 1); |
| 2229 | for (; virtcols >= 0; virtcols--) |
| 2230 | { |
| 2231 | pchar(curwin->w_cursor, c); |
| 2232 | if (inc(&curwin->w_cursor) == -1) |
| 2233 | break; |
| 2234 | } |
| 2235 | } |
| 2236 | #endif |
| 2237 | |
| 2238 | /* Advance to next character, stop at the end of the file. */ |
| 2239 | if (inc_cursor() == -1) |
| 2240 | break; |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | curwin->w_cursor = oap->start; |
| 2245 | check_cursor(); |
| 2246 | changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L); |
| 2247 | |
| 2248 | /* Set "'[" and "']" marks. */ |
| 2249 | curbuf->b_op_start = oap->start; |
| 2250 | curbuf->b_op_end = oap->end; |
| 2251 | |
| 2252 | return OK; |
| 2253 | } |
| 2254 | #endif |
| 2255 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 2256 | static int swapchars(int op_type, pos_T *pos, int length); |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2257 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | /* |
| 2259 | * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?". |
| 2260 | */ |
| 2261 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2262 | op_tilde(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2263 | { |
| 2264 | pos_T pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2265 | struct block_def bd; |
Bram Moolenaar | e60c58d | 2008-02-06 13:44:43 +0000 | [diff] [blame] | 2266 | int did_change = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2267 | |
| 2268 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 2269 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 2270 | return; |
| 2271 | |
| 2272 | pos = oap->start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2273 | if (oap->block_mode) /* Visual block mode */ |
| 2274 | { |
| 2275 | for (; pos.lnum <= oap->end.lnum; ++pos.lnum) |
| 2276 | { |
Bram Moolenaar | 555b3d5 | 2008-12-03 12:38:36 +0000 | [diff] [blame] | 2277 | int one_change; |
| 2278 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2279 | block_prep(oap, &bd, pos.lnum, FALSE); |
| 2280 | pos.col = bd.textcol; |
Bram Moolenaar | 555b3d5 | 2008-12-03 12:38:36 +0000 | [diff] [blame] | 2281 | one_change = swapchars(oap->op_type, &pos, bd.textlen); |
| 2282 | did_change |= one_change; |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2283 | |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 2284 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2285 | if (netbeans_active() && one_change) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2286 | { |
| 2287 | char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 2288 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2289 | netbeans_removed(curbuf, pos.lnum, bd.textcol, |
| 2290 | (long)bd.textlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2291 | netbeans_inserted(curbuf, pos.lnum, bd.textcol, |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2292 | &ptr[bd.textcol], bd.textlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2293 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 2294 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2295 | } |
| 2296 | if (did_change) |
| 2297 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
| 2298 | } |
| 2299 | else /* not block mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2300 | { |
| 2301 | if (oap->motion_type == MLINE) |
| 2302 | { |
| 2303 | oap->start.col = 0; |
| 2304 | pos.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 | e60c58d | 2008-02-06 13:44:43 +0000 | [diff] [blame] | 2312 | if (pos.lnum == oap->end.lnum) |
| 2313 | did_change = swapchars(oap->op_type, &pos, |
| 2314 | oap->end.col - pos.col + 1); |
| 2315 | else |
| 2316 | for (;;) |
| 2317 | { |
| 2318 | did_change |= swapchars(oap->op_type, &pos, |
| 2319 | pos.lnum == oap->end.lnum ? oap->end.col + 1: |
| 2320 | (int)STRLEN(ml_get_pos(&pos))); |
| 2321 | if (ltoreq(oap->end, pos) || inc(&pos) == -1) |
| 2322 | break; |
| 2323 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2324 | if (did_change) |
| 2325 | { |
| 2326 | changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, |
| 2327 | 0L); |
| 2328 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2329 | if (netbeans_active() && did_change) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2330 | { |
| 2331 | char_u *ptr; |
| 2332 | int count; |
| 2333 | |
| 2334 | pos = oap->start; |
| 2335 | while (pos.lnum < oap->end.lnum) |
| 2336 | { |
| 2337 | ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2338 | count = (int)STRLEN(ptr) - pos.col; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2339 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2340 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2341 | &ptr[pos.col], count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2342 | pos.col = 0; |
| 2343 | pos.lnum++; |
| 2344 | } |
| 2345 | ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 2346 | count = oap->end.col - pos.col + 1; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2347 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2348 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2349 | &ptr[pos.col], count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2350 | } |
| 2351 | #endif |
| 2352 | } |
| 2353 | } |
| 2354 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2355 | if (!did_change && oap->is_VIsual) |
| 2356 | /* No change: need to remove the Visual selection */ |
| 2357 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2358 | |
| 2359 | /* |
| 2360 | * Set '[ and '] marks. |
| 2361 | */ |
| 2362 | curbuf->b_op_start = oap->start; |
| 2363 | curbuf->b_op_end = oap->end; |
| 2364 | |
| 2365 | if (oap->line_count > p_report) |
| 2366 | { |
| 2367 | if (oap->line_count == 1) |
| 2368 | MSG(_("1 line changed")); |
| 2369 | else |
| 2370 | smsg((char_u *)_("%ld lines changed"), oap->line_count); |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | /* |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2375 | * Invoke swapchar() on "length" bytes at position "pos". |
| 2376 | * "pos" is advanced to just after the changed characters. |
| 2377 | * "length" is rounded up to include the whole last multi-byte character. |
| 2378 | * Also works correctly when the number of bytes changes. |
| 2379 | * Returns TRUE if some character was changed. |
| 2380 | */ |
| 2381 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2382 | swapchars(int op_type, pos_T *pos, int length) |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2383 | { |
| 2384 | int todo; |
| 2385 | int did_change = 0; |
| 2386 | |
| 2387 | for (todo = length; todo > 0; --todo) |
| 2388 | { |
| 2389 | # ifdef FEAT_MBYTE |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2390 | if (has_mbyte) |
Bram Moolenaar | f17968b | 2013-08-09 19:48:40 +0200 | [diff] [blame] | 2391 | { |
| 2392 | int len = (*mb_ptr2len)(ml_get_pos(pos)); |
| 2393 | |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2394 | /* we're counting bytes, not characters */ |
Bram Moolenaar | f17968b | 2013-08-09 19:48:40 +0200 | [diff] [blame] | 2395 | if (len > 0) |
| 2396 | todo -= len - 1; |
| 2397 | } |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2398 | # endif |
| 2399 | did_change |= swapchar(op_type, pos); |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2400 | if (inc(pos) == -1) /* at end of file */ |
| 2401 | break; |
| 2402 | } |
| 2403 | return did_change; |
| 2404 | } |
| 2405 | |
| 2406 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2407 | * If op_type == OP_UPPER: make uppercase, |
| 2408 | * if op_type == OP_LOWER: make lowercase, |
| 2409 | * if op_type == OP_ROT13: do rot13 encoding, |
| 2410 | * else swap case of character at 'pos' |
| 2411 | * returns TRUE when something actually changed. |
| 2412 | */ |
| 2413 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2414 | swapchar(int op_type, pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2415 | { |
| 2416 | int c; |
| 2417 | int nc; |
| 2418 | |
| 2419 | c = gchar_pos(pos); |
| 2420 | |
| 2421 | /* Only do rot13 encoding for ASCII characters. */ |
| 2422 | if (c >= 0x80 && op_type == OP_ROT13) |
| 2423 | return FALSE; |
| 2424 | |
| 2425 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 2426 | if (op_type == OP_UPPER && c == 0xdf |
| 2427 | && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0)) |
Bram Moolenaar | 6f16eb8 | 2005-08-23 21:02:42 +0000 | [diff] [blame] | 2428 | { |
| 2429 | pos_T sp = curwin->w_cursor; |
| 2430 | |
| 2431 | /* Special handling of German sharp s: change to "SS". */ |
| 2432 | curwin->w_cursor = *pos; |
| 2433 | del_char(FALSE); |
| 2434 | ins_char('S'); |
| 2435 | ins_char('S'); |
| 2436 | curwin->w_cursor = sp; |
| 2437 | inc(pos); |
| 2438 | } |
| 2439 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2440 | if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */ |
| 2441 | return FALSE; |
| 2442 | #endif |
| 2443 | nc = c; |
| 2444 | if (MB_ISLOWER(c)) |
| 2445 | { |
| 2446 | if (op_type == OP_ROT13) |
| 2447 | nc = ROT13(c, 'a'); |
| 2448 | else if (op_type != OP_LOWER) |
| 2449 | nc = MB_TOUPPER(c); |
| 2450 | } |
| 2451 | else if (MB_ISUPPER(c)) |
| 2452 | { |
| 2453 | if (op_type == OP_ROT13) |
| 2454 | nc = ROT13(c, 'A'); |
| 2455 | else if (op_type != OP_UPPER) |
| 2456 | nc = MB_TOLOWER(c); |
| 2457 | } |
| 2458 | if (nc != c) |
| 2459 | { |
| 2460 | #ifdef FEAT_MBYTE |
| 2461 | if (enc_utf8 && (c >= 0x80 || nc >= 0x80)) |
| 2462 | { |
| 2463 | pos_T sp = curwin->w_cursor; |
| 2464 | |
| 2465 | curwin->w_cursor = *pos; |
Bram Moolenaar | d1cb65e | 2010-08-01 14:22:48 +0200 | [diff] [blame] | 2466 | /* don't use del_char(), it also removes composing chars */ |
| 2467 | del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2468 | ins_char(nc); |
| 2469 | curwin->w_cursor = sp; |
| 2470 | } |
| 2471 | else |
| 2472 | #endif |
| 2473 | pchar(*pos, nc); |
| 2474 | return TRUE; |
| 2475 | } |
| 2476 | return FALSE; |
| 2477 | } |
| 2478 | |
| 2479 | #if defined(FEAT_VISUALEXTRA) || defined(PROTO) |
| 2480 | /* |
| 2481 | * op_insert - Insert and append operators for Visual mode. |
| 2482 | */ |
| 2483 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2484 | op_insert(oparg_T *oap, long count1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | { |
| 2486 | long ins_len, pre_textlen = 0; |
| 2487 | char_u *firstline, *ins_text; |
| 2488 | struct block_def bd; |
| 2489 | int i; |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2490 | pos_T t1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2491 | |
| 2492 | /* edit() changes this - record it for OP_APPEND */ |
| 2493 | bd.is_MAX = (curwin->w_curswant == MAXCOL); |
| 2494 | |
| 2495 | /* vis block is still marked. Get rid of it now. */ |
| 2496 | curwin->w_cursor.lnum = oap->start.lnum; |
| 2497 | update_screen(INVERTED); |
| 2498 | |
| 2499 | if (oap->block_mode) |
| 2500 | { |
| 2501 | #ifdef FEAT_VIRTUALEDIT |
| 2502 | /* When 'virtualedit' is used, need to insert the extra spaces before |
| 2503 | * doing block_prep(). When only "block" is used, virtual edit is |
| 2504 | * already disabled, but still need it when calling |
| 2505 | * coladvance_force(). */ |
| 2506 | if (curwin->w_cursor.coladd > 0) |
| 2507 | { |
| 2508 | int old_ve_flags = ve_flags; |
| 2509 | |
| 2510 | ve_flags = VE_ALL; |
| 2511 | if (u_save_cursor() == FAIL) |
| 2512 | return; |
| 2513 | coladvance_force(oap->op_type == OP_APPEND |
| 2514 | ? oap->end_vcol + 1 : getviscol()); |
| 2515 | if (oap->op_type == OP_APPEND) |
| 2516 | --curwin->w_cursor.col; |
| 2517 | ve_flags = old_ve_flags; |
| 2518 | } |
| 2519 | #endif |
| 2520 | /* Get the info about the block before entering the text */ |
| 2521 | block_prep(oap, &bd, oap->start.lnum, TRUE); |
| 2522 | firstline = ml_get(oap->start.lnum) + bd.textcol; |
| 2523 | if (oap->op_type == OP_APPEND) |
| 2524 | firstline += bd.textlen; |
| 2525 | pre_textlen = (long)STRLEN(firstline); |
| 2526 | } |
| 2527 | |
| 2528 | if (oap->op_type == OP_APPEND) |
| 2529 | { |
| 2530 | if (oap->block_mode |
| 2531 | #ifdef FEAT_VIRTUALEDIT |
| 2532 | && curwin->w_cursor.coladd == 0 |
| 2533 | #endif |
| 2534 | ) |
| 2535 | { |
| 2536 | /* Move the cursor to the character right of the block. */ |
| 2537 | curwin->w_set_curswant = TRUE; |
| 2538 | while (*ml_get_cursor() != NUL |
| 2539 | && (curwin->w_cursor.col < bd.textcol + bd.textlen)) |
| 2540 | ++curwin->w_cursor.col; |
| 2541 | if (bd.is_short && !bd.is_MAX) |
| 2542 | { |
| 2543 | /* First line was too short, make it longer and adjust the |
| 2544 | * values in "bd". */ |
| 2545 | if (u_save_cursor() == FAIL) |
| 2546 | return; |
| 2547 | for (i = 0; i < bd.endspaces; ++i) |
| 2548 | ins_char(' '); |
| 2549 | bd.textlen += bd.endspaces; |
| 2550 | } |
| 2551 | } |
| 2552 | else |
| 2553 | { |
| 2554 | curwin->w_cursor = oap->end; |
Bram Moolenaar | 6a584dc | 2006-06-20 18:29:34 +0000 | [diff] [blame] | 2555 | check_cursor_col(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2556 | |
| 2557 | /* Works just like an 'i'nsert on the next character. */ |
| 2558 | if (!lineempty(curwin->w_cursor.lnum) |
| 2559 | && oap->start_vcol != oap->end_vcol) |
| 2560 | inc_cursor(); |
| 2561 | } |
| 2562 | } |
| 2563 | |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2564 | t1 = oap->start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2565 | edit(NUL, FALSE, (linenr_T)count1); |
| 2566 | |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2567 | /* When a tab was inserted, and the characters in front of the tab |
| 2568 | * have been converted to a tab as well, the column of the cursor |
| 2569 | * might have actually been reduced, so need to adjust here. */ |
| 2570 | if (t1.lnum == curbuf->b_op_start_orig.lnum |
| 2571 | && lt(curbuf->b_op_start_orig, t1)) |
| 2572 | oap->start = curbuf->b_op_start_orig; |
| 2573 | |
Bram Moolenaar | aacbb00 | 2008-01-03 15:34:40 +0000 | [diff] [blame] | 2574 | /* If user has moved off this line, we don't know what to do, so do |
| 2575 | * nothing. |
| 2576 | * Also don't repeat the insert when Insert mode ended with CTRL-C. */ |
| 2577 | if (curwin->w_cursor.lnum != oap->start.lnum || got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2578 | return; |
| 2579 | |
| 2580 | if (oap->block_mode) |
| 2581 | { |
| 2582 | struct block_def bd2; |
| 2583 | |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2584 | /* The user may have moved the cursor before inserting something, try |
| 2585 | * to adjust the block for that. */ |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 2586 | if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX) |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2587 | { |
| 2588 | if (oap->op_type == OP_INSERT |
Bram Moolenaar | 4c9a949 | 2014-03-19 18:57:54 +0100 | [diff] [blame] | 2589 | && oap->start.col |
| 2590 | #ifdef FEAT_VIRTUALEDIT |
| 2591 | + oap->start.coladd |
| 2592 | #endif |
| 2593 | != curbuf->b_op_start_orig.col |
| 2594 | #ifdef FEAT_VIRTUALEDIT |
| 2595 | + curbuf->b_op_start_orig.coladd |
| 2596 | #endif |
| 2597 | ) |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2598 | { |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2599 | int t = getviscol2(curbuf->b_op_start_orig.col, |
| 2600 | curbuf->b_op_start_orig.coladd); |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 2601 | oap->start.col = curbuf->b_op_start_orig.col; |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2602 | pre_textlen -= t - oap->start_vcol; |
| 2603 | oap->start_vcol = t; |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2604 | } |
| 2605 | else if (oap->op_type == OP_APPEND |
Bram Moolenaar | 4c9a949 | 2014-03-19 18:57:54 +0100 | [diff] [blame] | 2606 | && oap->end.col |
| 2607 | #ifdef FEAT_VIRTUALEDIT |
| 2608 | + oap->end.coladd |
| 2609 | #endif |
| 2610 | >= curbuf->b_op_start_orig.col |
| 2611 | #ifdef FEAT_VIRTUALEDIT |
| 2612 | + curbuf->b_op_start_orig.coladd |
| 2613 | #endif |
| 2614 | ) |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2615 | { |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2616 | int t = getviscol2(curbuf->b_op_start_orig.col, |
| 2617 | curbuf->b_op_start_orig.coladd); |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 2618 | oap->start.col = curbuf->b_op_start_orig.col; |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2619 | /* reset pre_textlen to the value of OP_INSERT */ |
| 2620 | pre_textlen += bd.textlen; |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 2621 | pre_textlen -= t - oap->start_vcol; |
| 2622 | oap->start_vcol = t; |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 2623 | oap->op_type = OP_INSERT; |
| 2624 | } |
| 2625 | } |
| 2626 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2627 | /* |
| 2628 | * 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] | 2629 | * tabs. Get the starting column again and correct the length. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2630 | * Don't do this when "$" used, end-of-line will have changed. |
| 2631 | */ |
| 2632 | block_prep(oap, &bd2, oap->start.lnum, TRUE); |
| 2633 | if (!bd.is_MAX || bd2.textlen < bd.textlen) |
| 2634 | { |
| 2635 | if (oap->op_type == OP_APPEND) |
| 2636 | { |
| 2637 | pre_textlen += bd2.textlen - bd.textlen; |
| 2638 | if (bd2.endspaces) |
| 2639 | --bd2.textlen; |
| 2640 | } |
| 2641 | bd.textcol = bd2.textcol; |
| 2642 | bd.textlen = bd2.textlen; |
| 2643 | } |
| 2644 | |
| 2645 | /* |
| 2646 | * Subsequent calls to ml_get() flush the firstline data - take a |
| 2647 | * copy of the required string. |
| 2648 | */ |
| 2649 | firstline = ml_get(oap->start.lnum) + bd.textcol; |
| 2650 | if (oap->op_type == OP_APPEND) |
| 2651 | firstline += bd.textlen; |
Bram Moolenaar | 5e4b9e9 | 2012-03-23 14:16:23 +0100 | [diff] [blame] | 2652 | if (pre_textlen >= 0 |
| 2653 | && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2654 | { |
| 2655 | ins_text = vim_strnsave(firstline, (int)ins_len); |
| 2656 | if (ins_text != NULL) |
| 2657 | { |
| 2658 | /* block handled here */ |
| 2659 | if (u_save(oap->start.lnum, |
| 2660 | (linenr_T)(oap->end.lnum + 1)) == OK) |
| 2661 | block_insert(oap, ins_text, (oap->op_type == OP_INSERT), |
| 2662 | &bd); |
| 2663 | |
| 2664 | curwin->w_cursor.col = oap->start.col; |
| 2665 | check_cursor(); |
| 2666 | vim_free(ins_text); |
| 2667 | } |
| 2668 | } |
| 2669 | } |
| 2670 | } |
| 2671 | #endif |
| 2672 | |
| 2673 | /* |
| 2674 | * op_change - handle a change operation |
| 2675 | * |
| 2676 | * return TRUE if edit() returns because of a CTRL-O command |
| 2677 | */ |
| 2678 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2679 | op_change(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2680 | { |
| 2681 | colnr_T l; |
| 2682 | int retval; |
| 2683 | #ifdef FEAT_VISUALEXTRA |
| 2684 | long offset; |
| 2685 | linenr_T linenr; |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 2686 | long ins_len; |
| 2687 | long pre_textlen = 0; |
| 2688 | long pre_indent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2689 | char_u *firstline; |
| 2690 | char_u *ins_text, *newp, *oldp; |
| 2691 | struct block_def bd; |
| 2692 | #endif |
| 2693 | |
| 2694 | l = oap->start.col; |
| 2695 | if (oap->motion_type == MLINE) |
| 2696 | { |
| 2697 | l = 0; |
| 2698 | #ifdef FEAT_SMARTINDENT |
| 2699 | if (!p_paste && curbuf->b_p_si |
| 2700 | # ifdef FEAT_CINDENT |
| 2701 | && !curbuf->b_p_cin |
| 2702 | # endif |
| 2703 | ) |
| 2704 | can_si = TRUE; /* It's like opening a new line, do si */ |
| 2705 | #endif |
| 2706 | } |
| 2707 | |
| 2708 | /* First delete the text in the region. In an empty buffer only need to |
| 2709 | * save for undo */ |
| 2710 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 2711 | { |
| 2712 | if (u_save_cursor() == FAIL) |
| 2713 | return FALSE; |
| 2714 | } |
| 2715 | else if (op_delete(oap) == FAIL) |
| 2716 | return FALSE; |
| 2717 | |
| 2718 | if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum) |
| 2719 | && !virtual_op) |
| 2720 | inc_cursor(); |
| 2721 | |
| 2722 | #ifdef FEAT_VISUALEXTRA |
| 2723 | /* check for still on same line (<CR> in inserted text meaningless) */ |
| 2724 | /* skip blank lines too */ |
| 2725 | if (oap->block_mode) |
| 2726 | { |
| 2727 | # ifdef FEAT_VIRTUALEDIT |
| 2728 | /* Add spaces before getting the current line length. */ |
| 2729 | if (virtual_op && (curwin->w_cursor.coladd > 0 |
| 2730 | || gchar_cursor() == NUL)) |
| 2731 | coladvance_force(getviscol()); |
| 2732 | # endif |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 2733 | firstline = ml_get(oap->start.lnum); |
| 2734 | pre_textlen = (long)STRLEN(firstline); |
| 2735 | pre_indent = (long)(skipwhite(firstline) - firstline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2736 | bd.textcol = curwin->w_cursor.col; |
| 2737 | } |
| 2738 | #endif |
| 2739 | |
| 2740 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
| 2741 | if (oap->motion_type == MLINE) |
| 2742 | fix_indent(); |
| 2743 | #endif |
| 2744 | |
| 2745 | retval = edit(NUL, FALSE, (linenr_T)1); |
| 2746 | |
| 2747 | #ifdef FEAT_VISUALEXTRA |
| 2748 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2749 | * 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] | 2750 | * block. |
Bram Moolenaar | aacbb00 | 2008-01-03 15:34:40 +0000 | [diff] [blame] | 2751 | * Don't repeat the insert when Insert mode ended with CTRL-C. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2752 | */ |
Bram Moolenaar | aacbb00 | 2008-01-03 15:34:40 +0000 | [diff] [blame] | 2753 | if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2754 | { |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 2755 | /* Auto-indenting may have changed the indent. If the cursor was past |
| 2756 | * the indent, exclude that indent change from the inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2757 | firstline = ml_get(oap->start.lnum); |
Bram Moolenaar | b8dc4d4 | 2007-09-25 12:20:19 +0000 | [diff] [blame] | 2758 | if (bd.textcol > (colnr_T)pre_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2759 | { |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 2760 | long new_indent = (long)(skipwhite(firstline) - firstline); |
| 2761 | |
| 2762 | pre_textlen += new_indent - pre_indent; |
| 2763 | bd.textcol += new_indent - pre_indent; |
| 2764 | } |
| 2765 | |
| 2766 | ins_len = (long)STRLEN(firstline) - pre_textlen; |
| 2767 | if (ins_len > 0) |
| 2768 | { |
| 2769 | /* Subsequent calls to ml_get() flush the firstline data - take a |
| 2770 | * copy of the inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2771 | if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL) |
| 2772 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2773 | vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2774 | for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum; |
| 2775 | linenr++) |
| 2776 | { |
| 2777 | block_prep(oap, &bd, linenr, TRUE); |
| 2778 | if (!bd.is_short || virtual_op) |
| 2779 | { |
| 2780 | # ifdef FEAT_VIRTUALEDIT |
| 2781 | pos_T vpos; |
| 2782 | |
| 2783 | /* If the block starts in virtual space, count the |
| 2784 | * initial coladd offset as part of "startspaces" */ |
| 2785 | if (bd.is_short) |
| 2786 | { |
Bram Moolenaar | a1381de | 2009-11-03 15:44:21 +0000 | [diff] [blame] | 2787 | vpos.lnum = linenr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2788 | (void)getvpos(&vpos, oap->start_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2789 | } |
| 2790 | else |
| 2791 | vpos.coladd = 0; |
| 2792 | # endif |
| 2793 | oldp = ml_get(linenr); |
| 2794 | newp = alloc_check((unsigned)(STRLEN(oldp) |
| 2795 | # ifdef FEAT_VIRTUALEDIT |
| 2796 | + vpos.coladd |
| 2797 | # endif |
| 2798 | + ins_len + 1)); |
| 2799 | if (newp == NULL) |
| 2800 | continue; |
| 2801 | /* copy up to block start */ |
| 2802 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 2803 | offset = bd.textcol; |
| 2804 | # ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 2805 | vim_memset(newp + offset, ' ', (size_t)vpos.coladd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2806 | offset += vpos.coladd; |
| 2807 | # endif |
| 2808 | mch_memmove(newp + offset, ins_text, (size_t)ins_len); |
| 2809 | offset += ins_len; |
| 2810 | oldp += bd.textcol; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 2811 | STRMOVE(newp + offset, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2812 | ml_replace(linenr, newp, FALSE); |
| 2813 | } |
| 2814 | } |
| 2815 | check_cursor(); |
| 2816 | |
| 2817 | changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); |
| 2818 | } |
| 2819 | vim_free(ins_text); |
| 2820 | } |
| 2821 | } |
| 2822 | #endif |
| 2823 | |
| 2824 | return retval; |
| 2825 | } |
| 2826 | |
| 2827 | /* |
| 2828 | * set all the yank registers to empty (called from main()) |
| 2829 | */ |
| 2830 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2831 | init_yank(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2832 | { |
| 2833 | int i; |
| 2834 | |
| 2835 | for (i = 0; i < NUM_REGISTERS; ++i) |
| 2836 | y_regs[i].y_array = NULL; |
| 2837 | } |
| 2838 | |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 2839 | #if defined(EXITFREE) || defined(PROTO) |
| 2840 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2841 | clear_registers(void) |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 2842 | { |
| 2843 | int i; |
| 2844 | |
| 2845 | for (i = 0; i < NUM_REGISTERS; ++i) |
| 2846 | { |
| 2847 | y_current = &y_regs[i]; |
| 2848 | if (y_current->y_array != NULL) |
| 2849 | free_yank_all(); |
| 2850 | } |
| 2851 | } |
| 2852 | #endif |
| 2853 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2854 | /* |
| 2855 | * Free "n" lines from the current yank register. |
| 2856 | * Called for normal freeing and in case of error. |
| 2857 | */ |
| 2858 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2859 | free_yank(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2860 | { |
| 2861 | if (y_current->y_array != NULL) |
| 2862 | { |
| 2863 | long i; |
| 2864 | |
| 2865 | for (i = n; --i >= 0; ) |
| 2866 | { |
| 2867 | #ifdef AMIGA /* only for very slow machines */ |
| 2868 | if ((i & 1023) == 1023) /* this may take a while */ |
| 2869 | { |
| 2870 | /* |
| 2871 | * This message should never cause a hit-return message. |
| 2872 | * Overwrite this message with any next message. |
| 2873 | */ |
| 2874 | ++no_wait_return; |
| 2875 | smsg((char_u *)_("freeing %ld lines"), i + 1); |
| 2876 | --no_wait_return; |
| 2877 | msg_didout = FALSE; |
| 2878 | msg_col = 0; |
| 2879 | } |
| 2880 | #endif |
| 2881 | vim_free(y_current->y_array[i]); |
| 2882 | } |
| 2883 | vim_free(y_current->y_array); |
| 2884 | y_current->y_array = NULL; |
| 2885 | #ifdef AMIGA |
| 2886 | if (n >= 1000) |
| 2887 | MSG(""); |
| 2888 | #endif |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2893 | free_yank_all(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2894 | { |
| 2895 | free_yank(y_current->y_size); |
| 2896 | } |
| 2897 | |
| 2898 | /* |
| 2899 | * Yank the text between "oap->start" and "oap->end" into a yank register. |
| 2900 | * If we are to append (uppercase register), we first yank into a new yank |
| 2901 | * register and then concatenate the old and the new one (so we keep the old |
| 2902 | * one in case of out-of-memory). |
| 2903 | * |
Bram Moolenaar | 1a4a75c | 2013-07-28 16:03:06 +0200 | [diff] [blame] | 2904 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2905 | */ |
| 2906 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2907 | op_yank(oparg_T *oap, int deleting, int mess) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2908 | { |
| 2909 | long y_idx; /* index in y_array[] */ |
| 2910 | struct yankreg *curr; /* copy of y_current */ |
| 2911 | struct yankreg newreg; /* new yank register when appending */ |
| 2912 | char_u **new_ptr; |
| 2913 | linenr_T lnum; /* current line number */ |
| 2914 | long j; |
| 2915 | int yanktype = oap->motion_type; |
| 2916 | long yanklines = oap->line_count; |
| 2917 | linenr_T yankendlnum = oap->end.lnum; |
| 2918 | char_u *p; |
| 2919 | char_u *pnew; |
| 2920 | struct block_def bd; |
Bram Moolenaar | 9c52c3a | 2010-12-08 14:23:15 +0100 | [diff] [blame] | 2921 | #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 2922 | int did_star = FALSE; |
Bram Moolenaar | 9c52c3a | 2010-12-08 14:23:15 +0100 | [diff] [blame] | 2923 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2924 | |
| 2925 | /* check for read-only register */ |
| 2926 | if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE)) |
| 2927 | { |
| 2928 | beep_flush(); |
| 2929 | return FAIL; |
| 2930 | } |
| 2931 | if (oap->regname == '_') /* black hole: nothing to do */ |
| 2932 | return OK; |
| 2933 | |
| 2934 | #ifdef FEAT_CLIPBOARD |
| 2935 | if (!clip_star.available && oap->regname == '*') |
| 2936 | oap->regname = 0; |
| 2937 | else if (!clip_plus.available && oap->regname == '+') |
| 2938 | oap->regname = 0; |
| 2939 | #endif |
| 2940 | |
| 2941 | if (!deleting) /* op_delete() already set y_current */ |
| 2942 | get_yank_register(oap->regname, TRUE); |
| 2943 | |
| 2944 | curr = y_current; |
| 2945 | /* append to existing contents */ |
| 2946 | if (y_append && y_current->y_array != NULL) |
| 2947 | y_current = &newreg; |
| 2948 | else |
| 2949 | free_yank_all(); /* free previously yanked lines */ |
| 2950 | |
Bram Moolenaar | 2c7a29c | 2005-12-12 22:02:31 +0000 | [diff] [blame] | 2951 | /* |
| 2952 | * If the cursor was in column 1 before and after the movement, and the |
| 2953 | * operator is not inclusive, the yank is always linewise. |
| 2954 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2955 | if ( oap->motion_type == MCHAR |
| 2956 | && oap->start.col == 0 |
| 2957 | && !oap->inclusive |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2958 | && (!oap->is_VIsual || *p_sel == 'o') |
Bram Moolenaar | ec2dad6 | 2005-01-02 11:36:03 +0000 | [diff] [blame] | 2959 | && !oap->block_mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2960 | && oap->end.col == 0 |
| 2961 | && yanklines > 1) |
| 2962 | { |
| 2963 | yanktype = MLINE; |
| 2964 | --yankendlnum; |
| 2965 | --yanklines; |
| 2966 | } |
| 2967 | |
| 2968 | y_current->y_size = yanklines; |
| 2969 | y_current->y_type = yanktype; /* set the yank register type */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2970 | y_current->y_width = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2971 | y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) * |
| 2972 | yanklines), TRUE); |
| 2973 | |
| 2974 | if (y_current->y_array == NULL) |
| 2975 | { |
| 2976 | y_current = curr; |
| 2977 | return FAIL; |
| 2978 | } |
| 2979 | |
| 2980 | y_idx = 0; |
| 2981 | lnum = oap->start.lnum; |
| 2982 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2983 | if (oap->block_mode) |
| 2984 | { |
| 2985 | /* Visual block mode */ |
| 2986 | y_current->y_type = MBLOCK; /* set the yank register type */ |
| 2987 | y_current->y_width = oap->end_vcol - oap->start_vcol; |
| 2988 | |
| 2989 | if (curwin->w_curswant == MAXCOL && y_current->y_width > 0) |
| 2990 | y_current->y_width--; |
| 2991 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2992 | |
| 2993 | for ( ; lnum <= yankendlnum; lnum++, y_idx++) |
| 2994 | { |
| 2995 | switch (y_current->y_type) |
| 2996 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2997 | case MBLOCK: |
| 2998 | block_prep(oap, &bd, lnum, FALSE); |
| 2999 | if (yank_copy_line(&bd, y_idx) == FAIL) |
| 3000 | goto fail; |
| 3001 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3002 | |
| 3003 | case MLINE: |
| 3004 | if ((y_current->y_array[y_idx] = |
| 3005 | vim_strsave(ml_get(lnum))) == NULL) |
| 3006 | goto fail; |
| 3007 | break; |
| 3008 | |
| 3009 | case MCHAR: |
| 3010 | { |
| 3011 | colnr_T startcol = 0, endcol = MAXCOL; |
| 3012 | #ifdef FEAT_VIRTUALEDIT |
| 3013 | int is_oneChar = FALSE; |
| 3014 | colnr_T cs, ce; |
| 3015 | #endif |
| 3016 | p = ml_get(lnum); |
| 3017 | bd.startspaces = 0; |
| 3018 | bd.endspaces = 0; |
| 3019 | |
| 3020 | if (lnum == oap->start.lnum) |
| 3021 | { |
| 3022 | startcol = oap->start.col; |
| 3023 | #ifdef FEAT_VIRTUALEDIT |
| 3024 | if (virtual_op) |
| 3025 | { |
| 3026 | getvcol(curwin, &oap->start, &cs, NULL, &ce); |
| 3027 | if (ce != cs && oap->start.coladd > 0) |
| 3028 | { |
| 3029 | /* Part of a tab selected -- but don't |
| 3030 | * double-count it. */ |
| 3031 | bd.startspaces = (ce - cs + 1) |
| 3032 | - oap->start.coladd; |
| 3033 | startcol++; |
| 3034 | } |
| 3035 | } |
| 3036 | #endif |
| 3037 | } |
| 3038 | |
| 3039 | if (lnum == oap->end.lnum) |
| 3040 | { |
| 3041 | endcol = oap->end.col; |
| 3042 | #ifdef FEAT_VIRTUALEDIT |
| 3043 | if (virtual_op) |
| 3044 | { |
| 3045 | getvcol(curwin, &oap->end, &cs, NULL, &ce); |
| 3046 | if (p[endcol] == NUL || (cs + oap->end.coladd < ce |
| 3047 | # ifdef FEAT_MBYTE |
| 3048 | /* Don't add space for double-wide |
| 3049 | * char; endcol will be on last byte |
| 3050 | * of multi-byte char. */ |
| 3051 | && (*mb_head_off)(p, p + endcol) == 0 |
| 3052 | # endif |
| 3053 | )) |
| 3054 | { |
| 3055 | if (oap->start.lnum == oap->end.lnum |
| 3056 | && oap->start.col == oap->end.col) |
| 3057 | { |
| 3058 | /* Special case: inside a single char */ |
| 3059 | is_oneChar = TRUE; |
| 3060 | bd.startspaces = oap->end.coladd |
| 3061 | - oap->start.coladd + oap->inclusive; |
| 3062 | endcol = startcol; |
| 3063 | } |
| 3064 | else |
| 3065 | { |
| 3066 | bd.endspaces = oap->end.coladd |
| 3067 | + oap->inclusive; |
| 3068 | endcol -= oap->inclusive; |
| 3069 | } |
| 3070 | } |
| 3071 | } |
| 3072 | #endif |
| 3073 | } |
Bram Moolenaar | 18e00d2 | 2012-05-18 12:49:40 +0200 | [diff] [blame] | 3074 | if (endcol == MAXCOL) |
| 3075 | endcol = (colnr_T)STRLEN(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3076 | if (startcol > endcol |
| 3077 | #ifdef FEAT_VIRTUALEDIT |
| 3078 | || is_oneChar |
| 3079 | #endif |
| 3080 | ) |
| 3081 | bd.textlen = 0; |
| 3082 | else |
| 3083 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3084 | bd.textlen = endcol - startcol + oap->inclusive; |
| 3085 | } |
| 3086 | bd.textstart = p + startcol; |
| 3087 | if (yank_copy_line(&bd, y_idx) == FAIL) |
| 3088 | goto fail; |
| 3089 | break; |
| 3090 | } |
| 3091 | /* NOTREACHED */ |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | if (curr != y_current) /* append the new block to the old block */ |
| 3096 | { |
| 3097 | new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) * |
| 3098 | (curr->y_size + y_current->y_size)), TRUE); |
| 3099 | if (new_ptr == NULL) |
| 3100 | goto fail; |
| 3101 | for (j = 0; j < curr->y_size; ++j) |
| 3102 | new_ptr[j] = curr->y_array[j]; |
| 3103 | vim_free(curr->y_array); |
| 3104 | curr->y_array = new_ptr; |
| 3105 | |
| 3106 | if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */ |
| 3107 | curr->y_type = MLINE; |
| 3108 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 3109 | /* Concatenate the last line of the old block with the first line of |
| 3110 | * the new block, unless being Vi compatible. */ |
| 3111 | if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3112 | { |
| 3113 | pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1]) |
| 3114 | + STRLEN(y_current->y_array[0]) + 1), TRUE); |
| 3115 | if (pnew == NULL) |
| 3116 | { |
| 3117 | y_idx = y_current->y_size - 1; |
| 3118 | goto fail; |
| 3119 | } |
| 3120 | STRCPY(pnew, curr->y_array[--j]); |
| 3121 | STRCAT(pnew, y_current->y_array[0]); |
| 3122 | vim_free(curr->y_array[j]); |
| 3123 | vim_free(y_current->y_array[0]); |
| 3124 | curr->y_array[j++] = pnew; |
| 3125 | y_idx = 1; |
| 3126 | } |
| 3127 | else |
| 3128 | y_idx = 0; |
| 3129 | while (y_idx < y_current->y_size) |
| 3130 | curr->y_array[j++] = y_current->y_array[y_idx++]; |
| 3131 | curr->y_size = j; |
| 3132 | vim_free(y_current->y_array); |
| 3133 | y_current = curr; |
| 3134 | } |
Bram Moolenaar | 7ec8343 | 2014-06-17 18:16:11 +0200 | [diff] [blame] | 3135 | if (curwin->w_p_rnu) |
| 3136 | redraw_later(SOME_VALID); /* cursor moved to start */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3137 | if (mess) /* Display message about yank? */ |
| 3138 | { |
| 3139 | if (yanktype == MCHAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3140 | && !oap->block_mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3141 | && yanklines == 1) |
| 3142 | yanklines = 0; |
| 3143 | /* Some versions of Vi use ">=" here, some don't... */ |
| 3144 | if (yanklines > p_report) |
| 3145 | { |
| 3146 | /* redisplay now, so message is not deleted */ |
| 3147 | update_topline_redraw(); |
| 3148 | if (yanklines == 1) |
Bram Moolenaar | e2cc970 | 2005-03-15 22:43:58 +0000 | [diff] [blame] | 3149 | { |
Bram Moolenaar | e2cc970 | 2005-03-15 22:43:58 +0000 | [diff] [blame] | 3150 | if (oap->block_mode) |
| 3151 | MSG(_("block of 1 line yanked")); |
| 3152 | else |
Bram Moolenaar | e2cc970 | 2005-03-15 22:43:58 +0000 | [diff] [blame] | 3153 | MSG(_("1 line yanked")); |
| 3154 | } |
Bram Moolenaar | e2cc970 | 2005-03-15 22:43:58 +0000 | [diff] [blame] | 3155 | else if (oap->block_mode) |
| 3156 | smsg((char_u *)_("block of %ld lines yanked"), yanklines); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3157 | else |
| 3158 | smsg((char_u *)_("%ld lines yanked"), yanklines); |
| 3159 | } |
| 3160 | } |
| 3161 | |
| 3162 | /* |
| 3163 | * Set "'[" and "']" marks. |
| 3164 | */ |
| 3165 | curbuf->b_op_start = oap->start; |
| 3166 | curbuf->b_op_end = oap->end; |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 3167 | if (yanktype == MLINE && !oap->block_mode) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 3168 | { |
| 3169 | curbuf->b_op_start.col = 0; |
| 3170 | curbuf->b_op_end.col = MAXCOL; |
| 3171 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3172 | |
| 3173 | #ifdef FEAT_CLIPBOARD |
| 3174 | /* |
| 3175 | * If we were yanking to the '*' register, send result to clipboard. |
| 3176 | * If no register was specified, and "unnamed" in 'clipboard', make a copy |
| 3177 | * to the '*' register. |
| 3178 | */ |
| 3179 | if (clip_star.available |
| 3180 | && (curr == &(y_regs[STAR_REGISTER]) |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 3181 | || (!deleting && oap->regname == 0 |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 3182 | && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3183 | { |
| 3184 | if (curr != &(y_regs[STAR_REGISTER])) |
| 3185 | /* Copy the text from register 0 to the clipboard register. */ |
| 3186 | copy_yank_reg(&(y_regs[STAR_REGISTER])); |
| 3187 | |
| 3188 | clip_own_selection(&clip_star); |
| 3189 | clip_gen_set_selection(&clip_star); |
Bram Moolenaar | 9c52c3a | 2010-12-08 14:23:15 +0100 | [diff] [blame] | 3190 | # ifdef FEAT_X11 |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 3191 | did_star = TRUE; |
Bram Moolenaar | 9c52c3a | 2010-12-08 14:23:15 +0100 | [diff] [blame] | 3192 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | # ifdef FEAT_X11 |
| 3196 | /* |
| 3197 | * If we were yanking to the '+' register, send result to selection. |
| 3198 | * Also copy to the '*' register, in case auto-select is off. |
| 3199 | */ |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 3200 | if (clip_plus.available |
| 3201 | && (curr == &(y_regs[PLUS_REGISTER]) |
| 3202 | || (!deleting && oap->regname == 0 |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 3203 | && ((clip_unnamed | clip_unnamed_saved) & |
| 3204 | CLIP_UNNAMED_PLUS)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3205 | { |
Bram Moolenaar | bf9680e | 2010-12-02 21:43:16 +0100 | [diff] [blame] | 3206 | if (curr != &(y_regs[PLUS_REGISTER])) |
| 3207 | /* Copy the text from register 0 to the clipboard register. */ |
| 3208 | copy_yank_reg(&(y_regs[PLUS_REGISTER])); |
| 3209 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3210 | clip_own_selection(&clip_plus); |
| 3211 | clip_gen_set_selection(&clip_plus); |
Bram Moolenaar | c0885aa | 2012-07-10 16:49:23 +0200 | [diff] [blame] | 3212 | if (!clip_isautosel_star() && !did_star |
| 3213 | && curr == &(y_regs[PLUS_REGISTER])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3214 | { |
| 3215 | copy_yank_reg(&(y_regs[STAR_REGISTER])); |
| 3216 | clip_own_selection(&clip_star); |
| 3217 | clip_gen_set_selection(&clip_star); |
| 3218 | } |
| 3219 | } |
| 3220 | # endif |
| 3221 | #endif |
| 3222 | |
| 3223 | return OK; |
| 3224 | |
| 3225 | fail: /* free the allocated lines */ |
| 3226 | free_yank(y_idx + 1); |
| 3227 | y_current = curr; |
| 3228 | return FAIL; |
| 3229 | } |
| 3230 | |
| 3231 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3232 | yank_copy_line(struct block_def *bd, long y_idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3233 | { |
| 3234 | char_u *pnew; |
| 3235 | |
| 3236 | if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1)) |
| 3237 | == NULL) |
| 3238 | return FAIL; |
| 3239 | y_current->y_array[y_idx] = pnew; |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3240 | vim_memset(pnew, ' ', (size_t)bd->startspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3241 | pnew += bd->startspaces; |
| 3242 | mch_memmove(pnew, bd->textstart, (size_t)bd->textlen); |
| 3243 | pnew += bd->textlen; |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3244 | vim_memset(pnew, ' ', (size_t)bd->endspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3245 | pnew += bd->endspaces; |
| 3246 | *pnew = NUL; |
| 3247 | return OK; |
| 3248 | } |
| 3249 | |
| 3250 | #ifdef FEAT_CLIPBOARD |
| 3251 | /* |
| 3252 | * Make a copy of the y_current register to register "reg". |
| 3253 | */ |
| 3254 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3255 | copy_yank_reg(struct yankreg *reg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3256 | { |
| 3257 | struct yankreg *curr = y_current; |
| 3258 | long j; |
| 3259 | |
| 3260 | y_current = reg; |
| 3261 | free_yank_all(); |
| 3262 | *y_current = *curr; |
| 3263 | y_current->y_array = (char_u **)lalloc_clear( |
| 3264 | (long_u)(sizeof(char_u *) * y_current->y_size), TRUE); |
| 3265 | if (y_current->y_array == NULL) |
| 3266 | y_current->y_size = 0; |
| 3267 | else |
| 3268 | for (j = 0; j < y_current->y_size; ++j) |
| 3269 | if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL) |
| 3270 | { |
| 3271 | free_yank(j); |
| 3272 | y_current->y_size = 0; |
| 3273 | break; |
| 3274 | } |
| 3275 | y_current = curr; |
| 3276 | } |
| 3277 | #endif |
| 3278 | |
| 3279 | /* |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 3280 | * Put contents of register "regname" into the text. |
| 3281 | * Caller must check "regname" to be valid! |
| 3282 | * "flags": PUT_FIXINDENT make indent look nice |
| 3283 | * PUT_CURSEND leave cursor after end of new text |
| 3284 | * PUT_LINE force linewise put (":put") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3285 | */ |
| 3286 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3287 | do_put( |
| 3288 | int regname, |
| 3289 | int dir, /* BACKWARD for 'P', FORWARD for 'p' */ |
| 3290 | long count, |
| 3291 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3292 | { |
| 3293 | char_u *ptr; |
| 3294 | char_u *newp, *oldp; |
| 3295 | int yanklen; |
| 3296 | int totlen = 0; /* init for gcc */ |
| 3297 | linenr_T lnum; |
| 3298 | colnr_T col; |
| 3299 | long i; /* index in y_array[] */ |
| 3300 | int y_type; |
| 3301 | long y_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3302 | int oldlen; |
| 3303 | long y_width = 0; |
| 3304 | colnr_T vcol; |
| 3305 | int delcount; |
| 3306 | int incr = 0; |
| 3307 | long j; |
| 3308 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3309 | char_u **y_array = NULL; |
| 3310 | long nr_lines = 0; |
| 3311 | pos_T new_cursor; |
| 3312 | int indent; |
| 3313 | int orig_indent = 0; /* init for gcc */ |
| 3314 | int indent_diff = 0; /* init for gcc */ |
| 3315 | int first_indent = TRUE; |
| 3316 | int lendiff = 0; |
| 3317 | pos_T old_pos; |
| 3318 | char_u *insert_string = NULL; |
| 3319 | int allocated = FALSE; |
| 3320 | long cnt; |
| 3321 | |
| 3322 | #ifdef FEAT_CLIPBOARD |
| 3323 | /* Adjust register name for "unnamed" in 'clipboard'. */ |
| 3324 | adjust_clip_reg(®name); |
| 3325 | (void)may_get_selection(regname); |
| 3326 | #endif |
| 3327 | |
| 3328 | if (flags & PUT_FIXINDENT) |
| 3329 | orig_indent = get_indent(); |
| 3330 | |
| 3331 | curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ |
| 3332 | curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ |
| 3333 | |
| 3334 | /* |
| 3335 | * Using inserted text works differently, because the register includes |
| 3336 | * special characters (newlines, etc.). |
| 3337 | */ |
| 3338 | if (regname == '.') |
| 3339 | { |
| 3340 | (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') : |
| 3341 | (count == -1 ? 'O' : 'i')), count, FALSE); |
| 3342 | /* Putting the text is done later, so can't really move the cursor to |
| 3343 | * the next character. Use "l" to simulate it. */ |
| 3344 | if ((flags & PUT_CURSEND) && gchar_cursor() != NUL) |
| 3345 | stuffcharReadbuff('l'); |
| 3346 | return; |
| 3347 | } |
| 3348 | |
| 3349 | /* |
| 3350 | * For special registers '%' (file name), '#' (alternate file name) and |
| 3351 | * ':' (last command line), etc. we have to create a fake yank register. |
| 3352 | */ |
| 3353 | if (get_spec_reg(regname, &insert_string, &allocated, TRUE)) |
| 3354 | { |
| 3355 | if (insert_string == NULL) |
| 3356 | return; |
| 3357 | } |
| 3358 | |
Bram Moolenaar | 27356ad | 2012-12-12 16:11:36 +0100 | [diff] [blame] | 3359 | #ifdef FEAT_AUTOCMD |
| 3360 | /* Autocommands may be executed when saving lines for undo, which may make |
| 3361 | * y_array invalid. Start undo now to avoid that. */ |
| 3362 | u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1); |
| 3363 | #endif |
| 3364 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3365 | if (insert_string != NULL) |
| 3366 | { |
| 3367 | y_type = MCHAR; |
| 3368 | #ifdef FEAT_EVAL |
| 3369 | if (regname == '=') |
| 3370 | { |
| 3371 | /* For the = register we need to split the string at NL |
Bram Moolenaar | a554a19 | 2011-09-21 17:33:53 +0200 | [diff] [blame] | 3372 | * characters. |
| 3373 | * Loop twice: count the number of lines and save them. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3374 | for (;;) |
| 3375 | { |
| 3376 | y_size = 0; |
| 3377 | ptr = insert_string; |
| 3378 | while (ptr != NULL) |
| 3379 | { |
| 3380 | if (y_array != NULL) |
| 3381 | y_array[y_size] = ptr; |
| 3382 | ++y_size; |
| 3383 | ptr = vim_strchr(ptr, '\n'); |
| 3384 | if (ptr != NULL) |
| 3385 | { |
| 3386 | if (y_array != NULL) |
| 3387 | *ptr = NUL; |
| 3388 | ++ptr; |
Bram Moolenaar | a554a19 | 2011-09-21 17:33:53 +0200 | [diff] [blame] | 3389 | /* A trailing '\n' makes the register linewise. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3390 | if (*ptr == NUL) |
| 3391 | { |
| 3392 | y_type = MLINE; |
| 3393 | break; |
| 3394 | } |
| 3395 | } |
| 3396 | } |
| 3397 | if (y_array != NULL) |
| 3398 | break; |
| 3399 | y_array = (char_u **)alloc((unsigned) |
| 3400 | (y_size * sizeof(char_u *))); |
| 3401 | if (y_array == NULL) |
| 3402 | goto end; |
| 3403 | } |
| 3404 | } |
| 3405 | else |
| 3406 | #endif |
| 3407 | { |
| 3408 | y_size = 1; /* use fake one-line yank register */ |
| 3409 | y_array = &insert_string; |
| 3410 | } |
| 3411 | } |
| 3412 | else |
| 3413 | { |
| 3414 | get_yank_register(regname, FALSE); |
| 3415 | |
| 3416 | y_type = y_current->y_type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3417 | y_width = y_current->y_width; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3418 | y_size = y_current->y_size; |
| 3419 | y_array = y_current->y_array; |
| 3420 | } |
| 3421 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3422 | if (y_type == MLINE) |
| 3423 | { |
| 3424 | if (flags & PUT_LINE_SPLIT) |
| 3425 | { |
Bram Moolenaar | c004bc2 | 2015-06-19 15:17:55 +0200 | [diff] [blame] | 3426 | char_u *p; |
| 3427 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3428 | /* "p" or "P" in Visual mode: split the lines to put the text in |
| 3429 | * between. */ |
| 3430 | if (u_save_cursor() == FAIL) |
| 3431 | goto end; |
Bram Moolenaar | c004bc2 | 2015-06-19 15:17:55 +0200 | [diff] [blame] | 3432 | p = ml_get_cursor(); |
| 3433 | if (dir == FORWARD && *p != NUL) |
| 3434 | mb_ptr_adv(p); |
| 3435 | ptr = vim_strsave(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3436 | if (ptr == NULL) |
| 3437 | goto end; |
| 3438 | ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE); |
| 3439 | vim_free(ptr); |
| 3440 | |
Bram Moolenaar | c004bc2 | 2015-06-19 15:17:55 +0200 | [diff] [blame] | 3441 | oldp = ml_get_curline(); |
| 3442 | p = oldp + curwin->w_cursor.col; |
| 3443 | if (dir == FORWARD && *p != NUL) |
| 3444 | mb_ptr_adv(p); |
| 3445 | ptr = vim_strnsave(oldp, p - oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3446 | if (ptr == NULL) |
| 3447 | goto end; |
| 3448 | ml_replace(curwin->w_cursor.lnum, ptr, FALSE); |
| 3449 | ++nr_lines; |
| 3450 | dir = FORWARD; |
| 3451 | } |
| 3452 | if (flags & PUT_LINE_FORWARD) |
| 3453 | { |
| 3454 | /* Must be "p" for a Visual block, put lines below the block. */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 3455 | curwin->w_cursor = curbuf->b_visual.vi_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3456 | dir = FORWARD; |
| 3457 | } |
| 3458 | curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ |
| 3459 | curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ |
| 3460 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3461 | |
| 3462 | if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */ |
| 3463 | y_type = MLINE; |
| 3464 | |
| 3465 | if (y_size == 0 || y_array == NULL) |
| 3466 | { |
| 3467 | EMSG2(_("E353: Nothing in register %s"), |
| 3468 | regname == 0 ? (char_u *)"\"" : transchar(regname)); |
| 3469 | goto end; |
| 3470 | } |
| 3471 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3472 | if (y_type == MBLOCK) |
| 3473 | { |
| 3474 | lnum = curwin->w_cursor.lnum + y_size + 1; |
| 3475 | if (lnum > curbuf->b_ml.ml_line_count) |
| 3476 | lnum = curbuf->b_ml.ml_line_count + 1; |
| 3477 | if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL) |
| 3478 | goto end; |
| 3479 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 3480 | else if (y_type == MLINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3481 | { |
| 3482 | lnum = curwin->w_cursor.lnum; |
| 3483 | #ifdef FEAT_FOLDING |
| 3484 | /* Correct line number for closed fold. Don't move the cursor yet, |
| 3485 | * u_save() uses it. */ |
| 3486 | if (dir == BACKWARD) |
| 3487 | (void)hasFolding(lnum, &lnum, NULL); |
| 3488 | else |
| 3489 | (void)hasFolding(lnum, NULL, &lnum); |
| 3490 | #endif |
| 3491 | if (dir == FORWARD) |
| 3492 | ++lnum; |
Bram Moolenaar | 10315b1 | 2013-06-29 17:19:28 +0200 | [diff] [blame] | 3493 | /* In an empty buffer the empty line is going to be replaced, include |
| 3494 | * it in the saved lines. */ |
Bram Moolenaar | caf2dff | 2013-07-03 14:19:54 +0200 | [diff] [blame] | 3495 | if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3496 | goto end; |
| 3497 | #ifdef FEAT_FOLDING |
| 3498 | if (dir == FORWARD) |
| 3499 | curwin->w_cursor.lnum = lnum - 1; |
| 3500 | else |
| 3501 | curwin->w_cursor.lnum = lnum; |
| 3502 | curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */ |
| 3503 | #endif |
| 3504 | } |
| 3505 | else if (u_save_cursor() == FAIL) |
| 3506 | goto end; |
| 3507 | |
| 3508 | yanklen = (int)STRLEN(y_array[0]); |
| 3509 | |
| 3510 | #ifdef FEAT_VIRTUALEDIT |
| 3511 | if (ve_flags == VE_ALL && y_type == MCHAR) |
| 3512 | { |
| 3513 | if (gchar_cursor() == TAB) |
| 3514 | { |
| 3515 | /* Don't need to insert spaces when "p" on the last position of a |
| 3516 | * tab or "P" on the first position. */ |
| 3517 | if (dir == FORWARD |
| 3518 | ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1 |
| 3519 | : curwin->w_cursor.coladd > 0) |
| 3520 | coladvance_force(getviscol()); |
| 3521 | else |
| 3522 | curwin->w_cursor.coladd = 0; |
| 3523 | } |
| 3524 | else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL) |
| 3525 | coladvance_force(getviscol() + (dir == FORWARD)); |
| 3526 | } |
| 3527 | #endif |
| 3528 | |
| 3529 | lnum = curwin->w_cursor.lnum; |
| 3530 | col = curwin->w_cursor.col; |
| 3531 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3532 | /* |
| 3533 | * Block mode |
| 3534 | */ |
| 3535 | if (y_type == MBLOCK) |
| 3536 | { |
| 3537 | char c = gchar_cursor(); |
| 3538 | colnr_T endcol2 = 0; |
| 3539 | |
| 3540 | if (dir == FORWARD && c != NUL) |
| 3541 | { |
| 3542 | #ifdef FEAT_VIRTUALEDIT |
| 3543 | if (ve_flags == VE_ALL) |
| 3544 | getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); |
| 3545 | else |
| 3546 | #endif |
| 3547 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); |
| 3548 | |
| 3549 | #ifdef FEAT_MBYTE |
| 3550 | if (has_mbyte) |
| 3551 | /* move to start of next multi-byte character */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3552 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3553 | else |
| 3554 | #endif |
| 3555 | #ifdef FEAT_VIRTUALEDIT |
| 3556 | if (c != TAB || ve_flags != VE_ALL) |
| 3557 | #endif |
| 3558 | ++curwin->w_cursor.col; |
| 3559 | ++col; |
| 3560 | } |
| 3561 | else |
| 3562 | getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); |
| 3563 | |
| 3564 | #ifdef FEAT_VIRTUALEDIT |
| 3565 | col += curwin->w_cursor.coladd; |
Bram Moolenaar | e649ef0 | 2007-06-28 20:18:51 +0000 | [diff] [blame] | 3566 | if (ve_flags == VE_ALL |
| 3567 | && (curwin->w_cursor.coladd > 0 |
| 3568 | || endcol2 == curwin->w_cursor.col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3569 | { |
| 3570 | if (dir == FORWARD && c == NUL) |
| 3571 | ++col; |
| 3572 | if (dir != FORWARD && c != NUL) |
| 3573 | ++curwin->w_cursor.col; |
| 3574 | if (c == TAB) |
| 3575 | { |
| 3576 | if (dir == BACKWARD && curwin->w_cursor.col) |
| 3577 | curwin->w_cursor.col--; |
| 3578 | if (dir == FORWARD && col - 1 == endcol2) |
| 3579 | curwin->w_cursor.col++; |
| 3580 | } |
| 3581 | } |
| 3582 | curwin->w_cursor.coladd = 0; |
| 3583 | #endif |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 3584 | bd.textcol = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3585 | for (i = 0; i < y_size; ++i) |
| 3586 | { |
| 3587 | int spaces; |
| 3588 | char shortline; |
| 3589 | |
| 3590 | bd.startspaces = 0; |
| 3591 | bd.endspaces = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3592 | vcol = 0; |
| 3593 | delcount = 0; |
| 3594 | |
| 3595 | /* add a new line */ |
| 3596 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 3597 | { |
| 3598 | if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"", |
| 3599 | (colnr_T)1, FALSE) == FAIL) |
| 3600 | break; |
| 3601 | ++nr_lines; |
| 3602 | } |
| 3603 | /* get the old line and advance to the position to insert at */ |
| 3604 | oldp = ml_get_curline(); |
| 3605 | oldlen = (int)STRLEN(oldp); |
| 3606 | for (ptr = oldp; vcol < col && *ptr; ) |
| 3607 | { |
| 3608 | /* 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] | 3609 | incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3610 | vcol += incr; |
| 3611 | } |
| 3612 | bd.textcol = (colnr_T)(ptr - oldp); |
| 3613 | |
| 3614 | shortline = (vcol < col) || (vcol == col && !*ptr) ; |
| 3615 | |
| 3616 | if (vcol < col) /* line too short, padd with spaces */ |
| 3617 | bd.startspaces = col - vcol; |
| 3618 | else if (vcol > col) |
| 3619 | { |
| 3620 | bd.endspaces = vcol - col; |
| 3621 | bd.startspaces = incr - bd.endspaces; |
| 3622 | --bd.textcol; |
| 3623 | delcount = 1; |
| 3624 | #ifdef FEAT_MBYTE |
| 3625 | if (has_mbyte) |
| 3626 | bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol); |
| 3627 | #endif |
| 3628 | if (oldp[bd.textcol] != TAB) |
| 3629 | { |
| 3630 | /* Only a Tab can be split into spaces. Other |
| 3631 | * characters will have to be moved to after the |
| 3632 | * block, causing misalignment. */ |
| 3633 | delcount = 0; |
| 3634 | bd.endspaces = 0; |
| 3635 | } |
| 3636 | } |
| 3637 | |
| 3638 | yanklen = (int)STRLEN(y_array[i]); |
| 3639 | |
| 3640 | /* calculate number of spaces required to fill right side of block*/ |
| 3641 | spaces = y_width + 1; |
| 3642 | for (j = 0; j < yanklen; j++) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 3643 | spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3644 | if (spaces < 0) |
| 3645 | spaces = 0; |
| 3646 | |
| 3647 | /* insert the new text */ |
| 3648 | totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces; |
| 3649 | newp = alloc_check((unsigned)totlen + oldlen + 1); |
| 3650 | if (newp == NULL) |
| 3651 | break; |
| 3652 | /* copy part up to cursor to new line */ |
| 3653 | ptr = newp; |
| 3654 | mch_memmove(ptr, oldp, (size_t)bd.textcol); |
| 3655 | ptr += bd.textcol; |
| 3656 | /* may insert some spaces before the new text */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3657 | vim_memset(ptr, ' ', (size_t)bd.startspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3658 | ptr += bd.startspaces; |
| 3659 | /* insert the new text */ |
| 3660 | for (j = 0; j < count; ++j) |
| 3661 | { |
| 3662 | mch_memmove(ptr, y_array[i], (size_t)yanklen); |
| 3663 | ptr += yanklen; |
| 3664 | |
| 3665 | /* insert block's trailing spaces only if there's text behind */ |
| 3666 | if ((j < count - 1 || !shortline) && spaces) |
| 3667 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3668 | vim_memset(ptr, ' ', (size_t)spaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3669 | ptr += spaces; |
| 3670 | } |
| 3671 | } |
| 3672 | /* may insert some spaces after the new text */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 3673 | vim_memset(ptr, ' ', (size_t)bd.endspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3674 | ptr += bd.endspaces; |
| 3675 | /* move the text after the cursor to the end of the line. */ |
| 3676 | mch_memmove(ptr, oldp + bd.textcol + delcount, |
| 3677 | (size_t)(oldlen - bd.textcol - delcount + 1)); |
| 3678 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 3679 | |
| 3680 | ++curwin->w_cursor.lnum; |
| 3681 | if (i == 0) |
| 3682 | curwin->w_cursor.col += bd.startspaces; |
| 3683 | } |
| 3684 | |
| 3685 | changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines); |
| 3686 | |
| 3687 | /* Set '[ mark. */ |
| 3688 | curbuf->b_op_start = curwin->w_cursor; |
| 3689 | curbuf->b_op_start.lnum = lnum; |
| 3690 | |
| 3691 | /* adjust '] mark */ |
| 3692 | curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1; |
| 3693 | curbuf->b_op_end.col = bd.textcol + totlen - 1; |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 3694 | # ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3695 | curbuf->b_op_end.coladd = 0; |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 3696 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3697 | if (flags & PUT_CURSEND) |
| 3698 | { |
Bram Moolenaar | 12dec75 | 2006-07-23 20:37:09 +0000 | [diff] [blame] | 3699 | colnr_T len; |
| 3700 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3701 | curwin->w_cursor = curbuf->b_op_end; |
| 3702 | curwin->w_cursor.col++; |
Bram Moolenaar | 12dec75 | 2006-07-23 20:37:09 +0000 | [diff] [blame] | 3703 | |
| 3704 | /* in Insert mode we might be after the NUL, correct for that */ |
| 3705 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 3706 | if (curwin->w_cursor.col > len) |
| 3707 | curwin->w_cursor.col = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3708 | } |
| 3709 | else |
| 3710 | curwin->w_cursor.lnum = lnum; |
| 3711 | } |
| 3712 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3713 | { |
| 3714 | /* |
| 3715 | * Character or Line mode |
| 3716 | */ |
| 3717 | if (y_type == MCHAR) |
| 3718 | { |
| 3719 | /* if type is MCHAR, FORWARD is the same as BACKWARD on the next |
| 3720 | * char */ |
| 3721 | if (dir == FORWARD && gchar_cursor() != NUL) |
| 3722 | { |
| 3723 | #ifdef FEAT_MBYTE |
| 3724 | if (has_mbyte) |
| 3725 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3726 | int bytelen = (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3727 | |
| 3728 | /* put it on the next of the multi-byte character. */ |
| 3729 | col += bytelen; |
| 3730 | if (yanklen) |
| 3731 | { |
| 3732 | curwin->w_cursor.col += bytelen; |
| 3733 | curbuf->b_op_end.col += bytelen; |
| 3734 | } |
| 3735 | } |
| 3736 | else |
| 3737 | #endif |
| 3738 | { |
| 3739 | ++col; |
| 3740 | if (yanklen) |
| 3741 | { |
| 3742 | ++curwin->w_cursor.col; |
| 3743 | ++curbuf->b_op_end.col; |
| 3744 | } |
| 3745 | } |
| 3746 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3747 | curbuf->b_op_start = curwin->w_cursor; |
| 3748 | } |
| 3749 | /* |
| 3750 | * Line mode: BACKWARD is the same as FORWARD on the previous line |
| 3751 | */ |
| 3752 | else if (dir == BACKWARD) |
| 3753 | --lnum; |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 3754 | new_cursor = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3755 | |
| 3756 | /* |
| 3757 | * simple case: insert into current line |
| 3758 | */ |
| 3759 | if (y_type == MCHAR && y_size == 1) |
| 3760 | { |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3761 | do { |
| 3762 | totlen = count * yanklen; |
| 3763 | if (totlen > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3764 | { |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3765 | oldp = ml_get(lnum); |
| 3766 | newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1)); |
| 3767 | if (newp == NULL) |
| 3768 | goto end; /* alloc() gave an error message */ |
| 3769 | mch_memmove(newp, oldp, (size_t)col); |
| 3770 | ptr = newp + col; |
| 3771 | for (i = 0; i < count; ++i) |
| 3772 | { |
| 3773 | mch_memmove(ptr, y_array[0], (size_t)yanklen); |
| 3774 | ptr += yanklen; |
| 3775 | } |
| 3776 | STRMOVE(ptr, oldp + col); |
| 3777 | ml_replace(lnum, newp, FALSE); |
| 3778 | /* Place cursor on last putted char. */ |
| 3779 | if (lnum == curwin->w_cursor.lnum) |
Bram Moolenaar | 1e42f7a | 2013-11-21 13:24:41 +0100 | [diff] [blame] | 3780 | { |
| 3781 | /* make sure curwin->w_virtcol is updated */ |
| 3782 | changed_cline_bef_curs(); |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3783 | curwin->w_cursor.col += (colnr_T)(totlen - 1); |
Bram Moolenaar | 1e42f7a | 2013-11-21 13:24:41 +0100 | [diff] [blame] | 3784 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3785 | } |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3786 | if (VIsual_active) |
| 3787 | lnum++; |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 3788 | } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum); |
Bram Moolenaar | ec11aef | 2013-09-22 15:23:44 +0200 | [diff] [blame] | 3789 | |
Bram Moolenaar | 06e7ce1 | 2014-11-19 17:35:39 +0100 | [diff] [blame] | 3790 | if (VIsual_active) /* reset lnum to the last visual line */ |
| 3791 | lnum--; |
| 3792 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3793 | curbuf->b_op_end = curwin->w_cursor; |
| 3794 | /* For "CTRL-O p" in Insert mode, put cursor after last char */ |
| 3795 | if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND))) |
| 3796 | ++curwin->w_cursor.col; |
| 3797 | changed_bytes(lnum, col); |
| 3798 | } |
| 3799 | else |
| 3800 | { |
| 3801 | /* |
| 3802 | * Insert at least one line. When y_type is MCHAR, break the first |
| 3803 | * line in two. |
| 3804 | */ |
| 3805 | for (cnt = 1; cnt <= count; ++cnt) |
| 3806 | { |
| 3807 | i = 0; |
| 3808 | if (y_type == MCHAR) |
| 3809 | { |
| 3810 | /* |
| 3811 | * Split the current line in two at the insert position. |
| 3812 | * First insert y_array[size - 1] in front of second line. |
| 3813 | * Then append y_array[0] to first line. |
| 3814 | */ |
| 3815 | lnum = new_cursor.lnum; |
| 3816 | ptr = ml_get(lnum) + col; |
| 3817 | totlen = (int)STRLEN(y_array[y_size - 1]); |
| 3818 | newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1)); |
| 3819 | if (newp == NULL) |
| 3820 | goto error; |
| 3821 | STRCPY(newp, y_array[y_size - 1]); |
| 3822 | STRCAT(newp, ptr); |
| 3823 | /* insert second line */ |
| 3824 | ml_append(lnum, newp, (colnr_T)0, FALSE); |
| 3825 | vim_free(newp); |
| 3826 | |
| 3827 | oldp = ml_get(lnum); |
| 3828 | newp = alloc_check((unsigned)(col + yanklen + 1)); |
| 3829 | if (newp == NULL) |
| 3830 | goto error; |
| 3831 | /* copy first part of line */ |
| 3832 | mch_memmove(newp, oldp, (size_t)col); |
| 3833 | /* append to first line */ |
| 3834 | mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1)); |
| 3835 | ml_replace(lnum, newp, FALSE); |
| 3836 | |
| 3837 | curwin->w_cursor.lnum = lnum; |
| 3838 | i = 1; |
| 3839 | } |
| 3840 | |
| 3841 | for (; i < y_size; ++i) |
| 3842 | { |
| 3843 | if ((y_type != MCHAR || i < y_size - 1) |
| 3844 | && ml_append(lnum, y_array[i], (colnr_T)0, FALSE) |
| 3845 | == FAIL) |
| 3846 | goto error; |
| 3847 | lnum++; |
| 3848 | ++nr_lines; |
| 3849 | if (flags & PUT_FIXINDENT) |
| 3850 | { |
| 3851 | old_pos = curwin->w_cursor; |
| 3852 | curwin->w_cursor.lnum = lnum; |
| 3853 | ptr = ml_get(lnum); |
| 3854 | if (cnt == count && i == y_size - 1) |
| 3855 | lendiff = (int)STRLEN(ptr); |
| 3856 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) |
| 3857 | if (*ptr == '#' && preprocs_left()) |
| 3858 | indent = 0; /* Leave # lines at start */ |
| 3859 | else |
| 3860 | #endif |
| 3861 | if (*ptr == NUL) |
| 3862 | indent = 0; /* Ignore empty lines */ |
| 3863 | else if (first_indent) |
| 3864 | { |
| 3865 | indent_diff = orig_indent - get_indent(); |
| 3866 | indent = orig_indent; |
| 3867 | first_indent = FALSE; |
| 3868 | } |
| 3869 | else if ((indent = get_indent() + indent_diff) < 0) |
| 3870 | indent = 0; |
| 3871 | (void)set_indent(indent, 0); |
| 3872 | curwin->w_cursor = old_pos; |
| 3873 | /* remember how many chars were removed */ |
| 3874 | if (cnt == count && i == y_size - 1) |
| 3875 | lendiff -= (int)STRLEN(ml_get(lnum)); |
| 3876 | } |
| 3877 | } |
| 3878 | } |
| 3879 | |
| 3880 | error: |
| 3881 | /* Adjust marks. */ |
| 3882 | if (y_type == MLINE) |
| 3883 | { |
| 3884 | curbuf->b_op_start.col = 0; |
| 3885 | if (dir == FORWARD) |
| 3886 | curbuf->b_op_start.lnum++; |
| 3887 | } |
| 3888 | mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR), |
| 3889 | (linenr_T)MAXLNUM, nr_lines, 0L); |
| 3890 | |
| 3891 | /* note changed text for displaying and folding */ |
| 3892 | if (y_type == MCHAR) |
| 3893 | changed_lines(curwin->w_cursor.lnum, col, |
| 3894 | curwin->w_cursor.lnum + 1, nr_lines); |
| 3895 | else |
| 3896 | changed_lines(curbuf->b_op_start.lnum, 0, |
| 3897 | curbuf->b_op_start.lnum, nr_lines); |
| 3898 | |
| 3899 | /* put '] mark at last inserted character */ |
| 3900 | curbuf->b_op_end.lnum = lnum; |
| 3901 | /* correct length for change in indent */ |
| 3902 | col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff; |
| 3903 | if (col > 1) |
| 3904 | curbuf->b_op_end.col = col - 1; |
| 3905 | else |
| 3906 | curbuf->b_op_end.col = 0; |
| 3907 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 3908 | if (flags & PUT_CURSLINE) |
| 3909 | { |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 3910 | /* ":put": put cursor on last inserted line */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 3911 | curwin->w_cursor.lnum = lnum; |
| 3912 | beginline(BL_WHITE | BL_FIX); |
| 3913 | } |
| 3914 | else if (flags & PUT_CURSEND) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3915 | { |
| 3916 | /* put cursor after inserted text */ |
| 3917 | if (y_type == MLINE) |
| 3918 | { |
| 3919 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 3920 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 3921 | else |
| 3922 | curwin->w_cursor.lnum = lnum + 1; |
| 3923 | curwin->w_cursor.col = 0; |
| 3924 | } |
| 3925 | else |
| 3926 | { |
| 3927 | curwin->w_cursor.lnum = lnum; |
| 3928 | curwin->w_cursor.col = col; |
| 3929 | } |
| 3930 | } |
| 3931 | else if (y_type == MLINE) |
| 3932 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 3933 | /* put cursor on first non-blank in first inserted line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3934 | curwin->w_cursor.col = 0; |
| 3935 | if (dir == FORWARD) |
| 3936 | ++curwin->w_cursor.lnum; |
| 3937 | beginline(BL_WHITE | BL_FIX); |
| 3938 | } |
| 3939 | else /* put cursor on first inserted character */ |
| 3940 | curwin->w_cursor = new_cursor; |
| 3941 | } |
| 3942 | } |
| 3943 | |
| 3944 | msgmore(nr_lines); |
| 3945 | curwin->w_set_curswant = TRUE; |
| 3946 | |
| 3947 | end: |
| 3948 | if (allocated) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3949 | vim_free(insert_string); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 3950 | if (regname == '=') |
| 3951 | vim_free(y_array); |
| 3952 | |
Bram Moolenaar | 033d888 | 2013-09-25 23:24:57 +0200 | [diff] [blame] | 3953 | VIsual_active = FALSE; |
Bram Moolenaar | 033d888 | 2013-09-25 23:24:57 +0200 | [diff] [blame] | 3954 | |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 3955 | /* 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] | 3956 | adjust_cursor_eol(); |
| 3957 | } |
| 3958 | |
| 3959 | /* |
| 3960 | * When the cursor is on the NUL past the end of the line and it should not be |
| 3961 | * there move it left. |
| 3962 | */ |
| 3963 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3964 | adjust_cursor_eol(void) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 3965 | { |
| 3966 | if (curwin->w_cursor.col > 0 |
| 3967 | && gchar_cursor() == NUL |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 3968 | #ifdef FEAT_VIRTUALEDIT |
| 3969 | && (ve_flags & VE_ONEMORE) == 0 |
| 3970 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3971 | && !(restart_edit || (State & INSERT))) |
| 3972 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3973 | /* Put the cursor on the last character in the line. */ |
Bram Moolenaar | a5fac54 | 2005-10-12 20:58:49 +0000 | [diff] [blame] | 3974 | dec_cursor(); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 3975 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3976 | #ifdef FEAT_VIRTUALEDIT |
| 3977 | if (ve_flags == VE_ALL) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3978 | { |
| 3979 | colnr_T scol, ecol; |
| 3980 | |
| 3981 | /* Coladd is set to the width of the last character. */ |
| 3982 | getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol); |
| 3983 | curwin->w_cursor.coladd = ecol - scol + 1; |
| 3984 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3985 | #endif |
| 3986 | } |
| 3987 | } |
| 3988 | |
| 3989 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO) |
| 3990 | /* |
| 3991 | * Return TRUE if lines starting with '#' should be left aligned. |
| 3992 | */ |
| 3993 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3994 | preprocs_left(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3995 | { |
| 3996 | return |
| 3997 | # ifdef FEAT_SMARTINDENT |
| 3998 | # ifdef FEAT_CINDENT |
| 3999 | (curbuf->b_p_si && !curbuf->b_p_cin) || |
| 4000 | # else |
| 4001 | curbuf->b_p_si |
| 4002 | # endif |
| 4003 | # endif |
| 4004 | # ifdef FEAT_CINDENT |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4005 | (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE) |
| 4006 | && curbuf->b_ind_hash_comment == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4007 | # endif |
| 4008 | ; |
| 4009 | } |
| 4010 | #endif |
| 4011 | |
| 4012 | /* Return the character name of the register with the given number */ |
| 4013 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4014 | get_register_name(int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4015 | { |
| 4016 | if (num == -1) |
| 4017 | return '"'; |
| 4018 | else if (num < 10) |
| 4019 | return num + '0'; |
| 4020 | else if (num == DELETION_REGISTER) |
| 4021 | return '-'; |
| 4022 | #ifdef FEAT_CLIPBOARD |
| 4023 | else if (num == STAR_REGISTER) |
| 4024 | return '*'; |
| 4025 | else if (num == PLUS_REGISTER) |
| 4026 | return '+'; |
| 4027 | #endif |
| 4028 | else |
| 4029 | { |
| 4030 | #ifdef EBCDIC |
| 4031 | int i; |
| 4032 | |
| 4033 | /* EBCDIC is really braindead ... */ |
| 4034 | i = 'a' + (num - 10); |
| 4035 | if (i > 'i') |
| 4036 | i += 7; |
| 4037 | if (i > 'r') |
| 4038 | i += 8; |
| 4039 | return i; |
| 4040 | #else |
| 4041 | return num + 'a' - 10; |
| 4042 | #endif |
| 4043 | } |
| 4044 | } |
| 4045 | |
| 4046 | /* |
| 4047 | * ":dis" and ":registers": Display the contents of the yank registers. |
| 4048 | */ |
| 4049 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4050 | ex_display(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4051 | { |
| 4052 | int i, n; |
| 4053 | long j; |
| 4054 | char_u *p; |
| 4055 | struct yankreg *yb; |
| 4056 | int name; |
| 4057 | int attr; |
| 4058 | char_u *arg = eap->arg; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4059 | #ifdef FEAT_MBYTE |
| 4060 | int clen; |
| 4061 | #else |
| 4062 | # define clen 1 |
| 4063 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4064 | |
| 4065 | if (arg != NULL && *arg == NUL) |
| 4066 | arg = NULL; |
| 4067 | attr = hl_attr(HLF_8); |
| 4068 | |
| 4069 | /* Highlight title */ |
| 4070 | MSG_PUTS_TITLE(_("\n--- Registers ---")); |
| 4071 | for (i = -1; i < NUM_REGISTERS && !got_int; ++i) |
| 4072 | { |
| 4073 | name = get_register_name(i); |
Bram Moolenaar | 0818b87 | 2010-11-24 14:28:58 +0100 | [diff] [blame] | 4074 | if (arg != NULL && vim_strchr(arg, name) == NULL |
| 4075 | #ifdef ONE_CLIPBOARD |
| 4076 | /* Star register and plus register contain the same thing. */ |
| 4077 | && (name != '*' || vim_strchr(arg, '+') == NULL) |
| 4078 | #endif |
| 4079 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4080 | continue; /* did not ask for this register */ |
| 4081 | |
| 4082 | #ifdef FEAT_CLIPBOARD |
| 4083 | /* Adjust register name for "unnamed" in 'clipboard'. |
| 4084 | * When it's a clipboard register, fill it with the current contents |
| 4085 | * of the clipboard. */ |
| 4086 | adjust_clip_reg(&name); |
| 4087 | (void)may_get_selection(name); |
| 4088 | #endif |
| 4089 | |
| 4090 | if (i == -1) |
| 4091 | { |
| 4092 | if (y_previous != NULL) |
| 4093 | yb = y_previous; |
| 4094 | else |
| 4095 | yb = &(y_regs[0]); |
| 4096 | } |
| 4097 | else |
| 4098 | yb = &(y_regs[i]); |
Bram Moolenaar | cde547a | 2009-11-17 11:43:06 +0000 | [diff] [blame] | 4099 | |
| 4100 | #ifdef FEAT_EVAL |
| 4101 | if (name == MB_TOLOWER(redir_reg) |
| 4102 | || (redir_reg == '"' && yb == y_previous)) |
| 4103 | continue; /* do not list register being written to, the |
| 4104 | * pointer can be freed */ |
| 4105 | #endif |
| 4106 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4107 | if (yb->y_array != NULL) |
| 4108 | { |
| 4109 | msg_putchar('\n'); |
| 4110 | msg_putchar('"'); |
| 4111 | msg_putchar(name); |
| 4112 | MSG_PUTS(" "); |
| 4113 | |
| 4114 | n = (int)Columns - 6; |
| 4115 | for (j = 0; j < yb->y_size && n > 1; ++j) |
| 4116 | { |
| 4117 | if (j) |
| 4118 | { |
| 4119 | MSG_PUTS_ATTR("^J", attr); |
| 4120 | n -= 2; |
| 4121 | } |
| 4122 | for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p) |
| 4123 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4124 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4125 | clen = (*mb_ptr2len)(p); |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4126 | #endif |
| 4127 | msg_outtrans_len(p, clen); |
| 4128 | #ifdef FEAT_MBYTE |
| 4129 | p += clen - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4130 | #endif |
| 4131 | } |
| 4132 | } |
| 4133 | if (n > 1 && yb->y_type == MLINE) |
| 4134 | MSG_PUTS_ATTR("^J", attr); |
| 4135 | out_flush(); /* show one line at a time */ |
| 4136 | } |
| 4137 | ui_breakcheck(); |
| 4138 | } |
| 4139 | |
| 4140 | /* |
| 4141 | * display last inserted text |
| 4142 | */ |
| 4143 | if ((p = get_last_insert()) != NULL |
| 4144 | && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int) |
| 4145 | { |
| 4146 | MSG_PUTS("\n\". "); |
| 4147 | dis_msg(p, TRUE); |
| 4148 | } |
| 4149 | |
| 4150 | /* |
| 4151 | * display last command line |
| 4152 | */ |
| 4153 | if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL) |
| 4154 | && !got_int) |
| 4155 | { |
| 4156 | MSG_PUTS("\n\": "); |
| 4157 | dis_msg(last_cmdline, FALSE); |
| 4158 | } |
| 4159 | |
| 4160 | /* |
| 4161 | * display current file name |
| 4162 | */ |
| 4163 | if (curbuf->b_fname != NULL |
| 4164 | && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) |
| 4165 | { |
| 4166 | MSG_PUTS("\n\"% "); |
| 4167 | dis_msg(curbuf->b_fname, FALSE); |
| 4168 | } |
| 4169 | |
| 4170 | /* |
| 4171 | * display alternate file name |
| 4172 | */ |
| 4173 | if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) |
| 4174 | { |
| 4175 | char_u *fname; |
| 4176 | linenr_T dummy; |
| 4177 | |
| 4178 | if (buflist_name_nr(0, &fname, &dummy) != FAIL) |
| 4179 | { |
| 4180 | MSG_PUTS("\n\"# "); |
| 4181 | dis_msg(fname, FALSE); |
| 4182 | } |
| 4183 | } |
| 4184 | |
| 4185 | /* |
| 4186 | * display last search pattern |
| 4187 | */ |
| 4188 | if (last_search_pat() != NULL |
| 4189 | && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int) |
| 4190 | { |
| 4191 | MSG_PUTS("\n\"/ "); |
| 4192 | dis_msg(last_search_pat(), FALSE); |
| 4193 | } |
| 4194 | |
| 4195 | #ifdef FEAT_EVAL |
| 4196 | /* |
| 4197 | * display last used expression |
| 4198 | */ |
| 4199 | if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL) |
| 4200 | && !got_int) |
| 4201 | { |
| 4202 | MSG_PUTS("\n\"= "); |
| 4203 | dis_msg(expr_line, FALSE); |
| 4204 | } |
| 4205 | #endif |
| 4206 | } |
| 4207 | |
| 4208 | /* |
| 4209 | * display a string for do_dis() |
| 4210 | * truncate at end of screen line |
| 4211 | */ |
| 4212 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4213 | dis_msg( |
| 4214 | char_u *p, |
| 4215 | int skip_esc) /* if TRUE, ignore trailing ESC */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4216 | { |
| 4217 | int n; |
| 4218 | #ifdef FEAT_MBYTE |
| 4219 | int l; |
| 4220 | #endif |
| 4221 | |
| 4222 | n = (int)Columns - 6; |
| 4223 | while (*p != NUL |
| 4224 | && !(*p == ESC && skip_esc && *(p + 1) == NUL) |
| 4225 | && (n -= ptr2cells(p)) >= 0) |
| 4226 | { |
| 4227 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4228 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4229 | { |
| 4230 | msg_outtrans_len(p, l); |
| 4231 | p += l; |
| 4232 | } |
| 4233 | else |
| 4234 | #endif |
| 4235 | msg_outtrans_len(p++, 1); |
| 4236 | } |
| 4237 | ui_breakcheck(); |
| 4238 | } |
| 4239 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4240 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4241 | /* |
| 4242 | * If "process" is TRUE and the line begins with a comment leader (possibly |
| 4243 | * after some white space), return a pointer to the text after it. Put a boolean |
| 4244 | * value indicating whether the line ends with an unclosed comment in |
| 4245 | * "is_comment". |
| 4246 | * line - line to be processed, |
| 4247 | * 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] | 4248 | * comment, |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4249 | * include_space - whether to also skip space following the comment leader, |
| 4250 | * is_comment - will indicate whether the current line ends with an unclosed |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4251 | * comment. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4252 | */ |
| 4253 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4254 | skip_comment( |
| 4255 | char_u *line, |
| 4256 | int process, |
| 4257 | int include_space, |
| 4258 | int *is_comment) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4259 | { |
| 4260 | char_u *comment_flags = NULL; |
| 4261 | int lead_len; |
| 4262 | int leader_offset = get_last_leader_offset(line, &comment_flags); |
| 4263 | |
| 4264 | *is_comment = FALSE; |
| 4265 | if (leader_offset != -1) |
| 4266 | { |
| 4267 | /* Let's check whether the line ends with an unclosed comment. |
| 4268 | * If the last comment leader has COM_END in flags, there's no comment. |
| 4269 | */ |
| 4270 | while (*comment_flags) |
| 4271 | { |
| 4272 | if (*comment_flags == COM_END |
| 4273 | || *comment_flags == ':') |
| 4274 | break; |
| 4275 | ++comment_flags; |
| 4276 | } |
| 4277 | if (*comment_flags != COM_END) |
| 4278 | *is_comment = TRUE; |
| 4279 | } |
| 4280 | |
| 4281 | if (process == FALSE) |
| 4282 | return line; |
| 4283 | |
| 4284 | lead_len = get_leader_len(line, &comment_flags, FALSE, include_space); |
| 4285 | |
| 4286 | if (lead_len == 0) |
| 4287 | return line; |
| 4288 | |
| 4289 | /* Find: |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4290 | * - COM_END, |
| 4291 | * - colon, |
| 4292 | * whichever comes first. |
| 4293 | */ |
| 4294 | while (*comment_flags) |
| 4295 | { |
Bram Moolenaar | e04a48f | 2012-06-13 14:01:41 +0200 | [diff] [blame] | 4296 | if (*comment_flags == COM_END |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4297 | || *comment_flags == ':') |
| 4298 | { |
| 4299 | break; |
| 4300 | } |
| 4301 | ++comment_flags; |
| 4302 | } |
| 4303 | |
| 4304 | /* 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] | 4305 | * starting with a closing part of a three-part comment. That's good, |
| 4306 | * 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] | 4307 | */ |
| 4308 | if (*comment_flags == ':' || *comment_flags == NUL) |
| 4309 | line += lead_len; |
| 4310 | |
| 4311 | return line; |
| 4312 | } |
| 4313 | #endif |
| 4314 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4315 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4316 | * Join 'count' lines (minimal 2) at cursor position. |
| 4317 | * When "save_undo" is TRUE save lines for undo first. |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 4318 | * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment |
| 4319 | * leaders should not be removed. |
| 4320 | * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected |
| 4321 | * to set those marks. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4322 | * |
Bram Moolenaar | 10c5695 | 2007-05-10 18:38:52 +0000 | [diff] [blame] | 4323 | * return FAIL for failure, OK otherwise |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4324 | */ |
| 4325 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4326 | do_join( |
| 4327 | long count, |
| 4328 | int insert_space, |
| 4329 | int save_undo, |
| 4330 | int use_formatoptions UNUSED, |
| 4331 | int setmark) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4332 | { |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4333 | char_u *curr = NULL; |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 4334 | char_u *curr_start = NULL; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4335 | char_u *cend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4336 | char_u *newp; |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 4337 | char_u *spaces; /* number of spaces inserted before a line */ |
Bram Moolenaar | d43848c | 2010-07-14 14:28:26 +0200 | [diff] [blame] | 4338 | int endcurr1 = NUL; |
| 4339 | int endcurr2 = NUL; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4340 | int currsize = 0; /* size of the current line */ |
| 4341 | int sumsize = 0; /* size of the long new line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4342 | linenr_T t; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4343 | colnr_T col = 0; |
| 4344 | int ret = OK; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4345 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
Bram Moolenaar | 802053f | 2012-06-06 23:08:38 +0200 | [diff] [blame] | 4346 | int *comments = NULL; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4347 | int remove_comments = (use_formatoptions == TRUE) |
| 4348 | && has_format_option(FO_REMOVE_COMS); |
| 4349 | int prev_was_comment; |
| 4350 | #endif |
| 4351 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4352 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4353 | if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 4354 | (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL) |
| 4355 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4356 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4357 | /* Allocate an array to store the number of spaces inserted before each |
| 4358 | * line. We will use it to pre-compute the length of the new line and the |
| 4359 | * proper placement of each original line in the new one. */ |
| 4360 | spaces = lalloc_clear((long_u)count, TRUE); |
| 4361 | if (spaces == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4362 | return FAIL; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4363 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4364 | if (remove_comments) |
| 4365 | { |
| 4366 | comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE); |
| 4367 | if (comments == NULL) |
| 4368 | { |
| 4369 | vim_free(spaces); |
| 4370 | return FAIL; |
| 4371 | } |
| 4372 | } |
| 4373 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4374 | |
| 4375 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4376 | * Don't move anything, just compute the final line length |
| 4377 | * and setup the array of space strings lengths |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4378 | */ |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4379 | for (t = 0; t < count; ++t) |
| 4380 | { |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 4381 | curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t)); |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 4382 | if (t == 0 && setmark) |
Bram Moolenaar | f92d8a2 | 2014-02-11 19:33:07 +0100 | [diff] [blame] | 4383 | { |
| 4384 | /* Set the '[ mark. */ |
| 4385 | curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum; |
| 4386 | curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr); |
| 4387 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4388 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4389 | if (remove_comments) |
| 4390 | { |
| 4391 | /* We don't want to remove the comment leader if the |
| 4392 | * previous line is not a comment. */ |
| 4393 | if (t > 0 && prev_was_comment) |
| 4394 | { |
| 4395 | |
| 4396 | char_u *new_curr = skip_comment(curr, TRUE, insert_space, |
| 4397 | &prev_was_comment); |
Bram Moolenaar | 27ba088 | 2012-06-07 21:09:39 +0200 | [diff] [blame] | 4398 | comments[t] = (int)(new_curr - curr); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4399 | curr = new_curr; |
| 4400 | } |
| 4401 | else |
| 4402 | curr = skip_comment(curr, FALSE, insert_space, |
| 4403 | &prev_was_comment); |
| 4404 | } |
| 4405 | #endif |
| 4406 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4407 | if (insert_space && t > 0) |
| 4408 | { |
| 4409 | curr = skipwhite(curr); |
| 4410 | if (*curr != ')' && currsize != 0 && endcurr1 != TAB |
| 4411 | #ifdef FEAT_MBYTE |
| 4412 | && (!has_format_option(FO_MBYTE_JOIN) |
| 4413 | || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100)) |
| 4414 | && (!has_format_option(FO_MBYTE_JOIN2) |
| 4415 | || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100) |
| 4416 | #endif |
| 4417 | ) |
| 4418 | { |
| 4419 | /* don't add a space if the line is ending in a space */ |
| 4420 | if (endcurr1 == ' ') |
| 4421 | endcurr1 = endcurr2; |
| 4422 | else |
| 4423 | ++spaces[t]; |
| 4424 | /* extra space when 'joinspaces' set and line ends in '.' */ |
| 4425 | if ( p_js |
| 4426 | && (endcurr1 == '.' |
| 4427 | || (vim_strchr(p_cpo, CPO_JOINSP) == NULL |
| 4428 | && (endcurr1 == '?' || endcurr1 == '!')))) |
| 4429 | ++spaces[t]; |
| 4430 | } |
| 4431 | } |
| 4432 | currsize = (int)STRLEN(curr); |
| 4433 | sumsize += currsize + spaces[t]; |
| 4434 | endcurr1 = endcurr2 = NUL; |
| 4435 | if (insert_space && currsize > 0) |
| 4436 | { |
| 4437 | #ifdef FEAT_MBYTE |
| 4438 | if (has_mbyte) |
| 4439 | { |
| 4440 | cend = curr + currsize; |
| 4441 | mb_ptr_back(curr, cend); |
| 4442 | endcurr1 = (*mb_ptr2char)(cend); |
| 4443 | if (cend > curr) |
| 4444 | { |
| 4445 | mb_ptr_back(curr, cend); |
| 4446 | endcurr2 = (*mb_ptr2char)(cend); |
| 4447 | } |
| 4448 | } |
| 4449 | else |
| 4450 | #endif |
| 4451 | { |
| 4452 | endcurr1 = *(curr + currsize - 1); |
| 4453 | if (currsize > 1) |
| 4454 | endcurr2 = *(curr + currsize - 2); |
| 4455 | } |
| 4456 | } |
| 4457 | line_breakcheck(); |
| 4458 | if (got_int) |
| 4459 | { |
| 4460 | ret = FAIL; |
| 4461 | goto theend; |
| 4462 | } |
| 4463 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4464 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4465 | /* store the column position before last line */ |
| 4466 | col = sumsize - currsize - spaces[count - 1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4467 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4468 | /* allocate the space for the new line */ |
| 4469 | newp = alloc_check((unsigned)(sumsize + 1)); |
| 4470 | cend = newp + sumsize; |
| 4471 | *cend = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4472 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4473 | /* |
| 4474 | * Move affected lines to the new long one. |
| 4475 | * |
| 4476 | * Move marks from each deleted line to the joined line, adjusting the |
| 4477 | * column. This is not Vi compatible, but Vi deletes the marks, thus that |
| 4478 | * should not really be a problem. |
| 4479 | */ |
| 4480 | for (t = count - 1; ; --t) |
| 4481 | { |
| 4482 | cend -= currsize; |
| 4483 | mch_memmove(cend, curr, (size_t)currsize); |
| 4484 | if (spaces[t] > 0) |
| 4485 | { |
| 4486 | cend -= spaces[t]; |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 4487 | vim_memset(cend, ' ', (size_t)(spaces[t])); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4488 | } |
| 4489 | 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] | 4490 | (long)(cend - newp + spaces[t] - (curr - curr_start))); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4491 | if (t == 0) |
| 4492 | break; |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 4493 | 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] | 4494 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4495 | if (remove_comments) |
| 4496 | curr += comments[t - 1]; |
| 4497 | #endif |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4498 | if (insert_space && t > 1) |
| 4499 | curr = skipwhite(curr); |
| 4500 | currsize = (int)STRLEN(curr); |
| 4501 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4502 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 4503 | |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 4504 | if (setmark) |
| 4505 | { |
| 4506 | /* Set the '] mark. */ |
| 4507 | curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum; |
| 4508 | curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp); |
| 4509 | } |
Bram Moolenaar | f92d8a2 | 2014-02-11 19:33:07 +0100 | [diff] [blame] | 4510 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4511 | /* Only report the change in the first line here, del_lines() will report |
| 4512 | * the deleted line. */ |
| 4513 | changed_lines(curwin->w_cursor.lnum, currsize, |
| 4514 | curwin->w_cursor.lnum + 1, 0L); |
| 4515 | |
| 4516 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4517 | * Delete following lines. To do this we move the cursor there |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4518 | * briefly, and then move it back. After del_lines() the cursor may |
| 4519 | * 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] | 4520 | */ |
| 4521 | t = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4522 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4523 | del_lines(count - 1, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4524 | curwin->w_cursor.lnum = t; |
| 4525 | |
| 4526 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4527 | * Set the cursor column: |
| 4528 | * Vi compatible: use the column of the first join |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4529 | * vim: use the column of the last join |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4530 | */ |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4531 | curwin->w_cursor.col = |
| 4532 | (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4533 | check_cursor_col(); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4534 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4535 | #ifdef FEAT_VIRTUALEDIT |
| 4536 | curwin->w_cursor.coladd = 0; |
| 4537 | #endif |
| 4538 | curwin->w_set_curswant = TRUE; |
| 4539 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4540 | theend: |
| 4541 | vim_free(spaces); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 4542 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 4543 | if (remove_comments) |
| 4544 | vim_free(comments); |
| 4545 | #endif |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4546 | return ret; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | #ifdef FEAT_COMMENTS |
| 4550 | /* |
| 4551 | * Return TRUE if the two comment leaders given are the same. "lnum" is |
| 4552 | * the first line. White-space is ignored. Note that the whole of |
| 4553 | * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb |
| 4554 | */ |
| 4555 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4556 | same_leader( |
| 4557 | linenr_T lnum, |
| 4558 | int leader1_len, |
| 4559 | char_u *leader1_flags, |
| 4560 | int leader2_len, |
| 4561 | char_u *leader2_flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4562 | { |
| 4563 | int idx1 = 0, idx2 = 0; |
| 4564 | char_u *p; |
| 4565 | char_u *line1; |
| 4566 | char_u *line2; |
| 4567 | |
| 4568 | if (leader1_len == 0) |
| 4569 | return (leader2_len == 0); |
| 4570 | |
| 4571 | /* |
| 4572 | * If first leader has 'f' flag, the lines can be joined only if the |
| 4573 | * second line does not have a leader. |
| 4574 | * If first leader has 'e' flag, the lines can never be joined. |
| 4575 | * If fist leader has 's' flag, the lines can only be joined if there is |
| 4576 | * some text after it and the second line has the 'm' flag. |
| 4577 | */ |
| 4578 | if (leader1_flags != NULL) |
| 4579 | { |
| 4580 | for (p = leader1_flags; *p && *p != ':'; ++p) |
| 4581 | { |
| 4582 | if (*p == COM_FIRST) |
| 4583 | return (leader2_len == 0); |
| 4584 | if (*p == COM_END) |
| 4585 | return FALSE; |
| 4586 | if (*p == COM_START) |
| 4587 | { |
| 4588 | if (*(ml_get(lnum) + leader1_len) == NUL) |
| 4589 | return FALSE; |
| 4590 | if (leader2_flags == NULL || leader2_len == 0) |
| 4591 | return FALSE; |
| 4592 | for (p = leader2_flags; *p && *p != ':'; ++p) |
| 4593 | if (*p == COM_MIDDLE) |
| 4594 | return TRUE; |
| 4595 | return FALSE; |
| 4596 | } |
| 4597 | } |
| 4598 | } |
| 4599 | |
| 4600 | /* |
| 4601 | * Get current line and next line, compare the leaders. |
| 4602 | * The first line has to be saved, only one line can be locked at a time. |
| 4603 | */ |
| 4604 | line1 = vim_strsave(ml_get(lnum)); |
| 4605 | if (line1 != NULL) |
| 4606 | { |
| 4607 | for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1) |
| 4608 | ; |
| 4609 | line2 = ml_get(lnum + 1); |
| 4610 | for (idx2 = 0; idx2 < leader2_len; ++idx2) |
| 4611 | { |
| 4612 | if (!vim_iswhite(line2[idx2])) |
| 4613 | { |
| 4614 | if (line1[idx1++] != line2[idx2]) |
| 4615 | break; |
| 4616 | } |
| 4617 | else |
| 4618 | while (vim_iswhite(line1[idx1])) |
| 4619 | ++idx1; |
| 4620 | } |
| 4621 | vim_free(line1); |
| 4622 | } |
| 4623 | return (idx2 == leader2_len && idx1 == leader1_len); |
| 4624 | } |
| 4625 | #endif |
| 4626 | |
| 4627 | /* |
Bram Moolenaar | 66accae | 2012-01-10 13:44:27 +0100 | [diff] [blame] | 4628 | * Implementation of the format operator 'gq'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4629 | */ |
| 4630 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4631 | op_format( |
| 4632 | oparg_T *oap, |
| 4633 | int keep_cursor) /* keep cursor on same text char */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4634 | { |
| 4635 | long old_line_count = curbuf->b_ml.ml_line_count; |
| 4636 | |
| 4637 | /* Place the cursor where the "gq" or "gw" command was given, so that "u" |
| 4638 | * can put it back there. */ |
| 4639 | curwin->w_cursor = oap->cursor_start; |
| 4640 | |
| 4641 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 4642 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 4643 | return; |
| 4644 | curwin->w_cursor = oap->start; |
| 4645 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4646 | if (oap->is_VIsual) |
| 4647 | /* When there is no change: need to remove the Visual selection */ |
| 4648 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4649 | |
| 4650 | /* Set '[ mark at the start of the formatted area */ |
| 4651 | curbuf->b_op_start = oap->start; |
| 4652 | |
| 4653 | /* For "gw" remember the cursor position and put it back below (adjusted |
| 4654 | * for joined and split lines). */ |
| 4655 | if (keep_cursor) |
| 4656 | saved_cursor = oap->cursor_start; |
| 4657 | |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 4658 | format_lines(oap->line_count, keep_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4659 | |
| 4660 | /* |
| 4661 | * Leave the cursor at the first non-blank of the last formatted line. |
| 4662 | * If the cursor was moved one line back (e.g. with "Q}") go to the next |
| 4663 | * line, so "." will do the next lines. |
| 4664 | */ |
| 4665 | if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 4666 | ++curwin->w_cursor.lnum; |
| 4667 | beginline(BL_WHITE | BL_FIX); |
| 4668 | old_line_count = curbuf->b_ml.ml_line_count - old_line_count; |
| 4669 | msgmore(old_line_count); |
| 4670 | |
| 4671 | /* put '] mark on the end of the formatted area */ |
| 4672 | curbuf->b_op_end = curwin->w_cursor; |
| 4673 | |
| 4674 | if (keep_cursor) |
| 4675 | { |
| 4676 | curwin->w_cursor = saved_cursor; |
| 4677 | saved_cursor.lnum = 0; |
| 4678 | } |
| 4679 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4680 | if (oap->is_VIsual) |
| 4681 | { |
| 4682 | win_T *wp; |
| 4683 | |
| 4684 | FOR_ALL_WINDOWS(wp) |
| 4685 | { |
| 4686 | if (wp->w_old_cursor_lnum != 0) |
| 4687 | { |
| 4688 | /* When lines have been inserted or deleted, adjust the end of |
| 4689 | * the Visual area to be redrawn. */ |
| 4690 | if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum) |
| 4691 | wp->w_old_cursor_lnum += old_line_count; |
| 4692 | else |
| 4693 | wp->w_old_visual_lnum += old_line_count; |
| 4694 | } |
| 4695 | } |
| 4696 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4699 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 4700 | /* |
| 4701 | * Implementation of the format operator 'gq' for when using 'formatexpr'. |
| 4702 | */ |
| 4703 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4704 | op_formatexpr(oparg_T *oap) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4705 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4706 | if (oap->is_VIsual) |
| 4707 | /* When there is no change: need to remove the Visual selection */ |
| 4708 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4709 | |
Bram Moolenaar | 700303e | 2010-07-11 17:35:50 +0200 | [diff] [blame] | 4710 | if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0) |
| 4711 | /* As documented: when 'formatexpr' returns non-zero fall back to |
| 4712 | * internal formatting. */ |
| 4713 | op_format(oap, FALSE); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4717 | fex_format( |
| 4718 | linenr_T lnum, |
| 4719 | long count, |
| 4720 | int c) /* character to be inserted */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4721 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4722 | int use_sandbox = was_set_insecurely((char_u *)"formatexpr", |
| 4723 | OPT_LOCAL); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4724 | int r; |
| 4725 | |
| 4726 | /* |
| 4727 | * 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] | 4728 | * Set v:char to the character to be inserted (can be NUL). |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4729 | */ |
| 4730 | set_vim_var_nr(VV_LNUM, lnum); |
| 4731 | set_vim_var_nr(VV_COUNT, count); |
Bram Moolenaar | da9591e | 2009-09-30 13:17:02 +0000 | [diff] [blame] | 4732 | set_vim_var_char(c); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4733 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4734 | /* |
| 4735 | * Evaluate the function. |
| 4736 | */ |
| 4737 | if (use_sandbox) |
| 4738 | ++sandbox; |
| 4739 | r = eval_to_number(curbuf->b_p_fex); |
| 4740 | if (use_sandbox) |
| 4741 | --sandbox; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4742 | |
| 4743 | set_vim_var_string(VV_CHAR, NULL, -1); |
| 4744 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4745 | return r; |
| 4746 | } |
| 4747 | #endif |
| 4748 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4749 | /* |
| 4750 | * Format "line_count" lines, starting at the cursor position. |
| 4751 | * When "line_count" is negative, format until the end of the paragraph. |
| 4752 | * Lines after the cursor line are saved for undo, caller must have saved the |
| 4753 | * first line. |
| 4754 | */ |
| 4755 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4756 | format_lines( |
| 4757 | linenr_T line_count, |
| 4758 | int avoid_fex) /* don't use 'formatexpr' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4759 | { |
| 4760 | int max_len; |
| 4761 | int is_not_par; /* current line not part of parag. */ |
| 4762 | int next_is_not_par; /* next line not part of paragraph */ |
| 4763 | int is_end_par; /* at end of paragraph */ |
| 4764 | int prev_is_end_par = FALSE;/* prev. line not part of parag. */ |
| 4765 | int next_is_start_par = FALSE; |
| 4766 | #ifdef FEAT_COMMENTS |
| 4767 | int leader_len = 0; /* leader len of current line */ |
| 4768 | int next_leader_len; /* leader len of next line */ |
| 4769 | char_u *leader_flags = NULL; /* flags for leader of current line */ |
| 4770 | char_u *next_leader_flags; /* flags for leader of next line */ |
| 4771 | int do_comments; /* format comments */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4772 | int do_comments_list = 0; /* format comments with 'n' or '2' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4773 | #endif |
| 4774 | int advance = TRUE; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4775 | int second_indent = -1; /* indent for second line (comment |
| 4776 | * aware) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4777 | int do_second_indent; |
| 4778 | int do_number_indent; |
| 4779 | int do_trail_white; |
| 4780 | int first_par_line = TRUE; |
| 4781 | int smd_save; |
| 4782 | long count; |
| 4783 | int need_set_indent = TRUE; /* set indent of next paragraph */ |
| 4784 | int force_format = FALSE; |
| 4785 | int old_State = State; |
| 4786 | |
| 4787 | /* length of a line to force formatting: 3 * 'tw' */ |
| 4788 | max_len = comp_textwidth(TRUE) * 3; |
| 4789 | |
| 4790 | /* check for 'q', '2' and '1' in 'formatoptions' */ |
| 4791 | #ifdef FEAT_COMMENTS |
| 4792 | do_comments = has_format_option(FO_Q_COMS); |
| 4793 | #endif |
| 4794 | do_second_indent = has_format_option(FO_Q_SECOND); |
| 4795 | do_number_indent = has_format_option(FO_Q_NUMBER); |
| 4796 | do_trail_white = has_format_option(FO_WHITE_PAR); |
| 4797 | |
| 4798 | /* |
| 4799 | * Get info about the previous and current line. |
| 4800 | */ |
| 4801 | if (curwin->w_cursor.lnum > 1) |
| 4802 | is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1 |
| 4803 | #ifdef FEAT_COMMENTS |
| 4804 | , &leader_len, &leader_flags, do_comments |
| 4805 | #endif |
| 4806 | ); |
| 4807 | else |
| 4808 | is_not_par = TRUE; |
| 4809 | next_is_not_par = fmt_check_par(curwin->w_cursor.lnum |
| 4810 | #ifdef FEAT_COMMENTS |
| 4811 | , &next_leader_len, &next_leader_flags, do_comments |
| 4812 | #endif |
| 4813 | ); |
| 4814 | is_end_par = (is_not_par || next_is_not_par); |
| 4815 | if (!is_end_par && do_trail_white) |
| 4816 | is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1); |
| 4817 | |
| 4818 | curwin->w_cursor.lnum--; |
| 4819 | for (count = line_count; count != 0 && !got_int; --count) |
| 4820 | { |
| 4821 | /* |
| 4822 | * Advance to next paragraph. |
| 4823 | */ |
| 4824 | if (advance) |
| 4825 | { |
| 4826 | curwin->w_cursor.lnum++; |
| 4827 | prev_is_end_par = is_end_par; |
| 4828 | is_not_par = next_is_not_par; |
| 4829 | #ifdef FEAT_COMMENTS |
| 4830 | leader_len = next_leader_len; |
| 4831 | leader_flags = next_leader_flags; |
| 4832 | #endif |
| 4833 | } |
| 4834 | |
| 4835 | /* |
| 4836 | * The last line to be formatted. |
| 4837 | */ |
| 4838 | if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count) |
| 4839 | { |
| 4840 | next_is_not_par = TRUE; |
| 4841 | #ifdef FEAT_COMMENTS |
| 4842 | next_leader_len = 0; |
| 4843 | next_leader_flags = NULL; |
| 4844 | #endif |
| 4845 | } |
| 4846 | else |
| 4847 | { |
| 4848 | next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1 |
| 4849 | #ifdef FEAT_COMMENTS |
| 4850 | , &next_leader_len, &next_leader_flags, do_comments |
| 4851 | #endif |
| 4852 | ); |
| 4853 | if (do_number_indent) |
| 4854 | next_is_start_par = |
| 4855 | (get_number_indent(curwin->w_cursor.lnum + 1) > 0); |
| 4856 | } |
| 4857 | advance = TRUE; |
| 4858 | is_end_par = (is_not_par || next_is_not_par || next_is_start_par); |
| 4859 | if (!is_end_par && do_trail_white) |
| 4860 | is_end_par = !ends_in_white(curwin->w_cursor.lnum); |
| 4861 | |
| 4862 | /* |
| 4863 | * Skip lines that are not in a paragraph. |
| 4864 | */ |
| 4865 | if (is_not_par) |
| 4866 | { |
| 4867 | if (line_count < 0) |
| 4868 | break; |
| 4869 | } |
| 4870 | else |
| 4871 | { |
| 4872 | /* |
| 4873 | * For the first line of a paragraph, check indent of second line. |
| 4874 | * Don't do this for comments and empty lines. |
| 4875 | */ |
| 4876 | if (first_par_line |
| 4877 | && (do_second_indent || do_number_indent) |
| 4878 | && prev_is_end_par |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4879 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4880 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4881 | if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1)) |
| 4882 | { |
| 4883 | #ifdef FEAT_COMMENTS |
| 4884 | if (leader_len == 0 && next_leader_len == 0) |
| 4885 | { |
| 4886 | /* no comment found */ |
| 4887 | #endif |
| 4888 | second_indent = |
| 4889 | get_indent_lnum(curwin->w_cursor.lnum + 1); |
| 4890 | #ifdef FEAT_COMMENTS |
| 4891 | } |
| 4892 | else |
| 4893 | { |
| 4894 | second_indent = next_leader_len; |
| 4895 | do_comments_list = 1; |
| 4896 | } |
| 4897 | #endif |
| 4898 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4899 | else if (do_number_indent) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4900 | { |
| 4901 | #ifdef FEAT_COMMENTS |
| 4902 | if (leader_len == 0 && next_leader_len == 0) |
| 4903 | { |
| 4904 | /* no comment found */ |
| 4905 | #endif |
| 4906 | second_indent = |
| 4907 | get_number_indent(curwin->w_cursor.lnum); |
| 4908 | #ifdef FEAT_COMMENTS |
| 4909 | } |
| 4910 | else |
| 4911 | { |
| 4912 | /* get_number_indent() is now "comment aware"... */ |
| 4913 | second_indent = |
| 4914 | get_number_indent(curwin->w_cursor.lnum); |
| 4915 | do_comments_list = 1; |
| 4916 | } |
| 4917 | #endif |
| 4918 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | /* |
| 4922 | * When the comment leader changes, it's the end of the paragraph. |
| 4923 | */ |
| 4924 | if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count |
| 4925 | #ifdef FEAT_COMMENTS |
| 4926 | || !same_leader(curwin->w_cursor.lnum, |
| 4927 | leader_len, leader_flags, |
| 4928 | next_leader_len, next_leader_flags) |
| 4929 | #endif |
| 4930 | ) |
| 4931 | is_end_par = TRUE; |
| 4932 | |
| 4933 | /* |
| 4934 | * If we have got to the end of a paragraph, or the line is |
| 4935 | * getting long, format it. |
| 4936 | */ |
| 4937 | if (is_end_par || force_format) |
| 4938 | { |
| 4939 | if (need_set_indent) |
| 4940 | /* replace indent in first line with minimal number of |
| 4941 | * tabs and spaces, according to current options */ |
| 4942 | (void)set_indent(get_indent(), SIN_CHANGED); |
| 4943 | |
| 4944 | /* put cursor on last non-space */ |
| 4945 | State = NORMAL; /* don't go past end-of-line */ |
| 4946 | coladvance((colnr_T)MAXCOL); |
| 4947 | while (curwin->w_cursor.col && vim_isspace(gchar_cursor())) |
| 4948 | dec_cursor(); |
| 4949 | |
| 4950 | /* do the formatting, without 'showmode' */ |
| 4951 | State = INSERT; /* for open_line() */ |
| 4952 | smd_save = p_smd; |
| 4953 | p_smd = FALSE; |
| 4954 | insertchar(NUL, INSCHAR_FORMAT |
| 4955 | #ifdef FEAT_COMMENTS |
| 4956 | + (do_comments ? INSCHAR_DO_COM : 0) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 4957 | + (do_comments && do_comments_list |
| 4958 | ? INSCHAR_COM_LIST : 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4959 | #endif |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 4960 | + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4961 | State = old_State; |
| 4962 | p_smd = smd_save; |
| 4963 | second_indent = -1; |
| 4964 | /* at end of par.: need to set indent of next par. */ |
| 4965 | need_set_indent = is_end_par; |
| 4966 | if (is_end_par) |
| 4967 | { |
| 4968 | /* When called with a negative line count, break at the |
| 4969 | * end of the paragraph. */ |
| 4970 | if (line_count < 0) |
| 4971 | break; |
| 4972 | first_par_line = TRUE; |
| 4973 | } |
| 4974 | force_format = FALSE; |
| 4975 | } |
| 4976 | |
| 4977 | /* |
| 4978 | * When still in same paragraph, join the lines together. But |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 4979 | * first delete the leader from the second line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4980 | */ |
| 4981 | if (!is_end_par) |
| 4982 | { |
| 4983 | advance = FALSE; |
| 4984 | curwin->w_cursor.lnum++; |
| 4985 | curwin->w_cursor.col = 0; |
| 4986 | if (line_count < 0 && u_save_cursor() == FAIL) |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 4987 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4988 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4989 | if (next_leader_len > 0) |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 4990 | { |
| 4991 | (void)del_bytes((long)next_leader_len, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4992 | mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L, |
| 4993 | (long)-next_leader_len); |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 4994 | } else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4995 | #endif |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 4996 | if (second_indent > 0) /* the "leader" for FO_Q_SECOND */ |
| 4997 | { |
| 4998 | char_u *p = ml_get_curline(); |
Bram Moolenaar | 92c2db8 | 2013-11-02 23:59:35 +0100 | [diff] [blame] | 4999 | int indent = (int)(skipwhite(p) - p); |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 5000 | |
| 5001 | if (indent > 0) |
| 5002 | { |
| 5003 | (void)del_bytes(indent, FALSE, FALSE); |
| 5004 | mark_col_adjust(curwin->w_cursor.lnum, |
| 5005 | (colnr_T)0, 0L, (long)-indent); |
Bram Moolenaar | 92c2db8 | 2013-11-02 23:59:35 +0100 | [diff] [blame] | 5006 | } |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 5007 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5008 | curwin->w_cursor.lnum--; |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 5009 | if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5010 | { |
| 5011 | beep_flush(); |
| 5012 | break; |
| 5013 | } |
| 5014 | first_par_line = FALSE; |
| 5015 | /* If the line is getting long, format it next time */ |
| 5016 | if (STRLEN(ml_get_curline()) > (size_t)max_len) |
| 5017 | force_format = TRUE; |
| 5018 | else |
| 5019 | force_format = FALSE; |
| 5020 | } |
| 5021 | } |
| 5022 | line_breakcheck(); |
| 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | /* |
| 5027 | * Return TRUE if line "lnum" ends in a white character. |
| 5028 | */ |
| 5029 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5030 | ends_in_white(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5031 | { |
| 5032 | char_u *s = ml_get(lnum); |
| 5033 | size_t l; |
| 5034 | |
| 5035 | if (*s == NUL) |
| 5036 | return FALSE; |
| 5037 | /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro |
| 5038 | * invocation may call function multiple times". */ |
| 5039 | l = STRLEN(s) - 1; |
| 5040 | return vim_iswhite(s[l]); |
| 5041 | } |
| 5042 | |
| 5043 | /* |
| 5044 | * Blank lines, and lines containing only the comment leader, are left |
| 5045 | * untouched by the formatting. The function returns TRUE in this |
| 5046 | * case. It also returns TRUE when a line starts with the end of a comment |
| 5047 | * ('e' in comment flags), so that this line is skipped, and not joined to the |
| 5048 | * previous line. A new paragraph starts after a blank line, or when the |
| 5049 | * comment leader changes -- webb. |
| 5050 | */ |
| 5051 | #ifdef FEAT_COMMENTS |
| 5052 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5053 | fmt_check_par( |
| 5054 | linenr_T lnum, |
| 5055 | int *leader_len, |
| 5056 | char_u **leader_flags, |
| 5057 | int do_comments) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5058 | { |
| 5059 | char_u *flags = NULL; /* init for GCC */ |
| 5060 | char_u *ptr; |
| 5061 | |
| 5062 | ptr = ml_get(lnum); |
| 5063 | if (do_comments) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 5064 | *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5065 | else |
| 5066 | *leader_len = 0; |
| 5067 | |
| 5068 | if (*leader_len > 0) |
| 5069 | { |
| 5070 | /* |
| 5071 | * Search for 'e' flag in comment leader flags. |
| 5072 | */ |
| 5073 | flags = *leader_flags; |
| 5074 | while (*flags && *flags != ':' && *flags != COM_END) |
| 5075 | ++flags; |
| 5076 | } |
| 5077 | |
| 5078 | return (*skipwhite(ptr + *leader_len) == NUL |
| 5079 | || (*leader_len > 0 && *flags == COM_END) |
| 5080 | || startPS(lnum, NUL, FALSE)); |
| 5081 | } |
| 5082 | #else |
| 5083 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5084 | fmt_check_par(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5085 | { |
| 5086 | return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE)); |
| 5087 | } |
| 5088 | #endif |
| 5089 | |
| 5090 | /* |
| 5091 | * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the |
| 5092 | * previous line is in the same paragraph. Used for auto-formatting. |
| 5093 | */ |
| 5094 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5095 | paragraph_start(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5096 | { |
| 5097 | char_u *p; |
| 5098 | #ifdef FEAT_COMMENTS |
| 5099 | int leader_len = 0; /* leader len of current line */ |
| 5100 | char_u *leader_flags = NULL; /* flags for leader of current line */ |
| 5101 | int next_leader_len; /* leader len of next line */ |
| 5102 | char_u *next_leader_flags; /* flags for leader of next line */ |
| 5103 | int do_comments; /* format comments */ |
| 5104 | #endif |
| 5105 | |
| 5106 | if (lnum <= 1) |
| 5107 | return TRUE; /* start of the file */ |
| 5108 | |
| 5109 | p = ml_get(lnum - 1); |
| 5110 | if (*p == NUL) |
| 5111 | return TRUE; /* after empty line */ |
| 5112 | |
| 5113 | #ifdef FEAT_COMMENTS |
| 5114 | do_comments = has_format_option(FO_Q_COMS); |
| 5115 | #endif |
| 5116 | if (fmt_check_par(lnum - 1 |
| 5117 | #ifdef FEAT_COMMENTS |
| 5118 | , &leader_len, &leader_flags, do_comments |
| 5119 | #endif |
| 5120 | )) |
| 5121 | return TRUE; /* after non-paragraph line */ |
| 5122 | |
| 5123 | if (fmt_check_par(lnum |
| 5124 | #ifdef FEAT_COMMENTS |
| 5125 | , &next_leader_len, &next_leader_flags, do_comments |
| 5126 | #endif |
| 5127 | )) |
| 5128 | return TRUE; /* "lnum" is not a paragraph line */ |
| 5129 | |
| 5130 | if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1)) |
| 5131 | return TRUE; /* missing trailing space in previous line. */ |
| 5132 | |
| 5133 | if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0)) |
| 5134 | return TRUE; /* numbered item starts in "lnum". */ |
| 5135 | |
| 5136 | #ifdef FEAT_COMMENTS |
| 5137 | if (!same_leader(lnum - 1, leader_len, leader_flags, |
| 5138 | next_leader_len, next_leader_flags)) |
| 5139 | return TRUE; /* change of comment leader. */ |
| 5140 | #endif |
| 5141 | |
| 5142 | return FALSE; |
| 5143 | } |
| 5144 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5145 | /* |
| 5146 | * prepare a few things for block mode yank/delete/tilde |
| 5147 | * |
| 5148 | * for delete: |
| 5149 | * - textlen includes the first/last char to be (partly) deleted |
| 5150 | * - start/endspaces is the number of columns that are taken by the |
| 5151 | * 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] | 5152 | * deleted. |
| 5153 | * for yank and tilde: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5154 | * - textlen includes the first/last char to be wholly yanked |
| 5155 | * - start/endspaces is the number of columns of the first/last yanked char |
| 5156 | * that are to be yanked. |
| 5157 | */ |
| 5158 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5159 | block_prep( |
| 5160 | oparg_T *oap, |
| 5161 | struct block_def *bdp, |
| 5162 | linenr_T lnum, |
| 5163 | int is_del) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5164 | { |
| 5165 | int incr = 0; |
| 5166 | char_u *pend; |
| 5167 | char_u *pstart; |
| 5168 | char_u *line; |
| 5169 | char_u *prev_pstart; |
| 5170 | char_u *prev_pend; |
| 5171 | |
| 5172 | bdp->startspaces = 0; |
| 5173 | bdp->endspaces = 0; |
| 5174 | bdp->textlen = 0; |
| 5175 | bdp->start_vcol = 0; |
| 5176 | bdp->end_vcol = 0; |
| 5177 | #ifdef FEAT_VISUALEXTRA |
| 5178 | bdp->is_short = FALSE; |
| 5179 | bdp->is_oneChar = FALSE; |
| 5180 | bdp->pre_whitesp = 0; |
| 5181 | bdp->pre_whitesp_c = 0; |
| 5182 | bdp->end_char_vcols = 0; |
| 5183 | #endif |
| 5184 | bdp->start_char_vcols = 0; |
| 5185 | |
| 5186 | line = ml_get(lnum); |
| 5187 | pstart = line; |
| 5188 | prev_pstart = line; |
| 5189 | while (bdp->start_vcol < oap->start_vcol && *pstart) |
| 5190 | { |
| 5191 | /* 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] | 5192 | incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5193 | bdp->start_vcol += incr; |
| 5194 | #ifdef FEAT_VISUALEXTRA |
| 5195 | if (vim_iswhite(*pstart)) |
| 5196 | { |
| 5197 | bdp->pre_whitesp += incr; |
| 5198 | bdp->pre_whitesp_c++; |
| 5199 | } |
| 5200 | else |
| 5201 | { |
| 5202 | bdp->pre_whitesp = 0; |
| 5203 | bdp->pre_whitesp_c = 0; |
| 5204 | } |
| 5205 | #endif |
| 5206 | prev_pstart = pstart; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5207 | mb_ptr_adv(pstart); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5208 | } |
| 5209 | bdp->start_char_vcols = incr; |
| 5210 | if (bdp->start_vcol < oap->start_vcol) /* line too short */ |
| 5211 | { |
| 5212 | bdp->end_vcol = bdp->start_vcol; |
| 5213 | #ifdef FEAT_VISUALEXTRA |
| 5214 | bdp->is_short = TRUE; |
| 5215 | #endif |
| 5216 | if (!is_del || oap->op_type == OP_APPEND) |
| 5217 | bdp->endspaces = oap->end_vcol - oap->start_vcol + 1; |
| 5218 | } |
| 5219 | else |
| 5220 | { |
| 5221 | /* notice: this converts partly selected Multibyte characters to |
| 5222 | * spaces, too. */ |
| 5223 | bdp->startspaces = bdp->start_vcol - oap->start_vcol; |
| 5224 | if (is_del && bdp->startspaces) |
| 5225 | bdp->startspaces = bdp->start_char_vcols - bdp->startspaces; |
| 5226 | pend = pstart; |
| 5227 | bdp->end_vcol = bdp->start_vcol; |
| 5228 | if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */ |
| 5229 | { |
| 5230 | #ifdef FEAT_VISUALEXTRA |
| 5231 | bdp->is_oneChar = TRUE; |
| 5232 | #endif |
| 5233 | if (oap->op_type == OP_INSERT) |
| 5234 | bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; |
| 5235 | else if (oap->op_type == OP_APPEND) |
| 5236 | { |
| 5237 | bdp->startspaces += oap->end_vcol - oap->start_vcol + 1; |
| 5238 | bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; |
| 5239 | } |
| 5240 | else |
| 5241 | { |
| 5242 | bdp->startspaces = oap->end_vcol - oap->start_vcol + 1; |
| 5243 | if (is_del && oap->op_type != OP_LSHIFT) |
| 5244 | { |
| 5245 | /* just putting the sum of those two into |
| 5246 | * bdp->startspaces doesn't work for Visual replace, |
| 5247 | * so we have to split the tab in two */ |
| 5248 | bdp->startspaces = bdp->start_char_vcols |
| 5249 | - (bdp->start_vcol - oap->start_vcol); |
| 5250 | bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; |
| 5251 | } |
| 5252 | } |
| 5253 | } |
| 5254 | else |
| 5255 | { |
| 5256 | prev_pend = pend; |
| 5257 | while (bdp->end_vcol <= oap->end_vcol && *pend != NUL) |
| 5258 | { |
| 5259 | /* Count a tab for what it's worth (if list mode not on) */ |
| 5260 | prev_pend = pend; |
Bram Moolenaar | 1dc9233 | 2015-01-27 13:22:20 +0100 | [diff] [blame] | 5261 | incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5262 | bdp->end_vcol += incr; |
| 5263 | } |
| 5264 | if (bdp->end_vcol <= oap->end_vcol |
| 5265 | && (!is_del |
| 5266 | || oap->op_type == OP_APPEND |
| 5267 | || oap->op_type == OP_REPLACE)) /* line too short */ |
| 5268 | { |
| 5269 | #ifdef FEAT_VISUALEXTRA |
| 5270 | bdp->is_short = TRUE; |
| 5271 | #endif |
| 5272 | /* Alternative: include spaces to fill up the block. |
| 5273 | * Disadvantage: can lead to trailing spaces when the line is |
| 5274 | * short where the text is put */ |
| 5275 | /* if (!is_del || oap->op_type == OP_APPEND) */ |
| 5276 | if (oap->op_type == OP_APPEND || virtual_op) |
| 5277 | bdp->endspaces = oap->end_vcol - bdp->end_vcol |
Bram Moolenaar | 2c7a29c | 2005-12-12 22:02:31 +0000 | [diff] [blame] | 5278 | + oap->inclusive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5279 | else |
| 5280 | bdp->endspaces = 0; /* replace doesn't add characters */ |
| 5281 | } |
| 5282 | else if (bdp->end_vcol > oap->end_vcol) |
| 5283 | { |
| 5284 | bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; |
| 5285 | if (!is_del && bdp->endspaces) |
| 5286 | { |
| 5287 | bdp->endspaces = incr - bdp->endspaces; |
| 5288 | if (pend != pstart) |
| 5289 | pend = prev_pend; |
| 5290 | } |
| 5291 | } |
| 5292 | } |
| 5293 | #ifdef FEAT_VISUALEXTRA |
| 5294 | bdp->end_char_vcols = incr; |
| 5295 | #endif |
| 5296 | if (is_del && bdp->startspaces) |
| 5297 | pstart = prev_pstart; |
| 5298 | bdp->textlen = (int)(pend - pstart); |
| 5299 | } |
| 5300 | bdp->textcol = (colnr_T) (pstart - line); |
| 5301 | bdp->textstart = pstart; |
| 5302 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5303 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5304 | /* |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5305 | * Handle the add/subtract operator. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5306 | */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5307 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5308 | op_addsub( |
| 5309 | oparg_T *oap, |
| 5310 | linenr_T Prenum1, /* Amount of add/subtract */ |
| 5311 | int g_cmd) /* was g<c-a>/g<c-x> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5312 | { |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5313 | pos_T pos; |
| 5314 | struct block_def bd; |
| 5315 | int change_cnt = 0; |
| 5316 | linenr_T amount = Prenum1; |
| 5317 | |
| 5318 | if (!VIsual_active) |
| 5319 | { |
| 5320 | pos = curwin->w_cursor; |
| 5321 | if (u_save_cursor() == FAIL) |
| 5322 | return; |
| 5323 | change_cnt = do_addsub(oap->op_type, &pos, 0, amount); |
| 5324 | if (change_cnt) |
| 5325 | changed_lines(pos.lnum, 0, pos.lnum + 1, 0L); |
| 5326 | } |
| 5327 | else |
| 5328 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5329 | int one_change; |
| 5330 | int length; |
| 5331 | pos_T startpos; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5332 | |
| 5333 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 5334 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 5335 | return; |
| 5336 | |
| 5337 | pos = oap->start; |
| 5338 | for (; pos.lnum <= oap->end.lnum; ++pos.lnum) |
| 5339 | { |
| 5340 | if (oap->block_mode) /* Visual block mode */ |
| 5341 | { |
| 5342 | block_prep(oap, &bd, pos.lnum, FALSE); |
| 5343 | pos.col = bd.textcol; |
| 5344 | length = bd.textlen; |
| 5345 | } |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5346 | else if (oap->motion_type == MLINE) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5347 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5348 | curwin->w_cursor.col = 0; |
| 5349 | pos.col = 0; |
| 5350 | length = (colnr_T)STRLEN(ml_get(pos.lnum)); |
| 5351 | } |
| 5352 | else /* oap->motion_type == MCHAR */ |
| 5353 | { |
| 5354 | if (!oap->inclusive) |
| 5355 | dec(&(oap->end)); |
| 5356 | length = (colnr_T)STRLEN(ml_get(pos.lnum)); |
| 5357 | pos.col = 0; |
| 5358 | if (pos.lnum == oap->start.lnum) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5359 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5360 | pos.col += oap->start.col; |
| 5361 | length -= oap->start.col; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5362 | } |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5363 | if (pos.lnum == oap->end.lnum) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5364 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5365 | length = (int)STRLEN(ml_get(oap->end.lnum)); |
| 5366 | if (oap->end.col >= length) |
| 5367 | oap->end.col = length - 1; |
| 5368 | length = oap->end.col - pos.col + 1; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5369 | } |
| 5370 | } |
| 5371 | one_change = do_addsub(oap->op_type, &pos, length, amount); |
| 5372 | if (one_change) |
| 5373 | { |
| 5374 | /* Remember the start position of the first change. */ |
| 5375 | if (change_cnt == 0) |
| 5376 | startpos = curbuf->b_op_start; |
| 5377 | ++change_cnt; |
| 5378 | } |
| 5379 | |
| 5380 | #ifdef FEAT_NETBEANS_INTG |
| 5381 | if (netbeans_active() && one_change) |
| 5382 | { |
| 5383 | char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 5384 | |
| 5385 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)length); |
| 5386 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
| 5387 | &ptr[pos.col], length); |
| 5388 | } |
| 5389 | #endif |
| 5390 | if (g_cmd && one_change) |
| 5391 | amount += Prenum1; |
| 5392 | } |
| 5393 | if (change_cnt) |
| 5394 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
| 5395 | |
| 5396 | if (!change_cnt && oap->is_VIsual) |
| 5397 | /* No change: need to remove the Visual selection */ |
| 5398 | redraw_curbuf_later(INVERTED); |
| 5399 | |
| 5400 | /* Set '[ mark if something changed. Keep the last end |
| 5401 | * position from do_addsub(). */ |
| 5402 | if (change_cnt > 0) |
| 5403 | curbuf->b_op_start = startpos; |
| 5404 | |
| 5405 | if (change_cnt > p_report) |
| 5406 | { |
| 5407 | if (change_cnt == 1) |
| 5408 | MSG(_("1 line changed")); |
| 5409 | else |
| 5410 | smsg((char_u *)_("%ld lines changed"), change_cnt); |
| 5411 | } |
| 5412 | } |
| 5413 | } |
| 5414 | |
| 5415 | /* |
| 5416 | * Add or subtract 'Prenum1' from a number in a line |
| 5417 | * op_type is OP_NR_ADD or OP_NR_SUB |
| 5418 | * |
| 5419 | * Returns TRUE if some character was changed. |
| 5420 | */ |
| 5421 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5422 | do_addsub( |
| 5423 | int op_type, |
| 5424 | pos_T *pos, |
| 5425 | int length, |
| 5426 | linenr_T Prenum1) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5427 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5428 | int col; |
| 5429 | char_u *buf1; |
| 5430 | char_u buf2[NUMBUFLEN]; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5431 | int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5432 | static int hexupper = FALSE; /* 0xABC */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5433 | unsigned long n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5434 | long_u oldn; |
| 5435 | char_u *ptr; |
| 5436 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5437 | int todel; |
| 5438 | int dohex; |
| 5439 | int dooct; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5440 | int dobin; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5441 | int doalp; |
| 5442 | int firstdigit; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5443 | int subtract; |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5444 | int negative = FALSE; |
Bram Moolenaar | 9bb1930 | 2015-07-03 12:44:07 +0200 | [diff] [blame] | 5445 | int was_positive = TRUE; |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5446 | int visual = VIsual_active; |
Bram Moolenaar | 3ec3261 | 2015-07-12 15:02:38 +0200 | [diff] [blame] | 5447 | int did_change = FALSE; |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5448 | pos_T save_cursor = curwin->w_cursor; |
Bram Moolenaar | 5d1bc78 | 2015-07-17 13:03:48 +0200 | [diff] [blame] | 5449 | int maxlen = 0; |
Bram Moolenaar | a52dfae | 2016-01-10 20:21:57 +0100 | [diff] [blame] | 5450 | pos_T startpos; |
| 5451 | pos_T endpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5452 | |
| 5453 | dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */ |
| 5454 | dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */ |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5455 | dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5456 | doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */ |
| 5457 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5458 | curwin->w_cursor = *pos; |
| 5459 | ptr = ml_get(pos->lnum); |
| 5460 | col = pos->col; |
| 5461 | |
| 5462 | if (*ptr == NUL) |
| 5463 | goto theend; |
| 5464 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5465 | /* |
| 5466 | * First check if we are on a hexadecimal number, after the "0x". |
| 5467 | */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5468 | if (!VIsual_active) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5469 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5470 | if (dobin) |
| 5471 | while (col > 0 && vim_isbdigit(ptr[col])) |
| 5472 | --col; |
| 5473 | |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5474 | if (dohex) |
| 5475 | while (col > 0 && vim_isxdigit(ptr[col])) |
| 5476 | --col; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5477 | |
| 5478 | if ( dobin |
| 5479 | && dohex |
| 5480 | && ! ((col > 0 |
| 5481 | && (ptr[col] == 'X' |
| 5482 | || ptr[col] == 'x') |
| 5483 | && ptr[col - 1] == '0' |
| 5484 | && vim_isxdigit(ptr[col + 1])))) |
| 5485 | { |
| 5486 | |
| 5487 | /* In case of binary/hexadecimal pattern overlap match, rescan */ |
| 5488 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5489 | col = pos->col; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5490 | |
| 5491 | while (col > 0 && vim_isdigit(ptr[col])) |
| 5492 | col--; |
| 5493 | } |
| 5494 | |
| 5495 | if (( dohex |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5496 | && col > 0 |
| 5497 | && (ptr[col] == 'X' |
| 5498 | || ptr[col] == 'x') |
| 5499 | && ptr[col - 1] == '0' |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5500 | && vim_isxdigit(ptr[col + 1])) || |
| 5501 | ( dobin |
| 5502 | && col > 0 |
| 5503 | && (ptr[col] == 'B' |
| 5504 | || ptr[col] == 'b') |
| 5505 | && ptr[col - 1] == '0' |
| 5506 | && vim_isbdigit(ptr[col + 1]))) |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5507 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 5508 | /* Found hexadecimal or binary number, move to its start. */ |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5509 | --col; |
| 5510 | } |
| 5511 | else |
| 5512 | { |
| 5513 | /* |
| 5514 | * Search forward and then backward to find the start of number. |
| 5515 | */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5516 | col = pos->col; |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 5517 | |
| 5518 | while (ptr[col] != NUL |
| 5519 | && !vim_isdigit(ptr[col]) |
| 5520 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
| 5521 | ++col; |
| 5522 | |
| 5523 | while (col > 0 |
| 5524 | && vim_isdigit(ptr[col - 1]) |
| 5525 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
| 5526 | --col; |
| 5527 | } |
| 5528 | } |
| 5529 | |
Bram Moolenaar | 5d1bc78 | 2015-07-17 13:03:48 +0200 | [diff] [blame] | 5530 | if (visual) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5531 | { |
| 5532 | while (ptr[col] != NUL && length > 0 |
| 5533 | && !vim_isdigit(ptr[col]) |
| 5534 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
| 5535 | { |
| 5536 | ++col; |
| 5537 | --length; |
| 5538 | } |
| 5539 | |
| 5540 | if (length == 0) |
| 5541 | goto theend; |
| 5542 | |
| 5543 | if (col > pos->col && ptr[col - 1] == '-') |
| 5544 | { |
| 5545 | negative = TRUE; |
| 5546 | was_positive = FALSE; |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | /* |
| 5551 | * If a number was found, and saving for undo works, replace the number. |
| 5552 | */ |
| 5553 | firstdigit = ptr[col]; |
| 5554 | if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit))) |
| 5555 | { |
| 5556 | beep_flush(); |
| 5557 | goto theend; |
| 5558 | } |
| 5559 | |
| 5560 | if (doalp && ASCII_ISALPHA(firstdigit)) |
| 5561 | { |
| 5562 | /* decrement or increment alphabetic character */ |
| 5563 | if (op_type == OP_NR_SUB) |
| 5564 | { |
| 5565 | if (CharOrd(firstdigit) < Prenum1) |
| 5566 | { |
| 5567 | if (isupper(firstdigit)) |
| 5568 | firstdigit = 'A'; |
| 5569 | else |
| 5570 | firstdigit = 'a'; |
| 5571 | } |
| 5572 | else |
| 5573 | #ifdef EBCDIC |
| 5574 | firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1); |
| 5575 | #else |
| 5576 | firstdigit -= Prenum1; |
| 5577 | #endif |
| 5578 | } |
| 5579 | else |
| 5580 | { |
| 5581 | if (26 - CharOrd(firstdigit) - 1 < Prenum1) |
| 5582 | { |
| 5583 | if (isupper(firstdigit)) |
| 5584 | firstdigit = 'Z'; |
| 5585 | else |
| 5586 | firstdigit = 'z'; |
| 5587 | } |
| 5588 | else |
| 5589 | #ifdef EBCDIC |
| 5590 | firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1); |
| 5591 | #else |
| 5592 | firstdigit += Prenum1; |
| 5593 | #endif |
| 5594 | } |
| 5595 | curwin->w_cursor.col = col; |
| 5596 | if (!did_change) |
| 5597 | startpos = curwin->w_cursor; |
| 5598 | did_change = TRUE; |
| 5599 | (void)del_char(FALSE); |
| 5600 | ins_char(firstdigit); |
| 5601 | endpos = curwin->w_cursor; |
| 5602 | curwin->w_cursor.col = col; |
| 5603 | } |
| 5604 | else |
| 5605 | { |
| 5606 | if (col > 0 && ptr[col - 1] == '-' && !visual) |
| 5607 | { |
| 5608 | /* negative number */ |
| 5609 | --col; |
| 5610 | negative = TRUE; |
| 5611 | } |
| 5612 | /* get the number value (unsigned) */ |
| 5613 | if (visual && VIsual_mode != 'V') |
| 5614 | maxlen = (curbuf->b_visual.vi_curswant == MAXCOL |
| 5615 | ? (int)STRLEN(ptr) - col |
| 5616 | : length); |
| 5617 | |
| 5618 | vim_str2nr(ptr + col, &pre, &length, |
| 5619 | 0 + (dobin ? STR2NR_BIN : 0) |
| 5620 | + (dooct ? STR2NR_OCT : 0) |
| 5621 | + (dohex ? STR2NR_HEX : 0), |
| 5622 | NULL, &n, maxlen); |
| 5623 | |
| 5624 | /* ignore leading '-' for hex and octal and bin numbers */ |
| 5625 | if (pre && negative) |
| 5626 | { |
| 5627 | ++col; |
| 5628 | --length; |
| 5629 | negative = FALSE; |
| 5630 | } |
| 5631 | /* add or subtract */ |
| 5632 | subtract = FALSE; |
| 5633 | if (op_type == OP_NR_SUB) |
| 5634 | subtract ^= TRUE; |
| 5635 | if (negative) |
| 5636 | subtract ^= TRUE; |
| 5637 | |
| 5638 | oldn = n; |
| 5639 | if (subtract) |
| 5640 | n -= (unsigned long)Prenum1; |
| 5641 | else |
| 5642 | n += (unsigned long)Prenum1; |
| 5643 | /* handle wraparound for decimal numbers */ |
| 5644 | if (!pre) |
| 5645 | { |
| 5646 | if (subtract) |
| 5647 | { |
| 5648 | if (n > oldn) |
| 5649 | { |
| 5650 | n = 1 + (n ^ (unsigned long)-1); |
| 5651 | negative ^= TRUE; |
| 5652 | } |
| 5653 | } |
| 5654 | else |
| 5655 | { |
| 5656 | /* add */ |
| 5657 | if (n < oldn) |
| 5658 | { |
| 5659 | n = (n ^ (unsigned long)-1); |
| 5660 | negative ^= TRUE; |
| 5661 | } |
| 5662 | } |
| 5663 | if (n == 0) |
| 5664 | negative = FALSE; |
| 5665 | } |
| 5666 | |
| 5667 | if (visual && !was_positive && !negative && col > 0) |
| 5668 | { |
| 5669 | /* need to remove the '-' */ |
| 5670 | col--; |
| 5671 | length++; |
| 5672 | } |
| 5673 | |
| 5674 | /* |
| 5675 | * Delete the old number. |
| 5676 | */ |
| 5677 | curwin->w_cursor.col = col; |
| 5678 | if (!did_change) |
| 5679 | startpos = curwin->w_cursor; |
| 5680 | did_change = TRUE; |
| 5681 | todel = length; |
| 5682 | c = gchar_cursor(); |
| 5683 | /* |
| 5684 | * Don't include the '-' in the length, only the length of the |
| 5685 | * part after it is kept the same. |
| 5686 | */ |
| 5687 | if (c == '-') |
| 5688 | --length; |
| 5689 | while (todel-- > 0) |
| 5690 | { |
| 5691 | if (c < 0x100 && isalpha(c)) |
| 5692 | { |
| 5693 | if (isupper(c)) |
| 5694 | hexupper = TRUE; |
| 5695 | else |
| 5696 | hexupper = FALSE; |
| 5697 | } |
| 5698 | /* del_char() will mark line needing displaying */ |
| 5699 | (void)del_char(FALSE); |
| 5700 | c = gchar_cursor(); |
| 5701 | } |
| 5702 | |
| 5703 | /* |
| 5704 | * Prepare the leading characters in buf1[]. |
| 5705 | * When there are many leading zeros it could be very long. |
| 5706 | * Allocate a bit too much. |
| 5707 | */ |
| 5708 | buf1 = alloc((unsigned)length + NUMBUFLEN); |
| 5709 | if (buf1 == NULL) |
| 5710 | goto theend; |
| 5711 | ptr = buf1; |
| 5712 | if (negative && (!visual || (visual && was_positive))) |
| 5713 | { |
| 5714 | *ptr++ = '-'; |
| 5715 | } |
| 5716 | if (pre) |
| 5717 | { |
| 5718 | *ptr++ = '0'; |
| 5719 | --length; |
| 5720 | } |
| 5721 | if (pre == 'b' || pre == 'B' || |
| 5722 | pre == 'x' || pre == 'X') |
| 5723 | { |
| 5724 | *ptr++ = pre; |
| 5725 | --length; |
| 5726 | } |
| 5727 | |
| 5728 | /* |
| 5729 | * Put the number characters in buf2[]. |
| 5730 | */ |
| 5731 | if (pre == 'b' || pre == 'B') |
| 5732 | { |
| 5733 | int i; |
| 5734 | int bit = 0; |
| 5735 | int bits = sizeof(unsigned long) * 8; |
| 5736 | |
| 5737 | /* leading zeros */ |
| 5738 | for (bit = bits; bit > 0; bit--) |
| 5739 | if ((n >> (bit - 1)) & 0x1) break; |
| 5740 | |
| 5741 | for (i = 0; bit > 0; bit--) |
| 5742 | buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0'; |
| 5743 | |
| 5744 | buf2[i] = '\0'; |
| 5745 | } |
| 5746 | else if (pre == 0) |
| 5747 | sprintf((char *)buf2, "%lu", n); |
| 5748 | else if (pre == '0') |
| 5749 | sprintf((char *)buf2, "%lo", n); |
| 5750 | else if (pre && hexupper) |
| 5751 | sprintf((char *)buf2, "%lX", n); |
| 5752 | else |
| 5753 | sprintf((char *)buf2, "%lx", n); |
| 5754 | length -= (int)STRLEN(buf2); |
| 5755 | |
| 5756 | /* |
| 5757 | * Adjust number of zeros to the new number of digits, so the |
| 5758 | * total length of the number remains the same. |
| 5759 | * Don't do this when |
| 5760 | * the result may look like an octal number. |
| 5761 | */ |
| 5762 | if (firstdigit == '0' && !(dooct && pre == 0)) |
| 5763 | while (length-- > 0) |
| 5764 | *ptr++ = '0'; |
| 5765 | *ptr = NUL; |
| 5766 | STRCAT(buf1, buf2); |
| 5767 | ins_str(buf1); /* insert the new number */ |
| 5768 | vim_free(buf1); |
| 5769 | endpos = curwin->w_cursor; |
| 5770 | if (did_change && curwin->w_cursor.col) |
| 5771 | --curwin->w_cursor.col; |
| 5772 | } |
| 5773 | |
Bram Moolenaar | a52dfae | 2016-01-10 20:21:57 +0100 | [diff] [blame] | 5774 | if (did_change) |
| 5775 | { |
| 5776 | /* set the '[ and '] marks */ |
| 5777 | curbuf->b_op_start = startpos; |
| 5778 | curbuf->b_op_end = endpos; |
| 5779 | if (curbuf->b_op_end.col > 0) |
| 5780 | --curbuf->b_op_end.col; |
| 5781 | } |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5782 | |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 5783 | theend: |
| 5784 | if (visual) |
| 5785 | curwin->w_cursor = save_cursor; |
| 5786 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 5787 | return did_change; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | #ifdef FEAT_VIMINFO |
| 5791 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5792 | read_viminfo_register(vir_T *virp, int force) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5793 | { |
| 5794 | int eof; |
| 5795 | int do_it = TRUE; |
| 5796 | int size; |
| 5797 | int limit; |
| 5798 | int i; |
| 5799 | int set_prev = FALSE; |
| 5800 | char_u *str; |
| 5801 | char_u **array = NULL; |
Bram Moolenaar | 7cbc703 | 2015-01-18 14:08:56 +0100 | [diff] [blame] | 5802 | int new_type = MCHAR; /* init to shut up compiler */ |
| 5803 | colnr_T new_width = 0; /* init to shut up compiler */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5804 | |
| 5805 | /* We only get here (hopefully) if line[0] == '"' */ |
| 5806 | str = virp->vir_line + 1; |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 5807 | |
| 5808 | /* If the line starts with "" this is the y_previous register. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5809 | if (*str == '"') |
| 5810 | { |
| 5811 | set_prev = TRUE; |
| 5812 | str++; |
| 5813 | } |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 5814 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5815 | if (!ASCII_ISALNUM(*str) && *str != '-') |
| 5816 | { |
| 5817 | if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line)) |
| 5818 | return TRUE; /* too many errors, pretend end-of-file */ |
| 5819 | do_it = FALSE; |
| 5820 | } |
| 5821 | get_yank_register(*str++, FALSE); |
| 5822 | if (!force && y_current->y_array != NULL) |
| 5823 | do_it = FALSE; |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 5824 | |
| 5825 | if (*str == '@') |
| 5826 | { |
| 5827 | /* "x@: register x used for @@ */ |
| 5828 | if (force || execreg_lastc == NUL) |
| 5829 | execreg_lastc = str[-1]; |
| 5830 | } |
| 5831 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5832 | size = 0; |
| 5833 | limit = 100; /* Optimized for registers containing <= 100 lines */ |
| 5834 | if (do_it) |
| 5835 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5836 | /* |
| 5837 | * Build the new register in array[]. |
| 5838 | * y_array is kept as-is until done. |
| 5839 | * The "do_it" flag is reset when something is wrong, in which case |
| 5840 | * array[] needs to be freed. |
| 5841 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5842 | if (set_prev) |
| 5843 | y_previous = y_current; |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5844 | array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *))); |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 5845 | str = skipwhite(skiptowhite(str)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5846 | if (STRNCMP(str, "CHAR", 4) == 0) |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5847 | new_type = MCHAR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5848 | else if (STRNCMP(str, "BLOCK", 5) == 0) |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5849 | new_type = MBLOCK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5850 | else |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5851 | new_type = MLINE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5852 | /* get the block width; if it's missing we get a zero, which is OK */ |
| 5853 | str = skipwhite(skiptowhite(str)); |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5854 | new_width = getdigits(&str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5855 | } |
| 5856 | |
| 5857 | while (!(eof = viminfo_readline(virp)) |
| 5858 | && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<')) |
| 5859 | { |
| 5860 | if (do_it) |
| 5861 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5862 | if (size == limit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5863 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5864 | char_u **new_array = (char_u **) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5865 | alloc((unsigned)(limit * 2 * sizeof(char_u *))); |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5866 | |
| 5867 | if (new_array == NULL) |
| 5868 | { |
| 5869 | do_it = FALSE; |
| 5870 | break; |
| 5871 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5872 | for (i = 0; i < limit; i++) |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5873 | new_array[i] = array[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5874 | vim_free(array); |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5875 | array = new_array; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5876 | limit *= 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5877 | } |
| 5878 | str = viminfo_readstring(virp, 1, TRUE); |
| 5879 | if (str != NULL) |
| 5880 | array[size++] = str; |
| 5881 | else |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5882 | /* error, don't store the result */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5883 | do_it = FALSE; |
| 5884 | } |
| 5885 | } |
Bram Moolenaar | 7cbc703 | 2015-01-18 14:08:56 +0100 | [diff] [blame] | 5886 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5887 | if (do_it) |
| 5888 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5889 | /* free y_array[] */ |
| 5890 | for (i = 0; i < y_current->y_size; i++) |
| 5891 | vim_free(y_current->y_array[i]); |
| 5892 | vim_free(y_current->y_array); |
| 5893 | |
| 5894 | y_current->y_type = new_type; |
| 5895 | y_current->y_width = new_width; |
| 5896 | y_current->y_size = size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5897 | if (size == 0) |
| 5898 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5899 | y_current->y_array = NULL; |
| 5900 | } |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5901 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5902 | { |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5903 | /* Move the lines from array[] to y_array[]. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5904 | y_current->y_array = |
| 5905 | (char_u **)alloc((unsigned)(size * sizeof(char_u *))); |
| 5906 | for (i = 0; i < size; i++) |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5907 | { |
| 5908 | if (y_current->y_array == NULL) |
| 5909 | vim_free(array[i]); |
| 5910 | else |
| 5911 | y_current->y_array[i] = array[i]; |
| 5912 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5913 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5914 | } |
Bram Moolenaar | e88b003 | 2014-12-17 21:00:49 +0100 | [diff] [blame] | 5915 | else |
| 5916 | { |
| 5917 | /* Free array[] if it was filled. */ |
| 5918 | for (i = 0; i < size; i++) |
| 5919 | vim_free(array[i]); |
| 5920 | } |
| 5921 | vim_free(array); |
| 5922 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5923 | return eof; |
| 5924 | } |
| 5925 | |
| 5926 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5927 | write_viminfo_registers(FILE *fp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5928 | { |
| 5929 | int i, j; |
| 5930 | char_u *type; |
| 5931 | char_u c; |
| 5932 | int num_lines; |
| 5933 | int max_num_lines; |
| 5934 | int max_kbyte; |
| 5935 | long len; |
| 5936 | |
Bram Moolenaar | 6440447 | 2010-06-26 06:24:45 +0200 | [diff] [blame] | 5937 | fputs(_("\n# Registers:\n"), fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5938 | |
| 5939 | /* Get '<' value, use old '"' value if '<' is not found. */ |
| 5940 | max_num_lines = get_viminfo_parameter('<'); |
| 5941 | if (max_num_lines < 0) |
| 5942 | max_num_lines = get_viminfo_parameter('"'); |
| 5943 | if (max_num_lines == 0) |
| 5944 | return; |
| 5945 | max_kbyte = get_viminfo_parameter('s'); |
| 5946 | if (max_kbyte == 0) |
| 5947 | return; |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 5948 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5949 | for (i = 0; i < NUM_REGISTERS; i++) |
| 5950 | { |
| 5951 | if (y_regs[i].y_array == NULL) |
| 5952 | continue; |
| 5953 | #ifdef FEAT_CLIPBOARD |
| 5954 | /* Skip '*'/'+' register, we don't want them back next time */ |
| 5955 | if (i == STAR_REGISTER || i == PLUS_REGISTER) |
| 5956 | continue; |
| 5957 | #endif |
| 5958 | #ifdef FEAT_DND |
| 5959 | /* Neither do we want the '~' register */ |
| 5960 | if (i == TILDE_REGISTER) |
| 5961 | continue; |
| 5962 | #endif |
Bram Moolenaar | d7ee7ce | 2005-01-03 21:02:03 +0000 | [diff] [blame] | 5963 | /* Skip empty registers. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5964 | num_lines = y_regs[i].y_size; |
Bram Moolenaar | d7ee7ce | 2005-01-03 21:02:03 +0000 | [diff] [blame] | 5965 | if (num_lines == 0 |
| 5966 | || (num_lines == 1 && y_regs[i].y_type == MCHAR |
Bram Moolenaar | 6440447 | 2010-06-26 06:24:45 +0200 | [diff] [blame] | 5967 | && *y_regs[i].y_array[0] == NUL)) |
Bram Moolenaar | d7ee7ce | 2005-01-03 21:02:03 +0000 | [diff] [blame] | 5968 | continue; |
| 5969 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5970 | if (max_kbyte > 0) |
| 5971 | { |
| 5972 | /* Skip register if there is more text than the maximum size. */ |
| 5973 | len = 0; |
| 5974 | for (j = 0; j < num_lines; j++) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5975 | len += (long)STRLEN(y_regs[i].y_array[j]) + 1L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5976 | if (len > (long)max_kbyte * 1024L) |
| 5977 | continue; |
| 5978 | } |
| 5979 | |
| 5980 | switch (y_regs[i].y_type) |
| 5981 | { |
| 5982 | case MLINE: |
| 5983 | type = (char_u *)"LINE"; |
| 5984 | break; |
| 5985 | case MCHAR: |
| 5986 | type = (char_u *)"CHAR"; |
| 5987 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5988 | case MBLOCK: |
| 5989 | type = (char_u *)"BLOCK"; |
| 5990 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5991 | default: |
| 5992 | sprintf((char *)IObuff, _("E574: Unknown register type %d"), |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5993 | y_regs[i].y_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5994 | emsg(IObuff); |
| 5995 | type = (char_u *)"LINE"; |
| 5996 | break; |
| 5997 | } |
| 5998 | if (y_previous == &y_regs[i]) |
| 5999 | fprintf(fp, "\""); |
| 6000 | c = get_register_name(i); |
Bram Moolenaar | 42b9436 | 2009-05-26 16:12:37 +0000 | [diff] [blame] | 6001 | fprintf(fp, "\"%c", c); |
| 6002 | if (c == execreg_lastc) |
| 6003 | fprintf(fp, "@"); |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 6004 | fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6005 | |
| 6006 | /* If max_num_lines < 0, then we save ALL the lines in the register */ |
| 6007 | if (max_num_lines > 0 && num_lines > max_num_lines) |
| 6008 | num_lines = max_num_lines; |
| 6009 | for (j = 0; j < num_lines; j++) |
| 6010 | { |
| 6011 | putc('\t', fp); |
| 6012 | viminfo_writestring(fp, y_regs[i].y_array[j]); |
| 6013 | } |
| 6014 | } |
| 6015 | } |
| 6016 | #endif /* FEAT_VIMINFO */ |
| 6017 | |
| 6018 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 6019 | /* |
| 6020 | * SELECTION / PRIMARY ('*') |
| 6021 | * |
| 6022 | * Text selection stuff that uses the GUI selection register '*'. When using a |
| 6023 | * GUI this may be text from another window, otherwise it is the last text we |
| 6024 | * had highlighted with VIsual mode. With mouse support, clicking the middle |
| 6025 | * button performs the paste, otherwise you will need to do <"*p>. " |
| 6026 | * If not under X, it is synonymous with the clipboard register '+'. |
| 6027 | * |
| 6028 | * X CLIPBOARD ('+') |
| 6029 | * |
| 6030 | * Text selection stuff that uses the GUI clipboard register '+'. |
| 6031 | * Under X, this matches the standard cut/paste buffer CLIPBOARD selection. |
| 6032 | * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed", |
| 6033 | * otherwise you will need to do <"+p>. " |
| 6034 | * If not under X, it is synonymous with the selection register '*'. |
| 6035 | */ |
| 6036 | |
| 6037 | /* |
| 6038 | * Routine to export any final X selection we had to the environment |
| 6039 | * so that the text is still available after vim has exited. X selections |
| 6040 | * only exist while the owning application exists, so we write to the |
| 6041 | * permanent (while X runs) store CUT_BUFFER0. |
| 6042 | * Dump the CLIPBOARD selection if we own it (it's logically the more |
| 6043 | * 'permanent' of the two), otherwise the PRIMARY one. |
| 6044 | * For now, use a hard-coded sanity limit of 1Mb of data. |
| 6045 | */ |
| 6046 | #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD) |
| 6047 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6048 | x11_export_final_selection(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6049 | { |
| 6050 | Display *dpy; |
| 6051 | char_u *str = NULL; |
| 6052 | long_u len = 0; |
| 6053 | int motion_type = -1; |
| 6054 | |
| 6055 | # ifdef FEAT_GUI |
| 6056 | if (gui.in_use) |
| 6057 | dpy = X_DISPLAY; |
| 6058 | else |
| 6059 | # endif |
| 6060 | # ifdef FEAT_XCLIPBOARD |
| 6061 | dpy = xterm_dpy; |
| 6062 | # else |
| 6063 | return; |
| 6064 | # endif |
| 6065 | |
| 6066 | /* Get selection to export */ |
| 6067 | if (clip_plus.owned) |
| 6068 | motion_type = clip_convert_selection(&str, &len, &clip_plus); |
| 6069 | else if (clip_star.owned) |
| 6070 | motion_type = clip_convert_selection(&str, &len, &clip_star); |
| 6071 | |
| 6072 | /* Check it's OK */ |
| 6073 | if (dpy != NULL && str != NULL && motion_type >= 0 |
| 6074 | && len < 1024*1024 && len > 0) |
| 6075 | { |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6076 | #ifdef FEAT_MBYTE |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6077 | int ok = TRUE; |
| 6078 | |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6079 | /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from |
| 6080 | * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit |
| 6081 | * encoding conversion usually doesn't work, so keep the text as-is. |
| 6082 | */ |
| 6083 | if (has_mbyte) |
| 6084 | { |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6085 | vimconv_T vc; |
| 6086 | |
| 6087 | vc.vc_type = CONV_NONE; |
| 6088 | if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK) |
| 6089 | { |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 6090 | int intlen = len; |
| 6091 | char_u *conv_str; |
Bram Moolenaar | 331dafd | 2009-11-25 11:38:30 +0000 | [diff] [blame] | 6092 | |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6093 | vc.vc_fail = TRUE; |
Bram Moolenaar | 331dafd | 2009-11-25 11:38:30 +0000 | [diff] [blame] | 6094 | conv_str = string_convert(&vc, str, &intlen); |
| 6095 | len = intlen; |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6096 | if (conv_str != NULL) |
| 6097 | { |
| 6098 | vim_free(str); |
| 6099 | str = conv_str; |
| 6100 | } |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6101 | else |
| 6102 | { |
| 6103 | ok = FALSE; |
| 6104 | } |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6105 | convert_setup(&vc, NULL, NULL); |
| 6106 | } |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6107 | else |
| 6108 | { |
| 6109 | ok = FALSE; |
| 6110 | } |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6111 | } |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6112 | |
| 6113 | /* Do not store the string if conversion failed. Better to use any |
| 6114 | * other selection than garbled text. */ |
| 6115 | if (ok) |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 6116 | #endif |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 6117 | { |
| 6118 | XStoreBuffer(dpy, (char *)str, (int)len, 0); |
| 6119 | XFlush(dpy); |
| 6120 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
| 6123 | vim_free(str); |
| 6124 | } |
| 6125 | #endif |
| 6126 | |
| 6127 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6128 | clip_free_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6129 | { |
| 6130 | struct yankreg *y_ptr = y_current; |
| 6131 | |
| 6132 | if (cbd == &clip_plus) |
| 6133 | y_current = &y_regs[PLUS_REGISTER]; |
| 6134 | else |
| 6135 | y_current = &y_regs[STAR_REGISTER]; |
| 6136 | free_yank_all(); |
| 6137 | y_current->y_size = 0; |
| 6138 | y_current = y_ptr; |
| 6139 | } |
| 6140 | |
| 6141 | /* |
| 6142 | * Get the selected text and put it in the gui selection register '*' or '+'. |
| 6143 | */ |
| 6144 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6145 | clip_get_selection(VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6146 | { |
| 6147 | struct yankreg *old_y_previous, *old_y_current; |
| 6148 | pos_T old_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6149 | pos_T old_visual; |
| 6150 | int old_visual_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6151 | colnr_T old_curswant; |
| 6152 | int old_set_curswant; |
| 6153 | pos_T old_op_start, old_op_end; |
| 6154 | oparg_T oa; |
| 6155 | cmdarg_T ca; |
| 6156 | |
| 6157 | if (cbd->owned) |
| 6158 | { |
| 6159 | if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL) |
| 6160 | || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL)) |
| 6161 | return; |
| 6162 | |
| 6163 | /* Get the text between clip_star.start & clip_star.end */ |
| 6164 | old_y_previous = y_previous; |
| 6165 | old_y_current = y_current; |
| 6166 | old_cursor = curwin->w_cursor; |
| 6167 | old_curswant = curwin->w_curswant; |
| 6168 | old_set_curswant = curwin->w_set_curswant; |
| 6169 | old_op_start = curbuf->b_op_start; |
| 6170 | old_op_end = curbuf->b_op_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6171 | old_visual = VIsual; |
| 6172 | old_visual_mode = VIsual_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6173 | clear_oparg(&oa); |
| 6174 | oa.regname = (cbd == &clip_plus ? '+' : '*'); |
| 6175 | oa.op_type = OP_YANK; |
| 6176 | vim_memset(&ca, 0, sizeof(ca)); |
| 6177 | ca.oap = &oa; |
| 6178 | ca.cmdchar = 'y'; |
| 6179 | ca.count1 = 1; |
| 6180 | ca.retval = CA_NO_ADJ_OP_END; |
| 6181 | do_pending_operator(&ca, 0, TRUE); |
| 6182 | y_previous = old_y_previous; |
| 6183 | y_current = old_y_current; |
| 6184 | curwin->w_cursor = old_cursor; |
Bram Moolenaar | 5c8837f | 2006-02-25 21:52:33 +0000 | [diff] [blame] | 6185 | changed_cline_bef_curs(); /* need to update w_virtcol et al */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6186 | curwin->w_curswant = old_curswant; |
| 6187 | curwin->w_set_curswant = old_set_curswant; |
| 6188 | curbuf->b_op_start = old_op_start; |
| 6189 | curbuf->b_op_end = old_op_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6190 | VIsual = old_visual; |
| 6191 | VIsual_mode = old_visual_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6192 | } |
| 6193 | else |
| 6194 | { |
| 6195 | clip_free_selection(cbd); |
| 6196 | |
| 6197 | /* Try to get selected text from another window */ |
| 6198 | clip_gen_request_selection(cbd); |
| 6199 | } |
| 6200 | } |
| 6201 | |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 6202 | /* |
| 6203 | * Convert from the GUI selection string into the '*'/'+' register. |
| 6204 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6205 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6206 | clip_yank_selection( |
| 6207 | int type, |
| 6208 | char_u *str, |
| 6209 | long len, |
| 6210 | VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6211 | { |
| 6212 | struct yankreg *y_ptr; |
| 6213 | |
| 6214 | if (cbd == &clip_plus) |
| 6215 | y_ptr = &y_regs[PLUS_REGISTER]; |
| 6216 | else |
| 6217 | y_ptr = &y_regs[STAR_REGISTER]; |
| 6218 | |
| 6219 | clip_free_selection(cbd); |
| 6220 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6221 | str_to_reg(y_ptr, type, str, len, 0L, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | /* |
| 6225 | * Convert the '*'/'+' register into a GUI selection string returned in *str |
| 6226 | * with length *len. |
| 6227 | * Returns the motion type, or -1 for failure. |
| 6228 | */ |
| 6229 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6230 | clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6231 | { |
| 6232 | char_u *p; |
| 6233 | int lnum; |
| 6234 | int i, j; |
| 6235 | int_u eolsize; |
| 6236 | struct yankreg *y_ptr; |
| 6237 | |
| 6238 | if (cbd == &clip_plus) |
| 6239 | y_ptr = &y_regs[PLUS_REGISTER]; |
| 6240 | else |
| 6241 | y_ptr = &y_regs[STAR_REGISTER]; |
| 6242 | |
| 6243 | #ifdef USE_CRNL |
| 6244 | eolsize = 2; |
| 6245 | #else |
| 6246 | eolsize = 1; |
| 6247 | #endif |
| 6248 | |
| 6249 | *str = NULL; |
| 6250 | *len = 0; |
| 6251 | if (y_ptr->y_array == NULL) |
| 6252 | return -1; |
| 6253 | |
| 6254 | for (i = 0; i < y_ptr->y_size; i++) |
| 6255 | *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize; |
| 6256 | |
| 6257 | /* |
| 6258 | * Don't want newline character at end of last line if we're in MCHAR mode. |
| 6259 | */ |
| 6260 | if (y_ptr->y_type == MCHAR && *len >= eolsize) |
| 6261 | *len -= eolsize; |
| 6262 | |
| 6263 | p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */ |
| 6264 | if (p == NULL) |
| 6265 | return -1; |
| 6266 | lnum = 0; |
| 6267 | for (i = 0, j = 0; i < (int)*len; i++, j++) |
| 6268 | { |
| 6269 | if (y_ptr->y_array[lnum][j] == '\n') |
| 6270 | p[i] = NUL; |
| 6271 | else if (y_ptr->y_array[lnum][j] == NUL) |
| 6272 | { |
| 6273 | #ifdef USE_CRNL |
| 6274 | p[i++] = '\r'; |
| 6275 | #endif |
| 6276 | #ifdef USE_CR |
| 6277 | p[i] = '\r'; |
| 6278 | #else |
| 6279 | p[i] = '\n'; |
| 6280 | #endif |
| 6281 | lnum++; |
| 6282 | j = -1; |
| 6283 | } |
| 6284 | else |
| 6285 | p[i] = y_ptr->y_array[lnum][j]; |
| 6286 | } |
| 6287 | return y_ptr->y_type; |
| 6288 | } |
| 6289 | |
| 6290 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6291 | /* |
| 6292 | * If we have written to a clipboard register, send the text to the clipboard. |
| 6293 | */ |
| 6294 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6295 | may_set_selection(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6296 | { |
| 6297 | if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available) |
| 6298 | { |
| 6299 | clip_own_selection(&clip_star); |
| 6300 | clip_gen_set_selection(&clip_star); |
| 6301 | } |
| 6302 | else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available) |
| 6303 | { |
| 6304 | clip_own_selection(&clip_plus); |
| 6305 | clip_gen_set_selection(&clip_plus); |
| 6306 | } |
| 6307 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6308 | |
| 6309 | #endif /* FEAT_CLIPBOARD || PROTO */ |
| 6310 | |
| 6311 | |
| 6312 | #if defined(FEAT_DND) || defined(PROTO) |
| 6313 | /* |
| 6314 | * Replace the contents of the '~' register with str. |
| 6315 | */ |
| 6316 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6317 | dnd_yank_drag_data(char_u *str, long len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6318 | { |
| 6319 | struct yankreg *curr; |
| 6320 | |
| 6321 | curr = y_current; |
| 6322 | y_current = &y_regs[TILDE_REGISTER]; |
| 6323 | free_yank_all(); |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6324 | str_to_reg(y_current, MCHAR, str, len, 0L, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6325 | y_current = curr; |
| 6326 | } |
| 6327 | #endif |
| 6328 | |
| 6329 | |
| 6330 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6331 | /* |
| 6332 | * Return the type of a register. |
| 6333 | * Used for getregtype() |
| 6334 | * Returns MAUTO for error. |
| 6335 | */ |
| 6336 | char_u |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6337 | get_reg_type(int regname, long *reglen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6338 | { |
| 6339 | switch (regname) |
| 6340 | { |
| 6341 | case '%': /* file name */ |
| 6342 | case '#': /* alternate file name */ |
| 6343 | case '=': /* expression */ |
| 6344 | case ':': /* last command line */ |
| 6345 | case '/': /* last search-pattern */ |
| 6346 | case '.': /* last inserted text */ |
| 6347 | #ifdef FEAT_SEARCHPATH |
| 6348 | case Ctrl_F: /* Filename under cursor */ |
| 6349 | case Ctrl_P: /* Path under cursor, expand via "path" */ |
| 6350 | #endif |
| 6351 | case Ctrl_W: /* word under cursor */ |
| 6352 | case Ctrl_A: /* WORD (mnemonic All) under cursor */ |
| 6353 | case '_': /* black hole: always empty */ |
| 6354 | return MCHAR; |
| 6355 | } |
| 6356 | |
| 6357 | #ifdef FEAT_CLIPBOARD |
| 6358 | regname = may_get_selection(regname); |
| 6359 | #endif |
| 6360 | |
Bram Moolenaar | 32b9201 | 2014-01-14 12:33:36 +0100 | [diff] [blame] | 6361 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6362 | return MAUTO; |
Bram Moolenaar | 32b9201 | 2014-01-14 12:33:36 +0100 | [diff] [blame] | 6363 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6364 | get_yank_register(regname, FALSE); |
| 6365 | |
| 6366 | if (y_current->y_array != NULL) |
| 6367 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6368 | if (reglen != NULL && y_current->y_type == MBLOCK) |
| 6369 | *reglen = y_current->y_width; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6370 | return y_current->y_type; |
| 6371 | } |
| 6372 | return MAUTO; |
| 6373 | } |
| 6374 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 6375 | static char_u *getreg_wrap_one_line(char_u *s, int flags); |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6376 | |
| 6377 | /* |
| 6378 | * When "flags" has GREG_LIST return a list with text "s". |
| 6379 | * Otherwise just return "s". |
| 6380 | */ |
| 6381 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6382 | getreg_wrap_one_line(char_u *s, int flags) |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6383 | { |
| 6384 | if (flags & GREG_LIST) |
| 6385 | { |
| 6386 | list_T *list = list_alloc(); |
| 6387 | |
| 6388 | if (list != NULL) |
| 6389 | { |
| 6390 | if (list_append_string(list, NULL, -1) == FAIL) |
| 6391 | { |
| 6392 | list_free(list, TRUE); |
| 6393 | return NULL; |
| 6394 | } |
| 6395 | list->lv_first->li_tv.vval.v_string = s; |
| 6396 | } |
| 6397 | return (char_u *)list; |
| 6398 | } |
| 6399 | return s; |
| 6400 | } |
| 6401 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6402 | /* |
| 6403 | * Return the contents of a register as a single allocated string. |
| 6404 | * Used for "@r" in expressions and for getreg(). |
| 6405 | * Returns NULL for error. |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6406 | * Flags: |
| 6407 | * GREG_NO_EXPR Do not allow expression register |
| 6408 | * GREG_EXPR_SRC For the expression register: return expression itself, |
| 6409 | * not the result of its evaluation. |
| 6410 | * 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] | 6411 | */ |
| 6412 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6413 | get_reg_contents(int regname, int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6414 | { |
| 6415 | long i; |
| 6416 | char_u *retval; |
| 6417 | int allocated; |
| 6418 | long len; |
| 6419 | |
| 6420 | /* Don't allow using an expression register inside an expression */ |
| 6421 | if (regname == '=') |
| 6422 | { |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6423 | if (flags & GREG_NO_EXPR) |
| 6424 | return NULL; |
| 6425 | if (flags & GREG_EXPR_SRC) |
| 6426 | return getreg_wrap_one_line(get_expr_line_src(), flags); |
| 6427 | return getreg_wrap_one_line(get_expr_line(), flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | if (regname == '@') /* "@@" is used for unnamed register */ |
| 6431 | regname = '"'; |
| 6432 | |
| 6433 | /* check for valid regname */ |
| 6434 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
| 6435 | return NULL; |
| 6436 | |
| 6437 | #ifdef FEAT_CLIPBOARD |
| 6438 | regname = may_get_selection(regname); |
| 6439 | #endif |
| 6440 | |
| 6441 | if (get_spec_reg(regname, &retval, &allocated, FALSE)) |
| 6442 | { |
| 6443 | if (retval == NULL) |
| 6444 | return NULL; |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6445 | if (allocated) |
| 6446 | return getreg_wrap_one_line(retval, flags); |
| 6447 | return getreg_wrap_one_line(vim_strsave(retval), flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6448 | } |
| 6449 | |
| 6450 | get_yank_register(regname, FALSE); |
| 6451 | if (y_current->y_array == NULL) |
| 6452 | return NULL; |
| 6453 | |
Bram Moolenaar | b7cb42b | 2014-04-02 19:55:10 +0200 | [diff] [blame] | 6454 | if (flags & GREG_LIST) |
| 6455 | { |
| 6456 | list_T *list = list_alloc(); |
| 6457 | int error = FALSE; |
| 6458 | |
| 6459 | if (list == NULL) |
| 6460 | return NULL; |
| 6461 | for (i = 0; i < y_current->y_size; ++i) |
| 6462 | if (list_append_string(list, y_current->y_array[i], -1) == FAIL) |
| 6463 | error = TRUE; |
| 6464 | if (error) |
| 6465 | { |
| 6466 | list_free(list, TRUE); |
| 6467 | return NULL; |
| 6468 | } |
| 6469 | return (char_u *)list; |
| 6470 | } |
| 6471 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6472 | /* |
| 6473 | * Compute length of resulting string. |
| 6474 | */ |
| 6475 | len = 0; |
| 6476 | for (i = 0; i < y_current->y_size; ++i) |
| 6477 | { |
| 6478 | len += (long)STRLEN(y_current->y_array[i]); |
| 6479 | /* |
| 6480 | * Insert a newline between lines and after last line if |
| 6481 | * y_type is MLINE. |
| 6482 | */ |
| 6483 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 6484 | ++len; |
| 6485 | } |
| 6486 | |
| 6487 | retval = lalloc(len + 1, TRUE); |
| 6488 | |
| 6489 | /* |
| 6490 | * Copy the lines of the yank register into the string. |
| 6491 | */ |
| 6492 | if (retval != NULL) |
| 6493 | { |
| 6494 | len = 0; |
| 6495 | for (i = 0; i < y_current->y_size; ++i) |
| 6496 | { |
| 6497 | STRCPY(retval + len, y_current->y_array[i]); |
| 6498 | len += (long)STRLEN(retval + len); |
| 6499 | |
| 6500 | /* |
| 6501 | * Insert a NL between lines and after the last line if y_type is |
| 6502 | * MLINE. |
| 6503 | */ |
| 6504 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 6505 | retval[len++] = '\n'; |
| 6506 | } |
| 6507 | retval[len] = NUL; |
| 6508 | } |
| 6509 | |
| 6510 | return retval; |
| 6511 | } |
| 6512 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6513 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6514 | init_write_reg( |
| 6515 | int name, |
| 6516 | struct yankreg **old_y_previous, |
| 6517 | struct yankreg **old_y_current, |
| 6518 | int must_append, |
| 6519 | int *yank_type UNUSED) |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6520 | { |
| 6521 | if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */ |
| 6522 | { |
| 6523 | emsg_invreg(name); |
| 6524 | return FAIL; |
| 6525 | } |
| 6526 | |
| 6527 | /* Don't want to change the current (unnamed) register */ |
| 6528 | *old_y_previous = y_previous; |
| 6529 | *old_y_current = y_current; |
| 6530 | |
| 6531 | get_yank_register(name, TRUE); |
| 6532 | if (!y_append && !must_append) |
| 6533 | free_yank_all(); |
| 6534 | return OK; |
| 6535 | } |
| 6536 | |
| 6537 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6538 | finish_write_reg( |
| 6539 | int name, |
| 6540 | struct yankreg *old_y_previous, |
| 6541 | struct yankreg *old_y_current) |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6542 | { |
| 6543 | # ifdef FEAT_CLIPBOARD |
| 6544 | /* Send text of clipboard register to the clipboard. */ |
| 6545 | may_set_selection(); |
| 6546 | # endif |
| 6547 | |
| 6548 | /* ':let @" = "val"' should change the meaning of the "" register */ |
| 6549 | if (name != '"') |
| 6550 | y_previous = old_y_previous; |
| 6551 | y_current = old_y_current; |
| 6552 | } |
| 6553 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6554 | /* |
| 6555 | * Store string "str" in register "name". |
| 6556 | * "maxlen" is the maximum number of bytes to use, -1 for all bytes. |
| 6557 | * If "must_append" is TRUE, always append to the register. Otherwise append |
| 6558 | * if "name" is an uppercase letter. |
| 6559 | * Note: "maxlen" and "must_append" don't work for the "/" register. |
| 6560 | * Careful: 'str' is modified, you may have to use a copy! |
| 6561 | * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise. |
| 6562 | */ |
| 6563 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6564 | write_reg_contents( |
| 6565 | int name, |
| 6566 | char_u *str, |
| 6567 | int maxlen, |
| 6568 | int must_append) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6569 | { |
| 6570 | write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L); |
| 6571 | } |
| 6572 | |
| 6573 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6574 | write_reg_contents_lst( |
| 6575 | int name, |
| 6576 | char_u **strings, |
| 6577 | int maxlen UNUSED, |
| 6578 | int must_append, |
| 6579 | int yank_type, |
| 6580 | long block_len) |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6581 | { |
| 6582 | struct yankreg *old_y_previous, *old_y_current; |
| 6583 | |
| 6584 | if (name == '/' |
| 6585 | #ifdef FEAT_EVAL |
| 6586 | || name == '=' |
| 6587 | #endif |
| 6588 | ) |
| 6589 | { |
| 6590 | char_u *s; |
| 6591 | |
| 6592 | if (strings[0] == NULL) |
| 6593 | s = (char_u *)""; |
| 6594 | else if (strings[1] != NULL) |
| 6595 | { |
| 6596 | EMSG(_("E883: search pattern and expression register may not " |
| 6597 | "contain two or more lines")); |
| 6598 | return; |
| 6599 | } |
| 6600 | else |
| 6601 | s = strings[0]; |
| 6602 | write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len); |
| 6603 | return; |
| 6604 | } |
| 6605 | |
| 6606 | if (name == '_') /* black hole: nothing to do */ |
| 6607 | return; |
| 6608 | |
| 6609 | if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, |
| 6610 | &yank_type) == FAIL) |
| 6611 | return; |
| 6612 | |
| 6613 | str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE); |
| 6614 | |
| 6615 | finish_write_reg(name, old_y_previous, old_y_current); |
| 6616 | } |
| 6617 | |
| 6618 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6619 | write_reg_contents_ex( |
| 6620 | int name, |
| 6621 | char_u *str, |
| 6622 | int maxlen, |
| 6623 | int must_append, |
| 6624 | int yank_type, |
| 6625 | long block_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6626 | { |
| 6627 | struct yankreg *old_y_previous, *old_y_current; |
| 6628 | long len; |
| 6629 | |
Bram Moolenaar | e756604 | 2005-06-17 22:00:15 +0000 | [diff] [blame] | 6630 | if (maxlen >= 0) |
| 6631 | len = maxlen; |
| 6632 | else |
| 6633 | len = (long)STRLEN(str); |
| 6634 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6635 | /* Special case: '/' search pattern */ |
| 6636 | if (name == '/') |
| 6637 | { |
| 6638 | set_last_search_pat(str, RE_SEARCH, TRUE, TRUE); |
| 6639 | return; |
| 6640 | } |
| 6641 | |
Bram Moolenaar | 3b3a949 | 2015-01-27 18:44:16 +0100 | [diff] [blame] | 6642 | if (name == '#') |
| 6643 | { |
| 6644 | buf_T *buf; |
| 6645 | |
| 6646 | if (VIM_ISDIGIT(*str)) |
| 6647 | { |
| 6648 | int num = atoi((char *)str); |
| 6649 | |
| 6650 | buf = buflist_findnr(num); |
| 6651 | if (buf == NULL) |
| 6652 | EMSGN(_(e_nobufnr), (long)num); |
| 6653 | } |
| 6654 | else |
| 6655 | buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str), |
| 6656 | TRUE, FALSE, FALSE)); |
| 6657 | if (buf == NULL) |
| 6658 | return; |
| 6659 | curwin->w_alt_fnum = buf->b_fnum; |
| 6660 | return; |
| 6661 | } |
| 6662 | |
Bram Moolenaar | e756604 | 2005-06-17 22:00:15 +0000 | [diff] [blame] | 6663 | #ifdef FEAT_EVAL |
| 6664 | if (name == '=') |
| 6665 | { |
| 6666 | char_u *p, *s; |
| 6667 | |
| 6668 | p = vim_strnsave(str, (int)len); |
| 6669 | if (p == NULL) |
| 6670 | return; |
| 6671 | if (must_append) |
| 6672 | { |
| 6673 | s = concat_str(get_expr_line_src(), p); |
| 6674 | vim_free(p); |
| 6675 | p = s; |
Bram Moolenaar | e756604 | 2005-06-17 22:00:15 +0000 | [diff] [blame] | 6676 | } |
| 6677 | set_expr_line(p); |
| 6678 | return; |
| 6679 | } |
| 6680 | #endif |
| 6681 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6682 | if (name == '_') /* black hole: nothing to do */ |
| 6683 | return; |
| 6684 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6685 | if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, |
| 6686 | &yank_type) == FAIL) |
| 6687 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6688 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6689 | str_to_reg(y_current, yank_type, str, len, block_len, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6690 | |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6691 | finish_write_reg(name, old_y_previous, old_y_current); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6692 | } |
| 6693 | #endif /* FEAT_EVAL */ |
| 6694 | |
| 6695 | #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) |
| 6696 | /* |
| 6697 | * Put a string into a register. When the register is not empty, the string |
| 6698 | * is appended. |
| 6699 | */ |
| 6700 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6701 | str_to_reg( |
| 6702 | struct yankreg *y_ptr, /* pointer to yank register */ |
| 6703 | int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */ |
| 6704 | char_u *str, /* string to put in register */ |
| 6705 | long len, /* length of string */ |
| 6706 | long blocklen, /* width of Visual block */ |
| 6707 | int str_list) /* TRUE if str is char_u ** */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6708 | { |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 6709 | int type; /* MCHAR, MLINE or MBLOCK */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6710 | int lnum; |
| 6711 | long start; |
| 6712 | long i; |
| 6713 | int extra; |
| 6714 | int newlines; /* number of lines added */ |
| 6715 | int extraline = 0; /* extra line at the end */ |
| 6716 | int append = FALSE; /* append to last line in register */ |
| 6717 | char_u *s; |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6718 | char_u **ss; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6719 | char_u **pp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6720 | long maxlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6721 | |
Bram Moolenaar | cde547a | 2009-11-17 11:43:06 +0000 | [diff] [blame] | 6722 | if (y_ptr->y_array == NULL) /* NULL means empty register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6723 | y_ptr->y_size = 0; |
| 6724 | |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 6725 | if (yank_type == MAUTO) |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6726 | type = ((str_list || (len > 0 && (str[len - 1] == NL |
| 6727 | || str[len - 1] == CAR))) |
Bram Moolenaar | d44347f | 2011-06-19 01:14:29 +0200 | [diff] [blame] | 6728 | ? MLINE : MCHAR); |
| 6729 | else |
| 6730 | type = yank_type; |
| 6731 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6732 | /* |
| 6733 | * Count the number of lines within the string |
| 6734 | */ |
| 6735 | newlines = 0; |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6736 | if (str_list) |
| 6737 | { |
| 6738 | for (ss = (char_u **) str; *ss != NULL; ++ss) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6739 | ++newlines; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6740 | } |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6741 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6742 | { |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6743 | for (i = 0; i < len; i++) |
| 6744 | if (str[i] == '\n') |
| 6745 | ++newlines; |
| 6746 | if (type == MCHAR || len == 0 || str[len - 1] != '\n') |
| 6747 | { |
| 6748 | extraline = 1; |
| 6749 | ++newlines; /* count extra newline at the end */ |
| 6750 | } |
| 6751 | if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR) |
| 6752 | { |
| 6753 | append = TRUE; |
| 6754 | --newlines; /* uncount newline when appending first line */ |
| 6755 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6756 | } |
| 6757 | |
Bram Moolenaar | 659c94d | 2015-05-04 20:19:21 +0200 | [diff] [blame] | 6758 | /* Without any lines make the register empty. */ |
| 6759 | if (y_ptr->y_size + newlines == 0) |
| 6760 | { |
| 6761 | vim_free(y_ptr->y_array); |
| 6762 | y_ptr->y_array = NULL; |
| 6763 | return; |
| 6764 | } |
| 6765 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6766 | /* |
| 6767 | * Allocate an array to hold the pointers to the new register lines. |
| 6768 | * If the register was not empty, move the existing lines to the new array. |
| 6769 | */ |
| 6770 | pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines) |
| 6771 | * sizeof(char_u *), TRUE); |
| 6772 | if (pp == NULL) /* out of memory */ |
| 6773 | return; |
| 6774 | for (lnum = 0; lnum < y_ptr->y_size; ++lnum) |
| 6775 | pp[lnum] = y_ptr->y_array[lnum]; |
| 6776 | vim_free(y_ptr->y_array); |
| 6777 | y_ptr->y_array = pp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6778 | maxlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6779 | |
| 6780 | /* |
| 6781 | * Find the end of each line and save it into the array. |
| 6782 | */ |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6783 | if (str_list) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6784 | { |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6785 | for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum) |
| 6786 | { |
Bram Moolenaar | 121f9bd | 2014-04-29 15:55:43 +0200 | [diff] [blame] | 6787 | i = (long)STRLEN(*ss); |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6788 | pp[lnum] = vim_strnsave(*ss, i); |
| 6789 | if (i > maxlen) |
| 6790 | maxlen = i; |
| 6791 | } |
| 6792 | } |
| 6793 | else |
| 6794 | { |
| 6795 | for (start = 0; start < len + extraline; start += i + 1) |
| 6796 | { |
| 6797 | for (i = start; i < len; ++i) /* find the end of the line */ |
| 6798 | if (str[i] == '\n') |
| 6799 | break; |
| 6800 | i -= start; /* i is now length of line */ |
| 6801 | if (i > maxlen) |
| 6802 | maxlen = i; |
| 6803 | if (append) |
| 6804 | { |
| 6805 | --lnum; |
| 6806 | extra = (int)STRLEN(y_ptr->y_array[lnum]); |
| 6807 | } |
| 6808 | else |
| 6809 | extra = 0; |
| 6810 | s = alloc((unsigned)(i + extra + 1)); |
| 6811 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6812 | break; |
Bram Moolenaar | 5a50c22 | 2014-04-02 22:17:10 +0200 | [diff] [blame] | 6813 | if (extra) |
| 6814 | mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra); |
| 6815 | if (append) |
| 6816 | vim_free(y_ptr->y_array[lnum]); |
| 6817 | if (i) |
| 6818 | mch_memmove(s + extra, str + start, (size_t)i); |
| 6819 | extra += i; |
| 6820 | s[extra] = NUL; |
| 6821 | y_ptr->y_array[lnum++] = s; |
| 6822 | while (--extra >= 0) |
| 6823 | { |
| 6824 | if (*s == NUL) |
| 6825 | *s = '\n'; /* replace NUL with newline */ |
| 6826 | ++s; |
| 6827 | } |
| 6828 | append = FALSE; /* only first line is appended */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6829 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6830 | } |
| 6831 | y_ptr->y_type = type; |
| 6832 | y_ptr->y_size = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6833 | if (type == MBLOCK) |
| 6834 | y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen); |
| 6835 | else |
| 6836 | y_ptr->y_width = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6837 | } |
| 6838 | #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */ |
| 6839 | |
| 6840 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6841 | clear_oparg(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6842 | { |
| 6843 | vim_memset(oap, 0, sizeof(oparg_T)); |
| 6844 | } |
| 6845 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 6846 | static long line_count_info(char_u *line, long *wc, long *cc, long limit, int eol_size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6847 | |
| 6848 | /* |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6849 | * Count the number of bytes, characters and "words" in a line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6850 | * |
| 6851 | * "Words" are counted by looking for boundaries between non-space and |
| 6852 | * space characters. (it seems to produce results that match 'wc'.) |
| 6853 | * |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6854 | * Return value is byte count; word count for the line is added to "*wc". |
| 6855 | * Char count is added to "*cc". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6856 | * |
| 6857 | * The function will only examine the first "limit" characters in the |
| 6858 | * line, stopping if it encounters an end-of-line (NUL byte). In that |
| 6859 | * case, eol_size will be added to the character count to account for |
| 6860 | * the size of the EOL character. |
| 6861 | */ |
| 6862 | static long |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6863 | line_count_info( |
| 6864 | char_u *line, |
| 6865 | long *wc, |
| 6866 | long *cc, |
| 6867 | long limit, |
| 6868 | int eol_size) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6869 | { |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6870 | long i; |
| 6871 | long words = 0; |
| 6872 | long chars = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6873 | int is_word = 0; |
| 6874 | |
Bram Moolenaar | 88b1ba1 | 2012-06-29 13:34:19 +0200 | [diff] [blame] | 6875 | for (i = 0; i < limit && line[i] != NUL; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6876 | { |
| 6877 | if (is_word) |
| 6878 | { |
| 6879 | if (vim_isspace(line[i])) |
| 6880 | { |
| 6881 | words++; |
| 6882 | is_word = 0; |
| 6883 | } |
| 6884 | } |
| 6885 | else if (!vim_isspace(line[i])) |
| 6886 | is_word = 1; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6887 | ++chars; |
| 6888 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6889 | i += (*mb_ptr2len)(line + i); |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6890 | #else |
| 6891 | ++i; |
| 6892 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | if (is_word) |
| 6896 | words++; |
| 6897 | *wc += words; |
| 6898 | |
| 6899 | /* 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] | 6900 | if (i < limit && line[i] == NUL) |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6901 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6902 | i += eol_size; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6903 | chars += eol_size; |
| 6904 | } |
| 6905 | *cc += chars; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6906 | return i; |
| 6907 | } |
| 6908 | |
| 6909 | /* |
| 6910 | * Give some info about the position of the cursor (for "g CTRL-G"). |
| 6911 | * In Visual mode, give some info about the selected region. (In this case, |
| 6912 | * the *_count_cursor variables store running totals for the selection.) |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 6913 | * 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] | 6914 | */ |
| 6915 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6916 | cursor_pos_info(dict_T *dict) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6917 | { |
| 6918 | char_u *p; |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 6919 | char_u buf1[50]; |
| 6920 | char_u buf2[40]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6921 | linenr_T lnum; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6922 | long byte_count = 0; |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 6923 | #ifdef FEAT_MBYTE |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 6924 | long bom_count = 0; |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 6925 | #endif |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6926 | long byte_count_cursor = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6927 | long char_count = 0; |
| 6928 | long char_count_cursor = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6929 | long word_count = 0; |
| 6930 | long word_count_cursor = 0; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6931 | int eol_size; |
| 6932 | long last_check = 100000L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6933 | long line_count_selected = 0; |
| 6934 | pos_T min_pos, max_pos; |
| 6935 | oparg_T oparg; |
| 6936 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6937 | |
| 6938 | /* |
| 6939 | * Compute the length of the file in characters. |
| 6940 | */ |
| 6941 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 6942 | { |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 6943 | if (dict == NULL) |
| 6944 | { |
| 6945 | MSG(_(no_lines_msg)); |
| 6946 | return; |
| 6947 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6948 | } |
| 6949 | else |
| 6950 | { |
| 6951 | if (get_fileformat(curbuf) == EOL_DOS) |
| 6952 | eol_size = 2; |
| 6953 | else |
| 6954 | eol_size = 1; |
| 6955 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6956 | if (VIsual_active) |
| 6957 | { |
| 6958 | if (lt(VIsual, curwin->w_cursor)) |
| 6959 | { |
| 6960 | min_pos = VIsual; |
| 6961 | max_pos = curwin->w_cursor; |
| 6962 | } |
| 6963 | else |
| 6964 | { |
| 6965 | min_pos = curwin->w_cursor; |
| 6966 | max_pos = VIsual; |
| 6967 | } |
| 6968 | if (*p_sel == 'e' && max_pos.col > 0) |
| 6969 | --max_pos.col; |
| 6970 | |
| 6971 | if (VIsual_mode == Ctrl_V) |
| 6972 | { |
Bram Moolenaar | 81d0007 | 2009-04-29 15:41:40 +0000 | [diff] [blame] | 6973 | #ifdef FEAT_LINEBREAK |
| 6974 | char_u * saved_sbr = p_sbr; |
| 6975 | |
| 6976 | /* Make 'sbr' empty for a moment to get the correct size. */ |
| 6977 | p_sbr = empty_option; |
| 6978 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6979 | oparg.is_VIsual = 1; |
| 6980 | oparg.block_mode = TRUE; |
| 6981 | oparg.op_type = OP_NOP; |
| 6982 | getvcols(curwin, &min_pos, &max_pos, |
Bram Moolenaar | 5c8837f | 2006-02-25 21:52:33 +0000 | [diff] [blame] | 6983 | &oparg.start_vcol, &oparg.end_vcol); |
Bram Moolenaar | 81d0007 | 2009-04-29 15:41:40 +0000 | [diff] [blame] | 6984 | #ifdef FEAT_LINEBREAK |
| 6985 | p_sbr = saved_sbr; |
| 6986 | #endif |
Bram Moolenaar | 5c8837f | 2006-02-25 21:52:33 +0000 | [diff] [blame] | 6987 | if (curwin->w_curswant == MAXCOL) |
| 6988 | oparg.end_vcol = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6989 | /* Swap the start, end vcol if needed */ |
| 6990 | if (oparg.end_vcol < oparg.start_vcol) |
| 6991 | { |
| 6992 | oparg.end_vcol += oparg.start_vcol; |
| 6993 | oparg.start_vcol = oparg.end_vcol - oparg.start_vcol; |
| 6994 | oparg.end_vcol -= oparg.start_vcol; |
| 6995 | } |
| 6996 | } |
| 6997 | line_count_selected = max_pos.lnum - min_pos.lnum + 1; |
| 6998 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6999 | |
| 7000 | for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) |
| 7001 | { |
| 7002 | /* Check for a CTRL-C every 100000 characters. */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7003 | if (byte_count > last_check) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7004 | { |
| 7005 | ui_breakcheck(); |
| 7006 | if (got_int) |
| 7007 | return; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7008 | last_check = byte_count + 100000L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7009 | } |
| 7010 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7011 | /* Do extra processing for VIsual mode. */ |
| 7012 | if (VIsual_active |
| 7013 | && lnum >= min_pos.lnum && lnum <= max_pos.lnum) |
| 7014 | { |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7015 | char_u *s = NULL; |
| 7016 | long len = 0L; |
| 7017 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7018 | switch (VIsual_mode) |
| 7019 | { |
| 7020 | case Ctrl_V: |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7021 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7022 | virtual_op = virtual_active(); |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7023 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7024 | block_prep(&oparg, &bd, lnum, 0); |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7025 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7026 | virtual_op = MAYBE; |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7027 | #endif |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7028 | s = bd.textstart; |
| 7029 | len = (long)bd.textlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7030 | break; |
| 7031 | case 'V': |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7032 | s = ml_get(lnum); |
| 7033 | len = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7034 | break; |
| 7035 | case 'v': |
| 7036 | { |
| 7037 | colnr_T start_col = (lnum == min_pos.lnum) |
| 7038 | ? min_pos.col : 0; |
| 7039 | colnr_T end_col = (lnum == max_pos.lnum) |
| 7040 | ? max_pos.col - start_col + 1 : MAXCOL; |
| 7041 | |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7042 | s = ml_get(lnum) + start_col; |
| 7043 | len = end_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7044 | } |
| 7045 | break; |
| 7046 | } |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7047 | if (s != NULL) |
| 7048 | { |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7049 | byte_count_cursor += line_count_info(s, &word_count_cursor, |
| 7050 | &char_count_cursor, len, eol_size); |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7051 | if (lnum == curbuf->b_ml.ml_line_count |
| 7052 | && !curbuf->b_p_eol |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 7053 | && (curbuf->b_p_bin || !curbuf->b_p_fixeol) |
Bram Moolenaar | ec2dad6 | 2005-01-02 11:36:03 +0000 | [diff] [blame] | 7054 | && (long)STRLEN(s) < len) |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7055 | byte_count_cursor -= eol_size; |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 7056 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7057 | } |
| 7058 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7059 | { |
| 7060 | /* In non-visual mode, check for the line the cursor is on */ |
| 7061 | if (lnum == curwin->w_cursor.lnum) |
| 7062 | { |
| 7063 | word_count_cursor += word_count; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7064 | char_count_cursor += char_count; |
| 7065 | byte_count_cursor = byte_count + |
| 7066 | line_count_info(ml_get(lnum), |
| 7067 | &word_count_cursor, &char_count_cursor, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7068 | (long)(curwin->w_cursor.col + 1), eol_size); |
| 7069 | } |
| 7070 | } |
| 7071 | /* Add to the running totals */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7072 | byte_count += line_count_info(ml_get(lnum), &word_count, |
| 7073 | &char_count, (long)MAXCOL, eol_size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7074 | } |
| 7075 | |
| 7076 | /* Correction for when last line doesn't have an EOL. */ |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 7077 | 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] | 7078 | byte_count -= eol_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7079 | |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7080 | if (dict == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7081 | { |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7082 | if (VIsual_active) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7083 | { |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7084 | if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL) |
| 7085 | { |
| 7086 | getvcols(curwin, &min_pos, &max_pos, &min_pos.col, |
| 7087 | &max_pos.col); |
| 7088 | vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "), |
| 7089 | (long)(oparg.end_vcol - oparg.start_vcol + 1)); |
| 7090 | } |
| 7091 | else |
| 7092 | buf1[0] = NUL; |
| 7093 | |
| 7094 | if (char_count_cursor == byte_count_cursor |
| 7095 | && char_count == byte_count) |
| 7096 | vim_snprintf((char *)IObuff, IOSIZE, |
| 7097 | _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"), |
| 7098 | buf1, line_count_selected, |
| 7099 | (long)curbuf->b_ml.ml_line_count, |
| 7100 | word_count_cursor, word_count, |
| 7101 | byte_count_cursor, byte_count); |
| 7102 | else |
| 7103 | vim_snprintf((char *)IObuff, IOSIZE, |
| 7104 | _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"), |
| 7105 | buf1, line_count_selected, |
| 7106 | (long)curbuf->b_ml.ml_line_count, |
| 7107 | word_count_cursor, word_count, |
| 7108 | char_count_cursor, char_count, |
| 7109 | byte_count_cursor, byte_count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7110 | } |
| 7111 | else |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7112 | { |
| 7113 | p = ml_get_curline(); |
| 7114 | validate_virtcol(); |
| 7115 | col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1, |
| 7116 | (int)curwin->w_virtcol + 1); |
| 7117 | col_print(buf2, sizeof(buf2), (int)STRLEN(p), |
| 7118 | linetabsize(p)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7119 | |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7120 | if (char_count_cursor == byte_count_cursor |
| 7121 | && char_count == byte_count) |
| 7122 | vim_snprintf((char *)IObuff, IOSIZE, |
| 7123 | _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"), |
| 7124 | (char *)buf1, (char *)buf2, |
| 7125 | (long)curwin->w_cursor.lnum, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7126 | (long)curbuf->b_ml.ml_line_count, |
| 7127 | word_count_cursor, word_count, |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7128 | byte_count_cursor, byte_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7129 | else |
| 7130 | vim_snprintf((char *)IObuff, IOSIZE, |
| 7131 | _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"), |
| 7132 | (char *)buf1, (char *)buf2, |
| 7133 | (long)curwin->w_cursor.lnum, |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7134 | (long)curbuf->b_ml.ml_line_count, |
| 7135 | word_count_cursor, word_count, |
| 7136 | char_count_cursor, char_count, |
| 7137 | byte_count_cursor, byte_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7138 | } |
| 7139 | } |
| 7140 | |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7141 | #ifdef FEAT_MBYTE |
| 7142 | bom_count = bomb_size(); |
| 7143 | if (bom_count > 0) |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7144 | vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE, |
| 7145 | _("(+%ld for BOM)"), bom_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7146 | #endif |
| 7147 | if (dict == NULL) |
| 7148 | { |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7149 | /* Don't shorten this message, the user asked for it. */ |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7150 | p = p_shm; |
| 7151 | p_shm = (char_u *)""; |
| 7152 | msg(IObuff); |
| 7153 | p_shm = p; |
| 7154 | } |
| 7155 | } |
Bram Moolenaar | 718272a | 2016-01-03 22:56:45 +0100 | [diff] [blame] | 7156 | #if defined(FEAT_EVAL) |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 7157 | if (dict != NULL) |
| 7158 | { |
| 7159 | dict_add_nr_str(dict, "words", (long)word_count, NULL); |
| 7160 | dict_add_nr_str(dict, "chars", (long)char_count, NULL); |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 7161 | dict_add_nr_str(dict, "bytes", (long)byte_count |
| 7162 | # ifdef FEAT_MBYTE |
| 7163 | + bom_count |
| 7164 | # endif |
| 7165 | , NULL); |
| 7166 | dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes", |
| 7167 | (long)byte_count_cursor, NULL); |
| 7168 | dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars", |
| 7169 | (long)char_count_cursor, NULL); |
| 7170 | dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words", |
| 7171 | (long)word_count_cursor, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7172 | } |
Bram Moolenaar | 718272a | 2016-01-03 22:56:45 +0100 | [diff] [blame] | 7173 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7174 | } |