Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * ops.c: implementation of various operators: op_shift, op_delete, op_tilde, |
| 12 | * op_change, op_yank, do_put, do_join |
| 13 | */ |
| 14 | |
| 15 | #include "vim.h" |
| 16 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 17 | static void shift_block(oparg_T *oap, int amount); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 18 | static void mb_adjust_opend(oparg_T *oap); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 19 | static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 20 | static int ends_in_white(linenr_T lnum); |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 21 | 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] | 22 | |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 23 | // Flags for third item in "opchars". |
| 24 | #define OPF_LINES 1 // operator always works on lines |
| 25 | #define OPF_CHANGE 2 // operator changes text |
| 26 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | /* |
| 28 | * The names of operators. |
| 29 | * IMPORTANT: Index must correspond with defines in vim.h!!! |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 30 | * The third field holds OPF_ flags. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 31 | */ |
| 32 | static char opchars[][3] = |
| 33 | { |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 34 | {NUL, NUL, 0}, // OP_NOP |
| 35 | {'d', NUL, OPF_CHANGE}, // OP_DELETE |
| 36 | {'y', NUL, 0}, // OP_YANK |
| 37 | {'c', NUL, OPF_CHANGE}, // OP_CHANGE |
| 38 | {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT |
| 39 | {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT |
| 40 | {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER |
| 41 | {'g', '~', OPF_CHANGE}, // OP_TILDE |
| 42 | {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT |
| 43 | {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT |
| 44 | {':', NUL, OPF_LINES}, // OP_COLON |
| 45 | {'g', 'U', OPF_CHANGE}, // OP_UPPER |
| 46 | {'g', 'u', OPF_CHANGE}, // OP_LOWER |
| 47 | {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN |
| 48 | {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS |
| 49 | {'g', '?', OPF_CHANGE}, // OP_ROT13 |
| 50 | {'r', NUL, OPF_CHANGE}, // OP_REPLACE |
| 51 | {'I', NUL, OPF_CHANGE}, // OP_INSERT |
| 52 | {'A', NUL, OPF_CHANGE}, // OP_APPEND |
| 53 | {'z', 'f', OPF_LINES}, // OP_FOLD |
| 54 | {'z', 'o', OPF_LINES}, // OP_FOLDOPEN |
| 55 | {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC |
| 56 | {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE |
| 57 | {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC |
| 58 | {'z', 'd', OPF_LINES}, // OP_FOLDDEL |
| 59 | {'z', 'D', OPF_LINES}, // OP_FOLDDELREC |
| 60 | {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2 |
| 61 | {'g', '@', OPF_CHANGE}, // OP_FUNCTION |
| 62 | {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD |
| 63 | {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /* |
| 67 | * Translate a command name into an operator type. |
| 68 | * Must only be called with a valid operator name! |
| 69 | */ |
| 70 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 71 | get_op_type(int char1, int char2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 72 | { |
| 73 | int i; |
| 74 | |
| 75 | if (char1 == 'r') /* ignore second character */ |
| 76 | return OP_REPLACE; |
| 77 | if (char1 == '~') /* when tilde is an operator */ |
| 78 | return OP_TILDE; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 79 | if (char1 == 'g' && char2 == Ctrl_A) /* add */ |
| 80 | return OP_NR_ADD; |
| 81 | if (char1 == 'g' && char2 == Ctrl_X) /* subtract */ |
| 82 | return OP_NR_SUB; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | for (i = 0; ; ++i) |
Bram Moolenaar | 2efb323 | 2017-12-19 12:27:23 +0100 | [diff] [blame] | 84 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 85 | if (opchars[i][0] == char1 && opchars[i][1] == char2) |
| 86 | break; |
Bram Moolenaar | 2efb323 | 2017-12-19 12:27:23 +0100 | [diff] [blame] | 87 | if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1)) |
| 88 | { |
| 89 | internal_error("get_op_type()"); |
| 90 | break; |
| 91 | } |
| 92 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 93 | return i; |
| 94 | } |
| 95 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | /* |
| 97 | * Return TRUE if operator "op" always works on whole lines. |
| 98 | */ |
Bram Moolenaar | 792cf5e | 2019-09-30 23:12:16 +0200 | [diff] [blame] | 99 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 100 | op_on_lines(int op) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 101 | { |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 102 | return opchars[op][2] & OPF_LINES; |
| 103 | } |
| 104 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 105 | #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 106 | /* |
| 107 | * Return TRUE if operator "op" changes text. |
| 108 | */ |
| 109 | int |
| 110 | op_is_change(int op) |
| 111 | { |
| 112 | return opchars[op][2] & OPF_CHANGE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 113 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 114 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * Get first operator command character. |
| 118 | * Returns 'g' or 'z' if there is another command character. |
| 119 | */ |
| 120 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 121 | get_op_char(int optype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 122 | { |
| 123 | return opchars[optype][0]; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Get second operator command character. |
| 128 | */ |
| 129 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 130 | get_extra_op_char(int optype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | { |
| 132 | return opchars[optype][1]; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * op_shift - handle a shift operation |
| 137 | */ |
| 138 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 139 | op_shift(oparg_T *oap, int curs_top, int amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | { |
| 141 | long i; |
| 142 | int first_char; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | int block_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | |
| 145 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 146 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 147 | return; |
| 148 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 149 | if (oap->block_mode) |
| 150 | block_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 151 | |
| 152 | for (i = oap->line_count; --i >= 0; ) |
| 153 | { |
| 154 | first_char = *ml_get_curline(); |
| 155 | if (first_char == NUL) /* empty line */ |
| 156 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 157 | else if (oap->block_mode) |
| 158 | shift_block(oap, amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 159 | else |
| 160 | /* Move the line right if it doesn't start with '#', 'smartindent' |
| 161 | * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */ |
| 162 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) |
| 163 | if (first_char != '#' || !preprocs_left()) |
| 164 | #endif |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 165 | shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | ++curwin->w_cursor.lnum; |
| 167 | } |
| 168 | |
| 169 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 170 | if (oap->block_mode) |
| 171 | { |
| 172 | curwin->w_cursor.lnum = oap->start.lnum; |
| 173 | curwin->w_cursor.col = block_col; |
| 174 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 175 | else if (curs_top) /* put cursor on first line, for ">>" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 176 | { |
| 177 | curwin->w_cursor.lnum = oap->start.lnum; |
| 178 | beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */ |
| 179 | } |
| 180 | else |
| 181 | --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */ |
| 182 | |
Bram Moolenaar | 54b2bfa | 2017-01-02 14:57:08 +0100 | [diff] [blame] | 183 | #ifdef FEAT_FOLDING |
| 184 | /* The cursor line is not in a closed fold */ |
| 185 | foldOpenCursor(); |
| 186 | #endif |
| 187 | |
| 188 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | if (oap->line_count > p_report) |
| 190 | { |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 191 | char *op; |
| 192 | char *msg_line_single; |
| 193 | char *msg_line_plural; |
| 194 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | if (oap->op_type == OP_RSHIFT) |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 196 | op = ">"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 197 | else |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 198 | op = "<"; |
| 199 | msg_line_single = NGETTEXT("%ld line %sed %d time", |
| 200 | "%ld line %sed %d times", amount); |
| 201 | msg_line_plural = NGETTEXT("%ld lines %sed %d time", |
| 202 | "%ld lines %sed %d times", amount); |
| 203 | vim_snprintf((char *)IObuff, IOSIZE, |
| 204 | NGETTEXT(msg_line_single, msg_line_plural, oap->line_count), |
| 205 | oap->line_count, op, amount); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 206 | msg((char *)IObuff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Set "'[" and "']" marks. |
| 211 | */ |
| 212 | curbuf->b_op_start = oap->start; |
| 213 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 214 | curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 215 | if (curbuf->b_op_end.col > 0) |
| 216 | --curbuf->b_op_end.col; |
| 217 | } |
| 218 | |
| 219 | /* |
Bram Moolenaar | 870ba5f | 2019-01-11 14:37:20 +0100 | [diff] [blame] | 220 | * Shift the current line one shiftwidth left (if left != 0) or right |
| 221 | * leaves cursor on first blank in the line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 222 | */ |
| 223 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 224 | shift_line( |
| 225 | int left, |
| 226 | int round, |
| 227 | int amount, |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 228 | int call_changed_bytes) // call changed_bytes() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 229 | { |
| 230 | int count; |
| 231 | int i, j; |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 232 | int sw_val = (int)get_sw_value_indent(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 233 | |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 234 | count = get_indent(); // get current indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 236 | if (round) // round off indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 237 | { |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 238 | i = count / sw_val; // number of 'shiftwidth' rounded down |
| 239 | j = count % sw_val; // extra spaces |
| 240 | if (j && left) // first remove extra spaces |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 241 | --amount; |
| 242 | if (left) |
| 243 | { |
| 244 | i -= amount; |
| 245 | if (i < 0) |
| 246 | i = 0; |
| 247 | } |
| 248 | else |
| 249 | i += amount; |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 250 | count = i * sw_val; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 251 | } |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 252 | else // original vi indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 253 | { |
| 254 | if (left) |
| 255 | { |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 256 | count -= sw_val * amount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 257 | if (count < 0) |
| 258 | count = 0; |
| 259 | } |
| 260 | else |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 261 | count += sw_val * amount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 264 | // Set new indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 265 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 266 | change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 267 | else |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 268 | (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 271 | /* |
| 272 | * Shift one line of the current block one shiftwidth right or left. |
| 273 | * Leaves cursor on first character in block. |
| 274 | */ |
| 275 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 276 | shift_block(oparg_T *oap, int amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 277 | { |
| 278 | int left = (oap->op_type == OP_LSHIFT); |
| 279 | int oldstate = State; |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 280 | int total; |
| 281 | char_u *newp, *oldp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 282 | int oldcol = curwin->w_cursor.col; |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 283 | int sw_val = (int)get_sw_value_indent(curbuf); |
| 284 | int ts_val = (int)curbuf->b_p_ts; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 285 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 286 | int incr; |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 287 | colnr_T ws_vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 288 | int i = 0, j = 0; |
| 289 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 290 | #ifdef FEAT_RIGHTLEFT |
| 291 | int old_p_ri = p_ri; |
| 292 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 293 | p_ri = 0; /* don't want revins in indent */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 294 | #endif |
| 295 | |
| 296 | State = INSERT; /* don't want REPLACE for State */ |
| 297 | block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); |
| 298 | if (bd.is_short) |
| 299 | return; |
| 300 | |
| 301 | /* total is number of screen columns to be inserted/removed */ |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 302 | total = (int)((unsigned)amount * (unsigned)sw_val); |
| 303 | if ((total / sw_val) != amount) |
Bram Moolenaar | bae5a17 | 2017-08-06 15:42:06 +0200 | [diff] [blame] | 304 | return; /* multiplication overflow */ |
| 305 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 306 | oldp = ml_get_curline(); |
| 307 | |
| 308 | if (!left) |
| 309 | { |
| 310 | /* |
| 311 | * 1. Get start vcol |
| 312 | * 2. Total ws vcols |
| 313 | * 3. Divvy into TABs & spp |
| 314 | * 4. Construct new string |
| 315 | */ |
| 316 | total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */ |
| 317 | ws_vcol = bd.start_vcol - bd.pre_whitesp; |
| 318 | if (bd.startspaces) |
| 319 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 320 | if (has_mbyte) |
Bram Moolenaar | 20b4f46 | 2016-03-05 17:25:39 +0100 | [diff] [blame] | 321 | { |
| 322 | if ((*mb_ptr2len)(bd.textstart) == 1) |
| 323 | ++bd.textstart; |
| 324 | else |
| 325 | { |
| 326 | ws_vcol = 0; |
| 327 | bd.startspaces = 0; |
| 328 | } |
| 329 | } |
Bram Moolenaar | be1138b | 2009-11-11 16:22:28 +0000 | [diff] [blame] | 330 | else |
Bram Moolenaar | be1138b | 2009-11-11 16:22:28 +0000 | [diff] [blame] | 331 | ++bd.textstart; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 332 | } |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 333 | for ( ; VIM_ISWHITE(*bd.textstart); ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 334 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 335 | /* TODO: is passing bd.textstart for start of the line OK? */ |
| 336 | incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart, |
| 337 | (colnr_T)(bd.start_vcol)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 338 | total += incr; |
| 339 | bd.start_vcol += incr; |
| 340 | } |
| 341 | /* OK, now total=all the VWS reqd, and textstart points at the 1st |
| 342 | * non-ws char in the block. */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 343 | #ifdef FEAT_VARTABS |
| 344 | if (!curbuf->b_p_et) |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 345 | tabstop_fromto(ws_vcol, ws_vcol + total, |
| 346 | ts_val, curbuf->b_p_vts_array, &i, &j); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 347 | else |
| 348 | j = total; |
| 349 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 350 | if (!curbuf->b_p_et) |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 351 | i = ((ws_vcol % ts_val) + total) / ts_val; /* number of tabs */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 352 | if (i) |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 353 | j = ((ws_vcol % ts_val) + total) % ts_val; /* number of spp */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 354 | else |
| 355 | j = total; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 356 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 357 | /* if we're splitting a TAB, allow for it */ |
| 358 | bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0); |
| 359 | len = (int)STRLEN(bd.textstart) + 1; |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 360 | newp = alloc(bd.textcol + i + j + len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 361 | if (newp == NULL) |
| 362 | return; |
| 363 | vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len)); |
| 364 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 365 | vim_memset(newp + bd.textcol, TAB, (size_t)i); |
| 366 | vim_memset(newp + bd.textcol + i, ' ', (size_t)j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 367 | /* the end */ |
| 368 | mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len); |
| 369 | } |
| 370 | else /* left */ |
| 371 | { |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 372 | colnr_T destination_col; /* column to which text in block will |
| 373 | be shifted */ |
| 374 | char_u *verbatim_copy_end; /* end of the part of the line which is |
| 375 | copied verbatim */ |
| 376 | colnr_T verbatim_copy_width;/* the (displayed) width of this part |
| 377 | of line */ |
| 378 | unsigned fill; /* nr of spaces that replace a TAB */ |
| 379 | unsigned new_line_len; /* the length of the line after the |
| 380 | block shift */ |
| 381 | size_t block_space_width; |
| 382 | size_t shift_amount; |
| 383 | char_u *non_white = bd.textstart; |
| 384 | colnr_T non_white_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 385 | |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 386 | /* |
| 387 | * Firstly, let's find the first non-whitespace character that is |
| 388 | * displayed after the block's start column and the character's column |
| 389 | * number. Also, let's calculate the width of all the whitespace |
| 390 | * characters that are displayed in the block and precede the searched |
| 391 | * non-whitespace character. |
| 392 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 393 | |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 394 | /* If "bd.startspaces" is set, "bd.textstart" points to the character, |
| 395 | * the part of which is displayed at the block's beginning. Let's start |
| 396 | * searching from the next character. */ |
| 397 | if (bd.startspaces) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 398 | MB_PTR_ADV(non_white); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 399 | |
| 400 | /* The character's column is in "bd.start_vcol". */ |
| 401 | non_white_col = bd.start_vcol; |
| 402 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 403 | while (VIM_ISWHITE(*non_white)) |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 404 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 405 | incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 406 | non_white_col += incr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 409 | block_space_width = non_white_col - oap->start_vcol; |
| 410 | /* We will shift by "total" or "block_space_width", whichever is less. |
| 411 | */ |
Bram Moolenaar | 92a990b | 2009-04-22 15:45:05 +0000 | [diff] [blame] | 412 | shift_amount = (block_space_width < (size_t)total |
| 413 | ? block_space_width : (size_t)total); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 414 | |
| 415 | /* The column to which we will shift the text. */ |
Bram Moolenaar | 92a990b | 2009-04-22 15:45:05 +0000 | [diff] [blame] | 416 | destination_col = (colnr_T)(non_white_col - shift_amount); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 417 | |
| 418 | /* Now let's find out how much of the beginning of the line we can |
| 419 | * reuse without modification. */ |
| 420 | verbatim_copy_end = bd.textstart; |
| 421 | verbatim_copy_width = bd.start_vcol; |
| 422 | |
| 423 | /* If "bd.startspaces" is set, "bd.textstart" points to the character |
| 424 | * preceding the block. We have to subtract its width to obtain its |
| 425 | * column number. */ |
| 426 | if (bd.startspaces) |
| 427 | verbatim_copy_width -= bd.start_char_vcols; |
| 428 | while (verbatim_copy_width < destination_col) |
| 429 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 430 | char_u *line = verbatim_copy_end; |
| 431 | |
| 432 | /* TODO: is passing verbatim_copy_end for start of the line OK? */ |
| 433 | incr = lbr_chartabsize(line, verbatim_copy_end, |
| 434 | verbatim_copy_width); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 435 | if (verbatim_copy_width + incr > destination_col) |
| 436 | break; |
| 437 | verbatim_copy_width += incr; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 438 | MB_PTR_ADV(verbatim_copy_end); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* If "destination_col" is different from the width of the initial |
| 442 | * part of the line that will be copied, it means we encountered a tab |
| 443 | * character, which we will have to partly replace with spaces. */ |
| 444 | fill = destination_col - verbatim_copy_width; |
| 445 | |
| 446 | /* The replacement line will consist of: |
| 447 | * - the beginning of the original line up to "verbatim_copy_end", |
| 448 | * - "fill" number of spaces, |
| 449 | * - the rest of the line, pointed to by non_white. */ |
| 450 | new_line_len = (unsigned)(verbatim_copy_end - oldp) |
| 451 | + fill |
| 452 | + (unsigned)STRLEN(non_white) + 1; |
| 453 | |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 454 | newp = alloc(new_line_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 455 | if (newp == NULL) |
| 456 | return; |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 457 | mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp)); |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 458 | vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill); |
Bram Moolenaar | 9d77dcc | 2009-03-11 15:28:26 +0000 | [diff] [blame] | 459 | STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 460 | } |
| 461 | /* replace the line */ |
| 462 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 463 | changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol); |
| 464 | State = oldstate; |
| 465 | curwin->w_cursor.col = oldcol; |
| 466 | #ifdef FEAT_RIGHTLEFT |
| 467 | p_ri = old_p_ri; |
| 468 | #endif |
| 469 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 470 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 471 | /* |
| 472 | * Insert string "s" (b_insert ? before : after) block :AKelly |
| 473 | * Caller must prepare for undo. |
| 474 | */ |
| 475 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 476 | block_insert( |
| 477 | oparg_T *oap, |
| 478 | char_u *s, |
| 479 | int b_insert, |
| 480 | struct block_def *bdp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 481 | { |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 482 | int ts_val; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 483 | int count = 0; /* extra spaces to replace a cut TAB */ |
| 484 | int spaces = 0; /* non-zero if cutting a TAB */ |
| 485 | colnr_T offset; /* pointer along new line */ |
| 486 | unsigned s_len; /* STRLEN(s) */ |
| 487 | char_u *newp, *oldp; /* new, old lines */ |
| 488 | linenr_T lnum; /* loop var */ |
| 489 | int oldstate = State; |
| 490 | |
| 491 | State = INSERT; /* don't want REPLACE for State */ |
| 492 | s_len = (unsigned)STRLEN(s); |
| 493 | |
| 494 | for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++) |
| 495 | { |
| 496 | block_prep(oap, bdp, lnum, TRUE); |
| 497 | if (bdp->is_short && b_insert) |
| 498 | continue; /* OP_INSERT, line ends before block start */ |
| 499 | |
| 500 | oldp = ml_get(lnum); |
| 501 | |
| 502 | if (b_insert) |
| 503 | { |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 504 | ts_val = bdp->start_char_vcols; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 505 | spaces = bdp->startspaces; |
| 506 | if (spaces != 0) |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 507 | count = ts_val - 1; /* we're cutting a TAB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 508 | offset = bdp->textcol; |
| 509 | } |
| 510 | else /* append */ |
| 511 | { |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 512 | ts_val = bdp->end_char_vcols; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 513 | if (!bdp->is_short) /* spaces = padding after block */ |
| 514 | { |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 515 | spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 516 | if (spaces != 0) |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 517 | count = ts_val - 1; /* we're cutting a TAB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 518 | offset = bdp->textcol + bdp->textlen - (spaces != 0); |
| 519 | } |
| 520 | else /* spaces = padding to block edge */ |
| 521 | { |
| 522 | /* if $ used, just append to EOL (ie spaces==0) */ |
| 523 | if (!bdp->is_MAX) |
| 524 | spaces = (oap->end_vcol - bdp->end_vcol) + 1; |
| 525 | count = spaces; |
| 526 | offset = bdp->textcol + bdp->textlen; |
| 527 | } |
| 528 | } |
| 529 | |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 530 | if (has_mbyte && spaces > 0) |
| 531 | { |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 532 | int off; |
| 533 | |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 534 | /* Avoid starting halfway a multi-byte character. */ |
| 535 | if (b_insert) |
| 536 | { |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 537 | off = (*mb_head_off)(oldp, oldp + offset + spaces); |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 538 | } |
| 539 | else |
| 540 | { |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 541 | off = (*mb_off_next)(oldp, oldp + offset); |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 542 | offset += off; |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 543 | } |
Bram Moolenaar | fc3f23b | 2014-12-17 18:35:42 +0100 | [diff] [blame] | 544 | spaces -= off; |
| 545 | count -= off; |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 546 | } |
Bram Moolenaar | b5cf6c3 | 2014-08-16 18:36:43 +0200 | [diff] [blame] | 547 | |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 548 | newp = alloc(STRLEN(oldp) + s_len + count + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 549 | if (newp == NULL) |
| 550 | continue; |
| 551 | |
| 552 | /* copy up to shifted part */ |
| 553 | mch_memmove(newp, oldp, (size_t)(offset)); |
| 554 | oldp += offset; |
| 555 | |
| 556 | /* insert pre-padding */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 557 | vim_memset(newp + offset, ' ', (size_t)spaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 558 | |
| 559 | /* copy the new text */ |
| 560 | mch_memmove(newp + offset + spaces, s, (size_t)s_len); |
| 561 | offset += s_len; |
| 562 | |
| 563 | if (spaces && !bdp->is_short) |
| 564 | { |
| 565 | /* insert post-padding */ |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 566 | vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 567 | /* We're splitting a TAB, don't copy it. */ |
| 568 | oldp++; |
| 569 | /* We allowed for that TAB, remember this now */ |
| 570 | count++; |
| 571 | } |
| 572 | |
| 573 | if (spaces > 0) |
| 574 | offset += count; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 575 | STRMOVE(newp + offset, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 576 | |
| 577 | ml_replace(lnum, newp, FALSE); |
| 578 | |
| 579 | if (lnum == oap->end.lnum) |
| 580 | { |
| 581 | /* Set "']" mark to the end of the block instead of the end of |
| 582 | * the insert in the first line. */ |
| 583 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 584 | curbuf->b_op_end.col = offset; |
| 585 | } |
| 586 | } /* for all lnum */ |
| 587 | |
| 588 | changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); |
| 589 | |
| 590 | State = oldstate; |
| 591 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 592 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 593 | /* |
| 594 | * Stuff a string into the typeahead buffer, such that edit() will insert it |
| 595 | * literally ("literally" TRUE) or interpret is as typed characters. |
| 596 | */ |
Bram Moolenaar | 4aea03e | 2019-09-25 22:37:17 +0200 | [diff] [blame] | 597 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 598 | stuffescaped(char_u *arg, int literally) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 599 | { |
| 600 | int c; |
| 601 | char_u *start; |
| 602 | |
| 603 | while (*arg != NUL) |
| 604 | { |
| 605 | /* Stuff a sequence of normal ASCII characters, that's fast. Also |
| 606 | * stuff K_SPECIAL to get the effect of a special key when "literally" |
| 607 | * is TRUE. */ |
| 608 | start = arg; |
| 609 | while ((*arg >= ' ' |
| 610 | #ifndef EBCDIC |
| 611 | && *arg < DEL /* EBCDIC: chars above space are normal */ |
| 612 | #endif |
| 613 | ) |
| 614 | || (*arg == K_SPECIAL && !literally)) |
| 615 | ++arg; |
| 616 | if (arg > start) |
| 617 | stuffReadbuffLen(start, (long)(arg - start)); |
| 618 | |
| 619 | /* stuff a single special character */ |
| 620 | if (*arg != NUL) |
| 621 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 622 | if (has_mbyte) |
Bram Moolenaar | 3b1c485 | 2010-07-31 17:59:29 +0200 | [diff] [blame] | 623 | c = mb_cptr2char_adv(&arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 624 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 625 | c = *arg++; |
| 626 | if (literally && ((c < ' ' && c != TAB) || c == DEL)) |
| 627 | stuffcharReadbuff(Ctrl_V); |
| 628 | stuffcharReadbuff(c); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 634 | * Handle a delete operation. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 635 | * |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 636 | * Return FAIL if undo failed, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 637 | */ |
| 638 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 639 | op_delete(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 640 | { |
| 641 | int n; |
| 642 | linenr_T lnum; |
| 643 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 644 | char_u *newp, *oldp; |
| 645 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 646 | linenr_T old_lcount = curbuf->b_ml.ml_line_count; |
| 647 | int did_yank = FALSE; |
| 648 | |
| 649 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */ |
| 650 | return OK; |
| 651 | |
| 652 | /* Nothing to delete, return here. Do prepare undo, for op_change(). */ |
| 653 | if (oap->empty) |
| 654 | return u_save_cursor(); |
| 655 | |
| 656 | if (!curbuf->b_p_ma) |
| 657 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 658 | emsg(_(e_modifiable)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | return FAIL; |
| 660 | } |
| 661 | |
| 662 | #ifdef FEAT_CLIPBOARD |
| 663 | adjust_clip_reg(&oap->regname); |
| 664 | #endif |
| 665 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 666 | if (has_mbyte) |
| 667 | mb_adjust_opend(oap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 668 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 669 | /* |
| 670 | * Imitate the strange Vi behaviour: If the delete spans more than one |
| 671 | * line and motion_type == MCHAR and the result is a blank line, make the |
| 672 | * delete linewise. Don't do this for the change command or Visual mode. |
| 673 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 674 | if ( oap->motion_type == MCHAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 675 | && !oap->is_VIsual |
Bram Moolenaar | ec2dad6 | 2005-01-02 11:36:03 +0000 | [diff] [blame] | 676 | && !oap->block_mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 677 | && oap->line_count > 1 |
Bram Moolenaar | 64a7230 | 2012-01-10 13:46:22 +0100 | [diff] [blame] | 678 | && oap->motion_force == NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 679 | && oap->op_type == OP_DELETE) |
| 680 | { |
Bram Moolenaar | 44286ca | 2011-07-15 17:51:34 +0200 | [diff] [blame] | 681 | ptr = ml_get(oap->end.lnum) + oap->end.col; |
| 682 | if (*ptr != NUL) |
| 683 | ptr += oap->inclusive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 684 | ptr = skipwhite(ptr); |
| 685 | if (*ptr == NUL && inindent(0)) |
| 686 | oap->motion_type = MLINE; |
| 687 | } |
| 688 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 689 | /* |
| 690 | * Check for trying to delete (e.g. "D") in an empty line. |
| 691 | * Note: For the change operator it is ok. |
| 692 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 693 | if ( oap->motion_type == MCHAR |
| 694 | && oap->line_count == 1 |
| 695 | && oap->op_type == OP_DELETE |
| 696 | && *ml_get(oap->start.lnum) == NUL) |
| 697 | { |
| 698 | /* |
Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 699 | * 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] | 700 | * 'cpoptions' (Vi compatible). |
| 701 | */ |
Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 702 | if (virtual_op) |
| 703 | /* Virtual editing: Nothing gets deleted, but we set the '[ and '] |
| 704 | * marks as if it happened. */ |
| 705 | goto setmarks; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL) |
| 707 | beep_flush(); |
| 708 | return OK; |
| 709 | } |
| 710 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 711 | /* |
| 712 | * Do a yank of whatever we're about to delete. |
| 713 | * If a yank register was specified, put the deleted text into that |
| 714 | * register. For the black hole register '_' don't yank anything. |
| 715 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 716 | if (oap->regname != '_') |
| 717 | { |
| 718 | if (oap->regname != 0) |
| 719 | { |
| 720 | /* check for read-only register */ |
| 721 | if (!valid_yank_reg(oap->regname, TRUE)) |
| 722 | { |
| 723 | beep_flush(); |
| 724 | return OK; |
| 725 | } |
| 726 | get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */ |
| 727 | if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */ |
| 728 | did_yank = TRUE; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Put deleted text into register 1 and shift number registers if the |
Bram Moolenaar | 9d7fdd4 | 2019-03-08 09:50:52 +0100 | [diff] [blame] | 733 | * delete contains a line break, or when using a specific operator (Vi |
| 734 | * compatible) |
Bram Moolenaar | 7c82130 | 2012-09-05 14:18:45 +0200 | [diff] [blame] | 735 | * Use the register name from before adjust_clip_reg() may have |
| 736 | * changed it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 737 | */ |
Bram Moolenaar | 9d7fdd4 | 2019-03-08 09:50:52 +0100 | [diff] [blame] | 738 | if (oap->motion_type == MLINE || oap->line_count > 1 |
| 739 | || oap->use_reg_one) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 740 | { |
Bram Moolenaar | a189184 | 2017-02-04 21:34:31 +0100 | [diff] [blame] | 741 | shift_delete_registers(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 742 | if (op_yank(oap, TRUE, FALSE) == OK) |
| 743 | did_yank = TRUE; |
| 744 | } |
| 745 | |
Bram Moolenaar | 84298db | 2012-04-20 13:46:08 +0200 | [diff] [blame] | 746 | /* Yank into small delete register when no named register specified |
| 747 | * and the delete is within one line. */ |
| 748 | if (( |
| 749 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 750 | ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') || |
| 751 | ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') || |
Bram Moolenaar | 84298db | 2012-04-20 13:46:08 +0200 | [diff] [blame] | 752 | #endif |
| 753 | oap->regname == 0) && oap->motion_type != MLINE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 754 | && oap->line_count == 1) |
| 755 | { |
| 756 | oap->regname = '-'; |
| 757 | get_yank_register(oap->regname, TRUE); |
| 758 | if (op_yank(oap, TRUE, FALSE) == OK) |
| 759 | did_yank = TRUE; |
| 760 | oap->regname = 0; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * If there's too much stuff to fit in the yank register, then get a |
| 765 | * confirmation before doing the delete. This is crude, but simple. |
| 766 | * And it avoids doing a delete of something we can't put back if we |
| 767 | * want. |
| 768 | */ |
| 769 | if (!did_yank) |
| 770 | { |
| 771 | int msg_silent_save = msg_silent; |
| 772 | |
| 773 | msg_silent = 0; /* must display the prompt */ |
| 774 | n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE); |
| 775 | msg_silent = msg_silent_save; |
| 776 | if (n != 'y') |
| 777 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 778 | emsg(_(e_abort)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 779 | return FAIL; |
| 780 | } |
| 781 | } |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 782 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 783 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 784 | if (did_yank && has_textyankpost()) |
Bram Moolenaar | 4aea03e | 2019-09-25 22:37:17 +0200 | [diff] [blame] | 785 | yank_do_autocmd(oap, get_y_current()); |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 786 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Bram Moolenaar | d04b750 | 2010-07-08 22:27:55 +0200 | [diff] [blame] | 789 | /* |
| 790 | * block mode delete |
| 791 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 792 | if (oap->block_mode) |
| 793 | { |
| 794 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 795 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 796 | return FAIL; |
| 797 | |
| 798 | for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum) |
| 799 | { |
| 800 | block_prep(oap, &bd, lnum, TRUE); |
| 801 | if (bd.textlen == 0) /* nothing to delete */ |
| 802 | continue; |
| 803 | |
| 804 | /* Adjust cursor position for tab replaced by spaces and 'lbr'. */ |
| 805 | if (lnum == curwin->w_cursor.lnum) |
| 806 | { |
| 807 | curwin->w_cursor.col = bd.textcol + bd.startspaces; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 808 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 811 | // "n" == number of chars deleted |
| 812 | // If we delete a TAB, it may be replaced by several characters. |
| 813 | // Thus the number of characters may increase! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 814 | n = bd.textlen - bd.startspaces - bd.endspaces; |
| 815 | oldp = ml_get(lnum); |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 816 | newp = alloc(STRLEN(oldp) + 1 - n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | if (newp == NULL) |
| 818 | continue; |
| 819 | /* copy up to deleted part */ |
| 820 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 821 | /* insert spaces */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 822 | vim_memset(newp + bd.textcol, ' ', |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 823 | (size_t)(bd.startspaces + bd.endspaces)); |
| 824 | /* copy the part after the deleted part */ |
| 825 | oldp += bd.textcol + bd.textlen; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 826 | STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 827 | /* replace the line */ |
| 828 | ml_replace(lnum, newp, FALSE); |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 829 | |
| 830 | #ifdef FEAT_TEXT_PROP |
| 831 | if (curbuf->b_has_textprop && n != 0) |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 832 | adjust_prop_columns(lnum, bd.textcol, -n, 0); |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 833 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | check_cursor_col(); |
| 837 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 838 | oap->end.lnum + 1, 0L); |
| 839 | oap->line_count = 0; /* no lines deleted */ |
| 840 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 841 | else if (oap->motion_type == MLINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 842 | { |
| 843 | if (oap->op_type == OP_CHANGE) |
| 844 | { |
| 845 | /* Delete the lines except the first one. Temporarily move the |
| 846 | * cursor to the next line. Save the current line number, if the |
| 847 | * last line is deleted it may be changed. |
| 848 | */ |
| 849 | if (oap->line_count > 1) |
| 850 | { |
| 851 | lnum = curwin->w_cursor.lnum; |
| 852 | ++curwin->w_cursor.lnum; |
| 853 | del_lines((long)(oap->line_count - 1), TRUE); |
| 854 | curwin->w_cursor.lnum = lnum; |
| 855 | } |
| 856 | if (u_save_cursor() == FAIL) |
| 857 | return FAIL; |
| 858 | if (curbuf->b_p_ai) /* don't delete indent */ |
| 859 | { |
| 860 | beginline(BL_WHITE); /* cursor on first non-white */ |
| 861 | did_ai = TRUE; /* delete the indent when ESC hit */ |
| 862 | ai_col = curwin->w_cursor.col; |
| 863 | } |
| 864 | else |
| 865 | beginline(0); /* cursor in column 0 */ |
| 866 | truncate_line(FALSE); /* delete the rest of the line */ |
| 867 | /* leave cursor past last char in line */ |
| 868 | if (oap->line_count > 1) |
| 869 | u_clearline(); /* "U" command not possible after "2cc" */ |
| 870 | } |
| 871 | else |
| 872 | { |
| 873 | del_lines(oap->line_count, TRUE); |
| 874 | beginline(BL_WHITE | BL_FIX); |
| 875 | u_clearline(); /* "U" command not possible after "dd" */ |
| 876 | } |
| 877 | } |
| 878 | else |
| 879 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 880 | if (virtual_op) |
| 881 | { |
| 882 | int endcol = 0; |
| 883 | |
| 884 | /* For virtualedit: break the tabs that are partly included. */ |
| 885 | if (gchar_pos(&oap->start) == '\t') |
| 886 | { |
| 887 | if (u_save_cursor() == FAIL) /* save first line for undo */ |
| 888 | return FAIL; |
| 889 | if (oap->line_count == 1) |
| 890 | endcol = getviscol2(oap->end.col, oap->end.coladd); |
| 891 | coladvance_force(getviscol2(oap->start.col, oap->start.coladd)); |
| 892 | oap->start = curwin->w_cursor; |
| 893 | if (oap->line_count == 1) |
| 894 | { |
| 895 | coladvance(endcol); |
| 896 | oap->end.col = curwin->w_cursor.col; |
| 897 | oap->end.coladd = curwin->w_cursor.coladd; |
| 898 | curwin->w_cursor = oap->start; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | /* Break a tab only when it's included in the area. */ |
| 903 | if (gchar_pos(&oap->end) == '\t' |
| 904 | && (int)oap->end.coladd < oap->inclusive) |
| 905 | { |
| 906 | /* save last line for undo */ |
| 907 | if (u_save((linenr_T)(oap->end.lnum - 1), |
| 908 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 909 | return FAIL; |
| 910 | curwin->w_cursor = oap->end; |
| 911 | coladvance_force(getviscol2(oap->end.col, oap->end.coladd)); |
| 912 | oap->end = curwin->w_cursor; |
| 913 | curwin->w_cursor = oap->start; |
| 914 | } |
| 915 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 916 | |
| 917 | if (oap->line_count == 1) /* delete characters within one line */ |
| 918 | { |
| 919 | if (u_save_cursor() == FAIL) /* save line for undo */ |
| 920 | return FAIL; |
| 921 | |
| 922 | /* if 'cpoptions' contains '$', display '$' at end of change */ |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 923 | if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | && oap->op_type == OP_CHANGE |
| 925 | && oap->end.lnum == curwin->w_cursor.lnum |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 926 | && !oap->is_VIsual) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 927 | display_dollar(oap->end.col - !oap->inclusive); |
| 928 | |
| 929 | n = oap->end.col - oap->start.col + 1 - !oap->inclusive; |
| 930 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 931 | if (virtual_op) |
| 932 | { |
| 933 | /* fix up things for virtualedit-delete: |
| 934 | * break the tabs which are going to get in our way |
| 935 | */ |
| 936 | char_u *curline = ml_get_curline(); |
| 937 | int len = (int)STRLEN(curline); |
| 938 | |
| 939 | if (oap->end.coladd != 0 |
| 940 | && (int)oap->end.col >= len - 1 |
| 941 | && !(oap->start.coladd && (int)oap->end.col >= len - 1)) |
| 942 | n++; |
| 943 | /* Delete at least one char (e.g, when on a control char). */ |
| 944 | if (n == 0 && oap->start.coladd != oap->end.coladd) |
| 945 | n = 1; |
| 946 | |
| 947 | /* When deleted a char in the line, reset coladd. */ |
| 948 | if (gchar_cursor() != NUL) |
| 949 | curwin->w_cursor.coladd = 0; |
| 950 | } |
Bram Moolenaar | d009e86 | 2015-06-09 20:20:03 +0200 | [diff] [blame] | 951 | (void)del_bytes((long)n, !virtual_op, |
| 952 | oap->op_type == OP_DELETE && !oap->is_VIsual); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 953 | } |
| 954 | else /* delete characters between lines */ |
| 955 | { |
| 956 | pos_T curpos; |
| 957 | |
| 958 | /* save deleted and changed lines for undo */ |
| 959 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 960 | (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL) |
| 961 | return FAIL; |
| 962 | |
| 963 | truncate_line(TRUE); /* delete from cursor to end of line */ |
| 964 | |
| 965 | curpos = curwin->w_cursor; /* remember curwin->w_cursor */ |
| 966 | ++curwin->w_cursor.lnum; |
| 967 | del_lines((long)(oap->line_count - 2), FALSE); |
| 968 | |
Bram Moolenaar | d009e86 | 2015-06-09 20:20:03 +0200 | [diff] [blame] | 969 | /* delete from start of line until op_end */ |
Bram Moolenaar | 44286ca | 2011-07-15 17:51:34 +0200 | [diff] [blame] | 970 | n = (oap->end.col + 1 - !oap->inclusive); |
Bram Moolenaar | d009e86 | 2015-06-09 20:20:03 +0200 | [diff] [blame] | 971 | curwin->w_cursor.col = 0; |
| 972 | (void)del_bytes((long)n, !virtual_op, |
| 973 | oap->op_type == OP_DELETE && !oap->is_VIsual); |
| 974 | curwin->w_cursor = curpos; /* restore curwin->w_cursor */ |
| 975 | (void)do_join(2, FALSE, FALSE, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
| 979 | msgmore(curbuf->b_ml.ml_line_count - old_lcount); |
| 980 | |
Bram Moolenaar | 87e25fd | 2005-07-27 21:13:01 +0000 | [diff] [blame] | 981 | setmarks: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 982 | if (oap->block_mode) |
| 983 | { |
| 984 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 985 | curbuf->b_op_end.col = oap->start.col; |
| 986 | } |
| 987 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 988 | curbuf->b_op_end = oap->start; |
| 989 | curbuf->b_op_start = oap->start; |
| 990 | |
| 991 | return OK; |
| 992 | } |
| 993 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 994 | /* |
| 995 | * Adjust end of operating area for ending on a multi-byte character. |
| 996 | * Used for deletion. |
| 997 | */ |
| 998 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 999 | mb_adjust_opend(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1000 | { |
| 1001 | char_u *p; |
| 1002 | |
| 1003 | if (oap->inclusive) |
| 1004 | { |
| 1005 | p = ml_get(oap->end.lnum); |
| 1006 | oap->end.col += mb_tail_off(p, p + oap->end.col); |
| 1007 | } |
| 1008 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1009 | |
Bram Moolenaar | 630afe8 | 2018-06-28 19:26:28 +0200 | [diff] [blame] | 1010 | /* |
| 1011 | * Replace the character under the cursor with "c". |
| 1012 | * This takes care of multi-byte characters. |
| 1013 | */ |
| 1014 | static void |
| 1015 | replace_character(int c) |
| 1016 | { |
| 1017 | int n = State; |
| 1018 | |
| 1019 | State = REPLACE; |
| 1020 | ins_char(c); |
| 1021 | State = n; |
| 1022 | /* Backup to the replaced character. */ |
| 1023 | dec_cursor(); |
| 1024 | } |
| 1025 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1026 | /* |
| 1027 | * Replace a whole area with one character. |
| 1028 | */ |
| 1029 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1030 | op_replace(oparg_T *oap, int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1031 | { |
| 1032 | int n, numc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1033 | int num_chars; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1034 | char_u *newp, *oldp; |
| 1035 | size_t oldlen; |
| 1036 | struct block_def bd; |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 1037 | char_u *after_p = NULL; |
Bram Moolenaar | f12519d | 2018-02-06 22:52:49 +0100 | [diff] [blame] | 1038 | int had_ctrl_v_cr = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1039 | |
| 1040 | if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty) |
| 1041 | return OK; /* nothing to do */ |
| 1042 | |
Bram Moolenaar | f12519d | 2018-02-06 22:52:49 +0100 | [diff] [blame] | 1043 | if (c == REPLACE_CR_NCHAR) |
| 1044 | { |
| 1045 | had_ctrl_v_cr = TRUE; |
| 1046 | c = CAR; |
| 1047 | } |
| 1048 | else if (c == REPLACE_NL_NCHAR) |
| 1049 | { |
| 1050 | had_ctrl_v_cr = TRUE; |
| 1051 | c = NL; |
| 1052 | } |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 1053 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1054 | if (has_mbyte) |
| 1055 | mb_adjust_opend(oap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | |
| 1057 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 1058 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 1059 | return FAIL; |
| 1060 | |
| 1061 | /* |
| 1062 | * block mode replace |
| 1063 | */ |
| 1064 | if (oap->block_mode) |
| 1065 | { |
| 1066 | bd.is_MAX = (curwin->w_curswant == MAXCOL); |
| 1067 | for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum) |
| 1068 | { |
Bram Moolenaar | a1381de | 2009-11-03 15:44:21 +0000 | [diff] [blame] | 1069 | curwin->w_cursor.col = 0; /* make sure cursor position is valid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1070 | block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); |
| 1071 | if (bd.textlen == 0 && (!virtual_op || bd.is_MAX)) |
| 1072 | continue; /* nothing to replace */ |
| 1073 | |
| 1074 | /* n == number of extra chars required |
| 1075 | * If we split a TAB, it may be replaced by several characters. |
| 1076 | * Thus the number of characters may increase! |
| 1077 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1078 | /* If the range starts in virtual space, count the initial |
| 1079 | * coladd offset as part of "startspaces" */ |
| 1080 | if (virtual_op && bd.is_short && *bd.textstart == NUL) |
| 1081 | { |
| 1082 | pos_T vpos; |
| 1083 | |
Bram Moolenaar | a1381de | 2009-11-03 15:44:21 +0000 | [diff] [blame] | 1084 | vpos.lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1085 | getvpos(&vpos, oap->start_vcol); |
| 1086 | bd.startspaces += vpos.coladd; |
| 1087 | n = bd.startspaces; |
| 1088 | } |
| 1089 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1090 | /* allow for pre spaces */ |
| 1091 | n = (bd.startspaces ? bd.start_char_vcols - 1 : 0); |
| 1092 | |
| 1093 | /* allow for post spp */ |
| 1094 | n += (bd.endspaces |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1095 | && !bd.is_oneChar |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0; |
| 1097 | /* Figure out how many characters to replace. */ |
| 1098 | numc = oap->end_vcol - oap->start_vcol + 1; |
| 1099 | if (bd.is_short && (!virtual_op || bd.is_MAX)) |
| 1100 | numc -= (oap->end_vcol - bd.end_vcol) + 1; |
| 1101 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1102 | /* A double-wide character can be replaced only up to half the |
| 1103 | * times. */ |
| 1104 | if ((*mb_char2cells)(c) > 1) |
| 1105 | { |
| 1106 | if ((numc & 1) && !bd.is_short) |
| 1107 | { |
| 1108 | ++bd.endspaces; |
| 1109 | ++n; |
| 1110 | } |
| 1111 | numc = numc / 2; |
| 1112 | } |
| 1113 | |
| 1114 | /* Compute bytes needed, move character count to num_chars. */ |
| 1115 | num_chars = numc; |
| 1116 | numc *= (*mb_char2len)(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1117 | /* oldlen includes textlen, so don't double count */ |
| 1118 | n += numc - bd.textlen; |
| 1119 | |
| 1120 | oldp = ml_get_curline(); |
| 1121 | oldlen = STRLEN(oldp); |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1122 | newp = alloc(oldlen + 1 + n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1123 | if (newp == NULL) |
| 1124 | continue; |
| 1125 | vim_memset(newp, NUL, (size_t)(oldlen + 1 + n)); |
| 1126 | /* copy up to deleted part */ |
| 1127 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 1128 | oldp += bd.textcol + bd.textlen; |
| 1129 | /* insert pre-spaces */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 1130 | vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1131 | /* insert replacement chars CHECK FOR ALLOCATED SPACE */ |
Bram Moolenaar | f12519d | 2018-02-06 22:52:49 +0100 | [diff] [blame] | 1132 | /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR |
| 1133 | * literally. */ |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 1134 | if (had_ctrl_v_cr || (c != '\r' && c != '\n')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1135 | { |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 1136 | if (has_mbyte) |
| 1137 | { |
| 1138 | n = (int)STRLEN(newp); |
| 1139 | while (--num_chars >= 0) |
| 1140 | n += (*mb_char2bytes)(c, newp + n); |
| 1141 | } |
| 1142 | else |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 1143 | vim_memset(newp + STRLEN(newp), c, (size_t)numc); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 1144 | if (!bd.is_short) |
| 1145 | { |
| 1146 | /* insert post-spaces */ |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 1147 | vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 1148 | /* copy the part after the changed part */ |
| 1149 | STRMOVE(newp + STRLEN(newp), oldp); |
| 1150 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1151 | } |
| 1152 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1153 | { |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 1154 | /* Replacing with \r or \n means splitting the line. */ |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1155 | after_p = alloc(oldlen + 1 + n - STRLEN(newp)); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 1156 | if (after_p != NULL) |
| 1157 | STRMOVE(after_p, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | } |
| 1159 | /* replace the line */ |
| 1160 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
Bram Moolenaar | d982053 | 2013-11-04 01:41:17 +0100 | [diff] [blame] | 1161 | if (after_p != NULL) |
| 1162 | { |
| 1163 | ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE); |
| 1164 | appended_lines_mark(curwin->w_cursor.lnum, 1L); |
| 1165 | oap->end.lnum++; |
| 1166 | vim_free(after_p); |
| 1167 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | else |
| 1171 | { |
| 1172 | /* |
| 1173 | * MCHAR and MLINE motion replace. |
| 1174 | */ |
| 1175 | if (oap->motion_type == MLINE) |
| 1176 | { |
| 1177 | oap->start.col = 0; |
| 1178 | curwin->w_cursor.col = 0; |
| 1179 | oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 1180 | if (oap->end.col) |
| 1181 | --oap->end.col; |
| 1182 | } |
| 1183 | else if (!oap->inclusive) |
| 1184 | dec(&(oap->end)); |
| 1185 | |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1186 | while (LTOREQ_POS(curwin->w_cursor, oap->end)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1187 | { |
| 1188 | n = gchar_cursor(); |
| 1189 | if (n != NUL) |
| 1190 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1191 | if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1) |
| 1192 | { |
| 1193 | /* This is slow, but it handles replacing a single-byte |
| 1194 | * with a multi-byte and the other way around. */ |
Bram Moolenaar | db81395 | 2013-03-07 18:50:57 +0100 | [diff] [blame] | 1195 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 1196 | oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n); |
Bram Moolenaar | 630afe8 | 2018-06-28 19:26:28 +0200 | [diff] [blame] | 1197 | replace_character(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1198 | } |
| 1199 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1200 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1201 | if (n == TAB) |
| 1202 | { |
| 1203 | int end_vcol = 0; |
| 1204 | |
| 1205 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 1206 | { |
| 1207 | /* oap->end has to be recalculated when |
| 1208 | * the tab breaks */ |
| 1209 | end_vcol = getviscol2(oap->end.col, |
| 1210 | oap->end.coladd); |
| 1211 | } |
| 1212 | coladvance_force(getviscol()); |
| 1213 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 1214 | getvpos(&oap->end, end_vcol); |
| 1215 | } |
Bram Moolenaar | 630afe8 | 2018-06-28 19:26:28 +0200 | [diff] [blame] | 1216 | PBYTE(curwin->w_cursor, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1217 | } |
| 1218 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1219 | else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum) |
| 1220 | { |
| 1221 | int virtcols = oap->end.coladd; |
| 1222 | |
| 1223 | if (curwin->w_cursor.lnum == oap->start.lnum |
| 1224 | && oap->start.col == oap->end.col && oap->start.coladd) |
| 1225 | virtcols -= oap->start.coladd; |
| 1226 | |
| 1227 | /* oap->end has been trimmed so it's effectively inclusive; |
| 1228 | * as a result an extra +1 must be counted so we don't |
| 1229 | * trample the NUL byte. */ |
| 1230 | coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1); |
| 1231 | curwin->w_cursor.col -= (virtcols + 1); |
| 1232 | for (; virtcols >= 0; virtcols--) |
| 1233 | { |
Bram Moolenaar | 630afe8 | 2018-06-28 19:26:28 +0200 | [diff] [blame] | 1234 | if ((*mb_char2len)(c) > 1) |
| 1235 | replace_character(c); |
| 1236 | else |
Bram Moolenaar | 630afe8 | 2018-06-28 19:26:28 +0200 | [diff] [blame] | 1237 | PBYTE(curwin->w_cursor, c); |
| 1238 | if (inc(&curwin->w_cursor) == -1) |
| 1239 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1240 | } |
| 1241 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1242 | |
| 1243 | /* Advance to next character, stop at the end of the file. */ |
| 1244 | if (inc_cursor() == -1) |
| 1245 | break; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | curwin->w_cursor = oap->start; |
| 1250 | check_cursor(); |
| 1251 | changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L); |
| 1252 | |
| 1253 | /* Set "'[" and "']" marks. */ |
| 1254 | curbuf->b_op_start = oap->start; |
| 1255 | curbuf->b_op_end = oap->end; |
| 1256 | |
| 1257 | return OK; |
| 1258 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1259 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 1260 | static int swapchars(int op_type, pos_T *pos, int length); |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 1261 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1262 | /* |
| 1263 | * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?". |
| 1264 | */ |
Bram Moolenaar | 792cf5e | 2019-09-30 23:12:16 +0200 | [diff] [blame] | 1265 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1266 | op_tilde(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1267 | { |
| 1268 | pos_T pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | struct block_def bd; |
Bram Moolenaar | e60c58d | 2008-02-06 13:44:43 +0000 | [diff] [blame] | 1270 | int did_change = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1271 | |
| 1272 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 1273 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 1274 | return; |
| 1275 | |
| 1276 | pos = oap->start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1277 | if (oap->block_mode) /* Visual block mode */ |
| 1278 | { |
| 1279 | for (; pos.lnum <= oap->end.lnum; ++pos.lnum) |
| 1280 | { |
Bram Moolenaar | 555b3d5 | 2008-12-03 12:38:36 +0000 | [diff] [blame] | 1281 | int one_change; |
| 1282 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1283 | block_prep(oap, &bd, pos.lnum, FALSE); |
| 1284 | pos.col = bd.textcol; |
Bram Moolenaar | 555b3d5 | 2008-12-03 12:38:36 +0000 | [diff] [blame] | 1285 | one_change = swapchars(oap->op_type, &pos, bd.textlen); |
| 1286 | did_change |= one_change; |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 1287 | |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1288 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 1289 | if (netbeans_active() && one_change) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1290 | { |
| 1291 | char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 1292 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1293 | netbeans_removed(curbuf, pos.lnum, bd.textcol, |
| 1294 | (long)bd.textlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1295 | netbeans_inserted(curbuf, pos.lnum, bd.textcol, |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 1296 | &ptr[bd.textcol], bd.textlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1297 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1298 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1299 | } |
| 1300 | if (did_change) |
| 1301 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
| 1302 | } |
| 1303 | else /* not block mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1304 | { |
| 1305 | if (oap->motion_type == MLINE) |
| 1306 | { |
| 1307 | oap->start.col = 0; |
| 1308 | pos.col = 0; |
| 1309 | oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 1310 | if (oap->end.col) |
| 1311 | --oap->end.col; |
| 1312 | } |
| 1313 | else if (!oap->inclusive) |
| 1314 | dec(&(oap->end)); |
| 1315 | |
Bram Moolenaar | e60c58d | 2008-02-06 13:44:43 +0000 | [diff] [blame] | 1316 | if (pos.lnum == oap->end.lnum) |
| 1317 | did_change = swapchars(oap->op_type, &pos, |
| 1318 | oap->end.col - pos.col + 1); |
| 1319 | else |
| 1320 | for (;;) |
| 1321 | { |
| 1322 | did_change |= swapchars(oap->op_type, &pos, |
| 1323 | pos.lnum == oap->end.lnum ? oap->end.col + 1: |
| 1324 | (int)STRLEN(ml_get_pos(&pos))); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1325 | if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1) |
Bram Moolenaar | e60c58d | 2008-02-06 13:44:43 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1328 | if (did_change) |
| 1329 | { |
| 1330 | changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, |
| 1331 | 0L); |
| 1332 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 1333 | if (netbeans_active() && did_change) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1334 | { |
| 1335 | char_u *ptr; |
| 1336 | int count; |
| 1337 | |
| 1338 | pos = oap->start; |
| 1339 | while (pos.lnum < oap->end.lnum) |
| 1340 | { |
| 1341 | ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1342 | count = (int)STRLEN(ptr) - pos.col; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1343 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1344 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 1345 | &ptr[pos.col], count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1346 | pos.col = 0; |
| 1347 | pos.lnum++; |
| 1348 | } |
| 1349 | ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 1350 | count = oap->end.col - pos.col + 1; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1351 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1352 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 1353 | &ptr[pos.col], count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1354 | } |
| 1355 | #endif |
| 1356 | } |
| 1357 | } |
| 1358 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1359 | if (!did_change && oap->is_VIsual) |
| 1360 | /* No change: need to remove the Visual selection */ |
| 1361 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1362 | |
| 1363 | /* |
| 1364 | * Set '[ and '] marks. |
| 1365 | */ |
| 1366 | curbuf->b_op_start = oap->start; |
| 1367 | curbuf->b_op_end = oap->end; |
| 1368 | |
| 1369 | if (oap->line_count > p_report) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1370 | smsg(NGETTEXT("%ld line changed", "%ld lines changed", |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 1371 | oap->line_count), oap->line_count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /* |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 1375 | * Invoke swapchar() on "length" bytes at position "pos". |
| 1376 | * "pos" is advanced to just after the changed characters. |
| 1377 | * "length" is rounded up to include the whole last multi-byte character. |
| 1378 | * Also works correctly when the number of bytes changes. |
| 1379 | * Returns TRUE if some character was changed. |
| 1380 | */ |
| 1381 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1382 | swapchars(int op_type, pos_T *pos, int length) |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 1383 | { |
| 1384 | int todo; |
| 1385 | int did_change = 0; |
| 1386 | |
| 1387 | for (todo = length; todo > 0; --todo) |
| 1388 | { |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 1389 | if (has_mbyte) |
Bram Moolenaar | f17968b | 2013-08-09 19:48:40 +0200 | [diff] [blame] | 1390 | { |
| 1391 | int len = (*mb_ptr2len)(ml_get_pos(pos)); |
| 1392 | |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 1393 | /* we're counting bytes, not characters */ |
Bram Moolenaar | f17968b | 2013-08-09 19:48:40 +0200 | [diff] [blame] | 1394 | if (len > 0) |
| 1395 | todo -= len - 1; |
| 1396 | } |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 1397 | did_change |= swapchar(op_type, pos); |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 1398 | if (inc(pos) == -1) /* at end of file */ |
| 1399 | break; |
| 1400 | } |
| 1401 | return did_change; |
| 1402 | } |
| 1403 | |
| 1404 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1405 | * If op_type == OP_UPPER: make uppercase, |
| 1406 | * if op_type == OP_LOWER: make lowercase, |
| 1407 | * if op_type == OP_ROT13: do rot13 encoding, |
| 1408 | * else swap case of character at 'pos' |
| 1409 | * returns TRUE when something actually changed. |
| 1410 | */ |
| 1411 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1412 | swapchar(int op_type, pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1413 | { |
| 1414 | int c; |
| 1415 | int nc; |
| 1416 | |
| 1417 | c = gchar_pos(pos); |
| 1418 | |
| 1419 | /* Only do rot13 encoding for ASCII characters. */ |
| 1420 | if (c >= 0x80 && op_type == OP_ROT13) |
| 1421 | return FALSE; |
| 1422 | |
Bram Moolenaar | b44df0a | 2008-01-22 15:02:31 +0000 | [diff] [blame] | 1423 | if (op_type == OP_UPPER && c == 0xdf |
| 1424 | && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0)) |
Bram Moolenaar | 6f16eb8 | 2005-08-23 21:02:42 +0000 | [diff] [blame] | 1425 | { |
| 1426 | pos_T sp = curwin->w_cursor; |
| 1427 | |
| 1428 | /* Special handling of German sharp s: change to "SS". */ |
| 1429 | curwin->w_cursor = *pos; |
| 1430 | del_char(FALSE); |
| 1431 | ins_char('S'); |
| 1432 | ins_char('S'); |
| 1433 | curwin->w_cursor = sp; |
| 1434 | inc(pos); |
| 1435 | } |
| 1436 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1437 | if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */ |
| 1438 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1439 | nc = c; |
| 1440 | if (MB_ISLOWER(c)) |
| 1441 | { |
| 1442 | if (op_type == OP_ROT13) |
| 1443 | nc = ROT13(c, 'a'); |
| 1444 | else if (op_type != OP_LOWER) |
| 1445 | nc = MB_TOUPPER(c); |
| 1446 | } |
| 1447 | else if (MB_ISUPPER(c)) |
| 1448 | { |
| 1449 | if (op_type == OP_ROT13) |
| 1450 | nc = ROT13(c, 'A'); |
| 1451 | else if (op_type != OP_UPPER) |
| 1452 | nc = MB_TOLOWER(c); |
| 1453 | } |
| 1454 | if (nc != c) |
| 1455 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1456 | if (enc_utf8 && (c >= 0x80 || nc >= 0x80)) |
| 1457 | { |
| 1458 | pos_T sp = curwin->w_cursor; |
| 1459 | |
| 1460 | curwin->w_cursor = *pos; |
Bram Moolenaar | d1cb65e | 2010-08-01 14:22:48 +0200 | [diff] [blame] | 1461 | /* don't use del_char(), it also removes composing chars */ |
| 1462 | del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1463 | ins_char(nc); |
| 1464 | curwin->w_cursor = sp; |
| 1465 | } |
| 1466 | else |
Bram Moolenaar | 630afe8 | 2018-06-28 19:26:28 +0200 | [diff] [blame] | 1467 | PBYTE(*pos, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1468 | return TRUE; |
| 1469 | } |
| 1470 | return FALSE; |
| 1471 | } |
| 1472 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1473 | /* |
| 1474 | * op_insert - Insert and append operators for Visual mode. |
| 1475 | */ |
| 1476 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1477 | op_insert(oparg_T *oap, long count1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1478 | { |
| 1479 | long ins_len, pre_textlen = 0; |
| 1480 | char_u *firstline, *ins_text; |
Bram Moolenaar | 2254a8a | 2017-09-03 14:03:43 +0200 | [diff] [blame] | 1481 | colnr_T ind_pre = 0, ind_post; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1482 | struct block_def bd; |
| 1483 | int i; |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 1484 | pos_T t1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* edit() changes this - record it for OP_APPEND */ |
| 1487 | bd.is_MAX = (curwin->w_curswant == MAXCOL); |
| 1488 | |
| 1489 | /* vis block is still marked. Get rid of it now. */ |
| 1490 | curwin->w_cursor.lnum = oap->start.lnum; |
| 1491 | update_screen(INVERTED); |
| 1492 | |
| 1493 | if (oap->block_mode) |
| 1494 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1495 | /* When 'virtualedit' is used, need to insert the extra spaces before |
| 1496 | * doing block_prep(). When only "block" is used, virtual edit is |
| 1497 | * already disabled, but still need it when calling |
| 1498 | * coladvance_force(). */ |
| 1499 | if (curwin->w_cursor.coladd > 0) |
| 1500 | { |
| 1501 | int old_ve_flags = ve_flags; |
| 1502 | |
| 1503 | ve_flags = VE_ALL; |
| 1504 | if (u_save_cursor() == FAIL) |
| 1505 | return; |
| 1506 | coladvance_force(oap->op_type == OP_APPEND |
| 1507 | ? oap->end_vcol + 1 : getviscol()); |
| 1508 | if (oap->op_type == OP_APPEND) |
| 1509 | --curwin->w_cursor.col; |
| 1510 | ve_flags = old_ve_flags; |
| 1511 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1512 | /* Get the info about the block before entering the text */ |
| 1513 | block_prep(oap, &bd, oap->start.lnum, TRUE); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1514 | /* Get indent information */ |
| 1515 | ind_pre = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1516 | firstline = ml_get(oap->start.lnum) + bd.textcol; |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1517 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1518 | if (oap->op_type == OP_APPEND) |
| 1519 | firstline += bd.textlen; |
| 1520 | pre_textlen = (long)STRLEN(firstline); |
| 1521 | } |
| 1522 | |
| 1523 | if (oap->op_type == OP_APPEND) |
| 1524 | { |
Bram Moolenaar | 29ddebe | 2019-01-26 17:28:26 +0100 | [diff] [blame] | 1525 | if (oap->block_mode && curwin->w_cursor.coladd == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1526 | { |
| 1527 | /* Move the cursor to the character right of the block. */ |
| 1528 | curwin->w_set_curswant = TRUE; |
| 1529 | while (*ml_get_cursor() != NUL |
| 1530 | && (curwin->w_cursor.col < bd.textcol + bd.textlen)) |
| 1531 | ++curwin->w_cursor.col; |
| 1532 | if (bd.is_short && !bd.is_MAX) |
| 1533 | { |
| 1534 | /* First line was too short, make it longer and adjust the |
| 1535 | * values in "bd". */ |
| 1536 | if (u_save_cursor() == FAIL) |
| 1537 | return; |
| 1538 | for (i = 0; i < bd.endspaces; ++i) |
| 1539 | ins_char(' '); |
| 1540 | bd.textlen += bd.endspaces; |
| 1541 | } |
| 1542 | } |
| 1543 | else |
| 1544 | { |
| 1545 | curwin->w_cursor = oap->end; |
Bram Moolenaar | 6a584dc | 2006-06-20 18:29:34 +0000 | [diff] [blame] | 1546 | check_cursor_col(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1547 | |
| 1548 | /* Works just like an 'i'nsert on the next character. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1549 | if (!LINEEMPTY(curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1550 | && oap->start_vcol != oap->end_vcol) |
| 1551 | inc_cursor(); |
| 1552 | } |
| 1553 | } |
| 1554 | |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 1555 | t1 = oap->start; |
Bram Moolenaar | 23fa81d | 2017-02-01 21:50:21 +0100 | [diff] [blame] | 1556 | (void)edit(NUL, FALSE, (linenr_T)count1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1557 | |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 1558 | /* When a tab was inserted, and the characters in front of the tab |
| 1559 | * have been converted to a tab as well, the column of the cursor |
| 1560 | * might have actually been reduced, so need to adjust here. */ |
| 1561 | if (t1.lnum == curbuf->b_op_start_orig.lnum |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1562 | && LT_POS(curbuf->b_op_start_orig, t1)) |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 1563 | oap->start = curbuf->b_op_start_orig; |
| 1564 | |
Bram Moolenaar | aacbb00 | 2008-01-03 15:34:40 +0000 | [diff] [blame] | 1565 | /* If user has moved off this line, we don't know what to do, so do |
| 1566 | * nothing. |
| 1567 | * Also don't repeat the insert when Insert mode ended with CTRL-C. */ |
| 1568 | if (curwin->w_cursor.lnum != oap->start.lnum || got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1569 | return; |
| 1570 | |
| 1571 | if (oap->block_mode) |
| 1572 | { |
| 1573 | struct block_def bd2; |
Bram Moolenaar | 8c87a2b | 2018-04-10 13:15:47 +0200 | [diff] [blame] | 1574 | int did_indent = FALSE; |
Bram Moolenaar | 35e802e | 2018-04-30 17:21:03 +0200 | [diff] [blame] | 1575 | size_t len; |
| 1576 | int add; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1577 | |
Bram Moolenaar | 4ec86dd | 2017-09-02 23:28:54 +0200 | [diff] [blame] | 1578 | /* If indent kicked in, the firstline might have changed |
| 1579 | * but only do that, if the indent actually increased. */ |
| 1580 | ind_post = (colnr_T)getwhitecols_curline(); |
| 1581 | if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre) |
| 1582 | { |
| 1583 | bd.textcol += ind_post - ind_pre; |
| 1584 | bd.start_vcol += ind_post - ind_pre; |
Bram Moolenaar | 8c87a2b | 2018-04-10 13:15:47 +0200 | [diff] [blame] | 1585 | did_indent = TRUE; |
Bram Moolenaar | 4ec86dd | 2017-09-02 23:28:54 +0200 | [diff] [blame] | 1586 | } |
| 1587 | |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 1588 | /* The user may have moved the cursor before inserting something, try |
Bram Moolenaar | 8c87a2b | 2018-04-10 13:15:47 +0200 | [diff] [blame] | 1589 | * to adjust the block for that. But only do it, if the difference |
| 1590 | * does not come from indent kicking in. */ |
| 1591 | if (oap->start.lnum == curbuf->b_op_start_orig.lnum |
| 1592 | && !bd.is_MAX && !did_indent) |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 1593 | { |
| 1594 | if (oap->op_type == OP_INSERT |
Bram Moolenaar | 29ddebe | 2019-01-26 17:28:26 +0100 | [diff] [blame] | 1595 | && oap->start.col + oap->start.coladd |
Bram Moolenaar | 4c9a949 | 2014-03-19 18:57:54 +0100 | [diff] [blame] | 1596 | != curbuf->b_op_start_orig.col |
Bram Moolenaar | 29ddebe | 2019-01-26 17:28:26 +0100 | [diff] [blame] | 1597 | + curbuf->b_op_start_orig.coladd) |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 1598 | { |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 1599 | int t = getviscol2(curbuf->b_op_start_orig.col, |
Bram Moolenaar | 29ddebe | 2019-01-26 17:28:26 +0100 | [diff] [blame] | 1600 | curbuf->b_op_start_orig.coladd); |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 1601 | oap->start.col = curbuf->b_op_start_orig.col; |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 1602 | pre_textlen -= t - oap->start_vcol; |
| 1603 | oap->start_vcol = t; |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 1604 | } |
| 1605 | else if (oap->op_type == OP_APPEND |
Bram Moolenaar | 29ddebe | 2019-01-26 17:28:26 +0100 | [diff] [blame] | 1606 | && oap->end.col + oap->end.coladd |
Bram Moolenaar | 4c9a949 | 2014-03-19 18:57:54 +0100 | [diff] [blame] | 1607 | >= curbuf->b_op_start_orig.col |
Bram Moolenaar | 29ddebe | 2019-01-26 17:28:26 +0100 | [diff] [blame] | 1608 | + curbuf->b_op_start_orig.coladd) |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 1609 | { |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 1610 | int t = getviscol2(curbuf->b_op_start_orig.col, |
Bram Moolenaar | 29ddebe | 2019-01-26 17:28:26 +0100 | [diff] [blame] | 1611 | curbuf->b_op_start_orig.coladd); |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 1612 | oap->start.col = curbuf->b_op_start_orig.col; |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 1613 | /* reset pre_textlen to the value of OP_INSERT */ |
| 1614 | pre_textlen += bd.textlen; |
Bram Moolenaar | f2c03d7 | 2015-02-03 18:36:44 +0100 | [diff] [blame] | 1615 | pre_textlen -= t - oap->start_vcol; |
| 1616 | oap->start_vcol = t; |
Bram Moolenaar | 3f75e42 | 2013-11-11 01:29:22 +0100 | [diff] [blame] | 1617 | oap->op_type = OP_INSERT; |
| 1618 | } |
| 1619 | } |
| 1620 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1621 | /* |
| 1622 | * 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] | 1623 | * tabs. Get the starting column again and correct the length. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1624 | * Don't do this when "$" used, end-of-line will have changed. |
| 1625 | */ |
| 1626 | block_prep(oap, &bd2, oap->start.lnum, TRUE); |
| 1627 | if (!bd.is_MAX || bd2.textlen < bd.textlen) |
| 1628 | { |
| 1629 | if (oap->op_type == OP_APPEND) |
| 1630 | { |
| 1631 | pre_textlen += bd2.textlen - bd.textlen; |
| 1632 | if (bd2.endspaces) |
| 1633 | --bd2.textlen; |
| 1634 | } |
| 1635 | bd.textcol = bd2.textcol; |
| 1636 | bd.textlen = bd2.textlen; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * Subsequent calls to ml_get() flush the firstline data - take a |
| 1641 | * copy of the required string. |
| 1642 | */ |
Bram Moolenaar | 35e802e | 2018-04-30 17:21:03 +0200 | [diff] [blame] | 1643 | firstline = ml_get(oap->start.lnum); |
| 1644 | len = STRLEN(firstline); |
| 1645 | add = bd.textcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1646 | if (oap->op_type == OP_APPEND) |
Bram Moolenaar | 35e802e | 2018-04-30 17:21:03 +0200 | [diff] [blame] | 1647 | add += bd.textlen; |
| 1648 | if ((size_t)add > len) |
| 1649 | firstline += len; // short line, point to the NUL |
| 1650 | else |
| 1651 | firstline += add; |
Bram Moolenaar | 5e4b9e9 | 2012-03-23 14:16:23 +0100 | [diff] [blame] | 1652 | if (pre_textlen >= 0 |
| 1653 | && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1654 | { |
| 1655 | ins_text = vim_strnsave(firstline, (int)ins_len); |
| 1656 | if (ins_text != NULL) |
| 1657 | { |
| 1658 | /* block handled here */ |
| 1659 | if (u_save(oap->start.lnum, |
| 1660 | (linenr_T)(oap->end.lnum + 1)) == OK) |
| 1661 | block_insert(oap, ins_text, (oap->op_type == OP_INSERT), |
| 1662 | &bd); |
| 1663 | |
| 1664 | curwin->w_cursor.col = oap->start.col; |
| 1665 | check_cursor(); |
| 1666 | vim_free(ins_text); |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1671 | |
| 1672 | /* |
| 1673 | * op_change - handle a change operation |
| 1674 | * |
| 1675 | * return TRUE if edit() returns because of a CTRL-O command |
| 1676 | */ |
| 1677 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1678 | op_change(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1679 | { |
| 1680 | colnr_T l; |
| 1681 | int retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1682 | long offset; |
| 1683 | linenr_T linenr; |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 1684 | long ins_len; |
| 1685 | long pre_textlen = 0; |
| 1686 | long pre_indent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1687 | char_u *firstline; |
| 1688 | char_u *ins_text, *newp, *oldp; |
| 1689 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1690 | |
| 1691 | l = oap->start.col; |
| 1692 | if (oap->motion_type == MLINE) |
| 1693 | { |
| 1694 | l = 0; |
| 1695 | #ifdef FEAT_SMARTINDENT |
| 1696 | if (!p_paste && curbuf->b_p_si |
| 1697 | # ifdef FEAT_CINDENT |
| 1698 | && !curbuf->b_p_cin |
| 1699 | # endif |
| 1700 | ) |
| 1701 | can_si = TRUE; /* It's like opening a new line, do si */ |
| 1702 | #endif |
| 1703 | } |
| 1704 | |
| 1705 | /* First delete the text in the region. In an empty buffer only need to |
| 1706 | * save for undo */ |
| 1707 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 1708 | { |
| 1709 | if (u_save_cursor() == FAIL) |
| 1710 | return FALSE; |
| 1711 | } |
| 1712 | else if (op_delete(oap) == FAIL) |
| 1713 | return FALSE; |
| 1714 | |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1715 | if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1716 | && !virtual_op) |
| 1717 | inc_cursor(); |
| 1718 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1719 | /* check for still on same line (<CR> in inserted text meaningless) */ |
| 1720 | /* skip blank lines too */ |
| 1721 | if (oap->block_mode) |
| 1722 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1723 | /* Add spaces before getting the current line length. */ |
| 1724 | if (virtual_op && (curwin->w_cursor.coladd > 0 |
| 1725 | || gchar_cursor() == NUL)) |
| 1726 | coladvance_force(getviscol()); |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 1727 | firstline = ml_get(oap->start.lnum); |
| 1728 | pre_textlen = (long)STRLEN(firstline); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1729 | pre_indent = (long)getwhitecols(firstline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1730 | bd.textcol = curwin->w_cursor.col; |
| 1731 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1732 | |
| 1733 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
| 1734 | if (oap->motion_type == MLINE) |
| 1735 | fix_indent(); |
| 1736 | #endif |
| 1737 | |
| 1738 | retval = edit(NUL, FALSE, (linenr_T)1); |
| 1739 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1740 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1741 | * 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] | 1742 | * block. |
Bram Moolenaar | aacbb00 | 2008-01-03 15:34:40 +0000 | [diff] [blame] | 1743 | * Don't repeat the insert when Insert mode ended with CTRL-C. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1744 | */ |
Bram Moolenaar | aacbb00 | 2008-01-03 15:34:40 +0000 | [diff] [blame] | 1745 | if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1746 | { |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 1747 | /* Auto-indenting may have changed the indent. If the cursor was past |
| 1748 | * the indent, exclude that indent change from the inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1749 | firstline = ml_get(oap->start.lnum); |
Bram Moolenaar | b8dc4d4 | 2007-09-25 12:20:19 +0000 | [diff] [blame] | 1750 | if (bd.textcol > (colnr_T)pre_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1751 | { |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1752 | long new_indent = (long)getwhitecols(firstline); |
Bram Moolenaar | 53241da | 2007-09-13 20:41:32 +0000 | [diff] [blame] | 1753 | |
| 1754 | pre_textlen += new_indent - pre_indent; |
| 1755 | bd.textcol += new_indent - pre_indent; |
| 1756 | } |
| 1757 | |
| 1758 | ins_len = (long)STRLEN(firstline) - pre_textlen; |
| 1759 | if (ins_len > 0) |
| 1760 | { |
| 1761 | /* Subsequent calls to ml_get() flush the firstline data - take a |
| 1762 | * copy of the inserted text. */ |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1763 | if ((ins_text = alloc(ins_len + 1)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1764 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 1765 | vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1766 | for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum; |
| 1767 | linenr++) |
| 1768 | { |
| 1769 | block_prep(oap, &bd, linenr, TRUE); |
| 1770 | if (!bd.is_short || virtual_op) |
| 1771 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1772 | pos_T vpos; |
| 1773 | |
| 1774 | /* If the block starts in virtual space, count the |
| 1775 | * initial coladd offset as part of "startspaces" */ |
| 1776 | if (bd.is_short) |
| 1777 | { |
Bram Moolenaar | a1381de | 2009-11-03 15:44:21 +0000 | [diff] [blame] | 1778 | vpos.lnum = linenr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1779 | (void)getvpos(&vpos, oap->start_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1780 | } |
| 1781 | else |
| 1782 | vpos.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1783 | oldp = ml_get(linenr); |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1784 | newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1785 | if (newp == NULL) |
| 1786 | continue; |
| 1787 | /* copy up to block start */ |
| 1788 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 1789 | offset = bd.textcol; |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 1790 | vim_memset(newp + offset, ' ', (size_t)vpos.coladd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1791 | offset += vpos.coladd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1792 | mch_memmove(newp + offset, ins_text, (size_t)ins_len); |
| 1793 | offset += ins_len; |
| 1794 | oldp += bd.textcol; |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 1795 | STRMOVE(newp + offset, oldp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1796 | ml_replace(linenr, newp, FALSE); |
| 1797 | } |
| 1798 | } |
| 1799 | check_cursor(); |
| 1800 | |
| 1801 | changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); |
| 1802 | } |
| 1803 | vim_free(ins_text); |
| 1804 | } |
| 1805 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1806 | |
| 1807 | return retval; |
| 1808 | } |
| 1809 | |
| 1810 | /* |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 1811 | * When the cursor is on the NUL past the end of the line and it should not be |
| 1812 | * there move it left. |
| 1813 | */ |
| 1814 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1815 | adjust_cursor_eol(void) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 1816 | { |
| 1817 | if (curwin->w_cursor.col > 0 |
| 1818 | && gchar_cursor() == NUL |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 1819 | && (ve_flags & VE_ONEMORE) == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1820 | && !(restart_edit || (State & INSERT))) |
| 1821 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1822 | /* Put the cursor on the last character in the line. */ |
Bram Moolenaar | a5fac54 | 2005-10-12 20:58:49 +0000 | [diff] [blame] | 1823 | dec_cursor(); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 1824 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1825 | if (ve_flags == VE_ALL) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1826 | { |
| 1827 | colnr_T scol, ecol; |
| 1828 | |
| 1829 | /* Coladd is set to the width of the last character. */ |
| 1830 | getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol); |
| 1831 | curwin->w_cursor.coladd = ecol - scol + 1; |
| 1832 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1833 | } |
| 1834 | } |
| 1835 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1836 | /* |
| 1837 | * If "process" is TRUE and the line begins with a comment leader (possibly |
| 1838 | * after some white space), return a pointer to the text after it. Put a boolean |
| 1839 | * value indicating whether the line ends with an unclosed comment in |
| 1840 | * "is_comment". |
| 1841 | * line - line to be processed, |
| 1842 | * 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] | 1843 | * comment, |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1844 | * include_space - whether to also skip space following the comment leader, |
| 1845 | * is_comment - will indicate whether the current line ends with an unclosed |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1846 | * comment. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1847 | */ |
Bram Moolenaar | 025a6b7 | 2017-03-12 20:37:21 +0100 | [diff] [blame] | 1848 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1849 | skip_comment( |
| 1850 | char_u *line, |
| 1851 | int process, |
| 1852 | int include_space, |
| 1853 | int *is_comment) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1854 | { |
| 1855 | char_u *comment_flags = NULL; |
| 1856 | int lead_len; |
| 1857 | int leader_offset = get_last_leader_offset(line, &comment_flags); |
| 1858 | |
| 1859 | *is_comment = FALSE; |
| 1860 | if (leader_offset != -1) |
| 1861 | { |
| 1862 | /* Let's check whether the line ends with an unclosed comment. |
| 1863 | * If the last comment leader has COM_END in flags, there's no comment. |
| 1864 | */ |
| 1865 | while (*comment_flags) |
| 1866 | { |
| 1867 | if (*comment_flags == COM_END |
| 1868 | || *comment_flags == ':') |
| 1869 | break; |
| 1870 | ++comment_flags; |
| 1871 | } |
| 1872 | if (*comment_flags != COM_END) |
| 1873 | *is_comment = TRUE; |
| 1874 | } |
| 1875 | |
| 1876 | if (process == FALSE) |
| 1877 | return line; |
| 1878 | |
| 1879 | lead_len = get_leader_len(line, &comment_flags, FALSE, include_space); |
| 1880 | |
| 1881 | if (lead_len == 0) |
| 1882 | return line; |
| 1883 | |
| 1884 | /* Find: |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1885 | * - COM_END, |
| 1886 | * - colon, |
| 1887 | * whichever comes first. |
| 1888 | */ |
| 1889 | while (*comment_flags) |
| 1890 | { |
Bram Moolenaar | e04a48f | 2012-06-13 14:01:41 +0200 | [diff] [blame] | 1891 | if (*comment_flags == COM_END |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1892 | || *comment_flags == ':') |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1893 | break; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1894 | ++comment_flags; |
| 1895 | } |
| 1896 | |
| 1897 | /* 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] | 1898 | * starting with a closing part of a three-part comment. That's good, |
| 1899 | * 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] | 1900 | */ |
| 1901 | if (*comment_flags == ':' || *comment_flags == NUL) |
| 1902 | line += lead_len; |
| 1903 | |
| 1904 | return line; |
| 1905 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1906 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1907 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1908 | * Join 'count' lines (minimal 2) at cursor position. |
| 1909 | * When "save_undo" is TRUE save lines for undo first. |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 1910 | * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment |
| 1911 | * leaders should not be removed. |
| 1912 | * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected |
| 1913 | * to set those marks. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1914 | * |
Bram Moolenaar | 10c5695 | 2007-05-10 18:38:52 +0000 | [diff] [blame] | 1915 | * return FAIL for failure, OK otherwise |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1916 | */ |
| 1917 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1918 | do_join( |
| 1919 | long count, |
| 1920 | int insert_space, |
| 1921 | int save_undo, |
| 1922 | int use_formatoptions UNUSED, |
| 1923 | int setmark) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1924 | { |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1925 | char_u *curr = NULL; |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 1926 | char_u *curr_start = NULL; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1927 | char_u *cend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1928 | char_u *newp; |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 1929 | char_u *spaces; /* number of spaces inserted before a line */ |
Bram Moolenaar | d43848c | 2010-07-14 14:28:26 +0200 | [diff] [blame] | 1930 | int endcurr1 = NUL; |
| 1931 | int endcurr2 = NUL; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1932 | int currsize = 0; /* size of the current line */ |
| 1933 | int sumsize = 0; /* size of the long new line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1934 | linenr_T t; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1935 | colnr_T col = 0; |
| 1936 | int ret = OK; |
Bram Moolenaar | 802053f | 2012-06-06 23:08:38 +0200 | [diff] [blame] | 1937 | int *comments = NULL; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1938 | int remove_comments = (use_formatoptions == TRUE) |
| 1939 | && has_format_option(FO_REMOVE_COMS); |
| 1940 | int prev_was_comment; |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 1941 | #ifdef FEAT_TEXT_PROP |
| 1942 | textprop_T **prop_lines = NULL; |
| 1943 | int *prop_lengths = NULL; |
| 1944 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1945 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1946 | if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 1947 | (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL) |
| 1948 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1949 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1950 | /* Allocate an array to store the number of spaces inserted before each |
| 1951 | * line. We will use it to pre-compute the length of the new line and the |
| 1952 | * proper placement of each original line in the new one. */ |
Bram Moolenaar | 18a4ba2 | 2019-05-24 19:39:03 +0200 | [diff] [blame] | 1953 | spaces = lalloc_clear(count, TRUE); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1954 | if (spaces == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1955 | return FAIL; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1956 | if (remove_comments) |
| 1957 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 1958 | comments = lalloc_clear(count * sizeof(int), TRUE); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1959 | if (comments == NULL) |
| 1960 | { |
| 1961 | vim_free(spaces); |
| 1962 | return FAIL; |
| 1963 | } |
| 1964 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1965 | |
| 1966 | /* |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 1967 | * Don't move anything yet, just compute the final line length |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1968 | * and setup the array of space strings lengths |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 1969 | * This loops forward over the joined lines. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1970 | */ |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1971 | for (t = 0; t < count; ++t) |
| 1972 | { |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 1973 | curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t)); |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 1974 | if (t == 0 && setmark) |
Bram Moolenaar | f92d8a2 | 2014-02-11 19:33:07 +0100 | [diff] [blame] | 1975 | { |
| 1976 | /* Set the '[ mark. */ |
| 1977 | curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum; |
| 1978 | curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr); |
| 1979 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1980 | if (remove_comments) |
| 1981 | { |
| 1982 | /* We don't want to remove the comment leader if the |
| 1983 | * previous line is not a comment. */ |
| 1984 | if (t > 0 && prev_was_comment) |
| 1985 | { |
| 1986 | |
| 1987 | char_u *new_curr = skip_comment(curr, TRUE, insert_space, |
| 1988 | &prev_was_comment); |
Bram Moolenaar | 27ba088 | 2012-06-07 21:09:39 +0200 | [diff] [blame] | 1989 | comments[t] = (int)(new_curr - curr); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1990 | curr = new_curr; |
| 1991 | } |
| 1992 | else |
| 1993 | curr = skip_comment(curr, FALSE, insert_space, |
| 1994 | &prev_was_comment); |
| 1995 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1996 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 1997 | if (insert_space && t > 0) |
| 1998 | { |
| 1999 | curr = skipwhite(curr); |
| 2000 | if (*curr != ')' && currsize != 0 && endcurr1 != TAB |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2001 | && (!has_format_option(FO_MBYTE_JOIN) |
| 2002 | || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100)) |
| 2003 | && (!has_format_option(FO_MBYTE_JOIN2) |
| 2004 | || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100) |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2005 | ) |
| 2006 | { |
| 2007 | /* don't add a space if the line is ending in a space */ |
| 2008 | if (endcurr1 == ' ') |
| 2009 | endcurr1 = endcurr2; |
| 2010 | else |
| 2011 | ++spaces[t]; |
| 2012 | /* extra space when 'joinspaces' set and line ends in '.' */ |
| 2013 | if ( p_js |
| 2014 | && (endcurr1 == '.' |
| 2015 | || (vim_strchr(p_cpo, CPO_JOINSP) == NULL |
| 2016 | && (endcurr1 == '?' || endcurr1 == '!')))) |
| 2017 | ++spaces[t]; |
| 2018 | } |
| 2019 | } |
| 2020 | currsize = (int)STRLEN(curr); |
| 2021 | sumsize += currsize + spaces[t]; |
| 2022 | endcurr1 = endcurr2 = NUL; |
| 2023 | if (insert_space && currsize > 0) |
| 2024 | { |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2025 | if (has_mbyte) |
| 2026 | { |
| 2027 | cend = curr + currsize; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 2028 | MB_PTR_BACK(curr, cend); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2029 | endcurr1 = (*mb_ptr2char)(cend); |
| 2030 | if (cend > curr) |
| 2031 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 2032 | MB_PTR_BACK(curr, cend); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2033 | endcurr2 = (*mb_ptr2char)(cend); |
| 2034 | } |
| 2035 | } |
| 2036 | else |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2037 | { |
| 2038 | endcurr1 = *(curr + currsize - 1); |
| 2039 | if (currsize > 1) |
| 2040 | endcurr2 = *(curr + currsize - 2); |
| 2041 | } |
| 2042 | } |
| 2043 | line_breakcheck(); |
| 2044 | if (got_int) |
| 2045 | { |
| 2046 | ret = FAIL; |
| 2047 | goto theend; |
| 2048 | } |
| 2049 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2050 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2051 | /* store the column position before last line */ |
| 2052 | col = sumsize - currsize - spaces[count - 1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2053 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2054 | /* allocate the space for the new line */ |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 2055 | newp = alloc(sumsize + 1); |
Bram Moolenaar | 6f10c70 | 2019-08-20 22:58:37 +0200 | [diff] [blame] | 2056 | if (newp == NULL) |
| 2057 | { |
| 2058 | ret = FAIL; |
| 2059 | goto theend; |
| 2060 | } |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2061 | cend = newp + sumsize; |
| 2062 | *cend = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2063 | |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2064 | #ifdef FEAT_TEXT_PROP |
| 2065 | // We need to move properties of the lines that are going to be deleted to |
| 2066 | // the new long one. |
| 2067 | if (curbuf->b_has_textprop && !text_prop_frozen) |
| 2068 | { |
| 2069 | // Allocate an array to copy the text properties of joined lines into. |
| 2070 | // And another array to store the number of properties in each line. |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 2071 | prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1); |
| 2072 | prop_lengths = ALLOC_CLEAR_MULT(int, count - 1); |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2073 | if (prop_lengths == NULL) |
| 2074 | VIM_CLEAR(prop_lines); |
| 2075 | } |
| 2076 | #endif |
| 2077 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2078 | /* |
| 2079 | * Move affected lines to the new long one. |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2080 | * This loops backwards over the joined lines, including the original line. |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2081 | * |
| 2082 | * Move marks from each deleted line to the joined line, adjusting the |
| 2083 | * column. This is not Vi compatible, but Vi deletes the marks, thus that |
| 2084 | * should not really be a problem. |
| 2085 | */ |
| 2086 | for (t = count - 1; ; --t) |
| 2087 | { |
Bram Moolenaar | e1e714e | 2018-12-31 23:58:24 +0100 | [diff] [blame] | 2088 | int spaces_removed; |
| 2089 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2090 | cend -= currsize; |
| 2091 | mch_memmove(cend, curr, (size_t)currsize); |
| 2092 | if (spaces[t] > 0) |
| 2093 | { |
| 2094 | cend -= spaces[t]; |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 2095 | vim_memset(cend, ' ', (size_t)(spaces[t])); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2096 | } |
Bram Moolenaar | e1e714e | 2018-12-31 23:58:24 +0100 | [diff] [blame] | 2097 | |
| 2098 | // If deleting more spaces than adding, the cursor moves no more than |
| 2099 | // what is added if it is inside these spaces. |
| 2100 | spaces_removed = (curr - curr_start) - spaces[t]; |
| 2101 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2102 | mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t, |
Bram Moolenaar | e1e714e | 2018-12-31 23:58:24 +0100 | [diff] [blame] | 2103 | (long)(cend - newp - spaces_removed), spaces_removed); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2104 | if (t == 0) |
| 2105 | break; |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2106 | #ifdef FEAT_TEXT_PROP |
| 2107 | if (prop_lines != NULL) |
| 2108 | adjust_props_for_join(curwin->w_cursor.lnum + t, |
| 2109 | prop_lines + t - 1, prop_lengths + t - 1, |
| 2110 | (long)(cend - newp - spaces_removed), spaces_removed); |
| 2111 | #endif |
| 2112 | |
Bram Moolenaar | 341ad7a | 2010-10-09 17:23:31 +0200 | [diff] [blame] | 2113 | 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] | 2114 | if (remove_comments) |
| 2115 | curr += comments[t - 1]; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2116 | if (insert_space && t > 1) |
| 2117 | curr = skipwhite(curr); |
| 2118 | currsize = (int)STRLEN(curr); |
| 2119 | } |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2120 | |
| 2121 | #ifdef FEAT_TEXT_PROP |
| 2122 | if (prop_lines != NULL) |
| 2123 | join_prop_lines(curwin->w_cursor.lnum, newp, |
| 2124 | prop_lines, prop_lengths, count); |
| 2125 | else |
| 2126 | #endif |
| 2127 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2128 | |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 2129 | if (setmark) |
| 2130 | { |
| 2131 | /* Set the '] mark. */ |
| 2132 | curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 787880a | 2019-05-17 20:17:40 +0200 | [diff] [blame] | 2133 | curwin->w_buffer->b_op_end.col = (colnr_T)sumsize; |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 2134 | } |
Bram Moolenaar | f92d8a2 | 2014-02-11 19:33:07 +0100 | [diff] [blame] | 2135 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2136 | /* Only report the change in the first line here, del_lines() will report |
| 2137 | * the deleted line. */ |
| 2138 | changed_lines(curwin->w_cursor.lnum, currsize, |
| 2139 | curwin->w_cursor.lnum + 1, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2140 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2141 | * Delete following lines. To do this we move the cursor there |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2142 | * briefly, and then move it back. After del_lines() the cursor may |
| 2143 | * 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] | 2144 | */ |
| 2145 | t = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2146 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2147 | del_lines(count - 1, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2148 | curwin->w_cursor.lnum = t; |
| 2149 | |
| 2150 | /* |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2151 | * Set the cursor column: |
| 2152 | * Vi compatible: use the column of the first join |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 2153 | * vim: use the column of the last join |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2154 | */ |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2155 | curwin->w_cursor.col = |
| 2156 | (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2157 | check_cursor_col(); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2158 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2159 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2160 | curwin->w_set_curswant = TRUE; |
| 2161 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2162 | theend: |
| 2163 | vim_free(spaces); |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2164 | if (remove_comments) |
| 2165 | vim_free(comments); |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2166 | return ret; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2169 | /* |
| 2170 | * Return TRUE if the two comment leaders given are the same. "lnum" is |
| 2171 | * the first line. White-space is ignored. Note that the whole of |
| 2172 | * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb |
| 2173 | */ |
| 2174 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2175 | same_leader( |
| 2176 | linenr_T lnum, |
| 2177 | int leader1_len, |
| 2178 | char_u *leader1_flags, |
| 2179 | int leader2_len, |
| 2180 | char_u *leader2_flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | { |
| 2182 | int idx1 = 0, idx2 = 0; |
| 2183 | char_u *p; |
| 2184 | char_u *line1; |
| 2185 | char_u *line2; |
| 2186 | |
| 2187 | if (leader1_len == 0) |
| 2188 | return (leader2_len == 0); |
| 2189 | |
| 2190 | /* |
| 2191 | * If first leader has 'f' flag, the lines can be joined only if the |
| 2192 | * second line does not have a leader. |
| 2193 | * If first leader has 'e' flag, the lines can never be joined. |
| 2194 | * If fist leader has 's' flag, the lines can only be joined if there is |
| 2195 | * some text after it and the second line has the 'm' flag. |
| 2196 | */ |
| 2197 | if (leader1_flags != NULL) |
| 2198 | { |
| 2199 | for (p = leader1_flags; *p && *p != ':'; ++p) |
| 2200 | { |
| 2201 | if (*p == COM_FIRST) |
| 2202 | return (leader2_len == 0); |
| 2203 | if (*p == COM_END) |
| 2204 | return FALSE; |
| 2205 | if (*p == COM_START) |
| 2206 | { |
| 2207 | if (*(ml_get(lnum) + leader1_len) == NUL) |
| 2208 | return FALSE; |
| 2209 | if (leader2_flags == NULL || leader2_len == 0) |
| 2210 | return FALSE; |
| 2211 | for (p = leader2_flags; *p && *p != ':'; ++p) |
| 2212 | if (*p == COM_MIDDLE) |
| 2213 | return TRUE; |
| 2214 | return FALSE; |
| 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | /* |
| 2220 | * Get current line and next line, compare the leaders. |
| 2221 | * The first line has to be saved, only one line can be locked at a time. |
| 2222 | */ |
| 2223 | line1 = vim_strsave(ml_get(lnum)); |
| 2224 | if (line1 != NULL) |
| 2225 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2226 | for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2227 | ; |
| 2228 | line2 = ml_get(lnum + 1); |
| 2229 | for (idx2 = 0; idx2 < leader2_len; ++idx2) |
| 2230 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2231 | if (!VIM_ISWHITE(line2[idx2])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2232 | { |
| 2233 | if (line1[idx1++] != line2[idx2]) |
| 2234 | break; |
| 2235 | } |
| 2236 | else |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2237 | while (VIM_ISWHITE(line1[idx1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2238 | ++idx1; |
| 2239 | } |
| 2240 | vim_free(line1); |
| 2241 | } |
| 2242 | return (idx2 == leader2_len && idx1 == leader1_len); |
| 2243 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2244 | |
| 2245 | /* |
Bram Moolenaar | 66accae | 2012-01-10 13:44:27 +0100 | [diff] [blame] | 2246 | * Implementation of the format operator 'gq'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2247 | */ |
Bram Moolenaar | 792cf5e | 2019-09-30 23:12:16 +0200 | [diff] [blame] | 2248 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2249 | op_format( |
| 2250 | oparg_T *oap, |
| 2251 | int keep_cursor) /* keep cursor on same text char */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2252 | { |
| 2253 | long old_line_count = curbuf->b_ml.ml_line_count; |
| 2254 | |
| 2255 | /* Place the cursor where the "gq" or "gw" command was given, so that "u" |
| 2256 | * can put it back there. */ |
| 2257 | curwin->w_cursor = oap->cursor_start; |
| 2258 | |
| 2259 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 2260 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 2261 | return; |
| 2262 | curwin->w_cursor = oap->start; |
| 2263 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2264 | if (oap->is_VIsual) |
| 2265 | /* When there is no change: need to remove the Visual selection */ |
| 2266 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2267 | |
| 2268 | /* Set '[ mark at the start of the formatted area */ |
| 2269 | curbuf->b_op_start = oap->start; |
| 2270 | |
| 2271 | /* For "gw" remember the cursor position and put it back below (adjusted |
| 2272 | * for joined and split lines). */ |
| 2273 | if (keep_cursor) |
| 2274 | saved_cursor = oap->cursor_start; |
| 2275 | |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 2276 | format_lines(oap->line_count, keep_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2277 | |
| 2278 | /* |
| 2279 | * Leave the cursor at the first non-blank of the last formatted line. |
| 2280 | * If the cursor was moved one line back (e.g. with "Q}") go to the next |
| 2281 | * line, so "." will do the next lines. |
| 2282 | */ |
| 2283 | if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 2284 | ++curwin->w_cursor.lnum; |
| 2285 | beginline(BL_WHITE | BL_FIX); |
| 2286 | old_line_count = curbuf->b_ml.ml_line_count - old_line_count; |
| 2287 | msgmore(old_line_count); |
| 2288 | |
| 2289 | /* put '] mark on the end of the formatted area */ |
| 2290 | curbuf->b_op_end = curwin->w_cursor; |
| 2291 | |
| 2292 | if (keep_cursor) |
| 2293 | { |
| 2294 | curwin->w_cursor = saved_cursor; |
| 2295 | saved_cursor.lnum = 0; |
| 2296 | } |
| 2297 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2298 | if (oap->is_VIsual) |
| 2299 | { |
| 2300 | win_T *wp; |
| 2301 | |
| 2302 | FOR_ALL_WINDOWS(wp) |
| 2303 | { |
| 2304 | if (wp->w_old_cursor_lnum != 0) |
| 2305 | { |
| 2306 | /* When lines have been inserted or deleted, adjust the end of |
| 2307 | * the Visual area to be redrawn. */ |
| 2308 | if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum) |
| 2309 | wp->w_old_cursor_lnum += old_line_count; |
| 2310 | else |
| 2311 | wp->w_old_visual_lnum += old_line_count; |
| 2312 | } |
| 2313 | } |
| 2314 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2317 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2318 | /* |
| 2319 | * Implementation of the format operator 'gq' for when using 'formatexpr'. |
| 2320 | */ |
Bram Moolenaar | 792cf5e | 2019-09-30 23:12:16 +0200 | [diff] [blame] | 2321 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2322 | op_formatexpr(oparg_T *oap) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2323 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2324 | if (oap->is_VIsual) |
| 2325 | /* When there is no change: need to remove the Visual selection */ |
| 2326 | redraw_curbuf_later(INVERTED); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2327 | |
Bram Moolenaar | 700303e | 2010-07-11 17:35:50 +0200 | [diff] [blame] | 2328 | if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0) |
| 2329 | /* As documented: when 'formatexpr' returns non-zero fall back to |
| 2330 | * internal formatting. */ |
| 2331 | op_format(oap, FALSE); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2335 | fex_format( |
| 2336 | linenr_T lnum, |
| 2337 | long count, |
| 2338 | int c) /* character to be inserted */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2339 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2340 | int use_sandbox = was_set_insecurely((char_u *)"formatexpr", |
| 2341 | OPT_LOCAL); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2342 | int r; |
Bram Moolenaar | d77f9d5 | 2016-09-04 15:13:39 +0200 | [diff] [blame] | 2343 | char_u *fex; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2344 | |
| 2345 | /* |
| 2346 | * 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] | 2347 | * Set v:char to the character to be inserted (can be NUL). |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2348 | */ |
| 2349 | set_vim_var_nr(VV_LNUM, lnum); |
| 2350 | set_vim_var_nr(VV_COUNT, count); |
Bram Moolenaar | da9591e | 2009-09-30 13:17:02 +0000 | [diff] [blame] | 2351 | set_vim_var_char(c); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 2352 | |
Bram Moolenaar | d77f9d5 | 2016-09-04 15:13:39 +0200 | [diff] [blame] | 2353 | /* Make a copy, the option could be changed while calling it. */ |
| 2354 | fex = vim_strsave(curbuf->b_p_fex); |
| 2355 | if (fex == NULL) |
| 2356 | return 0; |
| 2357 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2358 | /* |
| 2359 | * Evaluate the function. |
| 2360 | */ |
| 2361 | if (use_sandbox) |
| 2362 | ++sandbox; |
Bram Moolenaar | d77f9d5 | 2016-09-04 15:13:39 +0200 | [diff] [blame] | 2363 | r = (int)eval_to_number(fex); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2364 | if (use_sandbox) |
| 2365 | --sandbox; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 2366 | |
| 2367 | set_vim_var_string(VV_CHAR, NULL, -1); |
Bram Moolenaar | d77f9d5 | 2016-09-04 15:13:39 +0200 | [diff] [blame] | 2368 | vim_free(fex); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 2369 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2370 | return r; |
| 2371 | } |
| 2372 | #endif |
| 2373 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2374 | /* |
| 2375 | * Format "line_count" lines, starting at the cursor position. |
| 2376 | * When "line_count" is negative, format until the end of the paragraph. |
| 2377 | * Lines after the cursor line are saved for undo, caller must have saved the |
| 2378 | * first line. |
| 2379 | */ |
| 2380 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2381 | format_lines( |
| 2382 | linenr_T line_count, |
| 2383 | int avoid_fex) /* don't use 'formatexpr' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2384 | { |
| 2385 | int max_len; |
| 2386 | int is_not_par; /* current line not part of parag. */ |
| 2387 | int next_is_not_par; /* next line not part of paragraph */ |
| 2388 | int is_end_par; /* at end of paragraph */ |
| 2389 | int prev_is_end_par = FALSE;/* prev. line not part of parag. */ |
| 2390 | int next_is_start_par = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2391 | int leader_len = 0; /* leader len of current line */ |
| 2392 | int next_leader_len; /* leader len of next line */ |
| 2393 | char_u *leader_flags = NULL; /* flags for leader of current line */ |
| 2394 | char_u *next_leader_flags; /* flags for leader of next line */ |
| 2395 | int do_comments; /* format comments */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2396 | int do_comments_list = 0; /* format comments with 'n' or '2' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2397 | int advance = TRUE; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2398 | int second_indent = -1; /* indent for second line (comment |
| 2399 | * aware) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2400 | int do_second_indent; |
| 2401 | int do_number_indent; |
| 2402 | int do_trail_white; |
| 2403 | int first_par_line = TRUE; |
| 2404 | int smd_save; |
| 2405 | long count; |
| 2406 | int need_set_indent = TRUE; /* set indent of next paragraph */ |
| 2407 | int force_format = FALSE; |
| 2408 | int old_State = State; |
| 2409 | |
| 2410 | /* length of a line to force formatting: 3 * 'tw' */ |
| 2411 | max_len = comp_textwidth(TRUE) * 3; |
| 2412 | |
| 2413 | /* check for 'q', '2' and '1' in 'formatoptions' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2414 | do_comments = has_format_option(FO_Q_COMS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2415 | do_second_indent = has_format_option(FO_Q_SECOND); |
| 2416 | do_number_indent = has_format_option(FO_Q_NUMBER); |
| 2417 | do_trail_white = has_format_option(FO_WHITE_PAR); |
| 2418 | |
| 2419 | /* |
| 2420 | * Get info about the previous and current line. |
| 2421 | */ |
| 2422 | if (curwin->w_cursor.lnum > 1) |
| 2423 | is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1 |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 2424 | , &leader_len, &leader_flags, do_comments); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2425 | else |
| 2426 | is_not_par = TRUE; |
| 2427 | next_is_not_par = fmt_check_par(curwin->w_cursor.lnum |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 2428 | , &next_leader_len, &next_leader_flags, do_comments); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2429 | is_end_par = (is_not_par || next_is_not_par); |
| 2430 | if (!is_end_par && do_trail_white) |
| 2431 | is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1); |
| 2432 | |
| 2433 | curwin->w_cursor.lnum--; |
| 2434 | for (count = line_count; count != 0 && !got_int; --count) |
| 2435 | { |
| 2436 | /* |
| 2437 | * Advance to next paragraph. |
| 2438 | */ |
| 2439 | if (advance) |
| 2440 | { |
| 2441 | curwin->w_cursor.lnum++; |
| 2442 | prev_is_end_par = is_end_par; |
| 2443 | is_not_par = next_is_not_par; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2444 | leader_len = next_leader_len; |
| 2445 | leader_flags = next_leader_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | * The last line to be formatted. |
| 2450 | */ |
| 2451 | if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count) |
| 2452 | { |
| 2453 | next_is_not_par = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2454 | next_leader_len = 0; |
| 2455 | next_leader_flags = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2456 | } |
| 2457 | else |
| 2458 | { |
| 2459 | next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1 |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 2460 | , &next_leader_len, &next_leader_flags, do_comments); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2461 | if (do_number_indent) |
| 2462 | next_is_start_par = |
| 2463 | (get_number_indent(curwin->w_cursor.lnum + 1) > 0); |
| 2464 | } |
| 2465 | advance = TRUE; |
| 2466 | is_end_par = (is_not_par || next_is_not_par || next_is_start_par); |
| 2467 | if (!is_end_par && do_trail_white) |
| 2468 | is_end_par = !ends_in_white(curwin->w_cursor.lnum); |
| 2469 | |
| 2470 | /* |
| 2471 | * Skip lines that are not in a paragraph. |
| 2472 | */ |
| 2473 | if (is_not_par) |
| 2474 | { |
| 2475 | if (line_count < 0) |
| 2476 | break; |
| 2477 | } |
| 2478 | else |
| 2479 | { |
| 2480 | /* |
| 2481 | * For the first line of a paragraph, check indent of second line. |
| 2482 | * Don't do this for comments and empty lines. |
| 2483 | */ |
| 2484 | if (first_par_line |
| 2485 | && (do_second_indent || do_number_indent) |
| 2486 | && prev_is_end_par |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2487 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2488 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2489 | if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2490 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2491 | if (leader_len == 0 && next_leader_len == 0) |
| 2492 | { |
| 2493 | /* no comment found */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2494 | second_indent = |
| 2495 | get_indent_lnum(curwin->w_cursor.lnum + 1); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2496 | } |
| 2497 | else |
| 2498 | { |
| 2499 | second_indent = next_leader_len; |
| 2500 | do_comments_list = 1; |
| 2501 | } |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2502 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2503 | else if (do_number_indent) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2504 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2505 | if (leader_len == 0 && next_leader_len == 0) |
| 2506 | { |
| 2507 | /* no comment found */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2508 | second_indent = |
| 2509 | get_number_indent(curwin->w_cursor.lnum); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2510 | } |
| 2511 | else |
| 2512 | { |
| 2513 | /* get_number_indent() is now "comment aware"... */ |
| 2514 | second_indent = |
| 2515 | get_number_indent(curwin->w_cursor.lnum); |
| 2516 | do_comments_list = 1; |
| 2517 | } |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2518 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | /* |
| 2522 | * When the comment leader changes, it's the end of the paragraph. |
| 2523 | */ |
| 2524 | if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2525 | || !same_leader(curwin->w_cursor.lnum, |
| 2526 | leader_len, leader_flags, |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 2527 | next_leader_len, next_leader_flags)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2528 | is_end_par = TRUE; |
| 2529 | |
| 2530 | /* |
| 2531 | * If we have got to the end of a paragraph, or the line is |
| 2532 | * getting long, format it. |
| 2533 | */ |
| 2534 | if (is_end_par || force_format) |
| 2535 | { |
| 2536 | if (need_set_indent) |
| 2537 | /* replace indent in first line with minimal number of |
| 2538 | * tabs and spaces, according to current options */ |
| 2539 | (void)set_indent(get_indent(), SIN_CHANGED); |
| 2540 | |
| 2541 | /* put cursor on last non-space */ |
| 2542 | State = NORMAL; /* don't go past end-of-line */ |
| 2543 | coladvance((colnr_T)MAXCOL); |
| 2544 | while (curwin->w_cursor.col && vim_isspace(gchar_cursor())) |
| 2545 | dec_cursor(); |
| 2546 | |
| 2547 | /* do the formatting, without 'showmode' */ |
| 2548 | State = INSERT; /* for open_line() */ |
| 2549 | smd_save = p_smd; |
| 2550 | p_smd = FALSE; |
| 2551 | insertchar(NUL, INSCHAR_FORMAT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2552 | + (do_comments ? INSCHAR_DO_COM : 0) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 2553 | + (do_comments && do_comments_list |
| 2554 | ? INSCHAR_COM_LIST : 0) |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 2555 | + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2556 | State = old_State; |
| 2557 | p_smd = smd_save; |
| 2558 | second_indent = -1; |
| 2559 | /* at end of par.: need to set indent of next par. */ |
| 2560 | need_set_indent = is_end_par; |
| 2561 | if (is_end_par) |
| 2562 | { |
| 2563 | /* When called with a negative line count, break at the |
| 2564 | * end of the paragraph. */ |
| 2565 | if (line_count < 0) |
| 2566 | break; |
| 2567 | first_par_line = TRUE; |
| 2568 | } |
| 2569 | force_format = FALSE; |
| 2570 | } |
| 2571 | |
| 2572 | /* |
| 2573 | * When still in same paragraph, join the lines together. But |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 2574 | * first delete the leader from the second line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2575 | */ |
| 2576 | if (!is_end_par) |
| 2577 | { |
| 2578 | advance = FALSE; |
| 2579 | curwin->w_cursor.lnum++; |
| 2580 | curwin->w_cursor.col = 0; |
| 2581 | if (line_count < 0 && u_save_cursor() == FAIL) |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 2582 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2583 | if (next_leader_len > 0) |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 2584 | { |
| 2585 | (void)del_bytes((long)next_leader_len, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2586 | mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L, |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 2587 | (long)-next_leader_len, 0); |
| 2588 | } |
| 2589 | else if (second_indent > 0) // the "leader" for FO_Q_SECOND |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 2590 | { |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 2591 | int indent = getwhitecols_curline(); |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 2592 | |
| 2593 | if (indent > 0) |
| 2594 | { |
| 2595 | (void)del_bytes(indent, FALSE, FALSE); |
| 2596 | mark_col_adjust(curwin->w_cursor.lnum, |
Bram Moolenaar | e1e714e | 2018-12-31 23:58:24 +0100 | [diff] [blame] | 2597 | (colnr_T)0, 0L, (long)-indent, 0); |
Bram Moolenaar | 92c2db8 | 2013-11-02 23:59:35 +0100 | [diff] [blame] | 2598 | } |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 2599 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2600 | curwin->w_cursor.lnum--; |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 2601 | if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2602 | { |
| 2603 | beep_flush(); |
| 2604 | break; |
| 2605 | } |
| 2606 | first_par_line = FALSE; |
| 2607 | /* If the line is getting long, format it next time */ |
| 2608 | if (STRLEN(ml_get_curline()) > (size_t)max_len) |
| 2609 | force_format = TRUE; |
| 2610 | else |
| 2611 | force_format = FALSE; |
| 2612 | } |
| 2613 | } |
| 2614 | line_breakcheck(); |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | /* |
| 2619 | * Return TRUE if line "lnum" ends in a white character. |
| 2620 | */ |
| 2621 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2622 | ends_in_white(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2623 | { |
| 2624 | char_u *s = ml_get(lnum); |
| 2625 | size_t l; |
| 2626 | |
| 2627 | if (*s == NUL) |
| 2628 | return FALSE; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2629 | /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2630 | * invocation may call function multiple times". */ |
| 2631 | l = STRLEN(s) - 1; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2632 | return VIM_ISWHITE(s[l]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | /* |
| 2636 | * Blank lines, and lines containing only the comment leader, are left |
| 2637 | * untouched by the formatting. The function returns TRUE in this |
| 2638 | * case. It also returns TRUE when a line starts with the end of a comment |
| 2639 | * ('e' in comment flags), so that this line is skipped, and not joined to the |
| 2640 | * previous line. A new paragraph starts after a blank line, or when the |
| 2641 | * comment leader changes -- webb. |
| 2642 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2643 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2644 | fmt_check_par( |
| 2645 | linenr_T lnum, |
| 2646 | int *leader_len, |
| 2647 | char_u **leader_flags, |
| 2648 | int do_comments) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2649 | { |
| 2650 | char_u *flags = NULL; /* init for GCC */ |
| 2651 | char_u *ptr; |
| 2652 | |
| 2653 | ptr = ml_get(lnum); |
| 2654 | if (do_comments) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2655 | *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2656 | else |
| 2657 | *leader_len = 0; |
| 2658 | |
| 2659 | if (*leader_len > 0) |
| 2660 | { |
| 2661 | /* |
| 2662 | * Search for 'e' flag in comment leader flags. |
| 2663 | */ |
| 2664 | flags = *leader_flags; |
| 2665 | while (*flags && *flags != ':' && *flags != COM_END) |
| 2666 | ++flags; |
| 2667 | } |
| 2668 | |
| 2669 | return (*skipwhite(ptr + *leader_len) == NUL |
| 2670 | || (*leader_len > 0 && *flags == COM_END) |
| 2671 | || startPS(lnum, NUL, FALSE)); |
| 2672 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2673 | |
| 2674 | /* |
| 2675 | * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the |
| 2676 | * previous line is in the same paragraph. Used for auto-formatting. |
| 2677 | */ |
| 2678 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2679 | paragraph_start(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2680 | { |
| 2681 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2682 | int leader_len = 0; /* leader len of current line */ |
| 2683 | char_u *leader_flags = NULL; /* flags for leader of current line */ |
| 2684 | int next_leader_len; /* leader len of next line */ |
| 2685 | char_u *next_leader_flags; /* flags for leader of next line */ |
| 2686 | int do_comments; /* format comments */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2687 | |
| 2688 | if (lnum <= 1) |
| 2689 | return TRUE; /* start of the file */ |
| 2690 | |
| 2691 | p = ml_get(lnum - 1); |
| 2692 | if (*p == NUL) |
| 2693 | return TRUE; /* after empty line */ |
| 2694 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2695 | do_comments = has_format_option(FO_Q_COMS); |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 2696 | if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2697 | return TRUE; /* after non-paragraph line */ |
| 2698 | |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 2699 | if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2700 | return TRUE; /* "lnum" is not a paragraph line */ |
| 2701 | |
| 2702 | if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1)) |
| 2703 | return TRUE; /* missing trailing space in previous line. */ |
| 2704 | |
| 2705 | if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0)) |
| 2706 | return TRUE; /* numbered item starts in "lnum". */ |
| 2707 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2708 | if (!same_leader(lnum - 1, leader_len, leader_flags, |
| 2709 | next_leader_len, next_leader_flags)) |
| 2710 | return TRUE; /* change of comment leader. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2711 | |
| 2712 | return FALSE; |
| 2713 | } |
| 2714 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2715 | /* |
| 2716 | * prepare a few things for block mode yank/delete/tilde |
| 2717 | * |
| 2718 | * for delete: |
| 2719 | * - textlen includes the first/last char to be (partly) deleted |
| 2720 | * - start/endspaces is the number of columns that are taken by the |
| 2721 | * 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] | 2722 | * deleted. |
| 2723 | * for yank and tilde: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2724 | * - textlen includes the first/last char to be wholly yanked |
| 2725 | * - start/endspaces is the number of columns of the first/last yanked char |
| 2726 | * that are to be yanked. |
| 2727 | */ |
Bram Moolenaar | 4aea03e | 2019-09-25 22:37:17 +0200 | [diff] [blame] | 2728 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2729 | block_prep( |
| 2730 | oparg_T *oap, |
| 2731 | struct block_def *bdp, |
| 2732 | linenr_T lnum, |
| 2733 | int is_del) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2734 | { |
| 2735 | int incr = 0; |
| 2736 | char_u *pend; |
| 2737 | char_u *pstart; |
| 2738 | char_u *line; |
| 2739 | char_u *prev_pstart; |
| 2740 | char_u *prev_pend; |
| 2741 | |
| 2742 | bdp->startspaces = 0; |
| 2743 | bdp->endspaces = 0; |
| 2744 | bdp->textlen = 0; |
| 2745 | bdp->start_vcol = 0; |
| 2746 | bdp->end_vcol = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2747 | bdp->is_short = FALSE; |
| 2748 | bdp->is_oneChar = FALSE; |
| 2749 | bdp->pre_whitesp = 0; |
| 2750 | bdp->pre_whitesp_c = 0; |
| 2751 | bdp->end_char_vcols = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2752 | bdp->start_char_vcols = 0; |
| 2753 | |
| 2754 | line = ml_get(lnum); |
| 2755 | pstart = line; |
| 2756 | prev_pstart = line; |
| 2757 | while (bdp->start_vcol < oap->start_vcol && *pstart) |
| 2758 | { |
| 2759 | /* 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] | 2760 | incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2761 | bdp->start_vcol += incr; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2762 | if (VIM_ISWHITE(*pstart)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2763 | { |
| 2764 | bdp->pre_whitesp += incr; |
| 2765 | bdp->pre_whitesp_c++; |
| 2766 | } |
| 2767 | else |
| 2768 | { |
| 2769 | bdp->pre_whitesp = 0; |
| 2770 | bdp->pre_whitesp_c = 0; |
| 2771 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2772 | prev_pstart = pstart; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 2773 | MB_PTR_ADV(pstart); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2774 | } |
| 2775 | bdp->start_char_vcols = incr; |
| 2776 | if (bdp->start_vcol < oap->start_vcol) /* line too short */ |
| 2777 | { |
| 2778 | bdp->end_vcol = bdp->start_vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2779 | bdp->is_short = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2780 | if (!is_del || oap->op_type == OP_APPEND) |
| 2781 | bdp->endspaces = oap->end_vcol - oap->start_vcol + 1; |
| 2782 | } |
| 2783 | else |
| 2784 | { |
| 2785 | /* notice: this converts partly selected Multibyte characters to |
| 2786 | * spaces, too. */ |
| 2787 | bdp->startspaces = bdp->start_vcol - oap->start_vcol; |
| 2788 | if (is_del && bdp->startspaces) |
| 2789 | bdp->startspaces = bdp->start_char_vcols - bdp->startspaces; |
| 2790 | pend = pstart; |
| 2791 | bdp->end_vcol = bdp->start_vcol; |
| 2792 | if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */ |
| 2793 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2794 | bdp->is_oneChar = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2795 | if (oap->op_type == OP_INSERT) |
| 2796 | bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; |
| 2797 | else if (oap->op_type == OP_APPEND) |
| 2798 | { |
| 2799 | bdp->startspaces += oap->end_vcol - oap->start_vcol + 1; |
| 2800 | bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; |
| 2801 | } |
| 2802 | else |
| 2803 | { |
| 2804 | bdp->startspaces = oap->end_vcol - oap->start_vcol + 1; |
| 2805 | if (is_del && oap->op_type != OP_LSHIFT) |
| 2806 | { |
| 2807 | /* just putting the sum of those two into |
| 2808 | * bdp->startspaces doesn't work for Visual replace, |
| 2809 | * so we have to split the tab in two */ |
| 2810 | bdp->startspaces = bdp->start_char_vcols |
| 2811 | - (bdp->start_vcol - oap->start_vcol); |
| 2812 | bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; |
| 2813 | } |
| 2814 | } |
| 2815 | } |
| 2816 | else |
| 2817 | { |
| 2818 | prev_pend = pend; |
| 2819 | while (bdp->end_vcol <= oap->end_vcol && *pend != NUL) |
| 2820 | { |
| 2821 | /* Count a tab for what it's worth (if list mode not on) */ |
| 2822 | prev_pend = pend; |
Bram Moolenaar | 1dc9233 | 2015-01-27 13:22:20 +0100 | [diff] [blame] | 2823 | incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2824 | bdp->end_vcol += incr; |
| 2825 | } |
| 2826 | if (bdp->end_vcol <= oap->end_vcol |
| 2827 | && (!is_del |
| 2828 | || oap->op_type == OP_APPEND |
| 2829 | || oap->op_type == OP_REPLACE)) /* line too short */ |
| 2830 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2831 | bdp->is_short = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2832 | /* Alternative: include spaces to fill up the block. |
| 2833 | * Disadvantage: can lead to trailing spaces when the line is |
| 2834 | * short where the text is put */ |
| 2835 | /* if (!is_del || oap->op_type == OP_APPEND) */ |
| 2836 | if (oap->op_type == OP_APPEND || virtual_op) |
| 2837 | bdp->endspaces = oap->end_vcol - bdp->end_vcol |
Bram Moolenaar | 2c7a29c | 2005-12-12 22:02:31 +0000 | [diff] [blame] | 2838 | + oap->inclusive; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2839 | else |
| 2840 | bdp->endspaces = 0; /* replace doesn't add characters */ |
| 2841 | } |
| 2842 | else if (bdp->end_vcol > oap->end_vcol) |
| 2843 | { |
| 2844 | bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; |
| 2845 | if (!is_del && bdp->endspaces) |
| 2846 | { |
| 2847 | bdp->endspaces = incr - bdp->endspaces; |
| 2848 | if (pend != pstart) |
| 2849 | pend = prev_pend; |
| 2850 | } |
| 2851 | } |
| 2852 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2853 | bdp->end_char_vcols = incr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2854 | if (is_del && bdp->startspaces) |
| 2855 | pstart = prev_pstart; |
| 2856 | bdp->textlen = (int)(pend - pstart); |
| 2857 | } |
| 2858 | bdp->textcol = (colnr_T) (pstart - line); |
| 2859 | bdp->textstart = pstart; |
| 2860 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2861 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2862 | /* |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2863 | * Handle the add/subtract operator. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2864 | */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2865 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2866 | op_addsub( |
| 2867 | oparg_T *oap, |
| 2868 | linenr_T Prenum1, /* Amount of add/subtract */ |
| 2869 | int g_cmd) /* was g<c-a>/g<c-x> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2870 | { |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2871 | pos_T pos; |
| 2872 | struct block_def bd; |
| 2873 | int change_cnt = 0; |
| 2874 | linenr_T amount = Prenum1; |
| 2875 | |
Bram Moolenaar | 6b73188 | 2018-11-16 20:54:47 +0100 | [diff] [blame] | 2876 | // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the |
Bram Moolenaar | bdace83 | 2019-03-02 10:13:42 +0100 | [diff] [blame] | 2877 | // buffer is not completely updated yet. Postpone updating folds until before |
Bram Moolenaar | 6b73188 | 2018-11-16 20:54:47 +0100 | [diff] [blame] | 2878 | // the call to changed_lines(). |
| 2879 | #ifdef FEAT_FOLDING |
| 2880 | disable_fold_update++; |
| 2881 | #endif |
| 2882 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2883 | if (!VIsual_active) |
| 2884 | { |
| 2885 | pos = curwin->w_cursor; |
| 2886 | if (u_save_cursor() == FAIL) |
Bram Moolenaar | 6b73188 | 2018-11-16 20:54:47 +0100 | [diff] [blame] | 2887 | { |
| 2888 | #ifdef FEAT_FOLDING |
| 2889 | disable_fold_update--; |
| 2890 | #endif |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2891 | return; |
Bram Moolenaar | 6b73188 | 2018-11-16 20:54:47 +0100 | [diff] [blame] | 2892 | } |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2893 | change_cnt = do_addsub(oap->op_type, &pos, 0, amount); |
Bram Moolenaar | 6b73188 | 2018-11-16 20:54:47 +0100 | [diff] [blame] | 2894 | #ifdef FEAT_FOLDING |
| 2895 | disable_fold_update--; |
| 2896 | #endif |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2897 | if (change_cnt) |
| 2898 | changed_lines(pos.lnum, 0, pos.lnum + 1, 0L); |
| 2899 | } |
| 2900 | else |
| 2901 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 2902 | int one_change; |
| 2903 | int length; |
| 2904 | pos_T startpos; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2905 | |
| 2906 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 2907 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
Bram Moolenaar | 6b73188 | 2018-11-16 20:54:47 +0100 | [diff] [blame] | 2908 | { |
| 2909 | #ifdef FEAT_FOLDING |
| 2910 | disable_fold_update--; |
| 2911 | #endif |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2912 | return; |
Bram Moolenaar | 6b73188 | 2018-11-16 20:54:47 +0100 | [diff] [blame] | 2913 | } |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2914 | |
| 2915 | pos = oap->start; |
| 2916 | for (; pos.lnum <= oap->end.lnum; ++pos.lnum) |
| 2917 | { |
| 2918 | if (oap->block_mode) /* Visual block mode */ |
| 2919 | { |
| 2920 | block_prep(oap, &bd, pos.lnum, FALSE); |
| 2921 | pos.col = bd.textcol; |
| 2922 | length = bd.textlen; |
| 2923 | } |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 2924 | else if (oap->motion_type == MLINE) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2925 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 2926 | curwin->w_cursor.col = 0; |
| 2927 | pos.col = 0; |
| 2928 | length = (colnr_T)STRLEN(ml_get(pos.lnum)); |
| 2929 | } |
| 2930 | else /* oap->motion_type == MCHAR */ |
| 2931 | { |
Bram Moolenaar | 5fe6bdf | 2017-12-05 17:22:12 +0100 | [diff] [blame] | 2932 | if (pos.lnum == oap->start.lnum && !oap->inclusive) |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 2933 | dec(&(oap->end)); |
| 2934 | length = (colnr_T)STRLEN(ml_get(pos.lnum)); |
| 2935 | pos.col = 0; |
| 2936 | if (pos.lnum == oap->start.lnum) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2937 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 2938 | pos.col += oap->start.col; |
| 2939 | length -= oap->start.col; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2940 | } |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 2941 | if (pos.lnum == oap->end.lnum) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2942 | { |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 2943 | length = (int)STRLEN(ml_get(oap->end.lnum)); |
| 2944 | if (oap->end.col >= length) |
| 2945 | oap->end.col = length - 1; |
| 2946 | length = oap->end.col - pos.col + 1; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2947 | } |
| 2948 | } |
| 2949 | one_change = do_addsub(oap->op_type, &pos, length, amount); |
| 2950 | if (one_change) |
| 2951 | { |
| 2952 | /* Remember the start position of the first change. */ |
| 2953 | if (change_cnt == 0) |
| 2954 | startpos = curbuf->b_op_start; |
| 2955 | ++change_cnt; |
| 2956 | } |
| 2957 | |
| 2958 | #ifdef FEAT_NETBEANS_INTG |
| 2959 | if (netbeans_active() && one_change) |
| 2960 | { |
| 2961 | char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 2962 | |
| 2963 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)length); |
| 2964 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
| 2965 | &ptr[pos.col], length); |
| 2966 | } |
| 2967 | #endif |
| 2968 | if (g_cmd && one_change) |
| 2969 | amount += Prenum1; |
| 2970 | } |
Bram Moolenaar | 6b73188 | 2018-11-16 20:54:47 +0100 | [diff] [blame] | 2971 | |
| 2972 | #ifdef FEAT_FOLDING |
| 2973 | disable_fold_update--; |
| 2974 | #endif |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2975 | if (change_cnt) |
| 2976 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
| 2977 | |
| 2978 | if (!change_cnt && oap->is_VIsual) |
| 2979 | /* No change: need to remove the Visual selection */ |
| 2980 | redraw_curbuf_later(INVERTED); |
| 2981 | |
| 2982 | /* Set '[ mark if something changed. Keep the last end |
| 2983 | * position from do_addsub(). */ |
| 2984 | if (change_cnt > 0) |
| 2985 | curbuf->b_op_start = startpos; |
| 2986 | |
| 2987 | if (change_cnt > p_report) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2988 | smsg(NGETTEXT("%ld line changed", "%ld lines changed", |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 2989 | change_cnt), change_cnt); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | /* |
| 2994 | * Add or subtract 'Prenum1' from a number in a line |
| 2995 | * op_type is OP_NR_ADD or OP_NR_SUB |
| 2996 | * |
| 2997 | * Returns TRUE if some character was changed. |
| 2998 | */ |
| 2999 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3000 | do_addsub( |
| 3001 | int op_type, |
| 3002 | pos_T *pos, |
| 3003 | int length, |
| 3004 | linenr_T Prenum1) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3005 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3006 | int col; |
| 3007 | char_u *buf1; |
| 3008 | char_u buf2[NUMBUFLEN]; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3009 | int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3010 | static int hexupper = FALSE; /* 0xABC */ |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3011 | uvarnumber_T n; |
| 3012 | uvarnumber_T oldn; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3013 | char_u *ptr; |
| 3014 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3015 | int todel; |
| 3016 | int dohex; |
| 3017 | int dooct; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3018 | int dobin; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3019 | int doalp; |
| 3020 | int firstdigit; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3021 | int subtract; |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3022 | int negative = FALSE; |
Bram Moolenaar | 9bb1930 | 2015-07-03 12:44:07 +0200 | [diff] [blame] | 3023 | int was_positive = TRUE; |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3024 | int visual = VIsual_active; |
Bram Moolenaar | 3ec3261 | 2015-07-12 15:02:38 +0200 | [diff] [blame] | 3025 | int did_change = FALSE; |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 3026 | pos_T save_cursor = curwin->w_cursor; |
Bram Moolenaar | 5d1bc78 | 2015-07-17 13:03:48 +0200 | [diff] [blame] | 3027 | int maxlen = 0; |
Bram Moolenaar | a52dfae | 2016-01-10 20:21:57 +0100 | [diff] [blame] | 3028 | pos_T startpos; |
| 3029 | pos_T endpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3030 | |
Bram Moolenaar | dac1347 | 2019-09-16 21:06:21 +0200 | [diff] [blame] | 3031 | dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX" |
| 3032 | dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal" |
| 3033 | dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin" |
| 3034 | doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3035 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3036 | curwin->w_cursor = *pos; |
| 3037 | ptr = ml_get(pos->lnum); |
| 3038 | col = pos->col; |
| 3039 | |
| 3040 | if (*ptr == NUL) |
| 3041 | goto theend; |
| 3042 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3043 | /* |
| 3044 | * First check if we are on a hexadecimal number, after the "0x". |
| 3045 | */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3046 | if (!VIsual_active) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3047 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3048 | if (dobin) |
| 3049 | while (col > 0 && vim_isbdigit(ptr[col])) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3050 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3051 | --col; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3052 | if (has_mbyte) |
| 3053 | col -= (*mb_head_off)(ptr, ptr + col); |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3054 | } |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3055 | |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3056 | if (dohex) |
| 3057 | while (col > 0 && vim_isxdigit(ptr[col])) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3058 | { |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3059 | --col; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3060 | if (has_mbyte) |
| 3061 | col -= (*mb_head_off)(ptr, ptr + col); |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3062 | } |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3063 | |
| 3064 | if ( dobin |
| 3065 | && dohex |
| 3066 | && ! ((col > 0 |
| 3067 | && (ptr[col] == 'X' |
| 3068 | || ptr[col] == 'x') |
| 3069 | && ptr[col - 1] == '0' |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3070 | && (!has_mbyte || |
| 3071 | !(*mb_head_off)(ptr, ptr + col - 1)) |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3072 | && vim_isxdigit(ptr[col + 1])))) |
| 3073 | { |
| 3074 | |
| 3075 | /* In case of binary/hexadecimal pattern overlap match, rescan */ |
| 3076 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3077 | col = pos->col; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3078 | |
| 3079 | while (col > 0 && vim_isdigit(ptr[col])) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3080 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3081 | col--; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3082 | if (has_mbyte) |
| 3083 | col -= (*mb_head_off)(ptr, ptr + col); |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3084 | } |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | if (( dohex |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3088 | && col > 0 |
| 3089 | && (ptr[col] == 'X' |
| 3090 | || ptr[col] == 'x') |
| 3091 | && ptr[col - 1] == '0' |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3092 | && (!has_mbyte || |
| 3093 | !(*mb_head_off)(ptr, ptr + col - 1)) |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3094 | && vim_isxdigit(ptr[col + 1])) || |
| 3095 | ( dobin |
| 3096 | && col > 0 |
| 3097 | && (ptr[col] == 'B' |
| 3098 | || ptr[col] == 'b') |
| 3099 | && ptr[col - 1] == '0' |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3100 | && (!has_mbyte || |
| 3101 | !(*mb_head_off)(ptr, ptr + col - 1)) |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3102 | && vim_isbdigit(ptr[col + 1]))) |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3103 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 3104 | /* Found hexadecimal or binary number, move to its start. */ |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3105 | --col; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3106 | if (has_mbyte) |
| 3107 | col -= (*mb_head_off)(ptr, ptr + col); |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3108 | } |
| 3109 | else |
| 3110 | { |
| 3111 | /* |
| 3112 | * Search forward and then backward to find the start of number. |
| 3113 | */ |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3114 | col = pos->col; |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3115 | |
| 3116 | while (ptr[col] != NUL |
| 3117 | && !vim_isdigit(ptr[col]) |
| 3118 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 3119 | col += mb_ptr2len(ptr + col); |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3120 | |
| 3121 | while (col > 0 |
| 3122 | && vim_isdigit(ptr[col - 1]) |
| 3123 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3124 | { |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3125 | --col; |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3126 | if (has_mbyte) |
| 3127 | col -= (*mb_head_off)(ptr, ptr + col); |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3128 | } |
Bram Moolenaar | 3a304b2 | 2015-06-25 13:57:36 +0200 | [diff] [blame] | 3129 | } |
| 3130 | } |
| 3131 | |
Bram Moolenaar | 5d1bc78 | 2015-07-17 13:03:48 +0200 | [diff] [blame] | 3132 | if (visual) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3133 | { |
| 3134 | while (ptr[col] != NUL && length > 0 |
| 3135 | && !vim_isdigit(ptr[col]) |
| 3136 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
| 3137 | { |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 3138 | int mb_len = mb_ptr2len(ptr + col); |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3139 | |
| 3140 | col += mb_len; |
| 3141 | length -= mb_len; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | if (length == 0) |
| 3145 | goto theend; |
| 3146 | |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3147 | if (col > pos->col && ptr[col - 1] == '-' |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 3148 | && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3149 | { |
| 3150 | negative = TRUE; |
| 3151 | was_positive = FALSE; |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | /* |
| 3156 | * If a number was found, and saving for undo works, replace the number. |
| 3157 | */ |
| 3158 | firstdigit = ptr[col]; |
| 3159 | if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit))) |
| 3160 | { |
| 3161 | beep_flush(); |
| 3162 | goto theend; |
| 3163 | } |
| 3164 | |
| 3165 | if (doalp && ASCII_ISALPHA(firstdigit)) |
| 3166 | { |
| 3167 | /* decrement or increment alphabetic character */ |
| 3168 | if (op_type == OP_NR_SUB) |
| 3169 | { |
| 3170 | if (CharOrd(firstdigit) < Prenum1) |
| 3171 | { |
| 3172 | if (isupper(firstdigit)) |
| 3173 | firstdigit = 'A'; |
| 3174 | else |
| 3175 | firstdigit = 'a'; |
| 3176 | } |
| 3177 | else |
| 3178 | #ifdef EBCDIC |
| 3179 | firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1); |
| 3180 | #else |
| 3181 | firstdigit -= Prenum1; |
| 3182 | #endif |
| 3183 | } |
| 3184 | else |
| 3185 | { |
| 3186 | if (26 - CharOrd(firstdigit) - 1 < Prenum1) |
| 3187 | { |
| 3188 | if (isupper(firstdigit)) |
| 3189 | firstdigit = 'Z'; |
| 3190 | else |
| 3191 | firstdigit = 'z'; |
| 3192 | } |
| 3193 | else |
| 3194 | #ifdef EBCDIC |
| 3195 | firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1); |
| 3196 | #else |
| 3197 | firstdigit += Prenum1; |
| 3198 | #endif |
| 3199 | } |
| 3200 | curwin->w_cursor.col = col; |
| 3201 | if (!did_change) |
| 3202 | startpos = curwin->w_cursor; |
| 3203 | did_change = TRUE; |
| 3204 | (void)del_char(FALSE); |
| 3205 | ins_char(firstdigit); |
| 3206 | endpos = curwin->w_cursor; |
| 3207 | curwin->w_cursor.col = col; |
| 3208 | } |
| 3209 | else |
| 3210 | { |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3211 | if (col > 0 && ptr[col - 1] == '-' |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3212 | && (!has_mbyte || |
| 3213 | !(*mb_head_off)(ptr, ptr + col - 1)) |
Bram Moolenaar | ad5ca9b | 2016-06-20 21:26:08 +0200 | [diff] [blame] | 3214 | && !visual) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3215 | { |
| 3216 | /* negative number */ |
| 3217 | --col; |
| 3218 | negative = TRUE; |
| 3219 | } |
| 3220 | /* get the number value (unsigned) */ |
| 3221 | if (visual && VIsual_mode != 'V') |
| 3222 | maxlen = (curbuf->b_visual.vi_curswant == MAXCOL |
| 3223 | ? (int)STRLEN(ptr) - col |
| 3224 | : length); |
| 3225 | |
| 3226 | vim_str2nr(ptr + col, &pre, &length, |
| 3227 | 0 + (dobin ? STR2NR_BIN : 0) |
| 3228 | + (dooct ? STR2NR_OCT : 0) |
| 3229 | + (dohex ? STR2NR_HEX : 0), |
Bram Moolenaar | 16e9b85 | 2019-05-19 19:59:35 +0200 | [diff] [blame] | 3230 | NULL, &n, maxlen, FALSE); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3231 | |
| 3232 | /* ignore leading '-' for hex and octal and bin numbers */ |
| 3233 | if (pre && negative) |
| 3234 | { |
| 3235 | ++col; |
| 3236 | --length; |
| 3237 | negative = FALSE; |
| 3238 | } |
| 3239 | /* add or subtract */ |
| 3240 | subtract = FALSE; |
| 3241 | if (op_type == OP_NR_SUB) |
| 3242 | subtract ^= TRUE; |
| 3243 | if (negative) |
| 3244 | subtract ^= TRUE; |
| 3245 | |
| 3246 | oldn = n; |
| 3247 | if (subtract) |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3248 | n -= (uvarnumber_T)Prenum1; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3249 | else |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3250 | n += (uvarnumber_T)Prenum1; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3251 | /* handle wraparound for decimal numbers */ |
| 3252 | if (!pre) |
| 3253 | { |
| 3254 | if (subtract) |
| 3255 | { |
| 3256 | if (n > oldn) |
| 3257 | { |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3258 | n = 1 + (n ^ (uvarnumber_T)-1); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3259 | negative ^= TRUE; |
| 3260 | } |
| 3261 | } |
| 3262 | else |
| 3263 | { |
| 3264 | /* add */ |
| 3265 | if (n < oldn) |
| 3266 | { |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3267 | n = (n ^ (uvarnumber_T)-1); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3268 | negative ^= TRUE; |
| 3269 | } |
| 3270 | } |
| 3271 | if (n == 0) |
| 3272 | negative = FALSE; |
| 3273 | } |
| 3274 | |
| 3275 | if (visual && !was_positive && !negative && col > 0) |
| 3276 | { |
| 3277 | /* need to remove the '-' */ |
| 3278 | col--; |
| 3279 | length++; |
| 3280 | } |
| 3281 | |
| 3282 | /* |
| 3283 | * Delete the old number. |
| 3284 | */ |
| 3285 | curwin->w_cursor.col = col; |
| 3286 | if (!did_change) |
| 3287 | startpos = curwin->w_cursor; |
| 3288 | did_change = TRUE; |
| 3289 | todel = length; |
| 3290 | c = gchar_cursor(); |
| 3291 | /* |
| 3292 | * Don't include the '-' in the length, only the length of the |
| 3293 | * part after it is kept the same. |
| 3294 | */ |
| 3295 | if (c == '-') |
| 3296 | --length; |
| 3297 | while (todel-- > 0) |
| 3298 | { |
| 3299 | if (c < 0x100 && isalpha(c)) |
| 3300 | { |
| 3301 | if (isupper(c)) |
| 3302 | hexupper = TRUE; |
| 3303 | else |
| 3304 | hexupper = FALSE; |
| 3305 | } |
| 3306 | /* del_char() will mark line needing displaying */ |
| 3307 | (void)del_char(FALSE); |
| 3308 | c = gchar_cursor(); |
| 3309 | } |
| 3310 | |
| 3311 | /* |
| 3312 | * Prepare the leading characters in buf1[]. |
| 3313 | * When there are many leading zeros it could be very long. |
| 3314 | * Allocate a bit too much. |
| 3315 | */ |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 3316 | buf1 = alloc(length + NUMBUFLEN); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3317 | if (buf1 == NULL) |
| 3318 | goto theend; |
| 3319 | ptr = buf1; |
Bram Moolenaar | dc633cf | 2016-04-23 14:33:19 +0200 | [diff] [blame] | 3320 | if (negative && (!visual || was_positive)) |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3321 | *ptr++ = '-'; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3322 | if (pre) |
| 3323 | { |
| 3324 | *ptr++ = '0'; |
| 3325 | --length; |
| 3326 | } |
| 3327 | if (pre == 'b' || pre == 'B' || |
| 3328 | pre == 'x' || pre == 'X') |
| 3329 | { |
| 3330 | *ptr++ = pre; |
| 3331 | --length; |
| 3332 | } |
| 3333 | |
| 3334 | /* |
| 3335 | * Put the number characters in buf2[]. |
| 3336 | */ |
| 3337 | if (pre == 'b' || pre == 'B') |
| 3338 | { |
| 3339 | int i; |
| 3340 | int bit = 0; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3341 | int bits = sizeof(uvarnumber_T) * 8; |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3342 | |
| 3343 | /* leading zeros */ |
| 3344 | for (bit = bits; bit > 0; bit--) |
| 3345 | if ((n >> (bit - 1)) & 0x1) break; |
| 3346 | |
| 3347 | for (i = 0; bit > 0; bit--) |
| 3348 | buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0'; |
| 3349 | |
| 3350 | buf2[i] = '\0'; |
| 3351 | } |
| 3352 | else if (pre == 0) |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 3353 | vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3354 | (long_long_u_T)n); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3355 | else if (pre == '0') |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 3356 | vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3357 | (long_long_u_T)n); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3358 | else if (pre && hexupper) |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 3359 | vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3360 | (long_long_u_T)n); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3361 | else |
Bram Moolenaar | ea39176 | 2018-04-08 13:07:22 +0200 | [diff] [blame] | 3362 | vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3363 | (long_long_u_T)n); |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3364 | length -= (int)STRLEN(buf2); |
| 3365 | |
| 3366 | /* |
| 3367 | * Adjust number of zeros to the new number of digits, so the |
| 3368 | * total length of the number remains the same. |
| 3369 | * Don't do this when |
| 3370 | * the result may look like an octal number. |
| 3371 | */ |
| 3372 | if (firstdigit == '0' && !(dooct && pre == 0)) |
| 3373 | while (length-- > 0) |
| 3374 | *ptr++ = '0'; |
| 3375 | *ptr = NUL; |
| 3376 | STRCAT(buf1, buf2); |
| 3377 | ins_str(buf1); /* insert the new number */ |
| 3378 | vim_free(buf1); |
| 3379 | endpos = curwin->w_cursor; |
| 3380 | if (did_change && curwin->w_cursor.col) |
| 3381 | --curwin->w_cursor.col; |
| 3382 | } |
| 3383 | |
Bram Moolenaar | a52dfae | 2016-01-10 20:21:57 +0100 | [diff] [blame] | 3384 | if (did_change) |
| 3385 | { |
| 3386 | /* set the '[ and '] marks */ |
| 3387 | curbuf->b_op_start = startpos; |
| 3388 | curbuf->b_op_end = endpos; |
| 3389 | if (curbuf->b_op_end.col > 0) |
| 3390 | --curbuf->b_op_end.col; |
| 3391 | } |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3392 | |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 3393 | theend: |
| 3394 | if (visual) |
| 3395 | curwin->w_cursor = save_cursor; |
Bram Moolenaar | 8e08125 | 2016-03-21 23:13:32 +0100 | [diff] [blame] | 3396 | else if (did_change) |
| 3397 | curwin->w_set_curswant = TRUE; |
Bram Moolenaar | 7ae4fbc | 2016-01-12 21:00:40 +0100 | [diff] [blame] | 3398 | |
Bram Moolenaar | d79e550 | 2016-01-10 22:13:02 +0100 | [diff] [blame] | 3399 | return did_change; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3402 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 3403 | /* |
| 3404 | * SELECTION / PRIMARY ('*') |
| 3405 | * |
| 3406 | * Text selection stuff that uses the GUI selection register '*'. When using a |
| 3407 | * GUI this may be text from another window, otherwise it is the last text we |
| 3408 | * had highlighted with VIsual mode. With mouse support, clicking the middle |
| 3409 | * button performs the paste, otherwise you will need to do <"*p>. " |
| 3410 | * If not under X, it is synonymous with the clipboard register '+'. |
| 3411 | * |
| 3412 | * X CLIPBOARD ('+') |
| 3413 | * |
| 3414 | * Text selection stuff that uses the GUI clipboard register '+'. |
| 3415 | * Under X, this matches the standard cut/paste buffer CLIPBOARD selection. |
| 3416 | * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed", |
| 3417 | * otherwise you will need to do <"+p>. " |
| 3418 | * If not under X, it is synonymous with the selection register '*'. |
| 3419 | */ |
| 3420 | |
| 3421 | /* |
| 3422 | * Routine to export any final X selection we had to the environment |
Bram Moolenaar | f58a847 | 2017-03-05 18:03:04 +0100 | [diff] [blame] | 3423 | * so that the text is still available after Vim has exited. X selections |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3424 | * only exist while the owning application exists, so we write to the |
| 3425 | * permanent (while X runs) store CUT_BUFFER0. |
| 3426 | * Dump the CLIPBOARD selection if we own it (it's logically the more |
| 3427 | * 'permanent' of the two), otherwise the PRIMARY one. |
| 3428 | * For now, use a hard-coded sanity limit of 1Mb of data. |
| 3429 | */ |
Bram Moolenaar | a6b7a08 | 2016-08-10 20:53:05 +0200 | [diff] [blame] | 3430 | #if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3431 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3432 | x11_export_final_selection(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3433 | { |
| 3434 | Display *dpy; |
| 3435 | char_u *str = NULL; |
| 3436 | long_u len = 0; |
| 3437 | int motion_type = -1; |
| 3438 | |
| 3439 | # ifdef FEAT_GUI |
| 3440 | if (gui.in_use) |
| 3441 | dpy = X_DISPLAY; |
| 3442 | else |
| 3443 | # endif |
| 3444 | # ifdef FEAT_XCLIPBOARD |
| 3445 | dpy = xterm_dpy; |
| 3446 | # else |
| 3447 | return; |
| 3448 | # endif |
| 3449 | |
| 3450 | /* Get selection to export */ |
| 3451 | if (clip_plus.owned) |
| 3452 | motion_type = clip_convert_selection(&str, &len, &clip_plus); |
| 3453 | else if (clip_star.owned) |
| 3454 | motion_type = clip_convert_selection(&str, &len, &clip_star); |
| 3455 | |
| 3456 | /* Check it's OK */ |
| 3457 | if (dpy != NULL && str != NULL && motion_type >= 0 |
| 3458 | && len < 1024*1024 && len > 0) |
| 3459 | { |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 3460 | int ok = TRUE; |
| 3461 | |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 3462 | /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from |
| 3463 | * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit |
| 3464 | * encoding conversion usually doesn't work, so keep the text as-is. |
| 3465 | */ |
| 3466 | if (has_mbyte) |
| 3467 | { |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 3468 | vimconv_T vc; |
| 3469 | |
| 3470 | vc.vc_type = CONV_NONE; |
| 3471 | if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK) |
| 3472 | { |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3473 | int intlen = len; |
| 3474 | char_u *conv_str; |
Bram Moolenaar | 331dafd | 2009-11-25 11:38:30 +0000 | [diff] [blame] | 3475 | |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 3476 | vc.vc_fail = TRUE; |
Bram Moolenaar | 331dafd | 2009-11-25 11:38:30 +0000 | [diff] [blame] | 3477 | conv_str = string_convert(&vc, str, &intlen); |
| 3478 | len = intlen; |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 3479 | if (conv_str != NULL) |
| 3480 | { |
| 3481 | vim_free(str); |
| 3482 | str = conv_str; |
| 3483 | } |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 3484 | else |
| 3485 | { |
| 3486 | ok = FALSE; |
| 3487 | } |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 3488 | convert_setup(&vc, NULL, NULL); |
| 3489 | } |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 3490 | else |
| 3491 | { |
| 3492 | ok = FALSE; |
| 3493 | } |
Bram Moolenaar | bbc936b | 2009-07-01 16:04:58 +0000 | [diff] [blame] | 3494 | } |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 3495 | |
| 3496 | /* Do not store the string if conversion failed. Better to use any |
| 3497 | * other selection than garbled text. */ |
| 3498 | if (ok) |
Bram Moolenaar | e2e663f | 2013-03-07 18:02:30 +0100 | [diff] [blame] | 3499 | { |
| 3500 | XStoreBuffer(dpy, (char *)str, (int)len, 0); |
| 3501 | XFlush(dpy); |
| 3502 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | vim_free(str); |
| 3506 | } |
| 3507 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3508 | #endif /* FEAT_CLIPBOARD || PROTO */ |
| 3509 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3510 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3511 | clear_oparg(oparg_T *oap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3512 | { |
| 3513 | vim_memset(oap, 0, sizeof(oparg_T)); |
| 3514 | } |
| 3515 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3516 | /* |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3517 | * Count the number of bytes, characters and "words" in a line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3518 | * |
| 3519 | * "Words" are counted by looking for boundaries between non-space and |
| 3520 | * space characters. (it seems to produce results that match 'wc'.) |
| 3521 | * |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3522 | * Return value is byte count; word count for the line is added to "*wc". |
| 3523 | * Char count is added to "*cc". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3524 | * |
| 3525 | * The function will only examine the first "limit" characters in the |
| 3526 | * line, stopping if it encounters an end-of-line (NUL byte). In that |
| 3527 | * case, eol_size will be added to the character count to account for |
| 3528 | * the size of the EOL character. |
| 3529 | */ |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3530 | static varnumber_T |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3531 | line_count_info( |
| 3532 | char_u *line, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3533 | varnumber_T *wc, |
| 3534 | varnumber_T *cc, |
| 3535 | varnumber_T limit, |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3536 | int eol_size) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3537 | { |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3538 | varnumber_T i; |
| 3539 | varnumber_T words = 0; |
| 3540 | varnumber_T chars = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3541 | int is_word = 0; |
| 3542 | |
Bram Moolenaar | 88b1ba1 | 2012-06-29 13:34:19 +0200 | [diff] [blame] | 3543 | for (i = 0; i < limit && line[i] != NUL; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3544 | { |
| 3545 | if (is_word) |
| 3546 | { |
| 3547 | if (vim_isspace(line[i])) |
| 3548 | { |
| 3549 | words++; |
| 3550 | is_word = 0; |
| 3551 | } |
| 3552 | } |
| 3553 | else if (!vim_isspace(line[i])) |
| 3554 | is_word = 1; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3555 | ++chars; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3556 | i += (*mb_ptr2len)(line + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | if (is_word) |
| 3560 | words++; |
| 3561 | *wc += words; |
| 3562 | |
| 3563 | /* 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] | 3564 | if (i < limit && line[i] == NUL) |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3565 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3566 | i += eol_size; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3567 | chars += eol_size; |
| 3568 | } |
| 3569 | *cc += chars; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3570 | return i; |
| 3571 | } |
| 3572 | |
| 3573 | /* |
| 3574 | * Give some info about the position of the cursor (for "g CTRL-G"). |
| 3575 | * In Visual mode, give some info about the selected region. (In this case, |
| 3576 | * the *_count_cursor variables store running totals for the selection.) |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3577 | * 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] | 3578 | */ |
| 3579 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3580 | cursor_pos_info(dict_T *dict) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3581 | { |
| 3582 | char_u *p; |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3583 | char_u buf1[50]; |
| 3584 | char_u buf2[40]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3585 | linenr_T lnum; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3586 | varnumber_T byte_count = 0; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3587 | varnumber_T bom_count = 0; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3588 | varnumber_T byte_count_cursor = 0; |
| 3589 | varnumber_T char_count = 0; |
| 3590 | varnumber_T char_count_cursor = 0; |
| 3591 | varnumber_T word_count = 0; |
| 3592 | varnumber_T word_count_cursor = 0; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3593 | int eol_size; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3594 | varnumber_T last_check = 100000L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3595 | long line_count_selected = 0; |
| 3596 | pos_T min_pos, max_pos; |
| 3597 | oparg_T oparg; |
| 3598 | struct block_def bd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3599 | |
| 3600 | /* |
| 3601 | * Compute the length of the file in characters. |
| 3602 | */ |
| 3603 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 3604 | { |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3605 | if (dict == NULL) |
| 3606 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3607 | msg(_(no_lines_msg)); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3608 | return; |
| 3609 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3610 | } |
| 3611 | else |
| 3612 | { |
| 3613 | if (get_fileformat(curbuf) == EOL_DOS) |
| 3614 | eol_size = 2; |
| 3615 | else |
| 3616 | eol_size = 1; |
| 3617 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3618 | if (VIsual_active) |
| 3619 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3620 | if (LT_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3621 | { |
| 3622 | min_pos = VIsual; |
| 3623 | max_pos = curwin->w_cursor; |
| 3624 | } |
| 3625 | else |
| 3626 | { |
| 3627 | min_pos = curwin->w_cursor; |
| 3628 | max_pos = VIsual; |
| 3629 | } |
| 3630 | if (*p_sel == 'e' && max_pos.col > 0) |
| 3631 | --max_pos.col; |
| 3632 | |
| 3633 | if (VIsual_mode == Ctrl_V) |
| 3634 | { |
Bram Moolenaar | 81d0007 | 2009-04-29 15:41:40 +0000 | [diff] [blame] | 3635 | #ifdef FEAT_LINEBREAK |
| 3636 | char_u * saved_sbr = p_sbr; |
| 3637 | |
| 3638 | /* Make 'sbr' empty for a moment to get the correct size. */ |
| 3639 | p_sbr = empty_option; |
| 3640 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3641 | oparg.is_VIsual = 1; |
| 3642 | oparg.block_mode = TRUE; |
| 3643 | oparg.op_type = OP_NOP; |
| 3644 | getvcols(curwin, &min_pos, &max_pos, |
Bram Moolenaar | 5c8837f | 2006-02-25 21:52:33 +0000 | [diff] [blame] | 3645 | &oparg.start_vcol, &oparg.end_vcol); |
Bram Moolenaar | 81d0007 | 2009-04-29 15:41:40 +0000 | [diff] [blame] | 3646 | #ifdef FEAT_LINEBREAK |
| 3647 | p_sbr = saved_sbr; |
| 3648 | #endif |
Bram Moolenaar | 5c8837f | 2006-02-25 21:52:33 +0000 | [diff] [blame] | 3649 | if (curwin->w_curswant == MAXCOL) |
| 3650 | oparg.end_vcol = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3651 | /* Swap the start, end vcol if needed */ |
| 3652 | if (oparg.end_vcol < oparg.start_vcol) |
| 3653 | { |
| 3654 | oparg.end_vcol += oparg.start_vcol; |
| 3655 | oparg.start_vcol = oparg.end_vcol - oparg.start_vcol; |
| 3656 | oparg.end_vcol -= oparg.start_vcol; |
| 3657 | } |
| 3658 | } |
| 3659 | line_count_selected = max_pos.lnum - min_pos.lnum + 1; |
| 3660 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3661 | |
| 3662 | for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) |
| 3663 | { |
| 3664 | /* Check for a CTRL-C every 100000 characters. */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3665 | if (byte_count > last_check) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3666 | { |
| 3667 | ui_breakcheck(); |
| 3668 | if (got_int) |
| 3669 | return; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3670 | last_check = byte_count + 100000L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3673 | /* Do extra processing for VIsual mode. */ |
| 3674 | if (VIsual_active |
| 3675 | && lnum >= min_pos.lnum && lnum <= max_pos.lnum) |
| 3676 | { |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 3677 | char_u *s = NULL; |
| 3678 | long len = 0L; |
| 3679 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3680 | switch (VIsual_mode) |
| 3681 | { |
| 3682 | case Ctrl_V: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3683 | virtual_op = virtual_active(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3684 | block_prep(&oparg, &bd, lnum, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3685 | virtual_op = MAYBE; |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 3686 | s = bd.textstart; |
| 3687 | len = (long)bd.textlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3688 | break; |
| 3689 | case 'V': |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 3690 | s = ml_get(lnum); |
| 3691 | len = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3692 | break; |
| 3693 | case 'v': |
| 3694 | { |
| 3695 | colnr_T start_col = (lnum == min_pos.lnum) |
| 3696 | ? min_pos.col : 0; |
| 3697 | colnr_T end_col = (lnum == max_pos.lnum) |
| 3698 | ? max_pos.col - start_col + 1 : MAXCOL; |
| 3699 | |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 3700 | s = ml_get(lnum) + start_col; |
| 3701 | len = end_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3702 | } |
| 3703 | break; |
| 3704 | } |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 3705 | if (s != NULL) |
| 3706 | { |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3707 | byte_count_cursor += line_count_info(s, &word_count_cursor, |
| 3708 | &char_count_cursor, len, eol_size); |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 3709 | if (lnum == curbuf->b_ml.ml_line_count |
| 3710 | && !curbuf->b_p_eol |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 3711 | && (curbuf->b_p_bin || !curbuf->b_p_fixeol) |
Bram Moolenaar | ec2dad6 | 2005-01-02 11:36:03 +0000 | [diff] [blame] | 3712 | && (long)STRLEN(s) < len) |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3713 | byte_count_cursor -= eol_size; |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 3714 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3715 | } |
| 3716 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3717 | { |
| 3718 | /* In non-visual mode, check for the line the cursor is on */ |
| 3719 | if (lnum == curwin->w_cursor.lnum) |
| 3720 | { |
| 3721 | word_count_cursor += word_count; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3722 | char_count_cursor += char_count; |
| 3723 | byte_count_cursor = byte_count + |
| 3724 | line_count_info(ml_get(lnum), |
| 3725 | &word_count_cursor, &char_count_cursor, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3726 | (varnumber_T)(curwin->w_cursor.col + 1), |
| 3727 | eol_size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3728 | } |
| 3729 | } |
| 3730 | /* Add to the running totals */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3731 | byte_count += line_count_info(ml_get(lnum), &word_count, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3732 | &char_count, (varnumber_T)MAXCOL, |
| 3733 | eol_size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | /* Correction for when last line doesn't have an EOL. */ |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 3737 | 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] | 3738 | byte_count -= eol_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3739 | |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3740 | if (dict == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3741 | { |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3742 | if (VIsual_active) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3743 | { |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3744 | if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL) |
| 3745 | { |
| 3746 | getvcols(curwin, &min_pos, &max_pos, &min_pos.col, |
| 3747 | &max_pos.col); |
| 3748 | vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "), |
| 3749 | (long)(oparg.end_vcol - oparg.start_vcol + 1)); |
| 3750 | } |
| 3751 | else |
| 3752 | buf1[0] = NUL; |
| 3753 | |
| 3754 | if (char_count_cursor == byte_count_cursor |
| 3755 | && char_count == byte_count) |
| 3756 | vim_snprintf((char *)IObuff, IOSIZE, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3757 | _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"), |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3758 | buf1, line_count_selected, |
| 3759 | (long)curbuf->b_ml.ml_line_count, |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3760 | (long_long_T)word_count_cursor, |
| 3761 | (long_long_T)word_count, |
| 3762 | (long_long_T)byte_count_cursor, |
| 3763 | (long_long_T)byte_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3764 | else |
| 3765 | vim_snprintf((char *)IObuff, IOSIZE, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3766 | _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"), |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3767 | buf1, line_count_selected, |
| 3768 | (long)curbuf->b_ml.ml_line_count, |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3769 | (long_long_T)word_count_cursor, |
| 3770 | (long_long_T)word_count, |
| 3771 | (long_long_T)char_count_cursor, |
| 3772 | (long_long_T)char_count, |
| 3773 | (long_long_T)byte_count_cursor, |
| 3774 | (long_long_T)byte_count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3775 | } |
| 3776 | else |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3777 | { |
| 3778 | p = ml_get_curline(); |
| 3779 | validate_virtcol(); |
| 3780 | col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1, |
| 3781 | (int)curwin->w_virtcol + 1); |
| 3782 | col_print(buf2, sizeof(buf2), (int)STRLEN(p), |
| 3783 | linetabsize(p)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3784 | |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3785 | if (char_count_cursor == byte_count_cursor |
| 3786 | && char_count == byte_count) |
| 3787 | vim_snprintf((char *)IObuff, IOSIZE, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3788 | _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"), |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3789 | (char *)buf1, (char *)buf2, |
| 3790 | (long)curwin->w_cursor.lnum, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3791 | (long)curbuf->b_ml.ml_line_count, |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3792 | (long_long_T)word_count_cursor, (long_long_T)word_count, |
| 3793 | (long_long_T)byte_count_cursor, (long_long_T)byte_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3794 | else |
| 3795 | vim_snprintf((char *)IObuff, IOSIZE, |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 3796 | _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"), |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3797 | (char *)buf1, (char *)buf2, |
| 3798 | (long)curwin->w_cursor.lnum, |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3799 | (long)curbuf->b_ml.ml_line_count, |
Bram Moolenaar | 88c86eb | 2019-01-17 17:13:30 +0100 | [diff] [blame] | 3800 | (long_long_T)word_count_cursor, (long_long_T)word_count, |
| 3801 | (long_long_T)char_count_cursor, (long_long_T)char_count, |
| 3802 | (long_long_T)byte_count_cursor, (long_long_T)byte_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3803 | } |
| 3804 | } |
| 3805 | |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3806 | bom_count = bomb_size(); |
Bram Moolenaar | dbec749 | 2019-09-13 22:16:21 +0200 | [diff] [blame] | 3807 | if (dict == NULL && bom_count > 0) |
Bram Moolenaar | c71982b | 2016-01-04 21:43:08 +0100 | [diff] [blame] | 3808 | vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE, |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 3809 | _("(+%lld for BOM)"), (long_long_T)bom_count); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3810 | if (dict == NULL) |
| 3811 | { |
Bram Moolenaar | dbec749 | 2019-09-13 22:16:21 +0200 | [diff] [blame] | 3812 | // Don't shorten this message, the user asked for it. |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3813 | p = p_shm; |
| 3814 | p_shm = (char_u *)""; |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3815 | msg((char *)IObuff); |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3816 | p_shm = p; |
| 3817 | } |
| 3818 | } |
Bram Moolenaar | 718272a | 2016-01-03 22:56:45 +0100 | [diff] [blame] | 3819 | #if defined(FEAT_EVAL) |
Bram Moolenaar | ed767a2 | 2016-01-03 22:49:16 +0100 | [diff] [blame] | 3820 | if (dict != NULL) |
| 3821 | { |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 3822 | dict_add_number(dict, "words", word_count); |
| 3823 | dict_add_number(dict, "chars", char_count); |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 3824 | dict_add_number(dict, "bytes", byte_count + bom_count); |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 3825 | dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes", |
| 3826 | byte_count_cursor); |
| 3827 | dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars", |
| 3828 | char_count_cursor); |
| 3829 | dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words", |
| 3830 | word_count_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3831 | } |
Bram Moolenaar | 718272a | 2016-01-03 22:56:45 +0100 | [diff] [blame] | 3832 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3833 | } |
Bram Moolenaar | 792cf5e | 2019-09-30 23:12:16 +0200 | [diff] [blame] | 3834 | |
| 3835 | /* |
| 3836 | * Handle indent and format operators and visual mode ":". |
| 3837 | */ |
| 3838 | static void |
| 3839 | op_colon(oparg_T *oap) |
| 3840 | { |
| 3841 | stuffcharReadbuff(':'); |
| 3842 | if (oap->is_VIsual) |
| 3843 | stuffReadbuff((char_u *)"'<,'>"); |
| 3844 | else |
| 3845 | { |
| 3846 | // Make the range look nice, so it can be repeated. |
| 3847 | if (oap->start.lnum == curwin->w_cursor.lnum) |
| 3848 | stuffcharReadbuff('.'); |
| 3849 | else |
| 3850 | stuffnumReadbuff((long)oap->start.lnum); |
| 3851 | if (oap->end.lnum != oap->start.lnum) |
| 3852 | { |
| 3853 | stuffcharReadbuff(','); |
| 3854 | if (oap->end.lnum == curwin->w_cursor.lnum) |
| 3855 | stuffcharReadbuff('.'); |
| 3856 | else if (oap->end.lnum == curbuf->b_ml.ml_line_count) |
| 3857 | stuffcharReadbuff('$'); |
| 3858 | else if (oap->start.lnum == curwin->w_cursor.lnum) |
| 3859 | { |
| 3860 | stuffReadbuff((char_u *)".+"); |
| 3861 | stuffnumReadbuff((long)oap->line_count - 1); |
| 3862 | } |
| 3863 | else |
| 3864 | stuffnumReadbuff((long)oap->end.lnum); |
| 3865 | } |
| 3866 | } |
| 3867 | if (oap->op_type != OP_COLON) |
| 3868 | stuffReadbuff((char_u *)"!"); |
| 3869 | if (oap->op_type == OP_INDENT) |
| 3870 | { |
| 3871 | #ifndef FEAT_CINDENT |
| 3872 | if (*get_equalprg() == NUL) |
| 3873 | stuffReadbuff((char_u *)"indent"); |
| 3874 | else |
| 3875 | #endif |
| 3876 | stuffReadbuff(get_equalprg()); |
| 3877 | stuffReadbuff((char_u *)"\n"); |
| 3878 | } |
| 3879 | else if (oap->op_type == OP_FORMAT) |
| 3880 | { |
| 3881 | if (*curbuf->b_p_fp != NUL) |
| 3882 | stuffReadbuff(curbuf->b_p_fp); |
| 3883 | else if (*p_fp != NUL) |
| 3884 | stuffReadbuff(p_fp); |
| 3885 | else |
| 3886 | stuffReadbuff((char_u *)"fmt"); |
| 3887 | stuffReadbuff((char_u *)"\n']"); |
| 3888 | } |
| 3889 | |
| 3890 | // do_cmdline() does the rest |
| 3891 | } |
| 3892 | |
| 3893 | /* |
| 3894 | * Handle the "g@" operator: call 'operatorfunc'. |
| 3895 | */ |
| 3896 | static void |
| 3897 | op_function(oparg_T *oap UNUSED) |
| 3898 | { |
| 3899 | #ifdef FEAT_EVAL |
| 3900 | typval_T argv[2]; |
| 3901 | int save_virtual_op = virtual_op; |
| 3902 | |
| 3903 | if (*p_opfunc == NUL) |
| 3904 | emsg(_("E774: 'operatorfunc' is empty")); |
| 3905 | else |
| 3906 | { |
| 3907 | // Set '[ and '] marks to text to be operated on. |
| 3908 | curbuf->b_op_start = oap->start; |
| 3909 | curbuf->b_op_end = oap->end; |
| 3910 | if (oap->motion_type != MLINE && !oap->inclusive) |
| 3911 | // Exclude the end position. |
| 3912 | decl(&curbuf->b_op_end); |
| 3913 | |
| 3914 | argv[0].v_type = VAR_STRING; |
| 3915 | if (oap->block_mode) |
| 3916 | argv[0].vval.v_string = (char_u *)"block"; |
| 3917 | else if (oap->motion_type == MLINE) |
| 3918 | argv[0].vval.v_string = (char_u *)"line"; |
| 3919 | else |
| 3920 | argv[0].vval.v_string = (char_u *)"char"; |
| 3921 | argv[1].v_type = VAR_UNKNOWN; |
| 3922 | |
| 3923 | // Reset virtual_op so that 'virtualedit' can be changed in the |
| 3924 | // function. |
| 3925 | virtual_op = MAYBE; |
| 3926 | |
| 3927 | (void)call_func_retnr(p_opfunc, 1, argv); |
| 3928 | |
| 3929 | virtual_op = save_virtual_op; |
| 3930 | } |
| 3931 | #else |
| 3932 | emsg(_("E775: Eval feature not available")); |
| 3933 | #endif |
| 3934 | } |
| 3935 | |
| 3936 | /* |
| 3937 | * Calculate start/end virtual columns for operating in block mode. |
| 3938 | */ |
| 3939 | static void |
| 3940 | get_op_vcol( |
| 3941 | oparg_T *oap, |
| 3942 | colnr_T redo_VIsual_vcol, |
| 3943 | int initial) // when TRUE adjust position for 'selectmode' |
| 3944 | { |
| 3945 | colnr_T start, end; |
| 3946 | |
| 3947 | if (VIsual_mode != Ctrl_V |
| 3948 | || (!initial && oap->end.col < curwin->w_width)) |
| 3949 | return; |
| 3950 | |
| 3951 | oap->block_mode = TRUE; |
| 3952 | |
| 3953 | // prevent from moving onto a trail byte |
| 3954 | if (has_mbyte) |
| 3955 | mb_adjustpos(curwin->w_buffer, &oap->end); |
| 3956 | |
| 3957 | getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol); |
| 3958 | |
| 3959 | if (!redo_VIsual_busy) |
| 3960 | { |
| 3961 | getvvcol(curwin, &(oap->end), &start, NULL, &end); |
| 3962 | |
| 3963 | if (start < oap->start_vcol) |
| 3964 | oap->start_vcol = start; |
| 3965 | if (end > oap->end_vcol) |
| 3966 | { |
| 3967 | if (initial && *p_sel == 'e' && start >= 1 |
| 3968 | && start - 1 >= oap->end_vcol) |
| 3969 | oap->end_vcol = start - 1; |
| 3970 | else |
| 3971 | oap->end_vcol = end; |
| 3972 | } |
| 3973 | } |
| 3974 | |
| 3975 | // if '$' was used, get oap->end_vcol from longest line |
| 3976 | if (curwin->w_curswant == MAXCOL) |
| 3977 | { |
| 3978 | curwin->w_cursor.col = MAXCOL; |
| 3979 | oap->end_vcol = 0; |
| 3980 | for (curwin->w_cursor.lnum = oap->start.lnum; |
| 3981 | curwin->w_cursor.lnum <= oap->end.lnum; |
| 3982 | ++curwin->w_cursor.lnum) |
| 3983 | { |
| 3984 | getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end); |
| 3985 | if (end > oap->end_vcol) |
| 3986 | oap->end_vcol = end; |
| 3987 | } |
| 3988 | } |
| 3989 | else if (redo_VIsual_busy) |
| 3990 | oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1; |
| 3991 | // Correct oap->end.col and oap->start.col to be the |
| 3992 | // upper-left and lower-right corner of the block area. |
| 3993 | // |
| 3994 | // (Actually, this does convert column positions into character |
| 3995 | // positions) |
| 3996 | curwin->w_cursor.lnum = oap->end.lnum; |
| 3997 | coladvance(oap->end_vcol); |
| 3998 | oap->end = curwin->w_cursor; |
| 3999 | |
| 4000 | curwin->w_cursor = oap->start; |
| 4001 | coladvance(oap->start_vcol); |
| 4002 | oap->start = curwin->w_cursor; |
| 4003 | } |
| 4004 | |
| 4005 | /* |
| 4006 | * Handle an operator after Visual mode or when the movement is finished. |
| 4007 | * "gui_yank" is true when yanking text for the clipboard. |
| 4008 | */ |
| 4009 | void |
| 4010 | do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank) |
| 4011 | { |
| 4012 | oparg_T *oap = cap->oap; |
| 4013 | pos_T old_cursor; |
| 4014 | int empty_region_error; |
| 4015 | int restart_edit_save; |
| 4016 | #ifdef FEAT_LINEBREAK |
| 4017 | int lbr_saved = curwin->w_p_lbr; |
| 4018 | #endif |
| 4019 | |
| 4020 | // The visual area is remembered for redo |
| 4021 | static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V |
| 4022 | static linenr_T redo_VIsual_line_count; // number of lines |
| 4023 | static colnr_T redo_VIsual_vcol; // number of cols or end column |
| 4024 | static long redo_VIsual_count; // count for Visual operator |
| 4025 | static int redo_VIsual_arg; // extra argument |
| 4026 | int include_line_break = FALSE; |
| 4027 | |
| 4028 | #if defined(FEAT_CLIPBOARD) |
| 4029 | // Yank the visual area into the GUI selection register before we operate |
| 4030 | // on it and lose it forever. |
| 4031 | // Don't do it if a specific register was specified, so that ""x"*P works. |
| 4032 | // This could call do_pending_operator() recursively, but that's OK |
| 4033 | // because gui_yank will be TRUE for the nested call. |
| 4034 | if ((clip_star.available || clip_plus.available) |
| 4035 | && oap->op_type != OP_NOP |
| 4036 | && !gui_yank |
| 4037 | && VIsual_active |
| 4038 | && !redo_VIsual_busy |
| 4039 | && oap->regname == 0) |
| 4040 | clip_auto_select(); |
| 4041 | #endif |
| 4042 | old_cursor = curwin->w_cursor; |
| 4043 | |
| 4044 | // If an operation is pending, handle it... |
| 4045 | if ((finish_op || VIsual_active) && oap->op_type != OP_NOP) |
| 4046 | { |
| 4047 | // Yank can be redone when 'y' is in 'cpoptions', but not when yanking |
| 4048 | // for the clipboard. |
| 4049 | int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank; |
| 4050 | |
| 4051 | #ifdef FEAT_LINEBREAK |
| 4052 | // Avoid a problem with unwanted linebreaks in block mode. |
| 4053 | if (curwin->w_p_lbr) |
| 4054 | curwin->w_valid &= ~VALID_VIRTCOL; |
| 4055 | curwin->w_p_lbr = FALSE; |
| 4056 | #endif |
| 4057 | oap->is_VIsual = VIsual_active; |
| 4058 | if (oap->motion_force == 'V') |
| 4059 | oap->motion_type = MLINE; |
| 4060 | else if (oap->motion_force == 'v') |
| 4061 | { |
| 4062 | // If the motion was linewise, "inclusive" will not have been set. |
| 4063 | // Use "exclusive" to be consistent. Makes "dvj" work nice. |
| 4064 | if (oap->motion_type == MLINE) |
| 4065 | oap->inclusive = FALSE; |
| 4066 | // If the motion already was characterwise, toggle "inclusive" |
| 4067 | else if (oap->motion_type == MCHAR) |
| 4068 | oap->inclusive = !oap->inclusive; |
| 4069 | oap->motion_type = MCHAR; |
| 4070 | } |
| 4071 | else if (oap->motion_force == Ctrl_V) |
| 4072 | { |
| 4073 | // Change line- or characterwise motion into Visual block mode. |
| 4074 | if (!VIsual_active) |
| 4075 | { |
| 4076 | VIsual_active = TRUE; |
| 4077 | VIsual = oap->start; |
| 4078 | } |
| 4079 | VIsual_mode = Ctrl_V; |
| 4080 | VIsual_select = FALSE; |
| 4081 | VIsual_reselect = FALSE; |
| 4082 | } |
| 4083 | |
| 4084 | // Only redo yank when 'y' flag is in 'cpoptions'. |
| 4085 | // Never redo "zf" (define fold). |
| 4086 | if ((redo_yank || oap->op_type != OP_YANK) |
| 4087 | && ((!VIsual_active || oap->motion_force) |
| 4088 | // Also redo Operator-pending Visual mode mappings |
| 4089 | || (VIsual_active && cap->cmdchar == ':' |
| 4090 | && oap->op_type != OP_COLON)) |
| 4091 | && cap->cmdchar != 'D' |
| 4092 | #ifdef FEAT_FOLDING |
| 4093 | && oap->op_type != OP_FOLD |
| 4094 | && oap->op_type != OP_FOLDOPEN |
| 4095 | && oap->op_type != OP_FOLDOPENREC |
| 4096 | && oap->op_type != OP_FOLDCLOSE |
| 4097 | && oap->op_type != OP_FOLDCLOSEREC |
| 4098 | && oap->op_type != OP_FOLDDEL |
| 4099 | && oap->op_type != OP_FOLDDELREC |
| 4100 | #endif |
| 4101 | ) |
| 4102 | { |
| 4103 | prep_redo(oap->regname, cap->count0, |
| 4104 | get_op_char(oap->op_type), get_extra_op_char(oap->op_type), |
| 4105 | oap->motion_force, cap->cmdchar, cap->nchar); |
| 4106 | if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search |
| 4107 | { |
| 4108 | // If 'cpoptions' does not contain 'r', insert the search |
| 4109 | // pattern to really repeat the same command. |
| 4110 | if (vim_strchr(p_cpo, CPO_REDO) == NULL) |
| 4111 | AppendToRedobuffLit(cap->searchbuf, -1); |
| 4112 | AppendToRedobuff(NL_STR); |
| 4113 | } |
| 4114 | else if (cap->cmdchar == ':') |
| 4115 | { |
| 4116 | // do_cmdline() has stored the first typed line in |
| 4117 | // "repeat_cmdline". When several lines are typed repeating |
| 4118 | // won't be possible. |
| 4119 | if (repeat_cmdline == NULL) |
| 4120 | ResetRedobuff(); |
| 4121 | else |
| 4122 | { |
| 4123 | AppendToRedobuffLit(repeat_cmdline, -1); |
| 4124 | AppendToRedobuff(NL_STR); |
| 4125 | VIM_CLEAR(repeat_cmdline); |
| 4126 | } |
| 4127 | } |
| 4128 | } |
| 4129 | |
| 4130 | if (redo_VIsual_busy) |
| 4131 | { |
| 4132 | // Redo of an operation on a Visual area. Use the same size from |
| 4133 | // redo_VIsual_line_count and redo_VIsual_vcol. |
| 4134 | oap->start = curwin->w_cursor; |
| 4135 | curwin->w_cursor.lnum += redo_VIsual_line_count - 1; |
| 4136 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 4137 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 4138 | VIsual_mode = redo_VIsual_mode; |
| 4139 | if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v') |
| 4140 | { |
| 4141 | if (VIsual_mode == 'v') |
| 4142 | { |
| 4143 | if (redo_VIsual_line_count <= 1) |
| 4144 | { |
| 4145 | validate_virtcol(); |
| 4146 | curwin->w_curswant = |
| 4147 | curwin->w_virtcol + redo_VIsual_vcol - 1; |
| 4148 | } |
| 4149 | else |
| 4150 | curwin->w_curswant = redo_VIsual_vcol; |
| 4151 | } |
| 4152 | else |
| 4153 | { |
| 4154 | curwin->w_curswant = MAXCOL; |
| 4155 | } |
| 4156 | coladvance(curwin->w_curswant); |
| 4157 | } |
| 4158 | cap->count0 = redo_VIsual_count; |
| 4159 | if (redo_VIsual_count != 0) |
| 4160 | cap->count1 = redo_VIsual_count; |
| 4161 | else |
| 4162 | cap->count1 = 1; |
| 4163 | } |
| 4164 | else if (VIsual_active) |
| 4165 | { |
| 4166 | if (!gui_yank) |
| 4167 | { |
| 4168 | // Save the current VIsual area for '< and '> marks, and "gv" |
| 4169 | curbuf->b_visual.vi_start = VIsual; |
| 4170 | curbuf->b_visual.vi_end = curwin->w_cursor; |
| 4171 | curbuf->b_visual.vi_mode = VIsual_mode; |
| 4172 | restore_visual_mode(); |
| 4173 | curbuf->b_visual.vi_curswant = curwin->w_curswant; |
| 4174 | # ifdef FEAT_EVAL |
| 4175 | curbuf->b_visual_mode_eval = VIsual_mode; |
| 4176 | # endif |
| 4177 | } |
| 4178 | |
| 4179 | // In Select mode, a linewise selection is operated upon like a |
| 4180 | // characterwise selection. |
| 4181 | // Special case: gH<Del> deletes the last line. |
| 4182 | if (VIsual_select && VIsual_mode == 'V' |
| 4183 | && cap->oap->op_type != OP_DELETE) |
| 4184 | { |
| 4185 | if (LT_POS(VIsual, curwin->w_cursor)) |
| 4186 | { |
| 4187 | VIsual.col = 0; |
| 4188 | curwin->w_cursor.col = |
| 4189 | (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum)); |
| 4190 | } |
| 4191 | else |
| 4192 | { |
| 4193 | curwin->w_cursor.col = 0; |
| 4194 | VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum)); |
| 4195 | } |
| 4196 | VIsual_mode = 'v'; |
| 4197 | } |
| 4198 | // If 'selection' is "exclusive", backup one character for |
| 4199 | // charwise selections. |
| 4200 | else if (VIsual_mode == 'v') |
| 4201 | include_line_break = unadjust_for_sel(); |
| 4202 | |
| 4203 | oap->start = VIsual; |
| 4204 | if (VIsual_mode == 'V') |
| 4205 | { |
| 4206 | oap->start.col = 0; |
| 4207 | oap->start.coladd = 0; |
| 4208 | } |
| 4209 | } |
| 4210 | |
| 4211 | // Set oap->start to the first position of the operated text, oap->end |
| 4212 | // to the end of the operated text. w_cursor is equal to oap->start. |
| 4213 | if (LT_POS(oap->start, curwin->w_cursor)) |
| 4214 | { |
| 4215 | #ifdef FEAT_FOLDING |
| 4216 | // Include folded lines completely. |
| 4217 | if (!VIsual_active) |
| 4218 | { |
| 4219 | if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL)) |
| 4220 | oap->start.col = 0; |
| 4221 | if ((curwin->w_cursor.col > 0 || oap->inclusive) |
| 4222 | && hasFolding(curwin->w_cursor.lnum, NULL, |
| 4223 | &curwin->w_cursor.lnum)) |
| 4224 | curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline()); |
| 4225 | } |
| 4226 | #endif |
| 4227 | oap->end = curwin->w_cursor; |
| 4228 | curwin->w_cursor = oap->start; |
| 4229 | |
| 4230 | // w_virtcol may have been updated; if the cursor goes back to its |
| 4231 | // previous position w_virtcol becomes invalid and isn't updated |
| 4232 | // automatically. |
| 4233 | curwin->w_valid &= ~VALID_VIRTCOL; |
| 4234 | } |
| 4235 | else |
| 4236 | { |
| 4237 | #ifdef FEAT_FOLDING |
| 4238 | // Include folded lines completely. |
| 4239 | if (!VIsual_active && oap->motion_type == MLINE) |
| 4240 | { |
| 4241 | if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, |
| 4242 | NULL)) |
| 4243 | curwin->w_cursor.col = 0; |
| 4244 | if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum)) |
| 4245 | oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum)); |
| 4246 | } |
| 4247 | #endif |
| 4248 | oap->end = oap->start; |
| 4249 | oap->start = curwin->w_cursor; |
| 4250 | } |
| 4251 | |
| 4252 | // Just in case lines were deleted that make the position invalid. |
| 4253 | check_pos(curwin->w_buffer, &oap->end); |
| 4254 | oap->line_count = oap->end.lnum - oap->start.lnum + 1; |
| 4255 | |
| 4256 | // Set "virtual_op" before resetting VIsual_active. |
| 4257 | virtual_op = virtual_active(); |
| 4258 | |
| 4259 | if (VIsual_active || redo_VIsual_busy) |
| 4260 | { |
| 4261 | get_op_vcol(oap, redo_VIsual_vcol, TRUE); |
| 4262 | |
| 4263 | if (!redo_VIsual_busy && !gui_yank) |
| 4264 | { |
| 4265 | // Prepare to reselect and redo Visual: this is based on the |
| 4266 | // size of the Visual text |
| 4267 | resel_VIsual_mode = VIsual_mode; |
| 4268 | if (curwin->w_curswant == MAXCOL) |
| 4269 | resel_VIsual_vcol = MAXCOL; |
| 4270 | else |
| 4271 | { |
| 4272 | if (VIsual_mode != Ctrl_V) |
| 4273 | getvvcol(curwin, &(oap->end), |
| 4274 | NULL, NULL, &oap->end_vcol); |
| 4275 | if (VIsual_mode == Ctrl_V || oap->line_count <= 1) |
| 4276 | { |
| 4277 | if (VIsual_mode != Ctrl_V) |
| 4278 | getvvcol(curwin, &(oap->start), |
| 4279 | &oap->start_vcol, NULL, NULL); |
| 4280 | resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1; |
| 4281 | } |
| 4282 | else |
| 4283 | resel_VIsual_vcol = oap->end_vcol; |
| 4284 | } |
| 4285 | resel_VIsual_line_count = oap->line_count; |
| 4286 | } |
| 4287 | |
| 4288 | // can't redo yank (unless 'y' is in 'cpoptions') and ":" |
| 4289 | if ((redo_yank || oap->op_type != OP_YANK) |
| 4290 | && oap->op_type != OP_COLON |
| 4291 | #ifdef FEAT_FOLDING |
| 4292 | && oap->op_type != OP_FOLD |
| 4293 | && oap->op_type != OP_FOLDOPEN |
| 4294 | && oap->op_type != OP_FOLDOPENREC |
| 4295 | && oap->op_type != OP_FOLDCLOSE |
| 4296 | && oap->op_type != OP_FOLDCLOSEREC |
| 4297 | && oap->op_type != OP_FOLDDEL |
| 4298 | && oap->op_type != OP_FOLDDELREC |
| 4299 | #endif |
| 4300 | && oap->motion_force == NUL |
| 4301 | ) |
| 4302 | { |
| 4303 | // Prepare for redoing. Only use the nchar field for "r", |
| 4304 | // otherwise it might be the second char of the operator. |
| 4305 | if (cap->cmdchar == 'g' && (cap->nchar == 'n' |
| 4306 | || cap->nchar == 'N')) |
| 4307 | prep_redo(oap->regname, cap->count0, |
| 4308 | get_op_char(oap->op_type), get_extra_op_char(oap->op_type), |
| 4309 | oap->motion_force, cap->cmdchar, cap->nchar); |
| 4310 | else if (cap->cmdchar != ':') |
| 4311 | { |
| 4312 | int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL; |
| 4313 | |
| 4314 | // reverse what nv_replace() did |
| 4315 | if (nchar == REPLACE_CR_NCHAR) |
| 4316 | nchar = CAR; |
| 4317 | else if (nchar == REPLACE_NL_NCHAR) |
| 4318 | nchar = NL; |
| 4319 | prep_redo(oap->regname, 0L, NUL, 'v', |
| 4320 | get_op_char(oap->op_type), |
| 4321 | get_extra_op_char(oap->op_type), |
| 4322 | nchar); |
| 4323 | } |
| 4324 | if (!redo_VIsual_busy) |
| 4325 | { |
| 4326 | redo_VIsual_mode = resel_VIsual_mode; |
| 4327 | redo_VIsual_vcol = resel_VIsual_vcol; |
| 4328 | redo_VIsual_line_count = resel_VIsual_line_count; |
| 4329 | redo_VIsual_count = cap->count0; |
| 4330 | redo_VIsual_arg = cap->arg; |
| 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | // oap->inclusive defaults to TRUE. |
| 4335 | // If oap->end is on a NUL (empty line) oap->inclusive becomes |
| 4336 | // FALSE. This makes "d}P" and "v}dP" work the same. |
| 4337 | if (oap->motion_force == NUL || oap->motion_type == MLINE) |
| 4338 | oap->inclusive = TRUE; |
| 4339 | if (VIsual_mode == 'V') |
| 4340 | oap->motion_type = MLINE; |
| 4341 | else |
| 4342 | { |
| 4343 | oap->motion_type = MCHAR; |
| 4344 | if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL |
| 4345 | && (include_line_break || !virtual_op)) |
| 4346 | { |
| 4347 | oap->inclusive = FALSE; |
| 4348 | // Try to include the newline, unless it's an operator |
| 4349 | // that works on lines only. |
| 4350 | if (*p_sel != 'o' |
| 4351 | && !op_on_lines(oap->op_type) |
| 4352 | && oap->end.lnum < curbuf->b_ml.ml_line_count) |
| 4353 | { |
| 4354 | ++oap->end.lnum; |
| 4355 | oap->end.col = 0; |
| 4356 | oap->end.coladd = 0; |
| 4357 | ++oap->line_count; |
| 4358 | } |
| 4359 | } |
| 4360 | } |
| 4361 | |
| 4362 | redo_VIsual_busy = FALSE; |
| 4363 | |
| 4364 | // Switch Visual off now, so screen updating does |
| 4365 | // not show inverted text when the screen is redrawn. |
| 4366 | // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is |
| 4367 | // no screen redraw, so it is done here to remove the inverted |
| 4368 | // part. |
| 4369 | if (!gui_yank) |
| 4370 | { |
| 4371 | VIsual_active = FALSE; |
Bram Moolenaar | 792cf5e | 2019-09-30 23:12:16 +0200 | [diff] [blame] | 4372 | setmouse(); |
| 4373 | mouse_dragging = 0; |
Bram Moolenaar | 792cf5e | 2019-09-30 23:12:16 +0200 | [diff] [blame] | 4374 | may_clear_cmdline(); |
| 4375 | if ((oap->op_type == OP_YANK |
| 4376 | || oap->op_type == OP_COLON |
| 4377 | || oap->op_type == OP_FUNCTION |
| 4378 | || oap->op_type == OP_FILTER) |
| 4379 | && oap->motion_force == NUL) |
| 4380 | { |
| 4381 | #ifdef FEAT_LINEBREAK |
| 4382 | // make sure redrawing is correct |
| 4383 | curwin->w_p_lbr = lbr_saved; |
| 4384 | #endif |
| 4385 | redraw_curbuf_later(INVERTED); |
| 4386 | } |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | // Include the trailing byte of a multi-byte char. |
| 4391 | if (has_mbyte && oap->inclusive) |
| 4392 | { |
| 4393 | int l; |
| 4394 | |
| 4395 | l = (*mb_ptr2len)(ml_get_pos(&oap->end)); |
| 4396 | if (l > 1) |
| 4397 | oap->end.col += l - 1; |
| 4398 | } |
| 4399 | curwin->w_set_curswant = TRUE; |
| 4400 | |
| 4401 | // oap->empty is set when start and end are the same. The inclusive |
| 4402 | // flag affects this too, unless yanking and the end is on a NUL. |
| 4403 | oap->empty = (oap->motion_type == MCHAR |
| 4404 | && (!oap->inclusive |
| 4405 | || (oap->op_type == OP_YANK |
| 4406 | && gchar_pos(&oap->end) == NUL)) |
| 4407 | && EQUAL_POS(oap->start, oap->end) |
| 4408 | && !(virtual_op && oap->start.coladd != oap->end.coladd)); |
| 4409 | // For delete, change and yank, it's an error to operate on an |
| 4410 | // empty region, when 'E' included in 'cpoptions' (Vi compatible). |
| 4411 | empty_region_error = (oap->empty |
| 4412 | && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL); |
| 4413 | |
| 4414 | // Force a redraw when operating on an empty Visual region, when |
| 4415 | // 'modifiable is off or creating a fold. |
| 4416 | if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma |
| 4417 | #ifdef FEAT_FOLDING |
| 4418 | || oap->op_type == OP_FOLD |
| 4419 | #endif |
| 4420 | )) |
| 4421 | { |
| 4422 | #ifdef FEAT_LINEBREAK |
| 4423 | curwin->w_p_lbr = lbr_saved; |
| 4424 | #endif |
| 4425 | redraw_curbuf_later(INVERTED); |
| 4426 | } |
| 4427 | |
| 4428 | // If the end of an operator is in column one while oap->motion_type |
| 4429 | // is MCHAR and oap->inclusive is FALSE, we put op_end after the last |
| 4430 | // character in the previous line. If op_start is on or before the |
| 4431 | // first non-blank in the line, the operator becomes linewise |
| 4432 | // (strange, but that's the way vi does it). |
| 4433 | if ( oap->motion_type == MCHAR |
| 4434 | && oap->inclusive == FALSE |
| 4435 | && !(cap->retval & CA_NO_ADJ_OP_END) |
| 4436 | && oap->end.col == 0 |
| 4437 | && (!oap->is_VIsual || *p_sel == 'o') |
| 4438 | && !oap->block_mode |
| 4439 | && oap->line_count > 1) |
| 4440 | { |
| 4441 | oap->end_adjusted = TRUE; // remember that we did this |
| 4442 | --oap->line_count; |
| 4443 | --oap->end.lnum; |
| 4444 | if (inindent(0)) |
| 4445 | oap->motion_type = MLINE; |
| 4446 | else |
| 4447 | { |
| 4448 | oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 4449 | if (oap->end.col) |
| 4450 | { |
| 4451 | --oap->end.col; |
| 4452 | oap->inclusive = TRUE; |
| 4453 | } |
| 4454 | } |
| 4455 | } |
| 4456 | else |
| 4457 | oap->end_adjusted = FALSE; |
| 4458 | |
| 4459 | switch (oap->op_type) |
| 4460 | { |
| 4461 | case OP_LSHIFT: |
| 4462 | case OP_RSHIFT: |
| 4463 | op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1); |
| 4464 | auto_format(FALSE, TRUE); |
| 4465 | break; |
| 4466 | |
| 4467 | case OP_JOIN_NS: |
| 4468 | case OP_JOIN: |
| 4469 | if (oap->line_count < 2) |
| 4470 | oap->line_count = 2; |
| 4471 | if (curwin->w_cursor.lnum + oap->line_count - 1 > |
| 4472 | curbuf->b_ml.ml_line_count) |
| 4473 | beep_flush(); |
| 4474 | else |
| 4475 | { |
| 4476 | (void)do_join(oap->line_count, oap->op_type == OP_JOIN, |
| 4477 | TRUE, TRUE, TRUE); |
| 4478 | auto_format(FALSE, TRUE); |
| 4479 | } |
| 4480 | break; |
| 4481 | |
| 4482 | case OP_DELETE: |
| 4483 | VIsual_reselect = FALSE; // don't reselect now |
| 4484 | if (empty_region_error) |
| 4485 | { |
| 4486 | vim_beep(BO_OPER); |
| 4487 | CancelRedo(); |
| 4488 | } |
| 4489 | else |
| 4490 | { |
| 4491 | (void)op_delete(oap); |
| 4492 | if (oap->motion_type == MLINE && has_format_option(FO_AUTO)) |
| 4493 | u_save_cursor(); // cursor line wasn't saved yet |
| 4494 | auto_format(FALSE, TRUE); |
| 4495 | } |
| 4496 | break; |
| 4497 | |
| 4498 | case OP_YANK: |
| 4499 | if (empty_region_error) |
| 4500 | { |
| 4501 | if (!gui_yank) |
| 4502 | { |
| 4503 | vim_beep(BO_OPER); |
| 4504 | CancelRedo(); |
| 4505 | } |
| 4506 | } |
| 4507 | else |
| 4508 | { |
| 4509 | #ifdef FEAT_LINEBREAK |
| 4510 | curwin->w_p_lbr = lbr_saved; |
| 4511 | #endif |
| 4512 | (void)op_yank(oap, FALSE, !gui_yank); |
| 4513 | } |
| 4514 | check_cursor_col(); |
| 4515 | break; |
| 4516 | |
| 4517 | case OP_CHANGE: |
| 4518 | VIsual_reselect = FALSE; // don't reselect now |
| 4519 | if (empty_region_error) |
| 4520 | { |
| 4521 | vim_beep(BO_OPER); |
| 4522 | CancelRedo(); |
| 4523 | } |
| 4524 | else |
| 4525 | { |
| 4526 | // This is a new edit command, not a restart. Need to |
| 4527 | // remember it to make 'insertmode' work with mappings for |
| 4528 | // Visual mode. But do this only once and not when typed and |
| 4529 | // 'insertmode' isn't set. |
| 4530 | if (p_im || !KeyTyped) |
| 4531 | restart_edit_save = restart_edit; |
| 4532 | else |
| 4533 | restart_edit_save = 0; |
| 4534 | restart_edit = 0; |
| 4535 | #ifdef FEAT_LINEBREAK |
| 4536 | // Restore linebreak, so that when the user edits it looks as |
| 4537 | // before. |
| 4538 | if (curwin->w_p_lbr != lbr_saved) |
| 4539 | { |
| 4540 | curwin->w_p_lbr = lbr_saved; |
| 4541 | get_op_vcol(oap, redo_VIsual_mode, FALSE); |
| 4542 | } |
| 4543 | #endif |
| 4544 | // Reset finish_op now, don't want it set inside edit(). |
| 4545 | finish_op = FALSE; |
| 4546 | if (op_change(oap)) // will call edit() |
| 4547 | cap->retval |= CA_COMMAND_BUSY; |
| 4548 | if (restart_edit == 0) |
| 4549 | restart_edit = restart_edit_save; |
| 4550 | } |
| 4551 | break; |
| 4552 | |
| 4553 | case OP_FILTER: |
| 4554 | if (vim_strchr(p_cpo, CPO_FILTER) != NULL) |
| 4555 | AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd |
| 4556 | else |
| 4557 | bangredo = TRUE; // do_bang() will put cmd in redo buffer |
| 4558 | // FALLTHROUGH |
| 4559 | |
| 4560 | case OP_INDENT: |
| 4561 | case OP_COLON: |
| 4562 | |
| 4563 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
| 4564 | // If 'equalprg' is empty, do the indenting internally. |
| 4565 | if (oap->op_type == OP_INDENT && *get_equalprg() == NUL) |
| 4566 | { |
| 4567 | # ifdef FEAT_LISP |
| 4568 | if (curbuf->b_p_lisp) |
| 4569 | { |
| 4570 | op_reindent(oap, get_lisp_indent); |
| 4571 | break; |
| 4572 | } |
| 4573 | # endif |
| 4574 | # ifdef FEAT_CINDENT |
| 4575 | op_reindent(oap, |
| 4576 | # ifdef FEAT_EVAL |
| 4577 | *curbuf->b_p_inde != NUL ? get_expr_indent : |
| 4578 | # endif |
| 4579 | get_c_indent); |
| 4580 | break; |
| 4581 | # endif |
| 4582 | } |
| 4583 | #endif |
| 4584 | |
| 4585 | op_colon(oap); |
| 4586 | break; |
| 4587 | |
| 4588 | case OP_TILDE: |
| 4589 | case OP_UPPER: |
| 4590 | case OP_LOWER: |
| 4591 | case OP_ROT13: |
| 4592 | if (empty_region_error) |
| 4593 | { |
| 4594 | vim_beep(BO_OPER); |
| 4595 | CancelRedo(); |
| 4596 | } |
| 4597 | else |
| 4598 | op_tilde(oap); |
| 4599 | check_cursor_col(); |
| 4600 | break; |
| 4601 | |
| 4602 | case OP_FORMAT: |
| 4603 | #if defined(FEAT_EVAL) |
| 4604 | if (*curbuf->b_p_fex != NUL) |
| 4605 | op_formatexpr(oap); // use expression |
| 4606 | else |
| 4607 | #endif |
| 4608 | if (*p_fp != NUL || *curbuf->b_p_fp != NUL) |
| 4609 | op_colon(oap); // use external command |
| 4610 | else |
| 4611 | op_format(oap, FALSE); // use internal function |
| 4612 | break; |
| 4613 | |
| 4614 | case OP_FORMAT2: |
| 4615 | op_format(oap, TRUE); // use internal function |
| 4616 | break; |
| 4617 | |
| 4618 | case OP_FUNCTION: |
| 4619 | #ifdef FEAT_LINEBREAK |
| 4620 | // Restore linebreak, so that when the user edits it looks as |
| 4621 | // before. |
| 4622 | curwin->w_p_lbr = lbr_saved; |
| 4623 | #endif |
| 4624 | op_function(oap); // call 'operatorfunc' |
| 4625 | break; |
| 4626 | |
| 4627 | case OP_INSERT: |
| 4628 | case OP_APPEND: |
| 4629 | VIsual_reselect = FALSE; // don't reselect now |
| 4630 | if (empty_region_error) |
| 4631 | { |
| 4632 | vim_beep(BO_OPER); |
| 4633 | CancelRedo(); |
| 4634 | } |
| 4635 | else |
| 4636 | { |
| 4637 | // This is a new edit command, not a restart. Need to |
| 4638 | // remember it to make 'insertmode' work with mappings for |
| 4639 | // Visual mode. But do this only once. |
| 4640 | restart_edit_save = restart_edit; |
| 4641 | restart_edit = 0; |
| 4642 | #ifdef FEAT_LINEBREAK |
| 4643 | // Restore linebreak, so that when the user edits it looks as |
| 4644 | // before. |
| 4645 | if (curwin->w_p_lbr != lbr_saved) |
| 4646 | { |
| 4647 | curwin->w_p_lbr = lbr_saved; |
| 4648 | get_op_vcol(oap, redo_VIsual_mode, FALSE); |
| 4649 | } |
| 4650 | #endif |
| 4651 | op_insert(oap, cap->count1); |
| 4652 | #ifdef FEAT_LINEBREAK |
| 4653 | // Reset linebreak, so that formatting works correctly. |
| 4654 | curwin->w_p_lbr = FALSE; |
| 4655 | #endif |
| 4656 | |
| 4657 | // TODO: when inserting in several lines, should format all |
| 4658 | // the lines. |
| 4659 | auto_format(FALSE, TRUE); |
| 4660 | |
| 4661 | if (restart_edit == 0) |
| 4662 | restart_edit = restart_edit_save; |
| 4663 | else |
| 4664 | cap->retval |= CA_COMMAND_BUSY; |
| 4665 | } |
| 4666 | break; |
| 4667 | |
| 4668 | case OP_REPLACE: |
| 4669 | VIsual_reselect = FALSE; // don't reselect now |
| 4670 | if (empty_region_error) |
| 4671 | { |
| 4672 | vim_beep(BO_OPER); |
| 4673 | CancelRedo(); |
| 4674 | } |
| 4675 | else |
| 4676 | { |
| 4677 | #ifdef FEAT_LINEBREAK |
| 4678 | // Restore linebreak, so that when the user edits it looks as |
| 4679 | // before. |
| 4680 | if (curwin->w_p_lbr != lbr_saved) |
| 4681 | { |
| 4682 | curwin->w_p_lbr = lbr_saved; |
| 4683 | get_op_vcol(oap, redo_VIsual_mode, FALSE); |
| 4684 | } |
| 4685 | #endif |
| 4686 | op_replace(oap, cap->nchar); |
| 4687 | } |
| 4688 | break; |
| 4689 | |
| 4690 | #ifdef FEAT_FOLDING |
| 4691 | case OP_FOLD: |
| 4692 | VIsual_reselect = FALSE; // don't reselect now |
| 4693 | foldCreate(oap->start.lnum, oap->end.lnum); |
| 4694 | break; |
| 4695 | |
| 4696 | case OP_FOLDOPEN: |
| 4697 | case OP_FOLDOPENREC: |
| 4698 | case OP_FOLDCLOSE: |
| 4699 | case OP_FOLDCLOSEREC: |
| 4700 | VIsual_reselect = FALSE; // don't reselect now |
| 4701 | opFoldRange(oap->start.lnum, oap->end.lnum, |
| 4702 | oap->op_type == OP_FOLDOPEN |
| 4703 | || oap->op_type == OP_FOLDOPENREC, |
| 4704 | oap->op_type == OP_FOLDOPENREC |
| 4705 | || oap->op_type == OP_FOLDCLOSEREC, |
| 4706 | oap->is_VIsual); |
| 4707 | break; |
| 4708 | |
| 4709 | case OP_FOLDDEL: |
| 4710 | case OP_FOLDDELREC: |
| 4711 | VIsual_reselect = FALSE; // don't reselect now |
| 4712 | deleteFold(oap->start.lnum, oap->end.lnum, |
| 4713 | oap->op_type == OP_FOLDDELREC, oap->is_VIsual); |
| 4714 | break; |
| 4715 | #endif |
| 4716 | case OP_NR_ADD: |
| 4717 | case OP_NR_SUB: |
| 4718 | if (empty_region_error) |
| 4719 | { |
| 4720 | vim_beep(BO_OPER); |
| 4721 | CancelRedo(); |
| 4722 | } |
| 4723 | else |
| 4724 | { |
| 4725 | VIsual_active = TRUE; |
| 4726 | #ifdef FEAT_LINEBREAK |
| 4727 | curwin->w_p_lbr = lbr_saved; |
| 4728 | #endif |
| 4729 | op_addsub(oap, cap->count1, redo_VIsual_arg); |
| 4730 | VIsual_active = FALSE; |
| 4731 | } |
| 4732 | check_cursor_col(); |
| 4733 | break; |
| 4734 | default: |
| 4735 | clearopbeep(oap); |
| 4736 | } |
| 4737 | virtual_op = MAYBE; |
| 4738 | if (!gui_yank) |
| 4739 | { |
| 4740 | // if 'sol' not set, go back to old column for some commands |
| 4741 | if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted |
| 4742 | && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT |
| 4743 | || oap->op_type == OP_DELETE)) |
| 4744 | { |
| 4745 | #ifdef FEAT_LINEBREAK |
| 4746 | curwin->w_p_lbr = FALSE; |
| 4747 | #endif |
| 4748 | coladvance(curwin->w_curswant = old_col); |
| 4749 | } |
| 4750 | } |
| 4751 | else |
| 4752 | { |
| 4753 | curwin->w_cursor = old_cursor; |
| 4754 | } |
| 4755 | oap->block_mode = FALSE; |
| 4756 | clearop(oap); |
| 4757 | motion_force = NUL; |
| 4758 | } |
| 4759 | #ifdef FEAT_LINEBREAK |
| 4760 | curwin->w_p_lbr = lbr_saved; |
| 4761 | #endif |
| 4762 | } |