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 | |
| 14 | #include "vim.h" |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 15 | #ifdef HAVE_FCNTL_H |
| 16 | # include <fcntl.h> |
| 17 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 18 | #include "version.h" |
| 19 | |
| 20 | #ifdef FEAT_EX_EXTRA |
| 21 | static int linelen __ARGS((int *has_tab)); |
| 22 | #endif |
| 23 | static void do_filter __ARGS((linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out)); |
| 24 | #ifdef FEAT_VIMINFO |
| 25 | static char_u *viminfo_filename __ARGS((char_u *)); |
| 26 | static void do_viminfo __ARGS((FILE *fp_in, FILE *fp_out, int want_info, int want_marks, int force_read)); |
| 27 | static int viminfo_encoding __ARGS((vir_T *virp)); |
| 28 | static int read_viminfo_up_to_marks __ARGS((vir_T *virp, int forceit, int writing)); |
| 29 | #endif |
| 30 | |
| 31 | static int check_overwrite __ARGS((exarg_T *eap, buf_T *buf, char_u *fname, char_u *ffname, int other)); |
| 32 | static int check_readonly __ARGS((int *forceit, buf_T *buf)); |
| 33 | #ifdef FEAT_AUTOCMD |
| 34 | static void delbuf_msg __ARGS((char_u *name)); |
| 35 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 36 | static int |
| 37 | #ifdef __BORLANDC__ |
| 38 | _RTLENTRYF |
| 39 | #endif |
| 40 | help_compare __ARGS((const void *s1, const void *s2)); |
| 41 | |
| 42 | /* |
| 43 | * ":ascii" and "ga". |
| 44 | */ |
| 45 | /*ARGSUSED*/ |
| 46 | void |
| 47 | do_ascii(eap) |
| 48 | exarg_T *eap; |
| 49 | { |
| 50 | int c; |
| 51 | char buf1[20]; |
| 52 | char buf2[20]; |
| 53 | char_u buf3[7]; |
| 54 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 55 | int cc[MAX_MCO]; |
| 56 | int ci = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 57 | int len; |
| 58 | |
| 59 | if (enc_utf8) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 60 | c = utfc_ptr2char(ml_get_cursor(), cc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | else |
| 62 | #endif |
| 63 | c = gchar_cursor(); |
| 64 | if (c == NUL) |
| 65 | { |
| 66 | MSG("NUL"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | #ifdef FEAT_MBYTE |
| 71 | IObuff[0] = NUL; |
| 72 | if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80) |
| 73 | #endif |
| 74 | { |
| 75 | if (c == NL) /* NUL is stored as NL */ |
| 76 | c = NUL; |
| 77 | if (vim_isprintc_strict(c) && (c < ' ' |
| 78 | #ifndef EBCDIC |
| 79 | || c > '~' |
| 80 | #endif |
| 81 | )) |
| 82 | { |
| 83 | transchar_nonprint(buf3, c); |
| 84 | sprintf(buf1, " <%s>", (char *)buf3); |
| 85 | } |
| 86 | else |
| 87 | buf1[0] = NUL; |
| 88 | #ifndef EBCDIC |
| 89 | if (c >= 0x80) |
| 90 | sprintf(buf2, " <M-%s>", transchar(c & 0x7f)); |
| 91 | else |
| 92 | #endif |
| 93 | buf2[0] = NUL; |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 94 | vim_snprintf((char *)IObuff, IOSIZE, |
| 95 | _("<%s>%s%s %d, Hex %02x, Octal %03o"), |
| 96 | transchar(c), buf1, buf2, c, c, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 98 | c = cc[ci++]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | #endif |
| 100 | } |
| 101 | |
| 102 | #ifdef FEAT_MBYTE |
| 103 | /* Repeat for combining characters. */ |
| 104 | while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80))) |
| 105 | { |
| 106 | len = (int)STRLEN(IObuff); |
| 107 | /* This assumes every multi-byte char is printable... */ |
| 108 | if (len > 0) |
| 109 | IObuff[len++] = ' '; |
| 110 | IObuff[len++] = '<'; |
| 111 | if (utf_iscomposing(c) |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 112 | # ifdef USE_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 113 | && !gui.in_use |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 114 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 115 | ) |
| 116 | IObuff[len++] = ' '; /* draw composing char on top of a space */ |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 117 | len += (*mb_char2bytes)(c, IObuff + len); |
| 118 | vim_snprintf((char *)IObuff + len, IOSIZE - len, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 119 | c < 0x10000 ? _("> %d, Hex %04x, Octal %o") |
| 120 | : _("> %d, Hex %08x, Octal %o"), c, c, c); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 121 | if (ci == MAX_MCO) |
| 122 | break; |
| 123 | c = cc[ci++]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 124 | } |
| 125 | #endif |
| 126 | |
| 127 | msg(IObuff); |
| 128 | } |
| 129 | |
| 130 | #if defined(FEAT_EX_EXTRA) || defined(PROTO) |
| 131 | /* |
| 132 | * ":left", ":center" and ":right": align text. |
| 133 | */ |
| 134 | void |
| 135 | ex_align(eap) |
| 136 | exarg_T *eap; |
| 137 | { |
| 138 | pos_T save_curpos; |
| 139 | int len; |
| 140 | int indent = 0; |
| 141 | int new_indent; |
| 142 | int has_tab; |
| 143 | int width; |
| 144 | |
| 145 | #ifdef FEAT_RIGHTLEFT |
| 146 | if (curwin->w_p_rl) |
| 147 | { |
| 148 | /* switch left and right aligning */ |
| 149 | if (eap->cmdidx == CMD_right) |
| 150 | eap->cmdidx = CMD_left; |
| 151 | else if (eap->cmdidx == CMD_left) |
| 152 | eap->cmdidx = CMD_right; |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | width = atoi((char *)eap->arg); |
| 157 | save_curpos = curwin->w_cursor; |
| 158 | if (eap->cmdidx == CMD_left) /* width is used for new indent */ |
| 159 | { |
| 160 | if (width >= 0) |
| 161 | indent = width; |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | /* |
| 166 | * if 'textwidth' set, use it |
| 167 | * else if 'wrapmargin' set, use it |
| 168 | * if invalid value, use 80 |
| 169 | */ |
| 170 | if (width <= 0) |
| 171 | width = curbuf->b_p_tw; |
| 172 | if (width == 0 && curbuf->b_p_wm > 0) |
| 173 | width = W_WIDTH(curwin) - curbuf->b_p_wm; |
| 174 | if (width <= 0) |
| 175 | width = 80; |
| 176 | } |
| 177 | |
| 178 | if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) |
| 179 | return; |
| 180 | |
| 181 | for (curwin->w_cursor.lnum = eap->line1; |
| 182 | curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum) |
| 183 | { |
| 184 | if (eap->cmdidx == CMD_left) /* left align */ |
| 185 | new_indent = indent; |
| 186 | else |
| 187 | { |
| 188 | len = linelen(eap->cmdidx == CMD_right ? &has_tab |
| 189 | : NULL) - get_indent(); |
| 190 | |
| 191 | if (len <= 0) /* skip blank lines */ |
| 192 | continue; |
| 193 | |
| 194 | if (eap->cmdidx == CMD_center) |
| 195 | new_indent = (width - len) / 2; |
| 196 | else |
| 197 | { |
| 198 | new_indent = width - len; /* right align */ |
| 199 | |
| 200 | /* |
| 201 | * Make sure that embedded TABs don't make the text go too far |
| 202 | * to the right. |
| 203 | */ |
| 204 | if (has_tab) |
| 205 | while (new_indent > 0) |
| 206 | { |
| 207 | (void)set_indent(new_indent, 0); |
| 208 | if (linelen(NULL) <= width) |
| 209 | { |
| 210 | /* |
| 211 | * Now try to move the line as much as possible to |
| 212 | * the right. Stop when it moves too far. |
| 213 | */ |
| 214 | do |
| 215 | (void)set_indent(++new_indent, 0); |
| 216 | while (linelen(NULL) <= width); |
| 217 | --new_indent; |
| 218 | break; |
| 219 | } |
| 220 | --new_indent; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | if (new_indent < 0) |
| 225 | new_indent = 0; |
| 226 | (void)set_indent(new_indent, 0); /* set indent */ |
| 227 | } |
| 228 | changed_lines(eap->line1, 0, eap->line2 + 1, 0L); |
| 229 | curwin->w_cursor = save_curpos; |
| 230 | beginline(BL_WHITE | BL_FIX); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Get the length of the current line, excluding trailing white space. |
| 235 | */ |
| 236 | static int |
| 237 | linelen(has_tab) |
| 238 | int *has_tab; |
| 239 | { |
| 240 | char_u *line; |
| 241 | char_u *first; |
| 242 | char_u *last; |
| 243 | int save; |
| 244 | int len; |
| 245 | |
| 246 | /* find the first non-blank character */ |
| 247 | line = ml_get_curline(); |
| 248 | first = skipwhite(line); |
| 249 | |
| 250 | /* find the character after the last non-blank character */ |
| 251 | for (last = first + STRLEN(first); |
| 252 | last > first && vim_iswhite(last[-1]); --last) |
| 253 | ; |
| 254 | save = *last; |
| 255 | *last = NUL; |
| 256 | len = linetabsize(line); /* get line length */ |
| 257 | if (has_tab != NULL) /* check for embedded TAB */ |
| 258 | *has_tab = (vim_strrchr(first, TAB) != NULL); |
| 259 | *last = save; |
| 260 | |
| 261 | return len; |
| 262 | } |
| 263 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 264 | /* Buffer for one line used during sorting. It's allocated to contain the |
| 265 | * longest line being sorted. */ |
| 266 | static char_u *sortbuf; |
| 267 | |
| 268 | static int sort_ic; /* ignore case */ |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 269 | static int sort_nr; /* sort on number */ |
| 270 | |
| 271 | /* Struct to store info to be sorted. */ |
| 272 | typedef struct |
| 273 | { |
| 274 | linenr_T lnum; /* line number */ |
| 275 | long col_nr; /* column number or number */ |
| 276 | } sorti_T; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 277 | |
| 278 | static int |
| 279 | #ifdef __BORLANDC__ |
| 280 | _RTLENTRYF |
| 281 | #endif |
| 282 | sort_compare __ARGS((const void *s1, const void *s2)); |
| 283 | |
| 284 | static int |
| 285 | #ifdef __BORLANDC__ |
| 286 | _RTLENTRYF |
| 287 | #endif |
| 288 | sort_compare(s1, s2) |
| 289 | const void *s1; |
| 290 | const void *s2; |
| 291 | { |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 292 | sorti_T l1 = *(sorti_T *)s1; |
| 293 | sorti_T l2 = *(sorti_T *)s2; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 294 | char_u *s; |
| 295 | |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 296 | /* When sorting numbers "col_nr" is the number, not the column number. */ |
| 297 | if (sort_nr) |
| 298 | return l1.col_nr - l2.col_nr; |
| 299 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 300 | /* We need to copy one line into "sortbuf", because there is no guarantee |
| 301 | * that the first pointer becomes invalid when obtaining the second one. */ |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 302 | STRCPY(sortbuf, ml_get(l1.lnum) + l1.col_nr); |
| 303 | s = ml_get(l2.lnum) + l2.col_nr; |
| 304 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 305 | return sort_ic ? STRICMP(sortbuf, s) : STRCMP(sortbuf, s); |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * ":sort". |
| 310 | */ |
| 311 | void |
| 312 | ex_sort(eap) |
| 313 | exarg_T *eap; |
| 314 | { |
| 315 | regmatch_T regmatch; |
| 316 | int len; |
| 317 | linenr_T lnum; |
| 318 | long maxlen = 0; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 319 | sorti_T *nrs; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 320 | size_t count = eap->line2 - eap->line1 + 1; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 321 | size_t i; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 322 | char_u *p; |
| 323 | char_u *s; |
| 324 | int unique = FALSE; |
| 325 | long deleted; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 326 | colnr_T col; |
| 327 | int sort_oct; /* sort on octal number */ |
| 328 | int sort_hex; /* sort on hex number */ |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 329 | |
| 330 | if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) |
| 331 | return; |
| 332 | sortbuf = NULL; |
| 333 | regmatch.regprog = NULL; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 334 | nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 335 | if (nrs == NULL) |
| 336 | goto theend; |
| 337 | |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 338 | sort_ic = sort_nr = sort_oct = sort_hex = 0; |
| 339 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 340 | for (p = eap->arg; *p != NUL; ++p) |
| 341 | { |
| 342 | if (vim_iswhite(*p)) |
| 343 | ; |
| 344 | else if (*p == 'i') |
| 345 | sort_ic = TRUE; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 346 | else if (*p == 'n') |
| 347 | sort_nr = 2; |
| 348 | else if (*p == 'o') |
| 349 | sort_oct = 2; |
| 350 | else if (*p == 'x') |
| 351 | sort_hex = 2; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 352 | else if (*p == 'u') |
| 353 | unique = TRUE; |
Bram Moolenaar | 0e6830e | 2005-05-27 20:23:44 +0000 | [diff] [blame] | 354 | else if (*p == '"') /* comment start */ |
| 355 | break; |
| 356 | else if (check_nextcmd(p) != NULL) |
| 357 | { |
| 358 | eap->nextcmd = check_nextcmd(p); |
| 359 | break; |
| 360 | } |
| 361 | else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL) |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 362 | { |
| 363 | s = skip_regexp(p + 1, *p, TRUE, NULL); |
| 364 | if (*s != *p) |
| 365 | { |
| 366 | EMSG(_(e_invalpat)); |
| 367 | goto theend; |
| 368 | } |
| 369 | *s = NUL; |
| 370 | regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC); |
| 371 | if (regmatch.regprog == NULL) |
| 372 | goto theend; |
Bram Moolenaar | 0e6830e | 2005-05-27 20:23:44 +0000 | [diff] [blame] | 373 | p = s; /* continue after the regexp */ |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 374 | regmatch.rm_ic = p_ic; |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | EMSG2(_(e_invarg2), p); |
| 379 | goto theend; |
| 380 | } |
| 381 | } |
| 382 | |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 383 | /* Can only have one of 'n', 'o' and 'x'. */ |
| 384 | if (sort_nr + sort_oct + sort_hex > 2) |
| 385 | { |
| 386 | EMSG(_(e_invarg)); |
| 387 | goto theend; |
| 388 | } |
| 389 | |
| 390 | /* From here on "sort_nr" is used as a flag for any number sorting. */ |
| 391 | sort_nr += sort_oct + sort_hex; |
| 392 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 393 | /* |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 394 | * 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] | 395 | * the lines into allocated memory. |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 396 | * When sorting on strings "col_nr" is de offset in the line, for numbers |
| 397 | * sorting it's the number to sort on. This means the pattern matching |
| 398 | * and number conversion only has to be done once per line. |
| 399 | * Also get the longest line length for allocating "sortbuf". |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 400 | */ |
| 401 | for (lnum = eap->line1; lnum <= eap->line2; ++lnum) |
| 402 | { |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 403 | s = ml_get(lnum); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 404 | len = STRLEN(s); |
| 405 | if (maxlen < len) |
| 406 | maxlen = len; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 407 | |
| 408 | if (regmatch.regprog != NULL && vim_regexec(®match, s, 0)) |
| 409 | col = regmatch.endp[0] - s; |
| 410 | else |
| 411 | col = 0; |
| 412 | |
| 413 | if (sort_nr) |
| 414 | { |
| 415 | /* Sorting on number: Store the number itself. */ |
| 416 | if (sort_hex) |
| 417 | s = skiptohex(s + col); |
| 418 | else |
| 419 | s = skiptodigit(s + col); |
| 420 | vim_str2nr(s, NULL, NULL, sort_oct, sort_hex, |
| 421 | &nrs[lnum - eap->line1].col_nr, NULL); |
| 422 | } |
| 423 | else |
| 424 | /* Store the column to sort at. */ |
| 425 | nrs[lnum - eap->line1].col_nr = col; |
| 426 | |
| 427 | nrs[lnum - eap->line1].lnum = lnum; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 428 | |
| 429 | if (regmatch.regprog != NULL) |
| 430 | fast_breakcheck(); |
| 431 | if (got_int) |
| 432 | goto theend; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 435 | /* Allocate a buffer that can hold the longest line. */ |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 436 | sortbuf = alloc((unsigned)maxlen + 1); |
| 437 | if (sortbuf == NULL) |
| 438 | goto theend; |
| 439 | |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 440 | /* Sort the array of line numbers. Note: can't be interrupted! */ |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 441 | qsort((void *)nrs, count, sizeof(sorti_T), sort_compare); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 442 | |
| 443 | /* Insert the lines in the sorted order below the last one. */ |
| 444 | lnum = eap->line2; |
| 445 | for (i = 0; i < count; ++i) |
| 446 | { |
| 447 | s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum); |
| 448 | if (!unique || i == 0 |
| 449 | || (sort_ic ? STRICMP(s, sortbuf) : STRCMP(s, sortbuf)) != 0) |
| 450 | { |
| 451 | if (ml_append(lnum++, s, (colnr_T)0, FALSE) == FAIL) |
| 452 | break; |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 453 | if (unique) |
| 454 | STRCPY(sortbuf, s); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 455 | } |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 456 | fast_breakcheck(); |
| 457 | if (got_int) |
| 458 | goto theend; |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* delete the original lines if appending worked */ |
| 462 | if (i == count) |
| 463 | for (i = 0; i < count; ++i) |
| 464 | ml_delete(eap->line1, FALSE); |
| 465 | else |
| 466 | count = 0; |
| 467 | |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 468 | /* Adjust marks for deleted (or added) lines and prepare for displaying. */ |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 469 | deleted = count - (lnum - eap->line2); |
| 470 | if (deleted > 0) |
| 471 | mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted); |
| 472 | else if (deleted < 0) |
| 473 | mark_adjust(eap->line2, MAXLNUM, -deleted, 0L); |
| 474 | changed_lines(eap->line1, 0, eap->line2 + 1, -deleted); |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 475 | |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 476 | curwin->w_cursor.lnum = eap->line1; |
| 477 | beginline(BL_WHITE | BL_FIX); |
| 478 | |
| 479 | theend: |
| 480 | vim_free(nrs); |
| 481 | vim_free(sortbuf); |
| 482 | vim_free(regmatch.regprog); |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 483 | if (got_int) |
| 484 | EMSG(_(e_interr)); |
Bram Moolenaar | 67fe1a1 | 2005-05-22 22:12:58 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 487 | /* |
| 488 | * ":retab". |
| 489 | */ |
| 490 | void |
| 491 | ex_retab(eap) |
| 492 | exarg_T *eap; |
| 493 | { |
| 494 | linenr_T lnum; |
| 495 | int got_tab = FALSE; |
| 496 | long num_spaces = 0; |
| 497 | long num_tabs; |
| 498 | long len; |
| 499 | long col; |
| 500 | long vcol; |
| 501 | long start_col = 0; /* For start of white-space string */ |
| 502 | long start_vcol = 0; /* For start of white-space string */ |
| 503 | int temp; |
| 504 | long old_len; |
| 505 | char_u *ptr; |
| 506 | char_u *new_line = (char_u *)1; /* init to non-NULL */ |
| 507 | int did_undo; /* called u_save for current line */ |
| 508 | int new_ts; |
| 509 | int save_list; |
| 510 | linenr_T first_line = 0; /* first changed line */ |
| 511 | linenr_T last_line = 0; /* last changed line */ |
| 512 | |
| 513 | save_list = curwin->w_p_list; |
| 514 | curwin->w_p_list = 0; /* don't want list mode here */ |
| 515 | |
| 516 | new_ts = getdigits(&(eap->arg)); |
| 517 | if (new_ts < 0) |
| 518 | { |
| 519 | EMSG(_(e_positive)); |
| 520 | return; |
| 521 | } |
| 522 | if (new_ts == 0) |
| 523 | new_ts = curbuf->b_p_ts; |
| 524 | for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum) |
| 525 | { |
| 526 | ptr = ml_get(lnum); |
| 527 | col = 0; |
| 528 | vcol = 0; |
| 529 | did_undo = FALSE; |
| 530 | for (;;) |
| 531 | { |
| 532 | if (vim_iswhite(ptr[col])) |
| 533 | { |
| 534 | if (!got_tab && num_spaces == 0) |
| 535 | { |
| 536 | /* First consecutive white-space */ |
| 537 | start_vcol = vcol; |
| 538 | start_col = col; |
| 539 | } |
| 540 | if (ptr[col] == ' ') |
| 541 | num_spaces++; |
| 542 | else |
| 543 | got_tab = TRUE; |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | if (got_tab || (eap->forceit && num_spaces > 1)) |
| 548 | { |
| 549 | /* Retabulate this string of white-space */ |
| 550 | |
| 551 | /* len is virtual length of white string */ |
| 552 | len = num_spaces = vcol - start_vcol; |
| 553 | num_tabs = 0; |
| 554 | if (!curbuf->b_p_et) |
| 555 | { |
| 556 | temp = new_ts - (start_vcol % new_ts); |
| 557 | if (num_spaces >= temp) |
| 558 | { |
| 559 | num_spaces -= temp; |
| 560 | num_tabs++; |
| 561 | } |
| 562 | num_tabs += num_spaces / new_ts; |
| 563 | num_spaces -= (num_spaces / new_ts) * new_ts; |
| 564 | } |
| 565 | if (curbuf->b_p_et || got_tab || |
| 566 | (num_spaces + num_tabs < len)) |
| 567 | { |
| 568 | if (did_undo == FALSE) |
| 569 | { |
| 570 | did_undo = TRUE; |
| 571 | if (u_save((linenr_T)(lnum - 1), |
| 572 | (linenr_T)(lnum + 1)) == FAIL) |
| 573 | { |
| 574 | new_line = NULL; /* flag out-of-memory */ |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /* len is actual number of white characters used */ |
| 580 | len = num_spaces + num_tabs; |
| 581 | old_len = (long)STRLEN(ptr); |
| 582 | new_line = lalloc(old_len - col + start_col + len + 1, |
| 583 | TRUE); |
| 584 | if (new_line == NULL) |
| 585 | break; |
| 586 | if (start_col > 0) |
| 587 | mch_memmove(new_line, ptr, (size_t)start_col); |
| 588 | mch_memmove(new_line + start_col + len, |
| 589 | ptr + col, (size_t)(old_len - col + 1)); |
| 590 | ptr = new_line + start_col; |
| 591 | for (col = 0; col < len; col++) |
| 592 | ptr[col] = (col < num_tabs) ? '\t' : ' '; |
| 593 | ml_replace(lnum, new_line, FALSE); |
| 594 | if (first_line == 0) |
| 595 | first_line = lnum; |
| 596 | last_line = lnum; |
| 597 | ptr = new_line; |
| 598 | col = start_col + len; |
| 599 | } |
| 600 | } |
| 601 | got_tab = FALSE; |
| 602 | num_spaces = 0; |
| 603 | } |
| 604 | if (ptr[col] == NUL) |
| 605 | break; |
| 606 | vcol += chartabsize(ptr + col, (colnr_T)vcol); |
| 607 | #ifdef FEAT_MBYTE |
| 608 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 609 | col += (*mb_ptr2len)(ptr + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 610 | else |
| 611 | #endif |
| 612 | ++col; |
| 613 | } |
| 614 | if (new_line == NULL) /* out of memory */ |
| 615 | break; |
| 616 | line_breakcheck(); |
| 617 | } |
| 618 | if (got_int) |
| 619 | EMSG(_(e_interr)); |
| 620 | |
| 621 | if (curbuf->b_p_ts != new_ts) |
| 622 | redraw_curbuf_later(NOT_VALID); |
| 623 | if (first_line != 0) |
| 624 | changed_lines(first_line, 0, last_line + 1, 0L); |
| 625 | |
| 626 | curwin->w_p_list = save_list; /* restore 'list' */ |
| 627 | |
| 628 | curbuf->b_p_ts = new_ts; |
| 629 | coladvance(curwin->w_curswant); |
| 630 | |
| 631 | u_clearline(); |
| 632 | } |
| 633 | #endif |
| 634 | |
| 635 | /* |
| 636 | * :move command - move lines line1-line2 to line dest |
| 637 | * |
| 638 | * return FAIL for failure, OK otherwise |
| 639 | */ |
| 640 | int |
| 641 | do_move(line1, line2, dest) |
| 642 | linenr_T line1; |
| 643 | linenr_T line2; |
| 644 | linenr_T dest; |
| 645 | { |
| 646 | char_u *str; |
| 647 | linenr_T l; |
| 648 | linenr_T extra; /* Num lines added before line1 */ |
| 649 | linenr_T num_lines; /* Num lines moved */ |
| 650 | linenr_T last_line; /* Last line in file after adding new text */ |
| 651 | |
| 652 | if (dest >= line1 && dest < line2) |
| 653 | { |
| 654 | EMSG(_("E134: Move lines into themselves")); |
| 655 | return FAIL; |
| 656 | } |
| 657 | |
| 658 | num_lines = line2 - line1 + 1; |
| 659 | |
| 660 | /* |
| 661 | * First we copy the old text to its new location -- webb |
| 662 | * Also copy the flag that ":global" command uses. |
| 663 | */ |
| 664 | if (u_save(dest, dest + 1) == FAIL) |
| 665 | return FAIL; |
| 666 | for (extra = 0, l = line1; l <= line2; l++) |
| 667 | { |
| 668 | str = vim_strsave(ml_get(l + extra)); |
| 669 | if (str != NULL) |
| 670 | { |
| 671 | ml_append(dest + l - line1, str, (colnr_T)0, FALSE); |
| 672 | vim_free(str); |
| 673 | if (dest < line1) |
| 674 | extra++; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Now we must be careful adjusting our marks so that we don't overlap our |
| 680 | * mark_adjust() calls. |
| 681 | * |
| 682 | * We adjust the marks within the old text so that they refer to the |
| 683 | * last lines of the file (temporarily), because we know no other marks |
| 684 | * will be set there since these line numbers did not exist until we added |
| 685 | * our new lines. |
| 686 | * |
| 687 | * Then we adjust the marks on lines between the old and new text positions |
| 688 | * (either forwards or backwards). |
| 689 | * |
| 690 | * And Finally we adjust the marks we put at the end of the file back to |
| 691 | * their final destination at the new text position -- webb |
| 692 | */ |
| 693 | last_line = curbuf->b_ml.ml_line_count; |
| 694 | mark_adjust(line1, line2, last_line - line2, 0L); |
| 695 | if (dest >= line2) |
| 696 | { |
| 697 | mark_adjust(line2 + 1, dest, -num_lines, 0L); |
| 698 | curbuf->b_op_start.lnum = dest - num_lines + 1; |
| 699 | curbuf->b_op_end.lnum = dest; |
| 700 | } |
| 701 | else |
| 702 | { |
| 703 | mark_adjust(dest + 1, line1 - 1, num_lines, 0L); |
| 704 | curbuf->b_op_start.lnum = dest + 1; |
| 705 | curbuf->b_op_end.lnum = dest + num_lines; |
| 706 | } |
| 707 | curbuf->b_op_start.col = curbuf->b_op_end.col = 0; |
| 708 | mark_adjust(last_line - num_lines + 1, last_line, |
| 709 | -(last_line - dest - extra), 0L); |
| 710 | |
| 711 | /* |
| 712 | * Now we delete the original text -- webb |
| 713 | */ |
| 714 | if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL) |
| 715 | return FAIL; |
| 716 | |
| 717 | for (l = line1; l <= line2; l++) |
| 718 | ml_delete(line1 + extra, TRUE); |
| 719 | |
| 720 | if (!global_busy && num_lines > p_report) |
| 721 | { |
| 722 | if (num_lines == 1) |
| 723 | MSG(_("1 line moved")); |
| 724 | else |
| 725 | smsg((char_u *)_("%ld lines moved"), num_lines); |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * Leave the cursor on the last of the moved lines. |
| 730 | */ |
| 731 | if (dest >= line1) |
| 732 | curwin->w_cursor.lnum = dest; |
| 733 | else |
| 734 | curwin->w_cursor.lnum = dest + (line2 - line1) + 1; |
| 735 | |
| 736 | if (line1 < dest) |
| 737 | changed_lines(line1, 0, dest + num_lines + 1, 0L); |
| 738 | else |
| 739 | changed_lines(dest + 1, 0, line1 + num_lines, 0L); |
| 740 | |
| 741 | return OK; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * ":copy" |
| 746 | */ |
| 747 | void |
| 748 | ex_copy(line1, line2, n) |
| 749 | linenr_T line1; |
| 750 | linenr_T line2; |
| 751 | linenr_T n; |
| 752 | { |
| 753 | linenr_T count; |
| 754 | char_u *p; |
| 755 | |
| 756 | count = line2 - line1 + 1; |
| 757 | curbuf->b_op_start.lnum = n + 1; |
| 758 | curbuf->b_op_end.lnum = n + count; |
| 759 | curbuf->b_op_start.col = curbuf->b_op_end.col = 0; |
| 760 | |
| 761 | /* |
| 762 | * there are three situations: |
| 763 | * 1. destination is above line1 |
| 764 | * 2. destination is between line1 and line2 |
| 765 | * 3. destination is below line2 |
| 766 | * |
| 767 | * n = destination (when starting) |
| 768 | * curwin->w_cursor.lnum = destination (while copying) |
| 769 | * line1 = start of source (while copying) |
| 770 | * line2 = end of source (while copying) |
| 771 | */ |
| 772 | if (u_save(n, n + 1) == FAIL) |
| 773 | return; |
| 774 | |
| 775 | curwin->w_cursor.lnum = n; |
| 776 | while (line1 <= line2) |
| 777 | { |
| 778 | /* need to use vim_strsave() because the line will be unlocked within |
| 779 | * ml_append() */ |
| 780 | p = vim_strsave(ml_get(line1)); |
| 781 | if (p != NULL) |
| 782 | { |
| 783 | ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE); |
| 784 | vim_free(p); |
| 785 | } |
| 786 | /* situation 2: skip already copied lines */ |
| 787 | if (line1 == n) |
| 788 | line1 = curwin->w_cursor.lnum; |
| 789 | ++line1; |
| 790 | if (curwin->w_cursor.lnum < line1) |
| 791 | ++line1; |
| 792 | if (curwin->w_cursor.lnum < line2) |
| 793 | ++line2; |
| 794 | ++curwin->w_cursor.lnum; |
| 795 | } |
| 796 | |
| 797 | appended_lines_mark(n, count); |
| 798 | |
| 799 | msgmore((long)count); |
| 800 | } |
| 801 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 802 | static char_u *prevcmd = NULL; /* the previous command */ |
| 803 | |
| 804 | #if defined(EXITFREE) || defined(PROTO) |
| 805 | void |
| 806 | free_prev_shellcmd() |
| 807 | { |
| 808 | vim_free(prevcmd); |
| 809 | } |
| 810 | #endif |
| 811 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 812 | /* |
| 813 | * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" |
| 814 | * Bangs in the argument are replaced with the previously entered command. |
| 815 | * Remember the argument. |
| 816 | * |
| 817 | * RISCOS: Bangs only replaced when followed by a space, since many |
| 818 | * pathnames contain one. |
| 819 | */ |
| 820 | void |
| 821 | do_bang(addr_count, eap, forceit, do_in, do_out) |
| 822 | int addr_count; |
| 823 | exarg_T *eap; |
| 824 | int forceit; |
| 825 | int do_in, do_out; |
| 826 | { |
| 827 | char_u *arg = eap->arg; /* command */ |
| 828 | linenr_T line1 = eap->line1; /* start of range */ |
| 829 | linenr_T line2 = eap->line2; /* end of range */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 830 | char_u *newcmd = NULL; /* the new command */ |
| 831 | int free_newcmd = FALSE; /* need to free() newcmd */ |
| 832 | int ins_prevcmd; |
| 833 | char_u *t; |
| 834 | char_u *p; |
| 835 | char_u *trailarg; |
| 836 | int len; |
| 837 | int scroll_save = msg_scroll; |
| 838 | |
| 839 | /* |
| 840 | * Disallow shell commands for "rvim". |
| 841 | * Disallow shell commands from .exrc and .vimrc in current directory for |
| 842 | * security reasons. |
| 843 | */ |
| 844 | if (check_restricted() || check_secure()) |
| 845 | return; |
| 846 | |
| 847 | if (addr_count == 0) /* :! */ |
| 848 | { |
| 849 | msg_scroll = FALSE; /* don't scroll here */ |
| 850 | autowrite_all(); |
| 851 | msg_scroll = scroll_save; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * Try to find an embedded bang, like in :!<cmd> ! [args] |
| 856 | * (:!! is indicated by the 'forceit' variable) |
| 857 | */ |
| 858 | ins_prevcmd = forceit; |
| 859 | trailarg = arg; |
| 860 | do |
| 861 | { |
| 862 | len = (int)STRLEN(trailarg) + 1; |
| 863 | if (newcmd != NULL) |
| 864 | len += (int)STRLEN(newcmd); |
| 865 | if (ins_prevcmd) |
| 866 | { |
| 867 | if (prevcmd == NULL) |
| 868 | { |
| 869 | EMSG(_(e_noprev)); |
| 870 | vim_free(newcmd); |
| 871 | return; |
| 872 | } |
| 873 | len += (int)STRLEN(prevcmd); |
| 874 | } |
| 875 | if ((t = alloc(len)) == NULL) |
| 876 | { |
| 877 | vim_free(newcmd); |
| 878 | return; |
| 879 | } |
| 880 | *t = NUL; |
| 881 | if (newcmd != NULL) |
| 882 | STRCAT(t, newcmd); |
| 883 | if (ins_prevcmd) |
| 884 | STRCAT(t, prevcmd); |
| 885 | p = t + STRLEN(t); |
| 886 | STRCAT(t, trailarg); |
| 887 | vim_free(newcmd); |
| 888 | newcmd = t; |
| 889 | |
| 890 | /* |
| 891 | * Scan the rest of the argument for '!', which is replaced by the |
| 892 | * previous command. "\!" is replaced by "!" (this is vi compatible). |
| 893 | */ |
| 894 | trailarg = NULL; |
| 895 | while (*p) |
| 896 | { |
| 897 | if (*p == '!' |
| 898 | #ifdef RISCOS |
| 899 | && (p[1] == ' ' || p[1] == NUL) |
| 900 | #endif |
| 901 | ) |
| 902 | { |
| 903 | if (p > newcmd && p[-1] == '\\') |
| 904 | mch_memmove(p - 1, p, (size_t)(STRLEN(p) + 1)); |
| 905 | else |
| 906 | { |
| 907 | trailarg = p; |
| 908 | *trailarg++ = NUL; |
| 909 | ins_prevcmd = TRUE; |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | ++p; |
| 914 | } |
| 915 | } while (trailarg != NULL); |
| 916 | |
| 917 | vim_free(prevcmd); |
| 918 | prevcmd = newcmd; |
| 919 | |
| 920 | if (bangredo) /* put cmd in redo buffer for ! command */ |
| 921 | { |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 922 | AppendToRedobuffLit(prevcmd, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 923 | AppendToRedobuff((char_u *)"\n"); |
| 924 | bangredo = FALSE; |
| 925 | } |
| 926 | /* |
| 927 | * Add quotes around the command, for shells that need them. |
| 928 | */ |
| 929 | if (*p_shq != NUL) |
| 930 | { |
| 931 | newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1)); |
| 932 | if (newcmd == NULL) |
| 933 | return; |
| 934 | STRCPY(newcmd, p_shq); |
| 935 | STRCAT(newcmd, prevcmd); |
| 936 | STRCAT(newcmd, p_shq); |
| 937 | free_newcmd = TRUE; |
| 938 | } |
| 939 | if (addr_count == 0) /* :! */ |
| 940 | { |
| 941 | /* echo the command */ |
| 942 | msg_start(); |
| 943 | msg_putchar(':'); |
| 944 | msg_putchar('!'); |
| 945 | msg_outtrans(newcmd); |
| 946 | msg_clr_eos(); |
| 947 | windgoto(msg_row, msg_col); |
| 948 | |
| 949 | do_shell(newcmd, 0); |
| 950 | } |
| 951 | else /* :range! */ |
Bram Moolenaar | ade0083 | 2006-03-10 21:46:58 +0000 | [diff] [blame] | 952 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 953 | /* Careful: This may recursively call do_bang() again! (because of |
| 954 | * autocommands) */ |
| 955 | do_filter(line1, line2, eap, newcmd, do_in, do_out); |
Bram Moolenaar | ade0083 | 2006-03-10 21:46:58 +0000 | [diff] [blame] | 956 | #ifdef FEAT_AUTOCMD |
| 957 | apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf); |
| 958 | #endif |
| 959 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 960 | if (free_newcmd) |
| 961 | vim_free(newcmd); |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | * do_filter: filter lines through a command given by the user |
| 966 | * |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 967 | * We mostly use temp files and the call_shell() routine here. This would |
| 968 | * normally be done using pipes on a UNIX machine, but this is more portable |
| 969 | * to non-unix machines. The call_shell() routine needs to be able |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 970 | * to deal with redirection somehow, and should handle things like looking |
| 971 | * at the PATH env. variable, and adding reasonable extensions to the |
| 972 | * command name given by the user. All reasonable versions of call_shell() |
| 973 | * do this. |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 974 | * Alternatively, if on Unix and redirecting input or output, but not both, |
| 975 | * and the 'shelltemp' option isn't set, use pipes. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 976 | * We use input redirection if do_in is TRUE. |
| 977 | * We use output redirection if do_out is TRUE. |
| 978 | */ |
| 979 | static void |
| 980 | do_filter(line1, line2, eap, cmd, do_in, do_out) |
| 981 | linenr_T line1, line2; |
| 982 | exarg_T *eap; /* for forced 'ff' and 'fenc' */ |
| 983 | char_u *cmd; |
| 984 | int do_in, do_out; |
| 985 | { |
| 986 | char_u *itmp = NULL; |
| 987 | char_u *otmp = NULL; |
| 988 | linenr_T linecount; |
| 989 | linenr_T read_linecount; |
| 990 | pos_T cursor_save; |
| 991 | char_u *cmd_buf; |
| 992 | #ifdef FEAT_AUTOCMD |
| 993 | buf_T *old_curbuf = curbuf; |
| 994 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 995 | int shell_flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 996 | |
| 997 | if (*cmd == NUL) /* no filter command */ |
| 998 | return; |
| 999 | |
| 1000 | #ifdef WIN3264 |
| 1001 | /* |
| 1002 | * Check if external commands are allowed now. |
| 1003 | */ |
| 1004 | if (can_end_termcap_mode(TRUE) == FALSE) |
| 1005 | return; |
| 1006 | #endif |
| 1007 | |
| 1008 | cursor_save = curwin->w_cursor; |
| 1009 | linecount = line2 - line1 + 1; |
| 1010 | curwin->w_cursor.lnum = line1; |
| 1011 | curwin->w_cursor.col = 0; |
| 1012 | changed_line_abv_curs(); |
| 1013 | invalidate_botline(); |
| 1014 | |
| 1015 | /* |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1016 | * When using temp files: |
| 1017 | * 1. * Form temp file names |
| 1018 | * 2. * Write the lines to a temp file |
| 1019 | * 3. Run the filter command on the temp file |
| 1020 | * 4. * Read the output of the command into the buffer |
| 1021 | * 5. * Delete the original lines to be filtered |
| 1022 | * 6. * Remove the temp files |
| 1023 | * |
| 1024 | * When writing the input with a pipe or when catching the output with a |
| 1025 | * pipe only need to do 3. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1026 | */ |
| 1027 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1028 | if (do_out) |
| 1029 | shell_flags |= SHELL_DOOUT; |
| 1030 | |
| 1031 | #if !defined(USE_SYSTEM) && defined(UNIX) |
| 1032 | if (!do_in && do_out && !p_stmp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1033 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1034 | /* Use a pipe to fetch stdout of the command, do not use a temp file. */ |
| 1035 | shell_flags |= SHELL_READ; |
| 1036 | curwin->w_cursor.lnum = line2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1037 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1038 | else if (do_in && !do_out && !p_stmp) |
| 1039 | { |
| 1040 | /* Use a pipe to write stdin of the command, do not use a temp file. */ |
| 1041 | shell_flags |= SHELL_WRITE; |
| 1042 | curbuf->b_op_start.lnum = line1; |
| 1043 | curbuf->b_op_end.lnum = line2; |
| 1044 | } |
| 1045 | else if (do_in && do_out && !p_stmp) |
| 1046 | { |
| 1047 | /* Use a pipe to write stdin and fetch stdout of the command, do not |
| 1048 | * use a temp file. */ |
| 1049 | shell_flags |= SHELL_READ|SHELL_WRITE; |
| 1050 | curbuf->b_op_start.lnum = line1; |
| 1051 | curbuf->b_op_end.lnum = line2; |
| 1052 | curwin->w_cursor.lnum = line2; |
| 1053 | } |
| 1054 | else |
| 1055 | #endif |
| 1056 | if ((do_in && (itmp = vim_tempname('i')) == NULL) |
| 1057 | || (do_out && (otmp = vim_tempname('o')) == NULL)) |
| 1058 | { |
| 1059 | EMSG(_(e_notmp)); |
| 1060 | goto filterend; |
| 1061 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1062 | |
| 1063 | /* |
| 1064 | * The writing and reading of temp files will not be shown. |
| 1065 | * Vi also doesn't do this and the messages are not very informative. |
| 1066 | */ |
| 1067 | ++no_wait_return; /* don't call wait_return() while busy */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1068 | if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1069 | FALSE, FALSE, FALSE, TRUE) == FAIL) |
| 1070 | { |
| 1071 | msg_putchar('\n'); /* keep message from buf_write() */ |
| 1072 | --no_wait_return; |
| 1073 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 1074 | if (!aborting()) |
| 1075 | #endif |
| 1076 | (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */ |
| 1077 | goto filterend; |
| 1078 | } |
| 1079 | #ifdef FEAT_AUTOCMD |
| 1080 | if (curbuf != old_curbuf) |
| 1081 | goto filterend; |
| 1082 | #endif |
| 1083 | |
| 1084 | if (!do_out) |
| 1085 | msg_putchar('\n'); |
| 1086 | |
| 1087 | cmd_buf = make_filter_cmd(cmd, itmp, otmp); |
| 1088 | if (cmd_buf == NULL) |
| 1089 | goto filterend; |
| 1090 | |
| 1091 | windgoto((int)Rows - 1, 0); |
| 1092 | cursor_on(); |
| 1093 | |
| 1094 | /* |
| 1095 | * When not redirecting the output the command can write anything to the |
| 1096 | * screen. If 'shellredir' is equal to ">", screen may be messed up by |
| 1097 | * stderr output of external command. Clear the screen later. |
| 1098 | * If do_in is FALSE, this could be something like ":r !cat", which may |
| 1099 | * also mess up the screen, clear it later. |
| 1100 | */ |
| 1101 | if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in) |
| 1102 | redraw_later_clear(); |
| 1103 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1104 | if (do_out) |
| 1105 | { |
| 1106 | if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL) |
| 1107 | goto error; |
| 1108 | redraw_curbuf_later(VALID); |
| 1109 | } |
| 1110 | read_linecount = curbuf->b_ml.ml_line_count; |
| 1111 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1112 | /* |
| 1113 | * When call_shell() fails wait_return() is called to give the user a |
| 1114 | * chance to read the error messages. Otherwise errors are ignored, so you |
| 1115 | * can see the error messages from the command that appear on stdout; use |
| 1116 | * 'u' to fix the text |
| 1117 | * Switch to cooked mode when not redirecting stdin, avoids that something |
| 1118 | * like ":r !cat" hangs. |
| 1119 | * Pass on the SHELL_DOOUT flag when the output is being redirected. |
| 1120 | */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1121 | if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1122 | { |
| 1123 | redraw_later_clear(); |
| 1124 | wait_return(FALSE); |
| 1125 | } |
| 1126 | vim_free(cmd_buf); |
| 1127 | |
| 1128 | did_check_timestamps = FALSE; |
| 1129 | need_check_timestamps = TRUE; |
| 1130 | |
| 1131 | /* When interrupting the shell command, it may still have produced some |
| 1132 | * useful output. Reset got_int here, so that readfile() won't cancel |
| 1133 | * reading. */ |
| 1134 | ui_breakcheck(); |
| 1135 | got_int = FALSE; |
| 1136 | |
| 1137 | if (do_out) |
| 1138 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1139 | if (otmp != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1140 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1141 | if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM, |
| 1142 | eap, READ_FILTER) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1143 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1144 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 1145 | if (!aborting()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1146 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1147 | { |
| 1148 | msg_putchar('\n'); |
| 1149 | EMSG2(_(e_notread), otmp); |
| 1150 | } |
| 1151 | goto error; |
| 1152 | } |
| 1153 | #ifdef FEAT_AUTOCMD |
| 1154 | if (curbuf != old_curbuf) |
| 1155 | goto filterend; |
| 1156 | #endif |
| 1157 | } |
| 1158 | |
| 1159 | read_linecount = curbuf->b_ml.ml_line_count - read_linecount; |
| 1160 | |
| 1161 | if (shell_flags & SHELL_READ) |
| 1162 | { |
| 1163 | curbuf->b_op_start.lnum = line2 + 1; |
| 1164 | curbuf->b_op_end.lnum = curwin->w_cursor.lnum; |
| 1165 | appended_lines_mark(line2, read_linecount); |
| 1166 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1167 | |
| 1168 | if (do_in) |
| 1169 | { |
| 1170 | if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL) |
| 1171 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1172 | if (read_linecount >= linecount) |
| 1173 | /* move all marks from old lines to new lines */ |
| 1174 | mark_adjust(line1, line2, linecount, 0L); |
| 1175 | else |
| 1176 | { |
| 1177 | /* move marks from old lines to new lines, delete marks |
| 1178 | * that are in deleted lines */ |
| 1179 | mark_adjust(line1, line1 + read_linecount - 1, |
| 1180 | linecount, 0L); |
| 1181 | mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * Put cursor on first filtered line for ":range!cmd". |
| 1187 | * Adjust '[ and '] (set by buf_write()). |
| 1188 | */ |
| 1189 | curwin->w_cursor.lnum = line1; |
| 1190 | del_lines(linecount, TRUE); |
| 1191 | curbuf->b_op_start.lnum -= linecount; /* adjust '[ */ |
| 1192 | curbuf->b_op_end.lnum -= linecount; /* adjust '] */ |
| 1193 | write_lnum_adjust(-linecount); /* adjust last line |
| 1194 | for next write */ |
Bram Moolenaar | 8c71145 | 2005-01-14 21:53:12 +0000 | [diff] [blame] | 1195 | #ifdef FEAT_FOLDING |
| 1196 | foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum); |
| 1197 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1198 | } |
| 1199 | else |
| 1200 | { |
| 1201 | /* |
| 1202 | * Put cursor on last new line for ":r !cmd". |
| 1203 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1204 | linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1205 | curwin->w_cursor.lnum = curbuf->b_op_end.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1206 | } |
Bram Moolenaar | 8c71145 | 2005-01-14 21:53:12 +0000 | [diff] [blame] | 1207 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1208 | beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */ |
| 1209 | --no_wait_return; |
| 1210 | |
| 1211 | if (linecount > p_report) |
| 1212 | { |
| 1213 | if (do_in) |
| 1214 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1215 | vim_snprintf((char *)msg_buf, sizeof(msg_buf), |
| 1216 | _("%ld lines filtered"), (long)linecount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1217 | if (msg(msg_buf) && !msg_scroll) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1218 | /* save message to display it after redraw */ |
Bram Moolenaar | 238a564 | 2006-02-21 22:12:05 +0000 | [diff] [blame] | 1219 | set_keep_msg(msg_buf, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1220 | } |
| 1221 | else |
| 1222 | msgmore((long)linecount); |
| 1223 | } |
| 1224 | } |
| 1225 | else |
| 1226 | { |
| 1227 | error: |
| 1228 | /* put cursor back in same position for ":w !cmd" */ |
| 1229 | curwin->w_cursor = cursor_save; |
| 1230 | --no_wait_return; |
| 1231 | wait_return(FALSE); |
| 1232 | } |
| 1233 | |
| 1234 | filterend: |
| 1235 | |
| 1236 | #ifdef FEAT_AUTOCMD |
| 1237 | if (curbuf != old_curbuf) |
| 1238 | { |
| 1239 | --no_wait_return; |
| 1240 | EMSG(_("E135: *Filter* Autocommands must not change current buffer")); |
| 1241 | } |
| 1242 | #endif |
| 1243 | if (itmp != NULL) |
| 1244 | mch_remove(itmp); |
| 1245 | if (otmp != NULL) |
| 1246 | mch_remove(otmp); |
| 1247 | vim_free(itmp); |
| 1248 | vim_free(otmp); |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | * Call a shell to execute a command. |
| 1253 | * When "cmd" is NULL start an interactive shell. |
| 1254 | */ |
| 1255 | void |
| 1256 | do_shell(cmd, flags) |
| 1257 | char_u *cmd; |
| 1258 | int flags; /* may be SHELL_DOOUT when output is redirected */ |
| 1259 | { |
| 1260 | buf_T *buf; |
| 1261 | #ifndef FEAT_GUI_MSWIN |
| 1262 | int save_nwr; |
| 1263 | #endif |
| 1264 | #ifdef MSWIN |
| 1265 | int winstart = FALSE; |
| 1266 | #endif |
| 1267 | |
| 1268 | /* |
| 1269 | * Disallow shell commands for "rvim". |
| 1270 | * Disallow shell commands from .exrc and .vimrc in current directory for |
| 1271 | * security reasons. |
| 1272 | */ |
| 1273 | if (check_restricted() || check_secure()) |
| 1274 | { |
| 1275 | msg_end(); |
| 1276 | return; |
| 1277 | } |
| 1278 | |
| 1279 | #ifdef MSWIN |
| 1280 | /* |
| 1281 | * Check if external commands are allowed now. |
| 1282 | */ |
| 1283 | if (can_end_termcap_mode(TRUE) == FALSE) |
| 1284 | return; |
| 1285 | |
| 1286 | /* |
| 1287 | * Check if ":!start" is used. |
| 1288 | */ |
| 1289 | if (cmd != NULL) |
| 1290 | winstart = (STRNICMP(cmd, "start ", 6) == 0); |
| 1291 | #endif |
| 1292 | |
| 1293 | /* |
| 1294 | * For autocommands we want to get the output on the current screen, to |
| 1295 | * avoid having to type return below. |
| 1296 | */ |
| 1297 | msg_putchar('\r'); /* put cursor at start of line */ |
| 1298 | #ifdef FEAT_AUTOCMD |
| 1299 | if (!autocmd_busy) |
| 1300 | #endif |
| 1301 | { |
| 1302 | #ifdef MSWIN |
| 1303 | if (!winstart) |
| 1304 | #endif |
| 1305 | stoptermcap(); |
| 1306 | } |
| 1307 | #ifdef MSWIN |
| 1308 | if (!winstart) |
| 1309 | #endif |
| 1310 | msg_putchar('\n'); /* may shift screen one line up */ |
| 1311 | |
| 1312 | /* warning message before calling the shell */ |
| 1313 | if (p_warn |
| 1314 | #ifdef FEAT_AUTOCMD |
| 1315 | && !autocmd_busy |
| 1316 | #endif |
| 1317 | && msg_silent == 0) |
| 1318 | for (buf = firstbuf; buf; buf = buf->b_next) |
| 1319 | if (bufIsChanged(buf)) |
| 1320 | { |
| 1321 | #ifdef FEAT_GUI_MSWIN |
| 1322 | if (!winstart) |
| 1323 | starttermcap(); /* don't want a message box here */ |
| 1324 | #endif |
| 1325 | MSG_PUTS(_("[No write since last change]\n")); |
| 1326 | #ifdef FEAT_GUI_MSWIN |
| 1327 | if (!winstart) |
| 1328 | stoptermcap(); |
| 1329 | #endif |
| 1330 | break; |
| 1331 | } |
| 1332 | |
| 1333 | /* This windgoto is required for when the '\n' resulted in a "delete line |
| 1334 | * 1" command to the terminal. */ |
| 1335 | if (!swapping_screen()) |
| 1336 | windgoto(msg_row, msg_col); |
| 1337 | cursor_on(); |
| 1338 | (void)call_shell(cmd, SHELL_COOKED | flags); |
| 1339 | did_check_timestamps = FALSE; |
| 1340 | need_check_timestamps = TRUE; |
| 1341 | |
| 1342 | /* |
| 1343 | * put the message cursor at the end of the screen, avoids wait_return() |
| 1344 | * to overwrite the text that the external command showed |
| 1345 | */ |
| 1346 | if (!swapping_screen()) |
| 1347 | { |
| 1348 | msg_row = Rows - 1; |
| 1349 | msg_col = 0; |
| 1350 | } |
| 1351 | |
| 1352 | #ifdef FEAT_AUTOCMD |
| 1353 | if (autocmd_busy) |
| 1354 | { |
| 1355 | if (msg_silent == 0) |
| 1356 | redraw_later_clear(); |
| 1357 | } |
| 1358 | else |
| 1359 | #endif |
| 1360 | { |
| 1361 | /* |
| 1362 | * For ":sh" there is no need to call wait_return(), just redraw. |
| 1363 | * Also for the Win32 GUI (the output is in a console window). |
| 1364 | * Otherwise there is probably text on the screen that the user wants |
| 1365 | * to read before redrawing, so call wait_return(). |
| 1366 | */ |
| 1367 | #ifndef FEAT_GUI_MSWIN |
| 1368 | if (cmd == NULL |
| 1369 | # ifdef WIN3264 |
| 1370 | || (winstart && !need_wait_return) |
| 1371 | # endif |
| 1372 | ) |
| 1373 | { |
| 1374 | if (msg_silent == 0) |
| 1375 | redraw_later_clear(); |
| 1376 | need_wait_return = FALSE; |
| 1377 | } |
| 1378 | else |
| 1379 | { |
| 1380 | /* |
| 1381 | * If we switch screens when starttermcap() is called, we really |
| 1382 | * want to wait for "hit return to continue". |
| 1383 | */ |
| 1384 | save_nwr = no_wait_return; |
| 1385 | if (swapping_screen()) |
| 1386 | no_wait_return = FALSE; |
| 1387 | # ifdef AMIGA |
| 1388 | wait_return(term_console ? -1 : msg_silent == 0); /* see below */ |
| 1389 | # else |
| 1390 | wait_return(msg_silent == 0); |
| 1391 | # endif |
| 1392 | no_wait_return = save_nwr; |
| 1393 | } |
| 1394 | #endif /* FEAT_GUI_W32 */ |
| 1395 | |
| 1396 | #ifdef MSWIN |
| 1397 | if (!winstart) /* if winstart==TRUE, never stopped termcap! */ |
| 1398 | #endif |
| 1399 | starttermcap(); /* start termcap if not done by wait_return() */ |
| 1400 | |
| 1401 | /* |
| 1402 | * In an Amiga window redrawing is caused by asking the window size. |
| 1403 | * If we got an interrupt this will not work. The chance that the |
| 1404 | * window size is wrong is very small, but we need to redraw the |
| 1405 | * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY |
| 1406 | * but it saves an extra redraw. |
| 1407 | */ |
| 1408 | #ifdef AMIGA |
| 1409 | if (skip_redraw) /* ':' hit in wait_return() */ |
| 1410 | { |
| 1411 | if (msg_silent == 0) |
| 1412 | redraw_later_clear(); |
| 1413 | } |
| 1414 | else if (term_console) |
| 1415 | { |
| 1416 | OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */ |
| 1417 | if (got_int && msg_silent == 0) |
| 1418 | redraw_later_clear(); /* if got_int is TRUE, redraw needed */ |
| 1419 | else |
| 1420 | must_redraw = 0; /* no extra redraw needed */ |
| 1421 | } |
| 1422 | #endif |
| 1423 | } |
| 1424 | |
| 1425 | /* display any error messages now */ |
| 1426 | display_errors(); |
Bram Moolenaar | ade0083 | 2006-03-10 21:46:58 +0000 | [diff] [blame] | 1427 | |
| 1428 | #ifdef FEAT_AUTOCMD |
| 1429 | apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf); |
| 1430 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * Create a shell command from a command string, input redirection file and |
| 1435 | * output redirection file. |
| 1436 | * Returns an allocated string with the shell command, or NULL for failure. |
| 1437 | */ |
| 1438 | char_u * |
| 1439 | make_filter_cmd(cmd, itmp, otmp) |
| 1440 | char_u *cmd; /* command */ |
| 1441 | char_u *itmp; /* NULL or name of input file */ |
| 1442 | char_u *otmp; /* NULL or name of output file */ |
| 1443 | { |
| 1444 | char_u *buf; |
| 1445 | long_u len; |
| 1446 | |
| 1447 | len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */ |
| 1448 | if (itmp != NULL) |
| 1449 | len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */ |
| 1450 | if (otmp != NULL) |
| 1451 | len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */ |
| 1452 | buf = lalloc(len, TRUE); |
| 1453 | if (buf == NULL) |
| 1454 | return NULL; |
| 1455 | |
| 1456 | #if (defined(UNIX) && !defined(ARCHIE)) || defined(OS2) |
| 1457 | /* |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1458 | * Put braces around the command (for concatenated commands) when |
| 1459 | * redirecting input and/or output. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1460 | */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1461 | if (itmp != NULL || otmp != NULL) |
| 1462 | sprintf((char *)buf, "(%s)", (char *)cmd); |
| 1463 | else |
| 1464 | STRCPY(buf, cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1465 | if (itmp != NULL) |
| 1466 | { |
| 1467 | STRCAT(buf, " < "); |
| 1468 | STRCAT(buf, itmp); |
| 1469 | } |
| 1470 | #else |
| 1471 | /* |
| 1472 | * for shells that don't understand braces around commands, at least allow |
| 1473 | * the use of commands in a pipe. |
| 1474 | */ |
| 1475 | STRCPY(buf, cmd); |
| 1476 | if (itmp != NULL) |
| 1477 | { |
| 1478 | char_u *p; |
| 1479 | |
| 1480 | /* |
| 1481 | * If there is a pipe, we have to put the '<' in front of it. |
| 1482 | * Don't do this when 'shellquote' is not empty, otherwise the |
| 1483 | * redirection would be inside the quotes. |
| 1484 | */ |
| 1485 | if (*p_shq == NUL) |
| 1486 | { |
| 1487 | p = vim_strchr(buf, '|'); |
| 1488 | if (p != NULL) |
| 1489 | *p = NUL; |
| 1490 | } |
| 1491 | # ifdef RISCOS |
| 1492 | STRCAT(buf, " { < "); /* Use RISC OS notation for input. */ |
| 1493 | STRCAT(buf, itmp); |
| 1494 | STRCAT(buf, " } "); |
| 1495 | # else |
| 1496 | STRCAT(buf, " <"); /* " < " causes problems on Amiga */ |
| 1497 | STRCAT(buf, itmp); |
| 1498 | # endif |
| 1499 | if (*p_shq == NUL) |
| 1500 | { |
| 1501 | p = vim_strchr(cmd, '|'); |
| 1502 | if (p != NULL) |
| 1503 | { |
| 1504 | STRCAT(buf, " "); /* insert a space before the '|' for DOS */ |
| 1505 | STRCAT(buf, p); |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | #endif |
| 1510 | if (otmp != NULL) |
| 1511 | append_redir(buf, p_srr, otmp); |
| 1512 | |
| 1513 | return buf; |
| 1514 | } |
| 1515 | |
| 1516 | /* |
| 1517 | * Append output redirection for file "fname" to the end of string buffer "buf" |
| 1518 | * Works with the 'shellredir' and 'shellpipe' options. |
| 1519 | * The caller should make sure that there is enough room: |
| 1520 | * STRLEN(opt) + STRLEN(fname) + 3 |
| 1521 | */ |
| 1522 | void |
| 1523 | append_redir(buf, opt, fname) |
| 1524 | char_u *buf; |
| 1525 | char_u *opt; |
| 1526 | char_u *fname; |
| 1527 | { |
| 1528 | char_u *p; |
| 1529 | |
| 1530 | buf += STRLEN(buf); |
| 1531 | /* find "%s", skipping "%%" */ |
| 1532 | for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p) |
| 1533 | if (p[1] == 's') |
| 1534 | break; |
| 1535 | if (p != NULL) |
| 1536 | { |
| 1537 | *buf = ' '; /* not really needed? Not with sh, ksh or bash */ |
| 1538 | sprintf((char *)buf + 1, (char *)opt, (char *)fname); |
| 1539 | } |
| 1540 | else |
| 1541 | sprintf((char *)buf, |
| 1542 | #ifdef FEAT_QUICKFIX |
| 1543 | # ifndef RISCOS |
| 1544 | opt != p_sp ? " %s%s" : |
| 1545 | # endif |
| 1546 | " %s %s", |
| 1547 | #else |
| 1548 | # ifndef RISCOS |
| 1549 | " %s%s", /* " > %s" causes problems on Amiga */ |
| 1550 | # else |
| 1551 | " %s %s", /* But is needed for 'shellpipe' and RISC OS */ |
| 1552 | # endif |
| 1553 | #endif |
| 1554 | (char *)opt, (char *)fname); |
| 1555 | } |
| 1556 | |
| 1557 | #ifdef FEAT_VIMINFO |
| 1558 | |
| 1559 | static int no_viminfo __ARGS((void)); |
| 1560 | static int viminfo_errcnt; |
| 1561 | |
| 1562 | static int |
| 1563 | no_viminfo() |
| 1564 | { |
| 1565 | /* "vim -i NONE" does not read or write a viminfo file */ |
| 1566 | return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0); |
| 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * Report an error for reading a viminfo file. |
| 1571 | * Count the number of errors. When there are more than 10, return TRUE. |
| 1572 | */ |
| 1573 | int |
| 1574 | viminfo_error(errnum, message, line) |
| 1575 | char *errnum; |
| 1576 | char *message; |
| 1577 | char_u *line; |
| 1578 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1579 | vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "), |
| 1580 | errnum, message); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1581 | STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff)); |
Bram Moolenaar | 758711c | 2005-02-02 23:11:38 +0000 | [diff] [blame] | 1582 | if (IObuff[STRLEN(IObuff) - 1] == '\n') |
| 1583 | IObuff[STRLEN(IObuff) - 1] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1584 | emsg(IObuff); |
| 1585 | if (++viminfo_errcnt >= 10) |
| 1586 | { |
| 1587 | EMSG(_("E136: viminfo: Too many errors, skipping rest of file")); |
| 1588 | return TRUE; |
| 1589 | } |
| 1590 | return FALSE; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * read_viminfo() -- Read the viminfo file. Registers etc. which are already |
| 1595 | * set are not over-written unless force is TRUE. -- webb |
| 1596 | */ |
| 1597 | int |
| 1598 | read_viminfo(file, want_info, want_marks, forceit) |
| 1599 | char_u *file; |
| 1600 | int want_info; |
| 1601 | int want_marks; |
| 1602 | int forceit; |
| 1603 | { |
| 1604 | FILE *fp; |
| 1605 | char_u *fname; |
| 1606 | |
| 1607 | if (no_viminfo()) |
| 1608 | return FAIL; |
| 1609 | |
| 1610 | fname = viminfo_filename(file); /* may set to default if NULL */ |
| 1611 | if (fname == NULL) |
| 1612 | return FAIL; |
| 1613 | fp = mch_fopen((char *)fname, READBIN); |
| 1614 | |
| 1615 | if (p_verbose > 0) |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 1616 | { |
| 1617 | verbose_enter(); |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1618 | smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"), |
| 1619 | fname, |
| 1620 | want_info ? _(" info") : "", |
| 1621 | want_marks ? _(" marks") : "", |
| 1622 | fp == NULL ? _(" FAILED") : ""); |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 1623 | verbose_leave(); |
| 1624 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1625 | |
| 1626 | vim_free(fname); |
| 1627 | if (fp == NULL) |
| 1628 | return FAIL; |
| 1629 | |
| 1630 | viminfo_errcnt = 0; |
| 1631 | do_viminfo(fp, NULL, want_info, want_marks, forceit); |
| 1632 | |
| 1633 | fclose(fp); |
| 1634 | |
| 1635 | return OK; |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * write_viminfo() -- Write the viminfo file. The old one is read in first so |
| 1640 | * that effectively a merge of current info and old info is done. This allows |
| 1641 | * multiple vims to run simultaneously, without losing any marks etc. If |
| 1642 | * forceit is TRUE, then the old file is not read in, and only internal info is |
| 1643 | * written to the file. -- webb |
| 1644 | */ |
| 1645 | void |
| 1646 | write_viminfo(file, forceit) |
| 1647 | char_u *file; |
| 1648 | int forceit; |
| 1649 | { |
| 1650 | char_u *fname; |
| 1651 | FILE *fp_in = NULL; /* input viminfo file, if any */ |
| 1652 | FILE *fp_out = NULL; /* output viminfo file */ |
| 1653 | char_u *tempname = NULL; /* name of temp viminfo file */ |
| 1654 | struct stat st_new; /* mch_stat() of potential new file */ |
| 1655 | char_u *wp; |
| 1656 | #if defined(UNIX) || defined(VMS) |
| 1657 | mode_t umask_save; |
| 1658 | #endif |
| 1659 | #ifdef UNIX |
| 1660 | int shortname = FALSE; /* use 8.3 file name */ |
| 1661 | struct stat st_old; /* mch_stat() of existing viminfo file */ |
| 1662 | #endif |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1663 | #ifdef WIN3264 |
| 1664 | long perm = -1; |
| 1665 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1666 | |
| 1667 | if (no_viminfo()) |
| 1668 | return; |
| 1669 | |
| 1670 | fname = viminfo_filename(file); /* may set to default if NULL */ |
| 1671 | if (fname == NULL) |
| 1672 | return; |
| 1673 | |
| 1674 | fp_in = mch_fopen((char *)fname, READBIN); |
| 1675 | if (fp_in == NULL) |
| 1676 | { |
| 1677 | /* if it does exist, but we can't read it, don't try writing */ |
| 1678 | if (mch_stat((char *)fname, &st_new) == 0) |
| 1679 | goto end; |
| 1680 | #if defined(UNIX) || defined(VMS) |
| 1681 | /* |
| 1682 | * For Unix we create the .viminfo non-accessible for others, |
| 1683 | * because it may contain text from non-accessible documents. |
| 1684 | */ |
| 1685 | umask_save = umask(077); |
| 1686 | #endif |
| 1687 | fp_out = mch_fopen((char *)fname, WRITEBIN); |
| 1688 | #if defined(UNIX) || defined(VMS) |
| 1689 | (void)umask(umask_save); |
| 1690 | #endif |
| 1691 | } |
| 1692 | else |
| 1693 | { |
| 1694 | /* |
| 1695 | * There is an existing viminfo file. Create a temporary file to |
| 1696 | * write the new viminfo into, in the same directory as the |
| 1697 | * existing viminfo file, which will be renamed later. |
| 1698 | */ |
| 1699 | #ifdef UNIX |
| 1700 | /* |
| 1701 | * For Unix we check the owner of the file. It's not very nice to |
| 1702 | * overwrite a user's viminfo file after a "su root", with a |
| 1703 | * viminfo file that the user can't read. |
| 1704 | */ |
| 1705 | st_old.st_dev = st_old.st_ino = 0; |
| 1706 | st_old.st_mode = 0600; |
Bram Moolenaar | 12625ca | 2005-11-25 19:58:47 +0000 | [diff] [blame] | 1707 | if (mch_stat((char *)fname, &st_old) == 0 && getuid() |
| 1708 | && !(st_old.st_uid == getuid() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1709 | ? (st_old.st_mode & 0200) |
| 1710 | : (st_old.st_gid == getgid() |
| 1711 | ? (st_old.st_mode & 0020) |
| 1712 | : (st_old.st_mode & 0002)))) |
| 1713 | { |
| 1714 | int tt; |
| 1715 | |
| 1716 | /* avoid a wait_return for this message, it's annoying */ |
| 1717 | tt = msg_didany; |
| 1718 | EMSG2(_("E137: Viminfo file is not writable: %s"), fname); |
| 1719 | msg_didany = tt; |
| 1720 | goto end; |
| 1721 | } |
| 1722 | #endif |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1723 | #ifdef WIN3264 |
| 1724 | /* Get the file attributes of the existing viminfo file. */ |
| 1725 | perm = mch_getperm(fname); |
| 1726 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1727 | |
| 1728 | /* |
| 1729 | * Make tempname. |
| 1730 | * May try twice: Once normal and once with shortname set, just in |
| 1731 | * case somebody puts his viminfo file in an 8.3 filesystem. |
| 1732 | */ |
| 1733 | for (;;) |
| 1734 | { |
| 1735 | tempname = buf_modname( |
| 1736 | #ifdef UNIX |
| 1737 | shortname, |
| 1738 | #else |
| 1739 | # ifdef SHORT_FNAME |
| 1740 | TRUE, |
| 1741 | # else |
| 1742 | # ifdef FEAT_GUI_W32 |
| 1743 | gui_is_win32s(), |
| 1744 | # else |
| 1745 | FALSE, |
| 1746 | # endif |
| 1747 | # endif |
| 1748 | #endif |
| 1749 | fname, |
| 1750 | #ifdef VMS |
| 1751 | (char_u *)"-tmp", |
| 1752 | #else |
| 1753 | # ifdef RISCOS |
| 1754 | (char_u *)"/tmp", |
| 1755 | # else |
| 1756 | (char_u *)".tmp", |
| 1757 | # endif |
| 1758 | #endif |
| 1759 | FALSE); |
| 1760 | if (tempname == NULL) /* out of memory */ |
| 1761 | break; |
| 1762 | |
| 1763 | /* |
| 1764 | * Check if tempfile already exists. Never overwrite an |
| 1765 | * existing file! |
| 1766 | */ |
| 1767 | if (mch_stat((char *)tempname, &st_new) == 0) |
| 1768 | { |
| 1769 | #ifdef UNIX |
| 1770 | /* |
| 1771 | * Check if tempfile is same as original file. May happen |
| 1772 | * when modname() gave the same file back. E.g. silly |
| 1773 | * link, or file name-length reached. Try again with |
| 1774 | * shortname set. |
| 1775 | */ |
Bram Moolenaar | 12625ca | 2005-11-25 19:58:47 +0000 | [diff] [blame] | 1776 | if (!shortname && st_new.st_dev == st_old.st_dev |
| 1777 | && st_new.st_ino == st_old.st_ino) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1778 | { |
| 1779 | vim_free(tempname); |
| 1780 | tempname = NULL; |
| 1781 | shortname = TRUE; |
| 1782 | continue; |
| 1783 | } |
| 1784 | #endif |
| 1785 | /* |
| 1786 | * Try another name. Change one character, just before |
| 1787 | * the extension. This should also work for an 8.3 |
| 1788 | * file name, when after adding the extension it still is |
| 1789 | * the same file as the original. |
| 1790 | */ |
| 1791 | wp = tempname + STRLEN(tempname) - 5; |
| 1792 | if (wp < gettail(tempname)) /* empty file name? */ |
| 1793 | wp = gettail(tempname); |
| 1794 | for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0; |
| 1795 | --*wp) |
| 1796 | { |
| 1797 | /* |
| 1798 | * They all exist? Must be something wrong! Don't |
| 1799 | * write the viminfo file then. |
| 1800 | */ |
| 1801 | if (*wp == 'a') |
| 1802 | { |
| 1803 | vim_free(tempname); |
| 1804 | tempname = NULL; |
| 1805 | break; |
| 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | break; |
| 1810 | } |
| 1811 | |
| 1812 | if (tempname != NULL) |
| 1813 | { |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 1814 | #ifdef VMS |
| 1815 | /* fdopen() fails for some reason */ |
Bram Moolenaar | cf03447 | 2006-03-15 23:07:59 +0000 | [diff] [blame] | 1816 | umask_save = umask(077); |
| 1817 | fp_out = mch_fopen((char *)tempname, WRITEBIN); |
| 1818 | (void)umask(umask_save); |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 1819 | #else |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1820 | int fd; |
| 1821 | |
| 1822 | /* 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] | 1823 | * protection: |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 1824 | * Unix: same as original file, but strip s-bit. Reset umask to |
| 1825 | * avoid it getting in the way. |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1826 | * Others: r&w for user only. */ |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 1827 | # ifdef UNIX |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 1828 | umask_save = umask(0); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1829 | fd = mch_open((char *)tempname, |
| 1830 | O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, |
Bram Moolenaar | bba577a | 2005-11-28 23:05:55 +0000 | [diff] [blame] | 1831 | (int)((st_old.st_mode & 0777) | 0600)); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 1832 | (void)umask(umask_save); |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 1833 | # else |
Bram Moolenaar | bba577a | 2005-11-28 23:05:55 +0000 | [diff] [blame] | 1834 | fd = mch_open((char *)tempname, |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 1835 | O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600); |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 1836 | # endif |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1837 | if (fd < 0) |
| 1838 | fp_out = NULL; |
| 1839 | else |
| 1840 | fp_out = fdopen(fd, WRITEBIN); |
Bram Moolenaar | 114216c | 2006-03-14 23:08:30 +0000 | [diff] [blame] | 1841 | #endif /* VMS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1842 | |
| 1843 | /* |
| 1844 | * If we can't create in the same directory, try creating a |
| 1845 | * "normal" temp file. |
| 1846 | */ |
| 1847 | if (fp_out == NULL) |
| 1848 | { |
| 1849 | vim_free(tempname); |
| 1850 | if ((tempname = vim_tempname('o')) != NULL) |
| 1851 | fp_out = mch_fopen((char *)tempname, WRITEBIN); |
| 1852 | } |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1853 | |
| 1854 | #if defined(UNIX) && defined(HAVE_FCHOWN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1855 | /* |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1856 | * Make sure the owner can read/write it. This only works for |
| 1857 | * root. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1858 | */ |
| 1859 | if (fp_out != NULL) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1860 | (void)fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1861 | #endif |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * Check if the new viminfo file can be written to. |
| 1867 | */ |
| 1868 | if (fp_out == NULL) |
| 1869 | { |
| 1870 | EMSG2(_("E138: Can't write viminfo file %s!"), |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 1871 | (fp_in == NULL || tempname == NULL) ? fname : tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1872 | if (fp_in != NULL) |
| 1873 | fclose(fp_in); |
| 1874 | goto end; |
| 1875 | } |
| 1876 | |
| 1877 | if (p_verbose > 0) |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 1878 | { |
| 1879 | verbose_enter(); |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1880 | smsg((char_u *)_("Writing viminfo file \"%s\""), fname); |
Bram Moolenaar | 54ee775 | 2005-05-31 22:22:17 +0000 | [diff] [blame] | 1881 | verbose_leave(); |
| 1882 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1883 | |
| 1884 | viminfo_errcnt = 0; |
| 1885 | do_viminfo(fp_in, fp_out, !forceit, !forceit, FALSE); |
| 1886 | |
| 1887 | fclose(fp_out); /* errors are ignored !? */ |
| 1888 | if (fp_in != NULL) |
| 1889 | { |
| 1890 | fclose(fp_in); |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1891 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1892 | /* |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1893 | * In case of an error keep the original viminfo file. |
| 1894 | * Otherwise rename the newly written file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1895 | */ |
| 1896 | if (viminfo_errcnt || vim_rename(tempname, fname) == -1) |
| 1897 | mch_remove(tempname); |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1898 | |
| 1899 | #ifdef WIN3264 |
| 1900 | /* If the viminfo file was hidden then also hide the new file. */ |
| 1901 | if (perm > 0 && (perm & FILE_ATTRIBUTE_HIDDEN)) |
| 1902 | mch_hide(fname); |
| 1903 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1904 | } |
Bram Moolenaar | bca84a1 | 2005-12-14 22:08:35 +0000 | [diff] [blame] | 1905 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1906 | end: |
| 1907 | vim_free(fname); |
| 1908 | vim_free(tempname); |
| 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * Get the viminfo file name to use. |
| 1913 | * If "file" is given and not empty, use it (has already been expanded by |
| 1914 | * cmdline functions). |
| 1915 | * Otherwise use "-i file_name", value from 'viminfo' or the default, and |
| 1916 | * expand environment variables. |
| 1917 | * Returns an allocated string. NULL when out of memory. |
| 1918 | */ |
| 1919 | static char_u * |
| 1920 | viminfo_filename(file) |
| 1921 | char_u *file; |
| 1922 | { |
| 1923 | if (file == NULL || *file == NUL) |
| 1924 | { |
| 1925 | if (use_viminfo != NULL) |
| 1926 | file = use_viminfo; |
| 1927 | else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL) |
| 1928 | { |
| 1929 | #ifdef VIMINFO_FILE2 |
| 1930 | /* don't use $HOME when not defined (turned into "c:/"!). */ |
| 1931 | # ifdef VMS |
| 1932 | if (mch_getenv((char_u *)"SYS$LOGIN") == NULL) |
| 1933 | # else |
| 1934 | if (mch_getenv((char_u *)"HOME") == NULL) |
| 1935 | # endif |
| 1936 | { |
| 1937 | /* don't use $VIM when not available. */ |
| 1938 | expand_env((char_u *)"$VIM", NameBuff, MAXPATHL); |
| 1939 | if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */ |
| 1940 | file = (char_u *)VIMINFO_FILE2; |
| 1941 | else |
| 1942 | file = (char_u *)VIMINFO_FILE; |
| 1943 | } |
| 1944 | else |
| 1945 | #endif |
| 1946 | file = (char_u *)VIMINFO_FILE; |
| 1947 | } |
| 1948 | expand_env(file, NameBuff, MAXPATHL); |
| 1949 | file = NameBuff; |
| 1950 | } |
| 1951 | return vim_strsave(file); |
| 1952 | } |
| 1953 | |
| 1954 | /* |
| 1955 | * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo(). |
| 1956 | */ |
| 1957 | static void |
| 1958 | do_viminfo(fp_in, fp_out, want_info, want_marks, force_read) |
| 1959 | FILE *fp_in; |
| 1960 | FILE *fp_out; |
| 1961 | int want_info; |
| 1962 | int want_marks; |
| 1963 | int force_read; |
| 1964 | { |
| 1965 | int count = 0; |
| 1966 | int eof = FALSE; |
| 1967 | vir_T vir; |
| 1968 | |
| 1969 | if ((vir.vir_line = alloc(LSIZE)) == NULL) |
| 1970 | return; |
| 1971 | vir.vir_fd = fp_in; |
| 1972 | #ifdef FEAT_MBYTE |
| 1973 | vir.vir_conv.vc_type = CONV_NONE; |
| 1974 | #endif |
| 1975 | |
| 1976 | if (fp_in != NULL) |
| 1977 | { |
| 1978 | if (want_info) |
| 1979 | eof = read_viminfo_up_to_marks(&vir, force_read, fp_out != NULL); |
| 1980 | else |
| 1981 | /* Skip info, find start of marks */ |
| 1982 | while (!(eof = viminfo_readline(&vir)) |
| 1983 | && vir.vir_line[0] != '>') |
| 1984 | ; |
| 1985 | } |
| 1986 | if (fp_out != NULL) |
| 1987 | { |
| 1988 | /* Write the info: */ |
| 1989 | fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"), |
| 1990 | VIM_VERSION_MEDIUM); |
| 1991 | fprintf(fp_out, _("# You may edit it if you're careful!\n\n")); |
| 1992 | #ifdef FEAT_MBYTE |
| 1993 | fprintf(fp_out, _("# Value of 'encoding' when this file was written\n")); |
| 1994 | fprintf(fp_out, "*encoding=%s\n\n", p_enc); |
| 1995 | #endif |
| 1996 | write_viminfo_search_pattern(fp_out); |
| 1997 | write_viminfo_sub_string(fp_out); |
| 1998 | #ifdef FEAT_CMDHIST |
| 1999 | write_viminfo_history(fp_out); |
| 2000 | #endif |
| 2001 | write_viminfo_registers(fp_out); |
| 2002 | #ifdef FEAT_EVAL |
| 2003 | write_viminfo_varlist(fp_out); |
| 2004 | #endif |
| 2005 | write_viminfo_filemarks(fp_out); |
| 2006 | write_viminfo_bufferlist(fp_out); |
| 2007 | count = write_viminfo_marks(fp_out); |
| 2008 | } |
| 2009 | if (fp_in != NULL && want_marks) |
| 2010 | copy_viminfo_marks(&vir, fp_out, count, eof); |
| 2011 | |
| 2012 | vim_free(vir.vir_line); |
| 2013 | #ifdef FEAT_MBYTE |
| 2014 | if (vir.vir_conv.vc_type != CONV_NONE) |
| 2015 | convert_setup(&vir.vir_conv, NULL, NULL); |
| 2016 | #endif |
| 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the |
| 2021 | * first part of the viminfo file which contains everything but the marks that |
| 2022 | * are local to a file. Returns TRUE when end-of-file is reached. -- webb |
| 2023 | */ |
| 2024 | static int |
| 2025 | read_viminfo_up_to_marks(virp, forceit, writing) |
| 2026 | vir_T *virp; |
| 2027 | int forceit; |
| 2028 | int writing; |
| 2029 | { |
| 2030 | int eof; |
| 2031 | buf_T *buf; |
| 2032 | |
| 2033 | #ifdef FEAT_CMDHIST |
| 2034 | prepare_viminfo_history(forceit ? 9999 : 0); |
| 2035 | #endif |
| 2036 | eof = viminfo_readline(virp); |
| 2037 | while (!eof && virp->vir_line[0] != '>') |
| 2038 | { |
| 2039 | switch (virp->vir_line[0]) |
| 2040 | { |
| 2041 | /* Characters reserved for future expansion, ignored now */ |
| 2042 | case '+': /* "+40 /path/dir file", for running vim without args */ |
| 2043 | case '|': /* to be defined */ |
| 2044 | case '^': /* to be defined */ |
| 2045 | case '<': /* long line - ignored */ |
| 2046 | /* A comment or empty line. */ |
| 2047 | case NUL: |
| 2048 | case '\r': |
| 2049 | case '\n': |
| 2050 | case '#': |
| 2051 | eof = viminfo_readline(virp); |
| 2052 | break; |
| 2053 | case '*': /* "*encoding=value" */ |
| 2054 | eof = viminfo_encoding(virp); |
| 2055 | break; |
| 2056 | case '!': /* global variable */ |
| 2057 | #ifdef FEAT_EVAL |
| 2058 | eof = read_viminfo_varlist(virp, writing); |
| 2059 | #else |
| 2060 | eof = viminfo_readline(virp); |
| 2061 | #endif |
| 2062 | break; |
| 2063 | case '%': /* entry for buffer list */ |
| 2064 | eof = read_viminfo_bufferlist(virp, writing); |
| 2065 | break; |
| 2066 | case '"': |
| 2067 | eof = read_viminfo_register(virp, forceit); |
| 2068 | break; |
| 2069 | case '/': /* Search string */ |
| 2070 | case '&': /* Substitute search string */ |
| 2071 | case '~': /* Last search string, followed by '/' or '&' */ |
| 2072 | eof = read_viminfo_search_pattern(virp, forceit); |
| 2073 | break; |
| 2074 | case '$': |
| 2075 | eof = read_viminfo_sub_string(virp, forceit); |
| 2076 | break; |
| 2077 | case ':': |
| 2078 | case '?': |
| 2079 | case '=': |
| 2080 | case '@': |
| 2081 | #ifdef FEAT_CMDHIST |
| 2082 | eof = read_viminfo_history(virp); |
| 2083 | #else |
| 2084 | eof = viminfo_readline(virp); |
| 2085 | #endif |
| 2086 | break; |
| 2087 | case '-': |
| 2088 | case '\'': |
| 2089 | eof = read_viminfo_filemark(virp, forceit); |
| 2090 | break; |
| 2091 | default: |
| 2092 | if (viminfo_error("E575: ", _("Illegal starting char"), |
| 2093 | virp->vir_line)) |
| 2094 | eof = TRUE; |
| 2095 | else |
| 2096 | eof = viminfo_readline(virp); |
| 2097 | break; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | #ifdef FEAT_CMDHIST |
| 2102 | /* Finish reading history items. */ |
| 2103 | finish_viminfo_history(); |
| 2104 | #endif |
| 2105 | |
| 2106 | /* Change file names to buffer numbers for fmarks. */ |
| 2107 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2108 | fmarks_check_names(buf); |
| 2109 | |
| 2110 | return eof; |
| 2111 | } |
| 2112 | |
| 2113 | /* |
| 2114 | * Compare the 'encoding' value in the viminfo file with the current value of |
| 2115 | * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for |
| 2116 | * conversion of text with iconv() in viminfo_readstring(). |
| 2117 | */ |
| 2118 | static int |
| 2119 | viminfo_encoding(virp) |
| 2120 | vir_T *virp; |
| 2121 | { |
| 2122 | #ifdef FEAT_MBYTE |
| 2123 | char_u *p; |
| 2124 | int i; |
| 2125 | |
| 2126 | if (get_viminfo_parameter('c') != 0) |
| 2127 | { |
| 2128 | p = vim_strchr(virp->vir_line, '='); |
| 2129 | if (p != NULL) |
| 2130 | { |
| 2131 | /* remove trailing newline */ |
| 2132 | ++p; |
| 2133 | for (i = 0; vim_isprintc(p[i]); ++i) |
| 2134 | ; |
| 2135 | p[i] = NUL; |
| 2136 | |
| 2137 | convert_setup(&virp->vir_conv, p, p_enc); |
| 2138 | } |
| 2139 | } |
| 2140 | #endif |
| 2141 | return viminfo_readline(virp); |
| 2142 | } |
| 2143 | |
| 2144 | /* |
| 2145 | * Read a line from the viminfo file. |
| 2146 | * Returns TRUE for end-of-file; |
| 2147 | */ |
| 2148 | int |
| 2149 | viminfo_readline(virp) |
| 2150 | vir_T *virp; |
| 2151 | { |
| 2152 | return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd); |
| 2153 | } |
| 2154 | |
| 2155 | /* |
| 2156 | * check string read from viminfo file |
| 2157 | * remove '\n' at the end of the line |
| 2158 | * - replace CTRL-V CTRL-V with CTRL-V |
| 2159 | * - replace CTRL-V 'n' with '\n' |
| 2160 | * |
| 2161 | * Check for a long line as written by viminfo_writestring(). |
| 2162 | * |
| 2163 | * Return the string in allocated memory (NULL when out of memory). |
| 2164 | */ |
| 2165 | /*ARGSUSED*/ |
| 2166 | char_u * |
| 2167 | viminfo_readstring(virp, off, convert) |
| 2168 | vir_T *virp; |
| 2169 | int off; /* offset for virp->vir_line */ |
| 2170 | int convert; /* convert the string */ |
| 2171 | { |
| 2172 | char_u *retval; |
| 2173 | char_u *s, *d; |
| 2174 | long len; |
| 2175 | |
| 2176 | if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1])) |
| 2177 | { |
| 2178 | len = atol((char *)virp->vir_line + off + 1); |
| 2179 | retval = lalloc(len, TRUE); |
| 2180 | if (retval == NULL) |
| 2181 | { |
| 2182 | /* Line too long? File messed up? Skip next line. */ |
| 2183 | (void)vim_fgets(virp->vir_line, 10, virp->vir_fd); |
| 2184 | return NULL; |
| 2185 | } |
| 2186 | (void)vim_fgets(retval, (int)len, virp->vir_fd); |
| 2187 | s = retval + 1; /* Skip the leading '<' */ |
| 2188 | } |
| 2189 | else |
| 2190 | { |
| 2191 | retval = vim_strsave(virp->vir_line + off); |
| 2192 | if (retval == NULL) |
| 2193 | return NULL; |
| 2194 | s = retval; |
| 2195 | } |
| 2196 | |
| 2197 | /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */ |
| 2198 | d = retval; |
| 2199 | while (*s != NUL && *s != '\n') |
| 2200 | { |
| 2201 | if (s[0] == Ctrl_V && s[1] != NUL) |
| 2202 | { |
| 2203 | if (s[1] == 'n') |
| 2204 | *d++ = '\n'; |
| 2205 | else |
| 2206 | *d++ = Ctrl_V; |
| 2207 | s += 2; |
| 2208 | } |
| 2209 | else |
| 2210 | *d++ = *s++; |
| 2211 | } |
| 2212 | *d = NUL; |
| 2213 | |
| 2214 | #ifdef FEAT_MBYTE |
| 2215 | if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL) |
| 2216 | { |
| 2217 | d = string_convert(&virp->vir_conv, retval, NULL); |
| 2218 | if (d != NULL) |
| 2219 | { |
| 2220 | vim_free(retval); |
| 2221 | retval = d; |
| 2222 | } |
| 2223 | } |
| 2224 | #endif |
| 2225 | |
| 2226 | return retval; |
| 2227 | } |
| 2228 | |
| 2229 | /* |
| 2230 | * write string to viminfo file |
| 2231 | * - replace CTRL-V with CTRL-V CTRL-V |
| 2232 | * - replace '\n' with CTRL-V 'n' |
| 2233 | * - add a '\n' at the end |
| 2234 | * |
| 2235 | * For a long line: |
| 2236 | * - write " CTRL-V <length> \n " in first line |
| 2237 | * - write " < <string> \n " in second line |
| 2238 | */ |
| 2239 | void |
| 2240 | viminfo_writestring(fd, p) |
| 2241 | FILE *fd; |
| 2242 | char_u *p; |
| 2243 | { |
| 2244 | int c; |
| 2245 | char_u *s; |
| 2246 | int len = 0; |
| 2247 | |
| 2248 | for (s = p; *s != NUL; ++s) |
| 2249 | { |
| 2250 | if (*s == Ctrl_V || *s == '\n') |
| 2251 | ++len; |
| 2252 | ++len; |
| 2253 | } |
| 2254 | |
| 2255 | /* If the string will be too long, write its length and put it in the next |
| 2256 | * line. Take into account that some room is needed for what comes before |
| 2257 | * the string (e.g., variable name). Add something to the length for the |
| 2258 | * '<', NL and trailing NUL. */ |
| 2259 | if (len > LSIZE / 2) |
| 2260 | fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3); |
| 2261 | |
| 2262 | while ((c = *p++) != NUL) |
| 2263 | { |
| 2264 | if (c == Ctrl_V || c == '\n') |
| 2265 | { |
| 2266 | putc(Ctrl_V, fd); |
| 2267 | if (c == '\n') |
| 2268 | c = 'n'; |
| 2269 | } |
| 2270 | putc(c, fd); |
| 2271 | } |
| 2272 | putc('\n', fd); |
| 2273 | } |
| 2274 | #endif /* FEAT_VIMINFO */ |
| 2275 | |
| 2276 | /* |
| 2277 | * Implementation of ":fixdel", also used by get_stty(). |
| 2278 | * <BS> resulting <Del> |
| 2279 | * ^? ^H |
| 2280 | * not ^? ^? |
| 2281 | */ |
| 2282 | /*ARGSUSED*/ |
| 2283 | void |
| 2284 | do_fixdel(eap) |
| 2285 | exarg_T *eap; |
| 2286 | { |
| 2287 | char_u *p; |
| 2288 | |
| 2289 | p = find_termcode((char_u *)"kb"); |
| 2290 | add_termcode((char_u *)"kD", p != NULL |
| 2291 | && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE); |
| 2292 | } |
| 2293 | |
| 2294 | void |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 2295 | print_line_no_prefix(lnum, use_number, list) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2296 | linenr_T lnum; |
| 2297 | int use_number; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 2298 | int list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2299 | { |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 2300 | char_u numbuf[30]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2301 | |
| 2302 | if (curwin->w_p_nu || use_number) |
| 2303 | { |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 2304 | sprintf((char *)numbuf, "%*ld ", number_width(curwin), (long)lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2305 | msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */ |
| 2306 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 2307 | msg_prt_line(ml_get(lnum), list); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | /* |
| 2311 | * Print a text line. Also in silent mode ("ex -s"). |
| 2312 | */ |
| 2313 | void |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 2314 | print_line(lnum, use_number, list) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2315 | linenr_T lnum; |
| 2316 | int use_number; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 2317 | int list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2318 | { |
| 2319 | int save_silent = silent_mode; |
| 2320 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2321 | msg_start(); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 2322 | silent_mode = FALSE; |
| 2323 | info_message = TRUE; /* use mch_msg(), not mch_errmsg() */ |
| 2324 | print_line_no_prefix(lnum, use_number, list); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2325 | if (save_silent) |
| 2326 | { |
| 2327 | msg_putchar('\n'); |
| 2328 | cursor_on(); /* msg_start() switches it off */ |
| 2329 | out_flush(); |
| 2330 | silent_mode = save_silent; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 2331 | info_message = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * ":file[!] [fname]". |
| 2337 | */ |
| 2338 | void |
| 2339 | ex_file(eap) |
| 2340 | exarg_T *eap; |
| 2341 | { |
| 2342 | char_u *fname, *sfname, *xfname; |
| 2343 | buf_T *buf; |
| 2344 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2345 | /* ":0file" removes the file name. Check for illegal uses ":3file", |
| 2346 | * "0file name", etc. */ |
| 2347 | if (eap->addr_count > 0 |
| 2348 | && (*eap->arg != NUL |
| 2349 | || eap->line2 > 0 |
| 2350 | || eap->addr_count > 1)) |
| 2351 | { |
| 2352 | EMSG(_(e_invarg)); |
| 2353 | return; |
| 2354 | } |
| 2355 | |
| 2356 | if (*eap->arg != NUL || eap->addr_count == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2357 | { |
| 2358 | #ifdef FEAT_AUTOCMD |
| 2359 | buf = curbuf; |
| 2360 | apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); |
| 2361 | /* buffer changed, don't change name now */ |
| 2362 | if (buf != curbuf) |
| 2363 | return; |
| 2364 | # ifdef FEAT_EVAL |
| 2365 | if (aborting()) /* autocmds may abort script processing */ |
| 2366 | return; |
| 2367 | # endif |
| 2368 | #endif |
| 2369 | /* |
| 2370 | * The name of the current buffer will be changed. |
| 2371 | * A new (unlisted) buffer entry needs to be made to hold the old file |
| 2372 | * name, which will become the alternate file name. |
Bram Moolenaar | 7b0294c | 2004-10-11 10:16:09 +0000 | [diff] [blame] | 2373 | * But don't set the alternate file name if the buffer didn't have a |
| 2374 | * name. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2375 | */ |
| 2376 | fname = curbuf->b_ffname; |
| 2377 | sfname = curbuf->b_sfname; |
| 2378 | xfname = curbuf->b_fname; |
| 2379 | curbuf->b_ffname = NULL; |
| 2380 | curbuf->b_sfname = NULL; |
| 2381 | if (setfname(curbuf, eap->arg, NULL, TRUE) == FAIL) |
| 2382 | { |
| 2383 | curbuf->b_ffname = fname; |
| 2384 | curbuf->b_sfname = sfname; |
| 2385 | return; |
| 2386 | } |
| 2387 | curbuf->b_flags |= BF_NOTEDITED; |
Bram Moolenaar | 7b0294c | 2004-10-11 10:16:09 +0000 | [diff] [blame] | 2388 | if (xfname != NULL && *xfname != NUL) |
| 2389 | { |
| 2390 | buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0); |
| 2391 | if (buf != NULL && !cmdmod.keepalt) |
| 2392 | curwin->w_alt_fnum = buf->b_fnum; |
| 2393 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2394 | vim_free(fname); |
| 2395 | vim_free(sfname); |
| 2396 | #ifdef FEAT_AUTOCMD |
| 2397 | apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); |
| 2398 | #endif |
| 2399 | } |
| 2400 | /* print full file name if :cd used */ |
| 2401 | fileinfo(FALSE, FALSE, eap->forceit); |
| 2402 | } |
| 2403 | |
| 2404 | /* |
| 2405 | * ":update". |
| 2406 | */ |
| 2407 | void |
| 2408 | ex_update(eap) |
| 2409 | exarg_T *eap; |
| 2410 | { |
| 2411 | if (curbufIsChanged()) |
| 2412 | (void)do_write(eap); |
| 2413 | } |
| 2414 | |
| 2415 | /* |
| 2416 | * ":write" and ":saveas". |
| 2417 | */ |
| 2418 | void |
| 2419 | ex_write(eap) |
| 2420 | exarg_T *eap; |
| 2421 | { |
| 2422 | if (eap->usefilter) /* input lines to shell command */ |
| 2423 | do_bang(1, eap, FALSE, TRUE, FALSE); |
| 2424 | else |
| 2425 | (void)do_write(eap); |
| 2426 | } |
| 2427 | |
| 2428 | /* |
| 2429 | * write current buffer to file 'eap->arg' |
| 2430 | * if 'eap->append' is TRUE, append to the file |
| 2431 | * |
| 2432 | * if *eap->arg == NUL write to current file |
| 2433 | * |
| 2434 | * return FAIL for failure, OK otherwise |
| 2435 | */ |
| 2436 | int |
| 2437 | do_write(eap) |
| 2438 | exarg_T *eap; |
| 2439 | { |
| 2440 | int other; |
| 2441 | char_u *fname = NULL; /* init to shut up gcc */ |
| 2442 | char_u *ffname; |
| 2443 | int retval = FAIL; |
| 2444 | char_u *free_fname = NULL; |
| 2445 | #ifdef FEAT_BROWSE |
| 2446 | char_u *browse_file = NULL; |
| 2447 | #endif |
| 2448 | buf_T *alt_buf = NULL; |
| 2449 | |
| 2450 | if (not_writing()) /* check 'write' option */ |
| 2451 | return FAIL; |
| 2452 | |
| 2453 | ffname = eap->arg; |
| 2454 | #ifdef FEAT_BROWSE |
| 2455 | if (cmdmod.browse) |
| 2456 | { |
Bram Moolenaar | 7b0294c | 2004-10-11 10:16:09 +0000 | [diff] [blame] | 2457 | browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2458 | NULL, NULL, NULL, curbuf); |
| 2459 | if (browse_file == NULL) |
| 2460 | goto theend; |
| 2461 | ffname = browse_file; |
| 2462 | } |
| 2463 | #endif |
| 2464 | if (*ffname == NUL) |
| 2465 | { |
| 2466 | if (eap->cmdidx == CMD_saveas) |
| 2467 | { |
| 2468 | EMSG(_(e_argreq)); |
| 2469 | goto theend; |
| 2470 | } |
| 2471 | other = FALSE; |
| 2472 | } |
| 2473 | else |
| 2474 | { |
| 2475 | fname = ffname; |
| 2476 | free_fname = fix_fname(ffname); |
| 2477 | /* |
| 2478 | * When out-of-memory, keep unexpanded file name, because we MUST be |
| 2479 | * able to write the file in this situation. |
| 2480 | */ |
| 2481 | if (free_fname != NULL) |
| 2482 | ffname = free_fname; |
| 2483 | other = otherfile(ffname); |
| 2484 | } |
| 2485 | |
| 2486 | /* |
| 2487 | * If we have a new file, put its name in the list of alternate file names. |
| 2488 | */ |
| 2489 | if (other) |
| 2490 | { |
| 2491 | if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL |
| 2492 | || eap->cmdidx == CMD_saveas) |
| 2493 | alt_buf = setaltfname(ffname, fname, (linenr_T)1); |
| 2494 | else |
| 2495 | alt_buf = buflist_findname(ffname); |
| 2496 | if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL) |
| 2497 | { |
| 2498 | /* Overwriting a file that is loaded in another buffer is not a |
| 2499 | * good idea. */ |
Bram Moolenaar | 0e4d877 | 2005-06-07 21:12:49 +0000 | [diff] [blame] | 2500 | EMSG(_(e_bufloaded)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2501 | goto theend; |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | /* |
| 2506 | * Writing to the current file is not allowed in readonly mode |
| 2507 | * and a file name is required. |
| 2508 | * "nofile" and "nowrite" buffers cannot be written implicitly either. |
| 2509 | */ |
| 2510 | if (!other && ( |
| 2511 | #ifdef FEAT_QUICKFIX |
| 2512 | bt_dontwrite_msg(curbuf) || |
| 2513 | #endif |
| 2514 | check_fname() == FAIL || check_readonly(&eap->forceit, curbuf))) |
| 2515 | goto theend; |
| 2516 | |
| 2517 | if (!other) |
| 2518 | { |
| 2519 | ffname = curbuf->b_ffname; |
| 2520 | fname = curbuf->b_fname; |
| 2521 | /* |
| 2522 | * Not writing the whole file is only allowed with '!'. |
| 2523 | */ |
| 2524 | if ( (eap->line1 != 1 |
| 2525 | || eap->line2 != curbuf->b_ml.ml_line_count) |
| 2526 | && !eap->forceit |
| 2527 | && !eap->append |
| 2528 | && !p_wa) |
| 2529 | { |
| 2530 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2531 | if (p_confirm || cmdmod.confirm) |
| 2532 | { |
| 2533 | if (vim_dialog_yesno(VIM_QUESTION, NULL, |
| 2534 | (char_u *)_("Write partial file?"), 2) != VIM_YES) |
| 2535 | goto theend; |
| 2536 | eap->forceit = TRUE; |
| 2537 | } |
| 2538 | else |
| 2539 | #endif |
| 2540 | { |
| 2541 | EMSG(_("E140: Use ! to write partial buffer")); |
| 2542 | goto theend; |
| 2543 | } |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | if (check_overwrite(eap, curbuf, fname, ffname, other) == OK) |
| 2548 | { |
| 2549 | if (eap->cmdidx == CMD_saveas && alt_buf != NULL) |
| 2550 | { |
| 2551 | #ifdef FEAT_AUTOCMD |
| 2552 | buf_T *was_curbuf = curbuf; |
| 2553 | |
| 2554 | apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 2555 | apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2556 | # ifdef FEAT_EVAL |
| 2557 | if (curbuf != was_curbuf || aborting()) |
| 2558 | # else |
| 2559 | if (curbuf != was_curbuf) |
| 2560 | # endif |
| 2561 | { |
| 2562 | /* buffer changed, don't change name now */ |
| 2563 | retval = FAIL; |
| 2564 | goto theend; |
| 2565 | } |
| 2566 | #endif |
| 2567 | /* Exchange the file names for the current and the alternate |
| 2568 | * buffer. This makes it look like we are now editing the buffer |
| 2569 | * under the new name. Must be done before buf_write(), because |
| 2570 | * if there is no file name and 'cpo' contains 'F', it will set |
| 2571 | * the file name. */ |
| 2572 | fname = alt_buf->b_fname; |
| 2573 | alt_buf->b_fname = curbuf->b_fname; |
| 2574 | curbuf->b_fname = fname; |
| 2575 | fname = alt_buf->b_ffname; |
| 2576 | alt_buf->b_ffname = curbuf->b_ffname; |
| 2577 | curbuf->b_ffname = fname; |
| 2578 | fname = alt_buf->b_sfname; |
| 2579 | alt_buf->b_sfname = curbuf->b_sfname; |
| 2580 | curbuf->b_sfname = fname; |
| 2581 | buf_name_changed(curbuf); |
| 2582 | #ifdef FEAT_AUTOCMD |
| 2583 | apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 2584 | apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2585 | if (!alt_buf->b_p_bl) |
| 2586 | { |
| 2587 | alt_buf->b_p_bl = TRUE; |
| 2588 | apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf); |
| 2589 | } |
| 2590 | # ifdef FEAT_EVAL |
| 2591 | if (curbuf != was_curbuf || aborting()) |
| 2592 | # else |
| 2593 | if (curbuf != was_curbuf) |
| 2594 | # endif |
| 2595 | { |
| 2596 | /* buffer changed, don't write the file */ |
| 2597 | retval = FAIL; |
| 2598 | goto theend; |
| 2599 | } |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2600 | |
| 2601 | /* If 'filetype' was empty try detecting it now. */ |
| 2602 | if (*curbuf->b_p_ft == NUL) |
| 2603 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2604 | if (au_has_group((char_u *)"filetypedetect")) |
| 2605 | (void)do_doautocmd((char_u *)"filetypedetect BufRead", |
| 2606 | TRUE); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 2607 | do_modelines(0); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2608 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2609 | #endif |
| 2610 | } |
| 2611 | |
| 2612 | retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2, |
| 2613 | eap, eap->append, eap->forceit, TRUE, FALSE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2614 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2615 | /* After ":saveas fname" reset 'readonly'. */ |
| 2616 | if (eap->cmdidx == CMD_saveas && retval == OK) |
| 2617 | curbuf->b_p_ro = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | theend: |
| 2621 | #ifdef FEAT_BROWSE |
| 2622 | vim_free(browse_file); |
| 2623 | #endif |
| 2624 | vim_free(free_fname); |
| 2625 | return retval; |
| 2626 | } |
| 2627 | |
| 2628 | /* |
| 2629 | * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED, |
| 2630 | * BF_NEW or BF_READERR, check for overwriting current file. |
| 2631 | * May set eap->forceit if a dialog says it's OK to overwrite. |
| 2632 | * Return OK if it's OK, FAIL if it is not. |
| 2633 | */ |
| 2634 | /*ARGSUSED*/ |
| 2635 | static int |
| 2636 | check_overwrite(eap, buf, fname, ffname, other) |
| 2637 | exarg_T *eap; |
| 2638 | buf_T *buf; |
| 2639 | char_u *fname; /* file name to be used (can differ from |
| 2640 | buf->ffname) */ |
| 2641 | char_u *ffname; /* full path version of fname */ |
| 2642 | int other; /* writing under other name */ |
| 2643 | { |
| 2644 | /* |
| 2645 | * write to other file or b_flags set or not writing the whole file: |
| 2646 | * overwriting only allowed with '!' |
| 2647 | */ |
| 2648 | if ( (other |
| 2649 | || (buf->b_flags & BF_NOTEDITED) |
| 2650 | || ((buf->b_flags & BF_NEW) |
| 2651 | && vim_strchr(p_cpo, CPO_OVERNEW) == NULL) |
| 2652 | || (buf->b_flags & BF_READERR)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2653 | && !p_wa |
| 2654 | && vim_fexists(ffname)) |
| 2655 | { |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2656 | if (!eap->forceit && !eap->append) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2657 | { |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2658 | #ifdef UNIX |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 2659 | /* with UNIX it is possible to open a directory */ |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2660 | if (mch_isdir(ffname)) |
| 2661 | { |
| 2662 | EMSG2(_(e_isadir2), ffname); |
| 2663 | return FAIL; |
| 2664 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2665 | #endif |
| 2666 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2667 | if (p_confirm || cmdmod.confirm) |
| 2668 | { |
| 2669 | char_u buff[IOSIZE]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2670 | |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2671 | dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname); |
| 2672 | if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) |
| 2673 | return FAIL; |
| 2674 | eap->forceit = TRUE; |
| 2675 | } |
| 2676 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2677 | #endif |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2678 | { |
| 2679 | EMSG(_(e_exists)); |
| 2680 | return FAIL; |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | /* For ":w! filename" check that no swap file exists for "filename". */ |
| 2685 | if (other && !emsg_silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2686 | { |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2687 | char_u dir[MAXPATHL]; |
| 2688 | char_u *p; |
| 2689 | int r; |
| 2690 | char_u *swapname; |
| 2691 | |
| 2692 | /* We only try the first entry in 'directory', without checking if |
| 2693 | * it's writable. If the "." directory is not writable the write |
| 2694 | * will probably fail anyway. |
| 2695 | * Use 'shortname' of the current buffer, since there is no buffer |
| 2696 | * for the written file. */ |
| 2697 | if (*p_dir == NUL) |
| 2698 | STRCPY(dir, "."); |
| 2699 | else |
| 2700 | { |
| 2701 | p = p_dir; |
| 2702 | copy_option_part(&p, dir, MAXPATHL, ","); |
| 2703 | } |
| 2704 | swapname = makeswapname(fname, ffname, curbuf, dir); |
| 2705 | r = vim_fexists(swapname); |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2706 | if (r) |
| 2707 | { |
| 2708 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2709 | if (p_confirm || cmdmod.confirm) |
| 2710 | { |
| 2711 | char_u buff[IOSIZE]; |
| 2712 | |
| 2713 | dialog_msg(buff, |
| 2714 | _("Swap file \"%s\" exists, overwrite anyway?"), |
| 2715 | swapname); |
| 2716 | if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) |
| 2717 | != VIM_YES) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2718 | { |
| 2719 | vim_free(swapname); |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2720 | return FAIL; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2721 | } |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2722 | eap->forceit = TRUE; |
| 2723 | } |
| 2724 | else |
| 2725 | #endif |
| 2726 | { |
| 2727 | EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"), |
| 2728 | swapname); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2729 | vim_free(swapname); |
Bram Moolenaar | 04a09c1 | 2005-08-01 22:02:32 +0000 | [diff] [blame] | 2730 | return FAIL; |
| 2731 | } |
| 2732 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2733 | vim_free(swapname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2734 | } |
| 2735 | } |
| 2736 | return OK; |
| 2737 | } |
| 2738 | |
| 2739 | /* |
| 2740 | * Handle ":wnext", ":wNext" and ":wprevious" commands. |
| 2741 | */ |
| 2742 | void |
| 2743 | ex_wnext(eap) |
| 2744 | exarg_T *eap; |
| 2745 | { |
| 2746 | int i; |
| 2747 | |
| 2748 | if (eap->cmd[1] == 'n') |
| 2749 | i = curwin->w_arg_idx + (int)eap->line2; |
| 2750 | else |
| 2751 | i = curwin->w_arg_idx - (int)eap->line2; |
| 2752 | eap->line1 = 1; |
| 2753 | eap->line2 = curbuf->b_ml.ml_line_count; |
| 2754 | if (do_write(eap) != FAIL) |
| 2755 | do_argfile(eap, i); |
| 2756 | } |
| 2757 | |
| 2758 | /* |
| 2759 | * ":wall", ":wqall" and ":xall": Write all changed files (and exit). |
| 2760 | */ |
| 2761 | void |
| 2762 | do_wqall(eap) |
| 2763 | exarg_T *eap; |
| 2764 | { |
| 2765 | buf_T *buf; |
| 2766 | int error = 0; |
| 2767 | int save_forceit = eap->forceit; |
| 2768 | |
| 2769 | if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall) |
| 2770 | exiting = TRUE; |
| 2771 | |
| 2772 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2773 | { |
| 2774 | if (bufIsChanged(buf)) |
| 2775 | { |
| 2776 | /* |
| 2777 | * Check if there is a reason the buffer cannot be written: |
| 2778 | * 1. if the 'write' option is set |
| 2779 | * 2. if there is no file name (even after browsing) |
| 2780 | * 3. if the 'readonly' is set (even after a dialog) |
| 2781 | * 4. if overwriting is allowed (even after a dialog) |
| 2782 | */ |
| 2783 | if (not_writing()) |
| 2784 | { |
| 2785 | ++error; |
| 2786 | break; |
| 2787 | } |
| 2788 | #ifdef FEAT_BROWSE |
| 2789 | /* ":browse wall": ask for file name if there isn't one */ |
| 2790 | if (buf->b_ffname == NULL && cmdmod.browse) |
| 2791 | browse_save_fname(buf); |
| 2792 | #endif |
| 2793 | if (buf->b_ffname == NULL) |
| 2794 | { |
| 2795 | EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum); |
| 2796 | ++error; |
| 2797 | } |
| 2798 | else if (check_readonly(&eap->forceit, buf) |
| 2799 | || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname, |
| 2800 | FALSE) == FAIL) |
| 2801 | { |
| 2802 | ++error; |
| 2803 | } |
| 2804 | else |
| 2805 | { |
| 2806 | if (buf_write_all(buf, eap->forceit) == FAIL) |
| 2807 | ++error; |
| 2808 | #ifdef FEAT_AUTOCMD |
| 2809 | /* an autocommand may have deleted the buffer */ |
| 2810 | if (!buf_valid(buf)) |
| 2811 | buf = firstbuf; |
| 2812 | #endif |
| 2813 | } |
| 2814 | eap->forceit = save_forceit; /* check_overwrite() may set it */ |
| 2815 | } |
| 2816 | } |
| 2817 | if (exiting) |
| 2818 | { |
| 2819 | if (!error) |
| 2820 | getout(0); /* exit Vim */ |
| 2821 | not_exiting(); |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | /* |
| 2826 | * Check the 'write' option. |
| 2827 | * Return TRUE and give a message when it's not st. |
| 2828 | */ |
| 2829 | int |
| 2830 | not_writing() |
| 2831 | { |
| 2832 | if (p_write) |
| 2833 | return FALSE; |
| 2834 | EMSG(_("E142: File not written: Writing is disabled by 'write' option")); |
| 2835 | return TRUE; |
| 2836 | } |
| 2837 | |
| 2838 | /* |
| 2839 | * Check if a buffer is read-only. Ask for overruling in a dialog. |
| 2840 | * Return TRUE and give an error message when the buffer is readonly. |
| 2841 | */ |
| 2842 | static int |
| 2843 | check_readonly(forceit, buf) |
| 2844 | int *forceit; |
| 2845 | buf_T *buf; |
| 2846 | { |
| 2847 | if (!*forceit && buf->b_p_ro) |
| 2848 | { |
| 2849 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2850 | if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) |
| 2851 | { |
| 2852 | char_u buff[IOSIZE]; |
| 2853 | |
Bram Moolenaar | 0e4d877 | 2005-06-07 21:12:49 +0000 | [diff] [blame] | 2854 | dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2855 | buf->b_fname); |
| 2856 | |
| 2857 | if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES) |
| 2858 | { |
| 2859 | /* Set forceit, to force the writing of a readonly file */ |
| 2860 | *forceit = TRUE; |
| 2861 | return FALSE; |
| 2862 | } |
| 2863 | else |
| 2864 | return TRUE; |
| 2865 | } |
| 2866 | else |
| 2867 | #endif |
| 2868 | EMSG(_(e_readonly)); |
| 2869 | return TRUE; |
| 2870 | } |
| 2871 | return FALSE; |
| 2872 | } |
| 2873 | |
| 2874 | /* |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2875 | * Try to abandon current file and edit a new or existing file. |
| 2876 | * 'fnum' is the number of the file, if zero use ffname/sfname. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2877 | * |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2878 | * Return 1 for "normal" error, 2 for "not written" error, 0 for success |
| 2879 | * -1 for succesfully opening another file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2880 | * 'lnum' is the line number for the cursor in the new file (if non-zero). |
| 2881 | */ |
| 2882 | int |
| 2883 | getfile(fnum, ffname, sfname, setpm, lnum, forceit) |
| 2884 | int fnum; |
| 2885 | char_u *ffname; |
| 2886 | char_u *sfname; |
| 2887 | int setpm; |
| 2888 | linenr_T lnum; |
| 2889 | int forceit; |
| 2890 | { |
| 2891 | int other; |
| 2892 | int retval; |
| 2893 | char_u *free_me = NULL; |
| 2894 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2895 | if (text_locked()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2896 | return 1; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2897 | #ifdef FEAT_AUTOCMD |
| 2898 | if (curbuf_locked()) |
| 2899 | return 1; |
| 2900 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2901 | |
| 2902 | if (fnum == 0) |
| 2903 | { |
| 2904 | /* make ffname full path, set sfname */ |
| 2905 | fname_expand(curbuf, &ffname, &sfname); |
| 2906 | other = otherfile(ffname); |
| 2907 | free_me = ffname; /* has been allocated, free() later */ |
| 2908 | } |
| 2909 | else |
| 2910 | other = (fnum != curbuf->b_fnum); |
| 2911 | |
| 2912 | if (other) |
| 2913 | ++no_wait_return; /* don't wait for autowrite message */ |
| 2914 | if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf) |
| 2915 | && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL) |
| 2916 | { |
| 2917 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2918 | if (p_confirm && p_write) |
| 2919 | dialog_changed(curbuf, FALSE); |
| 2920 | if (curbufIsChanged()) |
| 2921 | #endif |
| 2922 | { |
| 2923 | if (other) |
| 2924 | --no_wait_return; |
| 2925 | EMSG(_(e_nowrtmsg)); |
| 2926 | retval = 2; /* file has been changed */ |
| 2927 | goto theend; |
| 2928 | } |
| 2929 | } |
| 2930 | if (other) |
| 2931 | --no_wait_return; |
| 2932 | if (setpm) |
| 2933 | setpcmark(); |
| 2934 | if (!other) |
| 2935 | { |
| 2936 | if (lnum != 0) |
| 2937 | curwin->w_cursor.lnum = lnum; |
| 2938 | check_cursor_lnum(); |
| 2939 | beginline(BL_SOL | BL_FIX); |
| 2940 | retval = 0; /* it's in the same file */ |
| 2941 | } |
| 2942 | else if (do_ecmd(fnum, ffname, sfname, NULL, lnum, |
| 2943 | (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0)) == OK) |
| 2944 | retval = -1; /* opened another file */ |
| 2945 | else |
| 2946 | retval = 1; /* error encountered */ |
| 2947 | |
| 2948 | theend: |
| 2949 | vim_free(free_me); |
| 2950 | return retval; |
| 2951 | } |
| 2952 | |
| 2953 | /* |
| 2954 | * start editing a new file |
| 2955 | * |
| 2956 | * fnum: file number; if zero use ffname/sfname |
| 2957 | * ffname: the file name |
| 2958 | * - full path if sfname used, |
| 2959 | * - any file name if sfname is NULL |
| 2960 | * - empty string to re-edit with the same file name (but may be |
| 2961 | * in a different directory) |
| 2962 | * - NULL to start an empty buffer |
| 2963 | * sfname: the short file name (or NULL) |
| 2964 | * eap: contains the command to be executed after loading the file and |
| 2965 | * forced 'ff' and 'fenc' |
| 2966 | * newlnum: if > 0: put cursor on this line number (if possible) |
| 2967 | * if ECMD_LASTL: use last position in loaded file |
| 2968 | * if ECMD_LAST: use last position in all files |
| 2969 | * if ECMD_ONE: use first line |
| 2970 | * flags: |
| 2971 | * ECMD_HIDE: if TRUE don't free the current buffer |
| 2972 | * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file |
| 2973 | * ECMD_OLDBUF: use existing buffer if it exists |
| 2974 | * ECMD_FORCEIT: ! used for Ex command |
| 2975 | * ECMD_ADDBUF: don't edit, just add to buffer list |
| 2976 | * |
| 2977 | * return FAIL for failure, OK otherwise |
| 2978 | */ |
| 2979 | int |
| 2980 | do_ecmd(fnum, ffname, sfname, eap, newlnum, flags) |
| 2981 | int fnum; |
| 2982 | char_u *ffname; |
| 2983 | char_u *sfname; |
| 2984 | exarg_T *eap; /* can be NULL! */ |
| 2985 | linenr_T newlnum; |
| 2986 | int flags; |
| 2987 | { |
| 2988 | int other_file; /* TRUE if editing another file */ |
| 2989 | int oldbuf; /* TRUE if using existing buffer */ |
| 2990 | #ifdef FEAT_AUTOCMD |
| 2991 | int auto_buf = FALSE; /* TRUE if autocommands brought us |
| 2992 | into the buffer unexpectedly */ |
| 2993 | char_u *new_name = NULL; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 2994 | int did_set_swapcommand = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2995 | #endif |
| 2996 | buf_T *buf; |
| 2997 | #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2998 | buf_T *old_curbuf = curbuf; |
| 2999 | #endif |
| 3000 | char_u *free_fname = NULL; |
| 3001 | #ifdef FEAT_BROWSE |
| 3002 | char_u *browse_file = NULL; |
| 3003 | #endif |
| 3004 | int retval = FAIL; |
| 3005 | long n; |
| 3006 | linenr_T lnum; |
| 3007 | linenr_T topline = 0; |
| 3008 | int newcol = -1; |
| 3009 | int solcol = -1; |
| 3010 | pos_T *pos; |
| 3011 | #ifdef FEAT_SUN_WORKSHOP |
| 3012 | char_u *cp; |
| 3013 | #endif |
| 3014 | char_u *command = NULL; |
| 3015 | |
| 3016 | if (eap != NULL) |
| 3017 | command = eap->do_ecmd_cmd; |
| 3018 | |
| 3019 | if (fnum != 0) |
| 3020 | { |
| 3021 | if (fnum == curbuf->b_fnum) /* file is already being edited */ |
| 3022 | return OK; /* nothing to do */ |
| 3023 | other_file = TRUE; |
| 3024 | } |
| 3025 | else |
| 3026 | { |
| 3027 | #ifdef FEAT_BROWSE |
| 3028 | if (cmdmod.browse) |
| 3029 | { |
Bram Moolenaar | 0be6e64 | 2005-08-04 21:32:22 +0000 | [diff] [blame] | 3030 | if ( |
| 3031 | # ifdef FEAT_GUI |
| 3032 | !gui.in_use && |
| 3033 | # endif |
| 3034 | au_has_group((char_u *)"FileExplorer")) |
| 3035 | { |
| 3036 | /* No browsing supported but we do have the file explorer: |
| 3037 | * Edit the directory. */ |
| 3038 | if (ffname == NULL || !mch_isdir(ffname)) |
| 3039 | ffname = (char_u *)"."; |
| 3040 | } |
| 3041 | else |
| 3042 | { |
| 3043 | browse_file = do_browse(0, (char_u *)_("Edit File"), ffname, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3044 | NULL, NULL, NULL, curbuf); |
Bram Moolenaar | 0be6e64 | 2005-08-04 21:32:22 +0000 | [diff] [blame] | 3045 | if (browse_file == NULL) |
| 3046 | goto theend; |
| 3047 | ffname = browse_file; |
| 3048 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3049 | } |
| 3050 | #endif |
| 3051 | /* if no short name given, use ffname for short name */ |
| 3052 | if (sfname == NULL) |
| 3053 | sfname = ffname; |
| 3054 | #ifdef USE_FNAME_CASE |
| 3055 | # ifdef USE_LONG_FNAME |
| 3056 | if (USE_LONG_FNAME) |
| 3057 | # endif |
Bram Moolenaar | 342337a | 2005-07-21 21:11:17 +0000 | [diff] [blame] | 3058 | if (sfname != NULL) |
| 3059 | fname_case(sfname, 0); /* set correct case for sfname */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3060 | #endif |
| 3061 | |
| 3062 | #ifdef FEAT_LISTCMDS |
| 3063 | if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL)) |
| 3064 | goto theend; |
| 3065 | #endif |
| 3066 | |
| 3067 | if (ffname == NULL) |
| 3068 | other_file = TRUE; |
| 3069 | /* there is no file name */ |
| 3070 | else if (*ffname == NUL && curbuf->b_ffname == NULL) |
| 3071 | other_file = FALSE; |
| 3072 | else |
| 3073 | { |
| 3074 | if (*ffname == NUL) /* re-edit with same file name */ |
| 3075 | { |
| 3076 | ffname = curbuf->b_ffname; |
| 3077 | sfname = curbuf->b_fname; |
| 3078 | } |
| 3079 | free_fname = fix_fname(ffname); /* may expand to full path name */ |
| 3080 | if (free_fname != NULL) |
| 3081 | ffname = free_fname; |
| 3082 | other_file = otherfile(ffname); |
| 3083 | #ifdef FEAT_SUN_WORKSHOP |
| 3084 | if (usingSunWorkShop && p_acd |
| 3085 | && (cp = vim_strrchr(sfname, '/')) != NULL) |
| 3086 | sfname = ++cp; |
| 3087 | #endif |
| 3088 | } |
| 3089 | } |
| 3090 | |
| 3091 | /* |
| 3092 | * if the file was changed we may not be allowed to abandon it |
| 3093 | * - if we are going to re-edit the same file |
| 3094 | * - or if we are the only window on this file and if ECMD_HIDE is FALSE |
| 3095 | */ |
| 3096 | if ( ((!other_file && !(flags & ECMD_OLDBUF)) |
| 3097 | || (curbuf->b_nwindows == 1 |
| 3098 | && !(flags & (ECMD_HIDE | ECMD_ADDBUF)))) |
| 3099 | && check_changed(curbuf, p_awa, !other_file, |
| 3100 | (flags & ECMD_FORCEIT), FALSE)) |
| 3101 | { |
| 3102 | if (fnum == 0 && other_file && ffname != NULL) |
| 3103 | (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum); |
| 3104 | goto theend; |
| 3105 | } |
| 3106 | |
| 3107 | #ifdef FEAT_VISUAL |
| 3108 | /* |
| 3109 | * End Visual mode before switching to another buffer, so the text can be |
| 3110 | * copied into the GUI selection buffer. |
| 3111 | */ |
| 3112 | reset_VIsual(); |
| 3113 | #endif |
| 3114 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 3115 | #ifdef FEAT_AUTOCMD |
| 3116 | if ((command != NULL || newlnum > (linenr_T)0) |
| 3117 | && *get_vim_var_str(VV_SWAPCOMMAND) == NUL) |
| 3118 | { |
| 3119 | int len; |
| 3120 | char_u *p; |
| 3121 | |
| 3122 | /* Set v:swapcommand for the SwapExists autocommands. */ |
| 3123 | if (command != NULL) |
| 3124 | len = STRLEN(command) + 3; |
| 3125 | else |
| 3126 | len = 30; |
| 3127 | p = alloc((unsigned)len); |
| 3128 | if (p != NULL) |
| 3129 | { |
| 3130 | if (command != NULL) |
| 3131 | vim_snprintf((char *)p, len, ":%s\r", command); |
| 3132 | else |
| 3133 | vim_snprintf((char *)p, len, "%ldG", (long)newlnum); |
| 3134 | set_vim_var_string(VV_SWAPCOMMAND, p, -1); |
| 3135 | did_set_swapcommand = TRUE; |
| 3136 | vim_free(p); |
| 3137 | } |
| 3138 | } |
| 3139 | #endif |
| 3140 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3141 | /* |
| 3142 | * If we are starting to edit another file, open a (new) buffer. |
| 3143 | * Otherwise we re-use the current buffer. |
| 3144 | */ |
| 3145 | if (other_file) |
| 3146 | { |
| 3147 | #ifdef FEAT_LISTCMDS |
| 3148 | if (!(flags & ECMD_ADDBUF)) |
| 3149 | #endif |
| 3150 | { |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3151 | if (!cmdmod.keepalt) |
| 3152 | curwin->w_alt_fnum = curbuf->b_fnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3153 | buflist_altfpos(); |
| 3154 | } |
| 3155 | |
| 3156 | if (fnum) |
| 3157 | buf = buflist_findnr(fnum); |
| 3158 | else |
| 3159 | { |
| 3160 | #ifdef FEAT_LISTCMDS |
| 3161 | if (flags & ECMD_ADDBUF) |
| 3162 | { |
| 3163 | linenr_T tlnum = 1L; |
| 3164 | |
| 3165 | if (command != NULL) |
| 3166 | { |
| 3167 | tlnum = atol((char *)command); |
| 3168 | if (tlnum <= 0) |
| 3169 | tlnum = 1L; |
| 3170 | } |
| 3171 | (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED); |
| 3172 | goto theend; |
| 3173 | } |
| 3174 | #endif |
| 3175 | buf = buflist_new(ffname, sfname, 0L, |
| 3176 | BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED)); |
| 3177 | } |
| 3178 | if (buf == NULL) |
| 3179 | goto theend; |
| 3180 | if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */ |
| 3181 | { |
| 3182 | oldbuf = FALSE; |
| 3183 | buf->b_nwindows = 0; |
| 3184 | } |
| 3185 | else /* existing memfile */ |
| 3186 | { |
| 3187 | oldbuf = TRUE; |
| 3188 | (void)buf_check_timestamp(buf, FALSE); |
| 3189 | /* Check if autocommands made buffer invalid or changed the current |
| 3190 | * buffer. */ |
| 3191 | if (!buf_valid(buf) |
| 3192 | #ifdef FEAT_AUTOCMD |
| 3193 | || curbuf != old_curbuf |
| 3194 | #endif |
| 3195 | ) |
| 3196 | goto theend; |
| 3197 | #ifdef FEAT_EVAL |
| 3198 | if (aborting()) /* autocmds may abort script processing */ |
| 3199 | goto theend; |
| 3200 | #endif |
| 3201 | } |
| 3202 | |
| 3203 | /* May jump to last used line number for a loaded buffer or when asked |
| 3204 | * for explicitly */ |
| 3205 | if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST) |
| 3206 | { |
| 3207 | pos = buflist_findfpos(buf); |
| 3208 | newlnum = pos->lnum; |
| 3209 | solcol = pos->col; |
| 3210 | } |
| 3211 | |
| 3212 | /* |
| 3213 | * Make the (new) buffer the one used by the current window. |
| 3214 | * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE. |
| 3215 | * If the current buffer was empty and has no file name, curbuf |
| 3216 | * is returned by buflist_new(). |
| 3217 | */ |
| 3218 | if (buf != curbuf) |
| 3219 | { |
| 3220 | #ifdef FEAT_AUTOCMD |
| 3221 | /* |
| 3222 | * Be careful: The autocommands may delete any buffer and change |
| 3223 | * the current buffer. |
| 3224 | * - If the buffer we are going to edit is deleted, give up. |
| 3225 | * - If the current buffer is deleted, prefer to load the new |
| 3226 | * buffer when loading a buffer is required. This avoids |
| 3227 | * loading another buffer which then must be closed again. |
| 3228 | * - If we ended up in the new buffer already, need to skip a few |
| 3229 | * things, set auto_buf. |
| 3230 | */ |
| 3231 | if (buf->b_fname != NULL) |
| 3232 | new_name = vim_strsave(buf->b_fname); |
| 3233 | au_new_curbuf = buf; |
| 3234 | apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); |
| 3235 | if (!buf_valid(buf)) /* new buffer has been deleted */ |
| 3236 | { |
| 3237 | delbuf_msg(new_name); /* frees new_name */ |
| 3238 | goto theend; |
| 3239 | } |
| 3240 | # ifdef FEAT_EVAL |
| 3241 | if (aborting()) /* autocmds may abort script processing */ |
| 3242 | { |
| 3243 | vim_free(new_name); |
| 3244 | goto theend; |
| 3245 | } |
| 3246 | # endif |
| 3247 | if (buf == curbuf) /* already in new buffer */ |
| 3248 | auto_buf = TRUE; |
| 3249 | else |
| 3250 | { |
| 3251 | if (curbuf == old_curbuf) |
| 3252 | #endif |
| 3253 | buf_copy_options(buf, BCO_ENTER); |
| 3254 | |
| 3255 | /* close the link to the current buffer */ |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3256 | u_sync(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3257 | close_buffer(curwin, curbuf, |
| 3258 | (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD); |
| 3259 | |
| 3260 | #ifdef FEAT_AUTOCMD |
| 3261 | # ifdef FEAT_EVAL |
| 3262 | if (aborting()) /* autocmds may abort script processing */ |
| 3263 | { |
| 3264 | vim_free(new_name); |
| 3265 | goto theend; |
| 3266 | } |
| 3267 | # endif |
| 3268 | /* Be careful again, like above. */ |
| 3269 | if (!buf_valid(buf)) /* new buffer has been deleted */ |
| 3270 | { |
| 3271 | delbuf_msg(new_name); /* frees new_name */ |
| 3272 | goto theend; |
| 3273 | } |
| 3274 | if (buf == curbuf) /* already in new buffer */ |
| 3275 | auto_buf = TRUE; |
| 3276 | else |
| 3277 | #endif |
| 3278 | { |
| 3279 | curwin->w_buffer = buf; |
| 3280 | curbuf = buf; |
| 3281 | ++curbuf->b_nwindows; |
| 3282 | /* set 'fileformat' */ |
| 3283 | if (*p_ffs && !oldbuf) |
| 3284 | set_fileformat(default_fileformat(), OPT_LOCAL); |
| 3285 | } |
| 3286 | |
| 3287 | /* May get the window options from the last time this buffer |
| 3288 | * was in this window (or another window). If not used |
| 3289 | * before, reset the local window options to the global |
| 3290 | * values. Also restores old folding stuff. */ |
| 3291 | get_winopts(buf); |
| 3292 | |
| 3293 | #ifdef FEAT_AUTOCMD |
| 3294 | } |
| 3295 | vim_free(new_name); |
| 3296 | au_new_curbuf = NULL; |
| 3297 | #endif |
| 3298 | } |
| 3299 | else |
| 3300 | ++curbuf->b_nwindows; |
| 3301 | |
| 3302 | curwin->w_pcmark.lnum = 1; |
| 3303 | curwin->w_pcmark.col = 0; |
| 3304 | } |
| 3305 | else /* !other_file */ |
| 3306 | { |
| 3307 | if ( |
| 3308 | #ifdef FEAT_LISTCMDS |
| 3309 | (flags & ECMD_ADDBUF) || |
| 3310 | #endif |
| 3311 | check_fname() == FAIL) |
| 3312 | goto theend; |
| 3313 | oldbuf = (flags & ECMD_OLDBUF); |
| 3314 | } |
| 3315 | |
| 3316 | if ((flags & ECMD_SET_HELP) || keep_help_flag) |
| 3317 | { |
| 3318 | char_u *p; |
| 3319 | |
| 3320 | curbuf->b_help = TRUE; |
| 3321 | #ifdef FEAT_QUICKFIX |
| 3322 | set_string_option_direct((char_u *)"buftype", -1, |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3323 | (char_u *)"help", OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3324 | #endif |
| 3325 | |
| 3326 | /* |
| 3327 | * Always set these options after jumping to a help tag, because the |
| 3328 | * user may have an autocommand that gets in the way. |
| 3329 | * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and |
| 3330 | * latin1 word characters (for translated help files). |
| 3331 | * Only set it when needed, buf_init_chartab() is some work. |
| 3332 | */ |
| 3333 | p = |
| 3334 | #ifdef EBCDIC |
| 3335 | (char_u *)"65-255,^*,^|,^\""; |
| 3336 | #else |
| 3337 | (char_u *)"!-~,^*,^|,^\",192-255"; |
| 3338 | #endif |
| 3339 | if (STRCMP(curbuf->b_p_isk, p) != 0) |
| 3340 | { |
| 3341 | set_string_option_direct((char_u *)"isk", -1, p, |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3342 | OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3343 | check_buf_options(curbuf); |
| 3344 | (void)buf_init_chartab(curbuf, FALSE); |
| 3345 | } |
| 3346 | |
| 3347 | curbuf->b_p_ts = 8; /* 'tabstop' is 8 */ |
| 3348 | curwin->w_p_list = FALSE; /* no list mode */ |
| 3349 | |
| 3350 | curbuf->b_p_ma = FALSE; /* not modifiable */ |
| 3351 | curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */ |
| 3352 | curwin->w_p_nu = 0; /* no line numbers */ |
| 3353 | #ifdef FEAT_SCROLLBIND |
| 3354 | curwin->w_p_scb = FALSE; /* no scroll binding */ |
| 3355 | #endif |
| 3356 | #ifdef FEAT_ARABIC |
| 3357 | curwin->w_p_arab = FALSE; /* no arabic mode */ |
| 3358 | #endif |
| 3359 | #ifdef FEAT_RIGHTLEFT |
| 3360 | curwin->w_p_rl = FALSE; /* help window is left-to-right */ |
| 3361 | #endif |
| 3362 | #ifdef FEAT_FOLDING |
| 3363 | curwin->w_p_fen = FALSE; /* No folding in the help window */ |
| 3364 | #endif |
| 3365 | #ifdef FEAT_DIFF |
| 3366 | curwin->w_p_diff = FALSE; /* No 'diff' */ |
| 3367 | #endif |
Bram Moolenaar | a1956f6 | 2006-03-12 22:18:00 +0000 | [diff] [blame] | 3368 | #ifdef FEAT_SPELL |
Bram Moolenaar | 9a50b1b | 2005-06-27 22:48:21 +0000 | [diff] [blame] | 3369 | curwin->w_p_spell = FALSE; /* No spell checking */ |
| 3370 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3371 | |
| 3372 | #ifdef FEAT_AUTOCMD |
| 3373 | buf = curbuf; |
| 3374 | #endif |
| 3375 | set_buflisted(FALSE); |
| 3376 | } |
| 3377 | else |
| 3378 | { |
| 3379 | #ifdef FEAT_AUTOCMD |
| 3380 | buf = curbuf; |
| 3381 | #endif |
| 3382 | /* Don't make a buffer listed if it's a help buffer. Useful when |
| 3383 | * using CTRL-O to go back to a help file. */ |
| 3384 | if (!curbuf->b_help) |
| 3385 | set_buflisted(TRUE); |
| 3386 | } |
| 3387 | |
| 3388 | #ifdef FEAT_AUTOCMD |
| 3389 | /* If autocommands change buffers under our fingers, forget about |
| 3390 | * editing the file. */ |
| 3391 | if (buf != curbuf) |
| 3392 | goto theend; |
| 3393 | # ifdef FEAT_EVAL |
| 3394 | if (aborting()) /* autocmds may abort script processing */ |
| 3395 | goto theend; |
| 3396 | # endif |
| 3397 | |
| 3398 | /* Since we are starting to edit a file, consider the filetype to be |
| 3399 | * unset. Helps for when an autocommand changes files and expects syntax |
| 3400 | * highlighting to work in the other file. */ |
| 3401 | did_filetype = FALSE; |
| 3402 | #endif |
| 3403 | |
| 3404 | /* |
| 3405 | * other_file oldbuf |
| 3406 | * FALSE FALSE re-edit same file, buffer is re-used |
| 3407 | * FALSE TRUE re-edit same file, nothing changes |
| 3408 | * TRUE FALSE start editing new file, new buffer |
| 3409 | * TRUE TRUE start editing in existing buffer (nothing to do) |
| 3410 | */ |
| 3411 | if (!other_file && !oldbuf) /* re-use the buffer */ |
| 3412 | { |
| 3413 | set_last_cursor(curwin); /* may set b_last_cursor */ |
| 3414 | if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL) |
| 3415 | { |
| 3416 | newlnum = curwin->w_cursor.lnum; |
| 3417 | solcol = curwin->w_cursor.col; |
| 3418 | } |
| 3419 | #ifdef FEAT_AUTOCMD |
| 3420 | buf = curbuf; |
| 3421 | if (buf->b_fname != NULL) |
| 3422 | new_name = vim_strsave(buf->b_fname); |
| 3423 | else |
| 3424 | new_name = NULL; |
| 3425 | #endif |
| 3426 | buf_freeall(curbuf, FALSE, FALSE); /* free all things for buffer */ |
| 3427 | #ifdef FEAT_AUTOCMD |
| 3428 | /* If autocommands deleted the buffer we were going to re-edit, give |
| 3429 | * up and jump to the end. */ |
| 3430 | if (!buf_valid(buf)) |
| 3431 | { |
| 3432 | delbuf_msg(new_name); /* frees new_name */ |
| 3433 | goto theend; |
| 3434 | } |
| 3435 | vim_free(new_name); |
| 3436 | |
| 3437 | /* If autocommands change buffers under our fingers, forget about |
| 3438 | * re-editing the file. Should do the buf_clear_file(), but perhaps |
| 3439 | * the autocommands changed the buffer... */ |
| 3440 | if (buf != curbuf) |
| 3441 | goto theend; |
| 3442 | # ifdef FEAT_EVAL |
| 3443 | if (aborting()) /* autocmds may abort script processing */ |
| 3444 | goto theend; |
| 3445 | # endif |
| 3446 | #endif |
| 3447 | buf_clear_file(curbuf); |
| 3448 | curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */ |
| 3449 | curbuf->b_op_end.lnum = 0; |
| 3450 | } |
| 3451 | |
| 3452 | /* |
| 3453 | * If we get here we are sure to start editing |
| 3454 | */ |
| 3455 | /* don't redraw until the cursor is in the right line */ |
| 3456 | ++RedrawingDisabled; |
| 3457 | |
| 3458 | /* Assume success now */ |
| 3459 | retval = OK; |
| 3460 | |
| 3461 | /* |
| 3462 | * Reset cursor position, could be used by autocommands. |
| 3463 | */ |
| 3464 | check_cursor(); |
| 3465 | |
| 3466 | /* |
| 3467 | * Check if we are editing the w_arg_idx file in the argument list. |
| 3468 | */ |
| 3469 | check_arg_idx(curwin); |
| 3470 | |
| 3471 | #ifdef FEAT_AUTOCMD |
| 3472 | if (!auto_buf) |
| 3473 | #endif |
| 3474 | { |
| 3475 | /* |
| 3476 | * Set cursor and init window before reading the file and executing |
| 3477 | * autocommands. This allows for the autocommands to position the |
| 3478 | * cursor. |
| 3479 | */ |
Bram Moolenaar | faa959a | 2006-02-20 21:37:40 +0000 | [diff] [blame] | 3480 | curwin_init(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3481 | |
| 3482 | #ifdef FEAT_FOLDING |
| 3483 | /* It's like all lines in the buffer changed. Need to update |
| 3484 | * automatic folding. */ |
| 3485 | foldUpdateAll(curwin); |
| 3486 | #endif |
| 3487 | |
Bram Moolenaar | 7b89edc | 2006-04-06 20:21:51 +0000 | [diff] [blame] | 3488 | #ifdef FEAT_AUTOCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3489 | if (p_acd && curbuf->b_ffname != NULL |
| 3490 | && vim_chdirfile(curbuf->b_ffname) == OK) |
| 3491 | shorten_fnames(TRUE); |
| 3492 | #endif |
| 3493 | /* |
| 3494 | * Careful: open_buffer() and apply_autocmds() may change the current |
| 3495 | * buffer and window. |
| 3496 | */ |
| 3497 | lnum = curwin->w_cursor.lnum; |
| 3498 | topline = curwin->w_topline; |
| 3499 | if (!oldbuf) /* need to read the file */ |
| 3500 | { |
Bram Moolenaar | d5bc83f | 2005-12-07 21:07:59 +0000 | [diff] [blame] | 3501 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3502 | swap_exists_action = SEA_DIALOG; |
| 3503 | #endif |
| 3504 | curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */ |
| 3505 | |
| 3506 | /* |
| 3507 | * Open the buffer and read the file. |
| 3508 | */ |
| 3509 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 3510 | if (should_abort(open_buffer(FALSE, eap))) |
| 3511 | retval = FAIL; |
| 3512 | #else |
| 3513 | (void)open_buffer(FALSE, eap); |
| 3514 | #endif |
| 3515 | |
Bram Moolenaar | d5bc83f | 2005-12-07 21:07:59 +0000 | [diff] [blame] | 3516 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3517 | if (swap_exists_action == SEA_QUIT) |
| 3518 | retval = FAIL; |
| 3519 | handle_swap_exists(old_curbuf); |
| 3520 | #endif |
| 3521 | } |
| 3522 | #ifdef FEAT_AUTOCMD |
| 3523 | else |
| 3524 | { |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 3525 | /* Read the modelines, but only to set window-local options. Any |
| 3526 | * buffer-local options have already been set and may have been |
| 3527 | * changed by the user. */ |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 3528 | do_modelines(OPT_WINONLY); |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 3529 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3530 | apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, |
| 3531 | &retval); |
| 3532 | apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, |
| 3533 | &retval); |
| 3534 | } |
| 3535 | check_arg_idx(curwin); |
| 3536 | #endif |
| 3537 | |
| 3538 | /* |
| 3539 | * If autocommands change the cursor position or topline, we should |
| 3540 | * keep it. |
| 3541 | */ |
| 3542 | if (curwin->w_cursor.lnum != lnum) |
| 3543 | { |
| 3544 | newlnum = curwin->w_cursor.lnum; |
| 3545 | newcol = curwin->w_cursor.col; |
| 3546 | } |
| 3547 | if (curwin->w_topline == topline) |
| 3548 | topline = 0; |
| 3549 | |
| 3550 | /* Even when cursor didn't move we need to recompute topline. */ |
| 3551 | changed_line_abv_curs(); |
| 3552 | |
| 3553 | #ifdef FEAT_TITLE |
| 3554 | maketitle(); |
| 3555 | #endif |
| 3556 | } |
| 3557 | |
| 3558 | #ifdef FEAT_DIFF |
| 3559 | /* Tell the diff stuff that this buffer is new and/or needs updating. |
| 3560 | * Also needed when re-editing the same buffer, because unloading will |
| 3561 | * have removed it as a diff buffer. */ |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3562 | if (curwin->w_p_diff) |
| 3563 | { |
| 3564 | diff_buf_add(curbuf); |
| 3565 | diff_invalidate(curbuf); |
| 3566 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3567 | #endif |
| 3568 | |
| 3569 | if (command == NULL) |
| 3570 | { |
| 3571 | if (newcol >= 0) /* position set by autocommands */ |
| 3572 | { |
| 3573 | curwin->w_cursor.lnum = newlnum; |
| 3574 | curwin->w_cursor.col = newcol; |
| 3575 | check_cursor(); |
| 3576 | } |
| 3577 | else if (newlnum > 0) /* line number from caller or old position */ |
| 3578 | { |
| 3579 | curwin->w_cursor.lnum = newlnum; |
| 3580 | check_cursor_lnum(); |
| 3581 | if (solcol >= 0 && !p_sol) |
| 3582 | { |
| 3583 | /* 'sol' is off: Use last known column. */ |
| 3584 | curwin->w_cursor.col = solcol; |
| 3585 | check_cursor_col(); |
| 3586 | #ifdef FEAT_VIRTUALEDIT |
| 3587 | curwin->w_cursor.coladd = 0; |
| 3588 | #endif |
| 3589 | curwin->w_set_curswant = TRUE; |
| 3590 | } |
| 3591 | else |
| 3592 | beginline(BL_SOL | BL_FIX); |
| 3593 | } |
| 3594 | else /* no line number, go to last line in Ex mode */ |
| 3595 | { |
| 3596 | if (exmode_active) |
| 3597 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 3598 | beginline(BL_WHITE | BL_FIX); |
| 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | #ifdef FEAT_WINDOWS |
| 3603 | /* Check if cursors in other windows on the same buffer are still valid */ |
| 3604 | check_lnums(FALSE); |
| 3605 | #endif |
| 3606 | |
| 3607 | /* |
| 3608 | * Did not read the file, need to show some info about the file. |
| 3609 | * Do this after setting the cursor. |
| 3610 | */ |
| 3611 | if (oldbuf |
| 3612 | #ifdef FEAT_AUTOCMD |
| 3613 | && !auto_buf |
| 3614 | #endif |
| 3615 | ) |
| 3616 | { |
| 3617 | int msg_scroll_save = msg_scroll; |
| 3618 | |
| 3619 | /* Obey the 'O' flag in 'cpoptions': overwrite any previous file |
| 3620 | * message. */ |
| 3621 | if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) |
| 3622 | msg_scroll = FALSE; |
| 3623 | if (!msg_scroll) /* wait a bit when overwriting an error msg */ |
| 3624 | check_for_delay(FALSE); |
| 3625 | msg_start(); |
| 3626 | msg_scroll = msg_scroll_save; |
| 3627 | msg_scrolled_ign = TRUE; |
| 3628 | |
| 3629 | fileinfo(FALSE, TRUE, FALSE); |
| 3630 | |
| 3631 | msg_scrolled_ign = FALSE; |
| 3632 | } |
| 3633 | |
| 3634 | if (command != NULL) |
| 3635 | do_cmdline(command, NULL, NULL, DOCMD_VERBOSE); |
| 3636 | |
| 3637 | #ifdef FEAT_KEYMAP |
| 3638 | if (curbuf->b_kmap_state & KEYMAP_INIT) |
| 3639 | keymap_init(); |
| 3640 | #endif |
| 3641 | |
| 3642 | --RedrawingDisabled; |
| 3643 | if (!skip_redraw) |
| 3644 | { |
| 3645 | n = p_so; |
| 3646 | if (topline == 0 && command == NULL) |
| 3647 | p_so = 999; /* force cursor halfway the window */ |
| 3648 | update_topline(); |
| 3649 | #ifdef FEAT_SCROLLBIND |
| 3650 | curwin->w_scbind_pos = curwin->w_topline; |
| 3651 | #endif |
| 3652 | p_so = n; |
| 3653 | redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */ |
| 3654 | } |
| 3655 | |
| 3656 | if (p_im) |
| 3657 | need_start_insertmode = TRUE; |
| 3658 | |
Bram Moolenaar | 7b89edc | 2006-04-06 20:21:51 +0000 | [diff] [blame] | 3659 | #ifdef FEAT_AUTOCHDIR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3660 | /* Change directories when the acd option is set on. */ |
| 3661 | if (p_acd && curbuf->b_ffname != NULL |
| 3662 | && vim_chdirfile(curbuf->b_ffname) == OK) |
| 3663 | shorten_fnames(TRUE); |
Bram Moolenaar | 7b89edc | 2006-04-06 20:21:51 +0000 | [diff] [blame] | 3664 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3665 | |
Bram Moolenaar | 7b89edc | 2006-04-06 20:21:51 +0000 | [diff] [blame] | 3666 | #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3667 | if (gui.in_use && curbuf->b_ffname != NULL) |
| 3668 | { |
| 3669 | # ifdef FEAT_SUN_WORKSHOP |
| 3670 | if (usingSunWorkShop) |
| 3671 | workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro); |
| 3672 | # endif |
| 3673 | # ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | 35a9aaa | 2004-10-24 19:23:07 +0000 | [diff] [blame] | 3674 | if (usingNetbeans & ((flags & ECMD_SET_HELP) != ECMD_SET_HELP)) |
| 3675 | netbeans_file_opened(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3676 | # endif |
| 3677 | } |
| 3678 | #endif |
| 3679 | |
| 3680 | theend: |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 3681 | #ifdef FEAT_AUTOCMD |
| 3682 | if (did_set_swapcommand) |
| 3683 | set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); |
| 3684 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3685 | #ifdef FEAT_BROWSE |
| 3686 | vim_free(browse_file); |
| 3687 | #endif |
| 3688 | vim_free(free_fname); |
| 3689 | return retval; |
| 3690 | } |
| 3691 | |
| 3692 | #ifdef FEAT_AUTOCMD |
| 3693 | static void |
| 3694 | delbuf_msg(name) |
| 3695 | char_u *name; |
| 3696 | { |
| 3697 | EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"), |
| 3698 | name == NULL ? (char_u *)"" : name); |
| 3699 | vim_free(name); |
| 3700 | au_new_curbuf = NULL; |
| 3701 | } |
| 3702 | #endif |
| 3703 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3704 | static int append_indent = 0; /* autoindent for first line */ |
| 3705 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3706 | /* |
| 3707 | * ":insert" and ":append", also used by ":change" |
| 3708 | */ |
| 3709 | void |
| 3710 | ex_append(eap) |
| 3711 | exarg_T *eap; |
| 3712 | { |
| 3713 | char_u *theline; |
| 3714 | int did_undo = FALSE; |
| 3715 | linenr_T lnum = eap->line2; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3716 | int indent = 0; |
| 3717 | char_u *p; |
| 3718 | int vcol; |
| 3719 | int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3720 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3721 | /* the ! flag toggles autoindent */ |
| 3722 | if (eap->forceit) |
| 3723 | curbuf->b_p_ai = !curbuf->b_p_ai; |
| 3724 | |
| 3725 | /* First autoindent comes from the line we start on */ |
| 3726 | if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0) |
| 3727 | append_indent = get_indent_lnum(lnum); |
| 3728 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3729 | if (eap->cmdidx != CMD_append) |
| 3730 | --lnum; |
| 3731 | |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3732 | /* when the buffer is empty append to line 0 and delete the dummy line */ |
| 3733 | if (empty && lnum == 1) |
| 3734 | lnum = 0; |
| 3735 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3736 | State = INSERT; /* behave like in Insert mode */ |
| 3737 | if (curbuf->b_p_iminsert == B_IMODE_LMAP) |
| 3738 | State |= LANGMAP; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3739 | |
Bram Moolenaar | d8e9bb2 | 2005-07-09 21:14:46 +0000 | [diff] [blame] | 3740 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3741 | { |
| 3742 | msg_scroll = TRUE; |
| 3743 | need_wait_return = FALSE; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3744 | if (curbuf->b_p_ai) |
| 3745 | { |
| 3746 | if (append_indent >= 0) |
| 3747 | { |
| 3748 | indent = append_indent; |
| 3749 | append_indent = -1; |
| 3750 | } |
| 3751 | else if (lnum > 0) |
| 3752 | indent = get_indent_lnum(lnum); |
| 3753 | } |
| 3754 | ex_keep_indent = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3755 | if (eap->getline == NULL) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3756 | { |
| 3757 | /* No getline() function, use the lines that follow. This ends |
| 3758 | * when there is no more. */ |
| 3759 | if (eap->nextcmd == NULL || *eap->nextcmd == NUL) |
| 3760 | break; |
| 3761 | p = vim_strchr(eap->nextcmd, NL); |
| 3762 | if (p == NULL) |
| 3763 | p = eap->nextcmd + STRLEN(eap->nextcmd); |
| 3764 | theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd)); |
| 3765 | if (*p != NUL) |
| 3766 | ++p; |
| 3767 | eap->nextcmd = p; |
| 3768 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3769 | else |
| 3770 | theline = eap->getline( |
| 3771 | #ifdef FEAT_EVAL |
Bram Moolenaar | 3d60ec2 | 2005-01-05 22:19:46 +0000 | [diff] [blame] | 3772 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3773 | #endif |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3774 | NUL, eap->cookie, indent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3775 | lines_left = Rows - 1; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3776 | if (theline == NULL) |
| 3777 | break; |
| 3778 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3779 | /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */ |
| 3780 | if (ex_keep_indent) |
| 3781 | append_indent = indent; |
| 3782 | |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3783 | /* Look for the "." after automatic indent. */ |
| 3784 | vcol = 0; |
| 3785 | for (p = theline; indent > vcol; ++p) |
| 3786 | { |
| 3787 | if (*p == ' ') |
| 3788 | ++vcol; |
| 3789 | else if (*p == TAB) |
| 3790 | vcol += 8 - vcol % 8; |
| 3791 | else |
| 3792 | break; |
| 3793 | } |
| 3794 | if ((p[0] == '.' && p[1] == NUL) |
Bram Moolenaar | eaa48e7 | 2005-06-08 22:07:37 +0000 | [diff] [blame] | 3795 | || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0)) |
| 3796 | == FAIL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3797 | { |
| 3798 | vim_free(theline); |
| 3799 | break; |
| 3800 | } |
| 3801 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3802 | /* don't use autoindent if nothing was typed. */ |
| 3803 | if (p[0] == NUL) |
| 3804 | theline[0] = NUL; |
| 3805 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3806 | did_undo = TRUE; |
| 3807 | ml_append(lnum, theline, (colnr_T)0, FALSE); |
| 3808 | appended_lines_mark(lnum, 1L); |
| 3809 | |
| 3810 | vim_free(theline); |
| 3811 | ++lnum; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3812 | |
| 3813 | if (empty) |
| 3814 | { |
| 3815 | ml_delete(2L, FALSE); |
| 3816 | empty = FALSE; |
| 3817 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3818 | } |
| 3819 | State = NORMAL; |
| 3820 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3821 | if (eap->forceit) |
| 3822 | curbuf->b_p_ai = !curbuf->b_p_ai; |
| 3823 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3824 | /* "start" is set to eap->line2+1 unless that position is invalid (when |
| 3825 | * eap->line2 pointed to the end of the buffer and nothig was appended) |
| 3826 | * "end" is set to lnum when something has been appended, otherwise |
| 3827 | * it is the same than "start" -- Acevedo */ |
| 3828 | curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ? |
| 3829 | eap->line2 + 1 : curbuf->b_ml.ml_line_count; |
| 3830 | if (eap->cmdidx != CMD_append) |
| 3831 | --curbuf->b_op_start.lnum; |
| 3832 | curbuf->b_op_end.lnum = (eap->line2 < lnum) |
| 3833 | ? lnum : curbuf->b_op_start.lnum; |
| 3834 | curbuf->b_op_start.col = curbuf->b_op_end.col = 0; |
| 3835 | curwin->w_cursor.lnum = lnum; |
| 3836 | check_cursor_lnum(); |
| 3837 | beginline(BL_SOL | BL_FIX); |
| 3838 | |
| 3839 | need_wait_return = FALSE; /* don't use wait_return() now */ |
| 3840 | ex_no_reprint = TRUE; |
| 3841 | } |
| 3842 | |
| 3843 | /* |
| 3844 | * ":change" |
| 3845 | */ |
| 3846 | void |
| 3847 | ex_change(eap) |
| 3848 | exarg_T *eap; |
| 3849 | { |
| 3850 | linenr_T lnum; |
| 3851 | |
| 3852 | if (eap->line2 >= eap->line1 |
| 3853 | && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) |
| 3854 | return; |
| 3855 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3856 | /* the ! flag toggles autoindent */ |
| 3857 | if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai) |
| 3858 | append_indent = get_indent_lnum(eap->line1); |
| 3859 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3860 | for (lnum = eap->line2; lnum >= eap->line1; --lnum) |
| 3861 | { |
| 3862 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ |
| 3863 | break; |
| 3864 | ml_delete(eap->line1, FALSE); |
| 3865 | } |
| 3866 | deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum)); |
| 3867 | |
| 3868 | /* ":append" on the line above the deleted lines. */ |
| 3869 | eap->line2 = eap->line1; |
| 3870 | ex_append(eap); |
| 3871 | } |
| 3872 | |
| 3873 | void |
| 3874 | ex_z(eap) |
| 3875 | exarg_T *eap; |
| 3876 | { |
| 3877 | char_u *x; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3878 | int bigness; |
| 3879 | char_u *kind; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3880 | int minus = 0; |
| 3881 | linenr_T start, end, curs, i; |
| 3882 | int j; |
| 3883 | linenr_T lnum = eap->line2; |
| 3884 | |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3885 | /* Vi compatible: ":z!" uses display height, without a count uses |
| 3886 | * 'scroll' */ |
| 3887 | if (eap->forceit) |
| 3888 | bigness = curwin->w_height; |
| 3889 | else if (firstwin == lastwin) |
| 3890 | bigness = curwin->w_p_scr * 2; |
| 3891 | else |
| 3892 | bigness = curwin->w_height - 3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3893 | if (bigness < 1) |
| 3894 | bigness = 1; |
| 3895 | |
| 3896 | x = eap->arg; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3897 | kind = x; |
| 3898 | if (*kind == '-' || *kind == '+' || *kind == '=' |
| 3899 | || *kind == '^' || *kind == '.') |
| 3900 | ++x; |
| 3901 | while (*x == '-' || *x == '+') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3902 | ++x; |
| 3903 | |
| 3904 | if (*x != 0) |
| 3905 | { |
| 3906 | if (!VIM_ISDIGIT(*x)) |
| 3907 | { |
| 3908 | EMSG(_("E144: non-numeric argument to :z")); |
| 3909 | return; |
| 3910 | } |
| 3911 | else |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3912 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3913 | bigness = atoi((char *)x); |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3914 | p_window = bigness; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3915 | if (*kind == '=') |
| 3916 | bigness += 2; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3917 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3920 | /* the number of '-' and '+' multiplies the distance */ |
| 3921 | if (*kind == '-' || *kind == '+') |
| 3922 | for (x = kind + 1; *x == *kind; ++x) |
| 3923 | ; |
| 3924 | |
| 3925 | switch (*kind) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3926 | { |
| 3927 | case '-': |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3928 | start = lnum - bigness * (x - kind); |
| 3929 | end = start + bigness; |
| 3930 | curs = end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3931 | break; |
| 3932 | |
| 3933 | case '=': |
Bram Moolenaar | 2a8d1f8 | 2005-02-05 21:43:56 +0000 | [diff] [blame] | 3934 | start = lnum - (bigness + 1) / 2 + 1; |
| 3935 | end = lnum + (bigness + 1) / 2 - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3936 | curs = lnum; |
| 3937 | minus = 1; |
| 3938 | break; |
| 3939 | |
| 3940 | case '^': |
| 3941 | start = lnum - bigness * 2; |
| 3942 | end = lnum - bigness; |
| 3943 | curs = lnum - bigness; |
| 3944 | break; |
| 3945 | |
| 3946 | case '.': |
Bram Moolenaar | 2a8d1f8 | 2005-02-05 21:43:56 +0000 | [diff] [blame] | 3947 | start = lnum - (bigness + 1) / 2 + 1; |
| 3948 | end = lnum + (bigness + 1) / 2 - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3949 | curs = end; |
| 3950 | break; |
| 3951 | |
| 3952 | default: /* '+' */ |
| 3953 | start = lnum; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 3954 | if (*kind == '+') |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3955 | start += bigness * (x - kind - 1) + 1; |
| 3956 | else if (eap->addr_count == 0) |
| 3957 | ++start; |
| 3958 | end = start + bigness - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3959 | curs = end; |
| 3960 | break; |
| 3961 | } |
| 3962 | |
| 3963 | if (start < 1) |
| 3964 | start = 1; |
| 3965 | |
| 3966 | if (end > curbuf->b_ml.ml_line_count) |
| 3967 | end = curbuf->b_ml.ml_line_count; |
| 3968 | |
| 3969 | if (curs > curbuf->b_ml.ml_line_count) |
| 3970 | curs = curbuf->b_ml.ml_line_count; |
| 3971 | |
| 3972 | for (i = start; i <= end; i++) |
| 3973 | { |
| 3974 | if (minus && i == lnum) |
| 3975 | { |
| 3976 | msg_putchar('\n'); |
| 3977 | |
| 3978 | for (j = 1; j < Columns; j++) |
| 3979 | msg_putchar('-'); |
| 3980 | } |
| 3981 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3982 | print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3983 | |
| 3984 | if (minus && i == lnum) |
| 3985 | { |
| 3986 | msg_putchar('\n'); |
| 3987 | |
| 3988 | for (j = 1; j < Columns; j++) |
| 3989 | msg_putchar('-'); |
| 3990 | } |
| 3991 | } |
| 3992 | |
| 3993 | curwin->w_cursor.lnum = curs; |
| 3994 | ex_no_reprint = TRUE; |
| 3995 | } |
| 3996 | |
| 3997 | /* |
| 3998 | * Check if the restricted flag is set. |
| 3999 | * If so, give an error message and return TRUE. |
| 4000 | * Otherwise, return FALSE. |
| 4001 | */ |
| 4002 | int |
| 4003 | check_restricted() |
| 4004 | { |
| 4005 | if (restricted) |
| 4006 | { |
| 4007 | EMSG(_("E145: Shell commands not allowed in rvim")); |
| 4008 | return TRUE; |
| 4009 | } |
| 4010 | return FALSE; |
| 4011 | } |
| 4012 | |
| 4013 | /* |
| 4014 | * Check if the secure flag is set (.exrc or .vimrc in current directory). |
| 4015 | * If so, give an error message and return TRUE. |
| 4016 | * Otherwise, return FALSE. |
| 4017 | */ |
| 4018 | int |
| 4019 | check_secure() |
| 4020 | { |
| 4021 | if (secure) |
| 4022 | { |
| 4023 | secure = 2; |
| 4024 | EMSG(_(e_curdir)); |
| 4025 | return TRUE; |
| 4026 | } |
| 4027 | #ifdef HAVE_SANDBOX |
| 4028 | /* |
| 4029 | * In the sandbox more things are not allowed, including the things |
| 4030 | * disallowed in secure mode. |
| 4031 | */ |
| 4032 | if (sandbox != 0) |
| 4033 | { |
| 4034 | EMSG(_(e_sandbox)); |
| 4035 | return TRUE; |
| 4036 | } |
| 4037 | #endif |
| 4038 | return FALSE; |
| 4039 | } |
| 4040 | |
| 4041 | static char_u *old_sub = NULL; /* previous substitute pattern */ |
| 4042 | static int global_need_beginline; /* call beginline() after ":g" */ |
| 4043 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4044 | /* do_sub() |
| 4045 | * |
| 4046 | * Perform a substitution from line eap->line1 to line eap->line2 using the |
| 4047 | * command pointed to by eap->arg which should be of the form: |
| 4048 | * |
| 4049 | * /pattern/substitution/{flags} |
| 4050 | * |
| 4051 | * The usual escapes are supported as described in the regexp docs. |
| 4052 | */ |
| 4053 | void |
| 4054 | do_sub(eap) |
| 4055 | exarg_T *eap; |
| 4056 | { |
| 4057 | linenr_T lnum; |
| 4058 | long i = 0; |
| 4059 | regmmatch_T regmatch; |
| 4060 | static int do_all = FALSE; /* do multiple substitutions per line */ |
| 4061 | static int do_ask = FALSE; /* ask for confirmation */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4062 | static int do_count = FALSE; /* count only */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4063 | static int do_error = TRUE; /* if false, ignore errors */ |
| 4064 | static int do_print = FALSE; /* print last line with subs. */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4065 | static int do_list = FALSE; /* list last line with subs. */ |
| 4066 | static int do_number = FALSE; /* list last line with line nr*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4067 | static int do_ic = 0; /* ignore case flag */ |
| 4068 | char_u *pat = NULL, *sub = NULL; /* init for GCC */ |
| 4069 | int delimiter; |
| 4070 | int sublen; |
| 4071 | int got_quit = FALSE; |
| 4072 | int got_match = FALSE; |
| 4073 | int temp; |
| 4074 | int which_pat; |
| 4075 | char_u *cmd; |
| 4076 | int save_State; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4077 | linenr_T first_line = 0; /* first changed line */ |
| 4078 | linenr_T last_line= 0; /* below last changed line AFTER the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4079 | * change */ |
| 4080 | linenr_T old_line_count = curbuf->b_ml.ml_line_count; |
| 4081 | linenr_T line2; |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 4082 | long nmatch; /* number of lines in match */ |
| 4083 | linenr_T sub_firstlnum; /* nr of first sub line */ |
| 4084 | char_u *sub_firstline; /* allocated copy of first sub line */ |
| 4085 | int endcolumn = FALSE; /* cursor in last column when done */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4086 | pos_T old_cursor = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4087 | |
| 4088 | cmd = eap->arg; |
| 4089 | if (!global_busy) |
| 4090 | { |
| 4091 | sub_nsubs = 0; |
| 4092 | sub_nlines = 0; |
| 4093 | } |
| 4094 | |
| 4095 | #ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */ |
| 4096 | if (p_altkeymap && curwin->w_p_rl) |
| 4097 | lrF_sub(cmd); |
| 4098 | #endif |
| 4099 | |
| 4100 | if (eap->cmdidx == CMD_tilde) |
| 4101 | which_pat = RE_LAST; /* use last used regexp */ |
| 4102 | else |
| 4103 | which_pat = RE_SUBST; /* use last substitute regexp */ |
| 4104 | |
| 4105 | /* new pattern and substitution */ |
| 4106 | if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd) |
| 4107 | && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL) |
| 4108 | { |
| 4109 | /* don't accept alphanumeric for separator */ |
| 4110 | if (isalpha(*cmd)) |
| 4111 | { |
| 4112 | EMSG(_("E146: Regular expressions can't be delimited by letters")); |
| 4113 | return; |
| 4114 | } |
| 4115 | /* |
| 4116 | * undocumented vi feature: |
| 4117 | * "\/sub/" and "\?sub?" use last used search pattern (almost like |
| 4118 | * //sub/r). "\&sub&" use last substitute pattern (like //sub/). |
| 4119 | */ |
| 4120 | if (*cmd == '\\') |
| 4121 | { |
| 4122 | ++cmd; |
| 4123 | if (vim_strchr((char_u *)"/?&", *cmd) == NULL) |
| 4124 | { |
| 4125 | EMSG(_(e_backslash)); |
| 4126 | return; |
| 4127 | } |
| 4128 | if (*cmd != '&') |
| 4129 | which_pat = RE_SEARCH; /* use last '/' pattern */ |
| 4130 | pat = (char_u *)""; /* empty search pattern */ |
| 4131 | delimiter = *cmd++; /* remember delimiter character */ |
| 4132 | } |
| 4133 | else /* find the end of the regexp */ |
| 4134 | { |
| 4135 | which_pat = RE_LAST; /* use last used regexp */ |
| 4136 | delimiter = *cmd++; /* remember delimiter character */ |
| 4137 | pat = cmd; /* remember start of search pat */ |
| 4138 | cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg); |
| 4139 | if (cmd[0] == delimiter) /* end delimiter found */ |
| 4140 | *cmd++ = NUL; /* replace it with a NUL */ |
| 4141 | } |
| 4142 | |
| 4143 | /* |
| 4144 | * Small incompatibility: vi sees '\n' as end of the command, but in |
| 4145 | * Vim we want to use '\n' to find/substitute a NUL. |
| 4146 | */ |
| 4147 | sub = cmd; /* remember the start of the substitution */ |
| 4148 | |
| 4149 | while (cmd[0]) |
| 4150 | { |
| 4151 | if (cmd[0] == delimiter) /* end delimiter found */ |
| 4152 | { |
| 4153 | *cmd++ = NUL; /* replace it with a NUL */ |
| 4154 | break; |
| 4155 | } |
| 4156 | if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */ |
| 4157 | ++cmd; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4158 | mb_ptr_adv(cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | if (!eap->skip) |
| 4162 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4163 | /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */ |
| 4164 | if (STRCMP(sub, "%") == 0 |
| 4165 | && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL) |
| 4166 | { |
| 4167 | if (old_sub == NULL) /* there is no previous command */ |
| 4168 | { |
| 4169 | EMSG(_(e_nopresub)); |
| 4170 | return; |
| 4171 | } |
| 4172 | sub = old_sub; |
| 4173 | } |
| 4174 | else |
| 4175 | { |
| 4176 | vim_free(old_sub); |
| 4177 | old_sub = vim_strsave(sub); |
| 4178 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4179 | } |
| 4180 | } |
| 4181 | else if (!eap->skip) /* use previous pattern and substitution */ |
| 4182 | { |
| 4183 | if (old_sub == NULL) /* there is no previous command */ |
| 4184 | { |
| 4185 | EMSG(_(e_nopresub)); |
| 4186 | return; |
| 4187 | } |
| 4188 | pat = NULL; /* search_regcomp() will use previous pattern */ |
| 4189 | sub = old_sub; |
Bram Moolenaar | b11bd7e | 2005-02-07 22:05:52 +0000 | [diff] [blame] | 4190 | |
| 4191 | /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the |
| 4192 | * last column after using "$". */ |
| 4193 | endcolumn = (curwin->w_curswant == MAXCOL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | /* |
| 4197 | * Find trailing options. When '&' is used, keep old options. |
| 4198 | */ |
| 4199 | if (*cmd == '&') |
| 4200 | ++cmd; |
| 4201 | else |
| 4202 | { |
| 4203 | if (!p_ed) |
| 4204 | { |
| 4205 | if (p_gd) /* default is global on */ |
| 4206 | do_all = TRUE; |
| 4207 | else |
| 4208 | do_all = FALSE; |
| 4209 | do_ask = FALSE; |
| 4210 | } |
| 4211 | do_error = TRUE; |
| 4212 | do_print = FALSE; |
Bram Moolenaar | cc016f5 | 2005-12-10 20:23:46 +0000 | [diff] [blame] | 4213 | do_count = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4214 | do_ic = 0; |
| 4215 | } |
| 4216 | while (*cmd) |
| 4217 | { |
| 4218 | /* |
| 4219 | * Note that 'g' and 'c' are always inverted, also when p_ed is off. |
| 4220 | * 'r' is never inverted. |
| 4221 | */ |
| 4222 | if (*cmd == 'g') |
| 4223 | do_all = !do_all; |
| 4224 | else if (*cmd == 'c') |
| 4225 | do_ask = !do_ask; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4226 | else if (*cmd == 'n') |
| 4227 | do_count = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4228 | else if (*cmd == 'e') |
| 4229 | do_error = !do_error; |
| 4230 | else if (*cmd == 'r') /* use last used regexp */ |
| 4231 | which_pat = RE_LAST; |
| 4232 | else if (*cmd == 'p') |
| 4233 | do_print = TRUE; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4234 | else if (*cmd == '#') |
| 4235 | { |
| 4236 | do_print = TRUE; |
| 4237 | do_number = TRUE; |
| 4238 | } |
| 4239 | else if (*cmd == 'l') |
| 4240 | { |
| 4241 | do_print = TRUE; |
| 4242 | do_list = TRUE; |
| 4243 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4244 | else if (*cmd == 'i') /* ignore case */ |
| 4245 | do_ic = 'i'; |
| 4246 | else if (*cmd == 'I') /* don't ignore case */ |
| 4247 | do_ic = 'I'; |
| 4248 | else |
| 4249 | break; |
| 4250 | ++cmd; |
| 4251 | } |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4252 | if (do_count) |
| 4253 | do_ask = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4254 | |
| 4255 | /* |
| 4256 | * check for a trailing count |
| 4257 | */ |
| 4258 | cmd = skipwhite(cmd); |
| 4259 | if (VIM_ISDIGIT(*cmd)) |
| 4260 | { |
| 4261 | i = getdigits(&cmd); |
| 4262 | if (i <= 0 && !eap->skip && do_error) |
| 4263 | { |
| 4264 | EMSG(_(e_zerocount)); |
| 4265 | return; |
| 4266 | } |
| 4267 | eap->line1 = eap->line2; |
| 4268 | eap->line2 += i - 1; |
| 4269 | if (eap->line2 > curbuf->b_ml.ml_line_count) |
| 4270 | eap->line2 = curbuf->b_ml.ml_line_count; |
| 4271 | } |
| 4272 | |
| 4273 | /* |
| 4274 | * check for trailing command or garbage |
| 4275 | */ |
| 4276 | cmd = skipwhite(cmd); |
| 4277 | if (*cmd && *cmd != '"') /* if not end-of-line or comment */ |
| 4278 | { |
| 4279 | eap->nextcmd = check_nextcmd(cmd); |
| 4280 | if (eap->nextcmd == NULL) |
| 4281 | { |
| 4282 | EMSG(_(e_trailing)); |
| 4283 | return; |
| 4284 | } |
| 4285 | } |
| 4286 | |
| 4287 | if (eap->skip) /* not executing commands, only parsing */ |
| 4288 | return; |
| 4289 | |
| 4290 | if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, ®match) == FAIL) |
| 4291 | { |
| 4292 | if (do_error) |
| 4293 | EMSG(_(e_invcmd)); |
| 4294 | return; |
| 4295 | } |
| 4296 | |
| 4297 | /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */ |
| 4298 | if (do_ic == 'i') |
| 4299 | regmatch.rmm_ic = TRUE; |
| 4300 | else if (do_ic == 'I') |
| 4301 | regmatch.rmm_ic = FALSE; |
| 4302 | |
| 4303 | sub_firstline = NULL; |
| 4304 | |
| 4305 | /* |
| 4306 | * ~ in the substitute pattern is replaced with the old pattern. |
| 4307 | * We do it here once to avoid it to be replaced over and over again. |
| 4308 | * But don't do it when it starts with "\=", then it's an expression. |
| 4309 | */ |
| 4310 | if (!(sub[0] == '\\' && sub[1] == '=')) |
| 4311 | sub = regtilde(sub, p_magic); |
| 4312 | |
| 4313 | /* |
| 4314 | * Check for a match on each line. |
| 4315 | */ |
| 4316 | line2 = eap->line2; |
| 4317 | for (lnum = eap->line1; lnum <= line2 && !(got_quit |
| 4318 | #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) |
| 4319 | || aborting() |
| 4320 | #endif |
| 4321 | ); ++lnum) |
| 4322 | { |
| 4323 | sub_firstlnum = lnum; |
| 4324 | nmatch = vim_regexec_multi(®match, curwin, curbuf, lnum, (colnr_T)0); |
| 4325 | if (nmatch) |
| 4326 | { |
| 4327 | colnr_T copycol; |
| 4328 | colnr_T matchcol; |
| 4329 | colnr_T prev_matchcol = MAXCOL; |
| 4330 | char_u *new_end, *new_start = NULL; |
| 4331 | unsigned new_start_len = 0; |
| 4332 | char_u *p1; |
| 4333 | int did_sub = FALSE; |
| 4334 | int lastone; |
| 4335 | unsigned len, needed_len; |
| 4336 | long nmatch_tl = 0; /* nr of lines matched below lnum */ |
| 4337 | int do_again; /* do it again after joining lines */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 4338 | int skip_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4339 | |
| 4340 | /* |
| 4341 | * The new text is build up step by step, to avoid too much |
| 4342 | * copying. There are these pieces: |
| 4343 | * sub_firstline The old text, unmodifed. |
| 4344 | * copycol Column in the old text where we started |
| 4345 | * looking for a match; from here old text still |
| 4346 | * needs to be copied to the new text. |
| 4347 | * matchcol Column number of the old text where to look |
| 4348 | * for the next match. It's just after the |
| 4349 | * previous match or one further. |
| 4350 | * prev_matchcol Column just after the previous match (if any). |
| 4351 | * Mostly equal to matchcol, except for the first |
| 4352 | * match and after skipping an empty match. |
| 4353 | * regmatch.*pos Where the pattern matched in the old text. |
| 4354 | * new_start The new text, all that has been produced so |
| 4355 | * far. |
| 4356 | * new_end The new text, where to append new text. |
| 4357 | * |
| 4358 | * lnum The line number where we were looking for the |
| 4359 | * first match in the old line. |
| 4360 | * sub_firstlnum The line number in the buffer where to look |
| 4361 | * for a match. Can be different from "lnum" |
| 4362 | * when the pattern or substitute string contains |
| 4363 | * line breaks. |
| 4364 | * |
| 4365 | * Special situations: |
| 4366 | * - When the substitute string contains a line break, the part up |
| 4367 | * to the line break is inserted in the text, but the copy of |
| 4368 | * the original line is kept. "sub_firstlnum" is adjusted for |
| 4369 | * the inserted lines. |
| 4370 | * - When the matched pattern contains a line break, the old line |
| 4371 | * is taken from the line at the end of the pattern. The lines |
| 4372 | * in the match are deleted later, "sub_firstlnum" is adjusted |
| 4373 | * accordingly. |
| 4374 | * |
| 4375 | * The new text is built up in new_start[]. It has some extra |
| 4376 | * room to avoid using alloc()/free() too often. new_start_len is |
| 4377 | * the lenght of the allocated memory at new_start. |
| 4378 | * |
| 4379 | * Make a copy of the old line, so it won't be taken away when |
| 4380 | * updating the screen or handling a multi-line match. The "old_" |
| 4381 | * pointers point into this copy. |
| 4382 | */ |
| 4383 | sub_firstline = vim_strsave(ml_get(sub_firstlnum)); |
| 4384 | if (sub_firstline == NULL) |
| 4385 | { |
| 4386 | vim_free(new_start); |
| 4387 | goto outofmem; |
| 4388 | } |
| 4389 | copycol = 0; |
| 4390 | matchcol = 0; |
| 4391 | |
| 4392 | /* At first match, remember current cursor position. */ |
| 4393 | if (!got_match) |
| 4394 | { |
| 4395 | setpcmark(); |
| 4396 | got_match = TRUE; |
| 4397 | } |
| 4398 | |
| 4399 | /* |
| 4400 | * Loop until nothing more to replace in this line. |
| 4401 | * 1. Handle match with empty string. |
| 4402 | * 2. If do_ask is set, ask for confirmation. |
| 4403 | * 3. substitute the string. |
| 4404 | * 4. if do_all is set, find next match |
| 4405 | * 5. break if there isn't another match in this line |
| 4406 | */ |
| 4407 | for (;;) |
| 4408 | { |
| 4409 | /* Save the line number of the last change for the final |
| 4410 | * cursor position (just like Vi). */ |
| 4411 | curwin->w_cursor.lnum = lnum; |
| 4412 | do_again = FALSE; |
| 4413 | |
| 4414 | /* |
| 4415 | * 1. Match empty string does not count, except for first |
| 4416 | * match. This reproduces the strange vi behaviour. |
| 4417 | * This also catches endless loops. |
| 4418 | */ |
| 4419 | if (matchcol == prev_matchcol |
| 4420 | && regmatch.endpos[0].lnum == 0 |
| 4421 | && matchcol == regmatch.endpos[0].col) |
| 4422 | { |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 4423 | if (sub_firstline[matchcol] == NUL) |
| 4424 | /* We already were at the end of the line. Don't look |
| 4425 | * for a match in this line again. */ |
| 4426 | skip_match = TRUE; |
| 4427 | else |
| 4428 | ++matchcol; /* search for a match at next column */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4429 | goto skip; |
| 4430 | } |
| 4431 | |
| 4432 | /* Normally we continue searching for a match just after the |
| 4433 | * previous match. */ |
| 4434 | matchcol = regmatch.endpos[0].col; |
| 4435 | prev_matchcol = matchcol; |
| 4436 | |
| 4437 | /* |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4438 | * 2. If do_count is set only increase the counter. |
| 4439 | * If do_ask is set, ask for confirmation. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4440 | */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4441 | if (do_count) |
| 4442 | { |
| 4443 | /* For a multi-line match, put matchcol at the NUL at |
| 4444 | * the end of the line and set nmatch to one, so that |
| 4445 | * we continue looking for a match on the next line. |
| 4446 | * Avoids that ":s/\nB\@=//gc" get stuck. */ |
| 4447 | if (nmatch > 1) |
| 4448 | { |
| 4449 | matchcol = STRLEN(sub_firstline); |
| 4450 | nmatch = 1; |
| 4451 | } |
| 4452 | sub_nsubs++; |
| 4453 | did_sub = TRUE; |
| 4454 | goto skip; |
| 4455 | } |
| 4456 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4457 | if (do_ask) |
| 4458 | { |
| 4459 | /* change State to CONFIRM, so that the mouse works |
| 4460 | * properly */ |
| 4461 | save_State = State; |
| 4462 | State = CONFIRM; |
| 4463 | #ifdef FEAT_MOUSE |
| 4464 | setmouse(); /* disable mouse in xterm */ |
| 4465 | #endif |
| 4466 | curwin->w_cursor.col = regmatch.startpos[0].col; |
| 4467 | |
| 4468 | /* When 'cpoptions' contains "u" don't sync undo when |
| 4469 | * asking for confirmation. */ |
| 4470 | if (vim_strchr(p_cpo, CPO_UNDO) != NULL) |
| 4471 | ++no_u_sync; |
| 4472 | |
| 4473 | /* |
| 4474 | * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed. |
| 4475 | */ |
| 4476 | while (do_ask) |
| 4477 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4478 | if (exmode_active) |
| 4479 | { |
| 4480 | char_u *resp; |
| 4481 | colnr_T sc, ec; |
| 4482 | |
| 4483 | print_line_no_prefix(lnum, FALSE, FALSE); |
| 4484 | |
| 4485 | getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL); |
| 4486 | curwin->w_cursor.col = regmatch.endpos[0].col - 1; |
| 4487 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec); |
| 4488 | msg_start(); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4489 | for (i = 0; i < (long)sc; ++i) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4490 | msg_putchar(' '); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4491 | for ( ; i <= (long)ec; ++i) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4492 | msg_putchar('^'); |
| 4493 | |
| 4494 | resp = getexmodeline('?', NULL, 0); |
| 4495 | if (resp != NULL) |
| 4496 | { |
| 4497 | i = *resp; |
| 4498 | vim_free(resp); |
| 4499 | } |
| 4500 | } |
| 4501 | else |
| 4502 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4503 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4504 | int save_p_fen = curwin->w_p_fen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4505 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4506 | curwin->w_p_fen = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4507 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4508 | /* Invert the matched string. |
| 4509 | * Remove the inversion afterwards. */ |
| 4510 | temp = RedrawingDisabled; |
| 4511 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4512 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4513 | search_match_lines = regmatch.endpos[0].lnum; |
| 4514 | search_match_endcol = regmatch.endpos[0].col; |
| 4515 | highlight_match = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4516 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4517 | update_topline(); |
| 4518 | validate_cursor(); |
Bram Moolenaar | a1956f6 | 2006-03-12 22:18:00 +0000 | [diff] [blame] | 4519 | update_screen(SOME_VALID); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4520 | highlight_match = FALSE; |
Bram Moolenaar | a1956f6 | 2006-03-12 22:18:00 +0000 | [diff] [blame] | 4521 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4522 | |
| 4523 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4524 | curwin->w_p_fen = save_p_fen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4525 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4526 | if (msg_row == Rows - 1) |
| 4527 | msg_didout = FALSE; /* avoid a scroll-up */ |
| 4528 | msg_starthere(); |
| 4529 | i = msg_scroll; |
| 4530 | msg_scroll = 0; /* truncate msg when |
| 4531 | needed */ |
| 4532 | msg_no_more = TRUE; |
| 4533 | /* write message same highlighting as for |
| 4534 | * wait_return */ |
| 4535 | smsg_attr(hl_attr(HLF_R), |
| 4536 | (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub); |
| 4537 | msg_no_more = FALSE; |
| 4538 | msg_scroll = i; |
| 4539 | showruler(TRUE); |
| 4540 | windgoto(msg_row, msg_col); |
| 4541 | RedrawingDisabled = temp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4542 | |
| 4543 | #ifdef USE_ON_FLY_SCROLL |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4544 | dont_scroll = FALSE; /* allow scrolling here */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4545 | #endif |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4546 | ++no_mapping; /* don't map this key */ |
| 4547 | ++allow_keys; /* allow special keys */ |
| 4548 | i = safe_vgetc(); |
| 4549 | --allow_keys; |
| 4550 | --no_mapping; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4551 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4552 | /* clear the question */ |
| 4553 | msg_didout = FALSE; /* don't scroll up */ |
| 4554 | msg_col = 0; |
| 4555 | gotocmdline(TRUE); |
| 4556 | } |
| 4557 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4558 | need_wait_return = FALSE; /* no hit-return prompt */ |
| 4559 | if (i == 'q' || i == ESC || i == Ctrl_C |
| 4560 | #ifdef UNIX |
| 4561 | || i == intr_char |
| 4562 | #endif |
| 4563 | ) |
| 4564 | { |
| 4565 | got_quit = TRUE; |
| 4566 | break; |
| 4567 | } |
| 4568 | if (i == 'n') |
| 4569 | break; |
| 4570 | if (i == 'y') |
| 4571 | break; |
| 4572 | if (i == 'l') |
| 4573 | { |
| 4574 | /* last: replace and then stop */ |
| 4575 | do_all = FALSE; |
| 4576 | line2 = lnum; |
| 4577 | break; |
| 4578 | } |
| 4579 | if (i == 'a') |
| 4580 | { |
| 4581 | do_ask = FALSE; |
| 4582 | break; |
| 4583 | } |
| 4584 | #ifdef FEAT_INS_EXPAND |
| 4585 | if (i == Ctrl_E) |
| 4586 | scrollup_clamp(); |
| 4587 | else if (i == Ctrl_Y) |
| 4588 | scrolldown_clamp(); |
| 4589 | #endif |
| 4590 | } |
| 4591 | State = save_State; |
| 4592 | #ifdef FEAT_MOUSE |
| 4593 | setmouse(); |
| 4594 | #endif |
| 4595 | if (vim_strchr(p_cpo, CPO_UNDO) != NULL) |
| 4596 | --no_u_sync; |
| 4597 | |
| 4598 | if (i == 'n') |
| 4599 | { |
| 4600 | /* For a multi-line match, put matchcol at the NUL at |
| 4601 | * the end of the line and set nmatch to one, so that |
| 4602 | * we continue looking for a match on the next line. |
| 4603 | * Avoids that ":s/\nB\@=//gc" get stuck. */ |
| 4604 | if (nmatch > 1) |
| 4605 | { |
| 4606 | matchcol = STRLEN(sub_firstline); |
| 4607 | nmatch = 1; |
| 4608 | } |
| 4609 | goto skip; |
| 4610 | } |
| 4611 | if (got_quit) |
| 4612 | break; |
| 4613 | } |
| 4614 | |
| 4615 | /* Move the cursor to the start of the match, so that we can |
| 4616 | * use "\=col("."). */ |
| 4617 | curwin->w_cursor.col = regmatch.startpos[0].col; |
| 4618 | |
| 4619 | /* |
| 4620 | * 3. substitute the string. |
| 4621 | */ |
| 4622 | /* get length of substitution part */ |
| 4623 | sublen = vim_regsub_multi(®match, sub_firstlnum, |
| 4624 | sub, sub_firstline, FALSE, p_magic, TRUE); |
| 4625 | |
Bram Moolenaar | 4463f29 | 2005-09-25 22:20:24 +0000 | [diff] [blame] | 4626 | /* When the match included the "$" of the last line it may |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 4627 | * go beyond the last line of the buffer. */ |
Bram Moolenaar | 4463f29 | 2005-09-25 22:20:24 +0000 | [diff] [blame] | 4628 | if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) |
| 4629 | { |
| 4630 | nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1; |
| 4631 | skip_match = TRUE; |
| 4632 | } |
| 4633 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4634 | /* Need room for: |
| 4635 | * - result so far in new_start (not for first sub in line) |
| 4636 | * - original text up to match |
| 4637 | * - length of substituted part |
| 4638 | * - original text after match |
| 4639 | */ |
| 4640 | if (nmatch == 1) |
| 4641 | p1 = sub_firstline; |
| 4642 | else |
| 4643 | { |
| 4644 | p1 = ml_get(sub_firstlnum + nmatch - 1); |
| 4645 | nmatch_tl += nmatch - 1; |
| 4646 | } |
| 4647 | i = regmatch.startpos[0].col - copycol; |
| 4648 | needed_len = i + ((unsigned)STRLEN(p1) - regmatch.endpos[0].col) |
| 4649 | + sublen + 1; |
| 4650 | if (new_start == NULL) |
| 4651 | { |
| 4652 | /* |
| 4653 | * Get some space for a temporary buffer to do the |
| 4654 | * substitution into (and some extra space to avoid |
| 4655 | * too many calls to alloc()/free()). |
| 4656 | */ |
| 4657 | new_start_len = needed_len + 50; |
| 4658 | if ((new_start = alloc_check(new_start_len)) == NULL) |
| 4659 | goto outofmem; |
| 4660 | *new_start = NUL; |
| 4661 | new_end = new_start; |
| 4662 | } |
| 4663 | else |
| 4664 | { |
| 4665 | /* |
| 4666 | * Check if the temporary buffer is long enough to do the |
| 4667 | * substitution into. If not, make it larger (with a bit |
| 4668 | * extra to avoid too many calls to alloc()/free()). |
| 4669 | */ |
| 4670 | len = (unsigned)STRLEN(new_start); |
| 4671 | needed_len += len; |
| 4672 | if (needed_len > new_start_len) |
| 4673 | { |
| 4674 | new_start_len = needed_len + 50; |
| 4675 | if ((p1 = alloc_check(new_start_len)) == NULL) |
| 4676 | { |
| 4677 | vim_free(new_start); |
| 4678 | goto outofmem; |
| 4679 | } |
| 4680 | mch_memmove(p1, new_start, (size_t)(len + 1)); |
| 4681 | vim_free(new_start); |
| 4682 | new_start = p1; |
| 4683 | } |
| 4684 | new_end = new_start + len; |
| 4685 | } |
| 4686 | |
| 4687 | /* |
| 4688 | * copy the text up to the part that matched |
| 4689 | */ |
| 4690 | mch_memmove(new_end, sub_firstline + copycol, (size_t)i); |
| 4691 | new_end += i; |
| 4692 | |
| 4693 | (void)vim_regsub_multi(®match, sub_firstlnum, |
| 4694 | sub, new_end, TRUE, p_magic, TRUE); |
| 4695 | sub_nsubs++; |
| 4696 | did_sub = TRUE; |
| 4697 | |
| 4698 | /* Move the cursor to the start of the line, to avoid that it |
| 4699 | * is beyond the end of the line after the substitution. */ |
| 4700 | curwin->w_cursor.col = 0; |
| 4701 | |
| 4702 | /* For a multi-line match, make a copy of the last matched |
| 4703 | * line and continue in that one. */ |
| 4704 | if (nmatch > 1) |
| 4705 | { |
| 4706 | sub_firstlnum += nmatch - 1; |
| 4707 | vim_free(sub_firstline); |
| 4708 | sub_firstline = vim_strsave(ml_get(sub_firstlnum)); |
| 4709 | /* When going beyond the last line, stop substituting. */ |
| 4710 | if (sub_firstlnum <= line2) |
| 4711 | do_again = TRUE; |
| 4712 | else |
| 4713 | do_all = FALSE; |
| 4714 | } |
| 4715 | |
| 4716 | /* Remember next character to be copied. */ |
| 4717 | copycol = regmatch.endpos[0].col; |
| 4718 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 4719 | if (skip_match) |
| 4720 | { |
| 4721 | /* Already hit end of the buffer, sub_firstlnum is one |
| 4722 | * less than what it ought to be. */ |
| 4723 | vim_free(sub_firstline); |
| 4724 | sub_firstline = vim_strsave((char_u *)""); |
| 4725 | copycol = 0; |
| 4726 | } |
| 4727 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4728 | /* |
| 4729 | * Now the trick is to replace CTRL-M chars with a real line |
| 4730 | * break. This would make it impossible to insert a CTRL-M in |
| 4731 | * the text. The line break can be avoided by preceding the |
| 4732 | * CTRL-M with a backslash. To be able to insert a backslash, |
| 4733 | * they must be doubled in the string and are halved here. |
| 4734 | * That is Vi compatible. |
| 4735 | */ |
| 4736 | for (p1 = new_end; *p1; ++p1) |
| 4737 | { |
| 4738 | if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */ |
| 4739 | mch_memmove(p1, p1 + 1, STRLEN(p1)); |
| 4740 | else if (*p1 == CAR) |
| 4741 | { |
| 4742 | if (u_inssub(lnum) == OK) /* prepare for undo */ |
| 4743 | { |
| 4744 | *p1 = NUL; /* truncate up to the CR */ |
| 4745 | ml_append(lnum - 1, new_start, |
| 4746 | (colnr_T)(p1 - new_start + 1), FALSE); |
| 4747 | mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); |
| 4748 | if (do_ask) |
| 4749 | appended_lines(lnum - 1, 1L); |
| 4750 | else |
| 4751 | { |
| 4752 | if (first_line == 0) |
| 4753 | first_line = lnum; |
| 4754 | last_line = lnum + 1; |
| 4755 | } |
| 4756 | /* All line numbers increase. */ |
| 4757 | ++sub_firstlnum; |
| 4758 | ++lnum; |
| 4759 | ++line2; |
| 4760 | /* move the cursor to the new line, like Vi */ |
| 4761 | ++curwin->w_cursor.lnum; |
| 4762 | STRCPY(new_start, p1 + 1); /* copy the rest */ |
| 4763 | p1 = new_start - 1; |
| 4764 | } |
| 4765 | } |
| 4766 | #ifdef FEAT_MBYTE |
| 4767 | else if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4768 | p1 += (*mb_ptr2len)(p1) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4769 | #endif |
| 4770 | } |
| 4771 | |
| 4772 | /* |
| 4773 | * 4. If do_all is set, find next match. |
| 4774 | * Prevent endless loop with patterns that match empty |
| 4775 | * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g. |
| 4776 | * But ":s/\n/#/" is OK. |
| 4777 | */ |
| 4778 | skip: |
| 4779 | /* We already know that we did the last subst when we are at |
| 4780 | * the end of the line, except that a pattern like |
| 4781 | * "bar\|\nfoo" may match at the NUL. */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 4782 | lastone = (skip_match |
| 4783 | || got_int |
| 4784 | || got_quit |
| 4785 | || !(do_all || do_again) |
| 4786 | || (sub_firstline[matchcol] == NUL && nmatch <= 1 |
| 4787 | && !re_multiline(regmatch.regprog))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4788 | nmatch = -1; |
| 4789 | |
| 4790 | /* |
| 4791 | * Replace the line in the buffer when needed. This is |
| 4792 | * skipped when there are more matches. |
| 4793 | * The check for nmatch_tl is needed for when multi-line |
| 4794 | * matching must replace the lines before trying to do another |
| 4795 | * match, otherwise "\@<=" won't work. |
| 4796 | * When asking the user we like to show the already replaced |
| 4797 | * text, but don't do it when "\<@=" or "\<@!" is used, it |
| 4798 | * changes what matches. |
| 4799 | */ |
| 4800 | if (lastone |
| 4801 | || (do_ask && !re_lookbehind(regmatch.regprog)) |
| 4802 | || nmatch_tl > 0 |
| 4803 | || (nmatch = vim_regexec_multi(®match, curwin, |
| 4804 | curbuf, sub_firstlnum, matchcol)) == 0) |
| 4805 | { |
| 4806 | if (new_start != NULL) |
| 4807 | { |
| 4808 | /* |
| 4809 | * Copy the rest of the line, that didn't match. |
| 4810 | * "matchcol" has to be adjusted, we use the end of |
| 4811 | * the line as reference, because the substitute may |
| 4812 | * have changed the number of characters. Same for |
| 4813 | * "prev_matchcol". |
| 4814 | */ |
| 4815 | STRCAT(new_start, sub_firstline + copycol); |
| 4816 | matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; |
| 4817 | prev_matchcol = (colnr_T)STRLEN(sub_firstline) |
| 4818 | - prev_matchcol; |
| 4819 | |
| 4820 | if (u_savesub(lnum) != OK) |
| 4821 | break; |
| 4822 | ml_replace(lnum, new_start, TRUE); |
| 4823 | |
| 4824 | if (nmatch_tl > 0) |
| 4825 | { |
| 4826 | /* |
| 4827 | * Matched lines have now been substituted and are |
| 4828 | * useless, delete them. The part after the match |
| 4829 | * has been appended to new_start, we don't need |
| 4830 | * it in the buffer. |
| 4831 | */ |
| 4832 | ++lnum; |
| 4833 | if (u_savedel(lnum, nmatch_tl) != OK) |
| 4834 | break; |
| 4835 | for (i = 0; i < nmatch_tl; ++i) |
| 4836 | ml_delete(lnum, (int)FALSE); |
| 4837 | mark_adjust(lnum, lnum + nmatch_tl - 1, |
| 4838 | (long)MAXLNUM, -nmatch_tl); |
| 4839 | if (do_ask) |
| 4840 | deleted_lines(lnum, nmatch_tl); |
| 4841 | --lnum; |
| 4842 | line2 -= nmatch_tl; /* nr of lines decreases */ |
| 4843 | nmatch_tl = 0; |
| 4844 | } |
| 4845 | |
| 4846 | /* When asking, undo is saved each time, must also set |
| 4847 | * changed flag each time. */ |
| 4848 | if (do_ask) |
| 4849 | changed_bytes(lnum, 0); |
| 4850 | else |
| 4851 | { |
| 4852 | if (first_line == 0) |
| 4853 | first_line = lnum; |
| 4854 | last_line = lnum + 1; |
| 4855 | } |
| 4856 | |
| 4857 | sub_firstlnum = lnum; |
| 4858 | vim_free(sub_firstline); /* free the temp buffer */ |
| 4859 | sub_firstline = new_start; |
| 4860 | new_start = NULL; |
| 4861 | matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; |
| 4862 | prev_matchcol = (colnr_T)STRLEN(sub_firstline) |
| 4863 | - prev_matchcol; |
| 4864 | copycol = 0; |
| 4865 | } |
| 4866 | if (nmatch == -1 && !lastone) |
| 4867 | nmatch = vim_regexec_multi(®match, curwin, curbuf, |
| 4868 | sub_firstlnum, matchcol); |
| 4869 | |
| 4870 | /* |
| 4871 | * 5. break if there isn't another match in this line |
| 4872 | */ |
| 4873 | if (nmatch <= 0) |
| 4874 | break; |
| 4875 | } |
| 4876 | |
| 4877 | line_breakcheck(); |
| 4878 | } |
| 4879 | |
| 4880 | if (did_sub) |
| 4881 | ++sub_nlines; |
| 4882 | vim_free(sub_firstline); /* free the copy of the original line */ |
| 4883 | sub_firstline = NULL; |
| 4884 | } |
| 4885 | |
| 4886 | line_breakcheck(); |
| 4887 | } |
| 4888 | |
| 4889 | if (first_line != 0) |
| 4890 | { |
| 4891 | /* Need to subtract the number of added lines from "last_line" to get |
| 4892 | * the line number before the change (same as adding the number of |
| 4893 | * deleted lines). */ |
| 4894 | i = curbuf->b_ml.ml_line_count - old_line_count; |
| 4895 | changed_lines(first_line, 0, last_line - i, i); |
| 4896 | } |
| 4897 | |
| 4898 | outofmem: |
| 4899 | 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] | 4900 | |
| 4901 | /* ":s/pat//n" doesn't move the cursor */ |
| 4902 | if (do_count) |
| 4903 | curwin->w_cursor = old_cursor; |
| 4904 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4905 | if (sub_nsubs) |
| 4906 | { |
| 4907 | /* Set the '[ and '] marks. */ |
| 4908 | curbuf->b_op_start.lnum = eap->line1; |
| 4909 | curbuf->b_op_end.lnum = line2; |
| 4910 | curbuf->b_op_start.col = curbuf->b_op_end.col = 0; |
| 4911 | |
| 4912 | if (!global_busy) |
| 4913 | { |
Bram Moolenaar | b11bd7e | 2005-02-07 22:05:52 +0000 | [diff] [blame] | 4914 | if (endcolumn) |
| 4915 | coladvance((colnr_T)MAXCOL); |
| 4916 | else |
| 4917 | beginline(BL_WHITE | BL_FIX); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4918 | if (!do_sub_msg(do_count) && do_ask) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | MSG(""); |
| 4920 | } |
| 4921 | else |
| 4922 | global_need_beginline = TRUE; |
| 4923 | if (do_print) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4924 | print_line(curwin->w_cursor.lnum, do_number, do_list); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4925 | } |
| 4926 | else if (!global_busy) |
| 4927 | { |
| 4928 | if (got_int) /* interrupted */ |
| 4929 | EMSG(_(e_interr)); |
| 4930 | else if (got_match) /* did find something but nothing substituted */ |
| 4931 | MSG(""); |
| 4932 | else if (do_error) /* nothing found */ |
| 4933 | EMSG2(_(e_patnotf2), get_search_pat()); |
| 4934 | } |
| 4935 | |
| 4936 | vim_free(regmatch.regprog); |
| 4937 | } |
| 4938 | |
| 4939 | /* |
| 4940 | * Give message for number of substitutions. |
| 4941 | * Can also be used after a ":global" command. |
| 4942 | * Return TRUE if a message was given. |
| 4943 | */ |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 4944 | int |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4945 | do_sub_msg(count_only) |
| 4946 | int count_only; /* used 'n' flag for ":s" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4947 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4948 | int len = 0; |
| 4949 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4950 | /* |
| 4951 | * Only report substitutions when: |
| 4952 | * - more than 'report' substitutions |
| 4953 | * - command was typed by user, or number of changed lines > 'report' |
| 4954 | * - giving messages is not disabled by 'lazyredraw' |
| 4955 | */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4956 | if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1)) |
| 4957 | || count_only) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4958 | && messaging()) |
| 4959 | { |
| 4960 | if (got_int) |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4961 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4962 | STRCPY(msg_buf, _("(Interrupted) ")); |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4963 | len = STRLEN(msg_buf); |
| 4964 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4965 | if (sub_nsubs == 1) |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4966 | vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len, |
| 4967 | "%s", count_only ? _("1 match") : _("1 substitution")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4968 | else |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4969 | vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len, |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4970 | count_only ? _("%ld matches") : _("%ld substitutions"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4971 | sub_nsubs); |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4972 | len = STRLEN(msg_buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4973 | if (sub_nlines == 1) |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4974 | vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len, |
| 4975 | "%s", _(" on 1 line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4976 | else |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4977 | vim_snprintf((char *)msg_buf + len, sizeof(msg_buf) - len, |
| 4978 | _(" on %ld lines"), (long)sub_nlines); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4979 | if (msg(msg_buf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4980 | /* save message to display it after redraw */ |
Bram Moolenaar | 238a564 | 2006-02-21 22:12:05 +0000 | [diff] [blame] | 4981 | set_keep_msg(msg_buf, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4982 | return TRUE; |
| 4983 | } |
| 4984 | if (got_int) |
| 4985 | { |
| 4986 | EMSG(_(e_interr)); |
| 4987 | return TRUE; |
| 4988 | } |
| 4989 | return FALSE; |
| 4990 | } |
| 4991 | |
| 4992 | /* |
| 4993 | * Execute a global command of the form: |
| 4994 | * |
| 4995 | * g/pattern/X : execute X on all lines where pattern matches |
| 4996 | * v/pattern/X : execute X on all lines where pattern does not match |
| 4997 | * |
| 4998 | * where 'X' is an EX command |
| 4999 | * |
| 5000 | * The command character (as well as the trailing slash) is optional, and |
| 5001 | * is assumed to be 'p' if missing. |
| 5002 | * |
| 5003 | * This is implemented in two passes: first we scan the file for the pattern and |
| 5004 | * set a mark for each line that (not) matches. secondly we execute the command |
| 5005 | * for each line that has a mark. This is required because after deleting |
| 5006 | * lines we do not know where to search for the next match. |
| 5007 | */ |
| 5008 | void |
| 5009 | ex_global(eap) |
| 5010 | exarg_T *eap; |
| 5011 | { |
| 5012 | linenr_T lnum; /* line number according to old situation */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 5013 | int ndone = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5014 | int type; /* first char of cmd: 'v' or 'g' */ |
| 5015 | char_u *cmd; /* command argument */ |
| 5016 | |
| 5017 | char_u delim; /* delimiter, normally '/' */ |
| 5018 | char_u *pat; |
| 5019 | regmmatch_T regmatch; |
| 5020 | int match; |
| 5021 | int which_pat; |
| 5022 | |
| 5023 | if (global_busy) |
| 5024 | { |
| 5025 | EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */ |
| 5026 | return; |
| 5027 | } |
| 5028 | |
| 5029 | if (eap->forceit) /* ":global!" is like ":vglobal" */ |
| 5030 | type = 'v'; |
| 5031 | else |
| 5032 | type = *eap->cmd; |
| 5033 | cmd = eap->arg; |
| 5034 | which_pat = RE_LAST; /* default: use last used regexp */ |
| 5035 | sub_nsubs = 0; |
| 5036 | sub_nlines = 0; |
| 5037 | |
| 5038 | /* |
| 5039 | * undocumented vi feature: |
| 5040 | * "\/" and "\?": use previous search pattern. |
| 5041 | * "\&": use previous substitute pattern. |
| 5042 | */ |
| 5043 | if (*cmd == '\\') |
| 5044 | { |
| 5045 | ++cmd; |
| 5046 | if (vim_strchr((char_u *)"/?&", *cmd) == NULL) |
| 5047 | { |
| 5048 | EMSG(_(e_backslash)); |
| 5049 | return; |
| 5050 | } |
| 5051 | if (*cmd == '&') |
| 5052 | which_pat = RE_SUBST; /* use previous substitute pattern */ |
| 5053 | else |
| 5054 | which_pat = RE_SEARCH; /* use previous search pattern */ |
| 5055 | ++cmd; |
| 5056 | pat = (char_u *)""; |
| 5057 | } |
| 5058 | else if (*cmd == NUL) |
| 5059 | { |
| 5060 | EMSG(_("E148: Regular expression missing from global")); |
| 5061 | return; |
| 5062 | } |
| 5063 | else |
| 5064 | { |
| 5065 | delim = *cmd; /* get the delimiter */ |
| 5066 | if (delim) |
| 5067 | ++cmd; /* skip delimiter if there is one */ |
| 5068 | pat = cmd; /* remember start of pattern */ |
| 5069 | cmd = skip_regexp(cmd, delim, p_magic, &eap->arg); |
| 5070 | if (cmd[0] == delim) /* end delimiter found */ |
| 5071 | *cmd++ = NUL; /* replace it with a NUL */ |
| 5072 | } |
| 5073 | |
| 5074 | #ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */ |
| 5075 | if (p_altkeymap && curwin->w_p_rl) |
| 5076 | lrFswap(pat,0); |
| 5077 | #endif |
| 5078 | |
| 5079 | if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, ®match) == FAIL) |
| 5080 | { |
| 5081 | EMSG(_(e_invcmd)); |
| 5082 | return; |
| 5083 | } |
| 5084 | |
| 5085 | /* |
| 5086 | * pass 1: set marks for each (not) matching line |
| 5087 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5088 | for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum) |
| 5089 | { |
| 5090 | /* a match on this line? */ |
| 5091 | match = vim_regexec_multi(®match, curwin, curbuf, lnum, (colnr_T)0); |
| 5092 | if ((type == 'g' && match) || (type == 'v' && !match)) |
| 5093 | { |
| 5094 | ml_setmarked(lnum); |
| 5095 | ndone++; |
| 5096 | } |
| 5097 | line_breakcheck(); |
| 5098 | } |
| 5099 | |
| 5100 | /* |
| 5101 | * pass 2: execute the command for each line that has been marked |
| 5102 | */ |
| 5103 | if (got_int) |
| 5104 | MSG(_(e_interr)); |
| 5105 | else if (ndone == 0) |
| 5106 | { |
| 5107 | if (type == 'v') |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5108 | smsg((char_u *)_("Pattern found in every line: %s"), pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5109 | else |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5110 | smsg((char_u *)_(e_patnotf2), pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5111 | } |
| 5112 | else |
| 5113 | global_exe(cmd); |
| 5114 | |
| 5115 | ml_clearmarked(); /* clear rest of the marks */ |
| 5116 | vim_free(regmatch.regprog); |
| 5117 | } |
| 5118 | |
| 5119 | /* |
| 5120 | * Execute "cmd" on lines marked with ml_setmarked(). |
| 5121 | */ |
| 5122 | void |
| 5123 | global_exe(cmd) |
| 5124 | char_u *cmd; |
| 5125 | { |
| 5126 | linenr_T old_lcount; /* b_ml.ml_line_count before the command */ |
| 5127 | linenr_T lnum; /* line number according to old situation */ |
| 5128 | |
| 5129 | /* |
| 5130 | * Set current position only once for a global command. |
| 5131 | * If global_busy is set, setpcmark() will not do anything. |
| 5132 | * If there is an error, global_busy will be incremented. |
| 5133 | */ |
| 5134 | setpcmark(); |
| 5135 | |
| 5136 | /* When the command writes a message, don't overwrite the command. */ |
| 5137 | msg_didout = TRUE; |
| 5138 | |
| 5139 | global_need_beginline = FALSE; |
| 5140 | global_busy = 1; |
| 5141 | old_lcount = curbuf->b_ml.ml_line_count; |
| 5142 | while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1) |
| 5143 | { |
| 5144 | curwin->w_cursor.lnum = lnum; |
| 5145 | curwin->w_cursor.col = 0; |
| 5146 | if (*cmd == NUL || *cmd == '\n') |
| 5147 | do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT); |
| 5148 | else |
| 5149 | do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT); |
| 5150 | ui_breakcheck(); |
| 5151 | } |
| 5152 | |
| 5153 | global_busy = 0; |
| 5154 | if (global_need_beginline) |
| 5155 | beginline(BL_WHITE | BL_FIX); |
| 5156 | else |
| 5157 | check_cursor(); /* cursor may be beyond the end of the line */ |
| 5158 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5159 | /* the cursor may not have moved in the text but a change in a previous |
| 5160 | * line may move it on the screen */ |
| 5161 | changed_line_abv_curs(); |
| 5162 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5163 | /* If it looks like no message was written, allow overwriting the |
| 5164 | * command with the report for number of changes. */ |
| 5165 | if (msg_col == 0 && msg_scrolled == 0) |
| 5166 | msg_didout = FALSE; |
| 5167 | |
| 5168 | /* If subsitutes done, report number of substitues, otherwise report |
| 5169 | * number of extra or deleted lines. */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 5170 | if (!do_sub_msg(FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5171 | msgmore(curbuf->b_ml.ml_line_count - old_lcount); |
| 5172 | } |
| 5173 | |
| 5174 | #ifdef FEAT_VIMINFO |
| 5175 | int |
| 5176 | read_viminfo_sub_string(virp, force) |
| 5177 | vir_T *virp; |
| 5178 | int force; |
| 5179 | { |
| 5180 | if (old_sub != NULL && force) |
| 5181 | vim_free(old_sub); |
| 5182 | if (force || old_sub == NULL) |
| 5183 | old_sub = viminfo_readstring(virp, 1, TRUE); |
| 5184 | return viminfo_readline(virp); |
| 5185 | } |
| 5186 | |
| 5187 | void |
| 5188 | write_viminfo_sub_string(fp) |
| 5189 | FILE *fp; |
| 5190 | { |
| 5191 | if (get_viminfo_parameter('/') != 0 && old_sub != NULL) |
| 5192 | { |
| 5193 | fprintf(fp, _("\n# Last Substitute String:\n$")); |
| 5194 | viminfo_writestring(fp, old_sub); |
| 5195 | } |
| 5196 | } |
| 5197 | #endif /* FEAT_VIMINFO */ |
| 5198 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 5199 | #if defined(EXITFREE) || defined(PROTO) |
| 5200 | void |
| 5201 | free_old_sub() |
| 5202 | { |
| 5203 | vim_free(old_sub); |
| 5204 | } |
| 5205 | #endif |
| 5206 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5207 | #if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO) |
| 5208 | /* |
| 5209 | * Set up for a tagpreview. |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5210 | * Return TRUE when it was created. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5211 | */ |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5212 | int |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 5213 | prepare_tagpreview(undo_sync) |
| 5214 | int undo_sync; /* sync undo when leaving the window */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5215 | { |
| 5216 | win_T *wp; |
| 5217 | |
| 5218 | # ifdef FEAT_GUI |
| 5219 | need_mouse_correct = TRUE; |
| 5220 | # endif |
| 5221 | |
| 5222 | /* |
| 5223 | * If there is already a preview window open, use that one. |
| 5224 | */ |
| 5225 | if (!curwin->w_p_pvw) |
| 5226 | { |
| 5227 | for (wp = firstwin; wp != NULL; wp = wp->w_next) |
| 5228 | if (wp->w_p_pvw) |
| 5229 | break; |
| 5230 | if (wp != NULL) |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 5231 | win_enter(wp, undo_sync); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5232 | else |
| 5233 | { |
| 5234 | /* |
| 5235 | * There is no preview window open yet. Create one. |
| 5236 | */ |
| 5237 | if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0) |
| 5238 | == FAIL) |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5239 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5240 | curwin->w_p_pvw = TRUE; |
| 5241 | curwin->w_p_wfh = TRUE; |
| 5242 | # ifdef FEAT_SCROLLBIND |
| 5243 | curwin->w_p_scb = FALSE; /* don't take over 'scrollbind' */ |
| 5244 | # endif |
| 5245 | # ifdef FEAT_DIFF |
| 5246 | curwin->w_p_diff = FALSE; /* no 'diff' */ |
| 5247 | # endif |
| 5248 | # ifdef FEAT_FOLDING |
| 5249 | curwin->w_p_fdc = 0; /* no 'foldcolumn' */ |
| 5250 | # endif |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5251 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5252 | } |
| 5253 | } |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 5254 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | #endif |
| 5258 | |
| 5259 | |
| 5260 | /* |
| 5261 | * ":help": open a read-only window on a help file |
| 5262 | */ |
| 5263 | void |
| 5264 | ex_help(eap) |
| 5265 | exarg_T *eap; |
| 5266 | { |
| 5267 | char_u *arg; |
| 5268 | char_u *tag; |
| 5269 | FILE *helpfd; /* file descriptor of help file */ |
| 5270 | int n; |
| 5271 | int i; |
| 5272 | #ifdef FEAT_WINDOWS |
| 5273 | win_T *wp; |
| 5274 | #endif |
| 5275 | int num_matches; |
| 5276 | char_u **matches; |
| 5277 | char_u *p; |
| 5278 | int empty_fnum = 0; |
| 5279 | int alt_fnum = 0; |
| 5280 | buf_T *buf; |
| 5281 | #ifdef FEAT_MULTI_LANG |
| 5282 | int len; |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5283 | char_u *lang; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5284 | #endif |
| 5285 | |
| 5286 | if (eap != NULL) |
| 5287 | { |
| 5288 | /* |
| 5289 | * A ":help" command ends at the first LF, or at a '|' that is |
| 5290 | * followed by some text. Set nextcmd to the following command. |
| 5291 | */ |
| 5292 | for (arg = eap->arg; *arg; ++arg) |
| 5293 | { |
| 5294 | if (*arg == '\n' || *arg == '\r' |
| 5295 | || (*arg == '|' && arg[1] != NUL && arg[1] != '|')) |
| 5296 | { |
| 5297 | *arg++ = NUL; |
| 5298 | eap->nextcmd = arg; |
| 5299 | break; |
| 5300 | } |
| 5301 | } |
| 5302 | arg = eap->arg; |
| 5303 | |
| 5304 | if (eap->forceit && *arg == NUL) |
| 5305 | { |
| 5306 | EMSG(_("E478: Don't panic!")); |
| 5307 | return; |
| 5308 | } |
| 5309 | |
| 5310 | if (eap->skip) /* not executing commands */ |
| 5311 | return; |
| 5312 | } |
| 5313 | else |
| 5314 | arg = (char_u *)""; |
| 5315 | |
| 5316 | /* remove trailing blanks */ |
| 5317 | p = arg + STRLEN(arg) - 1; |
| 5318 | while (p > arg && vim_iswhite(*p) && p[-1] != '\\') |
| 5319 | *p-- = NUL; |
| 5320 | |
| 5321 | #ifdef FEAT_MULTI_LANG |
| 5322 | /* Check for a specified language */ |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5323 | lang = check_help_lang(arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5324 | #endif |
| 5325 | |
| 5326 | /* When no argument given go to the index. */ |
| 5327 | if (*arg == NUL) |
| 5328 | arg = (char_u *)"help.txt"; |
| 5329 | |
| 5330 | /* |
| 5331 | * Check if there is a match for the argument. |
| 5332 | */ |
| 5333 | n = find_help_tags(arg, &num_matches, &matches, |
| 5334 | eap != NULL && eap->forceit); |
| 5335 | |
| 5336 | i = 0; |
| 5337 | #ifdef FEAT_MULTI_LANG |
| 5338 | if (n != FAIL && lang != NULL) |
| 5339 | /* Find first item with the requested language. */ |
| 5340 | for (i = 0; i < num_matches; ++i) |
| 5341 | { |
| 5342 | len = STRLEN(matches[i]); |
| 5343 | if (len > 3 && matches[i][len - 3] == '@' |
| 5344 | && STRICMP(matches[i] + len - 2, lang) == 0) |
| 5345 | break; |
| 5346 | } |
| 5347 | #endif |
| 5348 | if (i >= num_matches || n == FAIL) |
| 5349 | { |
| 5350 | #ifdef FEAT_MULTI_LANG |
| 5351 | if (lang != NULL) |
| 5352 | EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg); |
| 5353 | else |
| 5354 | #endif |
| 5355 | EMSG2(_("E149: Sorry, no help for %s"), arg); |
| 5356 | if (n != FAIL) |
| 5357 | FreeWild(num_matches, matches); |
| 5358 | return; |
| 5359 | } |
| 5360 | |
| 5361 | /* The first match (in the requested language) is the best match. */ |
| 5362 | tag = vim_strsave(matches[i]); |
| 5363 | FreeWild(num_matches, matches); |
| 5364 | |
| 5365 | #ifdef FEAT_GUI |
| 5366 | need_mouse_correct = TRUE; |
| 5367 | #endif |
| 5368 | |
| 5369 | /* |
| 5370 | * Re-use an existing help window or open a new one. |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5371 | * Always open a new one for ":tab help". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5372 | */ |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5373 | if (!curwin->w_buffer->b_help |
| 5374 | #ifdef FEAT_WINDOWS |
| 5375 | || cmdmod.tab != 0 |
| 5376 | #endif |
| 5377 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5378 | { |
| 5379 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5380 | if (cmdmod.tab != 0) |
| 5381 | wp = NULL; |
| 5382 | else |
| 5383 | for (wp = firstwin; wp != NULL; wp = wp->w_next) |
| 5384 | if (wp->w_buffer != NULL && wp->w_buffer->b_help) |
| 5385 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5386 | if (wp != NULL && wp->w_buffer->b_nwindows > 0) |
| 5387 | win_enter(wp, TRUE); |
| 5388 | else |
| 5389 | #endif |
| 5390 | { |
| 5391 | /* |
| 5392 | * There is no help window yet. |
| 5393 | * Try to open the file specified by the "helpfile" option. |
| 5394 | */ |
| 5395 | if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL) |
| 5396 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5397 | smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5398 | goto erret; |
| 5399 | } |
| 5400 | fclose(helpfd); |
| 5401 | |
| 5402 | #ifdef FEAT_WINDOWS |
| 5403 | /* Split off help window; put it at far top if no position |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5404 | * specified, the current window is vertically split and |
| 5405 | * narrow. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5406 | n = WSP_HELP; |
| 5407 | # ifdef FEAT_VERTSPLIT |
| 5408 | if (cmdmod.split == 0 && curwin->w_width != Columns |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5409 | && curwin->w_width < 80) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5410 | n |= WSP_TOP; |
| 5411 | # endif |
| 5412 | if (win_split(0, n) == FAIL) |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5413 | goto erret; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5414 | #else |
| 5415 | /* use current window */ |
| 5416 | if (!can_abandon(curbuf, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5417 | goto erret; |
Bram Moolenaar | 2a3f7ee | 2006-02-23 21:34:44 +0000 | [diff] [blame] | 5418 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5419 | |
| 5420 | #ifdef FEAT_WINDOWS |
| 5421 | if (curwin->w_height < p_hh) |
| 5422 | win_setheight((int)p_hh); |
| 5423 | #endif |
| 5424 | |
| 5425 | /* |
| 5426 | * Open help file (do_ecmd() will set b_help flag, readfile() will |
| 5427 | * set b_p_ro flag). |
| 5428 | * Set the alternate file to the previously edited file. |
| 5429 | */ |
| 5430 | alt_fnum = curbuf->b_fnum; |
| 5431 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL, |
| 5432 | ECMD_HIDE + ECMD_SET_HELP); |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 5433 | if (!cmdmod.keepalt) |
| 5434 | curwin->w_alt_fnum = alt_fnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5435 | empty_fnum = curbuf->b_fnum; |
| 5436 | } |
| 5437 | } |
| 5438 | |
| 5439 | if (!p_im) |
| 5440 | restart_edit = 0; /* don't want insert mode in help file */ |
| 5441 | |
| 5442 | if (tag != NULL) |
| 5443 | do_tag(tag, DT_HELP, 1, FALSE, TRUE); |
| 5444 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5445 | /* Delete the empty buffer if we're not using it. Careful: autocommands |
| 5446 | * may have jumped to another window, check that the buffer is not in a |
| 5447 | * window. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5448 | if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum) |
| 5449 | { |
| 5450 | buf = buflist_findnr(empty_fnum); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5451 | if (buf != NULL && buf->b_nwindows == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5452 | wipe_buffer(buf, TRUE); |
| 5453 | } |
| 5454 | |
| 5455 | /* keep the previous alternate file */ |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 5456 | if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5457 | curwin->w_alt_fnum = alt_fnum; |
| 5458 | |
| 5459 | erret: |
| 5460 | vim_free(tag); |
| 5461 | } |
| 5462 | |
| 5463 | |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5464 | #if defined(FEAT_MULTI_LANG) || defined(PROTO) |
| 5465 | /* |
| 5466 | * In an argument search for a language specifiers in the form "@xx". |
| 5467 | * Changes the "@" to NUL if found, and returns a pointer to "xx". |
| 5468 | * Returns NULL if not found. |
| 5469 | */ |
| 5470 | char_u * |
| 5471 | check_help_lang(arg) |
| 5472 | char_u *arg; |
| 5473 | { |
| 5474 | int len = STRLEN(arg); |
| 5475 | |
| 5476 | if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2]) |
| 5477 | && ASCII_ISALPHA(arg[len - 1])) |
| 5478 | { |
| 5479 | arg[len - 3] = NUL; /* remove the '@' */ |
| 5480 | return arg + len - 2; |
| 5481 | } |
| 5482 | return NULL; |
| 5483 | } |
| 5484 | #endif |
| 5485 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5486 | /* |
| 5487 | * Return a heuristic indicating how well the given string matches. The |
| 5488 | * smaller the number, the better the match. This is the order of priorities, |
| 5489 | * from best match to worst match: |
| 5490 | * - Match with least alpha-numeric characters is better. |
| 5491 | * - Match with least total characters is better. |
| 5492 | * - Match towards the start is better. |
| 5493 | * - Match starting with "+" is worse (feature instead of command) |
| 5494 | * Assumption is made that the matched_string passed has already been found to |
| 5495 | * match some string for which help is requested. webb. |
| 5496 | */ |
| 5497 | int |
| 5498 | help_heuristic(matched_string, offset, wrong_case) |
| 5499 | char_u *matched_string; |
| 5500 | int offset; /* offset for match */ |
| 5501 | int wrong_case; /* no matching case */ |
| 5502 | { |
| 5503 | int num_letters; |
| 5504 | char_u *p; |
| 5505 | |
| 5506 | num_letters = 0; |
| 5507 | for (p = matched_string; *p; p++) |
| 5508 | if (ASCII_ISALNUM(*p)) |
| 5509 | num_letters++; |
| 5510 | |
| 5511 | /* |
| 5512 | * Multiply the number of letters by 100 to give it a much bigger |
| 5513 | * weighting than the number of characters. |
| 5514 | * If there only is a match while ignoring case, add 5000. |
| 5515 | * If the match starts in the middle of a word, add 10000 to put it |
| 5516 | * somewhere in the last half. |
| 5517 | * If the match is more than 2 chars from the start, multiply by 200 to |
| 5518 | * put it after matches at the start. |
| 5519 | */ |
| 5520 | if (ASCII_ISALNUM(matched_string[offset]) && offset > 0 |
| 5521 | && ASCII_ISALNUM(matched_string[offset - 1])) |
| 5522 | offset += 10000; |
| 5523 | else if (offset > 2) |
| 5524 | offset *= 200; |
| 5525 | if (wrong_case) |
| 5526 | offset += 5000; |
| 5527 | /* Features are less interesting than the subjects themselves, but "+" |
| 5528 | * alone is not a feature. */ |
| 5529 | if (matched_string[0] == '+' && matched_string[1] != NUL) |
| 5530 | offset += 100; |
| 5531 | return (int)(100 * num_letters + STRLEN(matched_string) + offset); |
| 5532 | } |
| 5533 | |
| 5534 | /* |
| 5535 | * Compare functions for qsort() below, that checks the help heuristics number |
| 5536 | * that has been put after the tagname by find_tags(). |
| 5537 | */ |
| 5538 | static int |
| 5539 | #ifdef __BORLANDC__ |
| 5540 | _RTLENTRYF |
| 5541 | #endif |
| 5542 | help_compare(s1, s2) |
| 5543 | const void *s1; |
| 5544 | const void *s2; |
| 5545 | { |
| 5546 | char *p1; |
| 5547 | char *p2; |
| 5548 | |
| 5549 | p1 = *(char **)s1 + strlen(*(char **)s1) + 1; |
| 5550 | p2 = *(char **)s2 + strlen(*(char **)s2) + 1; |
| 5551 | return strcmp(p1, p2); |
| 5552 | } |
| 5553 | |
| 5554 | /* |
| 5555 | * Find all help tags matching "arg", sort them and return in matches[], with |
| 5556 | * the number of matches in num_matches. |
| 5557 | * The matches will be sorted with a "best" match algorithm. |
| 5558 | * When "keep_lang" is TRUE try keeping the language of the current buffer. |
| 5559 | */ |
| 5560 | int |
| 5561 | find_help_tags(arg, num_matches, matches, keep_lang) |
| 5562 | char_u *arg; |
| 5563 | int *num_matches; |
| 5564 | char_u ***matches; |
| 5565 | int keep_lang; |
| 5566 | { |
| 5567 | char_u *s, *d; |
| 5568 | int i; |
| 5569 | static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*", |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5570 | "/*", "/\\*", "\"*", "**", |
| 5571 | "/\\(\\)", |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5572 | "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", |
Bram Moolenaar | f4630b6 | 2005-05-20 21:31:17 +0000 | [diff] [blame] | 5573 | "/\\?", "/\\z(\\)", "\\=", ":s\\=", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5574 | "[count]", "[quotex]", "[range]", |
| 5575 | "[pattern]", "\\|", "\\%$"}; |
| 5576 | static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star", |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5577 | "/star", "/\\\\star", "quotestar", "starstar", |
| 5578 | "/\\\\(\\\\)", |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5579 | "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", |
Bram Moolenaar | f4630b6 | 2005-05-20 21:31:17 +0000 | [diff] [blame] | 5580 | "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5581 | "\\[count]", "\\[quotex]", "\\[range]", |
| 5582 | "\\[pattern]", "\\\\bar", "/\\\\%\\$"}; |
| 5583 | int flags; |
| 5584 | |
| 5585 | d = IObuff; /* assume IObuff is long enough! */ |
| 5586 | |
| 5587 | /* |
| 5588 | * Recognize a few exceptions to the rule. Some strings that contain '*' |
| 5589 | * with "star". Otherwise '*' is recognized as a wildcard. |
| 5590 | */ |
| 5591 | for (i = sizeof(mtable) / sizeof(char *); --i >= 0; ) |
| 5592 | if (STRCMP(arg, mtable[i]) == 0) |
| 5593 | { |
| 5594 | STRCPY(d, rtable[i]); |
| 5595 | break; |
| 5596 | } |
| 5597 | |
| 5598 | if (i < 0) /* no match in table */ |
| 5599 | { |
| 5600 | /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched. |
| 5601 | * Also replace "\%^" and "\%(", they match every tag too. |
| 5602 | * Also "\zs", "\z1", etc. |
| 5603 | * Also "\@<", "\@=", "\@<=", etc. |
| 5604 | * And also "\_$" and "\_^". */ |
| 5605 | if (arg[0] == '\\' |
| 5606 | && ((arg[1] != NUL && arg[2] == NUL) |
| 5607 | || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL |
| 5608 | && arg[2] != NUL))) |
| 5609 | { |
| 5610 | STRCPY(d, "/\\\\"); |
| 5611 | STRCPY(d + 3, arg + 1); |
| 5612 | /* Check for "/\\_$", should be "/\\_\$" */ |
| 5613 | if (d[3] == '_' && d[4] == '$') |
| 5614 | STRCPY(d + 4, "\\$"); |
| 5615 | } |
| 5616 | else |
| 5617 | { |
| 5618 | /* replace "[:...:]" with "\[:...:]"; "[+...]" with "\[++...]" */ |
| 5619 | if (arg[0] == '[' && (arg[1] == ':' |
| 5620 | || (arg[1] == '+' && arg[2] == '+'))) |
| 5621 | *d++ = '\\'; |
| 5622 | |
| 5623 | for (s = arg; *s; ++s) |
| 5624 | { |
| 5625 | /* |
| 5626 | * Replace "|" with "bar" and '"' with "quote" to match the name of |
| 5627 | * the tags for these commands. |
| 5628 | * Replace "*" with ".*" and "?" with "." to match command line |
| 5629 | * completion. |
| 5630 | * Insert a backslash before '~', '$' and '.' to avoid their |
| 5631 | * special meaning. |
| 5632 | */ |
| 5633 | if (d - IObuff > IOSIZE - 10) /* getting too long!? */ |
| 5634 | break; |
| 5635 | switch (*s) |
| 5636 | { |
| 5637 | case '|': STRCPY(d, "bar"); |
| 5638 | d += 3; |
| 5639 | continue; |
| 5640 | case '"': STRCPY(d, "quote"); |
| 5641 | d += 5; |
| 5642 | continue; |
| 5643 | case '*': *d++ = '.'; |
| 5644 | break; |
| 5645 | case '?': *d++ = '.'; |
| 5646 | continue; |
| 5647 | case '$': |
| 5648 | case '.': |
| 5649 | case '~': *d++ = '\\'; |
| 5650 | break; |
| 5651 | } |
| 5652 | |
| 5653 | /* |
| 5654 | * Replace "^x" by "CTRL-X". Don't do this for "^_" to make |
| 5655 | * ":help i_^_CTRL-D" work. |
| 5656 | * Insert '-' before and after "CTRL-X" when applicable. |
| 5657 | */ |
| 5658 | if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1]) |
| 5659 | || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL))) |
| 5660 | { |
| 5661 | if (d > IObuff && d[-1] != '_') |
| 5662 | *d++ = '_'; /* prepend a '_' */ |
| 5663 | STRCPY(d, "CTRL-"); |
| 5664 | d += 5; |
| 5665 | if (*s < ' ') |
| 5666 | { |
| 5667 | #ifdef EBCDIC |
| 5668 | *d++ = CtrlChar(*s); |
| 5669 | #else |
| 5670 | *d++ = *s + '@'; |
| 5671 | #endif |
| 5672 | if (d[-1] == '\\') |
| 5673 | *d++ = '\\'; /* double a backslash */ |
| 5674 | } |
| 5675 | else |
| 5676 | *d++ = *++s; |
| 5677 | if (s[1] != NUL && s[1] != '_') |
| 5678 | *d++ = '_'; /* append a '_' */ |
| 5679 | continue; |
| 5680 | } |
| 5681 | else if (*s == '^') /* "^" or "CTRL-^" or "^_" */ |
| 5682 | *d++ = '\\'; |
| 5683 | |
| 5684 | /* |
| 5685 | * Insert a backslash before a backslash after a slash, for search |
| 5686 | * pattern tags: "/\|" --> "/\\|". |
| 5687 | */ |
| 5688 | else if (s[0] == '\\' && s[1] != '\\' |
| 5689 | && *arg == '/' && s == arg + 1) |
| 5690 | *d++ = '\\'; |
| 5691 | |
| 5692 | /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in |
| 5693 | * "CTRL-\_CTRL-N" */ |
| 5694 | if (STRNICMP(s, "CTRL-\\_", 7) == 0) |
| 5695 | { |
| 5696 | STRCPY(d, "CTRL-\\\\"); |
| 5697 | d += 7; |
| 5698 | s += 6; |
| 5699 | } |
| 5700 | |
| 5701 | *d++ = *s; |
| 5702 | |
| 5703 | /* |
| 5704 | * If tag starts with ', toss everything after a second '. Fixes |
| 5705 | * CTRL-] on 'option'. (would include the trailing '.'). |
| 5706 | */ |
| 5707 | if (*s == '\'' && s > arg && *arg == '\'') |
| 5708 | break; |
| 5709 | } |
| 5710 | *d = NUL; |
| 5711 | } |
| 5712 | } |
| 5713 | |
| 5714 | *matches = (char_u **)""; |
| 5715 | *num_matches = 0; |
| 5716 | flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE; |
| 5717 | if (keep_lang) |
| 5718 | flags |= TAG_KEEP_LANG; |
| 5719 | if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK |
| 5720 | && *num_matches > 0) |
| 5721 | /* Sort the matches found on the heuristic number that is after the |
| 5722 | * tag name. */ |
| 5723 | qsort((void *)*matches, (size_t)*num_matches, |
| 5724 | sizeof(char_u *), help_compare); |
| 5725 | return OK; |
| 5726 | } |
| 5727 | |
| 5728 | /* |
| 5729 | * After reading a help file: May cleanup a help buffer when syntax |
| 5730 | * highlighting is not used. |
| 5731 | */ |
| 5732 | void |
| 5733 | fix_help_buffer() |
| 5734 | { |
| 5735 | linenr_T lnum; |
| 5736 | char_u *line; |
| 5737 | int in_example = FALSE; |
| 5738 | int len; |
| 5739 | char_u *p; |
| 5740 | char_u *rt; |
| 5741 | int mustfree; |
| 5742 | |
| 5743 | /* set filetype to "help". */ |
| 5744 | set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL); |
| 5745 | |
| 5746 | #ifdef FEAT_SYN_HL |
| 5747 | if (!syntax_present(curbuf)) |
| 5748 | #endif |
| 5749 | { |
| 5750 | for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) |
| 5751 | { |
| 5752 | line = ml_get_buf(curbuf, lnum, FALSE); |
| 5753 | len = (int)STRLEN(line); |
| 5754 | if (in_example && len > 0 && !vim_iswhite(line[0])) |
| 5755 | { |
| 5756 | /* End of example: non-white or '<' in first column. */ |
| 5757 | if (line[0] == '<') |
| 5758 | { |
| 5759 | /* blank-out a '<' in the first column */ |
| 5760 | line = ml_get_buf(curbuf, lnum, TRUE); |
| 5761 | line[0] = ' '; |
| 5762 | } |
| 5763 | in_example = FALSE; |
| 5764 | } |
| 5765 | if (!in_example && len > 0) |
| 5766 | { |
| 5767 | if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' ')) |
| 5768 | { |
| 5769 | /* blank-out a '>' in the last column (start of example) */ |
| 5770 | line = ml_get_buf(curbuf, lnum, TRUE); |
| 5771 | line[len - 1] = ' '; |
| 5772 | in_example = TRUE; |
| 5773 | } |
| 5774 | else if (line[len - 1] == '~') |
| 5775 | { |
| 5776 | /* blank-out a '~' at the end of line (header marker) */ |
| 5777 | line = ml_get_buf(curbuf, lnum, TRUE); |
| 5778 | line[len - 1] = ' '; |
| 5779 | } |
| 5780 | } |
| 5781 | } |
| 5782 | } |
| 5783 | |
| 5784 | /* |
| 5785 | * In the "help.txt" file, add the locally added help files. |
| 5786 | * This uses the very first line in the help file. |
| 5787 | */ |
| 5788 | if (fnamecmp(gettail(curbuf->b_fname), "help.txt") == 0) |
| 5789 | { |
| 5790 | for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum) |
| 5791 | { |
| 5792 | line = ml_get_buf(curbuf, lnum, FALSE); |
| 5793 | if (strstr((char *)line, "*local-additions*") != NULL) |
| 5794 | { |
| 5795 | /* Go through all directories in 'runtimepath', skipping |
| 5796 | * $VIMRUNTIME. */ |
| 5797 | p = p_rtp; |
| 5798 | while (*p != NUL) |
| 5799 | { |
| 5800 | copy_option_part(&p, NameBuff, MAXPATHL, ","); |
| 5801 | mustfree = FALSE; |
| 5802 | rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree); |
| 5803 | if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME) |
| 5804 | { |
| 5805 | int fcount; |
| 5806 | char_u **fnames; |
| 5807 | FILE *fd; |
| 5808 | char_u *s; |
| 5809 | int fi; |
| 5810 | #ifdef FEAT_MBYTE |
| 5811 | vimconv_T vc; |
| 5812 | char_u *cp; |
| 5813 | #endif |
| 5814 | |
| 5815 | /* Find all "doc/ *.txt" files in this directory. */ |
| 5816 | add_pathsep(NameBuff); |
| 5817 | STRCAT(NameBuff, "doc/*.txt"); |
| 5818 | if (gen_expand_wildcards(1, &NameBuff, &fcount, |
| 5819 | &fnames, EW_FILE|EW_SILENT) == OK |
| 5820 | && fcount > 0) |
| 5821 | { |
| 5822 | for (fi = 0; fi < fcount; ++fi) |
| 5823 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5824 | fd = mch_fopen((char *)fnames[fi], "r"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5825 | if (fd != NULL) |
| 5826 | { |
| 5827 | vim_fgets(IObuff, IOSIZE, fd); |
| 5828 | if (IObuff[0] == '*' |
| 5829 | && (s = vim_strchr(IObuff + 1, '*')) |
| 5830 | != NULL) |
| 5831 | { |
| 5832 | #ifdef FEAT_MBYTE |
| 5833 | int this_utf = MAYBE; |
| 5834 | #endif |
| 5835 | /* Change tag definition to a |
| 5836 | * reference and remove <CR>/<NL>. */ |
| 5837 | IObuff[0] = '|'; |
| 5838 | *s = '|'; |
| 5839 | while (*s != NUL) |
| 5840 | { |
| 5841 | if (*s == '\r' || *s == '\n') |
| 5842 | *s = NUL; |
| 5843 | #ifdef FEAT_MBYTE |
| 5844 | /* The text is utf-8 when a byte |
| 5845 | * above 127 is found and no |
| 5846 | * illegal byte sequence is found. |
| 5847 | */ |
| 5848 | if (*s >= 0x80 && this_utf != FALSE) |
| 5849 | { |
| 5850 | int l; |
| 5851 | |
| 5852 | this_utf = TRUE; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5853 | l = utf_ptr2len(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5854 | if (l == 1) |
| 5855 | this_utf = FALSE; |
| 5856 | s += l - 1; |
| 5857 | } |
| 5858 | #endif |
| 5859 | ++s; |
| 5860 | } |
| 5861 | #ifdef FEAT_MBYTE |
| 5862 | /* The help file is latin1 or utf-8; |
| 5863 | * conversion to the current |
| 5864 | * 'encoding' may be required. */ |
| 5865 | vc.vc_type = CONV_NONE; |
| 5866 | convert_setup(&vc, (char_u *)( |
| 5867 | this_utf == TRUE ? "utf-8" |
| 5868 | : "latin1"), p_enc); |
| 5869 | if (vc.vc_type == CONV_NONE) |
| 5870 | /* No conversion needed. */ |
| 5871 | cp = IObuff; |
| 5872 | else |
| 5873 | { |
| 5874 | /* Do the conversion. If it fails |
| 5875 | * use the unconverted text. */ |
| 5876 | cp = string_convert(&vc, IObuff, |
| 5877 | NULL); |
| 5878 | if (cp == NULL) |
| 5879 | cp = IObuff; |
| 5880 | } |
| 5881 | convert_setup(&vc, NULL, NULL); |
| 5882 | |
| 5883 | ml_append(lnum, cp, (colnr_T)0, FALSE); |
| 5884 | if (cp != IObuff) |
| 5885 | vim_free(cp); |
| 5886 | #else |
| 5887 | ml_append(lnum, IObuff, (colnr_T)0, |
| 5888 | FALSE); |
| 5889 | #endif |
| 5890 | ++lnum; |
| 5891 | } |
| 5892 | fclose(fd); |
| 5893 | } |
| 5894 | } |
| 5895 | FreeWild(fcount, fnames); |
| 5896 | } |
| 5897 | } |
| 5898 | if (mustfree) |
| 5899 | vim_free(rt); |
| 5900 | } |
| 5901 | break; |
| 5902 | } |
| 5903 | } |
| 5904 | } |
| 5905 | } |
| 5906 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 5907 | /* |
| 5908 | * ":exusage" |
| 5909 | */ |
| 5910 | /*ARGSUSED*/ |
| 5911 | void |
| 5912 | ex_exusage(eap) |
| 5913 | exarg_T *eap; |
| 5914 | { |
| 5915 | do_cmdline_cmd((char_u *)"help ex-cmd-index"); |
| 5916 | } |
| 5917 | |
| 5918 | /* |
| 5919 | * ":viusage" |
| 5920 | */ |
| 5921 | /*ARGSUSED*/ |
| 5922 | void |
| 5923 | ex_viusage(eap) |
| 5924 | exarg_T *eap; |
| 5925 | { |
| 5926 | do_cmdline_cmd((char_u *)"help normal-index"); |
| 5927 | } |
| 5928 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5929 | #if defined(FEAT_EX_EXTRA) || defined(PROTO) |
| 5930 | static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang)); |
| 5931 | |
| 5932 | /* |
| 5933 | * ":helptags" |
| 5934 | */ |
| 5935 | void |
| 5936 | ex_helptags(eap) |
| 5937 | exarg_T *eap; |
| 5938 | { |
| 5939 | garray_T ga; |
| 5940 | int i, j; |
| 5941 | int len; |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5942 | #ifdef FEAT_MULTI_LANG |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5943 | char_u lang[2]; |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 5944 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5945 | char_u ext[5]; |
| 5946 | char_u fname[8]; |
| 5947 | int filecount; |
| 5948 | char_u **files; |
| 5949 | |
| 5950 | if (!mch_isdir(eap->arg)) |
| 5951 | { |
| 5952 | EMSG2(_("E150: Not a directory: %s"), eap->arg); |
| 5953 | return; |
| 5954 | } |
| 5955 | |
| 5956 | #ifdef FEAT_MULTI_LANG |
| 5957 | /* Get a list of all files in the directory. */ |
| 5958 | STRCPY(NameBuff, eap->arg); |
| 5959 | add_pathsep(NameBuff); |
| 5960 | STRCAT(NameBuff, "*"); |
| 5961 | if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, |
| 5962 | EW_FILE|EW_SILENT) == FAIL |
| 5963 | || filecount == 0) |
| 5964 | { |
| 5965 | EMSG2("E151: No match: %s", NameBuff); |
| 5966 | return; |
| 5967 | } |
| 5968 | |
| 5969 | /* Go over all files in the directory to find out what languages are |
| 5970 | * present. */ |
| 5971 | ga_init2(&ga, 1, 10); |
| 5972 | for (i = 0; i < filecount; ++i) |
| 5973 | { |
| 5974 | len = STRLEN(files[i]); |
| 5975 | if (len > 4) |
| 5976 | { |
| 5977 | if (STRICMP(files[i] + len - 4, ".txt") == 0) |
| 5978 | { |
| 5979 | /* ".txt" -> language "en" */ |
| 5980 | lang[0] = 'e'; |
| 5981 | lang[1] = 'n'; |
| 5982 | } |
| 5983 | else if (files[i][len - 4] == '.' |
| 5984 | && ASCII_ISALPHA(files[i][len - 3]) |
| 5985 | && ASCII_ISALPHA(files[i][len - 2]) |
| 5986 | && TOLOWER_ASC(files[i][len - 1]) == 'x') |
| 5987 | { |
| 5988 | /* ".abx" -> language "ab" */ |
| 5989 | lang[0] = TOLOWER_ASC(files[i][len - 3]); |
| 5990 | lang[1] = TOLOWER_ASC(files[i][len - 2]); |
| 5991 | } |
| 5992 | else |
| 5993 | continue; |
| 5994 | |
| 5995 | /* Did we find this language already? */ |
| 5996 | for (j = 0; j < ga.ga_len; j += 2) |
| 5997 | if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0) |
| 5998 | break; |
| 5999 | if (j == ga.ga_len) |
| 6000 | { |
| 6001 | /* New language, add it. */ |
| 6002 | if (ga_grow(&ga, 2) == FAIL) |
| 6003 | break; |
| 6004 | ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0]; |
| 6005 | ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6006 | } |
| 6007 | } |
| 6008 | } |
| 6009 | |
| 6010 | /* |
| 6011 | * Loop over the found languages to generate a tags file for each one. |
| 6012 | */ |
| 6013 | for (j = 0; j < ga.ga_len; j += 2) |
| 6014 | { |
| 6015 | STRCPY(fname, "tags-xx"); |
| 6016 | fname[5] = ((char_u *)ga.ga_data)[j]; |
| 6017 | fname[6] = ((char_u *)ga.ga_data)[j + 1]; |
| 6018 | if (fname[5] == 'e' && fname[6] == 'n') |
| 6019 | { |
| 6020 | /* English is an exception: use ".txt" and "tags". */ |
| 6021 | fname[4] = NUL; |
| 6022 | STRCPY(ext, ".txt"); |
| 6023 | } |
| 6024 | else |
| 6025 | { |
| 6026 | /* Language "ab" uses ".abx" and "tags-ab". */ |
| 6027 | STRCPY(ext, ".xxx"); |
| 6028 | ext[1] = fname[5]; |
| 6029 | ext[2] = fname[6]; |
| 6030 | } |
| 6031 | helptags_one(eap->arg, ext, fname); |
| 6032 | } |
| 6033 | |
| 6034 | ga_clear(&ga); |
| 6035 | FreeWild(filecount, files); |
| 6036 | |
| 6037 | #else |
| 6038 | /* No language support, just use "*.txt" and "tags". */ |
| 6039 | helptags_one(eap->arg, (char_u *)".txt", (char_u *)"tags"); |
| 6040 | #endif |
| 6041 | } |
| 6042 | |
| 6043 | static void |
| 6044 | helptags_one(dir, ext, tagfname) |
| 6045 | char_u *dir; /* doc directory */ |
| 6046 | char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */ |
| 6047 | char_u *tagfname; /* "tags" for English, "tags-it" for Italian. */ |
| 6048 | { |
| 6049 | FILE *fd_tags; |
| 6050 | FILE *fd; |
| 6051 | garray_T ga; |
| 6052 | int filecount; |
| 6053 | char_u **files; |
| 6054 | char_u *p1, *p2; |
| 6055 | int fi; |
| 6056 | char_u *s; |
| 6057 | int i; |
| 6058 | char_u *fname; |
| 6059 | # ifdef FEAT_MBYTE |
| 6060 | int utf8 = MAYBE; |
| 6061 | int this_utf8; |
| 6062 | int firstline; |
| 6063 | int mix = FALSE; /* detected mixed encodings */ |
| 6064 | # endif |
| 6065 | |
| 6066 | /* |
| 6067 | * Find all *.txt files. |
| 6068 | */ |
| 6069 | STRCPY(NameBuff, dir); |
| 6070 | add_pathsep(NameBuff); |
| 6071 | STRCAT(NameBuff, "*"); |
| 6072 | STRCAT(NameBuff, ext); |
| 6073 | if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, |
| 6074 | EW_FILE|EW_SILENT) == FAIL |
| 6075 | || filecount == 0) |
| 6076 | { |
| 6077 | if (!got_int) |
| 6078 | EMSG2("E151: No match: %s", NameBuff); |
| 6079 | return; |
| 6080 | } |
| 6081 | |
| 6082 | /* |
| 6083 | * Open the tags file for writing. |
| 6084 | * Do this before scanning through all the files. |
| 6085 | */ |
| 6086 | STRCPY(NameBuff, dir); |
| 6087 | add_pathsep(NameBuff); |
| 6088 | STRCAT(NameBuff, tagfname); |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 6089 | fd_tags = mch_fopen((char *)NameBuff, "w"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6090 | if (fd_tags == NULL) |
| 6091 | { |
| 6092 | EMSG2(_("E152: Cannot open %s for writing"), NameBuff); |
| 6093 | FreeWild(filecount, files); |
| 6094 | return; |
| 6095 | } |
| 6096 | |
| 6097 | /* |
| 6098 | * If generating tags for "$VIMRUNTIME/doc" add the "help-tags" tag. |
| 6099 | */ |
| 6100 | ga_init2(&ga, (int)sizeof(char_u *), 100); |
| 6101 | if (fullpathcmp((char_u *)"$VIMRUNTIME/doc", dir, FALSE) == FPC_SAME) |
| 6102 | { |
| 6103 | if (ga_grow(&ga, 1) == FAIL) |
| 6104 | got_int = TRUE; |
| 6105 | else |
| 6106 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 6107 | s = alloc(18 + STRLEN(tagfname)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6108 | if (s == NULL) |
| 6109 | got_int = TRUE; |
| 6110 | else |
| 6111 | { |
| 6112 | sprintf((char *)s, "help-tags\t%s\t1\n", tagfname); |
| 6113 | ((char_u **)ga.ga_data)[ga.ga_len] = s; |
| 6114 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6115 | } |
| 6116 | } |
| 6117 | } |
| 6118 | |
| 6119 | /* |
| 6120 | * Go over all the files and extract the tags. |
| 6121 | */ |
| 6122 | for (fi = 0; fi < filecount && !got_int; ++fi) |
| 6123 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 6124 | fd = mch_fopen((char *)files[fi], "r"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6125 | if (fd == NULL) |
| 6126 | { |
| 6127 | EMSG2(_("E153: Unable to open %s for reading"), files[fi]); |
| 6128 | continue; |
| 6129 | } |
| 6130 | fname = gettail(files[fi]); |
| 6131 | |
| 6132 | # ifdef FEAT_MBYTE |
| 6133 | firstline = TRUE; |
| 6134 | # endif |
| 6135 | while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) |
| 6136 | { |
| 6137 | # ifdef FEAT_MBYTE |
| 6138 | if (firstline) |
| 6139 | { |
| 6140 | /* Detect utf-8 file by a non-ASCII char in the first line. */ |
| 6141 | this_utf8 = MAYBE; |
| 6142 | for (s = IObuff; *s != NUL; ++s) |
| 6143 | if (*s >= 0x80) |
| 6144 | { |
| 6145 | int l; |
| 6146 | |
| 6147 | this_utf8 = TRUE; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6148 | l = utf_ptr2len(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6149 | if (l == 1) |
| 6150 | { |
| 6151 | /* Illegal UTF-8 byte sequence. */ |
| 6152 | this_utf8 = FALSE; |
| 6153 | break; |
| 6154 | } |
| 6155 | s += l - 1; |
| 6156 | } |
| 6157 | if (this_utf8 == MAYBE) /* only ASCII characters found */ |
| 6158 | this_utf8 = FALSE; |
| 6159 | if (utf8 == MAYBE) /* first file */ |
| 6160 | utf8 = this_utf8; |
| 6161 | else if (utf8 != this_utf8) |
| 6162 | { |
| 6163 | EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]); |
| 6164 | mix = !got_int; |
| 6165 | got_int = TRUE; |
| 6166 | } |
| 6167 | firstline = FALSE; |
| 6168 | } |
| 6169 | # endif |
| 6170 | p1 = vim_strchr(IObuff, '*'); /* find first '*' */ |
| 6171 | while (p1 != NULL) |
| 6172 | { |
| 6173 | p2 = vim_strchr(p1 + 1, '*'); /* find second '*' */ |
| 6174 | if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */ |
| 6175 | { |
| 6176 | for (s = p1 + 1; s < p2; ++s) |
| 6177 | if (*s == ' ' || *s == '\t' || *s == '|') |
| 6178 | break; |
| 6179 | |
| 6180 | /* |
| 6181 | * Only accept a *tag* when it consists of valid |
| 6182 | * characters, there is white space before it and is |
| 6183 | * followed by a white character or end-of-line. |
| 6184 | */ |
| 6185 | if (s == p2 |
| 6186 | && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t') |
| 6187 | && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL |
| 6188 | || s[1] == '\0')) |
| 6189 | { |
| 6190 | *p2 = '\0'; |
| 6191 | ++p1; |
| 6192 | if (ga_grow(&ga, 1) == FAIL) |
| 6193 | { |
| 6194 | got_int = TRUE; |
| 6195 | break; |
| 6196 | } |
| 6197 | s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2)); |
| 6198 | if (s == NULL) |
| 6199 | { |
| 6200 | got_int = TRUE; |
| 6201 | break; |
| 6202 | } |
| 6203 | ((char_u **)ga.ga_data)[ga.ga_len] = s; |
| 6204 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6205 | sprintf((char *)s, "%s\t%s", p1, fname); |
| 6206 | |
| 6207 | /* find next '*' */ |
| 6208 | p2 = vim_strchr(p2 + 1, '*'); |
| 6209 | } |
| 6210 | } |
| 6211 | p1 = p2; |
| 6212 | } |
| 6213 | line_breakcheck(); |
| 6214 | } |
| 6215 | |
| 6216 | fclose(fd); |
| 6217 | } |
| 6218 | |
| 6219 | FreeWild(filecount, files); |
| 6220 | |
| 6221 | if (!got_int) |
| 6222 | { |
| 6223 | /* |
| 6224 | * Sort the tags. |
| 6225 | */ |
| 6226 | sort_strings((char_u **)ga.ga_data, ga.ga_len); |
| 6227 | |
| 6228 | /* |
| 6229 | * Check for duplicates. |
| 6230 | */ |
| 6231 | for (i = 1; i < ga.ga_len; ++i) |
| 6232 | { |
| 6233 | p1 = ((char_u **)ga.ga_data)[i - 1]; |
| 6234 | p2 = ((char_u **)ga.ga_data)[i]; |
| 6235 | while (*p1 == *p2) |
| 6236 | { |
| 6237 | if (*p2 == '\t') |
| 6238 | { |
| 6239 | *p2 = NUL; |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 6240 | vim_snprintf((char *)NameBuff, MAXPATHL, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6241 | _("E154: Duplicate tag \"%s\" in file %s/%s"), |
| 6242 | ((char_u **)ga.ga_data)[i], dir, p2 + 1); |
| 6243 | EMSG(NameBuff); |
| 6244 | *p2 = '\t'; |
| 6245 | break; |
| 6246 | } |
| 6247 | ++p1; |
| 6248 | ++p2; |
| 6249 | } |
| 6250 | } |
| 6251 | |
| 6252 | # ifdef FEAT_MBYTE |
| 6253 | if (utf8 == TRUE) |
| 6254 | fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n"); |
| 6255 | # endif |
| 6256 | |
| 6257 | /* |
| 6258 | * Write the tags into the file. |
| 6259 | */ |
| 6260 | for (i = 0; i < ga.ga_len; ++i) |
| 6261 | { |
| 6262 | s = ((char_u **)ga.ga_data)[i]; |
| 6263 | if (STRNCMP(s, "help-tags", 9) == 0) |
| 6264 | /* help-tags entry was added in formatted form */ |
| 6265 | fprintf(fd_tags, (char *)s); |
| 6266 | else |
| 6267 | { |
| 6268 | fprintf(fd_tags, "%s\t/*", s); |
| 6269 | for (p1 = s; *p1 != '\t'; ++p1) |
| 6270 | { |
| 6271 | /* insert backslash before '\\' and '/' */ |
| 6272 | if (*p1 == '\\' || *p1 == '/') |
| 6273 | putc('\\', fd_tags); |
| 6274 | putc(*p1, fd_tags); |
| 6275 | } |
| 6276 | fprintf(fd_tags, "*\n"); |
| 6277 | } |
| 6278 | } |
| 6279 | } |
| 6280 | #ifdef FEAT_MBYTE |
| 6281 | if (mix) |
| 6282 | got_int = FALSE; /* continue with other languages */ |
| 6283 | #endif |
| 6284 | |
| 6285 | for (i = 0; i < ga.ga_len; ++i) |
| 6286 | vim_free(((char_u **)ga.ga_data)[i]); |
| 6287 | ga_clear(&ga); |
| 6288 | fclose(fd_tags); /* there is no check for an error... */ |
| 6289 | } |
| 6290 | #endif |
| 6291 | |
| 6292 | #if defined(FEAT_SIGNS) || defined(PROTO) |
| 6293 | |
| 6294 | /* |
| 6295 | * Struct to hold the sign properties. |
| 6296 | */ |
| 6297 | typedef struct sign sign_T; |
| 6298 | |
| 6299 | struct sign |
| 6300 | { |
| 6301 | sign_T *sn_next; /* next sign in list */ |
| 6302 | int sn_typenr; /* type number of sign (negative if not equal |
| 6303 | to name) */ |
| 6304 | char_u *sn_name; /* name of sign */ |
| 6305 | char_u *sn_icon; /* name of pixmap */ |
| 6306 | #ifdef FEAT_SIGN_ICONS |
| 6307 | void *sn_image; /* icon image */ |
| 6308 | #endif |
| 6309 | char_u *sn_text; /* text used instead of pixmap */ |
| 6310 | int sn_line_hl; /* highlight ID for line */ |
| 6311 | int sn_text_hl; /* highlight ID for text */ |
| 6312 | }; |
| 6313 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6314 | static sign_T *first_sign = NULL; |
| 6315 | static int last_sign_typenr = MAX_TYPENR; /* is decremented */ |
| 6316 | |
| 6317 | static void sign_list_defined __ARGS((sign_T *sp)); |
| 6318 | |
| 6319 | /* |
| 6320 | * ":sign" command |
| 6321 | */ |
| 6322 | void |
| 6323 | ex_sign(eap) |
| 6324 | exarg_T *eap; |
| 6325 | { |
| 6326 | char_u *arg = eap->arg; |
| 6327 | char_u *p; |
| 6328 | int idx; |
| 6329 | sign_T *sp; |
| 6330 | sign_T *sp_prev; |
| 6331 | buf_T *buf; |
| 6332 | static char *cmds[] = { |
| 6333 | "define", |
| 6334 | #define SIGNCMD_DEFINE 0 |
| 6335 | "undefine", |
| 6336 | #define SIGNCMD_UNDEFINE 1 |
| 6337 | "list", |
| 6338 | #define SIGNCMD_LIST 2 |
| 6339 | "place", |
| 6340 | #define SIGNCMD_PLACE 3 |
| 6341 | "unplace", |
| 6342 | #define SIGNCMD_UNPLACE 4 |
| 6343 | "jump", |
| 6344 | #define SIGNCMD_JUMP 5 |
| 6345 | #define SIGNCMD_LAST 6 |
| 6346 | }; |
| 6347 | |
| 6348 | /* Parse the subcommand. */ |
| 6349 | p = skiptowhite(arg); |
| 6350 | if (*p != NUL) |
| 6351 | *p++ = NUL; |
| 6352 | for (idx = 0; ; ++idx) |
| 6353 | { |
| 6354 | if (idx == SIGNCMD_LAST) |
| 6355 | { |
| 6356 | EMSG2(_("E160: Unknown sign command: %s"), arg); |
| 6357 | return; |
| 6358 | } |
| 6359 | if (STRCMP(arg, cmds[idx]) == 0) |
| 6360 | break; |
| 6361 | } |
| 6362 | arg = skipwhite(p); |
| 6363 | |
| 6364 | if (idx <= SIGNCMD_LIST) |
| 6365 | { |
| 6366 | /* |
| 6367 | * Define, undefine or list signs. |
| 6368 | */ |
| 6369 | if (idx == SIGNCMD_LIST && *arg == NUL) |
| 6370 | { |
| 6371 | /* ":sign list": list all defined signs */ |
| 6372 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 6373 | sign_list_defined(sp); |
| 6374 | } |
| 6375 | else if (*arg == NUL) |
| 6376 | EMSG(_("E156: Missing sign name")); |
| 6377 | else |
| 6378 | { |
| 6379 | p = skiptowhite(arg); |
| 6380 | if (*p != NUL) |
| 6381 | *p++ = NUL; |
| 6382 | sp_prev = NULL; |
| 6383 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 6384 | { |
| 6385 | if (STRCMP(sp->sn_name, arg) == 0) |
| 6386 | break; |
| 6387 | sp_prev = sp; |
| 6388 | } |
| 6389 | if (idx == SIGNCMD_DEFINE) |
| 6390 | { |
| 6391 | /* ":sign define {name} ...": define a sign */ |
| 6392 | if (sp == NULL) |
| 6393 | { |
| 6394 | /* Allocate a new sign. */ |
| 6395 | sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T)); |
| 6396 | if (sp == NULL) |
| 6397 | return; |
| 6398 | if (sp_prev == NULL) |
| 6399 | first_sign = sp; |
| 6400 | else |
| 6401 | sp_prev->sn_next = sp; |
| 6402 | sp->sn_name = vim_strnsave(arg, (int)(p - arg)); |
| 6403 | |
| 6404 | /* If the name is a number use that for the typenr, |
| 6405 | * otherwise use a negative number. */ |
| 6406 | if (VIM_ISDIGIT(*arg)) |
| 6407 | sp->sn_typenr = atoi((char *)arg); |
| 6408 | else |
| 6409 | { |
| 6410 | sign_T *lp; |
| 6411 | int start = last_sign_typenr; |
| 6412 | |
| 6413 | for (lp = first_sign; lp != NULL; lp = lp->sn_next) |
| 6414 | { |
| 6415 | if (lp->sn_typenr == last_sign_typenr) |
| 6416 | { |
| 6417 | --last_sign_typenr; |
| 6418 | if (last_sign_typenr == 0) |
| 6419 | last_sign_typenr = MAX_TYPENR; |
| 6420 | if (last_sign_typenr == start) |
| 6421 | { |
| 6422 | EMSG(_("E612: Too many signs defined")); |
| 6423 | return; |
| 6424 | } |
| 6425 | lp = first_sign; |
| 6426 | continue; |
| 6427 | } |
| 6428 | } |
| 6429 | |
| 6430 | sp->sn_typenr = last_sign_typenr--; |
| 6431 | if (last_sign_typenr == 0) |
| 6432 | last_sign_typenr = MAX_TYPENR; /* wrap around */ |
| 6433 | } |
| 6434 | } |
| 6435 | |
| 6436 | /* set values for a defined sign. */ |
| 6437 | for (;;) |
| 6438 | { |
| 6439 | arg = skipwhite(p); |
| 6440 | if (*arg == NUL) |
| 6441 | break; |
| 6442 | p = skiptowhite_esc(arg); |
| 6443 | if (STRNCMP(arg, "icon=", 5) == 0) |
| 6444 | { |
| 6445 | arg += 5; |
| 6446 | vim_free(sp->sn_icon); |
| 6447 | sp->sn_icon = vim_strnsave(arg, (int)(p - arg)); |
| 6448 | backslash_halve(sp->sn_icon); |
| 6449 | #ifdef FEAT_SIGN_ICONS |
| 6450 | if (gui.in_use) |
| 6451 | { |
| 6452 | out_flush(); |
| 6453 | if (sp->sn_image != NULL) |
| 6454 | gui_mch_destroy_sign(sp->sn_image); |
| 6455 | sp->sn_image = gui_mch_register_sign(sp->sn_icon); |
| 6456 | } |
| 6457 | #endif |
| 6458 | } |
| 6459 | else if (STRNCMP(arg, "text=", 5) == 0) |
| 6460 | { |
| 6461 | char_u *s; |
| 6462 | int cells; |
| 6463 | int len; |
| 6464 | |
| 6465 | arg += 5; |
| 6466 | #ifdef FEAT_MBYTE |
| 6467 | /* Count cells and check for non-printable chars */ |
| 6468 | if (has_mbyte) |
| 6469 | { |
| 6470 | cells = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6471 | for (s = arg; s < p; s += (*mb_ptr2len)(s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6472 | { |
| 6473 | if (!vim_isprintc((*mb_ptr2char)(s))) |
| 6474 | break; |
| 6475 | cells += (*mb_ptr2cells)(s); |
| 6476 | } |
| 6477 | } |
| 6478 | else |
| 6479 | #endif |
| 6480 | { |
| 6481 | for (s = arg; s < p; ++s) |
| 6482 | if (!vim_isprintc(*s)) |
| 6483 | break; |
| 6484 | cells = s - arg; |
| 6485 | } |
| 6486 | /* Currently must be one or two display cells */ |
| 6487 | if (s != p || cells < 1 || cells > 2) |
| 6488 | { |
| 6489 | *p = NUL; |
| 6490 | EMSG2(_("E239: Invalid sign text: %s"), arg); |
| 6491 | return; |
| 6492 | } |
| 6493 | |
| 6494 | vim_free(sp->sn_text); |
| 6495 | /* Allocate one byte more if we need to pad up |
| 6496 | * with a space. */ |
| 6497 | len = p - arg + ((cells == 1) ? 1 : 0); |
| 6498 | sp->sn_text = vim_strnsave(arg, len); |
| 6499 | |
| 6500 | if (sp->sn_text != NULL && cells == 1) |
| 6501 | STRCPY(sp->sn_text + len - 1, " "); |
| 6502 | } |
| 6503 | else if (STRNCMP(arg, "linehl=", 7) == 0) |
| 6504 | { |
| 6505 | arg += 7; |
| 6506 | sp->sn_line_hl = syn_check_group(arg, (int)(p - arg)); |
| 6507 | } |
| 6508 | else if (STRNCMP(arg, "texthl=", 7) == 0) |
| 6509 | { |
| 6510 | arg += 7; |
| 6511 | sp->sn_text_hl = syn_check_group(arg, (int)(p - arg)); |
| 6512 | } |
| 6513 | else |
| 6514 | { |
| 6515 | EMSG2(_(e_invarg2), arg); |
| 6516 | return; |
| 6517 | } |
| 6518 | } |
| 6519 | } |
| 6520 | else if (sp == NULL) |
| 6521 | EMSG2(_("E155: Unknown sign: %s"), arg); |
| 6522 | else if (idx == SIGNCMD_LIST) |
| 6523 | /* ":sign list {name}" */ |
| 6524 | sign_list_defined(sp); |
| 6525 | else |
| 6526 | { |
| 6527 | /* ":sign undefine {name}" */ |
| 6528 | vim_free(sp->sn_name); |
| 6529 | vim_free(sp->sn_icon); |
| 6530 | #ifdef FEAT_SIGN_ICONS |
| 6531 | if (sp->sn_image != NULL) |
| 6532 | { |
| 6533 | out_flush(); |
| 6534 | gui_mch_destroy_sign(sp->sn_image); |
| 6535 | } |
| 6536 | #endif |
| 6537 | vim_free(sp->sn_text); |
| 6538 | if (sp_prev == NULL) |
| 6539 | first_sign = sp->sn_next; |
| 6540 | else |
| 6541 | sp_prev->sn_next = sp->sn_next; |
| 6542 | vim_free(sp); |
| 6543 | } |
| 6544 | } |
| 6545 | } |
| 6546 | else |
| 6547 | { |
| 6548 | int id = -1; |
| 6549 | linenr_T lnum = -1; |
| 6550 | char_u *sign_name = NULL; |
| 6551 | char_u *arg1; |
| 6552 | |
| 6553 | if (*arg == NUL) |
| 6554 | { |
| 6555 | if (idx == SIGNCMD_PLACE) |
| 6556 | { |
| 6557 | /* ":sign place": list placed signs in all buffers */ |
| 6558 | sign_list_placed(NULL); |
| 6559 | } |
| 6560 | else if (idx == SIGNCMD_UNPLACE) |
| 6561 | { |
| 6562 | /* ":sign unplace": remove placed sign at cursor */ |
| 6563 | id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum); |
| 6564 | if (id > 0) |
| 6565 | { |
| 6566 | buf_delsign(curwin->w_buffer, id); |
| 6567 | update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum); |
| 6568 | } |
| 6569 | else |
| 6570 | EMSG(_("E159: Missing sign number")); |
| 6571 | } |
| 6572 | else |
| 6573 | EMSG(_(e_argreq)); |
| 6574 | return; |
| 6575 | } |
| 6576 | |
| 6577 | if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL) |
| 6578 | { |
| 6579 | /* ":sign unplace *": remove all placed signs */ |
| 6580 | buf_delete_all_signs(); |
| 6581 | return; |
| 6582 | } |
| 6583 | |
| 6584 | /* first arg could be placed sign id */ |
| 6585 | arg1 = arg; |
| 6586 | if (VIM_ISDIGIT(*arg)) |
| 6587 | { |
| 6588 | id = getdigits(&arg); |
| 6589 | if (!vim_iswhite(*arg) && *arg != NUL) |
| 6590 | { |
| 6591 | id = -1; |
| 6592 | arg = arg1; |
| 6593 | } |
| 6594 | else |
| 6595 | { |
| 6596 | arg = skipwhite(arg); |
| 6597 | if (idx == SIGNCMD_UNPLACE && *arg == NUL) |
| 6598 | { |
| 6599 | /* ":sign unplace {id}": remove placed sign by number */ |
| 6600 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 6601 | if ((lnum = buf_delsign(buf, id)) != 0) |
| 6602 | update_debug_sign(buf, lnum); |
| 6603 | return; |
| 6604 | } |
| 6605 | } |
| 6606 | } |
| 6607 | |
| 6608 | /* |
| 6609 | * Check for line={lnum} name={name} and file={fname} or buffer={nr}. |
| 6610 | * Leave "arg" pointing to {fname}. |
| 6611 | */ |
| 6612 | for (;;) |
| 6613 | { |
| 6614 | if (STRNCMP(arg, "line=", 5) == 0) |
| 6615 | { |
| 6616 | arg += 5; |
| 6617 | lnum = atoi((char *)arg); |
| 6618 | arg = skiptowhite(arg); |
| 6619 | } |
| 6620 | else if (STRNCMP(arg, "name=", 5) == 0) |
| 6621 | { |
| 6622 | arg += 5; |
| 6623 | sign_name = arg; |
| 6624 | arg = skiptowhite(arg); |
| 6625 | if (*arg != NUL) |
| 6626 | *arg++ = NUL; |
| 6627 | } |
| 6628 | else if (STRNCMP(arg, "file=", 5) == 0) |
| 6629 | { |
| 6630 | arg += 5; |
| 6631 | buf = buflist_findname(arg); |
| 6632 | break; |
| 6633 | } |
| 6634 | else if (STRNCMP(arg, "buffer=", 7) == 0) |
| 6635 | { |
| 6636 | arg += 7; |
| 6637 | buf = buflist_findnr((int)getdigits(&arg)); |
| 6638 | if (*skipwhite(arg) != NUL) |
| 6639 | EMSG(_(e_trailing)); |
| 6640 | break; |
| 6641 | } |
| 6642 | else |
| 6643 | { |
| 6644 | EMSG(_(e_invarg)); |
| 6645 | return; |
| 6646 | } |
| 6647 | arg = skipwhite(arg); |
| 6648 | } |
| 6649 | |
| 6650 | if (buf == NULL) |
| 6651 | { |
| 6652 | EMSG2(_("E158: Invalid buffer name: %s"), arg); |
| 6653 | } |
| 6654 | else if (id <= 0) |
| 6655 | { |
| 6656 | if (lnum >= 0 || sign_name != NULL) |
| 6657 | EMSG(_(e_invarg)); |
| 6658 | else |
| 6659 | /* ":sign place file={fname}": list placed signs in one file */ |
| 6660 | sign_list_placed(buf); |
| 6661 | } |
| 6662 | else if (idx == SIGNCMD_JUMP) |
| 6663 | { |
| 6664 | /* ":sign jump {id} file={fname}" */ |
| 6665 | if (lnum >= 0 || sign_name != NULL) |
| 6666 | EMSG(_(e_invarg)); |
| 6667 | else if ((lnum = buf_findsign(buf, id)) > 0) |
| 6668 | { /* goto a sign ... */ |
| 6669 | if (buf_jump_open_win(buf) != NULL) |
| 6670 | { /* ... in a current window */ |
| 6671 | curwin->w_cursor.lnum = lnum; |
| 6672 | check_cursor_lnum(); |
| 6673 | beginline(BL_WHITE); |
| 6674 | } |
| 6675 | else |
| 6676 | { /* ... not currently in a window */ |
| 6677 | char_u *cmd; |
| 6678 | |
| 6679 | cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25); |
| 6680 | if (cmd == NULL) |
| 6681 | return; |
| 6682 | sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname); |
| 6683 | do_cmdline_cmd(cmd); |
| 6684 | vim_free(cmd); |
| 6685 | } |
| 6686 | #ifdef FEAT_FOLDING |
| 6687 | foldOpenCursor(); |
| 6688 | #endif |
| 6689 | } |
| 6690 | else |
| 6691 | EMSGN(_("E157: Invalid sign ID: %ld"), id); |
| 6692 | } |
| 6693 | else if (idx == SIGNCMD_UNPLACE) |
| 6694 | { |
| 6695 | /* ":sign unplace {id} file={fname}" */ |
| 6696 | if (lnum >= 0 || sign_name != NULL) |
| 6697 | EMSG(_(e_invarg)); |
| 6698 | else |
| 6699 | { |
| 6700 | lnum = buf_delsign(buf, id); |
| 6701 | update_debug_sign(buf, lnum); |
| 6702 | } |
| 6703 | } |
| 6704 | /* idx == SIGNCMD_PLACE */ |
| 6705 | else if (sign_name != NULL) |
| 6706 | { |
| 6707 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 6708 | if (STRCMP(sp->sn_name, sign_name) == 0) |
| 6709 | break; |
| 6710 | if (sp == NULL) |
| 6711 | { |
| 6712 | EMSG2(_("E155: Unknown sign: %s"), sign_name); |
| 6713 | return; |
| 6714 | } |
| 6715 | if (lnum > 0) |
| 6716 | /* ":sign place {id} line={lnum} name={name} file={fname}": |
| 6717 | * place a sign */ |
| 6718 | buf_addsign(buf, id, lnum, sp->sn_typenr); |
| 6719 | else |
| 6720 | /* ":sign place {id} file={fname}": change sign type */ |
| 6721 | lnum = buf_change_sign_type(buf, id, sp->sn_typenr); |
| 6722 | update_debug_sign(buf, lnum); |
| 6723 | } |
| 6724 | else |
| 6725 | EMSG(_(e_invarg)); |
| 6726 | } |
| 6727 | } |
| 6728 | |
| 6729 | #if defined(FEAT_SIGN_ICONS) || defined(PROTO) |
| 6730 | /* |
| 6731 | * Allocate the icons. Called when the GUI has started. Allows defining |
| 6732 | * signs before it starts. |
| 6733 | */ |
| 6734 | void |
| 6735 | sign_gui_started() |
| 6736 | { |
| 6737 | sign_T *sp; |
| 6738 | |
| 6739 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 6740 | if (sp->sn_icon != NULL) |
| 6741 | sp->sn_image = gui_mch_register_sign(sp->sn_icon); |
| 6742 | } |
| 6743 | #endif |
| 6744 | |
| 6745 | /* |
| 6746 | * List one sign. |
| 6747 | */ |
| 6748 | static void |
| 6749 | sign_list_defined(sp) |
| 6750 | sign_T *sp; |
| 6751 | { |
| 6752 | char_u *p; |
| 6753 | |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 6754 | smsg((char_u *)"sign %s", sp->sn_name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6755 | if (sp->sn_icon != NULL) |
| 6756 | { |
| 6757 | MSG_PUTS(" icon="); |
| 6758 | msg_outtrans(sp->sn_icon); |
| 6759 | #ifdef FEAT_SIGN_ICONS |
| 6760 | if (sp->sn_image == NULL) |
| 6761 | MSG_PUTS(_(" (NOT FOUND)")); |
| 6762 | #else |
| 6763 | MSG_PUTS(_(" (not supported)")); |
| 6764 | #endif |
| 6765 | } |
| 6766 | if (sp->sn_text != NULL) |
| 6767 | { |
| 6768 | MSG_PUTS(" text="); |
| 6769 | msg_outtrans(sp->sn_text); |
| 6770 | } |
| 6771 | if (sp->sn_line_hl > 0) |
| 6772 | { |
| 6773 | MSG_PUTS(" linehl="); |
| 6774 | p = get_highlight_name(NULL, sp->sn_line_hl - 1); |
| 6775 | if (p == NULL) |
| 6776 | MSG_PUTS("NONE"); |
| 6777 | else |
| 6778 | msg_puts(p); |
| 6779 | } |
| 6780 | if (sp->sn_text_hl > 0) |
| 6781 | { |
| 6782 | MSG_PUTS(" texthl="); |
| 6783 | p = get_highlight_name(NULL, sp->sn_text_hl - 1); |
| 6784 | if (p == NULL) |
| 6785 | MSG_PUTS("NONE"); |
| 6786 | else |
| 6787 | msg_puts(p); |
| 6788 | } |
| 6789 | } |
| 6790 | |
| 6791 | /* |
| 6792 | * Get highlighting attribute for sign "typenr". |
| 6793 | * If "line" is TRUE: line highl, if FALSE: text highl. |
| 6794 | */ |
| 6795 | int |
| 6796 | sign_get_attr(typenr, line) |
| 6797 | int typenr; |
| 6798 | int line; |
| 6799 | { |
| 6800 | sign_T *sp; |
| 6801 | |
| 6802 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 6803 | if (sp->sn_typenr == typenr) |
| 6804 | { |
| 6805 | if (line) |
| 6806 | { |
| 6807 | if (sp->sn_line_hl > 0) |
| 6808 | return syn_id2attr(sp->sn_line_hl); |
| 6809 | } |
| 6810 | else |
| 6811 | { |
| 6812 | if (sp->sn_text_hl > 0) |
| 6813 | return syn_id2attr(sp->sn_text_hl); |
| 6814 | } |
| 6815 | break; |
| 6816 | } |
| 6817 | return 0; |
| 6818 | } |
| 6819 | |
| 6820 | /* |
| 6821 | * Get text mark for sign "typenr". |
| 6822 | * Returns NULL if there isn't one. |
| 6823 | */ |
| 6824 | char_u * |
| 6825 | sign_get_text(typenr) |
| 6826 | int typenr; |
| 6827 | { |
| 6828 | sign_T *sp; |
| 6829 | |
| 6830 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 6831 | if (sp->sn_typenr == typenr) |
| 6832 | return sp->sn_text; |
| 6833 | return NULL; |
| 6834 | } |
| 6835 | |
| 6836 | #if defined(FEAT_SIGN_ICONS) || defined(PROTO) |
| 6837 | void * |
| 6838 | sign_get_image(typenr) |
| 6839 | int typenr; /* the attribute which may have a sign */ |
| 6840 | { |
| 6841 | sign_T *sp; |
| 6842 | |
| 6843 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 6844 | if (sp->sn_typenr == typenr) |
| 6845 | return sp->sn_image; |
| 6846 | return NULL; |
| 6847 | } |
| 6848 | #endif |
| 6849 | |
| 6850 | /* |
| 6851 | * Get the name of a sign by its typenr. |
| 6852 | */ |
| 6853 | char_u * |
| 6854 | sign_typenr2name(typenr) |
| 6855 | int typenr; |
| 6856 | { |
| 6857 | sign_T *sp; |
| 6858 | |
| 6859 | for (sp = first_sign; sp != NULL; sp = sp->sn_next) |
| 6860 | if (sp->sn_typenr == typenr) |
| 6861 | return sp->sn_name; |
| 6862 | return (char_u *)_("[Deleted]"); |
| 6863 | } |
| 6864 | |
| 6865 | #endif |
| 6866 | |
| 6867 | #if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO) |
| 6868 | /* |
| 6869 | * ":drop" |
| 6870 | * Opens the first argument in a window. When there are two or more arguments |
| 6871 | * the argument list is redefined. |
| 6872 | */ |
| 6873 | void |
| 6874 | ex_drop(eap) |
| 6875 | exarg_T *eap; |
| 6876 | { |
| 6877 | int split = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6878 | win_T *wp; |
| 6879 | buf_T *buf; |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6880 | # ifdef FEAT_WINDOWS |
| 6881 | tabpage_T *tp; |
| 6882 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6883 | |
| 6884 | /* |
| 6885 | * Check if the first argument is already being edited in a window. If |
| 6886 | * so, jump to that window. |
| 6887 | * We would actually need to check all arguments, but that's complicated |
| 6888 | * and mostly only one file is dropped. |
| 6889 | * This also ignores wildcards, since it is very unlikely the user is |
| 6890 | * editing a file name with a wildcard character. |
| 6891 | */ |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6892 | set_arglist(eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6893 | |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6894 | # ifdef FEAT_WINDOWS |
| 6895 | if (cmdmod.tab) |
| 6896 | { |
| 6897 | /* ":tab drop file ...": open a tab for each argument that isn't |
| 6898 | * edited in a window yet. It's like ":tab all" but without closing |
| 6899 | * windows or tabs. */ |
| 6900 | ex_all(eap); |
| 6901 | } |
| 6902 | else |
| 6903 | # endif |
| 6904 | { |
| 6905 | /* ":drop file ...": Edit the first argument. Jump to an existing |
| 6906 | * window if possible, edit in current window if the current buffer |
| 6907 | * can be abandoned, otherwise open a new window. */ |
| 6908 | buf = buflist_findnr(ARGLIST[0].ae_fnum); |
| 6909 | |
| 6910 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 6911 | { |
| 6912 | if (wp->w_buffer == buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6913 | { |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6914 | # ifdef FEAT_WINDOWS |
| 6915 | goto_tabpage_tp(tp); |
| 6916 | win_enter(wp, TRUE); |
| 6917 | # ifdef FEAT_GUI_TABLINE |
| 6918 | if (gui_use_tabline()) |
| 6919 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 6920 | # endif |
| 6921 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6922 | curwin->w_arg_idx = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6923 | return; |
| 6924 | } |
| 6925 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6926 | |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6927 | /* |
| 6928 | * Check whether the current buffer is changed. If so, we will need |
| 6929 | * to split the current window or data could be lost. |
| 6930 | * Skip the check if the 'hidden' option is set, as in this case the |
| 6931 | * buffer won't be lost. |
| 6932 | */ |
| 6933 | if (!P_HID(curbuf)) |
| 6934 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6935 | # ifdef FEAT_WINDOWS |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6936 | ++emsg_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6937 | # endif |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6938 | split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6939 | # ifdef FEAT_WINDOWS |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6940 | --emsg_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6941 | # else |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6942 | if (split) |
| 6943 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6944 | # endif |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6945 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6946 | |
Bram Moolenaar | a5b6ad1 | 2006-03-11 21:36:59 +0000 | [diff] [blame] | 6947 | /* Fake a ":sfirst" or ":first" command edit the first argument. */ |
| 6948 | if (split) |
| 6949 | { |
| 6950 | eap->cmdidx = CMD_sfirst; |
| 6951 | eap->cmd[0] = 's'; |
| 6952 | } |
| 6953 | else |
| 6954 | eap->cmdidx = CMD_first; |
| 6955 | ex_rewind(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6956 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6957 | } |
| 6958 | #endif |