Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * ex_cmds.c: some functions for command line commands |
| 12 | */ |
| 13 | |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 14 | #include "vim.h" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | #include "version.h" |
| 16 | |
Bram Moolenaar | 17576a1 | 2016-01-20 20:05:44 +0100 | [diff] [blame] | 17 | #ifdef FEAT_FLOAT |
| 18 | # include <float.h> |
| 19 | #endif |
| 20 | |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 21 | static int linelen(int *has_tab); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 22 | static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 23 | #ifdef FEAT_VIMINFO |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 24 | static char_u *viminfo_filename(char_u *); |
| 25 | static void do_viminfo(FILE *fp_in, FILE *fp_out, int flags); |
| 26 | static int viminfo_encoding(vir_T *virp); |
| 27 | static int read_viminfo_up_to_marks(vir_T *virp, int forceit, int writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 30 | static int check_readonly(int *forceit, buf_T *buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 31 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 32 | static void delbuf_msg(char_u *name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | static int |
| 35 | #ifdef __BORLANDC__ |
| 36 | _RTLENTRYF |
| 37 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 38 | help_compare(const void *s1, const void *s2); |
| 39 | static void prepare_help_buffer(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * ":ascii" and "ga". |
| 43 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 44 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 45 | do_ascii(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | { |
| 47 | int c; |
Bram Moolenaar | 1418a0d | 2009-01-13 15:58:01 +0000 | [diff] [blame] | 48 | int cval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 49 | char buf1[20]; |
| 50 | char buf2[20]; |
| 51 | char_u buf3[7]; |
| 52 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 53 | int cc[MAX_MCO]; |
| 54 | int ci = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 55 | int len; |
| 56 | |
| 57 | if (enc_utf8) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 58 | c = utfc_ptr2char(ml_get_cursor(), cc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | else |
| 60 | #endif |
| 61 | c = gchar_cursor(); |
| 62 | if (c == NUL) |
| 63 | { |
| 64 | MSG("NUL"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | #ifdef FEAT_MBYTE |
| 69 | IObuff[0] = NUL; |
| 70 | if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80) |
| 71 | #endif |
| 72 | { |
| 73 | if (c == NL) /* NUL is stored as NL */ |
| 74 | c = NUL; |
Bram Moolenaar | 1418a0d | 2009-01-13 15:58:01 +0000 | [diff] [blame] | 75 | if (c == CAR && get_fileformat(curbuf) == EOL_MAC) |
| 76 | cval = NL; /* NL is stored as CR */ |
| 77 | else |
| 78 | cval = c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | if (vim_isprintc_strict(c) && (c < ' ' |
| 80 | #ifndef EBCDIC |
| 81 | || c > '~' |
| 82 | #endif |
| 83 | )) |
| 84 | { |
| 85 | transchar_nonprint(buf3, c); |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 86 | vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | buf1[0] = NUL; |
| 90 | #ifndef EBCDIC |
| 91 | if (c >= 0x80) |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 92 | vim_snprintf(buf2, sizeof(buf2), " <M-%s>", |
| 93 | (char *)transchar(c & 0x7f)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 94 | else |
| 95 | #endif |
| 96 | buf2[0] = NUL; |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 97 | vim_snprintf((char *)IObuff, IOSIZE, |
| 98 | _("<%s>%s%s %d, Hex %02x, Octal %03o"), |
Bram Moolenaar | 1418a0d | 2009-01-13 15:58:01 +0000 | [diff] [blame] | 99 | transchar(c), buf1, buf2, cval, cval, cval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 100 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1f788e7 | 2006-09-05 16:30:40 +0000 | [diff] [blame] | 101 | if (enc_utf8) |
| 102 | c = cc[ci++]; |
| 103 | else |
| 104 | c = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 105 | #endif |
| 106 | } |
| 107 | |
| 108 | #ifdef FEAT_MBYTE |
| 109 | /* Repeat for combining characters. */ |
| 110 | while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80))) |
| 111 | { |
| 112 | len = (int)STRLEN(IObuff); |
| 113 | /* This assumes every multi-byte char is printable... */ |
| 114 | if (len > 0) |
| 115 | IObuff[len++] = ' '; |
| 116 | IObuff[len++] = '<'; |
Bram Moolenaar | 1f788e7 | 2006-09-05 16:30:40 +0000 | [diff] [blame] | 117 | if (enc_utf8 && utf_iscomposing(c) |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 118 | # ifdef USE_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 119 | && !gui.in_use |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 120 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | ) |
| 122 | IObuff[len++] = ' '; /* draw composing char on top of a space */ |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 123 | len += (*mb_char2bytes)(c, IObuff + len); |
| 124 | vim_snprintf((char *)IObuff + len, IOSIZE - len, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 125 | c < 0x10000 ? _("> %d, Hex %04x, Octal %o") |
| 126 | : _("> %d, Hex %08x, Octal %o"), c, c, c); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 127 | if (ci == MAX_MCO) |
| 128 | break; |
Bram Moolenaar | 1f788e7 | 2006-09-05 16:30:40 +0000 | [diff] [blame] | 129 | if (enc_utf8) |
| 130 | c = cc[ci++]; |
| 131 | else |
| 132 | c = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | } |
| 134 | #endif |
| 135 | |
| 136 | msg(IObuff); |
| 137 | } |
| 138 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | /* |
| 140 | * ":left", ":center" and ":right": align text. |
| 141 | */ |
| 142 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 143 | ex_align(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | { |
| 145 | pos_T save_curpos; |
| 146 | int len; |
| 147 | int indent = 0; |
| 148 | int new_indent; |
| 149 | int has_tab; |
| 150 | int width; |
| 151 | |
| 152 | #ifdef FEAT_RIGHTLEFT |
| 153 | if (curwin->w_p_rl) |
| 154 | { |
| 155 | /* switch left and right aligning */ |
| 156 | if (eap->cmdidx == CMD_right) |
| 157 | eap->cmdidx = CMD_left; |
| 158 | else if (eap->cmdidx == CMD_left) |
| 159 | eap->cmdidx = CMD_right; |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | width = atoi((char *)eap->arg); |
| 164 | save_curpos = curwin->w_cursor; |
| 165 | if (eap->cmdidx == CMD_left) /* width is used for new indent */ |
| 166 | { |
| 167 | if (width >= 0) |
| 168 | indent = width; |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | /* |
| 173 | * if 'textwidth' set, use it |
| 174 | * else if 'wrapmargin' set, use it |
| 175 | * if invalid value, use 80 |
| 176 | */ |
| 177 | if (width <= 0) |
| 178 | width = curbuf->b_p_tw; |
| 179 | if (width == 0 && curbuf->b_p_wm > 0) |
| 180 | width = W_WIDTH(curwin) - curbuf->b_p_wm; |
| 181 | if (width <= 0) |
| 182 | width = 80; |
| 183 | } |
| 184 | |
| 185 | if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) |
| 186 | return; |
| 187 | |
| 188 | for (curwin->w_cursor.lnum = eap->line1; |
| 189 | curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum) |
| 190 | { |
| 191 | if (eap->cmdidx == CMD_left) /* left align */ |
| 192 | new_indent = indent; |
| 193 | else |
| 194 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 195 | has_tab = FALSE; /* avoid uninit warnings */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 196 | len = linelen(eap->cmdidx == CMD_right ? &has_tab |
| 197 | : NULL) - get_indent(); |
| 198 | |
| 199 | if (len <= 0) /* skip blank lines */ |
| 200 | continue; |
| 201 | |
| 202 | if (eap->cmdidx == CMD_center) |
| 203 | new_indent = (width - len) / 2; |
| 204 | else |
| 205 | { |
| 206 | new_indent = width - len; /* right align */ |
| 207 | |
| 208 | /* |
| 209 | * Make sure that embedded TABs don't make the text go too far |
| 210 | * to the right. |
| 211 | */ |
| 212 | if (has_tab) |
| 213 | while (new_indent > 0) |
| 214 | { |
| 215 | (void)set_indent(new_indent, 0); |
| 216 | if (linelen(NULL) <= width) |
| 217 | { |
| 218 | /* |
| 219 | * Now try to move the line as much as possible to |
| 220 | * the right. Stop when it moves too far. |
| 221 | */ |
| 222 | do |
| 223 | (void)set_indent(++new_indent, 0); |
| 224 | while (linelen(NULL) <= width); |
| 225 | --new_indent; |
| 226 | break; |
| 227 | } |
| 228 | --new_indent; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | if (new_indent < 0) |
| 233 | new_indent = 0; |
| 234 | (void)set_indent(new_indent, 0); /* set indent */ |
| 235 | } |
| 236 | changed_lines(eap->line1, 0, eap->line2 + 1, 0L); |
| 237 | curwin->w_cursor = save_curpos; |
| 238 | beginline(BL_WHITE | BL_FIX); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Get the length of the current line, excluding trailing white space. |
| 243 | */ |
| 244 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 245 | linelen(int *has_tab) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 246 | { |
| 247 | char_u *line; |
| 248 | char_u *first; |
| 249 | char_u *last; |
| 250 | int save; |
| 251 | int len; |
| 252 | |
| 253 | /* find the first non-blank character */ |
| 254 | line = ml_get_curline(); |
| 255 | first = skipwhite(line); |
| 256 | |
| 257 | /* find the character after the last non-blank character */ |
| 258 | for (last = first + STRLEN(first); |
| 259 | last > first && vim_iswhite(last[-1]); --last) |
| 260 | ; |
| 261 | save = *last; |
| 262 | *last = NUL; |
| 263 | len = linetabsize(line); /* get line length */ |
| 264 | if (has_tab != NULL) /* check for embedded TAB */ |
| 265 | *has_tab = (vim_strrchr(first, TAB) != NULL); |
| 266 | *last = save; |
| 267 | |
| 268 | return len; |
| 269 | } |
| 270 | |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 271 | /* Buffer for two lines used during sorting. They are allocated to |
| 272 | * contain the longest line being sorted. */ |
| 273 | static char_u *sortbuf1; |
| 274 | static char_u *sortbuf2; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 275 | |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 276 | static int sort_ic; /* ignore case */ |
| 277 | static int sort_nr; /* sort on number */ |
| 278 | static int sort_rx; /* sort on regex instead of skipping it */ |
| 279 | #ifdef FEAT_FLOAT |
| 280 | static int sort_flt; /* sort on floating number */ |
| 281 | #endif |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 282 | |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 283 | static int sort_abort; /* flag to indicate if sorting has been interrupted */ |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 284 | |
| 285 | /* Struct to store info to be sorted. */ |
| 286 | typedef struct |
| 287 | { |
| 288 | linenr_T lnum; /* line number */ |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 289 | union { |
| 290 | struct |
| 291 | { |
| 292 | long start_col_nr; /* starting column number */ |
| 293 | long end_col_nr; /* ending column number */ |
| 294 | } line; |
| 295 | long value; /* value if sorting by integer */ |
| 296 | #ifdef FEAT_FLOAT |
| 297 | float_T value_flt; /* value if sorting by float */ |
| 298 | #endif |
| 299 | } st_u; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 300 | } sorti_T; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 301 | |
| 302 | static int |
| 303 | #ifdef __BORLANDC__ |
| 304 | _RTLENTRYF |
| 305 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 306 | sort_compare(const void *s1, const void *s2); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 307 | |
| 308 | static int |
| 309 | #ifdef __BORLANDC__ |
| 310 | _RTLENTRYF |
| 311 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 312 | sort_compare(const void *s1, const void *s2) |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 313 | { |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 314 | sorti_T l1 = *(sorti_T *)s1; |
| 315 | sorti_T l2 = *(sorti_T *)s2; |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 316 | int result = 0; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 317 | |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 318 | /* If the user interrupts, there's no way to stop qsort() immediately, but |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 319 | * if we return 0 every time, qsort will assume it's done sorting and |
| 320 | * exit. */ |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 321 | if (sort_abort) |
| 322 | return 0; |
| 323 | fast_breakcheck(); |
| 324 | if (got_int) |
| 325 | sort_abort = TRUE; |
| 326 | |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 327 | /* When sorting numbers "start_col_nr" is the number, not the column |
| 328 | * number. */ |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 329 | if (sort_nr) |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 330 | result = l1.st_u.value == l2.st_u.value ? 0 |
| 331 | : l1.st_u.value > l2.st_u.value ? 1 : -1; |
| 332 | #ifdef FEAT_FLOAT |
| 333 | else if (sort_flt) |
| 334 | result = l1.st_u.value_flt == l2.st_u.value_flt ? 0 |
| 335 | : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1; |
| 336 | #endif |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 337 | else |
| 338 | { |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 339 | /* We need to copy one line into "sortbuf1", because there is no |
| 340 | * guarantee that the first pointer becomes invalid when obtaining the |
| 341 | * second one. */ |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 342 | STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr, |
| 343 | l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1); |
| 344 | sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0; |
| 345 | STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr, |
| 346 | l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1); |
| 347 | sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 348 | |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 349 | result = sort_ic ? STRICMP(sortbuf1, sortbuf2) |
| 350 | : STRCMP(sortbuf1, sortbuf2); |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 351 | } |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 352 | |
| 353 | /* If two lines have the same value, preserve the original line order. */ |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 354 | if (result == 0) |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 355 | return (int)(l1.lnum - l2.lnum); |
| 356 | return result; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
| 360 | * ":sort". |
| 361 | */ |
| 362 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 363 | ex_sort(exarg_T *eap) |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 364 | { |
| 365 | regmatch_T regmatch; |
| 366 | int len; |
| 367 | linenr_T lnum; |
| 368 | long maxlen = 0; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 369 | sorti_T *nrs; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 370 | size_t count = (size_t)(eap->line2 - eap->line1 + 1); |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 371 | size_t i; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 372 | char_u *p; |
| 373 | char_u *s; |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 374 | char_u *s2; |
| 375 | char_u c; /* temporary character storage */ |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 376 | int unique = FALSE; |
| 377 | long deleted; |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 378 | colnr_T start_col; |
| 379 | colnr_T end_col; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 380 | int sort_what = 0; |
| 381 | int format_found = 0; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 382 | |
Bram Moolenaar | 48c9916 | 2008-02-18 18:42:52 +0000 | [diff] [blame] | 383 | /* Sorting one line is really quick! */ |
| 384 | if (count <= 1) |
| 385 | return; |
| 386 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 387 | if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) |
| 388 | return; |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 389 | sortbuf1 = NULL; |
| 390 | sortbuf2 = NULL; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 391 | regmatch.regprog = NULL; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 392 | nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 393 | if (nrs == NULL) |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 394 | goto sortend; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 395 | |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 396 | sort_abort = sort_ic = sort_rx = sort_nr = 0; |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 397 | #ifdef FEAT_FLOAT |
| 398 | sort_flt = 0; |
| 399 | #endif |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 400 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 401 | for (p = eap->arg; *p != NUL; ++p) |
| 402 | { |
| 403 | if (vim_iswhite(*p)) |
| 404 | ; |
| 405 | else if (*p == 'i') |
| 406 | sort_ic = TRUE; |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 407 | else if (*p == 'r') |
| 408 | sort_rx = TRUE; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 409 | else if (*p == 'n') |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 410 | { |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 411 | sort_nr = 1; |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 412 | ++format_found; |
| 413 | } |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 414 | #ifdef FEAT_FLOAT |
| 415 | else if (*p == 'f') |
| 416 | { |
| 417 | sort_flt = 1; |
| 418 | ++format_found; |
| 419 | } |
| 420 | #endif |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 421 | else if (*p == 'b') |
| 422 | { |
| 423 | sort_what = STR2NR_BIN + STR2NR_FORCE; |
| 424 | ++format_found; |
| 425 | } |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 426 | else if (*p == 'o') |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 427 | { |
| 428 | sort_what = STR2NR_OCT + STR2NR_FORCE; |
| 429 | ++format_found; |
| 430 | } |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 431 | else if (*p == 'x') |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 432 | { |
| 433 | sort_what = STR2NR_HEX + STR2NR_FORCE; |
| 434 | ++format_found; |
| 435 | } |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 436 | else if (*p == 'u') |
| 437 | unique = TRUE; |
Bram Moolenaar | 0e6830e | 2005-05-27 20:23:44 +0000 | [diff] [blame] | 438 | else if (*p == '"') /* comment start */ |
| 439 | break; |
| 440 | else if (check_nextcmd(p) != NULL) |
| 441 | { |
| 442 | eap->nextcmd = check_nextcmd(p); |
| 443 | break; |
| 444 | } |
| 445 | else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL) |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 446 | { |
| 447 | s = skip_regexp(p + 1, *p, TRUE, NULL); |
| 448 | if (*s != *p) |
| 449 | { |
| 450 | EMSG(_(e_invalpat)); |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 451 | goto sortend; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 452 | } |
| 453 | *s = NUL; |
Bram Moolenaar | 1256e72 | 2007-07-10 15:26:20 +0000 | [diff] [blame] | 454 | /* Use last search pattern if sort pattern is empty. */ |
Bram Moolenaar | 210f370 | 2013-03-07 16:41:30 +0100 | [diff] [blame] | 455 | if (s == p + 1) |
| 456 | { |
| 457 | if (last_search_pat() == NULL) |
| 458 | { |
| 459 | EMSG(_(e_noprevre)); |
| 460 | goto sortend; |
| 461 | } |
Bram Moolenaar | 1256e72 | 2007-07-10 15:26:20 +0000 | [diff] [blame] | 462 | regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC); |
Bram Moolenaar | 210f370 | 2013-03-07 16:41:30 +0100 | [diff] [blame] | 463 | } |
Bram Moolenaar | 1256e72 | 2007-07-10 15:26:20 +0000 | [diff] [blame] | 464 | else |
| 465 | regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 466 | if (regmatch.regprog == NULL) |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 467 | goto sortend; |
Bram Moolenaar | 0e6830e | 2005-05-27 20:23:44 +0000 | [diff] [blame] | 468 | p = s; /* continue after the regexp */ |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 469 | regmatch.rm_ic = p_ic; |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | EMSG2(_(e_invarg2), p); |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 474 | goto sortend; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 478 | /* Can only have one of 'n', 'b', 'o' and 'x'. */ |
| 479 | if (format_found > 1) |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 480 | { |
| 481 | EMSG(_(e_invarg)); |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 482 | goto sortend; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 485 | /* From here on "sort_nr" is used as a flag for any integer number |
| 486 | * sorting. */ |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 487 | sort_nr += sort_what; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 488 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 489 | /* |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 490 | * Make an array with all line numbers. This avoids having to copy all |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 491 | * the lines into allocated memory. |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 492 | * When sorting on strings "start_col_nr" is the offset in the line, for |
| 493 | * numbers sorting it's the number to sort on. This means the pattern |
| 494 | * matching and number conversion only has to be done once per line. |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 495 | * Also get the longest line length for allocating "sortbuf". |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 496 | */ |
| 497 | for (lnum = eap->line1; lnum <= eap->line2; ++lnum) |
| 498 | { |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 499 | s = ml_get(lnum); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 500 | len = (int)STRLEN(s); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 501 | if (maxlen < len) |
| 502 | maxlen = len; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 503 | |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 504 | start_col = 0; |
| 505 | end_col = len; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 506 | if (regmatch.regprog != NULL && vim_regexec(®match, s, 0)) |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 507 | { |
| 508 | if (sort_rx) |
| 509 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 510 | start_col = (colnr_T)(regmatch.startp[0] - s); |
| 511 | end_col = (colnr_T)(regmatch.endp[0] - s); |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 512 | } |
| 513 | else |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 514 | start_col = (colnr_T)(regmatch.endp[0] - s); |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 515 | } |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 516 | else |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 517 | if (regmatch.regprog != NULL) |
| 518 | end_col = 0; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 519 | |
Bram Moolenaar | 937204a | 2016-01-30 23:37:38 +0100 | [diff] [blame] | 520 | if (sort_nr |
| 521 | #ifdef FEAT_FLOAT |
| 522 | || sort_flt |
| 523 | #endif |
| 524 | ) |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 525 | { |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 526 | /* Make sure vim_str2nr doesn't read any digits past the end |
| 527 | * of the match, by temporarily terminating the string there */ |
| 528 | s2 = s + end_col; |
| 529 | c = *s2; |
Bram Moolenaar | f75d498 | 2010-10-15 20:20:05 +0200 | [diff] [blame] | 530 | *s2 = NUL; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 531 | /* Sorting on number: Store the number itself. */ |
Bram Moolenaar | 1387a60 | 2008-07-24 19:31:11 +0000 | [diff] [blame] | 532 | p = s + start_col; |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 533 | if (sort_nr) |
| 534 | { |
| 535 | if (sort_what & STR2NR_HEX) |
| 536 | s = skiptohex(p); |
| 537 | else if (sort_what & STR2NR_BIN) |
| 538 | s = skiptobin(p); |
| 539 | else |
| 540 | s = skiptodigit(p); |
| 541 | if (s > p && s[-1] == '-') |
| 542 | --s; /* include preceding negative sign */ |
| 543 | if (*s == NUL) |
| 544 | /* empty line should sort before any number */ |
| 545 | nrs[lnum - eap->line1].st_u.value = -MAXLNUM; |
| 546 | else |
| 547 | vim_str2nr(s, NULL, NULL, sort_what, |
| 548 | &nrs[lnum - eap->line1].st_u.value, NULL, 0); |
| 549 | } |
| 550 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 551 | else |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 552 | { |
| 553 | s = skipwhite(p); |
| 554 | if (*s == '+') |
| 555 | s = skipwhite(s + 1); |
| 556 | |
| 557 | if (*s == NUL) |
| 558 | /* empty line should sort before any number */ |
| 559 | nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX; |
| 560 | else |
| 561 | nrs[lnum - eap->line1].st_u.value_flt = |
| 562 | strtod((char *)s, NULL); |
| 563 | } |
| 564 | #endif |
Bram Moolenaar | f75d498 | 2010-10-15 20:20:05 +0200 | [diff] [blame] | 565 | *s2 = c; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 566 | } |
| 567 | else |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 568 | { |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 569 | /* Store the column to sort at. */ |
Bram Moolenaar | f7edf40 | 2016-01-19 23:36:15 +0100 | [diff] [blame] | 570 | nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col; |
| 571 | nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col; |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 572 | } |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 573 | |
| 574 | nrs[lnum - eap->line1].lnum = lnum; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 575 | |
| 576 | if (regmatch.regprog != NULL) |
| 577 | fast_breakcheck(); |
| 578 | if (got_int) |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 579 | goto sortend; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 582 | /* Allocate a buffer that can hold the longest line. */ |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 583 | sortbuf1 = alloc((unsigned)maxlen + 1); |
| 584 | if (sortbuf1 == NULL) |
| 585 | goto sortend; |
| 586 | sortbuf2 = alloc((unsigned)maxlen + 1); |
| 587 | if (sortbuf2 == NULL) |
| 588 | goto sortend; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 589 | |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 590 | /* Sort the array of line numbers. Note: can't be interrupted! */ |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 591 | qsort((void *)nrs, count, sizeof(sorti_T), sort_compare); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 592 | |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 593 | if (sort_abort) |
| 594 | goto sortend; |
| 595 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 596 | /* Insert the lines in the sorted order below the last one. */ |
| 597 | lnum = eap->line2; |
| 598 | for (i = 0; i < count; ++i) |
| 599 | { |
| 600 | s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum); |
| 601 | if (!unique || i == 0 |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 602 | || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0) |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 603 | { |
Bram Moolenaar | 75e3ad0 | 2015-12-17 15:07:32 +0100 | [diff] [blame] | 604 | /* Copy the line into a buffer, it may become invalid in |
| 605 | * ml_append(). And it's needed for "unique". */ |
| 606 | STRCPY(sortbuf1, s); |
| 607 | if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL) |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 608 | break; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 609 | } |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 610 | fast_breakcheck(); |
| 611 | if (got_int) |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 612 | goto sortend; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* delete the original lines if appending worked */ |
| 616 | if (i == count) |
| 617 | for (i = 0; i < count; ++i) |
| 618 | ml_delete(eap->line1, FALSE); |
| 619 | else |
| 620 | count = 0; |
| 621 | |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 622 | /* Adjust marks for deleted (or added) lines and prepare for displaying. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 623 | deleted = (long)(count - (lnum - eap->line2)); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 624 | if (deleted > 0) |
| 625 | mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted); |
| 626 | else if (deleted < 0) |
| 627 | mark_adjust(eap->line2, MAXLNUM, -deleted, 0L); |
| 628 | changed_lines(eap->line1, 0, eap->line2 + 1, -deleted); |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 629 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 630 | curwin->w_cursor.lnum = eap->line1; |
| 631 | beginline(BL_WHITE | BL_FIX); |
| 632 | |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 633 | sortend: |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 634 | vim_free(nrs); |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 635 | vim_free(sortbuf1); |
| 636 | vim_free(sortbuf2); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 637 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 638 | if (got_int) |
| 639 | EMSG(_(e_interr)); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 642 | /* |
| 643 | * ":retab". |
| 644 | */ |
| 645 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 646 | ex_retab(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 647 | { |
| 648 | linenr_T lnum; |
| 649 | int got_tab = FALSE; |
| 650 | long num_spaces = 0; |
| 651 | long num_tabs; |
| 652 | long len; |
| 653 | long col; |
| 654 | long vcol; |
| 655 | long start_col = 0; /* For start of white-space string */ |
| 656 | long start_vcol = 0; /* For start of white-space string */ |
| 657 | int temp; |
| 658 | long old_len; |
| 659 | char_u *ptr; |
| 660 | char_u *new_line = (char_u *)1; /* init to non-NULL */ |
| 661 | int did_undo; /* called u_save for current line */ |
| 662 | int new_ts; |
| 663 | int save_list; |
| 664 | linenr_T first_line = 0; /* first changed line */ |
| 665 | linenr_T last_line = 0; /* last changed line */ |
| 666 | |
| 667 | save_list = curwin->w_p_list; |
| 668 | curwin->w_p_list = 0; /* don't want list mode here */ |
| 669 | |
| 670 | new_ts = getdigits(&(eap->arg)); |
| 671 | if (new_ts < 0) |
| 672 | { |
| 673 | EMSG(_(e_positive)); |
| 674 | return; |
| 675 | } |
| 676 | if (new_ts == 0) |
| 677 | new_ts = curbuf->b_p_ts; |
| 678 | for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum) |
| 679 | { |
| 680 | ptr = ml_get(lnum); |
| 681 | col = 0; |
| 682 | vcol = 0; |
| 683 | did_undo = FALSE; |
| 684 | for (;;) |
| 685 | { |
| 686 | if (vim_iswhite(ptr[col])) |
| 687 | { |
| 688 | if (!got_tab && num_spaces == 0) |
| 689 | { |
| 690 | /* First consecutive white-space */ |
| 691 | start_vcol = vcol; |
| 692 | start_col = col; |
| 693 | } |
| 694 | if (ptr[col] == ' ') |
| 695 | num_spaces++; |
| 696 | else |
| 697 | got_tab = TRUE; |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | if (got_tab || (eap->forceit && num_spaces > 1)) |
| 702 | { |
| 703 | /* Retabulate this string of white-space */ |
| 704 | |
| 705 | /* len is virtual length of white string */ |
| 706 | len = num_spaces = vcol - start_vcol; |
| 707 | num_tabs = 0; |
| 708 | if (!curbuf->b_p_et) |
| 709 | { |
| 710 | temp = new_ts - (start_vcol % new_ts); |
| 711 | if (num_spaces >= temp) |
| 712 | { |
| 713 | num_spaces -= temp; |
| 714 | num_tabs++; |
| 715 | } |
| 716 | num_tabs += num_spaces / new_ts; |
| 717 | num_spaces -= (num_spaces / new_ts) * new_ts; |
| 718 | } |
| 719 | if (curbuf->b_p_et || got_tab || |
| 720 | (num_spaces + num_tabs < len)) |
| 721 | { |
| 722 | if (did_undo == FALSE) |
| 723 | { |
| 724 | did_undo = TRUE; |
| 725 | if (u_save((linenr_T)(lnum - 1), |
| 726 | (linenr_T)(lnum + 1)) == FAIL) |
| 727 | { |
| 728 | new_line = NULL; /* flag out-of-memory */ |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* len is actual number of white characters used */ |
| 734 | len = num_spaces + num_tabs; |
| 735 | old_len = (long)STRLEN(ptr); |
| 736 | new_line = lalloc(old_len - col + start_col + len + 1, |
| 737 | TRUE); |
| 738 | if (new_line == NULL) |
| 739 | break; |
| 740 | if (start_col > 0) |
| 741 | mch_memmove(new_line, ptr, (size_t)start_col); |
| 742 | mch_memmove(new_line + start_col + len, |
| 743 | ptr + col, (size_t)(old_len - col + 1)); |
| 744 | ptr = new_line + start_col; |
| 745 | for (col = 0; col < len; col++) |
| 746 | ptr[col] = (col < num_tabs) ? '\t' : ' '; |
| 747 | ml_replace(lnum, new_line, FALSE); |
| 748 | if (first_line == 0) |
| 749 | first_line = lnum; |
| 750 | last_line = lnum; |
| 751 | ptr = new_line; |
| 752 | col = start_col + len; |
| 753 | } |
| 754 | } |
| 755 | got_tab = FALSE; |
| 756 | num_spaces = 0; |
| 757 | } |
| 758 | if (ptr[col] == NUL) |
| 759 | break; |
| 760 | vcol += chartabsize(ptr + col, (colnr_T)vcol); |
| 761 | #ifdef FEAT_MBYTE |
| 762 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 763 | col += (*mb_ptr2len)(ptr + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 764 | else |
| 765 | #endif |
| 766 | ++col; |
| 767 | } |
| 768 | if (new_line == NULL) /* out of memory */ |
| 769 | break; |
| 770 | line_breakcheck(); |
| 771 | } |
| 772 | if (got_int) |
| 773 | EMSG(_(e_interr)); |
| 774 | |
| 775 | if (curbuf->b_p_ts != new_ts) |
| 776 | redraw_curbuf_later(NOT_VALID); |
| 777 | if (first_line != 0) |
| 778 | changed_lines(first_line, 0, last_line + 1, 0L); |
| 779 | |
| 780 | curwin->w_p_list = save_list; /* restore 'list' */ |
| 781 | |
| 782 | curbuf->b_p_ts = new_ts; |
| 783 | coladvance(curwin->w_curswant); |
| 784 | |
| 785 | u_clearline(); |
| 786 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 787 | |
| 788 | /* |
| 789 | * :move command - move lines line1-line2 to line dest |
| 790 | * |
| 791 | * return FAIL for failure, OK otherwise |
| 792 | */ |
| 793 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 794 | do_move(linenr_T line1, linenr_T line2, linenr_T dest) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 795 | { |
| 796 | char_u *str; |
| 797 | linenr_T l; |
| 798 | linenr_T extra; /* Num lines added before line1 */ |
| 799 | linenr_T num_lines; /* Num lines moved */ |
| 800 | linenr_T last_line; /* Last line in file after adding new text */ |
Bram Moolenaar | d5f6933 | 2015-04-15 12:43:50 +0200 | [diff] [blame] | 801 | #ifdef FEAT_FOLDING |
| 802 | int isFolded; |
| 803 | |
| 804 | /* Moving lines seems to corrupt the folds, delete folding info now |
| 805 | * and recreate it when finished. Don't do this for manual folding, it |
| 806 | * would delete all folds. */ |
| 807 | isFolded = hasAnyFolding(curwin) && !foldmethodIsManual(curwin); |
| 808 | if (isFolded) |
| 809 | deleteFoldRecurse(&curwin->w_folds); |
| 810 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 811 | |
| 812 | if (dest >= line1 && dest < line2) |
| 813 | { |
| 814 | EMSG(_("E134: Move lines into themselves")); |
| 815 | return FAIL; |
| 816 | } |
| 817 | |
| 818 | num_lines = line2 - line1 + 1; |
| 819 | |
| 820 | /* |
| 821 | * First we copy the old text to its new location -- webb |
| 822 | * Also copy the flag that ":global" command uses. |
| 823 | */ |
| 824 | if (u_save(dest, dest + 1) == FAIL) |
| 825 | return FAIL; |
| 826 | for (extra = 0, l = line1; l <= line2; l++) |
| 827 | { |
| 828 | str = vim_strsave(ml_get(l + extra)); |
| 829 | if (str != NULL) |
| 830 | { |
| 831 | ml_append(dest + l - line1, str, (colnr_T)0, FALSE); |
| 832 | vim_free(str); |
| 833 | if (dest < line1) |
| 834 | extra++; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * Now we must be careful adjusting our marks so that we don't overlap our |
| 840 | * mark_adjust() calls. |
| 841 | * |
| 842 | * We adjust the marks within the old text so that they refer to the |
| 843 | * last lines of the file (temporarily), because we know no other marks |
| 844 | * will be set there since these line numbers did not exist until we added |
| 845 | * our new lines. |
| 846 | * |
| 847 | * Then we adjust the marks on lines between the old and new text positions |
| 848 | * (either forwards or backwards). |
| 849 | * |
| 850 | * And Finally we adjust the marks we put at the end of the file back to |
| 851 | * their final destination at the new text position -- webb |
| 852 | */ |
| 853 | last_line = curbuf->b_ml.ml_line_count; |
| 854 | mark_adjust(line1, line2, last_line - line2, 0L); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 855 | changed_lines(last_line - num_lines + 1, 0, last_line + 1, num_lines); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 856 | if (dest >= line2) |
| 857 | { |
| 858 | mark_adjust(line2 + 1, dest, -num_lines, 0L); |
| 859 | curbuf->b_op_start.lnum = dest - num_lines + 1; |
| 860 | curbuf->b_op_end.lnum = dest; |
| 861 | } |
| 862 | else |
| 863 | { |
| 864 | mark_adjust(dest + 1, line1 - 1, num_lines, 0L); |
| 865 | curbuf->b_op_start.lnum = dest + 1; |
| 866 | curbuf->b_op_end.lnum = dest + num_lines; |
| 867 | } |
| 868 | curbuf->b_op_start.col = curbuf->b_op_end.col = 0; |
| 869 | mark_adjust(last_line - num_lines + 1, last_line, |
| 870 | -(last_line - dest - extra), 0L); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 871 | changed_lines(last_line - num_lines + 1, 0, last_line + 1, -extra); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 872 | |
| 873 | /* |
| 874 | * Now we delete the original text -- webb |
| 875 | */ |
| 876 | if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL) |
| 877 | return FAIL; |
| 878 | |
| 879 | for (l = line1; l <= line2; l++) |
| 880 | ml_delete(line1 + extra, TRUE); |
| 881 | |
| 882 | if (!global_busy && num_lines > p_report) |
| 883 | { |
| 884 | if (num_lines == 1) |
| 885 | MSG(_("1 line moved")); |
| 886 | else |
| 887 | smsg((char_u *)_("%ld lines moved"), num_lines); |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Leave the cursor on the last of the moved lines. |
| 892 | */ |
| 893 | if (dest >= line1) |
| 894 | curwin->w_cursor.lnum = dest; |
| 895 | else |
| 896 | curwin->w_cursor.lnum = dest + (line2 - line1) + 1; |
| 897 | |
| 898 | if (line1 < dest) |
Bram Moolenaar | 0612ec8 | 2011-11-30 17:01:58 +0100 | [diff] [blame] | 899 | { |
| 900 | dest += num_lines + 1; |
| 901 | last_line = curbuf->b_ml.ml_line_count; |
| 902 | if (dest > last_line + 1) |
| 903 | dest = last_line + 1; |
| 904 | changed_lines(line1, 0, dest, 0L); |
| 905 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 906 | else |
| 907 | changed_lines(dest + 1, 0, line1 + num_lines, 0L); |
| 908 | |
Bram Moolenaar | d5f6933 | 2015-04-15 12:43:50 +0200 | [diff] [blame] | 909 | #ifdef FEAT_FOLDING |
| 910 | /* recreate folds */ |
| 911 | if (isFolded) |
| 912 | foldUpdateAll(curwin); |
| 913 | #endif |
| 914 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 915 | return OK; |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * ":copy" |
| 920 | */ |
| 921 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 922 | ex_copy(linenr_T line1, linenr_T line2, linenr_T n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 923 | { |
| 924 | linenr_T count; |
| 925 | char_u *p; |
| 926 | |
| 927 | count = line2 - line1 + 1; |
| 928 | curbuf->b_op_start.lnum = n + 1; |
| 929 | curbuf->b_op_end.lnum = n + count; |
| 930 | curbuf->b_op_start.col = curbuf->b_op_end.col = 0; |
| 931 | |
| 932 | /* |
| 933 | * there are three situations: |
| 934 | * 1. destination is above line1 |
| 935 | * 2. destination is between line1 and line2 |
| 936 | * 3. destination is below line2 |
| 937 | * |
| 938 | * n = destination (when starting) |
| 939 | * curwin->w_cursor.lnum = destination (while copying) |
| 940 | * line1 = start of source (while copying) |
| 941 | * line2 = end of source (while copying) |
| 942 | */ |
| 943 | if (u_save(n, n + 1) == FAIL) |
| 944 | return; |
| 945 | |
| 946 | curwin->w_cursor.lnum = n; |
| 947 | while (line1 <= line2) |
| 948 | { |
| 949 | /* need to use vim_strsave() because the line will be unlocked within |
| 950 | * ml_append() */ |
| 951 | p = vim_strsave(ml_get(line1)); |
| 952 | if (p != NULL) |
| 953 | { |
| 954 | ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE); |
| 955 | vim_free(p); |
| 956 | } |
| 957 | /* situation 2: skip already copied lines */ |
| 958 | if (line1 == n) |
| 959 | line1 = curwin->w_cursor.lnum; |
| 960 | ++line1; |
| 961 | if (curwin->w_cursor.lnum < line1) |
| 962 | ++line1; |
| 963 | if (curwin->w_cursor.lnum < line2) |
| 964 | ++line2; |
| 965 | ++curwin->w_cursor.lnum; |
| 966 | } |
| 967 | |
| 968 | appended_lines_mark(n, count); |
| 969 | |
| 970 | msgmore((long)count); |
| 971 | } |
| 972 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 973 | static char_u *prevcmd = NULL; /* the previous command */ |
| 974 | |
| 975 | #if defined(EXITFREE) || defined(PROTO) |
| 976 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 977 | free_prev_shellcmd(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 978 | { |
| 979 | vim_free(prevcmd); |
| 980 | } |
| 981 | #endif |
| 982 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 983 | /* |
| 984 | * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" |
| 985 | * Bangs in the argument are replaced with the previously entered command. |
| 986 | * Remember the argument. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 987 | */ |
| 988 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 989 | do_bang( |
| 990 | int addr_count, |
| 991 | exarg_T *eap, |
| 992 | int forceit, |
| 993 | int do_in, |
| 994 | int do_out) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 995 | { |
| 996 | char_u *arg = eap->arg; /* command */ |
| 997 | linenr_T line1 = eap->line1; /* start of range */ |
| 998 | linenr_T line2 = eap->line2; /* end of range */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 999 | char_u *newcmd = NULL; /* the new command */ |
| 1000 | int free_newcmd = FALSE; /* need to free() newcmd */ |
| 1001 | int ins_prevcmd; |
| 1002 | char_u *t; |
| 1003 | char_u *p; |
| 1004 | char_u *trailarg; |
| 1005 | int len; |
| 1006 | int scroll_save = msg_scroll; |
| 1007 | |
| 1008 | /* |
| 1009 | * Disallow shell commands for "rvim". |
| 1010 | * Disallow shell commands from .exrc and .vimrc in current directory for |
| 1011 | * security reasons. |
| 1012 | */ |
| 1013 | if (check_restricted() || check_secure()) |
| 1014 | return; |
| 1015 | |
| 1016 | if (addr_count == 0) /* :! */ |
| 1017 | { |
| 1018 | msg_scroll = FALSE; /* don't scroll here */ |
| 1019 | autowrite_all(); |
| 1020 | msg_scroll = scroll_save; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * Try to find an embedded bang, like in :!<cmd> ! [args] |
| 1025 | * (:!! is indicated by the 'forceit' variable) |
| 1026 | */ |
| 1027 | ins_prevcmd = forceit; |
| 1028 | trailarg = arg; |
| 1029 | do |
| 1030 | { |
| 1031 | len = (int)STRLEN(trailarg) + 1; |
| 1032 | if (newcmd != NULL) |
| 1033 | len += (int)STRLEN(newcmd); |
| 1034 | if (ins_prevcmd) |
| 1035 | { |
| 1036 | if (prevcmd == NULL) |
| 1037 | { |
| 1038 | EMSG(_(e_noprev)); |
| 1039 | vim_free(newcmd); |
| 1040 | return; |
| 1041 | } |
| 1042 | len += (int)STRLEN(prevcmd); |
| 1043 | } |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 1044 | if ((t = alloc((unsigned)len)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1045 | { |
| 1046 | vim_free(newcmd); |
| 1047 | return; |
| 1048 | } |
| 1049 | *t = NUL; |
| 1050 | if (newcmd != NULL) |
| 1051 | STRCAT(t, newcmd); |
| 1052 | if (ins_prevcmd) |
| 1053 | STRCAT(t, prevcmd); |
| 1054 | p = t + STRLEN(t); |
| 1055 | STRCAT(t, trailarg); |
| 1056 | vim_free(newcmd); |
| 1057 | newcmd = t; |
| 1058 | |
| 1059 | /* |
| 1060 | * Scan the rest of the argument for '!', which is replaced by the |
| 1061 | * previous command. "\!" is replaced by "!" (this is vi compatible). |
| 1062 | */ |
| 1063 | trailarg = NULL; |
| 1064 | while (*p) |
| 1065 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 1066 | if (*p == '!') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1067 | { |
| 1068 | if (p > newcmd && p[-1] == '\\') |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1069 | STRMOVE(p - 1, p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1070 | else |
| 1071 | { |
| 1072 | trailarg = p; |
| 1073 | *trailarg++ = NUL; |
| 1074 | ins_prevcmd = TRUE; |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | ++p; |
| 1079 | } |
| 1080 | } while (trailarg != NULL); |
| 1081 | |
| 1082 | vim_free(prevcmd); |
| 1083 | prevcmd = newcmd; |
| 1084 | |
| 1085 | if (bangredo) /* put cmd in redo buffer for ! command */ |
| 1086 | { |
Bram Moolenaar | 529d2d6 | 2014-03-19 17:41:23 +0100 | [diff] [blame] | 1087 | /* If % or # appears in the command, it must have been escaped. |
| 1088 | * Reescape them, so that redoing them does not substitute them by the |
| 1089 | * buffername. */ |
| 1090 | char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#"); |
| 1091 | |
| 1092 | if (cmd != NULL) |
| 1093 | { |
| 1094 | AppendToRedobuffLit(cmd, -1); |
| 1095 | vim_free(cmd); |
| 1096 | } |
| 1097 | else |
| 1098 | AppendToRedobuffLit(prevcmd, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1099 | AppendToRedobuff((char_u *)"\n"); |
| 1100 | bangredo = FALSE; |
| 1101 | } |
| 1102 | /* |
| 1103 | * Add quotes around the command, for shells that need them. |
| 1104 | */ |
| 1105 | if (*p_shq != NUL) |
| 1106 | { |
| 1107 | newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1)); |
| 1108 | if (newcmd == NULL) |
| 1109 | return; |
| 1110 | STRCPY(newcmd, p_shq); |
| 1111 | STRCAT(newcmd, prevcmd); |
| 1112 | STRCAT(newcmd, p_shq); |
| 1113 | free_newcmd = TRUE; |
| 1114 | } |
| 1115 | if (addr_count == 0) /* :! */ |
| 1116 | { |
| 1117 | /* echo the command */ |
| 1118 | msg_start(); |
| 1119 | msg_putchar(':'); |
| 1120 | msg_putchar('!'); |
| 1121 | msg_outtrans(newcmd); |
| 1122 | msg_clr_eos(); |
| 1123 | windgoto(msg_row, msg_col); |
| 1124 | |
| 1125 | do_shell(newcmd, 0); |
| 1126 | } |
| 1127 | else /* :range! */ |
Bram Moolenaar | ade0083 | 2006-03-10 21:46:58 +0000 | [diff] [blame] | 1128 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1129 | /* Careful: This may recursively call do_bang() again! (because of |
| 1130 | * autocommands) */ |
| 1131 | do_filter(line1, line2, eap, newcmd, do_in, do_out); |
Bram Moolenaar | ade0083 | 2006-03-10 21:46:58 +0000 | [diff] [blame] | 1132 | #ifdef FEAT_AUTOCMD |
| 1133 | apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf); |
| 1134 | #endif |
| 1135 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1136 | if (free_newcmd) |
| 1137 | vim_free(newcmd); |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * do_filter: filter lines through a command given by the user |
| 1142 | * |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1143 | * We mostly use temp files and the call_shell() routine here. This would |
| 1144 | * normally be done using pipes on a UNIX machine, but this is more portable |
| 1145 | * to non-unix machines. The call_shell() routine needs to be able |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1146 | * to deal with redirection somehow, and should handle things like looking |
| 1147 | * at the PATH env. variable, and adding reasonable extensions to the |
| 1148 | * command name given by the user. All reasonable versions of call_shell() |
| 1149 | * do this. |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1150 | * Alternatively, if on Unix and redirecting input or output, but not both, |
| 1151 | * and the 'shelltemp' option isn't set, use pipes. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1152 | * We use input redirection if do_in is TRUE. |
| 1153 | * We use output redirection if do_out is TRUE. |
| 1154 | */ |
| 1155 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1156 | do_filter( |
| 1157 | linenr_T line1, |
| 1158 | linenr_T line2, |
| 1159 | exarg_T *eap, /* for forced 'ff' and 'fenc' */ |
| 1160 | char_u *cmd, |
| 1161 | int do_in, |
| 1162 | int do_out) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1163 | { |
| 1164 | char_u *itmp = NULL; |
| 1165 | char_u *otmp = NULL; |
| 1166 | linenr_T linecount; |
| 1167 | linenr_T read_linecount; |
| 1168 | pos_T cursor_save; |
| 1169 | char_u *cmd_buf; |
| 1170 | #ifdef FEAT_AUTOCMD |
| 1171 | buf_T *old_curbuf = curbuf; |
| 1172 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1173 | int shell_flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1174 | |
| 1175 | if (*cmd == NUL) /* no filter command */ |
| 1176 | return; |
| 1177 | |
| 1178 | #ifdef WIN3264 |
| 1179 | /* |
| 1180 | * Check if external commands are allowed now. |
| 1181 | */ |
| 1182 | if (can_end_termcap_mode(TRUE) == FALSE) |
| 1183 | return; |
| 1184 | #endif |
| 1185 | |
| 1186 | cursor_save = curwin->w_cursor; |
| 1187 | linecount = line2 - line1 + 1; |
| 1188 | curwin->w_cursor.lnum = line1; |
| 1189 | curwin->w_cursor.col = 0; |
| 1190 | changed_line_abv_curs(); |
| 1191 | invalidate_botline(); |
| 1192 | |
| 1193 | /* |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1194 | * When using temp files: |
| 1195 | * 1. * Form temp file names |
| 1196 | * 2. * Write the lines to a temp file |
| 1197 | * 3. Run the filter command on the temp file |
| 1198 | * 4. * Read the output of the command into the buffer |
| 1199 | * 5. * Delete the original lines to be filtered |
| 1200 | * 6. * Remove the temp files |
| 1201 | * |
| 1202 | * When writing the input with a pipe or when catching the output with a |
| 1203 | * pipe only need to do 3. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1204 | */ |
| 1205 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1206 | if (do_out) |
| 1207 | shell_flags |= SHELL_DOOUT; |
| 1208 | |
Bram Moolenaar | 68a33fc | 2012-04-25 16:50:48 +0200 | [diff] [blame] | 1209 | #ifdef FEAT_FILTERPIPE |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1210 | if (!do_in && do_out && !p_stmp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1211 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1212 | /* Use a pipe to fetch stdout of the command, do not use a temp file. */ |
| 1213 | shell_flags |= SHELL_READ; |
| 1214 | curwin->w_cursor.lnum = line2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1215 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1216 | else if (do_in && !do_out && !p_stmp) |
| 1217 | { |
| 1218 | /* Use a pipe to write stdin of the command, do not use a temp file. */ |
| 1219 | shell_flags |= SHELL_WRITE; |
| 1220 | curbuf->b_op_start.lnum = line1; |
| 1221 | curbuf->b_op_end.lnum = line2; |
| 1222 | } |
| 1223 | else if (do_in && do_out && !p_stmp) |
| 1224 | { |
| 1225 | /* Use a pipe to write stdin and fetch stdout of the command, do not |
| 1226 | * use a temp file. */ |
| 1227 | shell_flags |= SHELL_READ|SHELL_WRITE; |
| 1228 | curbuf->b_op_start.lnum = line1; |
| 1229 | curbuf->b_op_end.lnum = line2; |
| 1230 | curwin->w_cursor.lnum = line2; |
| 1231 | } |
| 1232 | else |
| 1233 | #endif |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 1234 | if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL) |
| 1235 | || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL)) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1236 | { |
| 1237 | EMSG(_(e_notmp)); |
| 1238 | goto filterend; |
| 1239 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1240 | |
| 1241 | /* |
| 1242 | * The writing and reading of temp files will not be shown. |
| 1243 | * Vi also doesn't do this and the messages are not very informative. |
| 1244 | */ |
| 1245 | ++no_wait_return; /* don't call wait_return() while busy */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1246 | if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1247 | FALSE, FALSE, FALSE, TRUE) == FAIL) |
| 1248 | { |
| 1249 | msg_putchar('\n'); /* keep message from buf_write() */ |
| 1250 | --no_wait_return; |
| 1251 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 1252 | if (!aborting()) |
| 1253 | #endif |
| 1254 | (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */ |
| 1255 | goto filterend; |
| 1256 | } |
| 1257 | #ifdef FEAT_AUTOCMD |
| 1258 | if (curbuf != old_curbuf) |
| 1259 | goto filterend; |
| 1260 | #endif |
| 1261 | |
| 1262 | if (!do_out) |
| 1263 | msg_putchar('\n'); |
| 1264 | |
Bram Moolenaar | a9aafe5 | 2008-05-07 11:10:28 +0000 | [diff] [blame] | 1265 | /* Create the shell command in allocated memory. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1266 | cmd_buf = make_filter_cmd(cmd, itmp, otmp); |
| 1267 | if (cmd_buf == NULL) |
| 1268 | goto filterend; |
| 1269 | |
| 1270 | windgoto((int)Rows - 1, 0); |
| 1271 | cursor_on(); |
| 1272 | |
| 1273 | /* |
| 1274 | * When not redirecting the output the command can write anything to the |
| 1275 | * screen. If 'shellredir' is equal to ">", screen may be messed up by |
| 1276 | * stderr output of external command. Clear the screen later. |
| 1277 | * If do_in is FALSE, this could be something like ":r !cat", which may |
| 1278 | * also mess up the screen, clear it later. |
| 1279 | */ |
| 1280 | if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in) |
| 1281 | redraw_later_clear(); |
| 1282 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1283 | if (do_out) |
| 1284 | { |
| 1285 | if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL) |
Bram Moolenaar | a9aafe5 | 2008-05-07 11:10:28 +0000 | [diff] [blame] | 1286 | { |
| 1287 | vim_free(cmd_buf); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1288 | goto error; |
Bram Moolenaar | a9aafe5 | 2008-05-07 11:10:28 +0000 | [diff] [blame] | 1289 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1290 | redraw_curbuf_later(VALID); |
| 1291 | } |
| 1292 | read_linecount = curbuf->b_ml.ml_line_count; |
| 1293 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1294 | /* |
| 1295 | * When call_shell() fails wait_return() is called to give the user a |
| 1296 | * chance to read the error messages. Otherwise errors are ignored, so you |
| 1297 | * can see the error messages from the command that appear on stdout; use |
| 1298 | * 'u' to fix the text |
| 1299 | * Switch to cooked mode when not redirecting stdin, avoids that something |
| 1300 | * like ":r !cat" hangs. |
| 1301 | * Pass on the SHELL_DOOUT flag when the output is being redirected. |
| 1302 | */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1303 | if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1304 | { |
| 1305 | redraw_later_clear(); |
| 1306 | wait_return(FALSE); |
| 1307 | } |
| 1308 | vim_free(cmd_buf); |
| 1309 | |
| 1310 | did_check_timestamps = FALSE; |
| 1311 | need_check_timestamps = TRUE; |
| 1312 | |
| 1313 | /* When interrupting the shell command, it may still have produced some |
| 1314 | * useful output. Reset got_int here, so that readfile() won't cancel |
| 1315 | * reading. */ |
| 1316 | ui_breakcheck(); |
| 1317 | got_int = FALSE; |
| 1318 | |
| 1319 | if (do_out) |
| 1320 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1321 | if (otmp != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1322 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1323 | if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM, |
| 1324 | eap, READ_FILTER) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1325 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1326 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 1327 | if (!aborting()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1328 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1329 | { |
| 1330 | msg_putchar('\n'); |
| 1331 | EMSG2(_(e_notread), otmp); |
| 1332 | } |
| 1333 | goto error; |
| 1334 | } |
| 1335 | #ifdef FEAT_AUTOCMD |
| 1336 | if (curbuf != old_curbuf) |
| 1337 | goto filterend; |
| 1338 | #endif |
| 1339 | } |
| 1340 | |
| 1341 | read_linecount = curbuf->b_ml.ml_line_count - read_linecount; |
| 1342 | |
| 1343 | if (shell_flags & SHELL_READ) |
| 1344 | { |
| 1345 | curbuf->b_op_start.lnum = line2 + 1; |
| 1346 | curbuf->b_op_end.lnum = curwin->w_cursor.lnum; |
| 1347 | appended_lines_mark(line2, read_linecount); |
| 1348 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1349 | |
| 1350 | if (do_in) |
| 1351 | { |
| 1352 | if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL) |
| 1353 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1354 | if (read_linecount >= linecount) |
| 1355 | /* move all marks from old lines to new lines */ |
| 1356 | mark_adjust(line1, line2, linecount, 0L); |
| 1357 | else |
| 1358 | { |
| 1359 | /* move marks from old lines to new lines, delete marks |
| 1360 | * that are in deleted lines */ |
| 1361 | mark_adjust(line1, line1 + read_linecount - 1, |
| 1362 | linecount, 0L); |
| 1363 | mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | * Put cursor on first filtered line for ":range!cmd". |
| 1369 | * Adjust '[ and '] (set by buf_write()). |
| 1370 | */ |
| 1371 | curwin->w_cursor.lnum = line1; |
| 1372 | del_lines(linecount, TRUE); |
| 1373 | curbuf->b_op_start.lnum -= linecount; /* adjust '[ */ |
| 1374 | curbuf->b_op_end.lnum -= linecount; /* adjust '] */ |
| 1375 | write_lnum_adjust(-linecount); /* adjust last line |
| 1376 | for next write */ |
Bram Moolenaar | 8c71145 | 2005-01-14 21:53:12 +0000 | [diff] [blame] | 1377 | #ifdef FEAT_FOLDING |
| 1378 | foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum); |
| 1379 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1380 | } |
| 1381 | else |
| 1382 | { |
| 1383 | /* |
| 1384 | * Put cursor on last new line for ":r !cmd". |
| 1385 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1386 | linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1387 | curwin->w_cursor.lnum = curbuf->b_op_end.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1388 | } |
Bram Moolenaar | 8c71145 | 2005-01-14 21:53:12 +0000 | [diff] [blame] | 1389 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */ |
| 1391 | --no_wait_return; |
| 1392 | |
| 1393 | if (linecount > p_report) |
| 1394 | { |
| 1395 | if (do_in) |
| 1396 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1397 | vim_snprintf((char *)msg_buf, sizeof(msg_buf), |
| 1398 | _("%ld lines filtered"), (long)linecount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1399 | if (msg(msg_buf) && !msg_scroll) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1400 | /* save message to display it after redraw */ |
Bram Moolenaar | 238a564 | 2006-02-21 22:12:05 +0000 | [diff] [blame] | 1401 | set_keep_msg(msg_buf, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1402 | } |
| 1403 | else |
| 1404 | msgmore((long)linecount); |
| 1405 | } |
| 1406 | } |
| 1407 | else |
| 1408 | { |
| 1409 | error: |
| 1410 | /* put cursor back in same position for ":w !cmd" */ |
| 1411 | curwin->w_cursor = cursor_save; |
| 1412 | --no_wait_return; |
| 1413 | wait_return(FALSE); |
| 1414 | } |
| 1415 | |
| 1416 | filterend: |
| 1417 | |
| 1418 | #ifdef FEAT_AUTOCMD |
| 1419 | if (curbuf != old_curbuf) |
| 1420 | { |
| 1421 | --no_wait_return; |
| 1422 | EMSG(_("E135: *Filter* Autocommands must not change current buffer")); |
| 1423 | } |
| 1424 | #endif |
| 1425 | if (itmp != NULL) |
| 1426 | mch_remove(itmp); |
| 1427 | if (otmp != NULL) |
| 1428 | mch_remove(otmp); |
| 1429 | vim_free(itmp); |
| 1430 | vim_free(otmp); |
| 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * Call a shell to execute a command. |
| 1435 | * When "cmd" is NULL start an interactive shell. |
| 1436 | */ |
| 1437 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1438 | do_shell( |
| 1439 | char_u *cmd, |
| 1440 | int flags) /* may be SHELL_DOOUT when output is redirected */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1441 | { |
| 1442 | buf_T *buf; |
| 1443 | #ifndef FEAT_GUI_MSWIN |
| 1444 | int save_nwr; |
| 1445 | #endif |
| 1446 | #ifdef MSWIN |
| 1447 | int winstart = FALSE; |
| 1448 | #endif |
| 1449 | |
| 1450 | /* |
| 1451 | * Disallow shell commands for "rvim". |
| 1452 | * Disallow shell commands from .exrc and .vimrc in current directory for |
| 1453 | * security reasons. |
| 1454 | */ |
| 1455 | if (check_restricted() || check_secure()) |
| 1456 | { |
| 1457 | msg_end(); |
| 1458 | return; |
| 1459 | } |
| 1460 | |
| 1461 | #ifdef MSWIN |
| 1462 | /* |
| 1463 | * Check if external commands are allowed now. |
| 1464 | */ |
| 1465 | if (can_end_termcap_mode(TRUE) == FALSE) |
| 1466 | return; |
| 1467 | |
| 1468 | /* |
| 1469 | * Check if ":!start" is used. |
| 1470 | */ |
| 1471 | if (cmd != NULL) |
| 1472 | winstart = (STRNICMP(cmd, "start ", 6) == 0); |
| 1473 | #endif |
| 1474 | |
| 1475 | /* |
| 1476 | * For autocommands we want to get the output on the current screen, to |
| 1477 | * avoid having to type return below. |
| 1478 | */ |
| 1479 | msg_putchar('\r'); /* put cursor at start of line */ |
| 1480 | #ifdef FEAT_AUTOCMD |
| 1481 | if (!autocmd_busy) |
| 1482 | #endif |
| 1483 | { |
| 1484 | #ifdef MSWIN |
| 1485 | if (!winstart) |
| 1486 | #endif |
| 1487 | stoptermcap(); |
| 1488 | } |
| 1489 | #ifdef MSWIN |
| 1490 | if (!winstart) |
| 1491 | #endif |
| 1492 | msg_putchar('\n'); /* may shift screen one line up */ |
| 1493 | |
| 1494 | /* warning message before calling the shell */ |
| 1495 | if (p_warn |
| 1496 | #ifdef FEAT_AUTOCMD |
| 1497 | && !autocmd_busy |
| 1498 | #endif |
| 1499 | && msg_silent == 0) |
| 1500 | for (buf = firstbuf; buf; buf = buf->b_next) |
| 1501 | if (bufIsChanged(buf)) |
| 1502 | { |
| 1503 | #ifdef FEAT_GUI_MSWIN |
| 1504 | if (!winstart) |
| 1505 | starttermcap(); /* don't want a message box here */ |
| 1506 | #endif |
| 1507 | MSG_PUTS(_("[No write since last change]\n")); |
| 1508 | #ifdef FEAT_GUI_MSWIN |
| 1509 | if (!winstart) |
| 1510 | stoptermcap(); |
| 1511 | #endif |
| 1512 | break; |
| 1513 | } |
| 1514 | |
| 1515 | /* This windgoto is required for when the '\n' resulted in a "delete line |
| 1516 | * 1" command to the terminal. */ |
| 1517 | if (!swapping_screen()) |
| 1518 | windgoto(msg_row, msg_col); |
| 1519 | cursor_on(); |
| 1520 | (void)call_shell(cmd, SHELL_COOKED | flags); |
| 1521 | did_check_timestamps = FALSE; |
| 1522 | need_check_timestamps = TRUE; |
| 1523 | |
| 1524 | /* |
| 1525 | * put the message cursor at the end of the screen, avoids wait_return() |
| 1526 | * to overwrite the text that the external command showed |
| 1527 | */ |
| 1528 | if (!swapping_screen()) |
| 1529 | { |
| 1530 | msg_row = Rows - 1; |
| 1531 | msg_col = 0; |
| 1532 | } |
| 1533 | |
| 1534 | #ifdef FEAT_AUTOCMD |
| 1535 | if (autocmd_busy) |
| 1536 | { |
| 1537 | if (msg_silent == 0) |
| 1538 | redraw_later_clear(); |
| 1539 | } |
| 1540 | else |
| 1541 | #endif |
| 1542 | { |
| 1543 | /* |
| 1544 | * For ":sh" there is no need to call wait_return(), just redraw. |
| 1545 | * Also for the Win32 GUI (the output is in a console window). |
| 1546 | * Otherwise there is probably text on the screen that the user wants |
| 1547 | * to read before redrawing, so call wait_return(). |
| 1548 | */ |
| 1549 | #ifndef FEAT_GUI_MSWIN |
| 1550 | if (cmd == NULL |
| 1551 | # ifdef WIN3264 |
| 1552 | || (winstart && !need_wait_return) |
| 1553 | # endif |
| 1554 | ) |
| 1555 | { |
| 1556 | if (msg_silent == 0) |
| 1557 | redraw_later_clear(); |
| 1558 | need_wait_return = FALSE; |
| 1559 | } |
| 1560 | else |
| 1561 | { |
| 1562 | /* |
| 1563 | * If we switch screens when starttermcap() is called, we really |
| 1564 | * want to wait for "hit return to continue". |
| 1565 | */ |
| 1566 | save_nwr = no_wait_return; |
| 1567 | if (swapping_screen()) |
| 1568 | no_wait_return = FALSE; |
| 1569 | # ifdef AMIGA |
| 1570 | wait_return(term_console ? -1 : msg_silent == 0); /* see below */ |
| 1571 | # else |
| 1572 | wait_return(msg_silent == 0); |
| 1573 | # endif |
| 1574 | no_wait_return = save_nwr; |
| 1575 | } |
| 1576 | #endif /* FEAT_GUI_W32 */ |
| 1577 | |
| 1578 | #ifdef MSWIN |
| 1579 | if (!winstart) /* if winstart==TRUE, never stopped termcap! */ |
| 1580 | #endif |
| 1581 | starttermcap(); /* start termcap if not done by wait_return() */ |
| 1582 | |
| 1583 | /* |
| 1584 | * In an Amiga window redrawing is caused by asking the window size. |
| 1585 | * If we got an interrupt this will not work. The chance that the |
| 1586 | * window size is wrong is very small, but we need to redraw the |
| 1587 | * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY |
| 1588 | * but it saves an extra redraw. |
| 1589 | */ |
| 1590 | #ifdef AMIGA |
| 1591 | if (skip_redraw) /* ':' hit in wait_return() */ |
| 1592 | { |
| 1593 | if (msg_silent == 0) |
| 1594 | redraw_later_clear(); |
| 1595 | } |
| 1596 | else if (term_console) |
| 1597 | { |
| 1598 | OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */ |
| 1599 | if (got_int && msg_silent == 0) |
| 1600 | redraw_later_clear(); /* if got_int is TRUE, redraw needed */ |
| 1601 | else |
| 1602 | must_redraw = 0; /* no extra redraw needed */ |
| 1603 | } |
| 1604 | #endif |
| 1605 | } |
| 1606 | |
| 1607 | /* display any error messages now */ |
| 1608 | display_errors(); |
Bram Moolenaar | ade0083 | 2006-03-10 21:46:58 +0000 | [diff] [blame] | 1609 | |
| 1610 | #ifdef FEAT_AUTOCMD |
| 1611 | apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf); |
| 1612 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Create a shell command from a command string, input redirection file and |
| 1617 | * output redirection file. |
| 1618 | * Returns an allocated string with the shell command, or NULL for failure. |
| 1619 | */ |
| 1620 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1621 | make_filter_cmd( |
| 1622 | char_u *cmd, /* command */ |
| 1623 | char_u *itmp, /* NULL or name of input file */ |
| 1624 | char_u *otmp) /* NULL or name of output file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1625 | { |
| 1626 | char_u *buf; |
| 1627 | long_u len; |
| 1628 | |
Bram Moolenaar | 5307683 | 2015-12-31 19:53:21 +0100 | [diff] [blame] | 1629 | #if defined(UNIX) |
Bram Moolenaar | d442ec7 | 2014-05-09 20:33:04 +0200 | [diff] [blame] | 1630 | int is_fish_shell; |
Bram Moolenaar | 050fe7e | 2014-05-22 14:00:16 +0200 | [diff] [blame] | 1631 | char_u *shell_name = get_isolated_shell_name(); |
Bram Moolenaar | d442ec7 | 2014-05-09 20:33:04 +0200 | [diff] [blame] | 1632 | |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1633 | /* Account for fish's different syntax for subshells */ |
Bram Moolenaar | 050fe7e | 2014-05-22 14:00:16 +0200 | [diff] [blame] | 1634 | is_fish_shell = (fnamecmp(shell_name, "fish") == 0); |
| 1635 | vim_free(shell_name); |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1636 | if (is_fish_shell) |
| 1637 | len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */ |
| 1638 | else |
| 1639 | #endif |
| 1640 | len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1641 | if (itmp != NULL) |
| 1642 | len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */ |
| 1643 | if (otmp != NULL) |
| 1644 | len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */ |
| 1645 | buf = lalloc(len, TRUE); |
| 1646 | if (buf == NULL) |
| 1647 | return NULL; |
| 1648 | |
Bram Moolenaar | 5307683 | 2015-12-31 19:53:21 +0100 | [diff] [blame] | 1649 | #if defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1650 | /* |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1651 | * Put braces around the command (for concatenated commands) when |
| 1652 | * redirecting input and/or output. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1653 | */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1654 | if (itmp != NULL || otmp != NULL) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1655 | { |
| 1656 | if (is_fish_shell) |
| 1657 | vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd); |
| 1658 | else |
| 1659 | vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); |
| 1660 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1661 | else |
| 1662 | STRCPY(buf, cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1663 | if (itmp != NULL) |
| 1664 | { |
| 1665 | STRCAT(buf, " < "); |
| 1666 | STRCAT(buf, itmp); |
| 1667 | } |
| 1668 | #else |
| 1669 | /* |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1670 | * For shells that don't understand braces around commands, at least allow |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1671 | * the use of commands in a pipe. |
| 1672 | */ |
| 1673 | STRCPY(buf, cmd); |
| 1674 | if (itmp != NULL) |
| 1675 | { |
| 1676 | char_u *p; |
| 1677 | |
| 1678 | /* |
| 1679 | * If there is a pipe, we have to put the '<' in front of it. |
| 1680 | * Don't do this when 'shellquote' is not empty, otherwise the |
| 1681 | * redirection would be inside the quotes. |
| 1682 | */ |
| 1683 | if (*p_shq == NUL) |
| 1684 | { |
| 1685 | p = vim_strchr(buf, '|'); |
| 1686 | if (p != NULL) |
| 1687 | *p = NUL; |
| 1688 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1689 | STRCAT(buf, " <"); /* " < " causes problems on Amiga */ |
| 1690 | STRCAT(buf, itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1691 | if (*p_shq == NUL) |
| 1692 | { |
| 1693 | p = vim_strchr(cmd, '|'); |
| 1694 | if (p != NULL) |
| 1695 | { |
| 1696 | STRCAT(buf, " "); /* insert a space before the '|' for DOS */ |
| 1697 | STRCAT(buf, p); |
| 1698 | } |
| 1699 | } |
| 1700 | } |
| 1701 | #endif |
| 1702 | if (otmp != NULL) |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 1703 | append_redir(buf, (int)len, p_srr, otmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1704 | |
| 1705 | return buf; |
| 1706 | } |
| 1707 | |
| 1708 | /* |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 1709 | * Append output redirection for file "fname" to the end of string buffer |
| 1710 | * "buf[buflen]" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1711 | * Works with the 'shellredir' and 'shellpipe' options. |
| 1712 | * The caller should make sure that there is enough room: |
| 1713 | * STRLEN(opt) + STRLEN(fname) + 3 |
| 1714 | */ |
| 1715 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1716 | append_redir( |
| 1717 | char_u *buf, |
| 1718 | int buflen, |
| 1719 | char_u *opt, |
| 1720 | char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1721 | { |
| 1722 | char_u *p; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 1723 | char_u *end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1724 | |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 1725 | end = buf + STRLEN(buf); |
Bram Moolenaar | 542805a | 2013-08-02 14:15:13 +0200 | [diff] [blame] | 1726 | /* find "%s" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1727 | for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p) |
Bram Moolenaar | 542805a | 2013-08-02 14:15:13 +0200 | [diff] [blame] | 1728 | { |
| 1729 | if (p[1] == 's') /* found %s */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1730 | break; |
Bram Moolenaar | 542805a | 2013-08-02 14:15:13 +0200 | [diff] [blame] | 1731 | if (p[1] == '%') /* skip %% */ |
| 1732 | ++p; |
| 1733 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1734 | if (p != NULL) |
| 1735 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 1736 | *end = ' '; /* not really needed? Not with sh, ksh or bash */ |
| 1737 | vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)), |
| 1738 | (char *)opt, (char *)fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1739 | } |
| 1740 | else |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 1741 | vim_snprintf((char *)end, (size_t)(buflen - (end - buf)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1742 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1743 | " %s %s", |
| 1744 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1745 | " %s%s", /* " > %s" causes problems on Amiga */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1746 | #endif |
| 1747 | (char *)opt, (char *)fname); |
| 1748 | } |
| 1749 | |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 1750 | #if defined(FEAT_VIMINFO) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1751 | |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 1752 | static int no_viminfo(void); |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 1753 | static void write_viminfo_barlines(vir_T *virp, FILE *fp_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1754 | static int viminfo_errcnt; |
| 1755 | |
| 1756 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1757 | no_viminfo(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1758 | { |
| 1759 | /* "vim -i NONE" does not read or write a viminfo file */ |
| 1760 | return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0); |
| 1761 | } |
| 1762 | |
| 1763 | /* |
| 1764 | * Report an error for reading a viminfo file. |
| 1765 | * Count the number of errors. When there are more than 10, return TRUE. |
| 1766 | */ |
| 1767 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1768 | viminfo_error(char *errnum, char *message, char_u *line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1769 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1770 | vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "), |
| 1771 | errnum, message); |
Bram Moolenaar | 6c96483 | 2007-12-09 18:38:35 +0000 | [diff] [blame] | 1772 | STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1); |
Bram Moolenaar | 758711c | 2005-02-02 23:11:38 +0000 | [diff] [blame] | 1773 | if (IObuff[STRLEN(IObuff) - 1] == '\n') |
| 1774 | IObuff[STRLEN(IObuff) - 1] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1775 | emsg(IObuff); |
| 1776 | if (++viminfo_errcnt >= 10) |
| 1777 | { |
| 1778 | EMSG(_("E136: viminfo: Too many errors, skipping rest of file")); |
| 1779 | return TRUE; |
| 1780 | } |
| 1781 | return FALSE; |
| 1782 | } |
| 1783 | |
| 1784 | /* |
| 1785 | * read_viminfo() -- Read the viminfo file. Registers etc. which are already |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 1786 | * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1787 | */ |
| 1788 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1789 | read_viminfo( |
| 1790 | char_u *file, /* file name or NULL to use default name */ |
| 1791 | int flags) /* VIF_WANT_INFO et al. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1792 | { |
| 1793 | FILE *fp; |
| 1794 | char_u *fname; |
| 1795 | |
| 1796 | if (no_viminfo()) |
| 1797 | return FAIL; |
| 1798 | |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 1799 | fname = viminfo_filename(file); /* get file name in allocated buffer */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1800 | if (fname == NULL) |
| 1801 | return FAIL; |
| 1802 | fp = mch_fopen((char *)fname, READBIN); |
| 1803 | |
| 1804 | if (p_verbose > 0) |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 1805 | { |
| 1806 | verbose_enter(); |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1807 | smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"), |
| 1808 | fname, |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 1809 | (flags & VIF_WANT_INFO) ? _(" info") : "", |
| 1810 | (flags & VIF_WANT_MARKS) ? _(" marks") : "", |
| 1811 | (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "", |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1812 | fp == NULL ? _(" FAILED") : ""); |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 1813 | verbose_leave(); |
| 1814 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1815 | |
| 1816 | vim_free(fname); |
| 1817 | if (fp == NULL) |
| 1818 | return FAIL; |
| 1819 | |
| 1820 | viminfo_errcnt = 0; |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 1821 | do_viminfo(fp, NULL, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1822 | |
| 1823 | fclose(fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1824 | return OK; |
| 1825 | } |
| 1826 | |
| 1827 | /* |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 1828 | * Write the viminfo file. The old one is read in first so that effectively a |
| 1829 | * merge of current info and old info is done. This allows multiple vims to |
| 1830 | * run simultaneously, without losing any marks etc. |
| 1831 | * If "forceit" is TRUE, then the old file is not read in, and only internal |
| 1832 | * info is written to the file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1833 | */ |
| 1834 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1835 | write_viminfo(char_u *file, int forceit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1836 | { |
| 1837 | char_u *fname; |
| 1838 | FILE *fp_in = NULL; /* input viminfo file, if any */ |
| 1839 | FILE *fp_out = NULL; /* output viminfo file */ |
| 1840 | char_u *tempname = NULL; /* name of temp viminfo file */ |
| 1841 | struct stat st_new; /* mch_stat() of potential new file */ |
| 1842 | char_u *wp; |
| 1843 | #if defined(UNIX) || defined(VMS) |
| 1844 | mode_t umask_save; |
| 1845 | #endif |
| 1846 | #ifdef UNIX |
| 1847 | int shortname = FALSE; /* use 8.3 file name */ |
| 1848 | struct stat st_old; /* mch_stat() of existing viminfo file */ |
| 1849 | #endif |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1850 | #ifdef WIN3264 |
Bram Moolenaar | 8a52ba7 | 2015-11-02 14:45:56 +0100 | [diff] [blame] | 1851 | int hidden = FALSE; |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1852 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1853 | |
| 1854 | if (no_viminfo()) |
| 1855 | return; |
| 1856 | |
| 1857 | fname = viminfo_filename(file); /* may set to default if NULL */ |
| 1858 | if (fname == NULL) |
| 1859 | return; |
| 1860 | |
| 1861 | fp_in = mch_fopen((char *)fname, READBIN); |
| 1862 | if (fp_in == NULL) |
| 1863 | { |
| 1864 | /* if it does exist, but we can't read it, don't try writing */ |
| 1865 | if (mch_stat((char *)fname, &st_new) == 0) |
| 1866 | goto end; |
| 1867 | #if defined(UNIX) || defined(VMS) |
| 1868 | /* |
| 1869 | * For Unix we create the .viminfo non-accessible for others, |
| 1870 | * because it may contain text from non-accessible documents. |
| 1871 | */ |
| 1872 | umask_save = umask(077); |
| 1873 | #endif |
| 1874 | fp_out = mch_fopen((char *)fname, WRITEBIN); |
| 1875 | #if defined(UNIX) || defined(VMS) |
| 1876 | (void)umask(umask_save); |
| 1877 | #endif |
| 1878 | } |
| 1879 | else |
| 1880 | { |
| 1881 | /* |
| 1882 | * There is an existing viminfo file. Create a temporary file to |
| 1883 | * write the new viminfo into, in the same directory as the |
| 1884 | * existing viminfo file, which will be renamed later. |
| 1885 | */ |
| 1886 | #ifdef UNIX |
| 1887 | /* |
| 1888 | * For Unix we check the owner of the file. It's not very nice to |
| 1889 | * overwrite a user's viminfo file after a "su root", with a |
| 1890 | * viminfo file that the user can't read. |
| 1891 | */ |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1892 | st_old.st_dev = (dev_t)0; |
Bram Moolenaar | 6f19245 | 2007-11-08 19:49:02 +0000 | [diff] [blame] | 1893 | st_old.st_ino = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1894 | st_old.st_mode = 0600; |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 1895 | if (mch_stat((char *)fname, &st_old) == 0 |
| 1896 | && getuid() != ROOT_UID |
Bram Moolenaar | 12625ca | 2005-11-25 19:58:47 +0000 | [diff] [blame] | 1897 | && !(st_old.st_uid == getuid() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1898 | ? (st_old.st_mode & 0200) |
| 1899 | : (st_old.st_gid == getgid() |
| 1900 | ? (st_old.st_mode & 0020) |
| 1901 | : (st_old.st_mode & 0002)))) |
| 1902 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1903 | int tt = msg_didany; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1904 | |
| 1905 | /* avoid a wait_return for this message, it's annoying */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1906 | EMSG2(_("E137: Viminfo file is not writable: %s"), fname); |
| 1907 | msg_didany = tt; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 1908 | fclose(fp_in); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1909 | goto end; |
| 1910 | } |
| 1911 | #endif |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1912 | #ifdef WIN3264 |
| 1913 | /* Get the file attributes of the existing viminfo file. */ |
Bram Moolenaar | 8a52ba7 | 2015-11-02 14:45:56 +0100 | [diff] [blame] | 1914 | hidden = mch_ishidden(fname); |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1915 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1916 | |
| 1917 | /* |
| 1918 | * Make tempname. |
| 1919 | * May try twice: Once normal and once with shortname set, just in |
| 1920 | * case somebody puts his viminfo file in an 8.3 filesystem. |
| 1921 | */ |
| 1922 | for (;;) |
| 1923 | { |
| 1924 | tempname = buf_modname( |
| 1925 | #ifdef UNIX |
| 1926 | shortname, |
| 1927 | #else |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 1928 | # ifdef FEAT_GUI_W32 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1929 | gui_is_win32s(), |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 1930 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1931 | FALSE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1932 | # endif |
| 1933 | #endif |
| 1934 | fname, |
| 1935 | #ifdef VMS |
| 1936 | (char_u *)"-tmp", |
| 1937 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1938 | (char_u *)".tmp", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1939 | #endif |
| 1940 | FALSE); |
| 1941 | if (tempname == NULL) /* out of memory */ |
| 1942 | break; |
| 1943 | |
| 1944 | /* |
| 1945 | * Check if tempfile already exists. Never overwrite an |
| 1946 | * existing file! |
| 1947 | */ |
| 1948 | if (mch_stat((char *)tempname, &st_new) == 0) |
| 1949 | { |
| 1950 | #ifdef UNIX |
| 1951 | /* |
| 1952 | * Check if tempfile is same as original file. May happen |
| 1953 | * when modname() gave the same file back. E.g. silly |
| 1954 | * link, or file name-length reached. Try again with |
| 1955 | * shortname set. |
| 1956 | */ |
Bram Moolenaar | 12625ca | 2005-11-25 19:58:47 +0000 | [diff] [blame] | 1957 | if (!shortname && st_new.st_dev == st_old.st_dev |
| 1958 | && st_new.st_ino == st_old.st_ino) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1959 | { |
| 1960 | vim_free(tempname); |
| 1961 | tempname = NULL; |
| 1962 | shortname = TRUE; |
| 1963 | continue; |
| 1964 | } |
| 1965 | #endif |
| 1966 | /* |
| 1967 | * Try another name. Change one character, just before |
| 1968 | * the extension. This should also work for an 8.3 |
| 1969 | * file name, when after adding the extension it still is |
| 1970 | * the same file as the original. |
| 1971 | */ |
| 1972 | wp = tempname + STRLEN(tempname) - 5; |
| 1973 | if (wp < gettail(tempname)) /* empty file name? */ |
| 1974 | wp = gettail(tempname); |
| 1975 | for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0; |
| 1976 | --*wp) |
| 1977 | { |
| 1978 | /* |
| 1979 | * They all exist? Must be something wrong! Don't |
| 1980 | * write the viminfo file then. |
| 1981 | */ |
| 1982 | if (*wp == 'a') |
| 1983 | { |
| 1984 | vim_free(tempname); |
| 1985 | tempname = NULL; |
| 1986 | break; |
| 1987 | } |
| 1988 | } |
| 1989 | } |
| 1990 | break; |
| 1991 | } |
| 1992 | |
| 1993 | if (tempname != NULL) |
| 1994 | { |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 1995 | #ifdef VMS |
| 1996 | /* fdopen() fails for some reason */ |
Bram Moolenaar | cf03447 | 2006-03-15 23:07:59 +0000 | [diff] [blame] | 1997 | umask_save = umask(077); |
| 1998 | fp_out = mch_fopen((char *)tempname, WRITEBIN); |
| 1999 | (void)umask(umask_save); |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 2000 | #else |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2001 | int fd; |
| 2002 | |
| 2003 | /* Use mch_open() to be able to use O_NOFOLLOW and set file |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 2004 | * protection: |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2005 | * Unix: same as original file, but strip s-bit. Reset umask to |
| 2006 | * avoid it getting in the way. |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 2007 | * Others: r&w for user only. */ |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 2008 | # ifdef UNIX |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2009 | umask_save = umask(0); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2010 | fd = mch_open((char *)tempname, |
| 2011 | O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, |
Bram Moolenaar | bba577a | 2005-11-28 23:05:55 +0000 | [diff] [blame] | 2012 | (int)((st_old.st_mode & 0777) | 0600)); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2013 | (void)umask(umask_save); |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 2014 | # else |
Bram Moolenaar | bba577a | 2005-11-28 23:05:55 +0000 | [diff] [blame] | 2015 | fd = mch_open((char *)tempname, |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2016 | O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600); |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 2017 | # endif |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2018 | if (fd < 0) |
| 2019 | fp_out = NULL; |
| 2020 | else |
| 2021 | fp_out = fdopen(fd, WRITEBIN); |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 2022 | #endif /* VMS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2023 | |
| 2024 | /* |
| 2025 | * If we can't create in the same directory, try creating a |
| 2026 | * "normal" temp file. |
| 2027 | */ |
| 2028 | if (fp_out == NULL) |
| 2029 | { |
| 2030 | vim_free(tempname); |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 2031 | if ((tempname = vim_tempname('o', TRUE)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2032 | fp_out = mch_fopen((char *)tempname, WRITEBIN); |
| 2033 | } |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2034 | |
| 2035 | #if defined(UNIX) && defined(HAVE_FCHOWN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2036 | /* |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2037 | * Make sure the owner can read/write it. This only works for |
| 2038 | * root. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2039 | */ |
| 2040 | if (fp_out != NULL) |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 2041 | ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2042 | #endif |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | /* |
| 2047 | * Check if the new viminfo file can be written to. |
| 2048 | */ |
| 2049 | if (fp_out == NULL) |
| 2050 | { |
| 2051 | EMSG2(_("E138: Can't write viminfo file %s!"), |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 2052 | (fp_in == NULL || tempname == NULL) ? fname : tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2053 | if (fp_in != NULL) |
| 2054 | fclose(fp_in); |
| 2055 | goto end; |
| 2056 | } |
| 2057 | |
| 2058 | if (p_verbose > 0) |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 2059 | { |
| 2060 | verbose_enter(); |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 2061 | smsg((char_u *)_("Writing viminfo file \"%s\""), fname); |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 2062 | verbose_leave(); |
| 2063 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2064 | |
| 2065 | viminfo_errcnt = 0; |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 2066 | do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2067 | |
Bram Moolenaar | 927030a | 2016-03-15 18:23:55 +0100 | [diff] [blame] | 2068 | if (fclose(fp_out) == EOF) |
| 2069 | ++viminfo_errcnt; |
| 2070 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | if (fp_in != NULL) |
| 2072 | { |
| 2073 | fclose(fp_in); |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 2074 | |
Bram Moolenaar | 3ed8b13 | 2014-07-09 21:18:04 +0200 | [diff] [blame] | 2075 | /* In case of an error keep the original viminfo file. Otherwise |
| 2076 | * rename the newly written file. Give an error if that fails. */ |
Bram Moolenaar | 927030a | 2016-03-15 18:23:55 +0100 | [diff] [blame] | 2077 | if (viminfo_errcnt == 0) |
Bram Moolenaar | 3ed8b13 | 2014-07-09 21:18:04 +0200 | [diff] [blame] | 2078 | { |
Bram Moolenaar | 927030a | 2016-03-15 18:23:55 +0100 | [diff] [blame] | 2079 | if (vim_rename(tempname, fname) == -1) |
| 2080 | { |
| 2081 | ++viminfo_errcnt; |
| 2082 | EMSG2(_("E886: Can't rename viminfo file to %s!"), fname); |
| 2083 | } |
| 2084 | # ifdef WIN3264 |
| 2085 | /* If the viminfo file was hidden then also hide the new file. */ |
| 2086 | else if (hidden) |
| 2087 | mch_hide(fname); |
| 2088 | # endif |
Bram Moolenaar | 3ed8b13 | 2014-07-09 21:18:04 +0200 | [diff] [blame] | 2089 | } |
| 2090 | if (viminfo_errcnt > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2091 | mch_remove(tempname); |
| 2092 | } |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 2093 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2094 | end: |
| 2095 | vim_free(fname); |
| 2096 | vim_free(tempname); |
| 2097 | } |
| 2098 | |
| 2099 | /* |
| 2100 | * Get the viminfo file name to use. |
| 2101 | * If "file" is given and not empty, use it (has already been expanded by |
| 2102 | * cmdline functions). |
| 2103 | * Otherwise use "-i file_name", value from 'viminfo' or the default, and |
| 2104 | * expand environment variables. |
| 2105 | * Returns an allocated string. NULL when out of memory. |
| 2106 | */ |
| 2107 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2108 | viminfo_filename(char_u *file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2109 | { |
| 2110 | if (file == NULL || *file == NUL) |
| 2111 | { |
| 2112 | if (use_viminfo != NULL) |
| 2113 | file = use_viminfo; |
| 2114 | else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL) |
| 2115 | { |
| 2116 | #ifdef VIMINFO_FILE2 |
| 2117 | /* don't use $HOME when not defined (turned into "c:/"!). */ |
| 2118 | # ifdef VMS |
| 2119 | if (mch_getenv((char_u *)"SYS$LOGIN") == NULL) |
| 2120 | # else |
| 2121 | if (mch_getenv((char_u *)"HOME") == NULL) |
| 2122 | # endif |
| 2123 | { |
| 2124 | /* don't use $VIM when not available. */ |
| 2125 | expand_env((char_u *)"$VIM", NameBuff, MAXPATHL); |
| 2126 | if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */ |
| 2127 | file = (char_u *)VIMINFO_FILE2; |
| 2128 | else |
| 2129 | file = (char_u *)VIMINFO_FILE; |
| 2130 | } |
| 2131 | else |
| 2132 | #endif |
| 2133 | file = (char_u *)VIMINFO_FILE; |
| 2134 | } |
| 2135 | expand_env(file, NameBuff, MAXPATHL); |
| 2136 | file = NameBuff; |
| 2137 | } |
| 2138 | return vim_strsave(file); |
| 2139 | } |
| 2140 | |
| 2141 | /* |
| 2142 | * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo(). |
| 2143 | */ |
| 2144 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2145 | do_viminfo(FILE *fp_in, FILE *fp_out, int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2146 | { |
| 2147 | int count = 0; |
| 2148 | int eof = FALSE; |
| 2149 | vir_T vir; |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 2150 | int merge = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2151 | |
| 2152 | if ((vir.vir_line = alloc(LSIZE)) == NULL) |
| 2153 | return; |
| 2154 | vir.vir_fd = fp_in; |
| 2155 | #ifdef FEAT_MBYTE |
| 2156 | vir.vir_conv.vc_type = CONV_NONE; |
| 2157 | #endif |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 2158 | ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2159 | |
| 2160 | if (fp_in != NULL) |
| 2161 | { |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 2162 | if (flags & VIF_WANT_INFO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 2163 | { |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 2164 | eof = read_viminfo_up_to_marks(&vir, |
| 2165 | flags & VIF_FORCEIT, fp_out != NULL); |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 2166 | merge = TRUE; |
| 2167 | } |
| 2168 | else if (flags != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2169 | /* Skip info, find start of marks */ |
| 2170 | while (!(eof = viminfo_readline(&vir)) |
| 2171 | && vir.vir_line[0] != '>') |
| 2172 | ; |
| 2173 | } |
| 2174 | if (fp_out != NULL) |
| 2175 | { |
| 2176 | /* Write the info: */ |
| 2177 | fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"), |
| 2178 | VIM_VERSION_MEDIUM); |
Bram Moolenaar | 2f1e050 | 2010-08-13 11:18:02 +0200 | [diff] [blame] | 2179 | fputs(_("# You may edit it if you're careful!\n\n"), fp_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2180 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 2f1e050 | 2010-08-13 11:18:02 +0200 | [diff] [blame] | 2181 | fputs(_("# Value of 'encoding' when this file was written\n"), fp_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2182 | fprintf(fp_out, "*encoding=%s\n\n", p_enc); |
| 2183 | #endif |
| 2184 | write_viminfo_search_pattern(fp_out); |
| 2185 | write_viminfo_sub_string(fp_out); |
| 2186 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 2187 | write_viminfo_history(fp_out, merge); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2188 | #endif |
| 2189 | write_viminfo_registers(fp_out); |
| 2190 | #ifdef FEAT_EVAL |
| 2191 | write_viminfo_varlist(fp_out); |
| 2192 | #endif |
| 2193 | write_viminfo_filemarks(fp_out); |
| 2194 | write_viminfo_bufferlist(fp_out); |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 2195 | write_viminfo_barlines(&vir, fp_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2196 | count = write_viminfo_marks(fp_out); |
| 2197 | } |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 2198 | if (fp_in != NULL |
| 2199 | && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT))) |
| 2200 | copy_viminfo_marks(&vir, fp_out, count, eof, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2201 | |
| 2202 | vim_free(vir.vir_line); |
| 2203 | #ifdef FEAT_MBYTE |
| 2204 | if (vir.vir_conv.vc_type != CONV_NONE) |
| 2205 | convert_setup(&vir.vir_conv, NULL, NULL); |
| 2206 | #endif |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 2207 | ga_clear_strings(&vir.vir_barlines); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | /* |
| 2211 | * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the |
| 2212 | * first part of the viminfo file which contains everything but the marks that |
| 2213 | * are local to a file. Returns TRUE when end-of-file is reached. -- webb |
| 2214 | */ |
| 2215 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2216 | read_viminfo_up_to_marks( |
| 2217 | vir_T *virp, |
| 2218 | int forceit, |
| 2219 | int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2220 | { |
| 2221 | int eof; |
| 2222 | buf_T *buf; |
| 2223 | |
| 2224 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 2225 | prepare_viminfo_history(forceit ? 9999 : 0, writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2226 | #endif |
| 2227 | eof = viminfo_readline(virp); |
| 2228 | while (!eof && virp->vir_line[0] != '>') |
| 2229 | { |
| 2230 | switch (virp->vir_line[0]) |
| 2231 | { |
| 2232 | /* Characters reserved for future expansion, ignored now */ |
| 2233 | case '+': /* "+40 /path/dir file", for running vim without args */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2234 | case '^': /* to be defined */ |
| 2235 | case '<': /* long line - ignored */ |
| 2236 | /* A comment or empty line. */ |
| 2237 | case NUL: |
| 2238 | case '\r': |
| 2239 | case '\n': |
| 2240 | case '#': |
| 2241 | eof = viminfo_readline(virp); |
| 2242 | break; |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 2243 | case '|': /* copy line (for future use) */ |
| 2244 | if (writing) |
| 2245 | ga_add_string(&virp->vir_barlines, virp->vir_line); |
| 2246 | eof = viminfo_readline(virp); |
| 2247 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2248 | case '*': /* "*encoding=value" */ |
| 2249 | eof = viminfo_encoding(virp); |
| 2250 | break; |
| 2251 | case '!': /* global variable */ |
| 2252 | #ifdef FEAT_EVAL |
| 2253 | eof = read_viminfo_varlist(virp, writing); |
| 2254 | #else |
| 2255 | eof = viminfo_readline(virp); |
| 2256 | #endif |
| 2257 | break; |
| 2258 | case '%': /* entry for buffer list */ |
| 2259 | eof = read_viminfo_bufferlist(virp, writing); |
| 2260 | break; |
| 2261 | case '"': |
| 2262 | eof = read_viminfo_register(virp, forceit); |
| 2263 | break; |
| 2264 | case '/': /* Search string */ |
| 2265 | case '&': /* Substitute search string */ |
| 2266 | case '~': /* Last search string, followed by '/' or '&' */ |
| 2267 | eof = read_viminfo_search_pattern(virp, forceit); |
| 2268 | break; |
| 2269 | case '$': |
| 2270 | eof = read_viminfo_sub_string(virp, forceit); |
| 2271 | break; |
| 2272 | case ':': |
| 2273 | case '?': |
| 2274 | case '=': |
| 2275 | case '@': |
| 2276 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 2277 | eof = read_viminfo_history(virp, writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2278 | #else |
| 2279 | eof = viminfo_readline(virp); |
| 2280 | #endif |
| 2281 | break; |
| 2282 | case '-': |
| 2283 | case '\'': |
| 2284 | eof = read_viminfo_filemark(virp, forceit); |
| 2285 | break; |
| 2286 | default: |
| 2287 | if (viminfo_error("E575: ", _("Illegal starting char"), |
| 2288 | virp->vir_line)) |
| 2289 | eof = TRUE; |
| 2290 | else |
| 2291 | eof = viminfo_readline(virp); |
| 2292 | break; |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | #ifdef FEAT_CMDHIST |
| 2297 | /* Finish reading history items. */ |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 2298 | if (!writing) |
| 2299 | finish_viminfo_history(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2300 | #endif |
| 2301 | |
| 2302 | /* Change file names to buffer numbers for fmarks. */ |
| 2303 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2304 | fmarks_check_names(buf); |
| 2305 | |
| 2306 | return eof; |
| 2307 | } |
| 2308 | |
| 2309 | /* |
| 2310 | * Compare the 'encoding' value in the viminfo file with the current value of |
| 2311 | * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for |
| 2312 | * conversion of text with iconv() in viminfo_readstring(). |
| 2313 | */ |
| 2314 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2315 | viminfo_encoding(vir_T *virp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2316 | { |
| 2317 | #ifdef FEAT_MBYTE |
| 2318 | char_u *p; |
| 2319 | int i; |
| 2320 | |
| 2321 | if (get_viminfo_parameter('c') != 0) |
| 2322 | { |
| 2323 | p = vim_strchr(virp->vir_line, '='); |
| 2324 | if (p != NULL) |
| 2325 | { |
| 2326 | /* remove trailing newline */ |
| 2327 | ++p; |
| 2328 | for (i = 0; vim_isprintc(p[i]); ++i) |
| 2329 | ; |
| 2330 | p[i] = NUL; |
| 2331 | |
| 2332 | convert_setup(&virp->vir_conv, p, p_enc); |
| 2333 | } |
| 2334 | } |
| 2335 | #endif |
| 2336 | return viminfo_readline(virp); |
| 2337 | } |
| 2338 | |
| 2339 | /* |
| 2340 | * Read a line from the viminfo file. |
| 2341 | * Returns TRUE for end-of-file; |
| 2342 | */ |
| 2343 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2344 | viminfo_readline(vir_T *virp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2345 | { |
| 2346 | return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd); |
| 2347 | } |
| 2348 | |
| 2349 | /* |
| 2350 | * check string read from viminfo file |
| 2351 | * remove '\n' at the end of the line |
| 2352 | * - replace CTRL-V CTRL-V with CTRL-V |
| 2353 | * - replace CTRL-V 'n' with '\n' |
| 2354 | * |
| 2355 | * Check for a long line as written by viminfo_writestring(). |
| 2356 | * |
| 2357 | * Return the string in allocated memory (NULL when out of memory). |
| 2358 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2360 | viminfo_readstring( |
| 2361 | vir_T *virp, |
| 2362 | int off, /* offset for virp->vir_line */ |
| 2363 | int convert UNUSED) /* convert the string */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2364 | { |
| 2365 | char_u *retval; |
| 2366 | char_u *s, *d; |
| 2367 | long len; |
| 2368 | |
| 2369 | if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1])) |
| 2370 | { |
| 2371 | len = atol((char *)virp->vir_line + off + 1); |
| 2372 | retval = lalloc(len, TRUE); |
| 2373 | if (retval == NULL) |
| 2374 | { |
| 2375 | /* Line too long? File messed up? Skip next line. */ |
| 2376 | (void)vim_fgets(virp->vir_line, 10, virp->vir_fd); |
| 2377 | return NULL; |
| 2378 | } |
| 2379 | (void)vim_fgets(retval, (int)len, virp->vir_fd); |
| 2380 | s = retval + 1; /* Skip the leading '<' */ |
| 2381 | } |
| 2382 | else |
| 2383 | { |
| 2384 | retval = vim_strsave(virp->vir_line + off); |
| 2385 | if (retval == NULL) |
| 2386 | return NULL; |
| 2387 | s = retval; |
| 2388 | } |
| 2389 | |
| 2390 | /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */ |
| 2391 | d = retval; |
| 2392 | while (*s != NUL && *s != '\n') |
| 2393 | { |
| 2394 | if (s[0] == Ctrl_V && s[1] != NUL) |
| 2395 | { |
| 2396 | if (s[1] == 'n') |
| 2397 | *d++ = '\n'; |
| 2398 | else |
| 2399 | *d++ = Ctrl_V; |
| 2400 | s += 2; |
| 2401 | } |
| 2402 | else |
| 2403 | *d++ = *s++; |
| 2404 | } |
| 2405 | *d = NUL; |
| 2406 | |
| 2407 | #ifdef FEAT_MBYTE |
| 2408 | if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL) |
| 2409 | { |
| 2410 | d = string_convert(&virp->vir_conv, retval, NULL); |
| 2411 | if (d != NULL) |
| 2412 | { |
| 2413 | vim_free(retval); |
| 2414 | retval = d; |
| 2415 | } |
| 2416 | } |
| 2417 | #endif |
| 2418 | |
| 2419 | return retval; |
| 2420 | } |
| 2421 | |
| 2422 | /* |
| 2423 | * write string to viminfo file |
| 2424 | * - replace CTRL-V with CTRL-V CTRL-V |
| 2425 | * - replace '\n' with CTRL-V 'n' |
| 2426 | * - add a '\n' at the end |
| 2427 | * |
| 2428 | * For a long line: |
| 2429 | * - write " CTRL-V <length> \n " in first line |
| 2430 | * - write " < <string> \n " in second line |
| 2431 | */ |
| 2432 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2433 | viminfo_writestring(FILE *fd, char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2434 | { |
| 2435 | int c; |
| 2436 | char_u *s; |
| 2437 | int len = 0; |
| 2438 | |
| 2439 | for (s = p; *s != NUL; ++s) |
| 2440 | { |
| 2441 | if (*s == Ctrl_V || *s == '\n') |
| 2442 | ++len; |
| 2443 | ++len; |
| 2444 | } |
| 2445 | |
| 2446 | /* If the string will be too long, write its length and put it in the next |
| 2447 | * line. Take into account that some room is needed for what comes before |
| 2448 | * the string (e.g., variable name). Add something to the length for the |
| 2449 | * '<', NL and trailing NUL. */ |
| 2450 | if (len > LSIZE / 2) |
| 2451 | fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3); |
| 2452 | |
| 2453 | while ((c = *p++) != NUL) |
| 2454 | { |
| 2455 | if (c == Ctrl_V || c == '\n') |
| 2456 | { |
| 2457 | putc(Ctrl_V, fd); |
| 2458 | if (c == '\n') |
| 2459 | c = 'n'; |
| 2460 | } |
| 2461 | putc(c, fd); |
| 2462 | } |
| 2463 | putc('\n', fd); |
| 2464 | } |
Bram Moolenaar | b20e334 | 2016-01-18 23:29:01 +0100 | [diff] [blame] | 2465 | |
| 2466 | static void |
| 2467 | write_viminfo_barlines(vir_T *virp, FILE *fp_out) |
| 2468 | { |
| 2469 | int i; |
| 2470 | garray_T *gap = &virp->vir_barlines; |
| 2471 | |
| 2472 | if (gap->ga_len > 0) |
| 2473 | { |
| 2474 | fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out); |
| 2475 | |
| 2476 | for (i = 0; i < gap->ga_len; ++i) |
| 2477 | fputs(((char **)(gap->ga_data))[i], fp_out); |
| 2478 | } |
| 2479 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2480 | #endif /* FEAT_VIMINFO */ |
| 2481 | |
| 2482 | /* |
| 2483 | * Implementation of ":fixdel", also used by get_stty(). |
| 2484 | * <BS> resulting <Del> |
| 2485 | * ^? ^H |
| 2486 | * not ^? ^? |
| 2487 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2488 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2489 | do_fixdel(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2490 | { |
| 2491 | char_u *p; |
| 2492 | |
| 2493 | p = find_termcode((char_u *)"kb"); |
| 2494 | add_termcode((char_u *)"kD", p != NULL |
| 2495 | && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE); |
| 2496 | } |
| 2497 | |
| 2498 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2499 | print_line_no_prefix( |
| 2500 | linenr_T lnum, |
| 2501 | int use_number, |
| 2502 | int list) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2503 | { |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 2504 | char_u numbuf[30]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2505 | |
| 2506 | if (curwin->w_p_nu || use_number) |
| 2507 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 2508 | vim_snprintf((char *)numbuf, sizeof(numbuf), |
| 2509 | "%*ld ", number_width(curwin), (long)lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2510 | msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */ |
| 2511 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 2512 | msg_prt_line(ml_get(lnum), list); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | /* |
| 2516 | * Print a text line. Also in silent mode ("ex -s"). |
| 2517 | */ |
| 2518 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2519 | print_line(linenr_T lnum, int use_number, int list) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2520 | { |
| 2521 | int save_silent = silent_mode; |
| 2522 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2523 | msg_start(); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 2524 | silent_mode = FALSE; |
| 2525 | info_message = TRUE; /* use mch_msg(), not mch_errmsg() */ |
| 2526 | print_line_no_prefix(lnum, use_number, list); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2527 | if (save_silent) |
| 2528 | { |
| 2529 | msg_putchar('\n'); |
| 2530 | cursor_on(); /* msg_start() switches it off */ |
| 2531 | out_flush(); |
| 2532 | silent_mode = save_silent; |
| 2533 | } |
Bram Moolenaar | 65b9a6a | 2009-02-04 12:14:51 +0000 | [diff] [blame] | 2534 | info_message = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2537 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2538 | rename_buffer(char_u *new_fname) |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2539 | { |
| 2540 | char_u *fname, *sfname, *xfname; |
Bram Moolenaar | 7e28384 | 2013-05-30 11:43:15 +0200 | [diff] [blame] | 2541 | buf_T *buf; |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2542 | |
Bram Moolenaar | 7e28384 | 2013-05-30 11:43:15 +0200 | [diff] [blame] | 2543 | #ifdef FEAT_AUTOCMD |
| 2544 | buf = curbuf; |
| 2545 | apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2546 | /* buffer changed, don't change name now */ |
| 2547 | if (buf != curbuf) |
| 2548 | return FAIL; |
| 2549 | # ifdef FEAT_EVAL |
| 2550 | if (aborting()) /* autocmds may abort script processing */ |
| 2551 | return FAIL; |
| 2552 | # endif |
| 2553 | #endif |
| 2554 | /* |
| 2555 | * The name of the current buffer will be changed. |
| 2556 | * A new (unlisted) buffer entry needs to be made to hold the old file |
| 2557 | * name, which will become the alternate file name. |
| 2558 | * But don't set the alternate file name if the buffer didn't have a |
| 2559 | * name. |
| 2560 | */ |
Bram Moolenaar | 7e28384 | 2013-05-30 11:43:15 +0200 | [diff] [blame] | 2561 | fname = curbuf->b_ffname; |
| 2562 | sfname = curbuf->b_sfname; |
| 2563 | xfname = curbuf->b_fname; |
| 2564 | curbuf->b_ffname = NULL; |
| 2565 | curbuf->b_sfname = NULL; |
| 2566 | if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL) |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2567 | { |
Bram Moolenaar | 7e28384 | 2013-05-30 11:43:15 +0200 | [diff] [blame] | 2568 | curbuf->b_ffname = fname; |
| 2569 | curbuf->b_sfname = sfname; |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2570 | return FAIL; |
| 2571 | } |
Bram Moolenaar | 7e28384 | 2013-05-30 11:43:15 +0200 | [diff] [blame] | 2572 | curbuf->b_flags |= BF_NOTEDITED; |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2573 | if (xfname != NULL && *xfname != NUL) |
| 2574 | { |
| 2575 | buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0); |
| 2576 | if (buf != NULL && !cmdmod.keepalt) |
| 2577 | curwin->w_alt_fnum = buf->b_fnum; |
| 2578 | } |
| 2579 | vim_free(fname); |
| 2580 | vim_free(sfname); |
| 2581 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 7e28384 | 2013-05-30 11:43:15 +0200 | [diff] [blame] | 2582 | apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2583 | #endif |
| 2584 | /* Change directories when the 'acd' option is set. */ |
| 2585 | DO_AUTOCHDIR |
| 2586 | return OK; |
| 2587 | } |
| 2588 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2589 | /* |
| 2590 | * ":file[!] [fname]". |
| 2591 | */ |
| 2592 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2593 | ex_file(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2594 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2595 | /* ":0file" removes the file name. Check for illegal uses ":3file", |
| 2596 | * "0file name", etc. */ |
| 2597 | if (eap->addr_count > 0 |
| 2598 | && (*eap->arg != NUL |
| 2599 | || eap->line2 > 0 |
| 2600 | || eap->addr_count > 1)) |
| 2601 | { |
| 2602 | EMSG(_(e_invarg)); |
| 2603 | return; |
| 2604 | } |
| 2605 | |
| 2606 | if (*eap->arg != NUL || eap->addr_count == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2607 | { |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2608 | if (rename_buffer(eap->arg) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2609 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2610 | } |
| 2611 | /* print full file name if :cd used */ |
Bram Moolenaar | 426dd02 | 2016-03-15 15:09:29 +0100 | [diff] [blame] | 2612 | if (!shortmess(SHM_FILEINFO)) |
| 2613 | fileinfo(FALSE, FALSE, eap->forceit); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | /* |
| 2617 | * ":update". |
| 2618 | */ |
| 2619 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2620 | ex_update(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2621 | { |
| 2622 | if (curbufIsChanged()) |
| 2623 | (void)do_write(eap); |
| 2624 | } |
| 2625 | |
| 2626 | /* |
| 2627 | * ":write" and ":saveas". |
| 2628 | */ |
| 2629 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2630 | ex_write(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2631 | { |
| 2632 | if (eap->usefilter) /* input lines to shell command */ |
| 2633 | do_bang(1, eap, FALSE, TRUE, FALSE); |
| 2634 | else |
| 2635 | (void)do_write(eap); |
| 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | * write current buffer to file 'eap->arg' |
| 2640 | * if 'eap->append' is TRUE, append to the file |
| 2641 | * |
| 2642 | * if *eap->arg == NUL write to current file |
| 2643 | * |
| 2644 | * return FAIL for failure, OK otherwise |
| 2645 | */ |
| 2646 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2647 | do_write(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2648 | { |
| 2649 | int other; |
| 2650 | char_u *fname = NULL; /* init to shut up gcc */ |
| 2651 | char_u *ffname; |
| 2652 | int retval = FAIL; |
| 2653 | char_u *free_fname = NULL; |
| 2654 | #ifdef FEAT_BROWSE |
| 2655 | char_u *browse_file = NULL; |
| 2656 | #endif |
| 2657 | buf_T *alt_buf = NULL; |
| 2658 | |
| 2659 | if (not_writing()) /* check 'write' option */ |
| 2660 | return FAIL; |
| 2661 | |
| 2662 | ffname = eap->arg; |
| 2663 | #ifdef FEAT_BROWSE |
| 2664 | if (cmdmod.browse) |
| 2665 | { |
Bram Moolenaar | 7b0294c | 2004-10-11 10:16:09 +0000 | [diff] [blame] | 2666 | browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2667 | NULL, NULL, NULL, curbuf); |
| 2668 | if (browse_file == NULL) |
| 2669 | goto theend; |
| 2670 | ffname = browse_file; |
| 2671 | } |
| 2672 | #endif |
| 2673 | if (*ffname == NUL) |
| 2674 | { |
| 2675 | if (eap->cmdidx == CMD_saveas) |
| 2676 | { |
| 2677 | EMSG(_(e_argreq)); |
| 2678 | goto theend; |
| 2679 | } |
| 2680 | other = FALSE; |
| 2681 | } |
| 2682 | else |
| 2683 | { |
| 2684 | fname = ffname; |
| 2685 | free_fname = fix_fname(ffname); |
| 2686 | /* |
| 2687 | * When out-of-memory, keep unexpanded file name, because we MUST be |
| 2688 | * able to write the file in this situation. |
| 2689 | */ |
| 2690 | if (free_fname != NULL) |
| 2691 | ffname = free_fname; |
| 2692 | other = otherfile(ffname); |
| 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | * If we have a new file, put its name in the list of alternate file names. |
| 2697 | */ |
| 2698 | if (other) |
| 2699 | { |
| 2700 | if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL |
| 2701 | || eap->cmdidx == CMD_saveas) |
| 2702 | alt_buf = setaltfname(ffname, fname, (linenr_T)1); |
| 2703 | else |
| 2704 | alt_buf = buflist_findname(ffname); |
| 2705 | if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL) |
| 2706 | { |
| 2707 | /* Overwriting a file that is loaded in another buffer is not a |
| 2708 | * good idea. */ |
Bram Moolenaar | 0e4d877 | 2005-06-07 21:12:49 +0000 | [diff] [blame] | 2709 | EMSG(_(e_bufloaded)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2710 | goto theend; |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | /* |
| 2715 | * Writing to the current file is not allowed in readonly mode |
| 2716 | * and a file name is required. |
| 2717 | * "nofile" and "nowrite" buffers cannot be written implicitly either. |
| 2718 | */ |
| 2719 | if (!other && ( |
| 2720 | #ifdef FEAT_QUICKFIX |
| 2721 | bt_dontwrite_msg(curbuf) || |
| 2722 | #endif |
| 2723 | check_fname() == FAIL || check_readonly(&eap->forceit, curbuf))) |
| 2724 | goto theend; |
| 2725 | |
| 2726 | if (!other) |
| 2727 | { |
| 2728 | ffname = curbuf->b_ffname; |
| 2729 | fname = curbuf->b_fname; |
| 2730 | /* |
| 2731 | * Not writing the whole file is only allowed with '!'. |
| 2732 | */ |
| 2733 | if ( (eap->line1 != 1 |
| 2734 | || eap->line2 != curbuf->b_ml.ml_line_count) |
| 2735 | && !eap->forceit |
| 2736 | && !eap->append |
| 2737 | && !p_wa) |
| 2738 | { |
| 2739 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2740 | if (p_confirm || cmdmod.confirm) |
| 2741 | { |
| 2742 | if (vim_dialog_yesno(VIM_QUESTION, NULL, |
| 2743 | (char_u *)_("Write partial file?"), 2) != VIM_YES) |
| 2744 | goto theend; |
| 2745 | eap->forceit = TRUE; |
| 2746 | } |
| 2747 | else |
| 2748 | #endif |
| 2749 | { |
| 2750 | EMSG(_("E140: Use ! to write partial buffer")); |
| 2751 | goto theend; |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | if (check_overwrite(eap, curbuf, fname, ffname, other) == OK) |
| 2757 | { |
| 2758 | if (eap->cmdidx == CMD_saveas && alt_buf != NULL) |
| 2759 | { |
| 2760 | #ifdef FEAT_AUTOCMD |
| 2761 | buf_T *was_curbuf = curbuf; |
| 2762 | |
| 2763 | apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 2764 | apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2765 | # ifdef FEAT_EVAL |
| 2766 | if (curbuf != was_curbuf || aborting()) |
| 2767 | # else |
| 2768 | if (curbuf != was_curbuf) |
| 2769 | # endif |
| 2770 | { |
| 2771 | /* buffer changed, don't change name now */ |
| 2772 | retval = FAIL; |
| 2773 | goto theend; |
| 2774 | } |
| 2775 | #endif |
| 2776 | /* Exchange the file names for the current and the alternate |
| 2777 | * buffer. This makes it look like we are now editing the buffer |
| 2778 | * under the new name. Must be done before buf_write(), because |
| 2779 | * if there is no file name and 'cpo' contains 'F', it will set |
| 2780 | * the file name. */ |
| 2781 | fname = alt_buf->b_fname; |
| 2782 | alt_buf->b_fname = curbuf->b_fname; |
| 2783 | curbuf->b_fname = fname; |
| 2784 | fname = alt_buf->b_ffname; |
| 2785 | alt_buf->b_ffname = curbuf->b_ffname; |
| 2786 | curbuf->b_ffname = fname; |
| 2787 | fname = alt_buf->b_sfname; |
| 2788 | alt_buf->b_sfname = curbuf->b_sfname; |
| 2789 | curbuf->b_sfname = fname; |
| 2790 | buf_name_changed(curbuf); |
| 2791 | #ifdef FEAT_AUTOCMD |
| 2792 | apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 2793 | apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2794 | if (!alt_buf->b_p_bl) |
| 2795 | { |
| 2796 | alt_buf->b_p_bl = TRUE; |
| 2797 | apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf); |
| 2798 | } |
| 2799 | # ifdef FEAT_EVAL |
| 2800 | if (curbuf != was_curbuf || aborting()) |
| 2801 | # else |
| 2802 | if (curbuf != was_curbuf) |
| 2803 | # endif |
| 2804 | { |
| 2805 | /* buffer changed, don't write the file */ |
| 2806 | retval = FAIL; |
| 2807 | goto theend; |
| 2808 | } |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2809 | |
| 2810 | /* If 'filetype' was empty try detecting it now. */ |
| 2811 | if (*curbuf->b_p_ft == NUL) |
| 2812 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2813 | if (au_has_group((char_u *)"filetypedetect")) |
| 2814 | (void)do_doautocmd((char_u *)"filetypedetect BufRead", |
| 2815 | TRUE); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 2816 | do_modelines(0); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2817 | } |
Bram Moolenaar | f666f0e | 2010-11-24 17:59:32 +0100 | [diff] [blame] | 2818 | |
| 2819 | /* Autocommands may have changed buffer names, esp. when |
| 2820 | * 'autochdir' is set. */ |
| 2821 | fname = curbuf->b_sfname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2822 | #endif |
| 2823 | } |
| 2824 | |
| 2825 | retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2, |
| 2826 | eap, eap->append, eap->forceit, TRUE, FALSE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2827 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2828 | /* After ":saveas fname" reset 'readonly'. */ |
Bram Moolenaar | 498efdb | 2006-09-05 14:31:54 +0000 | [diff] [blame] | 2829 | if (eap->cmdidx == CMD_saveas) |
| 2830 | { |
| 2831 | if (retval == OK) |
Bram Moolenaar | 4352f13 | 2009-02-11 15:03:45 +0000 | [diff] [blame] | 2832 | { |
Bram Moolenaar | 498efdb | 2006-09-05 14:31:54 +0000 | [diff] [blame] | 2833 | curbuf->b_p_ro = FALSE; |
Bram Moolenaar | 4352f13 | 2009-02-11 15:03:45 +0000 | [diff] [blame] | 2834 | #ifdef FEAT_WINDOWS |
| 2835 | redraw_tabline = TRUE; |
| 2836 | #endif |
| 2837 | } |
Bram Moolenaar | 498efdb | 2006-09-05 14:31:54 +0000 | [diff] [blame] | 2838 | /* Change directories when the 'acd' option is set. */ |
| 2839 | DO_AUTOCHDIR |
| 2840 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | theend: |
| 2844 | #ifdef FEAT_BROWSE |
| 2845 | vim_free(browse_file); |
| 2846 | #endif |
| 2847 | vim_free(free_fname); |
| 2848 | return retval; |
| 2849 | } |
| 2850 | |
| 2851 | /* |
| 2852 | * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED, |
| 2853 | * BF_NEW or BF_READERR, check for overwriting current file. |
| 2854 | * May set eap->forceit if a dialog says it's OK to overwrite. |
| 2855 | * Return OK if it's OK, FAIL if it is not. |
| 2856 | */ |
Bram Moolenaar | 8218f60 | 2012-04-25 17:32:18 +0200 | [diff] [blame] | 2857 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2858 | check_overwrite( |
| 2859 | exarg_T *eap, |
| 2860 | buf_T *buf, |
| 2861 | char_u *fname, /* file name to be used (can differ from |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2862 | buf->ffname) */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2863 | char_u *ffname, /* full path version of fname */ |
| 2864 | int other) /* writing under other name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2865 | { |
| 2866 | /* |
| 2867 | * write to other file or b_flags set or not writing the whole file: |
| 2868 | * overwriting only allowed with '!' |
| 2869 | */ |
| 2870 | if ( (other |
| 2871 | || (buf->b_flags & BF_NOTEDITED) |
| 2872 | || ((buf->b_flags & BF_NEW) |
| 2873 | && vim_strchr(p_cpo, CPO_OVERNEW) == NULL) |
| 2874 | || (buf->b_flags & BF_READERR)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2875 | && !p_wa |
Bram Moolenaar | 11717bb | 2007-12-08 21:21:18 +0000 | [diff] [blame] | 2876 | #ifdef FEAT_QUICKFIX |
| 2877 | && !bt_nofile(buf) |
| 2878 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2879 | && vim_fexists(ffname)) |
| 2880 | { |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2881 | if (!eap->forceit && !eap->append) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2882 | { |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2883 | #ifdef UNIX |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 2884 | /* with UNIX it is possible to open a directory */ |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2885 | if (mch_isdir(ffname)) |
| 2886 | { |
| 2887 | EMSG2(_(e_isadir2), ffname); |
| 2888 | return FAIL; |
| 2889 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2890 | #endif |
| 2891 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2892 | if (p_confirm || cmdmod.confirm) |
| 2893 | { |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 2894 | char_u buff[DIALOG_MSG_SIZE]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2895 | |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2896 | dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname); |
| 2897 | if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) |
| 2898 | return FAIL; |
| 2899 | eap->forceit = TRUE; |
| 2900 | } |
| 2901 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2902 | #endif |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2903 | { |
| 2904 | EMSG(_(e_exists)); |
| 2905 | return FAIL; |
| 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | /* For ":w! filename" check that no swap file exists for "filename". */ |
| 2910 | if (other && !emsg_silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2911 | { |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 2912 | char_u *dir; |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2913 | char_u *p; |
| 2914 | int r; |
| 2915 | char_u *swapname; |
| 2916 | |
| 2917 | /* We only try the first entry in 'directory', without checking if |
| 2918 | * it's writable. If the "." directory is not writable the write |
| 2919 | * will probably fail anyway. |
| 2920 | * Use 'shortname' of the current buffer, since there is no buffer |
| 2921 | * for the written file. */ |
| 2922 | if (*p_dir == NUL) |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 2923 | { |
| 2924 | dir = alloc(5); |
| 2925 | if (dir == NULL) |
| 2926 | return FAIL; |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2927 | STRCPY(dir, "."); |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 2928 | } |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2929 | else |
| 2930 | { |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 2931 | dir = alloc(MAXPATHL); |
| 2932 | if (dir == NULL) |
| 2933 | return FAIL; |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2934 | p = p_dir; |
| 2935 | copy_option_part(&p, dir, MAXPATHL, ","); |
| 2936 | } |
| 2937 | swapname = makeswapname(fname, ffname, curbuf, dir); |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 2938 | vim_free(dir); |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2939 | r = vim_fexists(swapname); |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2940 | if (r) |
| 2941 | { |
| 2942 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2943 | if (p_confirm || cmdmod.confirm) |
| 2944 | { |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 2945 | char_u buff[DIALOG_MSG_SIZE]; |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2946 | |
| 2947 | dialog_msg(buff, |
| 2948 | _("Swap file \"%s\" exists, overwrite anyway?"), |
| 2949 | swapname); |
| 2950 | if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) |
| 2951 | != VIM_YES) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2952 | { |
| 2953 | vim_free(swapname); |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2954 | return FAIL; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2955 | } |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2956 | eap->forceit = TRUE; |
| 2957 | } |
| 2958 | else |
| 2959 | #endif |
| 2960 | { |
| 2961 | EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"), |
| 2962 | swapname); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2963 | vim_free(swapname); |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2964 | return FAIL; |
| 2965 | } |
| 2966 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2967 | vim_free(swapname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2968 | } |
| 2969 | } |
| 2970 | return OK; |
| 2971 | } |
| 2972 | |
| 2973 | /* |
| 2974 | * Handle ":wnext", ":wNext" and ":wprevious" commands. |
| 2975 | */ |
| 2976 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2977 | ex_wnext(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2978 | { |
| 2979 | int i; |
| 2980 | |
| 2981 | if (eap->cmd[1] == 'n') |
| 2982 | i = curwin->w_arg_idx + (int)eap->line2; |
| 2983 | else |
| 2984 | i = curwin->w_arg_idx - (int)eap->line2; |
| 2985 | eap->line1 = 1; |
| 2986 | eap->line2 = curbuf->b_ml.ml_line_count; |
| 2987 | if (do_write(eap) != FAIL) |
| 2988 | do_argfile(eap, i); |
| 2989 | } |
| 2990 | |
| 2991 | /* |
| 2992 | * ":wall", ":wqall" and ":xall": Write all changed files (and exit). |
| 2993 | */ |
| 2994 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2995 | do_wqall(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2996 | { |
| 2997 | buf_T *buf; |
| 2998 | int error = 0; |
| 2999 | int save_forceit = eap->forceit; |
| 3000 | |
| 3001 | if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall) |
| 3002 | exiting = TRUE; |
| 3003 | |
| 3004 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 3005 | { |
| 3006 | if (bufIsChanged(buf)) |
| 3007 | { |
| 3008 | /* |
| 3009 | * Check if there is a reason the buffer cannot be written: |
| 3010 | * 1. if the 'write' option is set |
| 3011 | * 2. if there is no file name (even after browsing) |
| 3012 | * 3. if the 'readonly' is set (even after a dialog) |
| 3013 | * 4. if overwriting is allowed (even after a dialog) |
| 3014 | */ |
| 3015 | if (not_writing()) |
| 3016 | { |
| 3017 | ++error; |
| 3018 | break; |
| 3019 | } |
| 3020 | #ifdef FEAT_BROWSE |
| 3021 | /* ":browse wall": ask for file name if there isn't one */ |
| 3022 | if (buf->b_ffname == NULL && cmdmod.browse) |
| 3023 | browse_save_fname(buf); |
| 3024 | #endif |
| 3025 | if (buf->b_ffname == NULL) |
| 3026 | { |
| 3027 | EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum); |
| 3028 | ++error; |
| 3029 | } |
| 3030 | else if (check_readonly(&eap->forceit, buf) |
| 3031 | || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname, |
| 3032 | FALSE) == FAIL) |
| 3033 | { |
| 3034 | ++error; |
| 3035 | } |
| 3036 | else |
| 3037 | { |
| 3038 | if (buf_write_all(buf, eap->forceit) == FAIL) |
| 3039 | ++error; |
| 3040 | #ifdef FEAT_AUTOCMD |
| 3041 | /* an autocommand may have deleted the buffer */ |
| 3042 | if (!buf_valid(buf)) |
| 3043 | buf = firstbuf; |
| 3044 | #endif |
| 3045 | } |
| 3046 | eap->forceit = save_forceit; /* check_overwrite() may set it */ |
| 3047 | } |
| 3048 | } |
| 3049 | if (exiting) |
| 3050 | { |
| 3051 | if (!error) |
| 3052 | getout(0); /* exit Vim */ |
| 3053 | not_exiting(); |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | /* |
| 3058 | * Check the 'write' option. |
| 3059 | * Return TRUE and give a message when it's not st. |
| 3060 | */ |
| 3061 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3062 | not_writing(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3063 | { |
| 3064 | if (p_write) |
| 3065 | return FALSE; |
| 3066 | EMSG(_("E142: File not written: Writing is disabled by 'write' option")); |
| 3067 | return TRUE; |
| 3068 | } |
| 3069 | |
| 3070 | /* |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3071 | * Check if a buffer is read-only (either 'readonly' option is set or file is |
| 3072 | * read-only). Ask for overruling in a dialog. Return TRUE and give an error |
| 3073 | * message when the buffer is readonly. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3074 | */ |
| 3075 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3076 | check_readonly(int *forceit, buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3077 | { |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3078 | struct stat st; |
| 3079 | |
| 3080 | /* Handle a file being readonly when the 'readonly' option is set or when |
| 3081 | * the file exists and permissions are read-only. |
| 3082 | * We will send 0777 to check_file_readonly(), as the "perm" variable is |
| 3083 | * important for device checks but not here. */ |
| 3084 | if (!*forceit && (buf->b_p_ro |
| 3085 | || (mch_stat((char *)buf->b_ffname, &st) >= 0 |
| 3086 | && check_file_readonly(buf->b_ffname, 0777)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3087 | { |
| 3088 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 3089 | if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) |
| 3090 | { |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 3091 | char_u buff[DIALOG_MSG_SIZE]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3092 | |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3093 | if (buf->b_p_ro) |
| 3094 | dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"), |
| 3095 | buf->b_fname); |
| 3096 | else |
| 3097 | dialog_msg(buff, _("File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3098 | buf->b_fname); |
| 3099 | |
| 3100 | if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES) |
| 3101 | { |
| 3102 | /* Set forceit, to force the writing of a readonly file */ |
| 3103 | *forceit = TRUE; |
| 3104 | return FALSE; |
| 3105 | } |
| 3106 | else |
| 3107 | return TRUE; |
| 3108 | } |
| 3109 | else |
| 3110 | #endif |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3111 | if (buf->b_p_ro) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3112 | EMSG(_(e_readonly)); |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3113 | else |
| 3114 | EMSG2(_("E505: \"%s\" is read-only (add ! to override)"), |
| 3115 | buf->b_fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3116 | return TRUE; |
| 3117 | } |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3118 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3119 | return FALSE; |
| 3120 | } |
| 3121 | |
| 3122 | /* |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3123 | * Try to abandon current file and edit a new or existing file. |
| 3124 | * 'fnum' is the number of the file, if zero use ffname/sfname. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3125 | * |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3126 | * Return 1 for "normal" error, 2 for "not written" error, 0 for success |
Bram Moolenaar | eb1b679 | 2007-08-21 13:29:28 +0000 | [diff] [blame] | 3127 | * -1 for successfully opening another file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3128 | * 'lnum' is the line number for the cursor in the new file (if non-zero). |
| 3129 | */ |
| 3130 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3131 | getfile( |
| 3132 | int fnum, |
| 3133 | char_u *ffname, |
| 3134 | char_u *sfname, |
| 3135 | int setpm, |
| 3136 | linenr_T lnum, |
| 3137 | int forceit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3138 | { |
| 3139 | int other; |
| 3140 | int retval; |
| 3141 | char_u *free_me = NULL; |
| 3142 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3143 | if (text_locked()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3144 | return 1; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3145 | #ifdef FEAT_AUTOCMD |
| 3146 | if (curbuf_locked()) |
| 3147 | return 1; |
| 3148 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3149 | |
| 3150 | if (fnum == 0) |
| 3151 | { |
| 3152 | /* make ffname full path, set sfname */ |
| 3153 | fname_expand(curbuf, &ffname, &sfname); |
| 3154 | other = otherfile(ffname); |
| 3155 | free_me = ffname; /* has been allocated, free() later */ |
| 3156 | } |
| 3157 | else |
| 3158 | other = (fnum != curbuf->b_fnum); |
| 3159 | |
| 3160 | if (other) |
| 3161 | ++no_wait_return; /* don't wait for autowrite message */ |
| 3162 | if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf) |
| 3163 | && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL) |
| 3164 | { |
| 3165 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 3166 | if (p_confirm && p_write) |
| 3167 | dialog_changed(curbuf, FALSE); |
| 3168 | if (curbufIsChanged()) |
| 3169 | #endif |
| 3170 | { |
| 3171 | if (other) |
| 3172 | --no_wait_return; |
| 3173 | EMSG(_(e_nowrtmsg)); |
| 3174 | retval = 2; /* file has been changed */ |
| 3175 | goto theend; |
| 3176 | } |
| 3177 | } |
| 3178 | if (other) |
| 3179 | --no_wait_return; |
| 3180 | if (setpm) |
| 3181 | setpcmark(); |
| 3182 | if (!other) |
| 3183 | { |
| 3184 | if (lnum != 0) |
| 3185 | curwin->w_cursor.lnum = lnum; |
| 3186 | check_cursor_lnum(); |
| 3187 | beginline(BL_SOL | BL_FIX); |
| 3188 | retval = 0; /* it's in the same file */ |
| 3189 | } |
| 3190 | else if (do_ecmd(fnum, ffname, sfname, NULL, lnum, |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 3191 | (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0), |
| 3192 | curwin) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3193 | retval = -1; /* opened another file */ |
| 3194 | else |
| 3195 | retval = 1; /* error encountered */ |
| 3196 | |
| 3197 | theend: |
| 3198 | vim_free(free_me); |
| 3199 | return retval; |
| 3200 | } |
| 3201 | |
| 3202 | /* |
| 3203 | * start editing a new file |
| 3204 | * |
| 3205 | * fnum: file number; if zero use ffname/sfname |
| 3206 | * ffname: the file name |
| 3207 | * - full path if sfname used, |
| 3208 | * - any file name if sfname is NULL |
| 3209 | * - empty string to re-edit with the same file name (but may be |
| 3210 | * in a different directory) |
| 3211 | * - NULL to start an empty buffer |
| 3212 | * sfname: the short file name (or NULL) |
| 3213 | * eap: contains the command to be executed after loading the file and |
| 3214 | * forced 'ff' and 'fenc' |
| 3215 | * newlnum: if > 0: put cursor on this line number (if possible) |
| 3216 | * if ECMD_LASTL: use last position in loaded file |
| 3217 | * if ECMD_LAST: use last position in all files |
| 3218 | * if ECMD_ONE: use first line |
| 3219 | * flags: |
| 3220 | * ECMD_HIDE: if TRUE don't free the current buffer |
| 3221 | * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file |
| 3222 | * ECMD_OLDBUF: use existing buffer if it exists |
| 3223 | * ECMD_FORCEIT: ! used for Ex command |
| 3224 | * ECMD_ADDBUF: don't edit, just add to buffer list |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 3225 | * oldwin: Should be "curwin" when editing a new buffer in the current |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 3226 | * window, NULL when splitting the window first. When not NULL info |
| 3227 | * of the previous buffer for "oldwin" is stored. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3228 | * |
| 3229 | * return FAIL for failure, OK otherwise |
| 3230 | */ |
| 3231 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3232 | do_ecmd( |
| 3233 | int fnum, |
| 3234 | char_u *ffname, |
| 3235 | char_u *sfname, |
| 3236 | exarg_T *eap, /* can be NULL! */ |
| 3237 | linenr_T newlnum, |
| 3238 | int flags, |
| 3239 | win_T *oldwin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3240 | { |
| 3241 | int other_file; /* TRUE if editing another file */ |
| 3242 | int oldbuf; /* TRUE if using existing buffer */ |
| 3243 | #ifdef FEAT_AUTOCMD |
| 3244 | int auto_buf = FALSE; /* TRUE if autocommands brought us |
| 3245 | into the buffer unexpectedly */ |
| 3246 | char_u *new_name = NULL; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 3247 | int did_set_swapcommand = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3248 | #endif |
| 3249 | buf_T *buf; |
| 3250 | #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 3251 | buf_T *old_curbuf = curbuf; |
| 3252 | #endif |
| 3253 | char_u *free_fname = NULL; |
| 3254 | #ifdef FEAT_BROWSE |
| 3255 | char_u *browse_file = NULL; |
| 3256 | #endif |
| 3257 | int retval = FAIL; |
| 3258 | long n; |
Bram Moolenaar | eab316b | 2015-03-24 11:46:30 +0100 | [diff] [blame] | 3259 | pos_T orig_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3260 | linenr_T topline = 0; |
| 3261 | int newcol = -1; |
| 3262 | int solcol = -1; |
| 3263 | pos_T *pos; |
| 3264 | #ifdef FEAT_SUN_WORKSHOP |
| 3265 | char_u *cp; |
| 3266 | #endif |
| 3267 | char_u *command = NULL; |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 3268 | #ifdef FEAT_SPELL |
| 3269 | int did_get_winopts = FALSE; |
| 3270 | #endif |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 3271 | int readfile_flags = 0; |
Bram Moolenaar | ab9fc7e | 2016-02-06 15:29:40 +0100 | [diff] [blame] | 3272 | int did_inc_redrawing_disabled = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3273 | |
| 3274 | if (eap != NULL) |
| 3275 | command = eap->do_ecmd_cmd; |
| 3276 | |
| 3277 | if (fnum != 0) |
| 3278 | { |
| 3279 | if (fnum == curbuf->b_fnum) /* file is already being edited */ |
| 3280 | return OK; /* nothing to do */ |
| 3281 | other_file = TRUE; |
| 3282 | } |
| 3283 | else |
| 3284 | { |
| 3285 | #ifdef FEAT_BROWSE |
| 3286 | if (cmdmod.browse) |
| 3287 | { |
Bram Moolenaar | 15bfa09 | 2008-07-24 16:45:38 +0000 | [diff] [blame] | 3288 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 0be6e64 | 2005-08-04 21:32:22 +0000 | [diff] [blame] | 3289 | if ( |
Bram Moolenaar | 15bfa09 | 2008-07-24 16:45:38 +0000 | [diff] [blame] | 3290 | # ifdef FEAT_GUI |
Bram Moolenaar | 0be6e64 | 2005-08-04 21:32:22 +0000 | [diff] [blame] | 3291 | !gui.in_use && |
Bram Moolenaar | 15bfa09 | 2008-07-24 16:45:38 +0000 | [diff] [blame] | 3292 | # endif |
Bram Moolenaar | 0be6e64 | 2005-08-04 21:32:22 +0000 | [diff] [blame] | 3293 | au_has_group((char_u *)"FileExplorer")) |
| 3294 | { |
| 3295 | /* No browsing supported but we do have the file explorer: |
| 3296 | * Edit the directory. */ |
| 3297 | if (ffname == NULL || !mch_isdir(ffname)) |
| 3298 | ffname = (char_u *)"."; |
| 3299 | } |
| 3300 | else |
Bram Moolenaar | 15bfa09 | 2008-07-24 16:45:38 +0000 | [diff] [blame] | 3301 | # endif |
Bram Moolenaar | 0be6e64 | 2005-08-04 21:32:22 +0000 | [diff] [blame] | 3302 | { |
| 3303 | browse_file = do_browse(0, (char_u *)_("Edit File"), ffname, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3304 | NULL, NULL, NULL, curbuf); |
Bram Moolenaar | 0be6e64 | 2005-08-04 21:32:22 +0000 | [diff] [blame] | 3305 | if (browse_file == NULL) |
| 3306 | goto theend; |
| 3307 | ffname = browse_file; |
| 3308 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3309 | } |
| 3310 | #endif |
| 3311 | /* if no short name given, use ffname for short name */ |
| 3312 | if (sfname == NULL) |
| 3313 | sfname = ffname; |
| 3314 | #ifdef USE_FNAME_CASE |
| 3315 | # ifdef USE_LONG_FNAME |
| 3316 | if (USE_LONG_FNAME) |
| 3317 | # endif |
Bram Moolenaar | 342337a | 2005-07-21 21:11:17 +0000 | [diff] [blame] | 3318 | if (sfname != NULL) |
| 3319 | fname_case(sfname, 0); /* set correct case for sfname */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3320 | #endif |
| 3321 | |
| 3322 | #ifdef FEAT_LISTCMDS |
| 3323 | if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL)) |
| 3324 | goto theend; |
| 3325 | #endif |
| 3326 | |
| 3327 | if (ffname == NULL) |
| 3328 | other_file = TRUE; |
| 3329 | /* there is no file name */ |
| 3330 | else if (*ffname == NUL && curbuf->b_ffname == NULL) |
| 3331 | other_file = FALSE; |
| 3332 | else |
| 3333 | { |
| 3334 | if (*ffname == NUL) /* re-edit with same file name */ |
| 3335 | { |
| 3336 | ffname = curbuf->b_ffname; |
| 3337 | sfname = curbuf->b_fname; |
| 3338 | } |
| 3339 | free_fname = fix_fname(ffname); /* may expand to full path name */ |
| 3340 | if (free_fname != NULL) |
| 3341 | ffname = free_fname; |
| 3342 | other_file = otherfile(ffname); |
| 3343 | #ifdef FEAT_SUN_WORKSHOP |
| 3344 | if (usingSunWorkShop && p_acd |
| 3345 | && (cp = vim_strrchr(sfname, '/')) != NULL) |
| 3346 | sfname = ++cp; |
| 3347 | #endif |
| 3348 | } |
| 3349 | } |
| 3350 | |
| 3351 | /* |
| 3352 | * if the file was changed we may not be allowed to abandon it |
| 3353 | * - if we are going to re-edit the same file |
| 3354 | * - or if we are the only window on this file and if ECMD_HIDE is FALSE |
| 3355 | */ |
| 3356 | if ( ((!other_file && !(flags & ECMD_OLDBUF)) |
| 3357 | || (curbuf->b_nwindows == 1 |
| 3358 | && !(flags & (ECMD_HIDE | ECMD_ADDBUF)))) |
Bram Moolenaar | 45d3b14 | 2013-11-09 03:31:51 +0100 | [diff] [blame] | 3359 | && check_changed(curbuf, (p_awa ? CCGD_AW : 0) |
| 3360 | | (other_file ? 0 : CCGD_MULTWIN) |
| 3361 | | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0) |
| 3362 | | (eap == NULL ? 0 : CCGD_EXCMD))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3363 | { |
| 3364 | if (fnum == 0 && other_file && ffname != NULL) |
| 3365 | (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum); |
| 3366 | goto theend; |
| 3367 | } |
| 3368 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3369 | /* |
| 3370 | * End Visual mode before switching to another buffer, so the text can be |
| 3371 | * copied into the GUI selection buffer. |
| 3372 | */ |
| 3373 | reset_VIsual(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3374 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 3375 | #ifdef FEAT_AUTOCMD |
| 3376 | if ((command != NULL || newlnum > (linenr_T)0) |
| 3377 | && *get_vim_var_str(VV_SWAPCOMMAND) == NUL) |
| 3378 | { |
| 3379 | int len; |
| 3380 | char_u *p; |
| 3381 | |
| 3382 | /* Set v:swapcommand for the SwapExists autocommands. */ |
| 3383 | if (command != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3384 | len = (int)STRLEN(command) + 3; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 3385 | else |
| 3386 | len = 30; |
| 3387 | p = alloc((unsigned)len); |
| 3388 | if (p != NULL) |
| 3389 | { |
| 3390 | if (command != NULL) |
| 3391 | vim_snprintf((char *)p, len, ":%s\r", command); |
| 3392 | else |
| 3393 | vim_snprintf((char *)p, len, "%ldG", (long)newlnum); |
| 3394 | set_vim_var_string(VV_SWAPCOMMAND, p, -1); |
| 3395 | did_set_swapcommand = TRUE; |
| 3396 | vim_free(p); |
| 3397 | } |
| 3398 | } |
| 3399 | #endif |
| 3400 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3401 | /* |
| 3402 | * If we are starting to edit another file, open a (new) buffer. |
| 3403 | * Otherwise we re-use the current buffer. |
| 3404 | */ |
| 3405 | if (other_file) |
| 3406 | { |
| 3407 | #ifdef FEAT_LISTCMDS |
| 3408 | if (!(flags & ECMD_ADDBUF)) |
| 3409 | #endif |
| 3410 | { |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3411 | if (!cmdmod.keepalt) |
| 3412 | curwin->w_alt_fnum = curbuf->b_fnum; |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 3413 | if (oldwin != NULL) |
| 3414 | buflist_altfpos(oldwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | if (fnum) |
| 3418 | buf = buflist_findnr(fnum); |
| 3419 | else |
| 3420 | { |
| 3421 | #ifdef FEAT_LISTCMDS |
| 3422 | if (flags & ECMD_ADDBUF) |
| 3423 | { |
| 3424 | linenr_T tlnum = 1L; |
| 3425 | |
| 3426 | if (command != NULL) |
| 3427 | { |
| 3428 | tlnum = atol((char *)command); |
| 3429 | if (tlnum <= 0) |
| 3430 | tlnum = 1L; |
| 3431 | } |
| 3432 | (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED); |
| 3433 | goto theend; |
| 3434 | } |
| 3435 | #endif |
| 3436 | buf = buflist_new(ffname, sfname, 0L, |
| 3437 | BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED)); |
Bram Moolenaar | 4c7ab1b | 2014-04-06 20:45:43 +0200 | [diff] [blame] | 3438 | #ifdef FEAT_AUTOCMD |
| 3439 | /* autocommands may change curwin and curbuf */ |
| 3440 | if (oldwin != NULL) |
| 3441 | oldwin = curwin; |
| 3442 | old_curbuf = curbuf; |
| 3443 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3444 | } |
| 3445 | if (buf == NULL) |
| 3446 | goto theend; |
| 3447 | if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */ |
| 3448 | { |
| 3449 | oldbuf = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3450 | } |
| 3451 | else /* existing memfile */ |
| 3452 | { |
| 3453 | oldbuf = TRUE; |
| 3454 | (void)buf_check_timestamp(buf, FALSE); |
| 3455 | /* Check if autocommands made buffer invalid or changed the current |
| 3456 | * buffer. */ |
| 3457 | if (!buf_valid(buf) |
| 3458 | #ifdef FEAT_AUTOCMD |
| 3459 | || curbuf != old_curbuf |
| 3460 | #endif |
| 3461 | ) |
| 3462 | goto theend; |
| 3463 | #ifdef FEAT_EVAL |
| 3464 | if (aborting()) /* autocmds may abort script processing */ |
| 3465 | goto theend; |
| 3466 | #endif |
| 3467 | } |
| 3468 | |
| 3469 | /* May jump to last used line number for a loaded buffer or when asked |
| 3470 | * for explicitly */ |
| 3471 | if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST) |
| 3472 | { |
| 3473 | pos = buflist_findfpos(buf); |
| 3474 | newlnum = pos->lnum; |
| 3475 | solcol = pos->col; |
| 3476 | } |
| 3477 | |
| 3478 | /* |
| 3479 | * Make the (new) buffer the one used by the current window. |
| 3480 | * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE. |
| 3481 | * If the current buffer was empty and has no file name, curbuf |
Bram Moolenaar | 8da9bbf | 2015-02-27 19:34:56 +0100 | [diff] [blame] | 3482 | * is returned by buflist_new(), nothing to do here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3483 | */ |
| 3484 | if (buf != curbuf) |
| 3485 | { |
| 3486 | #ifdef FEAT_AUTOCMD |
| 3487 | /* |
| 3488 | * Be careful: The autocommands may delete any buffer and change |
| 3489 | * the current buffer. |
| 3490 | * - If the buffer we are going to edit is deleted, give up. |
| 3491 | * - If the current buffer is deleted, prefer to load the new |
| 3492 | * buffer when loading a buffer is required. This avoids |
| 3493 | * loading another buffer which then must be closed again. |
| 3494 | * - If we ended up in the new buffer already, need to skip a few |
| 3495 | * things, set auto_buf. |
| 3496 | */ |
| 3497 | if (buf->b_fname != NULL) |
| 3498 | new_name = vim_strsave(buf->b_fname); |
| 3499 | au_new_curbuf = buf; |
| 3500 | apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); |
| 3501 | if (!buf_valid(buf)) /* new buffer has been deleted */ |
| 3502 | { |
| 3503 | delbuf_msg(new_name); /* frees new_name */ |
| 3504 | goto theend; |
| 3505 | } |
| 3506 | # ifdef FEAT_EVAL |
| 3507 | if (aborting()) /* autocmds may abort script processing */ |
| 3508 | { |
| 3509 | vim_free(new_name); |
| 3510 | goto theend; |
| 3511 | } |
| 3512 | # endif |
| 3513 | if (buf == curbuf) /* already in new buffer */ |
| 3514 | auto_buf = TRUE; |
| 3515 | else |
| 3516 | { |
| 3517 | if (curbuf == old_curbuf) |
| 3518 | #endif |
| 3519 | buf_copy_options(buf, BCO_ENTER); |
| 3520 | |
| 3521 | /* close the link to the current buffer */ |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3522 | u_sync(FALSE); |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 3523 | close_buffer(oldwin, curbuf, |
Bram Moolenaar | 42ec656 | 2012-02-22 14:58:37 +0100 | [diff] [blame] | 3524 | (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3525 | |
| 3526 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | fc57380 | 2011-12-30 15:01:59 +0100 | [diff] [blame] | 3527 | /* Autocommands may open a new window and leave oldwin open |
| 3528 | * which leads to crashes since the above call sets |
| 3529 | * oldwin->w_buffer to NULL. */ |
| 3530 | if (curwin != oldwin && oldwin != aucmd_win |
| 3531 | && win_valid(oldwin) && oldwin->w_buffer == NULL) |
| 3532 | win_close(oldwin, FALSE); |
| 3533 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3534 | # ifdef FEAT_EVAL |
| 3535 | if (aborting()) /* autocmds may abort script processing */ |
| 3536 | { |
| 3537 | vim_free(new_name); |
| 3538 | goto theend; |
| 3539 | } |
| 3540 | # endif |
| 3541 | /* Be careful again, like above. */ |
| 3542 | if (!buf_valid(buf)) /* new buffer has been deleted */ |
| 3543 | { |
| 3544 | delbuf_msg(new_name); /* frees new_name */ |
| 3545 | goto theend; |
| 3546 | } |
| 3547 | if (buf == curbuf) /* already in new buffer */ |
| 3548 | auto_buf = TRUE; |
| 3549 | else |
| 3550 | #endif |
| 3551 | { |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 3552 | #ifdef FEAT_SYN_HL |
| 3553 | /* |
| 3554 | * <VN> We could instead free the synblock |
| 3555 | * and re-attach to buffer, perhaps. |
| 3556 | */ |
| 3557 | if (curwin->w_s == &(curwin->w_buffer->b_s)) |
Bram Moolenaar | 720ce53 | 2012-04-25 12:57:28 +0200 | [diff] [blame] | 3558 | curwin->w_s = &(buf->b_s); |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 3559 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3560 | curwin->w_buffer = buf; |
| 3561 | curbuf = buf; |
| 3562 | ++curbuf->b_nwindows; |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 3563 | |
| 3564 | /* Set 'fileformat', 'binary' and 'fenc' when forced. */ |
| 3565 | if (!oldbuf && eap != NULL) |
| 3566 | { |
| 3567 | set_file_options(TRUE, eap); |
Bram Moolenaar | a2320f4 | 2013-07-28 15:16:19 +0200 | [diff] [blame] | 3568 | #ifdef FEAT_MBYTE |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 3569 | set_forced_fenc(eap); |
Bram Moolenaar | a2320f4 | 2013-07-28 15:16:19 +0200 | [diff] [blame] | 3570 | #endif |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 3571 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
| 3574 | /* May get the window options from the last time this buffer |
| 3575 | * was in this window (or another window). If not used |
| 3576 | * before, reset the local window options to the global |
| 3577 | * values. Also restores old folding stuff. */ |
Bram Moolenaar | b1269f1 | 2007-06-19 09:51:25 +0000 | [diff] [blame] | 3578 | get_winopts(curbuf); |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 3579 | #ifdef FEAT_SPELL |
| 3580 | did_get_winopts = TRUE; |
| 3581 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3582 | |
| 3583 | #ifdef FEAT_AUTOCMD |
| 3584 | } |
| 3585 | vim_free(new_name); |
| 3586 | au_new_curbuf = NULL; |
| 3587 | #endif |
| 3588 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3589 | |
| 3590 | curwin->w_pcmark.lnum = 1; |
| 3591 | curwin->w_pcmark.col = 0; |
| 3592 | } |
| 3593 | else /* !other_file */ |
| 3594 | { |
| 3595 | if ( |
| 3596 | #ifdef FEAT_LISTCMDS |
| 3597 | (flags & ECMD_ADDBUF) || |
| 3598 | #endif |
| 3599 | check_fname() == FAIL) |
| 3600 | goto theend; |
Bram Moolenaar | df5caa0 | 2015-01-27 11:26:15 +0100 | [diff] [blame] | 3601 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3602 | oldbuf = (flags & ECMD_OLDBUF); |
| 3603 | } |
| 3604 | |
Bram Moolenaar | ab9fc7e | 2016-02-06 15:29:40 +0100 | [diff] [blame] | 3605 | /* Don't redraw until the cursor is in the right line, otherwise |
| 3606 | * autocommands may cause ml_get errors. */ |
| 3607 | ++RedrawingDisabled; |
| 3608 | did_inc_redrawing_disabled = TRUE; |
| 3609 | |
Bram Moolenaar | 54fb438 | 2014-11-12 19:28:16 +0100 | [diff] [blame] | 3610 | #ifdef FEAT_AUTOCMD |
| 3611 | buf = curbuf; |
| 3612 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3613 | if ((flags & ECMD_SET_HELP) || keep_help_flag) |
| 3614 | { |
Bram Moolenaar | 54fb438 | 2014-11-12 19:28:16 +0100 | [diff] [blame] | 3615 | prepare_help_buffer(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3616 | } |
| 3617 | else |
| 3618 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3619 | /* Don't make a buffer listed if it's a help buffer. Useful when |
| 3620 | * using CTRL-O to go back to a help file. */ |
| 3621 | if (!curbuf->b_help) |
| 3622 | set_buflisted(TRUE); |
| 3623 | } |
| 3624 | |
| 3625 | #ifdef FEAT_AUTOCMD |
| 3626 | /* If autocommands change buffers under our fingers, forget about |
| 3627 | * editing the file. */ |
| 3628 | if (buf != curbuf) |
| 3629 | goto theend; |
| 3630 | # ifdef FEAT_EVAL |
| 3631 | if (aborting()) /* autocmds may abort script processing */ |
| 3632 | goto theend; |
| 3633 | # endif |
| 3634 | |
| 3635 | /* Since we are starting to edit a file, consider the filetype to be |
| 3636 | * unset. Helps for when an autocommand changes files and expects syntax |
| 3637 | * highlighting to work in the other file. */ |
| 3638 | did_filetype = FALSE; |
| 3639 | #endif |
| 3640 | |
| 3641 | /* |
| 3642 | * other_file oldbuf |
| 3643 | * FALSE FALSE re-edit same file, buffer is re-used |
| 3644 | * FALSE TRUE re-edit same file, nothing changes |
| 3645 | * TRUE FALSE start editing new file, new buffer |
| 3646 | * TRUE TRUE start editing in existing buffer (nothing to do) |
| 3647 | */ |
| 3648 | if (!other_file && !oldbuf) /* re-use the buffer */ |
| 3649 | { |
| 3650 | set_last_cursor(curwin); /* may set b_last_cursor */ |
| 3651 | if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL) |
| 3652 | { |
| 3653 | newlnum = curwin->w_cursor.lnum; |
| 3654 | solcol = curwin->w_cursor.col; |
| 3655 | } |
| 3656 | #ifdef FEAT_AUTOCMD |
| 3657 | buf = curbuf; |
| 3658 | if (buf->b_fname != NULL) |
| 3659 | new_name = vim_strsave(buf->b_fname); |
| 3660 | else |
| 3661 | new_name = NULL; |
| 3662 | #endif |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 3663 | if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) |
| 3664 | { |
| 3665 | /* Save all the text, so that the reload can be undone. |
| 3666 | * Sync first so that this is a separate undo-able action. */ |
| 3667 | u_sync(FALSE); |
| 3668 | if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE) |
| 3669 | == FAIL) |
| 3670 | goto theend; |
| 3671 | u_unchanged(curbuf); |
| 3672 | buf_freeall(curbuf, BFA_KEEP_UNDO); |
| 3673 | |
| 3674 | /* tell readfile() not to clear or reload undo info */ |
| 3675 | readfile_flags = READ_KEEP_UNDO; |
| 3676 | } |
| 3677 | else |
| 3678 | buf_freeall(curbuf, 0); /* free all things for buffer */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3679 | #ifdef FEAT_AUTOCMD |
| 3680 | /* If autocommands deleted the buffer we were going to re-edit, give |
| 3681 | * up and jump to the end. */ |
| 3682 | if (!buf_valid(buf)) |
| 3683 | { |
| 3684 | delbuf_msg(new_name); /* frees new_name */ |
| 3685 | goto theend; |
| 3686 | } |
| 3687 | vim_free(new_name); |
| 3688 | |
| 3689 | /* If autocommands change buffers under our fingers, forget about |
| 3690 | * re-editing the file. Should do the buf_clear_file(), but perhaps |
| 3691 | * the autocommands changed the buffer... */ |
| 3692 | if (buf != curbuf) |
| 3693 | goto theend; |
| 3694 | # ifdef FEAT_EVAL |
| 3695 | if (aborting()) /* autocmds may abort script processing */ |
| 3696 | goto theend; |
| 3697 | # endif |
| 3698 | #endif |
| 3699 | buf_clear_file(curbuf); |
| 3700 | curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */ |
| 3701 | curbuf->b_op_end.lnum = 0; |
| 3702 | } |
| 3703 | |
| 3704 | /* |
| 3705 | * If we get here we are sure to start editing |
| 3706 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3707 | /* Assume success now */ |
| 3708 | retval = OK; |
| 3709 | |
| 3710 | /* |
| 3711 | * Reset cursor position, could be used by autocommands. |
| 3712 | */ |
| 3713 | check_cursor(); |
| 3714 | |
| 3715 | /* |
| 3716 | * Check if we are editing the w_arg_idx file in the argument list. |
| 3717 | */ |
| 3718 | check_arg_idx(curwin); |
| 3719 | |
| 3720 | #ifdef FEAT_AUTOCMD |
| 3721 | if (!auto_buf) |
| 3722 | #endif |
| 3723 | { |
| 3724 | /* |
| 3725 | * Set cursor and init window before reading the file and executing |
| 3726 | * autocommands. This allows for the autocommands to position the |
| 3727 | * cursor. |
| 3728 | */ |
Bram Moolenaar | faa959a | 2006-02-20 21:37:40 +0000 | [diff] [blame] | 3729 | curwin_init(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3730 | |
| 3731 | #ifdef FEAT_FOLDING |
Bram Moolenaar | eb1b679 | 2007-08-21 13:29:28 +0000 | [diff] [blame] | 3732 | /* It's possible that all lines in the buffer changed. Need to update |
| 3733 | * automatic folding for all windows where it's used. */ |
| 3734 | # ifdef FEAT_WINDOWS |
| 3735 | { |
| 3736 | win_T *win; |
| 3737 | tabpage_T *tp; |
| 3738 | |
| 3739 | FOR_ALL_TAB_WINDOWS(tp, win) |
| 3740 | if (win->w_buffer == curbuf) |
| 3741 | foldUpdateAll(win); |
| 3742 | } |
| 3743 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3744 | foldUpdateAll(curwin); |
Bram Moolenaar | eb1b679 | 2007-08-21 13:29:28 +0000 | [diff] [blame] | 3745 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3746 | #endif |
| 3747 | |
Bram Moolenaar | 498efdb | 2006-09-05 14:31:54 +0000 | [diff] [blame] | 3748 | /* Change directories when the 'acd' option is set. */ |
| 3749 | DO_AUTOCHDIR |
| 3750 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3751 | /* |
| 3752 | * Careful: open_buffer() and apply_autocmds() may change the current |
| 3753 | * buffer and window. |
| 3754 | */ |
Bram Moolenaar | eab316b | 2015-03-24 11:46:30 +0100 | [diff] [blame] | 3755 | orig_pos = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3756 | topline = curwin->w_topline; |
| 3757 | if (!oldbuf) /* need to read the file */ |
| 3758 | { |
Bram Moolenaar | d5bc83f | 2005-12-07 21:07:59 +0000 | [diff] [blame] | 3759 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3760 | swap_exists_action = SEA_DIALOG; |
| 3761 | #endif |
| 3762 | curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */ |
| 3763 | |
| 3764 | /* |
| 3765 | * Open the buffer and read the file. |
| 3766 | */ |
| 3767 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 3768 | if (should_abort(open_buffer(FALSE, eap, readfile_flags))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3769 | retval = FAIL; |
| 3770 | #else |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 3771 | (void)open_buffer(FALSE, eap, readfile_flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3772 | #endif |
| 3773 | |
Bram Moolenaar | d5bc83f | 2005-12-07 21:07:59 +0000 | [diff] [blame] | 3774 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3775 | if (swap_exists_action == SEA_QUIT) |
| 3776 | retval = FAIL; |
| 3777 | handle_swap_exists(old_curbuf); |
| 3778 | #endif |
| 3779 | } |
| 3780 | #ifdef FEAT_AUTOCMD |
| 3781 | else |
| 3782 | { |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 3783 | /* Read the modelines, but only to set window-local options. Any |
| 3784 | * buffer-local options have already been set and may have been |
| 3785 | * changed by the user. */ |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 3786 | do_modelines(OPT_WINONLY); |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 3787 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3788 | apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, |
| 3789 | &retval); |
| 3790 | apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, |
| 3791 | &retval); |
| 3792 | } |
| 3793 | check_arg_idx(curwin); |
| 3794 | #endif |
| 3795 | |
Bram Moolenaar | eab316b | 2015-03-24 11:46:30 +0100 | [diff] [blame] | 3796 | /* If autocommands change the cursor position or topline, we should |
| 3797 | * keep it. Also when it moves within a line. */ |
| 3798 | if (!equalpos(curwin->w_cursor, orig_pos)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3799 | { |
| 3800 | newlnum = curwin->w_cursor.lnum; |
| 3801 | newcol = curwin->w_cursor.col; |
| 3802 | } |
| 3803 | if (curwin->w_topline == topline) |
| 3804 | topline = 0; |
| 3805 | |
| 3806 | /* Even when cursor didn't move we need to recompute topline. */ |
| 3807 | changed_line_abv_curs(); |
| 3808 | |
| 3809 | #ifdef FEAT_TITLE |
| 3810 | maketitle(); |
| 3811 | #endif |
| 3812 | } |
| 3813 | |
| 3814 | #ifdef FEAT_DIFF |
| 3815 | /* Tell the diff stuff that this buffer is new and/or needs updating. |
| 3816 | * Also needed when re-editing the same buffer, because unloading will |
| 3817 | * have removed it as a diff buffer. */ |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3818 | if (curwin->w_p_diff) |
| 3819 | { |
| 3820 | diff_buf_add(curbuf); |
| 3821 | diff_invalidate(curbuf); |
| 3822 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3823 | #endif |
| 3824 | |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 3825 | #ifdef FEAT_SPELL |
| 3826 | /* If the window options were changed may need to set the spell language. |
| 3827 | * Can only do this after the buffer has been properly setup. */ |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 3828 | if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) |
| 3829 | (void)did_set_spelllang(curwin); |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 3830 | #endif |
| 3831 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3832 | if (command == NULL) |
| 3833 | { |
| 3834 | if (newcol >= 0) /* position set by autocommands */ |
| 3835 | { |
| 3836 | curwin->w_cursor.lnum = newlnum; |
| 3837 | curwin->w_cursor.col = newcol; |
| 3838 | check_cursor(); |
| 3839 | } |
| 3840 | else if (newlnum > 0) /* line number from caller or old position */ |
| 3841 | { |
| 3842 | curwin->w_cursor.lnum = newlnum; |
| 3843 | check_cursor_lnum(); |
| 3844 | if (solcol >= 0 && !p_sol) |
| 3845 | { |
| 3846 | /* 'sol' is off: Use last known column. */ |
| 3847 | curwin->w_cursor.col = solcol; |
| 3848 | check_cursor_col(); |
| 3849 | #ifdef FEAT_VIRTUALEDIT |
| 3850 | curwin->w_cursor.coladd = 0; |
| 3851 | #endif |
| 3852 | curwin->w_set_curswant = TRUE; |
| 3853 | } |
| 3854 | else |
| 3855 | beginline(BL_SOL | BL_FIX); |
| 3856 | } |
| 3857 | else /* no line number, go to last line in Ex mode */ |
| 3858 | { |
| 3859 | if (exmode_active) |
| 3860 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 3861 | beginline(BL_WHITE | BL_FIX); |
| 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | #ifdef FEAT_WINDOWS |
| 3866 | /* Check if cursors in other windows on the same buffer are still valid */ |
| 3867 | check_lnums(FALSE); |
| 3868 | #endif |
| 3869 | |
| 3870 | /* |
| 3871 | * Did not read the file, need to show some info about the file. |
| 3872 | * Do this after setting the cursor. |
| 3873 | */ |
| 3874 | if (oldbuf |
| 3875 | #ifdef FEAT_AUTOCMD |
| 3876 | && !auto_buf |
| 3877 | #endif |
| 3878 | ) |
| 3879 | { |
| 3880 | int msg_scroll_save = msg_scroll; |
| 3881 | |
| 3882 | /* Obey the 'O' flag in 'cpoptions': overwrite any previous file |
| 3883 | * message. */ |
| 3884 | if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) |
| 3885 | msg_scroll = FALSE; |
| 3886 | if (!msg_scroll) /* wait a bit when overwriting an error msg */ |
| 3887 | check_for_delay(FALSE); |
| 3888 | msg_start(); |
| 3889 | msg_scroll = msg_scroll_save; |
| 3890 | msg_scrolled_ign = TRUE; |
| 3891 | |
Bram Moolenaar | 426dd02 | 2016-03-15 15:09:29 +0100 | [diff] [blame] | 3892 | if (!shortmess(SHM_FILEINFO)) |
| 3893 | fileinfo(FALSE, TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3894 | |
| 3895 | msg_scrolled_ign = FALSE; |
| 3896 | } |
| 3897 | |
| 3898 | if (command != NULL) |
| 3899 | do_cmdline(command, NULL, NULL, DOCMD_VERBOSE); |
| 3900 | |
| 3901 | #ifdef FEAT_KEYMAP |
| 3902 | if (curbuf->b_kmap_state & KEYMAP_INIT) |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3903 | (void)keymap_init(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3904 | #endif |
| 3905 | |
| 3906 | --RedrawingDisabled; |
Bram Moolenaar | ab9fc7e | 2016-02-06 15:29:40 +0100 | [diff] [blame] | 3907 | did_inc_redrawing_disabled = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3908 | if (!skip_redraw) |
| 3909 | { |
| 3910 | n = p_so; |
| 3911 | if (topline == 0 && command == NULL) |
| 3912 | p_so = 999; /* force cursor halfway the window */ |
| 3913 | update_topline(); |
| 3914 | #ifdef FEAT_SCROLLBIND |
| 3915 | curwin->w_scbind_pos = curwin->w_topline; |
| 3916 | #endif |
| 3917 | p_so = n; |
| 3918 | redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */ |
| 3919 | } |
| 3920 | |
| 3921 | if (p_im) |
| 3922 | need_start_insertmode = TRUE; |
| 3923 | |
Bram Moolenaar | 498efdb | 2006-09-05 14:31:54 +0000 | [diff] [blame] | 3924 | /* Change directories when the 'acd' option is set. */ |
| 3925 | DO_AUTOCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3926 | |
Bram Moolenaar | 7b89edc | 2006-04-06 20:21:51 +0000 | [diff] [blame] | 3927 | #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 3928 | if (curbuf->b_ffname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3929 | { |
| 3930 | # ifdef FEAT_SUN_WORKSHOP |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 3931 | if (gui.in_use && usingSunWorkShop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3932 | workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro); |
| 3933 | # endif |
| 3934 | # ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 3935 | if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP) |
Bram Moolenaar | 35a9aaa | 2004-10-24 19:23:07 +0000 | [diff] [blame] | 3936 | netbeans_file_opened(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3937 | # endif |
| 3938 | } |
| 3939 | #endif |
| 3940 | |
| 3941 | theend: |
Bram Moolenaar | ab9fc7e | 2016-02-06 15:29:40 +0100 | [diff] [blame] | 3942 | if (did_inc_redrawing_disabled) |
| 3943 | --RedrawingDisabled; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 3944 | #ifdef FEAT_AUTOCMD |
| 3945 | if (did_set_swapcommand) |
| 3946 | set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); |
| 3947 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3948 | #ifdef FEAT_BROWSE |
| 3949 | vim_free(browse_file); |
| 3950 | #endif |
| 3951 | vim_free(free_fname); |
| 3952 | return retval; |
| 3953 | } |
| 3954 | |
| 3955 | #ifdef FEAT_AUTOCMD |
| 3956 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3957 | delbuf_msg(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3958 | { |
| 3959 | EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"), |
| 3960 | name == NULL ? (char_u *)"" : name); |
| 3961 | vim_free(name); |
| 3962 | au_new_curbuf = NULL; |
| 3963 | } |
| 3964 | #endif |
| 3965 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3966 | static int append_indent = 0; /* autoindent for first line */ |
| 3967 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3968 | /* |
| 3969 | * ":insert" and ":append", also used by ":change" |
| 3970 | */ |
| 3971 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3972 | ex_append(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3973 | { |
| 3974 | char_u *theline; |
| 3975 | int did_undo = FALSE; |
| 3976 | linenr_T lnum = eap->line2; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3977 | int indent = 0; |
| 3978 | char_u *p; |
| 3979 | int vcol; |
| 3980 | int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3981 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3982 | /* the ! flag toggles autoindent */ |
| 3983 | if (eap->forceit) |
| 3984 | curbuf->b_p_ai = !curbuf->b_p_ai; |
| 3985 | |
| 3986 | /* First autoindent comes from the line we start on */ |
| 3987 | if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0) |
| 3988 | append_indent = get_indent_lnum(lnum); |
| 3989 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3990 | if (eap->cmdidx != CMD_append) |
| 3991 | --lnum; |
| 3992 | |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3993 | /* when the buffer is empty append to line 0 and delete the dummy line */ |
| 3994 | if (empty && lnum == 1) |
| 3995 | lnum = 0; |
| 3996 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3997 | State = INSERT; /* behave like in Insert mode */ |
| 3998 | if (curbuf->b_p_iminsert == B_IMODE_LMAP) |
| 3999 | State |= LANGMAP; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4000 | |
Bram Moolenaar | d8e9bb2 | 2005-07-09 21:14:46 +0000 | [diff] [blame] | 4001 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4002 | { |
| 4003 | msg_scroll = TRUE; |
| 4004 | need_wait_return = FALSE; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4005 | if (curbuf->b_p_ai) |
| 4006 | { |
| 4007 | if (append_indent >= 0) |
| 4008 | { |
| 4009 | indent = append_indent; |
| 4010 | append_indent = -1; |
| 4011 | } |
| 4012 | else if (lnum > 0) |
| 4013 | indent = get_indent_lnum(lnum); |
| 4014 | } |
| 4015 | ex_keep_indent = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4016 | if (eap->getline == NULL) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4017 | { |
| 4018 | /* No getline() function, use the lines that follow. This ends |
| 4019 | * when there is no more. */ |
| 4020 | if (eap->nextcmd == NULL || *eap->nextcmd == NUL) |
| 4021 | break; |
| 4022 | p = vim_strchr(eap->nextcmd, NL); |
| 4023 | if (p == NULL) |
| 4024 | p = eap->nextcmd + STRLEN(eap->nextcmd); |
| 4025 | theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd)); |
| 4026 | if (*p != NUL) |
| 4027 | ++p; |
| 4028 | eap->nextcmd = p; |
| 4029 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4030 | else |
Bram Moolenaar | 26f08b0 | 2014-08-29 09:02:27 +0200 | [diff] [blame] | 4031 | { |
| 4032 | int save_State = State; |
| 4033 | |
| 4034 | /* Set State to avoid the cursor shape to be set to INSERT mode |
| 4035 | * when getline() returns. */ |
| 4036 | State = CMDLINE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4037 | theline = eap->getline( |
| 4038 | #ifdef FEAT_EVAL |
Bram Moolenaar | 3d60ec2 | 2005-01-05 22:19:46 +0000 | [diff] [blame] | 4039 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4040 | #endif |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4041 | NUL, eap->cookie, indent); |
Bram Moolenaar | 26f08b0 | 2014-08-29 09:02:27 +0200 | [diff] [blame] | 4042 | State = save_State; |
| 4043 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4044 | lines_left = Rows - 1; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4045 | if (theline == NULL) |
| 4046 | break; |
| 4047 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4048 | /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */ |
| 4049 | if (ex_keep_indent) |
| 4050 | append_indent = indent; |
| 4051 | |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4052 | /* Look for the "." after automatic indent. */ |
| 4053 | vcol = 0; |
| 4054 | for (p = theline; indent > vcol; ++p) |
| 4055 | { |
| 4056 | if (*p == ' ') |
| 4057 | ++vcol; |
| 4058 | else if (*p == TAB) |
| 4059 | vcol += 8 - vcol % 8; |
| 4060 | else |
| 4061 | break; |
| 4062 | } |
| 4063 | if ((p[0] == '.' && p[1] == NUL) |
Bram Moolenaar | eaa48e7 | 2005-06-08 22:07:37 +0000 | [diff] [blame] | 4064 | || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0)) |
| 4065 | == FAIL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4066 | { |
| 4067 | vim_free(theline); |
| 4068 | break; |
| 4069 | } |
| 4070 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4071 | /* don't use autoindent if nothing was typed. */ |
| 4072 | if (p[0] == NUL) |
| 4073 | theline[0] = NUL; |
| 4074 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4075 | did_undo = TRUE; |
| 4076 | ml_append(lnum, theline, (colnr_T)0, FALSE); |
| 4077 | appended_lines_mark(lnum, 1L); |
| 4078 | |
| 4079 | vim_free(theline); |
| 4080 | ++lnum; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4081 | |
| 4082 | if (empty) |
| 4083 | { |
| 4084 | ml_delete(2L, FALSE); |
| 4085 | empty = FALSE; |
| 4086 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4087 | } |
| 4088 | State = NORMAL; |
| 4089 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4090 | if (eap->forceit) |
| 4091 | curbuf->b_p_ai = !curbuf->b_p_ai; |
| 4092 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4093 | /* "start" is set to eap->line2+1 unless that position is invalid (when |
Bram Moolenaar | 4566751 | 2007-05-10 19:24:43 +0000 | [diff] [blame] | 4094 | * eap->line2 pointed to the end of the buffer and nothing was appended) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4095 | * "end" is set to lnum when something has been appended, otherwise |
| 4096 | * it is the same than "start" -- Acevedo */ |
| 4097 | curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ? |
| 4098 | eap->line2 + 1 : curbuf->b_ml.ml_line_count; |
| 4099 | if (eap->cmdidx != CMD_append) |
| 4100 | --curbuf->b_op_start.lnum; |
| 4101 | curbuf->b_op_end.lnum = (eap->line2 < lnum) |
| 4102 | ? lnum : curbuf->b_op_start.lnum; |
| 4103 | curbuf->b_op_start.col = curbuf->b_op_end.col = 0; |
| 4104 | curwin->w_cursor.lnum = lnum; |
| 4105 | check_cursor_lnum(); |
| 4106 | beginline(BL_SOL | BL_FIX); |
| 4107 | |
| 4108 | need_wait_return = FALSE; /* don't use wait_return() now */ |
| 4109 | ex_no_reprint = TRUE; |
| 4110 | } |
| 4111 | |
| 4112 | /* |
| 4113 | * ":change" |
| 4114 | */ |
| 4115 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4116 | ex_change(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4117 | { |
| 4118 | linenr_T lnum; |
| 4119 | |
| 4120 | if (eap->line2 >= eap->line1 |
| 4121 | && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) |
| 4122 | return; |
| 4123 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4124 | /* the ! flag toggles autoindent */ |
| 4125 | if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai) |
| 4126 | append_indent = get_indent_lnum(eap->line1); |
| 4127 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4128 | for (lnum = eap->line2; lnum >= eap->line1; --lnum) |
| 4129 | { |
| 4130 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ |
| 4131 | break; |
| 4132 | ml_delete(eap->line1, FALSE); |
| 4133 | } |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 4134 | |
| 4135 | /* make sure the cursor is not beyond the end of the file now */ |
| 4136 | check_cursor_lnum(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4137 | deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum)); |
| 4138 | |
| 4139 | /* ":append" on the line above the deleted lines. */ |
| 4140 | eap->line2 = eap->line1; |
| 4141 | ex_append(eap); |
| 4142 | } |
| 4143 | |
| 4144 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4145 | ex_z(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4146 | { |
| 4147 | char_u *x; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4148 | int bigness; |
| 4149 | char_u *kind; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4150 | int minus = 0; |
| 4151 | linenr_T start, end, curs, i; |
| 4152 | int j; |
| 4153 | linenr_T lnum = eap->line2; |
| 4154 | |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4155 | /* Vi compatible: ":z!" uses display height, without a count uses |
| 4156 | * 'scroll' */ |
| 4157 | if (eap->forceit) |
| 4158 | bigness = curwin->w_height; |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 4159 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 631abc3 | 2014-02-22 22:27:47 +0100 | [diff] [blame] | 4160 | else if (firstwin != lastwin) |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4161 | bigness = curwin->w_height - 3; |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 4162 | #endif |
Bram Moolenaar | 631abc3 | 2014-02-22 22:27:47 +0100 | [diff] [blame] | 4163 | else |
| 4164 | bigness = curwin->w_p_scr * 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4165 | if (bigness < 1) |
| 4166 | bigness = 1; |
| 4167 | |
| 4168 | x = eap->arg; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4169 | kind = x; |
| 4170 | if (*kind == '-' || *kind == '+' || *kind == '=' |
| 4171 | || *kind == '^' || *kind == '.') |
| 4172 | ++x; |
| 4173 | while (*x == '-' || *x == '+') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4174 | ++x; |
| 4175 | |
| 4176 | if (*x != 0) |
| 4177 | { |
| 4178 | if (!VIM_ISDIGIT(*x)) |
| 4179 | { |
| 4180 | EMSG(_("E144: non-numeric argument to :z")); |
| 4181 | return; |
| 4182 | } |
| 4183 | else |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4184 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4185 | bigness = atoi((char *)x); |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4186 | p_window = bigness; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4187 | if (*kind == '=') |
| 4188 | bigness += 2; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4189 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4192 | /* the number of '-' and '+' multiplies the distance */ |
| 4193 | if (*kind == '-' || *kind == '+') |
| 4194 | for (x = kind + 1; *x == *kind; ++x) |
| 4195 | ; |
| 4196 | |
| 4197 | switch (*kind) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4198 | { |
| 4199 | case '-': |
Bram Moolenaar | d9758e3 | 2011-06-12 22:03:23 +0200 | [diff] [blame] | 4200 | start = lnum - bigness * (linenr_T)(x - kind) + 1; |
| 4201 | end = start + bigness - 1; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4202 | curs = end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4203 | break; |
| 4204 | |
| 4205 | case '=': |
Bram Moolenaar | 2a8d1f8 | 2005-02-05 21:43:56 +0000 | [diff] [blame] | 4206 | start = lnum - (bigness + 1) / 2 + 1; |
| 4207 | end = lnum + (bigness + 1) / 2 - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4208 | curs = lnum; |
| 4209 | minus = 1; |
| 4210 | break; |
| 4211 | |
| 4212 | case '^': |
| 4213 | start = lnum - bigness * 2; |
| 4214 | end = lnum - bigness; |
| 4215 | curs = lnum - bigness; |
| 4216 | break; |
| 4217 | |
| 4218 | case '.': |
Bram Moolenaar | 2a8d1f8 | 2005-02-05 21:43:56 +0000 | [diff] [blame] | 4219 | start = lnum - (bigness + 1) / 2 + 1; |
| 4220 | end = lnum + (bigness + 1) / 2 - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4221 | curs = end; |
| 4222 | break; |
| 4223 | |
| 4224 | default: /* '+' */ |
| 4225 | start = lnum; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4226 | if (*kind == '+') |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4227 | start += bigness * (linenr_T)(x - kind - 1) + 1; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4228 | else if (eap->addr_count == 0) |
| 4229 | ++start; |
| 4230 | end = start + bigness - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4231 | curs = end; |
| 4232 | break; |
| 4233 | } |
| 4234 | |
| 4235 | if (start < 1) |
| 4236 | start = 1; |
| 4237 | |
| 4238 | if (end > curbuf->b_ml.ml_line_count) |
| 4239 | end = curbuf->b_ml.ml_line_count; |
| 4240 | |
| 4241 | if (curs > curbuf->b_ml.ml_line_count) |
| 4242 | curs = curbuf->b_ml.ml_line_count; |
| 4243 | |
| 4244 | for (i = start; i <= end; i++) |
| 4245 | { |
| 4246 | if (minus && i == lnum) |
| 4247 | { |
| 4248 | msg_putchar('\n'); |
| 4249 | |
| 4250 | for (j = 1; j < Columns; j++) |
| 4251 | msg_putchar('-'); |
| 4252 | } |
| 4253 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4254 | print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4255 | |
| 4256 | if (minus && i == lnum) |
| 4257 | { |
| 4258 | msg_putchar('\n'); |
| 4259 | |
| 4260 | for (j = 1; j < Columns; j++) |
| 4261 | msg_putchar('-'); |
| 4262 | } |
| 4263 | } |
| 4264 | |
| 4265 | curwin->w_cursor.lnum = curs; |
| 4266 | ex_no_reprint = TRUE; |
| 4267 | } |
| 4268 | |
| 4269 | /* |
| 4270 | * Check if the restricted flag is set. |
| 4271 | * If so, give an error message and return TRUE. |
| 4272 | * Otherwise, return FALSE. |
| 4273 | */ |
| 4274 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4275 | check_restricted(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4276 | { |
| 4277 | if (restricted) |
| 4278 | { |
| 4279 | EMSG(_("E145: Shell commands not allowed in rvim")); |
| 4280 | return TRUE; |
| 4281 | } |
| 4282 | return FALSE; |
| 4283 | } |
| 4284 | |
| 4285 | /* |
| 4286 | * Check if the secure flag is set (.exrc or .vimrc in current directory). |
| 4287 | * If so, give an error message and return TRUE. |
| 4288 | * Otherwise, return FALSE. |
| 4289 | */ |
| 4290 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4291 | check_secure(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4292 | { |
| 4293 | if (secure) |
| 4294 | { |
| 4295 | secure = 2; |
| 4296 | EMSG(_(e_curdir)); |
| 4297 | return TRUE; |
| 4298 | } |
| 4299 | #ifdef HAVE_SANDBOX |
| 4300 | /* |
| 4301 | * In the sandbox more things are not allowed, including the things |
| 4302 | * disallowed in secure mode. |
| 4303 | */ |
| 4304 | if (sandbox != 0) |
| 4305 | { |
| 4306 | EMSG(_(e_sandbox)); |
| 4307 | return TRUE; |
| 4308 | } |
| 4309 | #endif |
| 4310 | return FALSE; |
| 4311 | } |
| 4312 | |
| 4313 | static char_u *old_sub = NULL; /* previous substitute pattern */ |
| 4314 | static int global_need_beginline; /* call beginline() after ":g" */ |
| 4315 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4316 | /* do_sub() |
| 4317 | * |
| 4318 | * Perform a substitution from line eap->line1 to line eap->line2 using the |
| 4319 | * command pointed to by eap->arg which should be of the form: |
| 4320 | * |
| 4321 | * /pattern/substitution/{flags} |
| 4322 | * |
| 4323 | * The usual escapes are supported as described in the regexp docs. |
| 4324 | */ |
| 4325 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4326 | do_sub(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4327 | { |
| 4328 | linenr_T lnum; |
| 4329 | long i = 0; |
| 4330 | regmmatch_T regmatch; |
| 4331 | static int do_all = FALSE; /* do multiple substitutions per line */ |
| 4332 | static int do_ask = FALSE; /* ask for confirmation */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4333 | static int do_count = FALSE; /* count only */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4334 | static int do_error = TRUE; /* if false, ignore errors */ |
| 4335 | static int do_print = FALSE; /* print last line with subs. */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4336 | static int do_list = FALSE; /* list last line with subs. */ |
| 4337 | static int do_number = FALSE; /* list last line with line nr*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4338 | static int do_ic = 0; /* ignore case flag */ |
Bram Moolenaar | cad2fc9 | 2015-05-04 10:46:03 +0200 | [diff] [blame] | 4339 | int save_do_all; /* remember user specified 'g' flag */ |
| 4340 | int save_do_ask; /* remember user specified 'c' flag */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4341 | char_u *pat = NULL, *sub = NULL; /* init for GCC */ |
| 4342 | int delimiter; |
| 4343 | int sublen; |
| 4344 | int got_quit = FALSE; |
| 4345 | int got_match = FALSE; |
| 4346 | int temp; |
| 4347 | int which_pat; |
| 4348 | char_u *cmd; |
| 4349 | int save_State; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4350 | linenr_T first_line = 0; /* first changed line */ |
| 4351 | linenr_T last_line= 0; /* below last changed line AFTER the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4352 | * change */ |
| 4353 | linenr_T old_line_count = curbuf->b_ml.ml_line_count; |
| 4354 | linenr_T line2; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4355 | long nmatch; /* number of lines in match */ |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4356 | char_u *sub_firstline; /* allocated copy of first sub line */ |
| 4357 | int endcolumn = FALSE; /* cursor in last column when done */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4358 | pos_T old_cursor = curwin->w_cursor; |
Bram Moolenaar | 4647552 | 2010-03-23 17:36:29 +0100 | [diff] [blame] | 4359 | int start_nsubs; |
Bram Moolenaar | 07e31c5 | 2012-08-08 16:51:15 +0200 | [diff] [blame] | 4360 | #ifdef FEAT_EVAL |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 4361 | int save_ma = 0; |
Bram Moolenaar | 07e31c5 | 2012-08-08 16:51:15 +0200 | [diff] [blame] | 4362 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4363 | |
| 4364 | cmd = eap->arg; |
| 4365 | if (!global_busy) |
| 4366 | { |
| 4367 | sub_nsubs = 0; |
| 4368 | sub_nlines = 0; |
| 4369 | } |
Bram Moolenaar | 4647552 | 2010-03-23 17:36:29 +0100 | [diff] [blame] | 4370 | start_nsubs = sub_nsubs; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4371 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4372 | if (eap->cmdidx == CMD_tilde) |
| 4373 | which_pat = RE_LAST; /* use last used regexp */ |
| 4374 | else |
| 4375 | which_pat = RE_SUBST; /* use last substitute regexp */ |
| 4376 | |
| 4377 | /* new pattern and substitution */ |
| 4378 | if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd) |
| 4379 | && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL) |
| 4380 | { |
| 4381 | /* don't accept alphanumeric for separator */ |
| 4382 | if (isalpha(*cmd)) |
| 4383 | { |
| 4384 | EMSG(_("E146: Regular expressions can't be delimited by letters")); |
| 4385 | return; |
| 4386 | } |
| 4387 | /* |
| 4388 | * undocumented vi feature: |
| 4389 | * "\/sub/" and "\?sub?" use last used search pattern (almost like |
| 4390 | * //sub/r). "\&sub&" use last substitute pattern (like //sub/). |
| 4391 | */ |
| 4392 | if (*cmd == '\\') |
| 4393 | { |
| 4394 | ++cmd; |
| 4395 | if (vim_strchr((char_u *)"/?&", *cmd) == NULL) |
| 4396 | { |
| 4397 | EMSG(_(e_backslash)); |
| 4398 | return; |
| 4399 | } |
| 4400 | if (*cmd != '&') |
| 4401 | which_pat = RE_SEARCH; /* use last '/' pattern */ |
| 4402 | pat = (char_u *)""; /* empty search pattern */ |
| 4403 | delimiter = *cmd++; /* remember delimiter character */ |
| 4404 | } |
| 4405 | else /* find the end of the regexp */ |
| 4406 | { |
Bram Moolenaar | 3a169c3 | 2008-01-02 12:59:21 +0000 | [diff] [blame] | 4407 | #ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */ |
| 4408 | if (p_altkeymap && curwin->w_p_rl) |
| 4409 | lrF_sub(cmd); |
| 4410 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4411 | which_pat = RE_LAST; /* use last used regexp */ |
| 4412 | delimiter = *cmd++; /* remember delimiter character */ |
| 4413 | pat = cmd; /* remember start of search pat */ |
| 4414 | cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg); |
| 4415 | if (cmd[0] == delimiter) /* end delimiter found */ |
| 4416 | *cmd++ = NUL; /* replace it with a NUL */ |
| 4417 | } |
| 4418 | |
| 4419 | /* |
| 4420 | * Small incompatibility: vi sees '\n' as end of the command, but in |
| 4421 | * Vim we want to use '\n' to find/substitute a NUL. |
| 4422 | */ |
| 4423 | sub = cmd; /* remember the start of the substitution */ |
| 4424 | |
| 4425 | while (cmd[0]) |
| 4426 | { |
| 4427 | if (cmd[0] == delimiter) /* end delimiter found */ |
| 4428 | { |
| 4429 | *cmd++ = NUL; /* replace it with a NUL */ |
| 4430 | break; |
| 4431 | } |
| 4432 | if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */ |
| 4433 | ++cmd; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4434 | mb_ptr_adv(cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | if (!eap->skip) |
| 4438 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4439 | /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */ |
| 4440 | if (STRCMP(sub, "%") == 0 |
| 4441 | && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL) |
| 4442 | { |
| 4443 | if (old_sub == NULL) /* there is no previous command */ |
| 4444 | { |
| 4445 | EMSG(_(e_nopresub)); |
| 4446 | return; |
| 4447 | } |
| 4448 | sub = old_sub; |
| 4449 | } |
| 4450 | else |
| 4451 | { |
| 4452 | vim_free(old_sub); |
| 4453 | old_sub = vim_strsave(sub); |
| 4454 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4455 | } |
| 4456 | } |
| 4457 | else if (!eap->skip) /* use previous pattern and substitution */ |
| 4458 | { |
| 4459 | if (old_sub == NULL) /* there is no previous command */ |
| 4460 | { |
| 4461 | EMSG(_(e_nopresub)); |
| 4462 | return; |
| 4463 | } |
| 4464 | pat = NULL; /* search_regcomp() will use previous pattern */ |
| 4465 | sub = old_sub; |
Bram Moolenaar | b11bd7e | 2005-02-07 22:05:52 +0000 | [diff] [blame] | 4466 | |
| 4467 | /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the |
| 4468 | * last column after using "$". */ |
| 4469 | endcolumn = (curwin->w_curswant == MAXCOL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Bram Moolenaar | fd3fe98 | 2014-04-01 17:49:44 +0200 | [diff] [blame] | 4472 | /* Recognize ":%s/\n//" and turn it into a join command, which is much |
| 4473 | * more efficient. |
| 4474 | * TODO: find a generic solution to make line-joining operations more |
| 4475 | * efficient, avoid allocating a string that grows in size. |
| 4476 | */ |
Bram Moolenaar | 21e854e | 2014-04-04 19:00:48 +0200 | [diff] [blame] | 4477 | if (pat != NULL && STRCMP(pat, "\\n") == 0 |
Bram Moolenaar | fd3fe98 | 2014-04-01 17:49:44 +0200 | [diff] [blame] | 4478 | && *sub == NUL |
| 4479 | && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l' |
| 4480 | || *cmd == 'p' || *cmd == '#')))) |
| 4481 | { |
Bram Moolenaar | cc2b9d5 | 2014-12-13 03:17:11 +0100 | [diff] [blame] | 4482 | linenr_T joined_lines_count; |
| 4483 | |
Bram Moolenaar | fd3fe98 | 2014-04-01 17:49:44 +0200 | [diff] [blame] | 4484 | curwin->w_cursor.lnum = eap->line1; |
| 4485 | if (*cmd == 'l') |
| 4486 | eap->flags = EXFLAG_LIST; |
| 4487 | else if (*cmd == '#') |
| 4488 | eap->flags = EXFLAG_NR; |
| 4489 | else if (*cmd == 'p') |
| 4490 | eap->flags = EXFLAG_PRINT; |
| 4491 | |
Bram Moolenaar | cc2b9d5 | 2014-12-13 03:17:11 +0100 | [diff] [blame] | 4492 | /* The number of lines joined is the number of lines in the range plus |
| 4493 | * one. One less when the last line is included. */ |
| 4494 | joined_lines_count = eap->line2 - eap->line1 + 1; |
| 4495 | if (eap->line2 < curbuf->b_ml.ml_line_count) |
| 4496 | ++joined_lines_count; |
| 4497 | if (joined_lines_count > 1) |
| 4498 | { |
| 4499 | (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE); |
| 4500 | sub_nsubs = joined_lines_count - 1; |
| 4501 | sub_nlines = 1; |
| 4502 | (void)do_sub_msg(FALSE); |
| 4503 | ex_may_print(eap); |
| 4504 | } |
| 4505 | |
| 4506 | if (!cmdmod.keeppatterns) |
| 4507 | save_re_pat(RE_SUBST, pat, p_magic); |
| 4508 | #ifdef FEAT_CMDHIST |
| 4509 | /* put pattern in history */ |
| 4510 | add_to_history(HIST_SEARCH, pat, TRUE, NUL); |
| 4511 | #endif |
| 4512 | |
Bram Moolenaar | fd3fe98 | 2014-04-01 17:49:44 +0200 | [diff] [blame] | 4513 | return; |
| 4514 | } |
| 4515 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4516 | /* |
| 4517 | * Find trailing options. When '&' is used, keep old options. |
| 4518 | */ |
| 4519 | if (*cmd == '&') |
| 4520 | ++cmd; |
| 4521 | else |
| 4522 | { |
| 4523 | if (!p_ed) |
| 4524 | { |
| 4525 | if (p_gd) /* default is global on */ |
| 4526 | do_all = TRUE; |
| 4527 | else |
| 4528 | do_all = FALSE; |
| 4529 | do_ask = FALSE; |
| 4530 | } |
| 4531 | do_error = TRUE; |
| 4532 | do_print = FALSE; |
Bram Moolenaar | cc016f5 | 2005-12-10 20:23:46 +0000 | [diff] [blame] | 4533 | do_count = FALSE; |
Bram Moolenaar | fe40d1a | 2007-07-24 09:16:38 +0000 | [diff] [blame] | 4534 | do_number = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4535 | do_ic = 0; |
| 4536 | } |
| 4537 | while (*cmd) |
| 4538 | { |
| 4539 | /* |
| 4540 | * Note that 'g' and 'c' are always inverted, also when p_ed is off. |
| 4541 | * 'r' is never inverted. |
| 4542 | */ |
| 4543 | if (*cmd == 'g') |
| 4544 | do_all = !do_all; |
| 4545 | else if (*cmd == 'c') |
| 4546 | do_ask = !do_ask; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4547 | else if (*cmd == 'n') |
| 4548 | do_count = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4549 | else if (*cmd == 'e') |
| 4550 | do_error = !do_error; |
| 4551 | else if (*cmd == 'r') /* use last used regexp */ |
| 4552 | which_pat = RE_LAST; |
| 4553 | else if (*cmd == 'p') |
| 4554 | do_print = TRUE; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4555 | else if (*cmd == '#') |
| 4556 | { |
| 4557 | do_print = TRUE; |
| 4558 | do_number = TRUE; |
| 4559 | } |
| 4560 | else if (*cmd == 'l') |
| 4561 | { |
| 4562 | do_print = TRUE; |
| 4563 | do_list = TRUE; |
| 4564 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4565 | else if (*cmd == 'i') /* ignore case */ |
| 4566 | do_ic = 'i'; |
| 4567 | else if (*cmd == 'I') /* don't ignore case */ |
| 4568 | do_ic = 'I'; |
| 4569 | else |
| 4570 | break; |
| 4571 | ++cmd; |
| 4572 | } |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4573 | if (do_count) |
| 4574 | do_ask = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4575 | |
Bram Moolenaar | cad2fc9 | 2015-05-04 10:46:03 +0200 | [diff] [blame] | 4576 | save_do_all = do_all; |
| 4577 | save_do_ask = do_ask; |
| 4578 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4579 | /* |
| 4580 | * check for a trailing count |
| 4581 | */ |
| 4582 | cmd = skipwhite(cmd); |
| 4583 | if (VIM_ISDIGIT(*cmd)) |
| 4584 | { |
| 4585 | i = getdigits(&cmd); |
| 4586 | if (i <= 0 && !eap->skip && do_error) |
| 4587 | { |
| 4588 | EMSG(_(e_zerocount)); |
| 4589 | return; |
| 4590 | } |
| 4591 | eap->line1 = eap->line2; |
| 4592 | eap->line2 += i - 1; |
| 4593 | if (eap->line2 > curbuf->b_ml.ml_line_count) |
| 4594 | eap->line2 = curbuf->b_ml.ml_line_count; |
| 4595 | } |
| 4596 | |
| 4597 | /* |
| 4598 | * check for trailing command or garbage |
| 4599 | */ |
| 4600 | cmd = skipwhite(cmd); |
| 4601 | if (*cmd && *cmd != '"') /* if not end-of-line or comment */ |
| 4602 | { |
| 4603 | eap->nextcmd = check_nextcmd(cmd); |
| 4604 | if (eap->nextcmd == NULL) |
| 4605 | { |
| 4606 | EMSG(_(e_trailing)); |
| 4607 | return; |
| 4608 | } |
| 4609 | } |
| 4610 | |
| 4611 | if (eap->skip) /* not executing commands, only parsing */ |
| 4612 | return; |
| 4613 | |
Bram Moolenaar | 61660ea | 2006-04-07 21:40:07 +0000 | [diff] [blame] | 4614 | if (!do_count && !curbuf->b_p_ma) |
| 4615 | { |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 4616 | /* Substitution is not allowed in non-'modifiable' buffer */ |
Bram Moolenaar | 61660ea | 2006-04-07 21:40:07 +0000 | [diff] [blame] | 4617 | EMSG(_(e_modifiable)); |
| 4618 | return; |
| 4619 | } |
| 4620 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4621 | if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, ®match) == FAIL) |
| 4622 | { |
| 4623 | if (do_error) |
| 4624 | EMSG(_(e_invcmd)); |
| 4625 | return; |
| 4626 | } |
| 4627 | |
| 4628 | /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */ |
| 4629 | if (do_ic == 'i') |
| 4630 | regmatch.rmm_ic = TRUE; |
| 4631 | else if (do_ic == 'I') |
| 4632 | regmatch.rmm_ic = FALSE; |
| 4633 | |
| 4634 | sub_firstline = NULL; |
| 4635 | |
| 4636 | /* |
| 4637 | * ~ in the substitute pattern is replaced with the old pattern. |
| 4638 | * We do it here once to avoid it to be replaced over and over again. |
| 4639 | * But don't do it when it starts with "\=", then it's an expression. |
| 4640 | */ |
| 4641 | if (!(sub[0] == '\\' && sub[1] == '=')) |
| 4642 | sub = regtilde(sub, p_magic); |
| 4643 | |
| 4644 | /* |
| 4645 | * Check for a match on each line. |
| 4646 | */ |
| 4647 | line2 = eap->line2; |
| 4648 | for (lnum = eap->line1; lnum <= line2 && !(got_quit |
| 4649 | #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) |
| 4650 | || aborting() |
| 4651 | #endif |
| 4652 | ); ++lnum) |
| 4653 | { |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 4654 | nmatch = vim_regexec_multi(®match, curwin, curbuf, lnum, |
| 4655 | (colnr_T)0, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4656 | if (nmatch) |
| 4657 | { |
| 4658 | colnr_T copycol; |
| 4659 | colnr_T matchcol; |
| 4660 | colnr_T prev_matchcol = MAXCOL; |
| 4661 | char_u *new_end, *new_start = NULL; |
| 4662 | unsigned new_start_len = 0; |
| 4663 | char_u *p1; |
| 4664 | int did_sub = FALSE; |
| 4665 | int lastone; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4666 | int len, copy_len, needed_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4667 | long nmatch_tl = 0; /* nr of lines matched below lnum */ |
| 4668 | int do_again; /* do it again after joining lines */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 4669 | int skip_match = FALSE; |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 4670 | linenr_T sub_firstlnum; /* nr of first sub line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4671 | |
| 4672 | /* |
| 4673 | * The new text is build up step by step, to avoid too much |
| 4674 | * copying. There are these pieces: |
Bram Moolenaar | a9aafe5 | 2008-05-07 11:10:28 +0000 | [diff] [blame] | 4675 | * sub_firstline The old text, unmodified. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4676 | * copycol Column in the old text where we started |
| 4677 | * looking for a match; from here old text still |
| 4678 | * needs to be copied to the new text. |
| 4679 | * matchcol Column number of the old text where to look |
| 4680 | * for the next match. It's just after the |
| 4681 | * previous match or one further. |
| 4682 | * prev_matchcol Column just after the previous match (if any). |
| 4683 | * Mostly equal to matchcol, except for the first |
| 4684 | * match and after skipping an empty match. |
| 4685 | * regmatch.*pos Where the pattern matched in the old text. |
| 4686 | * new_start The new text, all that has been produced so |
| 4687 | * far. |
| 4688 | * new_end The new text, where to append new text. |
| 4689 | * |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 4690 | * lnum The line number where we found the start of |
| 4691 | * the match. Can be below the line we searched |
| 4692 | * when there is a \n before a \zs in the |
| 4693 | * pattern. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4694 | * sub_firstlnum The line number in the buffer where to look |
| 4695 | * for a match. Can be different from "lnum" |
| 4696 | * when the pattern or substitute string contains |
| 4697 | * line breaks. |
| 4698 | * |
| 4699 | * Special situations: |
| 4700 | * - When the substitute string contains a line break, the part up |
| 4701 | * to the line break is inserted in the text, but the copy of |
| 4702 | * the original line is kept. "sub_firstlnum" is adjusted for |
| 4703 | * the inserted lines. |
| 4704 | * - When the matched pattern contains a line break, the old line |
| 4705 | * is taken from the line at the end of the pattern. The lines |
| 4706 | * in the match are deleted later, "sub_firstlnum" is adjusted |
| 4707 | * accordingly. |
| 4708 | * |
| 4709 | * The new text is built up in new_start[]. It has some extra |
| 4710 | * room to avoid using alloc()/free() too often. new_start_len is |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 4711 | * the length of the allocated memory at new_start. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4712 | * |
| 4713 | * Make a copy of the old line, so it won't be taken away when |
| 4714 | * updating the screen or handling a multi-line match. The "old_" |
| 4715 | * pointers point into this copy. |
| 4716 | */ |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 4717 | sub_firstlnum = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4718 | copycol = 0; |
| 4719 | matchcol = 0; |
| 4720 | |
| 4721 | /* At first match, remember current cursor position. */ |
| 4722 | if (!got_match) |
| 4723 | { |
| 4724 | setpcmark(); |
| 4725 | got_match = TRUE; |
| 4726 | } |
| 4727 | |
| 4728 | /* |
| 4729 | * Loop until nothing more to replace in this line. |
| 4730 | * 1. Handle match with empty string. |
| 4731 | * 2. If do_ask is set, ask for confirmation. |
| 4732 | * 3. substitute the string. |
| 4733 | * 4. if do_all is set, find next match |
| 4734 | * 5. break if there isn't another match in this line |
| 4735 | */ |
| 4736 | for (;;) |
| 4737 | { |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 4738 | /* Advance "lnum" to the line where the match starts. The |
| 4739 | * match does not start in the first line when there is a line |
| 4740 | * break before \zs. */ |
| 4741 | if (regmatch.startpos[0].lnum > 0) |
| 4742 | { |
| 4743 | lnum += regmatch.startpos[0].lnum; |
| 4744 | sub_firstlnum += regmatch.startpos[0].lnum; |
| 4745 | nmatch -= regmatch.startpos[0].lnum; |
| 4746 | vim_free(sub_firstline); |
| 4747 | sub_firstline = NULL; |
| 4748 | } |
| 4749 | |
| 4750 | if (sub_firstline == NULL) |
| 4751 | { |
| 4752 | sub_firstline = vim_strsave(ml_get(sub_firstlnum)); |
| 4753 | if (sub_firstline == NULL) |
| 4754 | { |
| 4755 | vim_free(new_start); |
| 4756 | goto outofmem; |
| 4757 | } |
| 4758 | } |
| 4759 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4760 | /* Save the line number of the last change for the final |
| 4761 | * cursor position (just like Vi). */ |
| 4762 | curwin->w_cursor.lnum = lnum; |
| 4763 | do_again = FALSE; |
| 4764 | |
| 4765 | /* |
| 4766 | * 1. Match empty string does not count, except for first |
| 4767 | * match. This reproduces the strange vi behaviour. |
| 4768 | * This also catches endless loops. |
| 4769 | */ |
| 4770 | if (matchcol == prev_matchcol |
| 4771 | && regmatch.endpos[0].lnum == 0 |
| 4772 | && matchcol == regmatch.endpos[0].col) |
| 4773 | { |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 4774 | if (sub_firstline[matchcol] == NUL) |
| 4775 | /* We already were at the end of the line. Don't look |
| 4776 | * for a match in this line again. */ |
| 4777 | skip_match = TRUE; |
| 4778 | else |
Bram Moolenaar | 0df1102 | 2011-05-19 14:30:16 +0200 | [diff] [blame] | 4779 | { |
| 4780 | /* search for a match at next column */ |
| 4781 | #ifdef FEAT_MBYTE |
| 4782 | if (has_mbyte) |
| 4783 | matchcol += mb_ptr2len(sub_firstline + matchcol); |
| 4784 | else |
| 4785 | #endif |
| 4786 | ++matchcol; |
| 4787 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4788 | goto skip; |
| 4789 | } |
| 4790 | |
| 4791 | /* Normally we continue searching for a match just after the |
| 4792 | * previous match. */ |
| 4793 | matchcol = regmatch.endpos[0].col; |
| 4794 | prev_matchcol = matchcol; |
| 4795 | |
| 4796 | /* |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4797 | * 2. If do_count is set only increase the counter. |
| 4798 | * If do_ask is set, ask for confirmation. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4799 | */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4800 | if (do_count) |
| 4801 | { |
| 4802 | /* For a multi-line match, put matchcol at the NUL at |
| 4803 | * the end of the line and set nmatch to one, so that |
| 4804 | * we continue looking for a match on the next line. |
| 4805 | * Avoids that ":s/\nB\@=//gc" get stuck. */ |
| 4806 | if (nmatch > 1) |
| 4807 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4808 | matchcol = (colnr_T)STRLEN(sub_firstline); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4809 | nmatch = 1; |
Bram Moolenaar | 12ddc3e | 2008-01-04 13:53:09 +0000 | [diff] [blame] | 4810 | skip_match = TRUE; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4811 | } |
| 4812 | sub_nsubs++; |
| 4813 | did_sub = TRUE; |
Bram Moolenaar | 07e31c5 | 2012-08-08 16:51:15 +0200 | [diff] [blame] | 4814 | #ifdef FEAT_EVAL |
| 4815 | /* Skip the substitution, unless an expression is used, |
| 4816 | * then it is evaluated in the sandbox. */ |
| 4817 | if (!(sub[0] == '\\' && sub[1] == '=')) |
| 4818 | #endif |
| 4819 | goto skip; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4822 | if (do_ask) |
| 4823 | { |
Bram Moolenaar | 985cb44 | 2009-05-14 19:51:46 +0000 | [diff] [blame] | 4824 | int typed = 0; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4825 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | /* change State to CONFIRM, so that the mouse works |
| 4827 | * properly */ |
| 4828 | save_State = State; |
| 4829 | State = CONFIRM; |
| 4830 | #ifdef FEAT_MOUSE |
| 4831 | setmouse(); /* disable mouse in xterm */ |
| 4832 | #endif |
| 4833 | curwin->w_cursor.col = regmatch.startpos[0].col; |
| 4834 | |
| 4835 | /* When 'cpoptions' contains "u" don't sync undo when |
| 4836 | * asking for confirmation. */ |
| 4837 | if (vim_strchr(p_cpo, CPO_UNDO) != NULL) |
| 4838 | ++no_u_sync; |
| 4839 | |
| 4840 | /* |
| 4841 | * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed. |
| 4842 | */ |
| 4843 | while (do_ask) |
| 4844 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4845 | if (exmode_active) |
| 4846 | { |
| 4847 | char_u *resp; |
| 4848 | colnr_T sc, ec; |
| 4849 | |
Bram Moolenaar | 3eead7c | 2013-10-02 18:43:06 +0200 | [diff] [blame] | 4850 | print_line_no_prefix(lnum, do_number, do_list); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4851 | |
| 4852 | getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL); |
| 4853 | curwin->w_cursor.col = regmatch.endpos[0].col - 1; |
| 4854 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec); |
Bram Moolenaar | 3eead7c | 2013-10-02 18:43:06 +0200 | [diff] [blame] | 4855 | if (do_number || curwin->w_p_nu) |
| 4856 | { |
| 4857 | int numw = number_width(curwin) + 1; |
| 4858 | sc += numw; |
| 4859 | ec += numw; |
| 4860 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4861 | msg_start(); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4862 | for (i = 0; i < (long)sc; ++i) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4863 | msg_putchar(' '); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4864 | for ( ; i <= (long)ec; ++i) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4865 | msg_putchar('^'); |
| 4866 | |
| 4867 | resp = getexmodeline('?', NULL, 0); |
| 4868 | if (resp != NULL) |
| 4869 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4870 | typed = *resp; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4871 | vim_free(resp); |
| 4872 | } |
| 4873 | } |
| 4874 | else |
| 4875 | { |
Bram Moolenaar | 4bc8cf0 | 2013-01-30 16:30:26 +0100 | [diff] [blame] | 4876 | char_u *orig_line = NULL; |
| 4877 | int len_change = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4878 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4879 | int save_p_fen = curwin->w_p_fen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4880 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4881 | curwin->w_p_fen = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4882 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4883 | /* Invert the matched string. |
| 4884 | * Remove the inversion afterwards. */ |
| 4885 | temp = RedrawingDisabled; |
| 4886 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4887 | |
Bram Moolenaar | 4bc8cf0 | 2013-01-30 16:30:26 +0100 | [diff] [blame] | 4888 | if (new_start != NULL) |
| 4889 | { |
| 4890 | /* There already was a substitution, we would |
| 4891 | * like to show this to the user. We cannot |
| 4892 | * really update the line, it would change |
| 4893 | * what matches. Temporarily replace the line |
| 4894 | * and change it back afterwards. */ |
| 4895 | orig_line = vim_strsave(ml_get(lnum)); |
| 4896 | if (orig_line != NULL) |
| 4897 | { |
| 4898 | char_u *new_line = concat_str(new_start, |
| 4899 | sub_firstline + copycol); |
| 4900 | |
| 4901 | if (new_line == NULL) |
| 4902 | { |
| 4903 | vim_free(orig_line); |
| 4904 | orig_line = NULL; |
| 4905 | } |
| 4906 | else |
| 4907 | { |
| 4908 | /* Position the cursor relative to the |
| 4909 | * end of the line, the previous |
| 4910 | * substitute may have inserted or |
| 4911 | * deleted characters before the |
| 4912 | * cursor. */ |
Bram Moolenaar | 04e5b5a | 2013-01-30 21:56:21 +0100 | [diff] [blame] | 4913 | len_change = (int)STRLEN(new_line) |
| 4914 | - (int)STRLEN(orig_line); |
Bram Moolenaar | 4bc8cf0 | 2013-01-30 16:30:26 +0100 | [diff] [blame] | 4915 | curwin->w_cursor.col += len_change; |
| 4916 | ml_replace(lnum, new_line, FALSE); |
| 4917 | } |
| 4918 | } |
| 4919 | } |
| 4920 | |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 4921 | search_match_lines = regmatch.endpos[0].lnum |
| 4922 | - regmatch.startpos[0].lnum; |
Bram Moolenaar | 4bc8cf0 | 2013-01-30 16:30:26 +0100 | [diff] [blame] | 4923 | search_match_endcol = regmatch.endpos[0].col |
| 4924 | + len_change; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4925 | highlight_match = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4926 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4927 | update_topline(); |
| 4928 | validate_cursor(); |
Bram Moolenaar | a1956f6 | 2006-03-12 22:18:00 +0000 | [diff] [blame] | 4929 | update_screen(SOME_VALID); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4930 | highlight_match = FALSE; |
Bram Moolenaar | a1956f6 | 2006-03-12 22:18:00 +0000 | [diff] [blame] | 4931 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4932 | |
| 4933 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4934 | curwin->w_p_fen = save_p_fen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4935 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4936 | if (msg_row == Rows - 1) |
| 4937 | msg_didout = FALSE; /* avoid a scroll-up */ |
| 4938 | msg_starthere(); |
| 4939 | i = msg_scroll; |
| 4940 | msg_scroll = 0; /* truncate msg when |
| 4941 | needed */ |
| 4942 | msg_no_more = TRUE; |
| 4943 | /* write message same highlighting as for |
| 4944 | * wait_return */ |
| 4945 | smsg_attr(hl_attr(HLF_R), |
| 4946 | (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub); |
| 4947 | msg_no_more = FALSE; |
| 4948 | msg_scroll = i; |
| 4949 | showruler(TRUE); |
| 4950 | windgoto(msg_row, msg_col); |
| 4951 | RedrawingDisabled = temp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4952 | |
| 4953 | #ifdef USE_ON_FLY_SCROLL |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4954 | dont_scroll = FALSE; /* allow scrolling here */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4955 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4956 | ++no_mapping; /* don't map this key */ |
| 4957 | ++allow_keys; /* allow special keys */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4958 | typed = plain_vgetc(); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4959 | --allow_keys; |
| 4960 | --no_mapping; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4961 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4962 | /* clear the question */ |
| 4963 | msg_didout = FALSE; /* don't scroll up */ |
| 4964 | msg_col = 0; |
| 4965 | gotocmdline(TRUE); |
Bram Moolenaar | 4bc8cf0 | 2013-01-30 16:30:26 +0100 | [diff] [blame] | 4966 | |
| 4967 | /* restore the line */ |
| 4968 | if (orig_line != NULL) |
| 4969 | ml_replace(lnum, orig_line, FALSE); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4972 | need_wait_return = FALSE; /* no hit-return prompt */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4973 | if (typed == 'q' || typed == ESC || typed == Ctrl_C |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4974 | #ifdef UNIX |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4975 | || typed == intr_char |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4976 | #endif |
| 4977 | ) |
| 4978 | { |
| 4979 | got_quit = TRUE; |
| 4980 | break; |
| 4981 | } |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4982 | if (typed == 'n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4983 | break; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4984 | if (typed == 'y') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4985 | break; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4986 | if (typed == 'l') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4987 | { |
| 4988 | /* last: replace and then stop */ |
| 4989 | do_all = FALSE; |
| 4990 | line2 = lnum; |
| 4991 | break; |
| 4992 | } |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4993 | if (typed == 'a') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4994 | { |
| 4995 | do_ask = FALSE; |
| 4996 | break; |
| 4997 | } |
| 4998 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4999 | if (typed == Ctrl_E) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5000 | scrollup_clamp(); |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5001 | else if (typed == Ctrl_Y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5002 | scrolldown_clamp(); |
| 5003 | #endif |
| 5004 | } |
| 5005 | State = save_State; |
| 5006 | #ifdef FEAT_MOUSE |
| 5007 | setmouse(); |
| 5008 | #endif |
| 5009 | if (vim_strchr(p_cpo, CPO_UNDO) != NULL) |
| 5010 | --no_u_sync; |
| 5011 | |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5012 | if (typed == 'n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5013 | { |
| 5014 | /* For a multi-line match, put matchcol at the NUL at |
| 5015 | * the end of the line and set nmatch to one, so that |
| 5016 | * we continue looking for a match on the next line. |
Bram Moolenaar | c432b50 | 2007-03-15 20:38:26 +0000 | [diff] [blame] | 5017 | * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc" |
| 5018 | * get stuck when pressing 'n'. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5019 | if (nmatch > 1) |
| 5020 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5021 | matchcol = (colnr_T)STRLEN(sub_firstline); |
Bram Moolenaar | c432b50 | 2007-03-15 20:38:26 +0000 | [diff] [blame] | 5022 | skip_match = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5023 | } |
| 5024 | goto skip; |
| 5025 | } |
| 5026 | if (got_quit) |
Bram Moolenaar | 11cb6e6 | 2013-02-06 18:24:02 +0100 | [diff] [blame] | 5027 | goto skip; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | /* Move the cursor to the start of the match, so that we can |
| 5031 | * use "\=col("."). */ |
| 5032 | curwin->w_cursor.col = regmatch.startpos[0].col; |
| 5033 | |
| 5034 | /* |
| 5035 | * 3. substitute the string. |
| 5036 | */ |
Bram Moolenaar | 07e31c5 | 2012-08-08 16:51:15 +0200 | [diff] [blame] | 5037 | #ifdef FEAT_EVAL |
| 5038 | if (do_count) |
| 5039 | { |
Bram Moolenaar | 7c0a86b | 2012-09-05 15:15:07 +0200 | [diff] [blame] | 5040 | /* prevent accidentally changing the buffer by a function */ |
Bram Moolenaar | 07e31c5 | 2012-08-08 16:51:15 +0200 | [diff] [blame] | 5041 | save_ma = curbuf->b_p_ma; |
| 5042 | curbuf->b_p_ma = FALSE; |
| 5043 | sandbox++; |
| 5044 | } |
| 5045 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5046 | /* get length of substitution part */ |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 5047 | sublen = vim_regsub_multi(®match, |
| 5048 | sub_firstlnum - regmatch.startpos[0].lnum, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5049 | sub, sub_firstline, FALSE, p_magic, TRUE); |
Bram Moolenaar | 07e31c5 | 2012-08-08 16:51:15 +0200 | [diff] [blame] | 5050 | #ifdef FEAT_EVAL |
| 5051 | if (do_count) |
| 5052 | { |
| 5053 | curbuf->b_p_ma = save_ma; |
| 5054 | sandbox--; |
| 5055 | goto skip; |
| 5056 | } |
| 5057 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5058 | |
Bram Moolenaar | 4463f29 | 2005-09-25 22:20:24 +0000 | [diff] [blame] | 5059 | /* When the match included the "$" of the last line it may |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5060 | * go beyond the last line of the buffer. */ |
Bram Moolenaar | 4463f29 | 2005-09-25 22:20:24 +0000 | [diff] [blame] | 5061 | if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) |
| 5062 | { |
| 5063 | nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1; |
| 5064 | skip_match = TRUE; |
| 5065 | } |
| 5066 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5067 | /* Need room for: |
| 5068 | * - result so far in new_start (not for first sub in line) |
| 5069 | * - original text up to match |
| 5070 | * - length of substituted part |
| 5071 | * - original text after match |
| 5072 | */ |
| 5073 | if (nmatch == 1) |
| 5074 | p1 = sub_firstline; |
| 5075 | else |
| 5076 | { |
| 5077 | p1 = ml_get(sub_firstlnum + nmatch - 1); |
| 5078 | nmatch_tl += nmatch - 1; |
| 5079 | } |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5080 | copy_len = regmatch.startpos[0].col - copycol; |
| 5081 | needed_len = copy_len + ((unsigned)STRLEN(p1) |
| 5082 | - regmatch.endpos[0].col) + sublen + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5083 | if (new_start == NULL) |
| 5084 | { |
| 5085 | /* |
| 5086 | * Get some space for a temporary buffer to do the |
| 5087 | * substitution into (and some extra space to avoid |
| 5088 | * too many calls to alloc()/free()). |
| 5089 | */ |
| 5090 | new_start_len = needed_len + 50; |
| 5091 | if ((new_start = alloc_check(new_start_len)) == NULL) |
| 5092 | goto outofmem; |
| 5093 | *new_start = NUL; |
| 5094 | new_end = new_start; |
| 5095 | } |
| 5096 | else |
| 5097 | { |
| 5098 | /* |
| 5099 | * Check if the temporary buffer is long enough to do the |
| 5100 | * substitution into. If not, make it larger (with a bit |
| 5101 | * extra to avoid too many calls to alloc()/free()). |
| 5102 | */ |
| 5103 | len = (unsigned)STRLEN(new_start); |
| 5104 | needed_len += len; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5105 | if (needed_len > (int)new_start_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5106 | { |
| 5107 | new_start_len = needed_len + 50; |
| 5108 | if ((p1 = alloc_check(new_start_len)) == NULL) |
| 5109 | { |
| 5110 | vim_free(new_start); |
| 5111 | goto outofmem; |
| 5112 | } |
| 5113 | mch_memmove(p1, new_start, (size_t)(len + 1)); |
| 5114 | vim_free(new_start); |
| 5115 | new_start = p1; |
| 5116 | } |
| 5117 | new_end = new_start + len; |
| 5118 | } |
| 5119 | |
| 5120 | /* |
| 5121 | * copy the text up to the part that matched |
| 5122 | */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5123 | mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len); |
| 5124 | new_end += copy_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5125 | |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 5126 | (void)vim_regsub_multi(®match, |
| 5127 | sub_firstlnum - regmatch.startpos[0].lnum, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5128 | sub, new_end, TRUE, p_magic, TRUE); |
| 5129 | sub_nsubs++; |
| 5130 | did_sub = TRUE; |
| 5131 | |
| 5132 | /* Move the cursor to the start of the line, to avoid that it |
| 5133 | * is beyond the end of the line after the substitution. */ |
| 5134 | curwin->w_cursor.col = 0; |
| 5135 | |
| 5136 | /* For a multi-line match, make a copy of the last matched |
| 5137 | * line and continue in that one. */ |
| 5138 | if (nmatch > 1) |
| 5139 | { |
| 5140 | sub_firstlnum += nmatch - 1; |
| 5141 | vim_free(sub_firstline); |
| 5142 | sub_firstline = vim_strsave(ml_get(sub_firstlnum)); |
| 5143 | /* When going beyond the last line, stop substituting. */ |
| 5144 | if (sub_firstlnum <= line2) |
| 5145 | do_again = TRUE; |
| 5146 | else |
| 5147 | do_all = FALSE; |
| 5148 | } |
| 5149 | |
| 5150 | /* Remember next character to be copied. */ |
| 5151 | copycol = regmatch.endpos[0].col; |
| 5152 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5153 | if (skip_match) |
| 5154 | { |
| 5155 | /* Already hit end of the buffer, sub_firstlnum is one |
| 5156 | * less than what it ought to be. */ |
| 5157 | vim_free(sub_firstline); |
| 5158 | sub_firstline = vim_strsave((char_u *)""); |
| 5159 | copycol = 0; |
| 5160 | } |
| 5161 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5162 | /* |
| 5163 | * Now the trick is to replace CTRL-M chars with a real line |
| 5164 | * break. This would make it impossible to insert a CTRL-M in |
| 5165 | * the text. The line break can be avoided by preceding the |
| 5166 | * CTRL-M with a backslash. To be able to insert a backslash, |
| 5167 | * they must be doubled in the string and are halved here. |
| 5168 | * That is Vi compatible. |
| 5169 | */ |
| 5170 | for (p1 = new_end; *p1; ++p1) |
| 5171 | { |
| 5172 | if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */ |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 5173 | STRMOVE(p1, p1 + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5174 | else if (*p1 == CAR) |
| 5175 | { |
| 5176 | if (u_inssub(lnum) == OK) /* prepare for undo */ |
| 5177 | { |
| 5178 | *p1 = NUL; /* truncate up to the CR */ |
| 5179 | ml_append(lnum - 1, new_start, |
| 5180 | (colnr_T)(p1 - new_start + 1), FALSE); |
| 5181 | mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); |
| 5182 | if (do_ask) |
| 5183 | appended_lines(lnum - 1, 1L); |
| 5184 | else |
| 5185 | { |
| 5186 | if (first_line == 0) |
| 5187 | first_line = lnum; |
| 5188 | last_line = lnum + 1; |
| 5189 | } |
| 5190 | /* All line numbers increase. */ |
| 5191 | ++sub_firstlnum; |
| 5192 | ++lnum; |
| 5193 | ++line2; |
| 5194 | /* move the cursor to the new line, like Vi */ |
| 5195 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | f9ffd18 | 2007-11-20 17:04:29 +0000 | [diff] [blame] | 5196 | /* copy the rest */ |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 5197 | STRMOVE(new_start, p1 + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5198 | p1 = new_start - 1; |
| 5199 | } |
| 5200 | } |
| 5201 | #ifdef FEAT_MBYTE |
| 5202 | else if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5203 | p1 += (*mb_ptr2len)(p1) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5204 | #endif |
| 5205 | } |
| 5206 | |
| 5207 | /* |
| 5208 | * 4. If do_all is set, find next match. |
| 5209 | * Prevent endless loop with patterns that match empty |
| 5210 | * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g. |
| 5211 | * But ":s/\n/#/" is OK. |
| 5212 | */ |
| 5213 | skip: |
| 5214 | /* We already know that we did the last subst when we are at |
| 5215 | * the end of the line, except that a pattern like |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 5216 | * "bar\|\nfoo" may match at the NUL. "lnum" can be below |
| 5217 | * "line2" when there is a \zs in the pattern after a line |
| 5218 | * break. */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 5219 | lastone = (skip_match |
| 5220 | || got_int |
| 5221 | || got_quit |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 5222 | || lnum > line2 |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 5223 | || !(do_all || do_again) |
| 5224 | || (sub_firstline[matchcol] == NUL && nmatch <= 1 |
| 5225 | && !re_multiline(regmatch.regprog))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5226 | nmatch = -1; |
| 5227 | |
| 5228 | /* |
| 5229 | * Replace the line in the buffer when needed. This is |
| 5230 | * skipped when there are more matches. |
| 5231 | * The check for nmatch_tl is needed for when multi-line |
| 5232 | * matching must replace the lines before trying to do another |
| 5233 | * match, otherwise "\@<=" won't work. |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 5234 | * When the match starts below where we start searching also |
| 5235 | * need to replace the line first (using \zs after \n). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5236 | */ |
| 5237 | if (lastone |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5238 | || nmatch_tl > 0 |
| 5239 | || (nmatch = vim_regexec_multi(®match, curwin, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 5240 | curbuf, sub_firstlnum, |
| 5241 | matchcol, NULL)) == 0 |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 5242 | || regmatch.startpos[0].lnum > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5243 | { |
| 5244 | if (new_start != NULL) |
| 5245 | { |
| 5246 | /* |
| 5247 | * Copy the rest of the line, that didn't match. |
| 5248 | * "matchcol" has to be adjusted, we use the end of |
| 5249 | * the line as reference, because the substitute may |
| 5250 | * have changed the number of characters. Same for |
| 5251 | * "prev_matchcol". |
| 5252 | */ |
| 5253 | STRCAT(new_start, sub_firstline + copycol); |
| 5254 | matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; |
| 5255 | prev_matchcol = (colnr_T)STRLEN(sub_firstline) |
| 5256 | - prev_matchcol; |
| 5257 | |
| 5258 | if (u_savesub(lnum) != OK) |
| 5259 | break; |
| 5260 | ml_replace(lnum, new_start, TRUE); |
| 5261 | |
| 5262 | if (nmatch_tl > 0) |
| 5263 | { |
| 5264 | /* |
| 5265 | * Matched lines have now been substituted and are |
| 5266 | * useless, delete them. The part after the match |
| 5267 | * has been appended to new_start, we don't need |
| 5268 | * it in the buffer. |
| 5269 | */ |
| 5270 | ++lnum; |
| 5271 | if (u_savedel(lnum, nmatch_tl) != OK) |
| 5272 | break; |
| 5273 | for (i = 0; i < nmatch_tl; ++i) |
| 5274 | ml_delete(lnum, (int)FALSE); |
| 5275 | mark_adjust(lnum, lnum + nmatch_tl - 1, |
| 5276 | (long)MAXLNUM, -nmatch_tl); |
| 5277 | if (do_ask) |
| 5278 | deleted_lines(lnum, nmatch_tl); |
| 5279 | --lnum; |
| 5280 | line2 -= nmatch_tl; /* nr of lines decreases */ |
| 5281 | nmatch_tl = 0; |
| 5282 | } |
| 5283 | |
| 5284 | /* When asking, undo is saved each time, must also set |
| 5285 | * changed flag each time. */ |
| 5286 | if (do_ask) |
| 5287 | changed_bytes(lnum, 0); |
| 5288 | else |
| 5289 | { |
| 5290 | if (first_line == 0) |
| 5291 | first_line = lnum; |
| 5292 | last_line = lnum + 1; |
| 5293 | } |
| 5294 | |
| 5295 | sub_firstlnum = lnum; |
| 5296 | vim_free(sub_firstline); /* free the temp buffer */ |
| 5297 | sub_firstline = new_start; |
| 5298 | new_start = NULL; |
| 5299 | matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; |
| 5300 | prev_matchcol = (colnr_T)STRLEN(sub_firstline) |
| 5301 | - prev_matchcol; |
| 5302 | copycol = 0; |
| 5303 | } |
| 5304 | if (nmatch == -1 && !lastone) |
| 5305 | nmatch = vim_regexec_multi(®match, curwin, curbuf, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 5306 | sub_firstlnum, matchcol, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5307 | |
| 5308 | /* |
| 5309 | * 5. break if there isn't another match in this line |
| 5310 | */ |
| 5311 | if (nmatch <= 0) |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 5312 | { |
| 5313 | /* If the match found didn't start where we were |
| 5314 | * searching, do the next search in the line where we |
| 5315 | * found the match. */ |
| 5316 | if (nmatch == -1) |
| 5317 | lnum -= regmatch.startpos[0].lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5318 | break; |
Bram Moolenaar | bd7cc03 | 2008-01-09 21:40:50 +0000 | [diff] [blame] | 5319 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | line_breakcheck(); |
| 5323 | } |
| 5324 | |
| 5325 | if (did_sub) |
| 5326 | ++sub_nlines; |
Bram Moolenaar | da2bd6f | 2008-09-14 19:41:30 +0000 | [diff] [blame] | 5327 | vim_free(new_start); /* for when substitute was cancelled */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5328 | vim_free(sub_firstline); /* free the copy of the original line */ |
| 5329 | sub_firstline = NULL; |
| 5330 | } |
| 5331 | |
| 5332 | line_breakcheck(); |
| 5333 | } |
| 5334 | |
| 5335 | if (first_line != 0) |
| 5336 | { |
| 5337 | /* Need to subtract the number of added lines from "last_line" to get |
| 5338 | * the line number before the change (same as adding the number of |
| 5339 | * deleted lines). */ |
| 5340 | i = curbuf->b_ml.ml_line_count - old_line_count; |
| 5341 | changed_lines(first_line, 0, last_line - i, i); |
| 5342 | } |
| 5343 | |
| 5344 | outofmem: |
| 5345 | vim_free(sub_firstline); /* may have to free allocated copy of the line */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 5346 | |
| 5347 | /* ":s/pat//n" doesn't move the cursor */ |
| 5348 | if (do_count) |
| 5349 | curwin->w_cursor = old_cursor; |
| 5350 | |
Bram Moolenaar | 4647552 | 2010-03-23 17:36:29 +0100 | [diff] [blame] | 5351 | if (sub_nsubs > start_nsubs) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5352 | { |
| 5353 | /* Set the '[ and '] marks. */ |
| 5354 | curbuf->b_op_start.lnum = eap->line1; |
| 5355 | curbuf->b_op_end.lnum = line2; |
| 5356 | curbuf->b_op_start.col = curbuf->b_op_end.col = 0; |
| 5357 | |
| 5358 | if (!global_busy) |
| 5359 | { |
Bram Moolenaar | 0faaeb8 | 2012-03-07 14:57:52 +0100 | [diff] [blame] | 5360 | if (!do_ask) /* when interactive leave cursor on the match */ |
| 5361 | { |
| 5362 | if (endcolumn) |
| 5363 | coladvance((colnr_T)MAXCOL); |
| 5364 | else |
| 5365 | beginline(BL_WHITE | BL_FIX); |
| 5366 | } |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 5367 | if (!do_sub_msg(do_count) && do_ask) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5368 | MSG(""); |
| 5369 | } |
| 5370 | else |
| 5371 | global_need_beginline = TRUE; |
| 5372 | if (do_print) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 5373 | print_line(curwin->w_cursor.lnum, do_number, do_list); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5374 | } |
| 5375 | else if (!global_busy) |
| 5376 | { |
| 5377 | if (got_int) /* interrupted */ |
| 5378 | EMSG(_(e_interr)); |
| 5379 | else if (got_match) /* did find something but nothing substituted */ |
| 5380 | MSG(""); |
| 5381 | else if (do_error) /* nothing found */ |
| 5382 | EMSG2(_(e_patnotf2), get_search_pat()); |
| 5383 | } |
| 5384 | |
Bram Moolenaar | 8c4fbd1 | 2013-01-17 18:34:05 +0100 | [diff] [blame] | 5385 | #ifdef FEAT_FOLDING |
| 5386 | if (do_ask && hasAnyFolding(curwin)) |
| 5387 | /* Cursor position may require updating */ |
| 5388 | changed_window_setting(); |
| 5389 | #endif |
| 5390 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5391 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | cad2fc9 | 2015-05-04 10:46:03 +0200 | [diff] [blame] | 5392 | |
| 5393 | /* Restore the flag values, they can be used for ":&&". */ |
| 5394 | do_all = save_do_all; |
| 5395 | do_ask = save_do_ask; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | /* |
| 5399 | * Give message for number of substitutions. |
| 5400 | * Can also be used after a ":global" command. |
| 5401 | * Return TRUE if a message was given. |
| 5402 | */ |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 5403 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5404 | do_sub_msg( |
| 5405 | int count_only) /* used 'n' flag for ":s" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5406 | { |
| 5407 | /* |
| 5408 | * Only report substitutions when: |
| 5409 | * - more than 'report' substitutions |
| 5410 | * - command was typed by user, or number of changed lines > 'report' |
| 5411 | * - giving messages is not disabled by 'lazyredraw' |
| 5412 | */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 5413 | if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1)) |
| 5414 | || count_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5415 | && messaging()) |
| 5416 | { |
| 5417 | if (got_int) |
| 5418 | STRCPY(msg_buf, _("(Interrupted) ")); |
Bram Moolenaar | 0bc380a | 2010-07-10 13:52:13 +0200 | [diff] [blame] | 5419 | else |
| 5420 | *msg_buf = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5421 | if (sub_nsubs == 1) |
Bram Moolenaar | a800b42 | 2010-06-27 01:15:55 +0200 | [diff] [blame] | 5422 | vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5423 | "%s", count_only ? _("1 match") : _("1 substitution")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5424 | else |
Bram Moolenaar | a800b42 | 2010-06-27 01:15:55 +0200 | [diff] [blame] | 5425 | vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 5426 | count_only ? _("%ld matches") : _("%ld substitutions"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5427 | sub_nsubs); |
| 5428 | if (sub_nlines == 1) |
Bram Moolenaar | a800b42 | 2010-06-27 01:15:55 +0200 | [diff] [blame] | 5429 | vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5430 | "%s", _(" on 1 line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5431 | else |
Bram Moolenaar | a800b42 | 2010-06-27 01:15:55 +0200 | [diff] [blame] | 5432 | vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5433 | _(" on %ld lines"), (long)sub_nlines); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5434 | if (msg(msg_buf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5435 | /* save message to display it after redraw */ |
Bram Moolenaar | 238a564 | 2006-02-21 22:12:05 +0000 | [diff] [blame] | 5436 | set_keep_msg(msg_buf, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5437 | return TRUE; |
| 5438 | } |
| 5439 | if (got_int) |
| 5440 | { |
| 5441 | EMSG(_(e_interr)); |
| 5442 | return TRUE; |
| 5443 | } |
| 5444 | return FALSE; |
| 5445 | } |
| 5446 | |
| 5447 | /* |
| 5448 | * Execute a global command of the form: |
| 5449 | * |
| 5450 | * g/pattern/X : execute X on all lines where pattern matches |
| 5451 | * v/pattern/X : execute X on all lines where pattern does not match |
| 5452 | * |
| 5453 | * where 'X' is an EX command |
| 5454 | * |
| 5455 | * The command character (as well as the trailing slash) is optional, and |
| 5456 | * is assumed to be 'p' if missing. |
| 5457 | * |
| 5458 | * This is implemented in two passes: first we scan the file for the pattern and |
Bram Moolenaar | 7c0a86b | 2012-09-05 15:15:07 +0200 | [diff] [blame] | 5459 | * set a mark for each line that (not) matches. Secondly we execute the command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5460 | * for each line that has a mark. This is required because after deleting |
| 5461 | * lines we do not know where to search for the next match. |
| 5462 | */ |
| 5463 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5464 | ex_global(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5465 | { |
| 5466 | linenr_T lnum; /* line number according to old situation */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 5467 | int ndone = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5468 | int type; /* first char of cmd: 'v' or 'g' */ |
| 5469 | char_u *cmd; /* command argument */ |
| 5470 | |
| 5471 | char_u delim; /* delimiter, normally '/' */ |
| 5472 | char_u *pat; |
| 5473 | regmmatch_T regmatch; |
| 5474 | int match; |
| 5475 | int which_pat; |
| 5476 | |
| 5477 | if (global_busy) |
| 5478 | { |
| 5479 | EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */ |
| 5480 | return; |
| 5481 | } |
| 5482 | |
| 5483 | if (eap->forceit) /* ":global!" is like ":vglobal" */ |
| 5484 | type = 'v'; |
| 5485 | else |
| 5486 | type = *eap->cmd; |
| 5487 | cmd = eap->arg; |
| 5488 | which_pat = RE_LAST; /* default: use last used regexp */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5489 | |
| 5490 | /* |
| 5491 | * undocumented vi feature: |
| 5492 | * "\/" and "\?": use previous search pattern. |
| 5493 | * "\&": use previous substitute pattern. |
| 5494 | */ |
| 5495 | if (*cmd == '\\') |
| 5496 | { |
| 5497 | ++cmd; |
| 5498 | if (vim_strchr((char_u *)"/?&", *cmd) == NULL) |
| 5499 | { |
| 5500 | EMSG(_(e_backslash)); |
| 5501 | return; |
| 5502 | } |
| 5503 | if (*cmd == '&') |
| 5504 | which_pat = RE_SUBST; /* use previous substitute pattern */ |
| 5505 | else |
| 5506 | which_pat = RE_SEARCH; /* use previous search pattern */ |
| 5507 | ++cmd; |
| 5508 | pat = (char_u *)""; |
| 5509 | } |
| 5510 | else if (*cmd == NUL) |
| 5511 | { |
| 5512 | EMSG(_("E148: Regular expression missing from global")); |
| 5513 | return; |
| 5514 | } |
| 5515 | else |
| 5516 | { |
| 5517 | delim = *cmd; /* get the delimiter */ |
| 5518 | if (delim) |
| 5519 | ++cmd; /* skip delimiter if there is one */ |
| 5520 | pat = cmd; /* remember start of pattern */ |
| 5521 | cmd = skip_regexp(cmd, delim, p_magic, &eap->arg); |
| 5522 | if (cmd[0] == delim) /* end delimiter found */ |
| 5523 | *cmd++ = NUL; /* replace it with a NUL */ |
| 5524 | } |
| 5525 | |
| 5526 | #ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */ |
| 5527 | if (p_altkeymap && curwin->w_p_rl) |
| 5528 | lrFswap(pat,0); |
| 5529 | #endif |
| 5530 | |
| 5531 | if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, ®match) == FAIL) |
| 5532 | { |
| 5533 | EMSG(_(e_invcmd)); |
| 5534 | return; |
| 5535 | } |
| 5536 | |
| 5537 | /* |
| 5538 | * pass 1: set marks for each (not) matching line |
| 5539 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5540 | for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum) |
| 5541 | { |
| 5542 | /* a match on this line? */ |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 5543 | match = vim_regexec_multi(®match, curwin, curbuf, lnum, |
| 5544 | (colnr_T)0, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5545 | if ((type == 'g' && match) || (type == 'v' && !match)) |
| 5546 | { |
| 5547 | ml_setmarked(lnum); |
| 5548 | ndone++; |
| 5549 | } |
| 5550 | line_breakcheck(); |
| 5551 | } |
| 5552 | |
| 5553 | /* |
| 5554 | * pass 2: execute the command for each line that has been marked |
| 5555 | */ |
| 5556 | if (got_int) |
| 5557 | MSG(_(e_interr)); |
| 5558 | else if (ndone == 0) |
| 5559 | { |
| 5560 | if (type == 'v') |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5561 | smsg((char_u *)_("Pattern found in every line: %s"), pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5562 | else |
Bram Moolenaar | c389fd3 | 2013-03-07 16:08:35 +0100 | [diff] [blame] | 5563 | smsg((char_u *)_("Pattern not found: %s"), pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5564 | } |
| 5565 | else |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 5566 | { |
| 5567 | #ifdef FEAT_CLIPBOARD |
| 5568 | start_global_changes(); |
| 5569 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5570 | global_exe(cmd); |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 5571 | #ifdef FEAT_CLIPBOARD |
| 5572 | end_global_changes(); |
| 5573 | #endif |
| 5574 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5575 | |
| 5576 | ml_clearmarked(); /* clear rest of the marks */ |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5577 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | /* |
| 5581 | * Execute "cmd" on lines marked with ml_setmarked(). |
| 5582 | */ |
| 5583 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5584 | global_exe(char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5585 | { |
Bram Moolenaar | bb99322 | 2011-05-10 16:00:47 +0200 | [diff] [blame] | 5586 | linenr_T old_lcount; /* b_ml.ml_line_count before the command */ |
| 5587 | buf_T *old_buf = curbuf; /* remember what buffer we started in */ |
| 5588 | linenr_T lnum; /* line number according to old situation */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5589 | |
| 5590 | /* |
| 5591 | * Set current position only once for a global command. |
| 5592 | * If global_busy is set, setpcmark() will not do anything. |
| 5593 | * If there is an error, global_busy will be incremented. |
| 5594 | */ |
| 5595 | setpcmark(); |
| 5596 | |
| 5597 | /* When the command writes a message, don't overwrite the command. */ |
| 5598 | msg_didout = TRUE; |
| 5599 | |
Bram Moolenaar | d25bc23 | 2010-03-23 17:49:24 +0100 | [diff] [blame] | 5600 | sub_nsubs = 0; |
| 5601 | sub_nlines = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5602 | global_need_beginline = FALSE; |
| 5603 | global_busy = 1; |
| 5604 | old_lcount = curbuf->b_ml.ml_line_count; |
| 5605 | while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1) |
| 5606 | { |
| 5607 | curwin->w_cursor.lnum = lnum; |
| 5608 | curwin->w_cursor.col = 0; |
| 5609 | if (*cmd == NUL || *cmd == '\n') |
| 5610 | do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT); |
| 5611 | else |
| 5612 | do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT); |
| 5613 | ui_breakcheck(); |
| 5614 | } |
| 5615 | |
| 5616 | global_busy = 0; |
| 5617 | if (global_need_beginline) |
| 5618 | beginline(BL_WHITE | BL_FIX); |
| 5619 | else |
| 5620 | check_cursor(); /* cursor may be beyond the end of the line */ |
| 5621 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5622 | /* the cursor may not have moved in the text but a change in a previous |
| 5623 | * line may move it on the screen */ |
| 5624 | changed_line_abv_curs(); |
| 5625 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5626 | /* If it looks like no message was written, allow overwriting the |
| 5627 | * command with the report for number of changes. */ |
| 5628 | if (msg_col == 0 && msg_scrolled == 0) |
| 5629 | msg_didout = FALSE; |
| 5630 | |
Bram Moolenaar | 4566751 | 2007-05-10 19:24:43 +0000 | [diff] [blame] | 5631 | /* If substitutes done, report number of substitutes, otherwise report |
Bram Moolenaar | bb99322 | 2011-05-10 16:00:47 +0200 | [diff] [blame] | 5632 | * number of extra or deleted lines. |
| 5633 | * Don't report extra or deleted lines in the edge case where the buffer |
| 5634 | * we are in after execution is different from the buffer we started in. */ |
| 5635 | if (!do_sub_msg(FALSE) && curbuf == old_buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5636 | msgmore(curbuf->b_ml.ml_line_count - old_lcount); |
| 5637 | } |
| 5638 | |
| 5639 | #ifdef FEAT_VIMINFO |
| 5640 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5641 | read_viminfo_sub_string(vir_T *virp, int force) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5642 | { |
Bram Moolenaar | 3c2d653 | 2011-02-01 13:48:53 +0100 | [diff] [blame] | 5643 | if (force) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5644 | vim_free(old_sub); |
| 5645 | if (force || old_sub == NULL) |
| 5646 | old_sub = viminfo_readstring(virp, 1, TRUE); |
| 5647 | return viminfo_readline(virp); |
| 5648 | } |
| 5649 | |
| 5650 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5651 | write_viminfo_sub_string(FILE *fp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5652 | { |
| 5653 | if (get_viminfo_parameter('/') != 0 && old_sub != NULL) |
| 5654 | { |
Bram Moolenaar | 2f1e050 | 2010-08-13 11:18:02 +0200 | [diff] [blame] | 5655 | fputs(_("\n# Last Substitute String:\n$"), fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5656 | viminfo_writestring(fp, old_sub); |
| 5657 | } |
| 5658 | } |
| 5659 | #endif /* FEAT_VIMINFO */ |
| 5660 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 5661 | #if defined(EXITFREE) || defined(PROTO) |
| 5662 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5663 | free_old_sub(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 5664 | { |
| 5665 | vim_free(old_sub); |
| 5666 | } |
| 5667 | #endif |
| 5668 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5669 | #if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO) |
| 5670 | /* |
| 5671 | * Set up for a tagpreview. |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5672 | * Return TRUE when it was created. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5673 | */ |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5674 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5675 | prepare_tagpreview( |
| 5676 | int undo_sync) /* sync undo when leaving the window */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5677 | { |
| 5678 | win_T *wp; |
| 5679 | |
| 5680 | # ifdef FEAT_GUI |
| 5681 | need_mouse_correct = TRUE; |
| 5682 | # endif |
| 5683 | |
| 5684 | /* |
| 5685 | * If there is already a preview window open, use that one. |
| 5686 | */ |
| 5687 | if (!curwin->w_p_pvw) |
| 5688 | { |
| 5689 | for (wp = firstwin; wp != NULL; wp = wp->w_next) |
| 5690 | if (wp->w_p_pvw) |
| 5691 | break; |
| 5692 | if (wp != NULL) |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 5693 | win_enter(wp, undo_sync); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5694 | else |
| 5695 | { |
| 5696 | /* |
| 5697 | * There is no preview window open yet. Create one. |
| 5698 | */ |
| 5699 | if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0) |
| 5700 | == FAIL) |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5701 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5702 | curwin->w_p_pvw = TRUE; |
| 5703 | curwin->w_p_wfh = TRUE; |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 5704 | RESET_BINDING(curwin); /* don't take over 'scrollbind' |
| 5705 | and 'cursorbind' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5706 | # ifdef FEAT_DIFF |
| 5707 | curwin->w_p_diff = FALSE; /* no 'diff' */ |
| 5708 | # endif |
| 5709 | # ifdef FEAT_FOLDING |
| 5710 | curwin->w_p_fdc = 0; /* no 'foldcolumn' */ |
| 5711 | # endif |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5712 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5713 | } |
| 5714 | } |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5715 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | #endif |
| 5719 | |
| 5720 | |
| 5721 | /* |
| 5722 | * ":help": open a read-only window on a help file |
| 5723 | */ |
| 5724 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5725 | ex_help(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5726 | { |
| 5727 | char_u *arg; |
| 5728 | char_u *tag; |
| 5729 | FILE *helpfd; /* file descriptor of help file */ |
| 5730 | int n; |
| 5731 | int i; |
| 5732 | #ifdef FEAT_WINDOWS |
| 5733 | win_T *wp; |
| 5734 | #endif |
| 5735 | int num_matches; |
| 5736 | char_u **matches; |
| 5737 | char_u *p; |
| 5738 | int empty_fnum = 0; |
| 5739 | int alt_fnum = 0; |
| 5740 | buf_T *buf; |
| 5741 | #ifdef FEAT_MULTI_LANG |
| 5742 | int len; |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5743 | char_u *lang; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5744 | #endif |
Bram Moolenaar | 8f53558 | 2011-09-30 17:30:31 +0200 | [diff] [blame] | 5745 | #ifdef FEAT_FOLDING |
| 5746 | int old_KeyTyped = KeyTyped; |
| 5747 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5748 | |
| 5749 | if (eap != NULL) |
| 5750 | { |
| 5751 | /* |
| 5752 | * A ":help" command ends at the first LF, or at a '|' that is |
| 5753 | * followed by some text. Set nextcmd to the following command. |
| 5754 | */ |
| 5755 | for (arg = eap->arg; *arg; ++arg) |
| 5756 | { |
| 5757 | if (*arg == '\n' || *arg == '\r' |
| 5758 | || (*arg == '|' && arg[1] != NUL && arg[1] != '|')) |
| 5759 | { |
| 5760 | *arg++ = NUL; |
| 5761 | eap->nextcmd = arg; |
| 5762 | break; |
| 5763 | } |
| 5764 | } |
| 5765 | arg = eap->arg; |
| 5766 | |
Bram Moolenaar | 3dbde62 | 2012-04-05 16:05:05 +0200 | [diff] [blame] | 5767 | if (eap->forceit && *arg == NUL && !curbuf->b_help) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5768 | { |
| 5769 | EMSG(_("E478: Don't panic!")); |
| 5770 | return; |
| 5771 | } |
| 5772 | |
| 5773 | if (eap->skip) /* not executing commands */ |
| 5774 | return; |
| 5775 | } |
| 5776 | else |
| 5777 | arg = (char_u *)""; |
| 5778 | |
| 5779 | /* remove trailing blanks */ |
| 5780 | p = arg + STRLEN(arg) - 1; |
| 5781 | while (p > arg && vim_iswhite(*p) && p[-1] != '\\') |
| 5782 | *p-- = NUL; |
| 5783 | |
| 5784 | #ifdef FEAT_MULTI_LANG |
| 5785 | /* Check for a specified language */ |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5786 | lang = check_help_lang(arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5787 | #endif |
| 5788 | |
| 5789 | /* When no argument given go to the index. */ |
| 5790 | if (*arg == NUL) |
| 5791 | arg = (char_u *)"help.txt"; |
| 5792 | |
| 5793 | /* |
| 5794 | * Check if there is a match for the argument. |
| 5795 | */ |
| 5796 | n = find_help_tags(arg, &num_matches, &matches, |
| 5797 | eap != NULL && eap->forceit); |
| 5798 | |
| 5799 | i = 0; |
| 5800 | #ifdef FEAT_MULTI_LANG |
| 5801 | if (n != FAIL && lang != NULL) |
| 5802 | /* Find first item with the requested language. */ |
| 5803 | for (i = 0; i < num_matches; ++i) |
| 5804 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5805 | len = (int)STRLEN(matches[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5806 | if (len > 3 && matches[i][len - 3] == '@' |
| 5807 | && STRICMP(matches[i] + len - 2, lang) == 0) |
| 5808 | break; |
| 5809 | } |
| 5810 | #endif |
| 5811 | if (i >= num_matches || n == FAIL) |
| 5812 | { |
| 5813 | #ifdef FEAT_MULTI_LANG |
| 5814 | if (lang != NULL) |
| 5815 | EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg); |
| 5816 | else |
| 5817 | #endif |
| 5818 | EMSG2(_("E149: Sorry, no help for %s"), arg); |
| 5819 | if (n != FAIL) |
| 5820 | FreeWild(num_matches, matches); |
| 5821 | return; |
| 5822 | } |
| 5823 | |
| 5824 | /* The first match (in the requested language) is the best match. */ |
| 5825 | tag = vim_strsave(matches[i]); |
| 5826 | FreeWild(num_matches, matches); |
| 5827 | |
| 5828 | #ifdef FEAT_GUI |
| 5829 | need_mouse_correct = TRUE; |
| 5830 | #endif |
| 5831 | |
| 5832 | /* |
| 5833 | * Re-use an existing help window or open a new one. |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5834 | * Always open a new one for ":tab help". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5835 | */ |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5836 | if (!curwin->w_buffer->b_help |
| 5837 | #ifdef FEAT_WINDOWS |
| 5838 | || cmdmod.tab != 0 |
| 5839 | #endif |
| 5840 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5841 | { |
| 5842 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5843 | if (cmdmod.tab != 0) |
| 5844 | wp = NULL; |
| 5845 | else |
| 5846 | for (wp = firstwin; wp != NULL; wp = wp->w_next) |
| 5847 | if (wp->w_buffer != NULL && wp->w_buffer->b_help) |
| 5848 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5849 | if (wp != NULL && wp->w_buffer->b_nwindows > 0) |
| 5850 | win_enter(wp, TRUE); |
| 5851 | else |
| 5852 | #endif |
| 5853 | { |
| 5854 | /* |
| 5855 | * There is no help window yet. |
| 5856 | * Try to open the file specified by the "helpfile" option. |
| 5857 | */ |
| 5858 | if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL) |
| 5859 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5860 | smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5861 | goto erret; |
| 5862 | } |
| 5863 | fclose(helpfd); |
| 5864 | |
| 5865 | #ifdef FEAT_WINDOWS |
| 5866 | /* Split off help window; put it at far top if no position |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5867 | * specified, the current window is vertically split and |
| 5868 | * narrow. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5869 | n = WSP_HELP; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5870 | if (cmdmod.split == 0 && curwin->w_width != Columns |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5871 | && curwin->w_width < 80) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5872 | n |= WSP_TOP; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5873 | if (win_split(0, n) == FAIL) |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5874 | goto erret; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5875 | #else |
| 5876 | /* use current window */ |
| 5877 | if (!can_abandon(curbuf, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5878 | goto erret; |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5879 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5880 | |
| 5881 | #ifdef FEAT_WINDOWS |
| 5882 | if (curwin->w_height < p_hh) |
| 5883 | win_setheight((int)p_hh); |
| 5884 | #endif |
| 5885 | |
| 5886 | /* |
| 5887 | * Open help file (do_ecmd() will set b_help flag, readfile() will |
| 5888 | * set b_p_ro flag). |
| 5889 | * Set the alternate file to the previously edited file. |
| 5890 | */ |
| 5891 | alt_fnum = curbuf->b_fnum; |
| 5892 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL, |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 5893 | ECMD_HIDE + ECMD_SET_HELP, |
| 5894 | #ifdef FEAT_WINDOWS |
| 5895 | NULL /* buffer is still open, don't store info */ |
| 5896 | #else |
| 5897 | curwin |
| 5898 | #endif |
| 5899 | ); |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 5900 | if (!cmdmod.keepalt) |
| 5901 | curwin->w_alt_fnum = alt_fnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5902 | empty_fnum = curbuf->b_fnum; |
| 5903 | } |
| 5904 | } |
| 5905 | |
| 5906 | if (!p_im) |
| 5907 | restart_edit = 0; /* don't want insert mode in help file */ |
| 5908 | |
Bram Moolenaar | 8f53558 | 2011-09-30 17:30:31 +0200 | [diff] [blame] | 5909 | #ifdef FEAT_FOLDING |
| 5910 | /* Restore KeyTyped, setting 'filetype=help' may reset it. |
| 5911 | * It is needed for do_tag top open folds under the cursor. */ |
| 5912 | KeyTyped = old_KeyTyped; |
| 5913 | #endif |
| 5914 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5915 | if (tag != NULL) |
| 5916 | do_tag(tag, DT_HELP, 1, FALSE, TRUE); |
| 5917 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5918 | /* Delete the empty buffer if we're not using it. Careful: autocommands |
| 5919 | * may have jumped to another window, check that the buffer is not in a |
| 5920 | * window. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5921 | if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum) |
| 5922 | { |
| 5923 | buf = buflist_findnr(empty_fnum); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5924 | if (buf != NULL && buf->b_nwindows == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5925 | wipe_buffer(buf, TRUE); |
| 5926 | } |
| 5927 | |
| 5928 | /* keep the previous alternate file */ |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 5929 | if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5930 | curwin->w_alt_fnum = alt_fnum; |
| 5931 | |
| 5932 | erret: |
| 5933 | vim_free(tag); |
| 5934 | } |
| 5935 | |
Bram Moolenaar | 5bfa2ed | 2014-09-19 19:39:34 +0200 | [diff] [blame] | 5936 | /* |
Bram Moolenaar | eb21e4c | 2014-09-19 22:05:53 +0200 | [diff] [blame] | 5937 | * ":helpclose": Close one help window |
Bram Moolenaar | 5bfa2ed | 2014-09-19 19:39:34 +0200 | [diff] [blame] | 5938 | */ |
| 5939 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5940 | ex_helpclose(exarg_T *eap UNUSED) |
Bram Moolenaar | 5bfa2ed | 2014-09-19 19:39:34 +0200 | [diff] [blame] | 5941 | { |
Bram Moolenaar | 3fa57e0 | 2014-09-19 22:23:26 +0200 | [diff] [blame] | 5942 | #if defined(FEAT_WINDOWS) |
Bram Moolenaar | 5bfa2ed | 2014-09-19 19:39:34 +0200 | [diff] [blame] | 5943 | win_T *win; |
| 5944 | |
| 5945 | FOR_ALL_WINDOWS(win) |
| 5946 | { |
| 5947 | if (win->w_buffer->b_help) |
| 5948 | { |
| 5949 | win_close(win, FALSE); |
Bram Moolenaar | eb21e4c | 2014-09-19 22:05:53 +0200 | [diff] [blame] | 5950 | return; |
Bram Moolenaar | 5bfa2ed | 2014-09-19 19:39:34 +0200 | [diff] [blame] | 5951 | } |
| 5952 | } |
Bram Moolenaar | 3fa57e0 | 2014-09-19 22:23:26 +0200 | [diff] [blame] | 5953 | #endif |
Bram Moolenaar | 5bfa2ed | 2014-09-19 19:39:34 +0200 | [diff] [blame] | 5954 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5955 | |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5956 | #if defined(FEAT_MULTI_LANG) || defined(PROTO) |
| 5957 | /* |
| 5958 | * In an argument search for a language specifiers in the form "@xx". |
| 5959 | * Changes the "@" to NUL if found, and returns a pointer to "xx". |
| 5960 | * Returns NULL if not found. |
| 5961 | */ |
| 5962 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5963 | check_help_lang(char_u *arg) |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5964 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5965 | int len = (int)STRLEN(arg); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5966 | |
| 5967 | if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2]) |
| 5968 | && ASCII_ISALPHA(arg[len - 1])) |
| 5969 | { |
| 5970 | arg[len - 3] = NUL; /* remove the '@' */ |
| 5971 | return arg + len - 2; |
| 5972 | } |
| 5973 | return NULL; |
| 5974 | } |
| 5975 | #endif |
| 5976 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5977 | /* |
| 5978 | * Return a heuristic indicating how well the given string matches. The |
| 5979 | * smaller the number, the better the match. This is the order of priorities, |
| 5980 | * from best match to worst match: |
| 5981 | * - Match with least alpha-numeric characters is better. |
| 5982 | * - Match with least total characters is better. |
| 5983 | * - Match towards the start is better. |
| 5984 | * - Match starting with "+" is worse (feature instead of command) |
| 5985 | * Assumption is made that the matched_string passed has already been found to |
| 5986 | * match some string for which help is requested. webb. |
| 5987 | */ |
| 5988 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5989 | help_heuristic( |
| 5990 | char_u *matched_string, |
| 5991 | int offset, /* offset for match */ |
| 5992 | int wrong_case) /* no matching case */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5993 | { |
| 5994 | int num_letters; |
| 5995 | char_u *p; |
| 5996 | |
| 5997 | num_letters = 0; |
| 5998 | for (p = matched_string; *p; p++) |
| 5999 | if (ASCII_ISALNUM(*p)) |
| 6000 | num_letters++; |
| 6001 | |
| 6002 | /* |
| 6003 | * Multiply the number of letters by 100 to give it a much bigger |
| 6004 | * weighting than the number of characters. |
| 6005 | * If there only is a match while ignoring case, add 5000. |
| 6006 | * If the match starts in the middle of a word, add 10000 to put it |
| 6007 | * somewhere in the last half. |
| 6008 | * If the match is more than 2 chars from the start, multiply by 200 to |
| 6009 | * put it after matches at the start. |
| 6010 | */ |
| 6011 | if (ASCII_ISALNUM(matched_string[offset]) && offset > 0 |
| 6012 | && ASCII_ISALNUM(matched_string[offset - 1])) |
| 6013 | offset += 10000; |
| 6014 | else if (offset > 2) |
| 6015 | offset *= 200; |
| 6016 | if (wrong_case) |
| 6017 | offset += 5000; |
| 6018 | /* Features are less interesting than the subjects themselves, but "+" |
| 6019 | * alone is not a feature. */ |
| 6020 | if (matched_string[0] == '+' && matched_string[1] != NUL) |
| 6021 | offset += 100; |
| 6022 | return (int)(100 * num_letters + STRLEN(matched_string) + offset); |
| 6023 | } |
| 6024 | |
| 6025 | /* |
| 6026 | * Compare functions for qsort() below, that checks the help heuristics number |
| 6027 | * that has been put after the tagname by find_tags(). |
| 6028 | */ |
| 6029 | static int |
| 6030 | #ifdef __BORLANDC__ |
| 6031 | _RTLENTRYF |
| 6032 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6033 | help_compare(const void *s1, const void *s2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6034 | { |
| 6035 | char *p1; |
| 6036 | char *p2; |
| 6037 | |
| 6038 | p1 = *(char **)s1 + strlen(*(char **)s1) + 1; |
| 6039 | p2 = *(char **)s2 + strlen(*(char **)s2) + 1; |
| 6040 | return strcmp(p1, p2); |
| 6041 | } |
| 6042 | |
| 6043 | /* |
| 6044 | * Find all help tags matching "arg", sort them and return in matches[], with |
| 6045 | * the number of matches in num_matches. |
| 6046 | * The matches will be sorted with a "best" match algorithm. |
| 6047 | * When "keep_lang" is TRUE try keeping the language of the current buffer. |
| 6048 | */ |
| 6049 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6050 | find_help_tags( |
| 6051 | char_u *arg, |
| 6052 | int *num_matches, |
| 6053 | char_u ***matches, |
| 6054 | int keep_lang) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6055 | { |
| 6056 | char_u *s, *d; |
| 6057 | int i; |
| 6058 | static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*", |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6059 | "/*", "/\\*", "\"*", "**", |
Bram Moolenaar | c528b1d | 2013-08-03 13:41:15 +0200 | [diff] [blame] | 6060 | "cpo-*", "/\\(\\)", "/\\%(\\)", |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6061 | "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", |
Bram Moolenaar | f4630b6 | 2005-05-20 21:31:17 +0000 | [diff] [blame] | 6062 | "/\\?", "/\\z(\\)", "\\=", ":s\\=", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6063 | "[count]", "[quotex]", "[range]", |
Bram Moolenaar | c467d9b | 2014-02-11 12:15:43 +0100 | [diff] [blame] | 6064 | "[pattern]", "\\|", "\\%$", |
| 6065 | "s/\\~", "s/\\U", "s/\\L", |
| 6066 | "s/\\1", "s/\\2", "s/\\3", "s/\\9"}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6067 | static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star", |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6068 | "/star", "/\\\\star", "quotestar", "starstar", |
Bram Moolenaar | c528b1d | 2013-08-03 13:41:15 +0200 | [diff] [blame] | 6069 | "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)", |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6070 | "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", |
Bram Moolenaar | f4630b6 | 2005-05-20 21:31:17 +0000 | [diff] [blame] | 6071 | "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6072 | "\\[count]", "\\[quotex]", "\\[range]", |
Bram Moolenaar | c467d9b | 2014-02-11 12:15:43 +0100 | [diff] [blame] | 6073 | "\\[pattern]", "\\\\bar", "/\\\\%\\$", |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6074 | "s/\\\\\\~", "s/\\\\U", "s/\\\\L", |
Bram Moolenaar | c467d9b | 2014-02-11 12:15:43 +0100 | [diff] [blame] | 6075 | "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6076 | int flags; |
| 6077 | |
| 6078 | d = IObuff; /* assume IObuff is long enough! */ |
| 6079 | |
| 6080 | /* |
| 6081 | * Recognize a few exceptions to the rule. Some strings that contain '*' |
| 6082 | * with "star". Otherwise '*' is recognized as a wildcard. |
| 6083 | */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6084 | for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6085 | if (STRCMP(arg, mtable[i]) == 0) |
| 6086 | { |
| 6087 | STRCPY(d, rtable[i]); |
| 6088 | break; |
| 6089 | } |
| 6090 | |
| 6091 | if (i < 0) /* no match in table */ |
| 6092 | { |
| 6093 | /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched. |
| 6094 | * Also replace "\%^" and "\%(", they match every tag too. |
| 6095 | * Also "\zs", "\z1", etc. |
| 6096 | * Also "\@<", "\@=", "\@<=", etc. |
| 6097 | * And also "\_$" and "\_^". */ |
| 6098 | if (arg[0] == '\\' |
| 6099 | && ((arg[1] != NUL && arg[2] == NUL) |
| 6100 | || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL |
| 6101 | && arg[2] != NUL))) |
| 6102 | { |
| 6103 | STRCPY(d, "/\\\\"); |
| 6104 | STRCPY(d + 3, arg + 1); |
| 6105 | /* Check for "/\\_$", should be "/\\_\$" */ |
| 6106 | if (d[3] == '_' && d[4] == '$') |
| 6107 | STRCPY(d + 4, "\\$"); |
| 6108 | } |
| 6109 | else |
| 6110 | { |
Bram Moolenaar | 7c0a86b | 2012-09-05 15:15:07 +0200 | [diff] [blame] | 6111 | /* Replace: |
| 6112 | * "[:...:]" with "\[:...:]" |
| 6113 | * "[++...]" with "\[++...]" |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6114 | * "\{" with "\\{" -- matching "} \}" |
Bram Moolenaar | 7c0a86b | 2012-09-05 15:15:07 +0200 | [diff] [blame] | 6115 | */ |
| 6116 | if ((arg[0] == '[' && (arg[1] == ':' |
| 6117 | || (arg[1] == '+' && arg[2] == '+'))) |
| 6118 | || (arg[0] == '\\' && arg[1] == '{')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6119 | *d++ = '\\'; |
| 6120 | |
Bram Moolenaar | 00f9e0d | 2016-03-15 13:44:12 +0100 | [diff] [blame] | 6121 | /* |
| 6122 | * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'. |
| 6123 | */ |
| 6124 | if (*arg == '(' && arg[1] == '\'') |
| 6125 | arg++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6126 | for (s = arg; *s; ++s) |
| 6127 | { |
| 6128 | /* |
| 6129 | * Replace "|" with "bar" and '"' with "quote" to match the name of |
| 6130 | * the tags for these commands. |
| 6131 | * Replace "*" with ".*" and "?" with "." to match command line |
| 6132 | * completion. |
| 6133 | * Insert a backslash before '~', '$' and '.' to avoid their |
| 6134 | * special meaning. |
| 6135 | */ |
| 6136 | if (d - IObuff > IOSIZE - 10) /* getting too long!? */ |
| 6137 | break; |
| 6138 | switch (*s) |
| 6139 | { |
| 6140 | case '|': STRCPY(d, "bar"); |
| 6141 | d += 3; |
| 6142 | continue; |
| 6143 | case '"': STRCPY(d, "quote"); |
| 6144 | d += 5; |
| 6145 | continue; |
| 6146 | case '*': *d++ = '.'; |
| 6147 | break; |
| 6148 | case '?': *d++ = '.'; |
| 6149 | continue; |
| 6150 | case '$': |
| 6151 | case '.': |
| 6152 | case '~': *d++ = '\\'; |
| 6153 | break; |
| 6154 | } |
| 6155 | |
| 6156 | /* |
| 6157 | * Replace "^x" by "CTRL-X". Don't do this for "^_" to make |
| 6158 | * ":help i_^_CTRL-D" work. |
| 6159 | * Insert '-' before and after "CTRL-X" when applicable. |
| 6160 | */ |
| 6161 | if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1]) |
| 6162 | || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL))) |
| 6163 | { |
Bram Moolenaar | d82db60 | 2013-08-07 15:24:41 +0200 | [diff] [blame] | 6164 | if (d > IObuff && d[-1] != '_' && d[-1] != '\\') |
| 6165 | *d++ = '_'; /* prepend a '_' to make x_CTRL-x */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6166 | STRCPY(d, "CTRL-"); |
| 6167 | d += 5; |
| 6168 | if (*s < ' ') |
| 6169 | { |
| 6170 | #ifdef EBCDIC |
| 6171 | *d++ = CtrlChar(*s); |
| 6172 | #else |
| 6173 | *d++ = *s + '@'; |
| 6174 | #endif |
| 6175 | if (d[-1] == '\\') |
| 6176 | *d++ = '\\'; /* double a backslash */ |
| 6177 | } |
| 6178 | else |
| 6179 | *d++ = *++s; |
| 6180 | if (s[1] != NUL && s[1] != '_') |
| 6181 | *d++ = '_'; /* append a '_' */ |
| 6182 | continue; |
| 6183 | } |
| 6184 | else if (*s == '^') /* "^" or "CTRL-^" or "^_" */ |
| 6185 | *d++ = '\\'; |
| 6186 | |
| 6187 | /* |
| 6188 | * Insert a backslash before a backslash after a slash, for search |
| 6189 | * pattern tags: "/\|" --> "/\\|". |
| 6190 | */ |
| 6191 | else if (s[0] == '\\' && s[1] != '\\' |
| 6192 | && *arg == '/' && s == arg + 1) |
| 6193 | *d++ = '\\'; |
| 6194 | |
| 6195 | /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in |
| 6196 | * "CTRL-\_CTRL-N" */ |
| 6197 | if (STRNICMP(s, "CTRL-\\_", 7) == 0) |
| 6198 | { |
| 6199 | STRCPY(d, "CTRL-\\\\"); |
| 6200 | d += 7; |
| 6201 | s += 6; |
| 6202 | } |
| 6203 | |
| 6204 | *d++ = *s; |
| 6205 | |
| 6206 | /* |
Bram Moolenaar | 81edd17 | 2016-04-14 13:51:37 +0200 | [diff] [blame] | 6207 | * If tag contains "({" or "([", tag terminates at the "(". |
| 6208 | * This is for help on functions, e.g.: abs({expr}). |
| 6209 | */ |
| 6210 | if (*s == '(' && (s[1] == '{' || s[1] =='[')) |
| 6211 | break; |
| 6212 | |
| 6213 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6214 | * If tag starts with ', toss everything after a second '. Fixes |
| 6215 | * CTRL-] on 'option'. (would include the trailing '.'). |
| 6216 | */ |
| 6217 | if (*s == '\'' && s > arg && *arg == '\'') |
| 6218 | break; |
| 6219 | } |
| 6220 | *d = NUL; |
Bram Moolenaar | 720ce53 | 2012-04-25 12:57:28 +0200 | [diff] [blame] | 6221 | |
| 6222 | if (*IObuff == '`') |
| 6223 | { |
| 6224 | if (d > IObuff + 2 && d[-1] == '`') |
| 6225 | { |
| 6226 | /* remove the backticks from `command` */ |
| 6227 | mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); |
| 6228 | d[-2] = NUL; |
| 6229 | } |
| 6230 | else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',') |
| 6231 | { |
| 6232 | /* remove the backticks and comma from `command`, */ |
| 6233 | mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); |
| 6234 | d[-3] = NUL; |
| 6235 | } |
| 6236 | else if (d > IObuff + 4 && d[-3] == '`' |
| 6237 | && d[-2] == '\\' && d[-1] == '.') |
| 6238 | { |
| 6239 | /* remove the backticks and dot from `command`\. */ |
| 6240 | mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); |
| 6241 | d[-4] = NUL; |
| 6242 | } |
| 6243 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6244 | } |
| 6245 | } |
| 6246 | |
| 6247 | *matches = (char_u **)""; |
| 6248 | *num_matches = 0; |
| 6249 | flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE; |
| 6250 | if (keep_lang) |
| 6251 | flags |= TAG_KEEP_LANG; |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 6252 | if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6253 | && *num_matches > 0) |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 6254 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6255 | /* Sort the matches found on the heuristic number that is after the |
| 6256 | * tag name. */ |
| 6257 | qsort((void *)*matches, (size_t)*num_matches, |
| 6258 | sizeof(char_u *), help_compare); |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 6259 | /* Delete more than TAG_MANY to reduce the size of the listing. */ |
| 6260 | while (*num_matches > TAG_MANY) |
| 6261 | vim_free((*matches)[--*num_matches]); |
| 6262 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6263 | return OK; |
| 6264 | } |
| 6265 | |
| 6266 | /* |
Bram Moolenaar | 54fb438 | 2014-11-12 19:28:16 +0100 | [diff] [blame] | 6267 | * Called when starting to edit a buffer for a help file. |
| 6268 | */ |
| 6269 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6270 | prepare_help_buffer(void) |
Bram Moolenaar | 54fb438 | 2014-11-12 19:28:16 +0100 | [diff] [blame] | 6271 | { |
| 6272 | char_u *p; |
| 6273 | |
| 6274 | curbuf->b_help = TRUE; |
| 6275 | #ifdef FEAT_QUICKFIX |
| 6276 | set_string_option_direct((char_u *)"buftype", -1, |
| 6277 | (char_u *)"help", OPT_FREE|OPT_LOCAL, 0); |
| 6278 | #endif |
| 6279 | |
| 6280 | /* |
| 6281 | * Always set these options after jumping to a help tag, because the |
| 6282 | * user may have an autocommand that gets in the way. |
| 6283 | * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and |
| 6284 | * latin1 word characters (for translated help files). |
| 6285 | * Only set it when needed, buf_init_chartab() is some work. |
| 6286 | */ |
| 6287 | p = |
| 6288 | #ifdef EBCDIC |
| 6289 | (char_u *)"65-255,^*,^|,^\""; |
| 6290 | #else |
| 6291 | (char_u *)"!-~,^*,^|,^\",192-255"; |
| 6292 | #endif |
| 6293 | if (STRCMP(curbuf->b_p_isk, p) != 0) |
| 6294 | { |
| 6295 | set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0); |
| 6296 | check_buf_options(curbuf); |
| 6297 | (void)buf_init_chartab(curbuf, FALSE); |
| 6298 | } |
| 6299 | |
Bram Moolenaar | 0b10541 | 2014-11-30 13:34:23 +0100 | [diff] [blame] | 6300 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 54fb438 | 2014-11-12 19:28:16 +0100 | [diff] [blame] | 6301 | /* Don't use the global foldmethod.*/ |
| 6302 | set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual", |
| 6303 | OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 0b10541 | 2014-11-30 13:34:23 +0100 | [diff] [blame] | 6304 | #endif |
Bram Moolenaar | 54fb438 | 2014-11-12 19:28:16 +0100 | [diff] [blame] | 6305 | |
| 6306 | curbuf->b_p_ts = 8; /* 'tabstop' is 8 */ |
| 6307 | curwin->w_p_list = FALSE; /* no list mode */ |
| 6308 | |
| 6309 | curbuf->b_p_ma = FALSE; /* not modifiable */ |
| 6310 | curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */ |
| 6311 | curwin->w_p_nu = 0; /* no line numbers */ |
| 6312 | curwin->w_p_rnu = 0; /* no relative line numbers */ |
| 6313 | RESET_BINDING(curwin); /* no scroll or cursor binding */ |
| 6314 | #ifdef FEAT_ARABIC |
| 6315 | curwin->w_p_arab = FALSE; /* no arabic mode */ |
| 6316 | #endif |
| 6317 | #ifdef FEAT_RIGHTLEFT |
| 6318 | curwin->w_p_rl = FALSE; /* help window is left-to-right */ |
| 6319 | #endif |
| 6320 | #ifdef FEAT_FOLDING |
| 6321 | curwin->w_p_fen = FALSE; /* No folding in the help window */ |
| 6322 | #endif |
| 6323 | #ifdef FEAT_DIFF |
| 6324 | curwin->w_p_diff = FALSE; /* No 'diff' */ |
| 6325 | #endif |
| 6326 | #ifdef FEAT_SPELL |
| 6327 | curwin->w_p_spell = FALSE; /* No spell checking */ |
| 6328 | #endif |
| 6329 | |
| 6330 | set_buflisted(FALSE); |
| 6331 | } |
| 6332 | |
| 6333 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6334 | * After reading a help file: May cleanup a help buffer when syntax |
| 6335 | * highlighting is not used. |
| 6336 | */ |
| 6337 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6338 | fix_help_buffer(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6339 | { |
| 6340 | linenr_T lnum; |
| 6341 | char_u *line; |
| 6342 | int in_example = FALSE; |
| 6343 | int len; |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6344 | char_u *fname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6345 | char_u *p; |
| 6346 | char_u *rt; |
| 6347 | int mustfree; |
| 6348 | |
| 6349 | /* set filetype to "help". */ |
| 6350 | set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL); |
| 6351 | |
| 6352 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 6353 | if (!syntax_present(curwin)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6354 | #endif |
| 6355 | { |
| 6356 | for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) |
| 6357 | { |
| 6358 | line = ml_get_buf(curbuf, lnum, FALSE); |
| 6359 | len = (int)STRLEN(line); |
| 6360 | if (in_example && len > 0 && !vim_iswhite(line[0])) |
| 6361 | { |
| 6362 | /* End of example: non-white or '<' in first column. */ |
| 6363 | if (line[0] == '<') |
| 6364 | { |
| 6365 | /* blank-out a '<' in the first column */ |
| 6366 | line = ml_get_buf(curbuf, lnum, TRUE); |
| 6367 | line[0] = ' '; |
| 6368 | } |
| 6369 | in_example = FALSE; |
| 6370 | } |
| 6371 | if (!in_example && len > 0) |
| 6372 | { |
| 6373 | if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' ')) |
| 6374 | { |
| 6375 | /* blank-out a '>' in the last column (start of example) */ |
| 6376 | line = ml_get_buf(curbuf, lnum, TRUE); |
| 6377 | line[len - 1] = ' '; |
| 6378 | in_example = TRUE; |
| 6379 | } |
| 6380 | else if (line[len - 1] == '~') |
| 6381 | { |
| 6382 | /* blank-out a '~' at the end of line (header marker) */ |
| 6383 | line = ml_get_buf(curbuf, lnum, TRUE); |
| 6384 | line[len - 1] = ' '; |
| 6385 | } |
| 6386 | } |
| 6387 | } |
| 6388 | } |
| 6389 | |
| 6390 | /* |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6391 | * In the "help.txt" and "help.abx" file, add the locally added help |
| 6392 | * files. This uses the very first line in the help file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6393 | */ |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6394 | fname = gettail(curbuf->b_fname); |
| 6395 | if (fnamecmp(fname, "help.txt") == 0 |
| 6396 | #ifdef FEAT_MULTI_LANG |
| 6397 | || (fnamencmp(fname, "help.", 5) == 0 |
| 6398 | && ASCII_ISALPHA(fname[5]) |
| 6399 | && ASCII_ISALPHA(fname[6]) |
| 6400 | && TOLOWER_ASC(fname[7]) == 'x' |
| 6401 | && fname[8] == NUL) |
| 6402 | #endif |
| 6403 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6404 | { |
| 6405 | for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum) |
| 6406 | { |
| 6407 | line = ml_get_buf(curbuf, lnum, FALSE); |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6408 | if (strstr((char *)line, "*local-additions*") == NULL) |
| 6409 | continue; |
| 6410 | |
| 6411 | /* Go through all directories in 'runtimepath', skipping |
| 6412 | * $VIMRUNTIME. */ |
| 6413 | p = p_rtp; |
| 6414 | while (*p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6415 | { |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6416 | copy_option_part(&p, NameBuff, MAXPATHL, ","); |
| 6417 | mustfree = FALSE; |
| 6418 | rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree); |
| 6419 | if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6420 | { |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6421 | int fcount; |
| 6422 | char_u **fnames; |
| 6423 | FILE *fd; |
| 6424 | char_u *s; |
| 6425 | int fi; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6426 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6427 | vimconv_T vc; |
| 6428 | char_u *cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6429 | #endif |
| 6430 | |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6431 | /* Find all "doc/ *.txt" files in this directory. */ |
| 6432 | add_pathsep(NameBuff); |
| 6433 | #ifdef FEAT_MULTI_LANG |
| 6434 | STRCAT(NameBuff, "doc/*.??[tx]"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6435 | #else |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6436 | STRCAT(NameBuff, "doc/*.txt"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6437 | #endif |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6438 | if (gen_expand_wildcards(1, &NameBuff, &fcount, |
| 6439 | &fnames, EW_FILE|EW_SILENT) == OK |
| 6440 | && fcount > 0) |
| 6441 | { |
| 6442 | #ifdef FEAT_MULTI_LANG |
| 6443 | int i1; |
| 6444 | int i2; |
| 6445 | char_u *f1; |
| 6446 | char_u *f2; |
| 6447 | char_u *t1; |
| 6448 | char_u *e1; |
| 6449 | char_u *e2; |
| 6450 | |
| 6451 | /* If foo.abx is found use it instead of foo.txt in |
| 6452 | * the same directory. */ |
| 6453 | for (i1 = 0; i1 < fcount; ++i1) |
| 6454 | { |
| 6455 | for (i2 = 0; i2 < fcount; ++i2) |
| 6456 | { |
| 6457 | if (i1 == i2) |
| 6458 | continue; |
| 6459 | if (fnames[i1] == NULL || fnames[i2] == NULL) |
| 6460 | continue; |
| 6461 | f1 = fnames[i1]; |
| 6462 | f2 = fnames[i2]; |
| 6463 | t1 = gettail(f1); |
| 6464 | if (fnamencmp(f1, f2, t1 - f1) != 0) |
| 6465 | continue; |
| 6466 | e1 = vim_strrchr(t1, '.'); |
| 6467 | e2 = vim_strrchr(gettail(f2), '.'); |
| 6468 | if (e1 == NUL || e2 == NUL) |
| 6469 | continue; |
| 6470 | if (fnamecmp(e1, ".txt") != 0 |
| 6471 | && fnamecmp(e1, fname + 4) != 0) |
| 6472 | { |
| 6473 | /* Not .txt and not .abx, remove it. */ |
| 6474 | vim_free(fnames[i1]); |
| 6475 | fnames[i1] = NULL; |
| 6476 | continue; |
| 6477 | } |
| 6478 | if (fnamencmp(f1, f2, e1 - f1) != 0) |
| 6479 | continue; |
| 6480 | if (fnamecmp(e1, ".txt") == 0 |
| 6481 | && fnamecmp(e2, fname + 4) == 0) |
| 6482 | { |
| 6483 | /* use .abx instead of .txt */ |
| 6484 | vim_free(fnames[i1]); |
| 6485 | fnames[i1] = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6486 | } |
| 6487 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6488 | } |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6489 | #endif |
| 6490 | for (fi = 0; fi < fcount; ++fi) |
| 6491 | { |
| 6492 | if (fnames[fi] == NULL) |
| 6493 | continue; |
| 6494 | fd = mch_fopen((char *)fnames[fi], "r"); |
| 6495 | if (fd != NULL) |
| 6496 | { |
| 6497 | vim_fgets(IObuff, IOSIZE, fd); |
| 6498 | if (IObuff[0] == '*' |
| 6499 | && (s = vim_strchr(IObuff + 1, '*')) |
| 6500 | != NULL) |
| 6501 | { |
| 6502 | #ifdef FEAT_MBYTE |
| 6503 | int this_utf = MAYBE; |
| 6504 | #endif |
| 6505 | /* Change tag definition to a |
| 6506 | * reference and remove <CR>/<NL>. */ |
| 6507 | IObuff[0] = '|'; |
| 6508 | *s = '|'; |
| 6509 | while (*s != NUL) |
| 6510 | { |
| 6511 | if (*s == '\r' || *s == '\n') |
| 6512 | *s = NUL; |
| 6513 | #ifdef FEAT_MBYTE |
| 6514 | /* The text is utf-8 when a byte |
| 6515 | * above 127 is found and no |
| 6516 | * illegal byte sequence is found. |
| 6517 | */ |
| 6518 | if (*s >= 0x80 && this_utf != FALSE) |
| 6519 | { |
| 6520 | int l; |
| 6521 | |
| 6522 | this_utf = TRUE; |
| 6523 | l = utf_ptr2len(s); |
| 6524 | if (l == 1) |
| 6525 | this_utf = FALSE; |
| 6526 | s += l - 1; |
| 6527 | } |
| 6528 | #endif |
| 6529 | ++s; |
| 6530 | } |
| 6531 | #ifdef FEAT_MBYTE |
| 6532 | /* The help file is latin1 or utf-8; |
| 6533 | * conversion to the current |
| 6534 | * 'encoding' may be required. */ |
| 6535 | vc.vc_type = CONV_NONE; |
| 6536 | convert_setup(&vc, (char_u *)( |
| 6537 | this_utf == TRUE ? "utf-8" |
| 6538 | : "latin1"), p_enc); |
| 6539 | if (vc.vc_type == CONV_NONE) |
| 6540 | /* No conversion needed. */ |
| 6541 | cp = IObuff; |
| 6542 | else |
| 6543 | { |
| 6544 | /* Do the conversion. If it fails |
| 6545 | * use the unconverted text. */ |
| 6546 | cp = string_convert(&vc, IObuff, |
| 6547 | NULL); |
| 6548 | if (cp == NULL) |
| 6549 | cp = IObuff; |
| 6550 | } |
| 6551 | convert_setup(&vc, NULL, NULL); |
| 6552 | |
| 6553 | ml_append(lnum, cp, (colnr_T)0, FALSE); |
| 6554 | if (cp != IObuff) |
| 6555 | vim_free(cp); |
| 6556 | #else |
| 6557 | ml_append(lnum, IObuff, (colnr_T)0, |
| 6558 | FALSE); |
| 6559 | #endif |
| 6560 | ++lnum; |
| 6561 | } |
| 6562 | fclose(fd); |
| 6563 | } |
| 6564 | } |
| 6565 | FreeWild(fcount, fnames); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6566 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6567 | } |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6568 | if (mustfree) |
| 6569 | vim_free(rt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6570 | } |
Bram Moolenaar | 667b4d2 | 2011-10-20 18:17:42 +0200 | [diff] [blame] | 6571 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6572 | } |
| 6573 | } |
| 6574 | } |
| 6575 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6576 | /* |
| 6577 | * ":exusage" |
| 6578 | */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6579 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6580 | ex_exusage(exarg_T *eap UNUSED) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6581 | { |
| 6582 | do_cmdline_cmd((char_u *)"help ex-cmd-index"); |
| 6583 | } |
| 6584 | |
| 6585 | /* |
| 6586 | * ":viusage" |
| 6587 | */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6588 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6589 | ex_viusage(exarg_T *eap UNUSED) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 6590 | { |
| 6591 | do_cmdline_cmd((char_u *)"help normal-index"); |
| 6592 | } |
| 6593 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6594 | /* |
Bram Moolenaar | 6bef530 | 2016-03-12 21:28:26 +0100 | [diff] [blame] | 6595 | * Generate tags in one help directory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6596 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6597 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6598 | helptags_one( |
| 6599 | char_u *dir, /* doc directory */ |
| 6600 | char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */ |
| 6601 | char_u *tagfname, /* "tags" for English, "tags-fr" for French. */ |
| 6602 | int add_help_tags) /* add "help-tags" tag */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6603 | { |
| 6604 | FILE *fd_tags; |
| 6605 | FILE *fd; |
| 6606 | garray_T ga; |
| 6607 | int filecount; |
| 6608 | char_u **files; |
| 6609 | char_u *p1, *p2; |
| 6610 | int fi; |
| 6611 | char_u *s; |
| 6612 | int i; |
| 6613 | char_u *fname; |
Bram Moolenaar | 442b5c4 | 2012-11-28 16:06:22 +0100 | [diff] [blame] | 6614 | int dirlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6615 | # ifdef FEAT_MBYTE |
| 6616 | int utf8 = MAYBE; |
| 6617 | int this_utf8; |
| 6618 | int firstline; |
| 6619 | int mix = FALSE; /* detected mixed encodings */ |
| 6620 | # endif |
| 6621 | |
| 6622 | /* |
| 6623 | * Find all *.txt files. |
| 6624 | */ |
Bram Moolenaar | 862cfa3 | 2012-11-29 20:10:00 +0100 | [diff] [blame] | 6625 | dirlen = (int)STRLEN(dir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6626 | STRCPY(NameBuff, dir); |
Bram Moolenaar | 442b5c4 | 2012-11-28 16:06:22 +0100 | [diff] [blame] | 6627 | STRCAT(NameBuff, "/**/*"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6628 | STRCAT(NameBuff, ext); |
| 6629 | if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, |
| 6630 | EW_FILE|EW_SILENT) == FAIL |
| 6631 | || filecount == 0) |
| 6632 | { |
| 6633 | if (!got_int) |
| 6634 | EMSG2("E151: No match: %s", NameBuff); |
| 6635 | return; |
| 6636 | } |
| 6637 | |
| 6638 | /* |
| 6639 | * Open the tags file for writing. |
| 6640 | * Do this before scanning through all the files. |
| 6641 | */ |
| 6642 | STRCPY(NameBuff, dir); |
| 6643 | add_pathsep(NameBuff); |
| 6644 | STRCAT(NameBuff, tagfname); |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 6645 | fd_tags = mch_fopen((char *)NameBuff, "w"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6646 | if (fd_tags == NULL) |
| 6647 | { |
| 6648 | EMSG2(_("E152: Cannot open %s for writing"), NameBuff); |
| 6649 | FreeWild(filecount, files); |
| 6650 | return; |
| 6651 | } |
| 6652 | |
| 6653 | /* |
Bram Moolenaar | 426e5c9 | 2008-01-11 20:02:02 +0000 | [diff] [blame] | 6654 | * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc" |
| 6655 | * add the "help-tags" tag. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6656 | */ |
| 6657 | ga_init2(&ga, (int)sizeof(char_u *), 100); |
Bram Moolenaar | 426e5c9 | 2008-01-11 20:02:02 +0000 | [diff] [blame] | 6658 | if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc", |
| 6659 | dir, FALSE) == FPC_SAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6660 | { |
| 6661 | if (ga_grow(&ga, 1) == FAIL) |
| 6662 | got_int = TRUE; |
| 6663 | else |
| 6664 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6665 | s = alloc(18 + (unsigned)STRLEN(tagfname)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6666 | if (s == NULL) |
| 6667 | got_int = TRUE; |
| 6668 | else |
| 6669 | { |
| 6670 | sprintf((char *)s, "help-tags\t%s\t1\n", tagfname); |
| 6671 | ((char_u **)ga.ga_data)[ga.ga_len] = s; |
| 6672 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6673 | } |
| 6674 | } |
| 6675 | } |
| 6676 | |
| 6677 | /* |
| 6678 | * Go over all the files and extract the tags. |
| 6679 | */ |
| 6680 | for (fi = 0; fi < filecount && !got_int; ++fi) |
| 6681 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 6682 | fd = mch_fopen((char *)files[fi], "r"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6683 | if (fd == NULL) |
| 6684 | { |
| 6685 | EMSG2(_("E153: Unable to open %s for reading"), files[fi]); |
| 6686 | continue; |
| 6687 | } |
Bram Moolenaar | 442b5c4 | 2012-11-28 16:06:22 +0100 | [diff] [blame] | 6688 | fname = files[fi] + dirlen + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6689 | |
| 6690 | # ifdef FEAT_MBYTE |
| 6691 | firstline = TRUE; |
| 6692 | # endif |
| 6693 | while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) |
| 6694 | { |
| 6695 | # ifdef FEAT_MBYTE |
| 6696 | if (firstline) |
| 6697 | { |
| 6698 | /* Detect utf-8 file by a non-ASCII char in the first line. */ |
| 6699 | this_utf8 = MAYBE; |
| 6700 | for (s = IObuff; *s != NUL; ++s) |
| 6701 | if (*s >= 0x80) |
| 6702 | { |
| 6703 | int l; |
| 6704 | |
| 6705 | this_utf8 = TRUE; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6706 | l = utf_ptr2len(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6707 | if (l == 1) |
| 6708 | { |
| 6709 | /* Illegal UTF-8 byte sequence. */ |
| 6710 | this_utf8 = FALSE; |
| 6711 | break; |
| 6712 | } |
| 6713 | s += l - 1; |
| 6714 | } |
| 6715 | if (this_utf8 == MAYBE) /* only ASCII characters found */ |
| 6716 | this_utf8 = FALSE; |
| 6717 | if (utf8 == MAYBE) /* first file */ |
| 6718 | utf8 = this_utf8; |
| 6719 | else if (utf8 != this_utf8) |
| 6720 | { |
| 6721 | EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]); |
| 6722 | mix = !got_int; |
| 6723 | got_int = TRUE; |
| 6724 | } |
| 6725 | firstline = FALSE; |
| 6726 | } |
| 6727 | # endif |
| 6728 | p1 = vim_strchr(IObuff, '*'); /* find first '*' */ |
| 6729 | while (p1 != NULL) |
| 6730 | { |
Bram Moolenaar | a0149c7 | 2012-05-18 16:24:11 +0200 | [diff] [blame] | 6731 | /* Use vim_strbyte() instead of vim_strchr() so that when |
| 6732 | * 'encoding' is dbcs it still works, don't find '*' in the |
| 6733 | * second byte. */ |
| 6734 | p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6735 | if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */ |
| 6736 | { |
| 6737 | for (s = p1 + 1; s < p2; ++s) |
| 6738 | if (*s == ' ' || *s == '\t' || *s == '|') |
| 6739 | break; |
| 6740 | |
| 6741 | /* |
| 6742 | * Only accept a *tag* when it consists of valid |
| 6743 | * characters, there is white space before it and is |
| 6744 | * followed by a white character or end-of-line. |
| 6745 | */ |
| 6746 | if (s == p2 |
| 6747 | && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t') |
| 6748 | && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL |
| 6749 | || s[1] == '\0')) |
| 6750 | { |
| 6751 | *p2 = '\0'; |
| 6752 | ++p1; |
| 6753 | if (ga_grow(&ga, 1) == FAIL) |
| 6754 | { |
| 6755 | got_int = TRUE; |
| 6756 | break; |
| 6757 | } |
| 6758 | s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2)); |
| 6759 | if (s == NULL) |
| 6760 | { |
| 6761 | got_int = TRUE; |
| 6762 | break; |
| 6763 | } |
| 6764 | ((char_u **)ga.ga_data)[ga.ga_len] = s; |
| 6765 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6766 | sprintf((char *)s, "%s\t%s", p1, fname); |
| 6767 | |
| 6768 | /* find next '*' */ |
| 6769 | p2 = vim_strchr(p2 + 1, '*'); |
| 6770 | } |
| 6771 | } |
| 6772 | p1 = p2; |
| 6773 | } |
| 6774 | line_breakcheck(); |
| 6775 | } |
| 6776 | |
| 6777 | fclose(fd); |
| 6778 | } |
| 6779 | |
| 6780 | FreeWild(filecount, files); |
| 6781 | |
| 6782 | if (!got_int) |
| 6783 | { |
| 6784 | /* |
| 6785 | * Sort the tags. |
| 6786 | */ |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 6787 | if (ga.ga_data != NULL) |
| 6788 | sort_strings((char_u **)ga.ga_data, ga.ga_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6789 | |
| 6790 | /* |
| 6791 | * Check for duplicates. |
| 6792 | */ |
| 6793 | for (i = 1; i < ga.ga_len; ++i) |
| 6794 | { |
| 6795 | p1 = ((char_u **)ga.ga_data)[i - 1]; |
| 6796 | p2 = ((char_u **)ga.ga_data)[i]; |
| 6797 | while (*p1 == *p2) |
| 6798 | { |
| 6799 | if (*p2 == '\t') |
| 6800 | { |
| 6801 | *p2 = NUL; |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 6802 | vim_snprintf((char *)NameBuff, MAXPATHL, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6803 | _("E154: Duplicate tag \"%s\" in file %s/%s"), |
| 6804 | ((char_u **)ga.ga_data)[i], dir, p2 + 1); |
| 6805 | EMSG(NameBuff); |
| 6806 | *p2 = '\t'; |
| 6807 | break; |
| 6808 | } |
| 6809 | ++p1; |
| 6810 | ++p2; |
| 6811 | } |
| 6812 | } |
| 6813 | |
| 6814 | # ifdef FEAT_MBYTE |
| 6815 | if (utf8 == TRUE) |
| 6816 | fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n"); |
| 6817 | # endif |
| 6818 | |
| 6819 | /* |
| 6820 | * Write the tags into the file. |
| 6821 | */ |
| 6822 | for (i = 0; i < ga.ga_len; ++i) |
| 6823 | { |
| 6824 | s = ((char_u **)ga.ga_data)[i]; |
Bram Moolenaar | f621048 | 2007-07-25 20:56:39 +0000 | [diff] [blame] | 6825 | if (STRNCMP(s, "help-tags\t", 10) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6826 | /* help-tags entry was added in formatted form */ |
Bram Moolenaar | f621048 | 2007-07-25 20:56:39 +0000 | [diff] [blame] | 6827 | fputs((char *)s, fd_tags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6828 | else |
| 6829 | { |
| 6830 | fprintf(fd_tags, "%s\t/*", s); |
| 6831 | for (p1 = s; *p1 != '\t'; ++p1) |
| 6832 | { |
| 6833 | /* insert backslash before '\\' and '/' */ |
| 6834 | if (*p1 == '\\' || *p1 == '/') |
| 6835 | putc('\\', fd_tags); |
| 6836 | putc(*p1, fd_tags); |
| 6837 | } |
| 6838 | fprintf(fd_tags, "*\n"); |
| 6839 | } |
| 6840 | } |
| 6841 | } |
| 6842 | #ifdef FEAT_MBYTE |
| 6843 | if (mix) |
| 6844 | got_int = FALSE; /* continue with other languages */ |
| 6845 | #endif |
| 6846 | |
| 6847 | for (i = 0; i < ga.ga_len; ++i) |
| 6848 | vim_free(((char_u **)ga.ga_data)[i]); |
| 6849 | ga_clear(&ga); |
| 6850 | fclose(fd_tags); /* there is no check for an error... */ |
| 6851 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6852 | |
Bram Moolenaar | 6bef530 | 2016-03-12 21:28:26 +0100 | [diff] [blame] | 6853 | /* |
| 6854 | * Generate tags in one help directory, taking care of translations. |
| 6855 | */ |
| 6856 | static void |
| 6857 | do_helptags(char_u *dirname, int add_help_tags) |
| 6858 | { |
| 6859 | #ifdef FEAT_MULTI_LANG |
| 6860 | int len; |
| 6861 | int i, j; |
| 6862 | garray_T ga; |
| 6863 | char_u lang[2]; |
| 6864 | char_u ext[5]; |
| 6865 | char_u fname[8]; |
| 6866 | int filecount; |
| 6867 | char_u **files; |
| 6868 | |
| 6869 | /* Get a list of all files in the help directory and in subdirectories. */ |
| 6870 | STRCPY(NameBuff, dirname); |
| 6871 | add_pathsep(NameBuff); |
| 6872 | STRCAT(NameBuff, "**"); |
| 6873 | if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, |
| 6874 | EW_FILE|EW_SILENT) == FAIL |
| 6875 | || filecount == 0) |
| 6876 | { |
| 6877 | EMSG2("E151: No match: %s", NameBuff); |
Bram Moolenaar | 6bef530 | 2016-03-12 21:28:26 +0100 | [diff] [blame] | 6878 | return; |
| 6879 | } |
| 6880 | |
| 6881 | /* Go over all files in the directory to find out what languages are |
| 6882 | * present. */ |
| 6883 | ga_init2(&ga, 1, 10); |
| 6884 | for (i = 0; i < filecount; ++i) |
| 6885 | { |
| 6886 | len = (int)STRLEN(files[i]); |
| 6887 | if (len > 4) |
| 6888 | { |
| 6889 | if (STRICMP(files[i] + len - 4, ".txt") == 0) |
| 6890 | { |
| 6891 | /* ".txt" -> language "en" */ |
| 6892 | lang[0] = 'e'; |
| 6893 | lang[1] = 'n'; |
| 6894 | } |
| 6895 | else if (files[i][len - 4] == '.' |
| 6896 | && ASCII_ISALPHA(files[i][len - 3]) |
| 6897 | && ASCII_ISALPHA(files[i][len - 2]) |
| 6898 | && TOLOWER_ASC(files[i][len - 1]) == 'x') |
| 6899 | { |
| 6900 | /* ".abx" -> language "ab" */ |
| 6901 | lang[0] = TOLOWER_ASC(files[i][len - 3]); |
| 6902 | lang[1] = TOLOWER_ASC(files[i][len - 2]); |
| 6903 | } |
| 6904 | else |
| 6905 | continue; |
| 6906 | |
| 6907 | /* Did we find this language already? */ |
| 6908 | for (j = 0; j < ga.ga_len; j += 2) |
| 6909 | if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0) |
| 6910 | break; |
| 6911 | if (j == ga.ga_len) |
| 6912 | { |
| 6913 | /* New language, add it. */ |
| 6914 | if (ga_grow(&ga, 2) == FAIL) |
| 6915 | break; |
| 6916 | ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0]; |
| 6917 | ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1]; |
| 6918 | } |
| 6919 | } |
| 6920 | } |
| 6921 | |
| 6922 | /* |
| 6923 | * Loop over the found languages to generate a tags file for each one. |
| 6924 | */ |
| 6925 | for (j = 0; j < ga.ga_len; j += 2) |
| 6926 | { |
| 6927 | STRCPY(fname, "tags-xx"); |
| 6928 | fname[5] = ((char_u *)ga.ga_data)[j]; |
| 6929 | fname[6] = ((char_u *)ga.ga_data)[j + 1]; |
| 6930 | if (fname[5] == 'e' && fname[6] == 'n') |
| 6931 | { |
| 6932 | /* English is an exception: use ".txt" and "tags". */ |
| 6933 | fname[4] = NUL; |
| 6934 | STRCPY(ext, ".txt"); |
| 6935 | } |
| 6936 | else |
| 6937 | { |
| 6938 | /* Language "ab" uses ".abx" and "tags-ab". */ |
| 6939 | STRCPY(ext, ".xxx"); |
| 6940 | ext[1] = fname[5]; |
| 6941 | ext[2] = fname[6]; |
| 6942 | } |
| 6943 | helptags_one(dirname, ext, fname, add_help_tags); |
| 6944 | } |
| 6945 | |
| 6946 | ga_clear(&ga); |
| 6947 | FreeWild(filecount, files); |
| 6948 | |
| 6949 | #else |
| 6950 | /* No language support, just use "*.txt" and "tags". */ |
| 6951 | helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags); |
| 6952 | #endif |
| 6953 | } |
| 6954 | |
| 6955 | static void |
| 6956 | helptags_cb(char_u *fname, void *cookie) |
| 6957 | { |
| 6958 | do_helptags(fname, *(int *)cookie); |
| 6959 | } |
| 6960 | |
| 6961 | /* |
| 6962 | * ":helptags" |
| 6963 | */ |
| 6964 | void |
| 6965 | ex_helptags(exarg_T *eap) |
| 6966 | { |
| 6967 | expand_T xpc; |
| 6968 | char_u *dirname; |
| 6969 | int add_help_tags = FALSE; |
| 6970 | |
| 6971 | /* Check for ":helptags ++t {dir}". */ |
| 6972 | if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3])) |
| 6973 | { |
| 6974 | add_help_tags = TRUE; |
| 6975 | eap->arg = skipwhite(eap->arg + 3); |
| 6976 | } |
| 6977 | |
| 6978 | if (STRCMP(eap->arg, "ALL") == 0) |
| 6979 | { |
| 6980 | do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR, |
| 6981 | helptags_cb, &add_help_tags); |
| 6982 | } |
| 6983 | else |
| 6984 | { |
| 6985 | ExpandInit(&xpc); |
| 6986 | xpc.xp_context = EXPAND_DIRECTORIES; |
| 6987 | dirname = ExpandOne(&xpc, eap->arg, NULL, |
| 6988 | WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE); |
| 6989 | if (dirname == NULL || !mch_isdir(dirname)) |
| 6990 | EMSG2(_("E150: Not a directory: %s"), eap->arg); |
| 6991 | else |
| 6992 | do_helptags(dirname, add_help_tags); |
| 6993 | vim_free(dirname); |
| 6994 | } |
| 6995 | } |
| 6996 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6997 | #if defined(FEAT_SIGNS) || defined(PROTO) |
| 6998 | |
| 6999 | /* |
| 7000 | * Struct to hold the sign properties. |
| 7001 | */ |
| 7002 | typedef struct sign sign_T; |
| 7003 | |
| 7004 | struct sign |
| 7005 | { |
| 7006 | sign_T *sn_next; /* next sign in list */ |
Bram Moolenaar | f75d498 | 2010-10-15 20:20:05 +0200 | [diff] [blame] | 7007 | int sn_typenr; /* type number of sign */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7008 | char_u *sn_name; /* name of sign */ |
| 7009 | char_u *sn_icon; /* name of pixmap */ |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7010 | # ifdef FEAT_SIGN_ICONS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7011 | void *sn_image; /* icon image */ |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7012 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7013 | char_u *sn_text; /* text used instead of pixmap */ |
| 7014 | int sn_line_hl; /* highlight ID for line */ |
| 7015 | int sn_text_hl; /* highlight ID for text */ |
| 7016 | }; |
| 7017 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7018 | static sign_T *first_sign = NULL; |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7019 | static int next_sign_typenr = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7020 | |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 7021 | static int sign_cmd_idx(char_u *begin_cmd, char_u *end_cmd); |
| 7022 | static void sign_list_defined(sign_T *sp); |
| 7023 | static void sign_undefine(sign_T *sp, sign_T *sp_prev); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7024 | |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7025 | static char *cmds[] = { |
| 7026 | "define", |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7027 | # define SIGNCMD_DEFINE 0 |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7028 | "undefine", |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7029 | # define SIGNCMD_UNDEFINE 1 |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7030 | "list", |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7031 | # define SIGNCMD_LIST 2 |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7032 | "place", |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7033 | # define SIGNCMD_PLACE 3 |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7034 | "unplace", |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7035 | # define SIGNCMD_UNPLACE 4 |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7036 | "jump", |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7037 | # define SIGNCMD_JUMP 5 |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7038 | NULL |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7039 | # define SIGNCMD_LAST 6 |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7040 | }; |
| 7041 | |
| 7042 | /* |
| 7043 | * Find index of a ":sign" subcmd from its name. |
| 7044 | * "*end_cmd" must be writable. |
| 7045 | */ |
| 7046 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7047 | sign_cmd_idx( |
| 7048 | char_u *begin_cmd, /* begin of sign subcmd */ |
| 7049 | char_u *end_cmd) /* just after sign subcmd */ |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7050 | { |
| 7051 | int idx; |
| 7052 | char save = *end_cmd; |
| 7053 | |
| 7054 | *end_cmd = NUL; |
| 7055 | for (idx = 0; ; ++idx) |
| 7056 | if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0) |
| 7057 | break; |
| 7058 | *end_cmd = save; |
| 7059 | return idx; |
| 7060 | } |
| 7061 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7062 | /* |
| 7063 | * ":sign" command |
| 7064 | */ |
| 7065 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7066 | ex_sign(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7067 | { |
| 7068 | char_u *arg = eap->arg; |
| 7069 | char_u *p; |
| 7070 | int idx; |
| 7071 | sign_T *sp; |
| 7072 | sign_T *sp_prev; |
| 7073 | buf_T *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7074 | |
| 7075 | /* Parse the subcommand. */ |
| 7076 | p = skiptowhite(arg); |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7077 | idx = sign_cmd_idx(arg, p); |
| 7078 | if (idx == SIGNCMD_LAST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7079 | { |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7080 | EMSG2(_("E160: Unknown sign command: %s"), arg); |
| 7081 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7082 | } |
| 7083 | arg = skipwhite(p); |
| 7084 | |
| 7085 | if (idx <= SIGNCMD_LIST) |
| 7086 | { |
| 7087 | /* |
| 7088 | * Define, undefine or list signs. |
| 7089 | */ |
| 7090 | if (idx == SIGNCMD_LIST && *arg == NUL) |
| 7091 | { |
| 7092 | /* ":sign list": list all defined signs */ |
Bram Moolenaar | 1c0b03e | 2012-03-16 14:32:15 +0100 | [diff] [blame] | 7093 | for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7094 | sign_list_defined(sp); |
| 7095 | } |
| 7096 | else if (*arg == NUL) |
| 7097 | EMSG(_("E156: Missing sign name")); |
| 7098 | else |
| 7099 | { |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7100 | /* Isolate the sign name. If it's a number skip leading zeroes, |
| 7101 | * so that "099" and "99" are the same sign. But keep "0". */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7102 | p = skiptowhite(arg); |
| 7103 | if (*p != NUL) |
| 7104 | *p++ = NUL; |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7105 | while (arg[0] == '0' && arg[1] != NUL) |
| 7106 | ++arg; |
| 7107 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7108 | sp_prev = NULL; |
| 7109 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 7110 | { |
| 7111 | if (STRCMP(sp->sn_name, arg) == 0) |
| 7112 | break; |
| 7113 | sp_prev = sp; |
| 7114 | } |
| 7115 | if (idx == SIGNCMD_DEFINE) |
| 7116 | { |
| 7117 | /* ":sign define {name} ...": define a sign */ |
| 7118 | if (sp == NULL) |
| 7119 | { |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7120 | sign_T *lp; |
| 7121 | int start = next_sign_typenr; |
| 7122 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7123 | /* Allocate a new sign. */ |
| 7124 | sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T)); |
| 7125 | if (sp == NULL) |
| 7126 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7127 | |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7128 | /* Check that next_sign_typenr is not already being used. |
| 7129 | * This only happens after wrapping around. Hopefully |
| 7130 | * another one got deleted and we can use its number. */ |
| 7131 | for (lp = first_sign; lp != NULL; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7132 | { |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7133 | if (lp->sn_typenr == next_sign_typenr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7134 | { |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7135 | ++next_sign_typenr; |
| 7136 | if (next_sign_typenr == MAX_TYPENR) |
| 7137 | next_sign_typenr = 1; |
| 7138 | if (next_sign_typenr == start) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7139 | { |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7140 | vim_free(sp); |
| 7141 | EMSG(_("E612: Too many signs defined")); |
| 7142 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7143 | } |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7144 | lp = first_sign; /* start all over */ |
| 7145 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7146 | } |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7147 | lp = lp->sn_next; |
| 7148 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7149 | |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7150 | sp->sn_typenr = next_sign_typenr; |
| 7151 | if (++next_sign_typenr == MAX_TYPENR) |
| 7152 | next_sign_typenr = 1; /* wrap around */ |
| 7153 | |
| 7154 | sp->sn_name = vim_strsave(arg); |
| 7155 | if (sp->sn_name == NULL) /* out of memory */ |
| 7156 | { |
| 7157 | vim_free(sp); |
| 7158 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7159 | } |
Bram Moolenaar | a4f332b | 2010-10-13 16:44:23 +0200 | [diff] [blame] | 7160 | |
| 7161 | /* add the new sign to the list of signs */ |
| 7162 | if (sp_prev == NULL) |
| 7163 | first_sign = sp; |
| 7164 | else |
| 7165 | sp_prev->sn_next = sp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | /* set values for a defined sign. */ |
| 7169 | for (;;) |
| 7170 | { |
| 7171 | arg = skipwhite(p); |
| 7172 | if (*arg == NUL) |
| 7173 | break; |
| 7174 | p = skiptowhite_esc(arg); |
| 7175 | if (STRNCMP(arg, "icon=", 5) == 0) |
| 7176 | { |
| 7177 | arg += 5; |
| 7178 | vim_free(sp->sn_icon); |
| 7179 | sp->sn_icon = vim_strnsave(arg, (int)(p - arg)); |
| 7180 | backslash_halve(sp->sn_icon); |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7181 | # ifdef FEAT_SIGN_ICONS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7182 | if (gui.in_use) |
| 7183 | { |
| 7184 | out_flush(); |
| 7185 | if (sp->sn_image != NULL) |
| 7186 | gui_mch_destroy_sign(sp->sn_image); |
| 7187 | sp->sn_image = gui_mch_register_sign(sp->sn_icon); |
| 7188 | } |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7189 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7190 | } |
| 7191 | else if (STRNCMP(arg, "text=", 5) == 0) |
| 7192 | { |
| 7193 | char_u *s; |
| 7194 | int cells; |
| 7195 | int len; |
| 7196 | |
| 7197 | arg += 5; |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7198 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7199 | /* Count cells and check for non-printable chars */ |
| 7200 | if (has_mbyte) |
| 7201 | { |
| 7202 | cells = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7203 | for (s = arg; s < p; s += (*mb_ptr2len)(s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7204 | { |
| 7205 | if (!vim_isprintc((*mb_ptr2char)(s))) |
| 7206 | break; |
| 7207 | cells += (*mb_ptr2cells)(s); |
| 7208 | } |
| 7209 | } |
| 7210 | else |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7211 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7212 | { |
| 7213 | for (s = arg; s < p; ++s) |
| 7214 | if (!vim_isprintc(*s)) |
| 7215 | break; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7216 | cells = (int)(s - arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7217 | } |
| 7218 | /* Currently must be one or two display cells */ |
| 7219 | if (s != p || cells < 1 || cells > 2) |
| 7220 | { |
| 7221 | *p = NUL; |
| 7222 | EMSG2(_("E239: Invalid sign text: %s"), arg); |
| 7223 | return; |
| 7224 | } |
| 7225 | |
| 7226 | vim_free(sp->sn_text); |
| 7227 | /* Allocate one byte more if we need to pad up |
| 7228 | * with a space. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7229 | len = (int)(p - arg + ((cells == 1) ? 1 : 0)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7230 | sp->sn_text = vim_strnsave(arg, len); |
| 7231 | |
| 7232 | if (sp->sn_text != NULL && cells == 1) |
| 7233 | STRCPY(sp->sn_text + len - 1, " "); |
| 7234 | } |
| 7235 | else if (STRNCMP(arg, "linehl=", 7) == 0) |
| 7236 | { |
| 7237 | arg += 7; |
| 7238 | sp->sn_line_hl = syn_check_group(arg, (int)(p - arg)); |
| 7239 | } |
| 7240 | else if (STRNCMP(arg, "texthl=", 7) == 0) |
| 7241 | { |
| 7242 | arg += 7; |
| 7243 | sp->sn_text_hl = syn_check_group(arg, (int)(p - arg)); |
| 7244 | } |
| 7245 | else |
| 7246 | { |
| 7247 | EMSG2(_(e_invarg2), arg); |
| 7248 | return; |
| 7249 | } |
| 7250 | } |
| 7251 | } |
| 7252 | else if (sp == NULL) |
| 7253 | EMSG2(_("E155: Unknown sign: %s"), arg); |
| 7254 | else if (idx == SIGNCMD_LIST) |
| 7255 | /* ":sign list {name}" */ |
| 7256 | sign_list_defined(sp); |
| 7257 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7258 | /* ":sign undefine {name}" */ |
Bram Moolenaar | de0dfed | 2009-02-24 03:30:14 +0000 | [diff] [blame] | 7259 | sign_undefine(sp, sp_prev); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7260 | } |
| 7261 | } |
| 7262 | else |
| 7263 | { |
| 7264 | int id = -1; |
| 7265 | linenr_T lnum = -1; |
| 7266 | char_u *sign_name = NULL; |
| 7267 | char_u *arg1; |
| 7268 | |
| 7269 | if (*arg == NUL) |
| 7270 | { |
| 7271 | if (idx == SIGNCMD_PLACE) |
| 7272 | { |
| 7273 | /* ":sign place": list placed signs in all buffers */ |
| 7274 | sign_list_placed(NULL); |
| 7275 | } |
| 7276 | else if (idx == SIGNCMD_UNPLACE) |
| 7277 | { |
| 7278 | /* ":sign unplace": remove placed sign at cursor */ |
| 7279 | id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum); |
| 7280 | if (id > 0) |
| 7281 | { |
| 7282 | buf_delsign(curwin->w_buffer, id); |
| 7283 | update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum); |
| 7284 | } |
| 7285 | else |
| 7286 | EMSG(_("E159: Missing sign number")); |
| 7287 | } |
| 7288 | else |
| 7289 | EMSG(_(e_argreq)); |
| 7290 | return; |
| 7291 | } |
| 7292 | |
| 7293 | if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL) |
| 7294 | { |
| 7295 | /* ":sign unplace *": remove all placed signs */ |
| 7296 | buf_delete_all_signs(); |
| 7297 | return; |
| 7298 | } |
| 7299 | |
| 7300 | /* first arg could be placed sign id */ |
| 7301 | arg1 = arg; |
| 7302 | if (VIM_ISDIGIT(*arg)) |
| 7303 | { |
| 7304 | id = getdigits(&arg); |
| 7305 | if (!vim_iswhite(*arg) && *arg != NUL) |
| 7306 | { |
| 7307 | id = -1; |
| 7308 | arg = arg1; |
| 7309 | } |
| 7310 | else |
| 7311 | { |
| 7312 | arg = skipwhite(arg); |
| 7313 | if (idx == SIGNCMD_UNPLACE && *arg == NUL) |
| 7314 | { |
| 7315 | /* ":sign unplace {id}": remove placed sign by number */ |
| 7316 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 7317 | if ((lnum = buf_delsign(buf, id)) != 0) |
| 7318 | update_debug_sign(buf, lnum); |
| 7319 | return; |
| 7320 | } |
| 7321 | } |
| 7322 | } |
| 7323 | |
| 7324 | /* |
| 7325 | * Check for line={lnum} name={name} and file={fname} or buffer={nr}. |
| 7326 | * Leave "arg" pointing to {fname}. |
| 7327 | */ |
| 7328 | for (;;) |
| 7329 | { |
| 7330 | if (STRNCMP(arg, "line=", 5) == 0) |
| 7331 | { |
| 7332 | arg += 5; |
| 7333 | lnum = atoi((char *)arg); |
| 7334 | arg = skiptowhite(arg); |
| 7335 | } |
Bram Moolenaar | f65e566 | 2012-07-10 15:18:22 +0200 | [diff] [blame] | 7336 | else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE) |
| 7337 | { |
| 7338 | if (id != -1) |
| 7339 | { |
| 7340 | EMSG(_(e_invarg)); |
| 7341 | return; |
| 7342 | } |
| 7343 | id = -2; |
| 7344 | arg = skiptowhite(arg + 1); |
| 7345 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7346 | else if (STRNCMP(arg, "name=", 5) == 0) |
| 7347 | { |
| 7348 | arg += 5; |
| 7349 | sign_name = arg; |
| 7350 | arg = skiptowhite(arg); |
| 7351 | if (*arg != NUL) |
| 7352 | *arg++ = NUL; |
Bram Moolenaar | b60574b | 2010-10-14 21:29:37 +0200 | [diff] [blame] | 7353 | while (sign_name[0] == '0' && sign_name[1] != NUL) |
| 7354 | ++sign_name; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7355 | } |
| 7356 | else if (STRNCMP(arg, "file=", 5) == 0) |
| 7357 | { |
| 7358 | arg += 5; |
| 7359 | buf = buflist_findname(arg); |
| 7360 | break; |
| 7361 | } |
| 7362 | else if (STRNCMP(arg, "buffer=", 7) == 0) |
| 7363 | { |
| 7364 | arg += 7; |
| 7365 | buf = buflist_findnr((int)getdigits(&arg)); |
| 7366 | if (*skipwhite(arg) != NUL) |
| 7367 | EMSG(_(e_trailing)); |
| 7368 | break; |
| 7369 | } |
| 7370 | else |
| 7371 | { |
| 7372 | EMSG(_(e_invarg)); |
| 7373 | return; |
| 7374 | } |
| 7375 | arg = skipwhite(arg); |
| 7376 | } |
| 7377 | |
| 7378 | if (buf == NULL) |
| 7379 | { |
| 7380 | EMSG2(_("E158: Invalid buffer name: %s"), arg); |
| 7381 | } |
Bram Moolenaar | f65e566 | 2012-07-10 15:18:22 +0200 | [diff] [blame] | 7382 | else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7383 | { |
| 7384 | if (lnum >= 0 || sign_name != NULL) |
| 7385 | EMSG(_(e_invarg)); |
| 7386 | else |
| 7387 | /* ":sign place file={fname}": list placed signs in one file */ |
| 7388 | sign_list_placed(buf); |
| 7389 | } |
| 7390 | else if (idx == SIGNCMD_JUMP) |
| 7391 | { |
| 7392 | /* ":sign jump {id} file={fname}" */ |
| 7393 | if (lnum >= 0 || sign_name != NULL) |
| 7394 | EMSG(_(e_invarg)); |
| 7395 | else if ((lnum = buf_findsign(buf, id)) > 0) |
| 7396 | { /* goto a sign ... */ |
| 7397 | if (buf_jump_open_win(buf) != NULL) |
| 7398 | { /* ... in a current window */ |
| 7399 | curwin->w_cursor.lnum = lnum; |
| 7400 | check_cursor_lnum(); |
| 7401 | beginline(BL_WHITE); |
| 7402 | } |
| 7403 | else |
| 7404 | { /* ... not currently in a window */ |
| 7405 | char_u *cmd; |
| 7406 | |
| 7407 | cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25); |
| 7408 | if (cmd == NULL) |
| 7409 | return; |
| 7410 | sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname); |
| 7411 | do_cmdline_cmd(cmd); |
| 7412 | vim_free(cmd); |
| 7413 | } |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7414 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7415 | foldOpenCursor(); |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7416 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7417 | } |
| 7418 | else |
| 7419 | EMSGN(_("E157: Invalid sign ID: %ld"), id); |
| 7420 | } |
| 7421 | else if (idx == SIGNCMD_UNPLACE) |
| 7422 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7423 | if (lnum >= 0 || sign_name != NULL) |
| 7424 | EMSG(_(e_invarg)); |
Bram Moolenaar | f65e566 | 2012-07-10 15:18:22 +0200 | [diff] [blame] | 7425 | else if (id == -2) |
| 7426 | { |
| 7427 | /* ":sign unplace * file={fname}" */ |
| 7428 | redraw_buf_later(buf, NOT_VALID); |
| 7429 | buf_delete_signs(buf); |
| 7430 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7431 | else |
| 7432 | { |
Bram Moolenaar | f65e566 | 2012-07-10 15:18:22 +0200 | [diff] [blame] | 7433 | /* ":sign unplace {id} file={fname}" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7434 | lnum = buf_delsign(buf, id); |
| 7435 | update_debug_sign(buf, lnum); |
| 7436 | } |
| 7437 | } |
| 7438 | /* idx == SIGNCMD_PLACE */ |
| 7439 | else if (sign_name != NULL) |
| 7440 | { |
| 7441 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 7442 | if (STRCMP(sp->sn_name, sign_name) == 0) |
| 7443 | break; |
| 7444 | if (sp == NULL) |
| 7445 | { |
| 7446 | EMSG2(_("E155: Unknown sign: %s"), sign_name); |
| 7447 | return; |
| 7448 | } |
| 7449 | if (lnum > 0) |
| 7450 | /* ":sign place {id} line={lnum} name={name} file={fname}": |
| 7451 | * place a sign */ |
| 7452 | buf_addsign(buf, id, lnum, sp->sn_typenr); |
| 7453 | else |
| 7454 | /* ":sign place {id} file={fname}": change sign type */ |
| 7455 | lnum = buf_change_sign_type(buf, id, sp->sn_typenr); |
Bram Moolenaar | f4d7f16 | 2014-05-07 14:38:44 +0200 | [diff] [blame] | 7456 | if (lnum > 0) |
| 7457 | update_debug_sign(buf, lnum); |
| 7458 | else |
| 7459 | EMSG2(_("E885: Not possible to change sign %s"), sign_name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7460 | } |
| 7461 | else |
| 7462 | EMSG(_(e_invarg)); |
| 7463 | } |
| 7464 | } |
| 7465 | |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7466 | # if defined(FEAT_SIGN_ICONS) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7467 | /* |
| 7468 | * Allocate the icons. Called when the GUI has started. Allows defining |
| 7469 | * signs before it starts. |
| 7470 | */ |
| 7471 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7472 | sign_gui_started(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7473 | { |
| 7474 | sign_T *sp; |
| 7475 | |
| 7476 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 7477 | if (sp->sn_icon != NULL) |
| 7478 | sp->sn_image = gui_mch_register_sign(sp->sn_icon); |
| 7479 | } |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7480 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7481 | |
| 7482 | /* |
| 7483 | * List one sign. |
| 7484 | */ |
| 7485 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7486 | sign_list_defined(sign_T *sp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7487 | { |
| 7488 | char_u *p; |
| 7489 | |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 7490 | smsg((char_u *)"sign %s", sp->sn_name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7491 | if (sp->sn_icon != NULL) |
| 7492 | { |
| 7493 | MSG_PUTS(" icon="); |
| 7494 | msg_outtrans(sp->sn_icon); |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7495 | # ifdef FEAT_SIGN_ICONS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7496 | if (sp->sn_image == NULL) |
| 7497 | MSG_PUTS(_(" (NOT FOUND)")); |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7498 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7499 | MSG_PUTS(_(" (not supported)")); |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7500 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7501 | } |
| 7502 | if (sp->sn_text != NULL) |
| 7503 | { |
| 7504 | MSG_PUTS(" text="); |
| 7505 | msg_outtrans(sp->sn_text); |
| 7506 | } |
| 7507 | if (sp->sn_line_hl > 0) |
| 7508 | { |
| 7509 | MSG_PUTS(" linehl="); |
| 7510 | p = get_highlight_name(NULL, sp->sn_line_hl - 1); |
| 7511 | if (p == NULL) |
| 7512 | MSG_PUTS("NONE"); |
| 7513 | else |
| 7514 | msg_puts(p); |
| 7515 | } |
| 7516 | if (sp->sn_text_hl > 0) |
| 7517 | { |
| 7518 | MSG_PUTS(" texthl="); |
| 7519 | p = get_highlight_name(NULL, sp->sn_text_hl - 1); |
| 7520 | if (p == NULL) |
| 7521 | MSG_PUTS("NONE"); |
| 7522 | else |
| 7523 | msg_puts(p); |
| 7524 | } |
| 7525 | } |
| 7526 | |
| 7527 | /* |
Bram Moolenaar | de0dfed | 2009-02-24 03:30:14 +0000 | [diff] [blame] | 7528 | * Undefine a sign and free its memory. |
| 7529 | */ |
| 7530 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7531 | sign_undefine(sign_T *sp, sign_T *sp_prev) |
Bram Moolenaar | de0dfed | 2009-02-24 03:30:14 +0000 | [diff] [blame] | 7532 | { |
| 7533 | vim_free(sp->sn_name); |
| 7534 | vim_free(sp->sn_icon); |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7535 | # ifdef FEAT_SIGN_ICONS |
Bram Moolenaar | de0dfed | 2009-02-24 03:30:14 +0000 | [diff] [blame] | 7536 | if (sp->sn_image != NULL) |
| 7537 | { |
| 7538 | out_flush(); |
| 7539 | gui_mch_destroy_sign(sp->sn_image); |
| 7540 | } |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7541 | # endif |
Bram Moolenaar | de0dfed | 2009-02-24 03:30:14 +0000 | [diff] [blame] | 7542 | vim_free(sp->sn_text); |
| 7543 | if (sp_prev == NULL) |
| 7544 | first_sign = sp->sn_next; |
| 7545 | else |
| 7546 | sp_prev->sn_next = sp->sn_next; |
| 7547 | vim_free(sp); |
| 7548 | } |
| 7549 | |
| 7550 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7551 | * Get highlighting attribute for sign "typenr". |
| 7552 | * If "line" is TRUE: line highl, if FALSE: text highl. |
| 7553 | */ |
| 7554 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7555 | sign_get_attr(int typenr, int line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7556 | { |
| 7557 | sign_T *sp; |
| 7558 | |
| 7559 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 7560 | if (sp->sn_typenr == typenr) |
| 7561 | { |
| 7562 | if (line) |
| 7563 | { |
| 7564 | if (sp->sn_line_hl > 0) |
| 7565 | return syn_id2attr(sp->sn_line_hl); |
| 7566 | } |
| 7567 | else |
| 7568 | { |
| 7569 | if (sp->sn_text_hl > 0) |
| 7570 | return syn_id2attr(sp->sn_text_hl); |
| 7571 | } |
| 7572 | break; |
| 7573 | } |
| 7574 | return 0; |
| 7575 | } |
| 7576 | |
| 7577 | /* |
| 7578 | * Get text mark for sign "typenr". |
| 7579 | * Returns NULL if there isn't one. |
| 7580 | */ |
| 7581 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7582 | sign_get_text(int typenr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7583 | { |
| 7584 | sign_T *sp; |
| 7585 | |
| 7586 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 7587 | if (sp->sn_typenr == typenr) |
| 7588 | return sp->sn_text; |
| 7589 | return NULL; |
| 7590 | } |
| 7591 | |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7592 | # if defined(FEAT_SIGN_ICONS) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7593 | void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7594 | sign_get_image( |
| 7595 | int typenr) /* the attribute which may have a sign */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7596 | { |
| 7597 | sign_T *sp; |
| 7598 | |
| 7599 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 7600 | if (sp->sn_typenr == typenr) |
| 7601 | return sp->sn_image; |
| 7602 | return NULL; |
| 7603 | } |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7604 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7605 | |
| 7606 | /* |
| 7607 | * Get the name of a sign by its typenr. |
| 7608 | */ |
| 7609 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7610 | sign_typenr2name(int typenr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7611 | { |
| 7612 | sign_T *sp; |
| 7613 | |
| 7614 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 7615 | if (sp->sn_typenr == typenr) |
| 7616 | return sp->sn_name; |
| 7617 | return (char_u *)_("[Deleted]"); |
| 7618 | } |
| 7619 | |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7620 | # if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | de0dfed | 2009-02-24 03:30:14 +0000 | [diff] [blame] | 7621 | /* |
| 7622 | * Undefine/free all signs. |
| 7623 | */ |
| 7624 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7625 | free_signs(void) |
Bram Moolenaar | de0dfed | 2009-02-24 03:30:14 +0000 | [diff] [blame] | 7626 | { |
| 7627 | while (first_sign != NULL) |
| 7628 | sign_undefine(first_sign, NULL); |
| 7629 | } |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7630 | # endif |
Bram Moolenaar | de0dfed | 2009-02-24 03:30:14 +0000 | [diff] [blame] | 7631 | |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7632 | # if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7633 | static enum |
| 7634 | { |
| 7635 | EXP_SUBCMD, /* expand :sign sub-commands */ |
| 7636 | EXP_DEFINE, /* expand :sign define {name} args */ |
| 7637 | EXP_PLACE, /* expand :sign place {id} args */ |
| 7638 | EXP_UNPLACE, /* expand :sign unplace" */ |
| 7639 | EXP_SIGN_NAMES /* expand with name of placed signs */ |
| 7640 | } expand_what; |
| 7641 | |
| 7642 | /* |
| 7643 | * Function given to ExpandGeneric() to obtain the sign command |
| 7644 | * expansion. |
| 7645 | */ |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7646 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7647 | get_sign_name(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7648 | { |
| 7649 | sign_T *sp; |
| 7650 | int current_idx; |
| 7651 | |
| 7652 | switch (expand_what) |
| 7653 | { |
| 7654 | case EXP_SUBCMD: |
| 7655 | return (char_u *)cmds[idx]; |
| 7656 | case EXP_DEFINE: |
| 7657 | { |
| 7658 | char *define_arg[] = |
| 7659 | { |
| 7660 | "icon=", "linehl=", "text=", "texthl=", NULL |
| 7661 | }; |
| 7662 | return (char_u *)define_arg[idx]; |
| 7663 | } |
| 7664 | case EXP_PLACE: |
| 7665 | { |
| 7666 | char *place_arg[] = |
| 7667 | { |
| 7668 | "line=", "name=", "file=", "buffer=", NULL |
| 7669 | }; |
| 7670 | return (char_u *)place_arg[idx]; |
| 7671 | } |
| 7672 | case EXP_UNPLACE: |
| 7673 | { |
| 7674 | char *unplace_arg[] = { "file=", "buffer=", NULL }; |
| 7675 | return (char_u *)unplace_arg[idx]; |
| 7676 | } |
| 7677 | case EXP_SIGN_NAMES: |
| 7678 | /* Complete with name of signs already defined */ |
| 7679 | current_idx = 0; |
| 7680 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 7681 | if (current_idx++ == idx) |
| 7682 | return sp->sn_name; |
| 7683 | return NULL; |
| 7684 | default: |
| 7685 | return NULL; |
| 7686 | } |
| 7687 | } |
| 7688 | |
| 7689 | /* |
| 7690 | * Handle command line completion for :sign command. |
| 7691 | */ |
| 7692 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7693 | set_context_in_sign_cmd(expand_T *xp, char_u *arg) |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7694 | { |
| 7695 | char_u *p; |
| 7696 | char_u *end_subcmd; |
| 7697 | char_u *last; |
| 7698 | int cmd_idx; |
| 7699 | char_u *begin_subcmd_args; |
| 7700 | |
| 7701 | /* Default: expand subcommands. */ |
| 7702 | xp->xp_context = EXPAND_SIGN; |
| 7703 | expand_what = EXP_SUBCMD; |
| 7704 | xp->xp_pattern = arg; |
| 7705 | |
| 7706 | end_subcmd = skiptowhite(arg); |
| 7707 | if (*end_subcmd == NUL) |
| 7708 | /* expand subcmd name |
| 7709 | * :sign {subcmd}<CTRL-D>*/ |
| 7710 | return; |
| 7711 | |
| 7712 | cmd_idx = sign_cmd_idx(arg, end_subcmd); |
| 7713 | |
| 7714 | /* :sign {subcmd} {subcmd_args} |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 7715 | * | |
| 7716 | * begin_subcmd_args */ |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7717 | begin_subcmd_args = skipwhite(end_subcmd); |
| 7718 | p = skiptowhite(begin_subcmd_args); |
| 7719 | if (*p == NUL) |
| 7720 | { |
| 7721 | /* |
| 7722 | * Expand first argument of subcmd when possible. |
| 7723 | * For ":jump {id}" and ":unplace {id}", we could |
| 7724 | * possibly expand the ids of all signs already placed. |
| 7725 | */ |
| 7726 | xp->xp_pattern = begin_subcmd_args; |
| 7727 | switch (cmd_idx) |
| 7728 | { |
| 7729 | case SIGNCMD_LIST: |
| 7730 | case SIGNCMD_UNDEFINE: |
| 7731 | /* :sign list <CTRL-D> |
| 7732 | * :sign undefine <CTRL-D> */ |
| 7733 | expand_what = EXP_SIGN_NAMES; |
| 7734 | break; |
| 7735 | default: |
| 7736 | xp->xp_context = EXPAND_NOTHING; |
| 7737 | } |
| 7738 | return; |
| 7739 | } |
| 7740 | |
| 7741 | /* expand last argument of subcmd */ |
| 7742 | |
| 7743 | /* :sign define {name} {args}... |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 7744 | * | |
| 7745 | * p */ |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7746 | |
| 7747 | /* Loop until reaching last argument. */ |
| 7748 | do |
| 7749 | { |
| 7750 | p = skipwhite(p); |
| 7751 | last = p; |
| 7752 | p = skiptowhite(p); |
| 7753 | } while (*p != NUL); |
| 7754 | |
| 7755 | p = vim_strchr(last, '='); |
| 7756 | |
| 7757 | /* :sign define {name} {args}... {last}= |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 7758 | * | | |
| 7759 | * last p */ |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7760 | if (p == NUL) |
| 7761 | { |
| 7762 | /* Expand last argument name (before equal sign). */ |
| 7763 | xp->xp_pattern = last; |
| 7764 | switch (cmd_idx) |
| 7765 | { |
| 7766 | case SIGNCMD_DEFINE: |
| 7767 | expand_what = EXP_DEFINE; |
| 7768 | break; |
| 7769 | case SIGNCMD_PLACE: |
| 7770 | expand_what = EXP_PLACE; |
| 7771 | break; |
| 7772 | case SIGNCMD_JUMP: |
| 7773 | case SIGNCMD_UNPLACE: |
| 7774 | expand_what = EXP_UNPLACE; |
| 7775 | break; |
| 7776 | default: |
| 7777 | xp->xp_context = EXPAND_NOTHING; |
| 7778 | } |
| 7779 | } |
| 7780 | else |
| 7781 | { |
| 7782 | /* Expand last argument value (after equal sign). */ |
| 7783 | xp->xp_pattern = p + 1; |
| 7784 | switch (cmd_idx) |
| 7785 | { |
| 7786 | case SIGNCMD_DEFINE: |
| 7787 | if (STRNCMP(last, "texthl", p - last) == 0 || |
| 7788 | STRNCMP(last, "linehl", p - last) == 0) |
| 7789 | xp->xp_context = EXPAND_HIGHLIGHT; |
| 7790 | else if (STRNCMP(last, "icon", p - last) == 0) |
| 7791 | xp->xp_context = EXPAND_FILES; |
| 7792 | else |
| 7793 | xp->xp_context = EXPAND_NOTHING; |
| 7794 | break; |
| 7795 | case SIGNCMD_PLACE: |
| 7796 | if (STRNCMP(last, "name", p - last) == 0) |
| 7797 | expand_what = EXP_SIGN_NAMES; |
| 7798 | else |
| 7799 | xp->xp_context = EXPAND_NOTHING; |
| 7800 | break; |
| 7801 | default: |
| 7802 | xp->xp_context = EXPAND_NOTHING; |
| 7803 | } |
| 7804 | } |
| 7805 | } |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7806 | # endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 7807 | #endif |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7808 | |
| 7809 | /* |
| 7810 | * Make the user happy. |
| 7811 | */ |
| 7812 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7813 | ex_smile(exarg_T *eap UNUSED) |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7814 | { |
Bram Moolenaar | 700eefe | 2016-04-13 21:14:37 +0200 | [diff] [blame] | 7815 | static char *code[] = { |
| 7816 | "\34 \4o\14$\4ox\30 \2o\30$\1ox\25 \2o\36$\1o\11 \1o\1$\3 \2$\1 \1o\1$x\5 \1o\1 \1$\1 \2o\10 \1o\44$\1o\7 \2$\1 \2$\1 \2$\1o\1$x\2 \2o\1 \1$\1 \1$\1 \1\"\1$\6 \1o\11$\4 \15$\4 \11$\1o\7 \3$\1o\2$\1o\1$x\2 \1\"\6$\1o\1$\5 \1o\11$\6 \13$\6 \12$\1o\4 \10$x\4 \7$\4 \13$\6 \13$\6 \27$x\4 \27$\4 \15$\4 \16$\2 \3\"\3$x\5 \1\"\3$\4\"\61$\5 \1\"\3$x\6 \3$\3 \1o\62$\5 \1\"\3$\1ox\5 \1o\2$\1\"\3 \63$\7 \3$\1ox\5 \3$\4 \55$\1\"\1 \1\"\6$", |
| 7817 | "\5o\4$\1ox\4 \1o\3$\4o\5$\2 \45$\3 \1o\21$x\4 \10$\1\"\4$\3 \42$\5 \4$\10\"x\3 \4\"\7 \4$\4 \1\"\34$\1\"\6 \1o\3$x\16 \1\"\3$\1o\5 \3\"\22$\1\"\2$\1\"\11 \3$x\20 \3$\1o\12 \1\"\2$\2\"\6$\4\"\13 \1o\3$x\21 \4$\1o\40 \1o\3$\1\"x\22 \1\"\4$\1o\6 \1o\6$\1o\1\"\4$\1o\10 \1o\4$x\24 \1\"\5$\2o\5 \2\"\4$\1o\5$\1o\3 \1o\4$\2\"x\27 \2\"\5$\4o\2 \1\"\3$\1o\11$\3\"x\32 \2\"\7$\2o\1 \12$x\42 \4\"\13$x\46 \14$x\47 \12$\1\"x\50 \1\"\3$\4\"x" |
| 7818 | }; |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7819 | char *p; |
| 7820 | int n; |
Bram Moolenaar | 700eefe | 2016-04-13 21:14:37 +0200 | [diff] [blame] | 7821 | int i; |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7822 | |
| 7823 | msg_start(); |
| 7824 | msg_putchar('\n'); |
Bram Moolenaar | 700eefe | 2016-04-13 21:14:37 +0200 | [diff] [blame] | 7825 | for (i = 0; i < 2; ++i) |
| 7826 | for (p = code[i]; *p != NUL; ++p) |
| 7827 | if (*p == 'x') |
| 7828 | msg_putchar('\n'); |
| 7829 | else |
| 7830 | for (n = *p++; n > 0; --n) |
| 7831 | if (*p == 'o' || *p == '$') |
| 7832 | msg_putchar_attr(*p, hl_attr(HLF_L)); |
| 7833 | else |
| 7834 | msg_putchar(*p); |
Bram Moolenaar | 86e179d | 2015-12-31 16:10:23 +0100 | [diff] [blame] | 7835 | msg_clr_eos(); |
| 7836 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7837 | |
| 7838 | #if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO) |
| 7839 | /* |
| 7840 | * ":drop" |
| 7841 | * Opens the first argument in a window. When there are two or more arguments |
| 7842 | * the argument list is redefined. |
| 7843 | */ |
| 7844 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7845 | ex_drop(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7846 | { |
| 7847 | int split = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7848 | win_T *wp; |
| 7849 | buf_T *buf; |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7850 | # ifdef FEAT_WINDOWS |
| 7851 | tabpage_T *tp; |
| 7852 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7853 | |
| 7854 | /* |
| 7855 | * Check if the first argument is already being edited in a window. If |
| 7856 | * so, jump to that window. |
| 7857 | * We would actually need to check all arguments, but that's complicated |
| 7858 | * and mostly only one file is dropped. |
| 7859 | * This also ignores wildcards, since it is very unlikely the user is |
| 7860 | * editing a file name with a wildcard character. |
| 7861 | */ |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7862 | set_arglist(eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7863 | |
Bram Moolenaar | b01a8b7 | 2007-02-13 02:47:22 +0000 | [diff] [blame] | 7864 | /* |
| 7865 | * Expanding wildcards may result in an empty argument list. E.g. when |
| 7866 | * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we |
| 7867 | * already did an error message for this. |
| 7868 | */ |
| 7869 | if (ARGCOUNT == 0) |
| 7870 | return; |
| 7871 | |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7872 | # ifdef FEAT_WINDOWS |
| 7873 | if (cmdmod.tab) |
| 7874 | { |
| 7875 | /* ":tab drop file ...": open a tab for each argument that isn't |
| 7876 | * edited in a window yet. It's like ":tab all" but without closing |
| 7877 | * windows or tabs. */ |
| 7878 | ex_all(eap); |
| 7879 | } |
| 7880 | else |
| 7881 | # endif |
| 7882 | { |
| 7883 | /* ":drop file ...": Edit the first argument. Jump to an existing |
| 7884 | * window if possible, edit in current window if the current buffer |
| 7885 | * can be abandoned, otherwise open a new window. */ |
| 7886 | buf = buflist_findnr(ARGLIST[0].ae_fnum); |
| 7887 | |
| 7888 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 7889 | { |
| 7890 | if (wp->w_buffer == buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7891 | { |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7892 | # ifdef FEAT_WINDOWS |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 7893 | goto_tabpage_win(tp, wp); |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7894 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7895 | curwin->w_arg_idx = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7896 | return; |
| 7897 | } |
| 7898 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7899 | |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7900 | /* |
| 7901 | * Check whether the current buffer is changed. If so, we will need |
| 7902 | * to split the current window or data could be lost. |
| 7903 | * Skip the check if the 'hidden' option is set, as in this case the |
| 7904 | * buffer won't be lost. |
| 7905 | */ |
| 7906 | if (!P_HID(curbuf)) |
| 7907 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7908 | # ifdef FEAT_WINDOWS |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7909 | ++emsg_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7910 | # endif |
Bram Moolenaar | 45d3b14 | 2013-11-09 03:31:51 +0100 | [diff] [blame] | 7911 | split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7912 | # ifdef FEAT_WINDOWS |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7913 | --emsg_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7914 | # else |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7915 | if (split) |
| 7916 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7917 | # endif |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7918 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7919 | |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 7920 | /* Fake a ":sfirst" or ":first" command edit the first argument. */ |
| 7921 | if (split) |
| 7922 | { |
| 7923 | eap->cmdidx = CMD_sfirst; |
| 7924 | eap->cmd[0] = 's'; |
| 7925 | } |
| 7926 | else |
| 7927 | eap->cmdidx = CMD_first; |
| 7928 | ex_rewind(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7929 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7930 | } |
| 7931 | #endif |