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