PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/vim7.2/src/ex_cmds.c

#
C | 2685 lines | 2460 code | 72 blank | 153 comment | 228 complexity | 0ad278ed4269fab95116e2829c6a1238 MD5 | raw file
  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. * ex_cmds.c: some functions for command line commands
  11. */
  12. #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
  13. # include "vimio.h" /* for mch_open(), must be before vim.h */
  14. #endif
  15. #include "vim.h"
  16. #include "version.h"
  17. #ifdef FEAT_EX_EXTRA
  18. static int linelen __ARGS((int *has_tab));
  19. #endif
  20. static void do_filter __ARGS((linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out));
  21. #ifdef FEAT_VIMINFO
  22. static char_u *viminfo_filename __ARGS((char_u *));
  23. static void do_viminfo __ARGS((FILE *fp_in, FILE *fp_out, int flags));
  24. static int viminfo_encoding __ARGS((vir_T *virp));
  25. static int read_viminfo_up_to_marks __ARGS((vir_T *virp, int forceit, int writing));
  26. #endif
  27. static int check_overwrite __ARGS((exarg_T *eap, buf_T *buf, char_u *fname, char_u *ffname, int other));
  28. static int check_readonly __ARGS((int *forceit, buf_T *buf));
  29. #ifdef FEAT_AUTOCMD
  30. static void delbuf_msg __ARGS((char_u *name));
  31. #endif
  32. static int
  33. #ifdef __BORLANDC__
  34. _RTLENTRYF
  35. #endif
  36. help_compare __ARGS((const void *s1, const void *s2));
  37. /*
  38. * ":ascii" and "ga".
  39. */
  40. void
  41. do_ascii(eap)
  42. exarg_T *eap UNUSED;
  43. {
  44. int c;
  45. int cval;
  46. char buf1[20];
  47. char buf2[20];
  48. char_u buf3[7];
  49. #ifdef FEAT_MBYTE
  50. int cc[MAX_MCO];
  51. int ci = 0;
  52. int len;
  53. if (enc_utf8)
  54. c = utfc_ptr2char(ml_get_cursor(), cc);
  55. else
  56. #endif
  57. c = gchar_cursor();
  58. if (c == NUL)
  59. {
  60. MSG("NUL");
  61. return;
  62. }
  63. #ifdef FEAT_MBYTE
  64. IObuff[0] = NUL;
  65. if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
  66. #endif
  67. {
  68. if (c == NL) /* NUL is stored as NL */
  69. c = NUL;
  70. if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
  71. cval = NL; /* NL is stored as CR */
  72. else
  73. cval = c;
  74. if (vim_isprintc_strict(c) && (c < ' '
  75. #ifndef EBCDIC
  76. || c > '~'
  77. #endif
  78. ))
  79. {
  80. transchar_nonprint(buf3, c);
  81. vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
  82. }
  83. else
  84. buf1[0] = NUL;
  85. #ifndef EBCDIC
  86. if (c >= 0x80)
  87. vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
  88. (char *)transchar(c & 0x7f));
  89. else
  90. #endif
  91. buf2[0] = NUL;
  92. vim_snprintf((char *)IObuff, IOSIZE,
  93. _("<%s>%s%s %d, Hex %02x, Octal %03o"),
  94. transchar(c), buf1, buf2, cval, cval, cval);
  95. #ifdef FEAT_MBYTE
  96. if (enc_utf8)
  97. c = cc[ci++];
  98. else
  99. c = 0;
  100. #endif
  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 (enc_utf8 && utf_iscomposing(c)
  112. # ifdef USE_GUI
  113. && !gui.in_use
  114. # endif
  115. )
  116. IObuff[len++] = ' '; /* draw composing char on top of a space */
  117. len += (*mb_char2bytes)(c, IObuff + len);
  118. vim_snprintf((char *)IObuff + len, IOSIZE - len,
  119. c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
  120. : _("> %d, Hex %08x, Octal %o"), c, c, c);
  121. if (ci == MAX_MCO)
  122. break;
  123. if (enc_utf8)
  124. c = cc[ci++];
  125. else
  126. c = 0;
  127. }
  128. #endif
  129. msg(IObuff);
  130. }
  131. #if defined(FEAT_EX_EXTRA) || defined(PROTO)
  132. /*
  133. * ":left", ":center" and ":right": align text.
  134. */
  135. void
  136. ex_align(eap)
  137. exarg_T *eap;
  138. {
  139. pos_T save_curpos;
  140. int len;
  141. int indent = 0;
  142. int new_indent;
  143. int has_tab;
  144. int width;
  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. width = atoi((char *)eap->arg);
  156. save_curpos = curwin->w_cursor;
  157. if (eap->cmdidx == CMD_left) /* width is used for new indent */
  158. {
  159. if (width >= 0)
  160. indent = width;
  161. }
  162. else
  163. {
  164. /*
  165. * if 'textwidth' set, use it
  166. * else if 'wrapmargin' set, use it
  167. * if invalid value, use 80
  168. */
  169. if (width <= 0)
  170. width = curbuf->b_p_tw;
  171. if (width == 0 && curbuf->b_p_wm > 0)
  172. width = W_WIDTH(curwin) - curbuf->b_p_wm;
  173. if (width <= 0)
  174. width = 80;
  175. }
  176. if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
  177. return;
  178. for (curwin->w_cursor.lnum = eap->line1;
  179. curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
  180. {
  181. if (eap->cmdidx == CMD_left) /* left align */
  182. new_indent = indent;
  183. else
  184. {
  185. has_tab = FALSE; /* avoid uninit warnings */
  186. len = linelen(eap->cmdidx == CMD_right ? &has_tab
  187. : NULL) - get_indent();
  188. if (len <= 0) /* skip blank lines */
  189. continue;
  190. if (eap->cmdidx == CMD_center)
  191. new_indent = (width - len) / 2;
  192. else
  193. {
  194. new_indent = width - len; /* right align */
  195. /*
  196. * Make sure that embedded TABs don't make the text go too far
  197. * to the right.
  198. */
  199. if (has_tab)
  200. while (new_indent > 0)
  201. {
  202. (void)set_indent(new_indent, 0);
  203. if (linelen(NULL) <= width)
  204. {
  205. /*
  206. * Now try to move the line as much as possible to
  207. * the right. Stop when it moves too far.
  208. */
  209. do
  210. (void)set_indent(++new_indent, 0);
  211. while (linelen(NULL) <= width);
  212. --new_indent;
  213. break;
  214. }
  215. --new_indent;
  216. }
  217. }
  218. }
  219. if (new_indent < 0)
  220. new_indent = 0;
  221. (void)set_indent(new_indent, 0); /* set indent */
  222. }
  223. changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
  224. curwin->w_cursor = save_curpos;
  225. beginline(BL_WHITE | BL_FIX);
  226. }
  227. /*
  228. * Get the length of the current line, excluding trailing white space.
  229. */
  230. static int
  231. linelen(has_tab)
  232. int *has_tab;
  233. {
  234. char_u *line;
  235. char_u *first;
  236. char_u *last;
  237. int save;
  238. int len;
  239. /* find the first non-blank character */
  240. line = ml_get_curline();
  241. first = skipwhite(line);
  242. /* find the character after the last non-blank character */
  243. for (last = first + STRLEN(first);
  244. last > first && vim_iswhite(last[-1]); --last)
  245. ;
  246. save = *last;
  247. *last = NUL;
  248. len = linetabsize(line); /* get line length */
  249. if (has_tab != NULL) /* check for embedded TAB */
  250. *has_tab = (vim_strrchr(first, TAB) != NULL);
  251. *last = save;
  252. return len;
  253. }
  254. /* Buffer for two lines used during sorting. They are allocated to
  255. * contain the longest line being sorted. */
  256. static char_u *sortbuf1;
  257. static char_u *sortbuf2;
  258. static int sort_ic; /* ignore case */
  259. static int sort_nr; /* sort on number */
  260. static int sort_rx; /* sort on regex instead of skipping it */
  261. static int sort_abort; /* flag to indicate if sorting has been interrupted */
  262. /* Struct to store info to be sorted. */
  263. typedef struct
  264. {
  265. linenr_T lnum; /* line number */
  266. long start_col_nr; /* starting column number or number */
  267. long end_col_nr; /* ending column number */
  268. } sorti_T;
  269. static int
  270. #ifdef __BORLANDC__
  271. _RTLENTRYF
  272. #endif
  273. sort_compare __ARGS((const void *s1, const void *s2));
  274. static int
  275. #ifdef __BORLANDC__
  276. _RTLENTRYF
  277. #endif
  278. sort_compare(s1, s2)
  279. const void *s1;
  280. const void *s2;
  281. {
  282. sorti_T l1 = *(sorti_T *)s1;
  283. sorti_T l2 = *(sorti_T *)s2;
  284. int result = 0;
  285. /* If the user interrupts, there's no way to stop qsort() immediately, but
  286. * if we return 0 every time, qsort will assume it's done sorting and
  287. * exit. */
  288. if (sort_abort)
  289. return 0;
  290. fast_breakcheck();
  291. if (got_int)
  292. sort_abort = TRUE;
  293. /* When sorting numbers "start_col_nr" is the number, not the column
  294. * number. */
  295. if (sort_nr)
  296. result = l1.start_col_nr - l2.start_col_nr;
  297. else
  298. {
  299. /* We need to copy one line into "sortbuf1", because there is no
  300. * guarantee that the first pointer becomes invalid when obtaining the
  301. * second one. */
  302. STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.start_col_nr,
  303. l1.end_col_nr - l1.start_col_nr + 1);
  304. sortbuf1[l1.end_col_nr - l1.start_col_nr] = 0;
  305. STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.start_col_nr,
  306. l2.end_col_nr - l2.start_col_nr + 1);
  307. sortbuf2[l2.end_col_nr - l2.start_col_nr] = 0;
  308. result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
  309. : STRCMP(sortbuf1, sortbuf2);
  310. }
  311. /* If two lines have the same value, preserve the original line order. */
  312. if (result == 0)
  313. return (int)(l1.lnum - l2.lnum);
  314. return result;
  315. }
  316. /*
  317. * ":sort".
  318. */
  319. void
  320. ex_sort(eap)
  321. exarg_T *eap;
  322. {
  323. regmatch_T regmatch;
  324. int len;
  325. linenr_T lnum;
  326. long maxlen = 0;
  327. sorti_T *nrs;
  328. size_t count = (size_t)(eap->line2 - eap->line1 + 1);
  329. size_t i;
  330. char_u *p;
  331. char_u *s;
  332. char_u *s2;
  333. char_u c; /* temporary character storage */
  334. int unique = FALSE;
  335. long deleted;
  336. colnr_T start_col;
  337. colnr_T end_col;
  338. int sort_oct; /* sort on octal number */
  339. int sort_hex; /* sort on hex number */
  340. /* Sorting one line is really quick! */
  341. if (count <= 1)
  342. return;
  343. if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
  344. return;
  345. sortbuf1 = NULL;
  346. sortbuf2 = NULL;
  347. regmatch.regprog = NULL;
  348. nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
  349. if (nrs == NULL)
  350. goto sortend;
  351. sort_abort = sort_ic = sort_rx = sort_nr = sort_oct = sort_hex = 0;
  352. for (p = eap->arg; *p != NUL; ++p)
  353. {
  354. if (vim_iswhite(*p))
  355. ;
  356. else if (*p == 'i')
  357. sort_ic = TRUE;
  358. else if (*p == 'r')
  359. sort_rx = TRUE;
  360. else if (*p == 'n')
  361. sort_nr = 2;
  362. else if (*p == 'o')
  363. sort_oct = 2;
  364. else if (*p == 'x')
  365. sort_hex = 2;
  366. else if (*p == 'u')
  367. unique = TRUE;
  368. else if (*p == '"') /* comment start */
  369. break;
  370. else if (check_nextcmd(p) != NULL)
  371. {
  372. eap->nextcmd = check_nextcmd(p);
  373. break;
  374. }
  375. else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
  376. {
  377. s = skip_regexp(p + 1, *p, TRUE, NULL);
  378. if (*s != *p)
  379. {
  380. EMSG(_(e_invalpat));
  381. goto sortend;
  382. }
  383. *s = NUL;
  384. /* Use last search pattern if sort pattern is empty. */
  385. if (s == p + 1 && last_search_pat() != NULL)
  386. regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
  387. else
  388. regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
  389. if (regmatch.regprog == NULL)
  390. goto sortend;
  391. p = s; /* continue after the regexp */
  392. regmatch.rm_ic = p_ic;
  393. }
  394. else
  395. {
  396. EMSG2(_(e_invarg2), p);
  397. goto sortend;
  398. }
  399. }
  400. /* Can only have one of 'n', 'o' and 'x'. */
  401. if (sort_nr + sort_oct + sort_hex > 2)
  402. {
  403. EMSG(_(e_invarg));
  404. goto sortend;
  405. }
  406. /* From here on "sort_nr" is used as a flag for any number sorting. */
  407. sort_nr += sort_oct + sort_hex;
  408. /*
  409. * Make an array with all line numbers. This avoids having to copy all
  410. * the lines into allocated memory.
  411. * When sorting on strings "start_col_nr" is the offset in the line, for
  412. * numbers sorting it's the number to sort on. This means the pattern
  413. * matching and number conversion only has to be done once per line.
  414. * Also get the longest line length for allocating "sortbuf".
  415. */
  416. for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
  417. {
  418. s = ml_get(lnum);
  419. len = (int)STRLEN(s);
  420. if (maxlen < len)
  421. maxlen = len;
  422. start_col = 0;
  423. end_col = len;
  424. if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
  425. {
  426. if (sort_rx)
  427. {
  428. start_col = (colnr_T)(regmatch.startp[0] - s);
  429. end_col = (colnr_T)(regmatch.endp[0] - s);
  430. }
  431. else
  432. start_col = (colnr_T)(regmatch.endp[0] - s);
  433. }
  434. else
  435. if (regmatch.regprog != NULL)
  436. end_col = 0;
  437. if (sort_nr)
  438. {
  439. /* Make sure vim_str2nr doesn't read any digits past the end
  440. * of the match, by temporarily terminating the string there */
  441. s2 = s + end_col;
  442. c = *s2;
  443. (*s2) = 0;
  444. /* Sorting on number: Store the number itself. */
  445. p = s + start_col;
  446. if (sort_hex)
  447. s = skiptohex(p);
  448. else
  449. s = skiptodigit(p);
  450. if (s > p && s[-1] == '-')
  451. --s; /* include preceding negative sign */
  452. vim_str2nr(s, NULL, NULL, sort_oct, sort_hex,
  453. &nrs[lnum - eap->line1].start_col_nr, NULL);
  454. (*s2) = c;
  455. }
  456. else
  457. {
  458. /* Store the column to sort at. */
  459. nrs[lnum - eap->line1].start_col_nr = start_col;
  460. nrs[lnum - eap->line1].end_col_nr = end_col;
  461. }
  462. nrs[lnum - eap->line1].lnum = lnum;
  463. if (regmatch.regprog != NULL)
  464. fast_breakcheck();
  465. if (got_int)
  466. goto sortend;
  467. }
  468. /* Allocate a buffer that can hold the longest line. */
  469. sortbuf1 = alloc((unsigned)maxlen + 1);
  470. if (sortbuf1 == NULL)
  471. goto sortend;
  472. sortbuf2 = alloc((unsigned)maxlen + 1);
  473. if (sortbuf2 == NULL)
  474. goto sortend;
  475. /* Sort the array of line numbers. Note: can't be interrupted! */
  476. qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
  477. if (sort_abort)
  478. goto sortend;
  479. /* Insert the lines in the sorted order below the last one. */
  480. lnum = eap->line2;
  481. for (i = 0; i < count; ++i)
  482. {
  483. s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
  484. if (!unique || i == 0
  485. || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
  486. {
  487. if (ml_append(lnum++, s, (colnr_T)0, FALSE) == FAIL)
  488. break;
  489. if (unique)
  490. STRCPY(sortbuf1, s);
  491. }
  492. fast_breakcheck();
  493. if (got_int)
  494. goto sortend;
  495. }
  496. /* delete the original lines if appending worked */
  497. if (i == count)
  498. for (i = 0; i < count; ++i)
  499. ml_delete(eap->line1, FALSE);
  500. else
  501. count = 0;
  502. /* Adjust marks for deleted (or added) lines and prepare for displaying. */
  503. deleted = (long)(count - (lnum - eap->line2));
  504. if (deleted > 0)
  505. mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
  506. else if (deleted < 0)
  507. mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
  508. changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
  509. curwin->w_cursor.lnum = eap->line1;
  510. beginline(BL_WHITE | BL_FIX);
  511. sortend:
  512. vim_free(nrs);
  513. vim_free(sortbuf1);
  514. vim_free(sortbuf2);
  515. vim_free(regmatch.regprog);
  516. if (got_int)
  517. EMSG(_(e_interr));
  518. }
  519. /*
  520. * ":retab".
  521. */
  522. void
  523. ex_retab(eap)
  524. exarg_T *eap;
  525. {
  526. linenr_T lnum;
  527. int got_tab = FALSE;
  528. long num_spaces = 0;
  529. long num_tabs;
  530. long len;
  531. long col;
  532. long vcol;
  533. long start_col = 0; /* For start of white-space string */
  534. long start_vcol = 0; /* For start of white-space string */
  535. int temp;
  536. long old_len;
  537. char_u *ptr;
  538. char_u *new_line = (char_u *)1; /* init to non-NULL */
  539. int did_undo; /* called u_save for current line */
  540. int new_ts;
  541. int save_list;
  542. linenr_T first_line = 0; /* first changed line */
  543. linenr_T last_line = 0; /* last changed line */
  544. save_list = curwin->w_p_list;
  545. curwin->w_p_list = 0; /* don't want list mode here */
  546. new_ts = getdigits(&(eap->arg));
  547. if (new_ts < 0)
  548. {
  549. EMSG(_(e_positive));
  550. return;
  551. }
  552. if (new_ts == 0)
  553. new_ts = curbuf->b_p_ts;
  554. for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
  555. {
  556. ptr = ml_get(lnum);
  557. col = 0;
  558. vcol = 0;
  559. did_undo = FALSE;
  560. for (;;)
  561. {
  562. if (vim_iswhite(ptr[col]))
  563. {
  564. if (!got_tab && num_spaces == 0)
  565. {
  566. /* First consecutive white-space */
  567. start_vcol = vcol;
  568. start_col = col;
  569. }
  570. if (ptr[col] == ' ')
  571. num_spaces++;
  572. else
  573. got_tab = TRUE;
  574. }
  575. else
  576. {
  577. if (got_tab || (eap->forceit && num_spaces > 1))
  578. {
  579. /* Retabulate this string of white-space */
  580. /* len is virtual length of white string */
  581. len = num_spaces = vcol - start_vcol;
  582. num_tabs = 0;
  583. if (!curbuf->b_p_et)
  584. {
  585. temp = new_ts - (start_vcol % new_ts);
  586. if (num_spaces >= temp)
  587. {
  588. num_spaces -= temp;
  589. num_tabs++;
  590. }
  591. num_tabs += num_spaces / new_ts;
  592. num_spaces -= (num_spaces / new_ts) * new_ts;
  593. }
  594. if (curbuf->b_p_et || got_tab ||
  595. (num_spaces + num_tabs < len))
  596. {
  597. if (did_undo == FALSE)
  598. {
  599. did_undo = TRUE;
  600. if (u_save((linenr_T)(lnum - 1),
  601. (linenr_T)(lnum + 1)) == FAIL)
  602. {
  603. new_line = NULL; /* flag out-of-memory */
  604. break;
  605. }
  606. }
  607. /* len is actual number of white characters used */
  608. len = num_spaces + num_tabs;
  609. old_len = (long)STRLEN(ptr);
  610. new_line = lalloc(old_len - col + start_col + len + 1,
  611. TRUE);
  612. if (new_line == NULL)
  613. break;
  614. if (start_col > 0)
  615. mch_memmove(new_line, ptr, (size_t)start_col);
  616. mch_memmove(new_line + start_col + len,
  617. ptr + col, (size_t)(old_len - col + 1));
  618. ptr = new_line + start_col;
  619. for (col = 0; col < len; col++)
  620. ptr[col] = (col < num_tabs) ? '\t' : ' ';
  621. ml_replace(lnum, new_line, FALSE);
  622. if (first_line == 0)
  623. first_line = lnum;
  624. last_line = lnum;
  625. ptr = new_line;
  626. col = start_col + len;
  627. }
  628. }
  629. got_tab = FALSE;
  630. num_spaces = 0;
  631. }
  632. if (ptr[col] == NUL)
  633. break;
  634. vcol += chartabsize(ptr + col, (colnr_T)vcol);
  635. #ifdef FEAT_MBYTE
  636. if (has_mbyte)
  637. col += (*mb_ptr2len)(ptr + col);
  638. else
  639. #endif
  640. ++col;
  641. }
  642. if (new_line == NULL) /* out of memory */
  643. break;
  644. line_breakcheck();
  645. }
  646. if (got_int)
  647. EMSG(_(e_interr));
  648. if (curbuf->b_p_ts != new_ts)
  649. redraw_curbuf_later(NOT_VALID);
  650. if (first_line != 0)
  651. changed_lines(first_line, 0, last_line + 1, 0L);
  652. curwin->w_p_list = save_list; /* restore 'list' */
  653. curbuf->b_p_ts = new_ts;
  654. coladvance(curwin->w_curswant);
  655. u_clearline();
  656. }
  657. #endif
  658. /*
  659. * :move command - move lines line1-line2 to line dest
  660. *
  661. * return FAIL for failure, OK otherwise
  662. */
  663. int
  664. do_move(line1, line2, dest)
  665. linenr_T line1;
  666. linenr_T line2;
  667. linenr_T dest;
  668. {
  669. char_u *str;
  670. linenr_T l;
  671. linenr_T extra; /* Num lines added before line1 */
  672. linenr_T num_lines; /* Num lines moved */
  673. linenr_T last_line; /* Last line in file after adding new text */
  674. if (dest >= line1 && dest < line2)
  675. {
  676. EMSG(_("E134: Move lines into themselves"));
  677. return FAIL;
  678. }
  679. num_lines = line2 - line1 + 1;
  680. /*
  681. * First we copy the old text to its new location -- webb
  682. * Also copy the flag that ":global" command uses.
  683. */
  684. if (u_save(dest, dest + 1) == FAIL)
  685. return FAIL;
  686. for (extra = 0, l = line1; l <= line2; l++)
  687. {
  688. str = vim_strsave(ml_get(l + extra));
  689. if (str != NULL)
  690. {
  691. ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
  692. vim_free(str);
  693. if (dest < line1)
  694. extra++;
  695. }
  696. }
  697. /*
  698. * Now we must be careful adjusting our marks so that we don't overlap our
  699. * mark_adjust() calls.
  700. *
  701. * We adjust the marks within the old text so that they refer to the
  702. * last lines of the file (temporarily), because we know no other marks
  703. * will be set there since these line numbers did not exist until we added
  704. * our new lines.
  705. *
  706. * Then we adjust the marks on lines between the old and new text positions
  707. * (either forwards or backwards).
  708. *
  709. * And Finally we adjust the marks we put at the end of the file back to
  710. * their final destination at the new text position -- webb
  711. */
  712. last_line = curbuf->b_ml.ml_line_count;
  713. mark_adjust(line1, line2, last_line - line2, 0L);
  714. if (dest >= line2)
  715. {
  716. mark_adjust(line2 + 1, dest, -num_lines, 0L);
  717. curbuf->b_op_start.lnum = dest - num_lines + 1;
  718. curbuf->b_op_end.lnum = dest;
  719. }
  720. else
  721. {
  722. mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
  723. curbuf->b_op_start.lnum = dest + 1;
  724. curbuf->b_op_end.lnum = dest + num_lines;
  725. }
  726. curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
  727. mark_adjust(last_line - num_lines + 1, last_line,
  728. -(last_line - dest - extra), 0L);
  729. /*
  730. * Now we delete the original text -- webb
  731. */
  732. if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
  733. return FAIL;
  734. for (l = line1; l <= line2; l++)
  735. ml_delete(line1 + extra, TRUE);
  736. if (!global_busy && num_lines > p_report)
  737. {
  738. if (num_lines == 1)
  739. MSG(_("1 line moved"));
  740. else
  741. smsg((char_u *)_("%ld lines moved"), num_lines);
  742. }
  743. /*
  744. * Leave the cursor on the last of the moved lines.
  745. */
  746. if (dest >= line1)
  747. curwin->w_cursor.lnum = dest;
  748. else
  749. curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
  750. if (line1 < dest)
  751. changed_lines(line1, 0, dest + num_lines + 1, 0L);
  752. else
  753. changed_lines(dest + 1, 0, line1 + num_lines, 0L);
  754. return OK;
  755. }
  756. /*
  757. * ":copy"
  758. */
  759. void
  760. ex_copy(line1, line2, n)
  761. linenr_T line1;
  762. linenr_T line2;
  763. linenr_T n;
  764. {
  765. linenr_T count;
  766. char_u *p;
  767. count = line2 - line1 + 1;
  768. curbuf->b_op_start.lnum = n + 1;
  769. curbuf->b_op_end.lnum = n + count;
  770. curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
  771. /*
  772. * there are three situations:
  773. * 1. destination is above line1
  774. * 2. destination is between line1 and line2
  775. * 3. destination is below line2
  776. *
  777. * n = destination (when starting)
  778. * curwin->w_cursor.lnum = destination (while copying)
  779. * line1 = start of source (while copying)
  780. * line2 = end of source (while copying)
  781. */
  782. if (u_save(n, n + 1) == FAIL)
  783. return;
  784. curwin->w_cursor.lnum = n;
  785. while (line1 <= line2)
  786. {
  787. /* need to use vim_strsave() because the line will be unlocked within
  788. * ml_append() */
  789. p = vim_strsave(ml_get(line1));
  790. if (p != NULL)
  791. {
  792. ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
  793. vim_free(p);
  794. }
  795. /* situation 2: skip already copied lines */
  796. if (line1 == n)
  797. line1 = curwin->w_cursor.lnum;
  798. ++line1;
  799. if (curwin->w_cursor.lnum < line1)
  800. ++line1;
  801. if (curwin->w_cursor.lnum < line2)
  802. ++line2;
  803. ++curwin->w_cursor.lnum;
  804. }
  805. appended_lines_mark(n, count);
  806. msgmore((long)count);
  807. }
  808. static char_u *prevcmd = NULL; /* the previous command */
  809. #if defined(EXITFREE) || defined(PROTO)
  810. void
  811. free_prev_shellcmd()
  812. {
  813. vim_free(prevcmd);
  814. }
  815. #endif
  816. /*
  817. * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
  818. * Bangs in the argument are replaced with the previously entered command.
  819. * Remember the argument.
  820. *
  821. * RISCOS: Bangs only replaced when followed by a space, since many
  822. * pathnames contain one.
  823. */
  824. void
  825. do_bang(addr_count, eap, forceit, do_in, do_out)
  826. int addr_count;
  827. exarg_T *eap;
  828. int forceit;
  829. int do_in, do_out;
  830. {
  831. char_u *arg = eap->arg; /* command */
  832. linenr_T line1 = eap->line1; /* start of range */
  833. linenr_T line2 = eap->line2; /* end of range */
  834. char_u *newcmd = NULL; /* the new command */
  835. int free_newcmd = FALSE; /* need to free() newcmd */
  836. int ins_prevcmd;
  837. char_u *t;
  838. char_u *p;
  839. char_u *trailarg;
  840. int len;
  841. int scroll_save = msg_scroll;
  842. /*
  843. * Disallow shell commands for "rvim".
  844. * Disallow shell commands from .exrc and .vimrc in current directory for
  845. * security reasons.
  846. */
  847. if (check_restricted() || check_secure())
  848. return;
  849. if (addr_count == 0) /* :! */
  850. {
  851. msg_scroll = FALSE; /* don't scroll here */
  852. autowrite_all();
  853. msg_scroll = scroll_save;
  854. }
  855. /*
  856. * Try to find an embedded bang, like in :!<cmd> ! [args]
  857. * (:!! is indicated by the 'forceit' variable)
  858. */
  859. ins_prevcmd = forceit;
  860. trailarg = arg;
  861. do
  862. {
  863. len = (int)STRLEN(trailarg) + 1;
  864. if (newcmd != NULL)
  865. len += (int)STRLEN(newcmd);
  866. if (ins_prevcmd)
  867. {
  868. if (prevcmd == NULL)
  869. {
  870. EMSG(_(e_noprev));
  871. vim_free(newcmd);
  872. return;
  873. }
  874. len += (int)STRLEN(prevcmd);
  875. }
  876. if ((t = alloc((unsigned)len)) == NULL)
  877. {
  878. vim_free(newcmd);
  879. return;
  880. }
  881. *t = NUL;
  882. if (newcmd != NULL)
  883. STRCAT(t, newcmd);
  884. if (ins_prevcmd)
  885. STRCAT(t, prevcmd);
  886. p = t + STRLEN(t);
  887. STRCAT(t, trailarg);
  888. vim_free(newcmd);
  889. newcmd = t;
  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. STRMOVE(p - 1, p);
  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. vim_free(prevcmd);
  917. prevcmd = newcmd;
  918. if (bangredo) /* put cmd in redo buffer for ! command */
  919. {
  920. AppendToRedobuffLit(prevcmd, -1);
  921. AppendToRedobuff((char_u *)"\n");
  922. bangredo = FALSE;
  923. }
  924. /*
  925. * Add quotes around the command, for shells that need them.
  926. */
  927. if (*p_shq != NUL)
  928. {
  929. newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
  930. if (newcmd == NULL)
  931. return;
  932. STRCPY(newcmd, p_shq);
  933. STRCAT(newcmd, prevcmd);
  934. STRCAT(newcmd, p_shq);
  935. free_newcmd = TRUE;
  936. }
  937. if (addr_count == 0) /* :! */
  938. {
  939. /* echo the command */
  940. msg_start();
  941. msg_putchar(':');
  942. msg_putchar('!');
  943. msg_outtrans(newcmd);
  944. msg_clr_eos();
  945. windgoto(msg_row, msg_col);
  946. do_shell(newcmd, 0);
  947. }
  948. else /* :range! */
  949. {
  950. /* Careful: This may recursively call do_bang() again! (because of
  951. * autocommands) */
  952. do_filter(line1, line2, eap, newcmd, do_in, do_out);
  953. #ifdef FEAT_AUTOCMD
  954. apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
  955. #endif
  956. }
  957. if (free_newcmd)
  958. vim_free(newcmd);
  959. }
  960. /*
  961. * do_filter: filter lines through a command given by the user
  962. *
  963. * We mostly use temp files and the call_shell() routine here. This would
  964. * normally be done using pipes on a UNIX machine, but this is more portable
  965. * to non-unix machines. The call_shell() routine needs to be able
  966. * to deal with redirection somehow, and should handle things like looking
  967. * at the PATH env. variable, and adding reasonable extensions to the
  968. * command name given by the user. All reasonable versions of call_shell()
  969. * do this.
  970. * Alternatively, if on Unix and redirecting input or output, but not both,
  971. * and the 'shelltemp' option isn't set, use pipes.
  972. * We use input redirection if do_in is TRUE.
  973. * We use output redirection if do_out is TRUE.
  974. */
  975. static void
  976. do_filter(line1, line2, eap, cmd, do_in, do_out)
  977. linenr_T line1, line2;
  978. exarg_T *eap; /* for forced 'ff' and 'fenc' */
  979. char_u *cmd;
  980. int do_in, do_out;
  981. {
  982. char_u *itmp = NULL;
  983. char_u *otmp = NULL;
  984. linenr_T linecount;
  985. linenr_T read_linecount;
  986. pos_T cursor_save;
  987. char_u *cmd_buf;
  988. #ifdef FEAT_AUTOCMD
  989. buf_T *old_curbuf = curbuf;
  990. #endif
  991. int shell_flags = 0;
  992. if (*cmd == NUL) /* no filter command */
  993. return;
  994. #ifdef WIN3264
  995. /*
  996. * Check if external commands are allowed now.
  997. */
  998. if (can_end_termcap_mode(TRUE) == FALSE)
  999. return;
  1000. #endif
  1001. cursor_save = curwin->w_cursor;
  1002. linecount = line2 - line1 + 1;
  1003. curwin->w_cursor.lnum = line1;
  1004. curwin->w_cursor.col = 0;
  1005. changed_line_abv_curs();
  1006. invalidate_botline();
  1007. /*
  1008. * When using temp files:
  1009. * 1. * Form temp file names
  1010. * 2. * Write the lines to a temp file
  1011. * 3. Run the filter command on the temp file
  1012. * 4. * Read the output of the command into the buffer
  1013. * 5. * Delete the original lines to be filtered
  1014. * 6. * Remove the temp files
  1015. *
  1016. * When writing the input with a pipe or when catching the output with a
  1017. * pipe only need to do 3.
  1018. */
  1019. if (do_out)
  1020. shell_flags |= SHELL_DOOUT;
  1021. #if !defined(USE_SYSTEM) && defined(UNIX)
  1022. if (!do_in && do_out && !p_stmp)
  1023. {
  1024. /* Use a pipe to fetch stdout of the command, do not use a temp file. */
  1025. shell_flags |= SHELL_READ;
  1026. curwin->w_cursor.lnum = line2;
  1027. }
  1028. else if (do_in && !do_out && !p_stmp)
  1029. {
  1030. /* Use a pipe to write stdin of the command, do not use a temp file. */
  1031. shell_flags |= SHELL_WRITE;
  1032. curbuf->b_op_start.lnum = line1;
  1033. curbuf->b_op_end.lnum = line2;
  1034. }
  1035. else if (do_in && do_out && !p_stmp)
  1036. {
  1037. /* Use a pipe to write stdin and fetch stdout of the command, do not
  1038. * use a temp file. */
  1039. shell_flags |= SHELL_READ|SHELL_WRITE;
  1040. curbuf->b_op_start.lnum = line1;
  1041. curbuf->b_op_end.lnum = line2;
  1042. curwin->w_cursor.lnum = line2;
  1043. }
  1044. else
  1045. #endif
  1046. if ((do_in && (itmp = vim_tempname('i')) == NULL)
  1047. || (do_out && (otmp = vim_tempname('o')) == NULL))
  1048. {
  1049. EMSG(_(e_notmp));
  1050. goto filterend;
  1051. }
  1052. /*
  1053. * The writing and reading of temp files will not be shown.
  1054. * Vi also doesn't do this and the messages are not very informative.
  1055. */
  1056. ++no_wait_return; /* don't call wait_return() while busy */
  1057. if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
  1058. FALSE, FALSE, FALSE, TRUE) == FAIL)
  1059. {
  1060. msg_putchar('\n'); /* keep message from buf_write() */
  1061. --no_wait_return;
  1062. #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  1063. if (!aborting())
  1064. #endif
  1065. (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
  1066. goto filterend;
  1067. }
  1068. #ifdef FEAT_AUTOCMD
  1069. if (curbuf != old_curbuf)
  1070. goto filterend;
  1071. #endif
  1072. if (!do_out)
  1073. msg_putchar('\n');
  1074. /* Create the shell command in allocated memory. */
  1075. cmd_buf = make_filter_cmd(cmd, itmp, otmp);
  1076. if (cmd_buf == NULL)
  1077. goto filterend;
  1078. windgoto((int)Rows - 1, 0);
  1079. cursor_on();
  1080. /*
  1081. * When not redirecting the output the command can write anything to the
  1082. * screen. If 'shellredir' is equal to ">", screen may be messed up by
  1083. * stderr output of external command. Clear the screen later.
  1084. * If do_in is FALSE, this could be something like ":r !cat", which may
  1085. * also mess up the screen, clear it later.
  1086. */
  1087. if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
  1088. redraw_later_clear();
  1089. if (do_out)
  1090. {
  1091. if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
  1092. {
  1093. vim_free(cmd_buf);
  1094. goto error;
  1095. }
  1096. redraw_curbuf_later(VALID);
  1097. }
  1098. read_linecount = curbuf->b_ml.ml_line_count;
  1099. /*
  1100. * When call_shell() fails wait_return() is called to give the user a
  1101. * chance to read the error messages. Otherwise errors are ignored, so you
  1102. * can see the error messages from the command that appear on stdout; use
  1103. * 'u' to fix the text
  1104. * Switch to cooked mode when not redirecting stdin, avoids that something
  1105. * like ":r !cat" hangs.
  1106. * Pass on the SHELL_DOOUT flag when the output is being redirected.
  1107. */
  1108. if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
  1109. {
  1110. redraw_later_clear();
  1111. wait_return(FALSE);
  1112. }
  1113. vim_free(cmd_buf);
  1114. did_check_timestamps = FALSE;
  1115. need_check_timestamps = TRUE;
  1116. /* When interrupting the shell command, it may still have produced some
  1117. * useful output. Reset got_int here, so that readfile() won't cancel
  1118. * reading. */
  1119. ui_breakcheck();
  1120. got_int = FALSE;
  1121. if (do_out)
  1122. {
  1123. if (otmp != NULL)
  1124. {
  1125. if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
  1126. eap, READ_FILTER) == FAIL)
  1127. {
  1128. #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  1129. if (!aborting())
  1130. #endif
  1131. {
  1132. msg_putchar('\n');
  1133. EMSG2(_(e_notread), otmp);
  1134. }
  1135. goto error;
  1136. }
  1137. #ifdef FEAT_AUTOCMD
  1138. if (curbuf != old_curbuf)
  1139. goto filterend;
  1140. #endif
  1141. }
  1142. read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
  1143. if (shell_flags & SHELL_READ)
  1144. {
  1145. curbuf->b_op_start.lnum = line2 + 1;
  1146. curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
  1147. appended_lines_mark(line2, read_linecount);
  1148. }
  1149. if (do_in)
  1150. {
  1151. if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
  1152. {
  1153. if (read_linecount >= linecount)
  1154. /* move all marks from old lines to new lines */
  1155. mark_adjust(line1, line2, linecount, 0L);
  1156. else
  1157. {
  1158. /* move marks from old lines to new lines, delete marks
  1159. * that are in deleted lines */
  1160. mark_adjust(line1, line1 + read_linecount - 1,
  1161. linecount, 0L);
  1162. mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
  1163. }
  1164. }
  1165. /*
  1166. * Put cursor on first filtered line for ":range!cmd".
  1167. * Adjust '[ and '] (set by buf_write()).
  1168. */
  1169. curwin->w_cursor.lnum = line1;
  1170. del_lines(linecount, TRUE);
  1171. curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
  1172. curbuf->b_op_end.lnum -= linecount; /* adjust '] */
  1173. write_lnum_adjust(-linecount); /* adjust last line
  1174. for next write */
  1175. #ifdef FEAT_FOLDING
  1176. foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
  1177. #endif
  1178. }
  1179. else
  1180. {
  1181. /*
  1182. * Put cursor on last new line for ":r !cmd".
  1183. */
  1184. linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
  1185. curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
  1186. }
  1187. beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
  1188. --no_wait_return;
  1189. if (linecount > p_report)
  1190. {
  1191. if (do_in)
  1192. {
  1193. vim_snprintf((char *)msg_buf, sizeof(msg_buf),
  1194. _("%ld lines filtered"), (long)linecount);
  1195. if (msg(msg_buf) && !msg_scroll)
  1196. /* save message to display it after redraw */
  1197. set_keep_msg(msg_buf, 0);
  1198. }
  1199. else
  1200. msgmore((long)linecount);
  1201. }
  1202. }
  1203. else
  1204. {
  1205. error:
  1206. /* put cursor back in same position for ":w !cmd" */
  1207. curwin->w_cursor = cursor_save;
  1208. --no_wait_return;
  1209. wait_return(FALSE);
  1210. }
  1211. filterend:
  1212. #ifdef FEAT_AUTOCMD
  1213. if (curbuf != old_curbuf)
  1214. {
  1215. --no_wait_return;
  1216. EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
  1217. }
  1218. #endif
  1219. if (itmp != NULL)
  1220. mch_remove(itmp);
  1221. if (otmp != NULL)
  1222. mch_remove(otmp);
  1223. vim_free(itmp);
  1224. vim_free(otmp);
  1225. }
  1226. /*
  1227. * Call a shell to execute a command.
  1228. * When "cmd" is NULL start an interactive shell.
  1229. */
  1230. void
  1231. do_shell(cmd, flags)
  1232. char_u *cmd;
  1233. int flags; /* may be SHELL_DOOUT when output is redirected */
  1234. {
  1235. buf_T *buf;
  1236. #ifndef FEAT_GUI_MSWIN
  1237. int save_nwr;
  1238. #endif
  1239. #ifdef MSWIN
  1240. int winstart = FALSE;
  1241. #endif
  1242. /*
  1243. * Disallow shell commands for "rvim".
  1244. * Disallow shell commands from .exrc and .vimrc in current directory for
  1245. * security reasons.
  1246. */
  1247. if (check_restricted() || check_secure())
  1248. {
  1249. msg_end();
  1250. return;
  1251. }
  1252. #ifdef MSWIN
  1253. /*
  1254. * Check if external commands are allowed now.
  1255. */
  1256. if (can_end_termcap_mode(TRUE) == FALSE)
  1257. return;
  1258. /*
  1259. * Check if ":!start" is used.
  1260. */
  1261. if (cmd != NULL)
  1262. winstart = (STRNICMP(cmd, "start ", 6) == 0);
  1263. #endif
  1264. /*
  1265. * For autocommands we want to get the output on the current screen, to
  1266. * avoid having to type return below.
  1267. */
  1268. msg_putchar('\r'); /* put cursor at start of line */
  1269. #ifdef FEAT_AUTOCMD
  1270. if (!autocmd_busy)
  1271. #endif
  1272. {
  1273. #ifdef MSWIN
  1274. if (!winstart)
  1275. #endif
  1276. stoptermcap();
  1277. }
  1278. #ifdef MSWIN
  1279. if (!winstart)
  1280. #endif
  1281. msg_putchar('\n'); /* may shift screen one line up */
  1282. /* warning message before calling the shell */
  1283. if (p_warn
  1284. #ifdef FEAT_AUTOCMD
  1285. && !autocmd_busy
  1286. #endif
  1287. && msg_silent == 0)
  1288. for (buf = firstbuf; buf; buf = buf->b_next)
  1289. if (bufIsChanged(buf))
  1290. {
  1291. #ifdef FEAT_GUI_MSWIN
  1292. if (!winstart)
  1293. starttermcap(); /* don't want a message box here */
  1294. #endif
  1295. MSG_PUTS(_("[No write since last change]\n"));
  1296. #ifdef FEAT_GUI_MSWIN
  1297. if (!winstart)
  1298. stoptermcap();
  1299. #endif
  1300. break;
  1301. }
  1302. /* This windgoto is required for when the '\n' resulted in a "delete line
  1303. * 1" command to the terminal. */
  1304. if (!swapping_screen())
  1305. windgoto(msg_row, msg_col);
  1306. cursor_on();
  1307. (void)call_shell(cmd, SHELL_COOKED | flags);
  1308. did_check_timestamps = FALSE;
  1309. need_check_timestamps = TRUE;
  1310. /*
  1311. * put the message cursor at the end of the screen, avoids wait_return()
  1312. * to overwrite the text that the external command showed
  1313. */
  1314. if (!swapping_screen())
  1315. {
  1316. msg_row = Rows - 1;
  1317. msg_col = 0;
  1318. }
  1319. #ifdef FEAT_AUTOCMD
  1320. if (autocmd_busy)
  1321. {
  1322. if (msg_silent == 0)
  1323. redraw_later_clear();
  1324. }
  1325. else
  1326. #endif
  1327. {
  1328. /*
  1329. * For ":sh" there is no need to call wait_return(), just redraw.
  1330. * Also for the Win32 GUI (the output is in a console window).
  1331. * Otherwise there is probably text on the screen that the user wants
  1332. * to read before redrawing, so call wait_return().
  1333. */
  1334. #ifndef FEAT_GUI_MSWIN
  1335. if (cmd == NULL
  1336. # ifdef WIN3264
  1337. || (winstart && !need_wait_return)
  1338. # endif
  1339. )
  1340. {
  1341. if (msg_silent == 0)
  1342. redraw_later_clear();
  1343. need_wait_return = FALSE;
  1344. }
  1345. else
  1346. {
  1347. /*
  1348. * If we switch screens when starttermcap() is called, we really
  1349. * want to wait for "hit return to continue".
  1350. */
  1351. save_nwr = no_wait_return;
  1352. if (swapping_screen())
  1353. no_wait_return = FALSE;
  1354. # ifdef AMIGA
  1355. wait_return(term_console ? -1 : msg_silent == 0); /* see below */
  1356. # else
  1357. wait_return(msg_silent == 0);
  1358. # endif
  1359. no_wait_return = save_nwr;
  1360. }
  1361. #endif /* FEAT_GUI_W32 */
  1362. #ifdef MSWIN
  1363. if (!winstart) /* if winstart==TRUE, never stopped termcap! */
  1364. #endif
  1365. starttermcap(); /* start termcap if not done by wait_return() */
  1366. /*
  1367. * In an Amiga window redrawing is caused by asking the window size.
  1368. * If we got an interrupt this will not work. The chance that the
  1369. * window size is wrong is very small, but we need to redraw the
  1370. * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
  1371. * but it saves an extra redraw.
  1372. */
  1373. #ifdef AMIGA
  1374. if (skip_redraw) /* ':' hit in wait_return() */
  1375. {
  1376. if (msg_silent == 0)
  1377. redraw_later_clear();
  1378. }
  1379. else if (term_console)
  1380. {
  1381. OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
  1382. if (got_int && msg_silent == 0)
  1383. redraw_later_clear(); /* if got_int is TRUE, redraw needed */
  1384. else
  1385. must_redraw = 0; /* no extra redraw needed */
  1386. }
  1387. #endif
  1388. }
  1389. /* display any error messages now */
  1390. display_errors();
  1391. #ifdef FEAT_AUTOCMD
  1392. apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
  1393. #endif
  1394. }
  1395. /*
  1396. * Create a shell command from a command string, input redirection file and
  1397. * output redirection file.
  1398. * Returns an allocated string with the shell command, or NULL for failure.
  1399. */
  1400. char_u *
  1401. make_filter_cmd(cmd, itmp, otmp)
  1402. char_u *cmd; /* command */
  1403. char_u *itmp; /* NULL or name of input file */
  1404. char_u *otmp; /* NULL or name of output file */
  1405. {
  1406. char_u *buf;
  1407. long_u len;
  1408. len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
  1409. if (itmp != NULL)
  1410. len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
  1411. if (otmp != NULL)
  1412. len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
  1413. buf = lalloc(len, TRUE);
  1414. if (buf == NULL)
  1415. return NULL;
  1416. #if (defined(UNIX) && !defined(ARCHIE)) || defined(OS2)
  1417. /*
  1418. * Put braces around the command (for concatenated commands) when
  1419. * redirecting input and/or output.
  1420. */
  1421. if (itmp != NULL || otmp != NULL)
  1422. vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
  1423. else
  1424. STRCPY(buf, cmd);
  1425. if (itmp != NULL)
  1426. {
  1427. STRCAT(buf, " < ");
  1428. STRCAT(buf, itmp);
  1429. }
  1430. #else
  1431. /*
  1432. * for shells that don't understand braces around commands, at least allow
  1433. * the use of commands in a pipe.
  1434. */
  1435. STRCPY(buf, cmd);
  1436. if (itmp != NULL)
  1437. {
  1438. char_u *p;
  1439. /*
  1440. * If there is a pipe, we have to put the '<' in front of it.
  1441. * Don't do this when 'shellquote' is not empty, otherwise the
  1442. * redirection would be inside the quotes.
  1443. */
  1444. if (*p_shq == NUL)
  1445. {
  1446. p = vim_strchr(buf, '|');
  1447. if (p != NULL)
  1448. *p = NUL;
  1449. }
  1450. # ifdef RISCOS
  1451. STRCAT(buf, " { < "); /* Use RISC OS notation for input. */
  1452. STRCAT(buf, itmp);
  1453. STRCAT(buf, " } ");
  1454. # else
  1455. STRCAT(buf, " <"); /* " < " causes problems on Amiga */
  1456. STRCAT(buf, itmp);
  1457. # endif
  1458. if (*p_shq == NUL)
  1459. {
  1460. p = vim_strchr(cmd, '|');
  1461. if (p != NULL)
  1462. {
  1463. STRCAT(buf, " "); /* insert a space before the '|' for DOS */
  1464. STRCAT(buf, p);
  1465. }
  1466. }
  1467. }
  1468. #endif
  1469. if (otmp != NULL)
  1470. append_redir(buf, (int)len, p_srr, otmp);
  1471. return buf;
  1472. }
  1473. /*
  1474. * Append output redirection for file "fname" to the end of string buffer
  1475. * "buf[buflen]"
  1476. * Works with the 'shellredir' and 'shellpipe' options.
  1477. * The caller should make sure that there is enough room:
  1478. * STRLEN(opt) + STRLEN(fname) + 3
  1479. */
  1480. void
  1481. append_redir(buf, buflen, opt, fname)
  1482. char_u *buf;
  1483. int buflen;
  1484. char_u *opt;
  1485. char_u *fname;
  1486. {
  1487. char_u *p;
  1488. char_u *end;
  1489. end = buf + STRLEN(buf);
  1490. /* find "%s", skipping "%%" */
  1491. for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
  1492. if (p[1] == 's')
  1493. break;
  1494. if (p != NULL)
  1495. {
  1496. *end = ' '; /* not really needed? Not with sh, ksh or bash */
  1497. vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
  1498. (char *)opt, (char *)fname);
  1499. }
  1500. else
  1501. vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
  1502. #ifdef FEAT_QUICKFIX
  1503. # ifndef RISCOS
  1504. opt != p_sp ? " %s%s" :
  1505. # endif
  1506. " %s %s",
  1507. #else
  1508. # ifndef RISCOS
  1509. " %s%s", /* " > %s" causes problems on Amiga */
  1510. # else
  1511. " %s %s", /* But is needed for 'shellpipe' and RISC OS */
  1512. # endif
  1513. #endif
  1514. (char *)opt, (char *)fname);
  1515. }
  1516. #ifdef FEAT_VIMINFO
  1517. static int no_viminfo __ARGS((void));
  1518. static int viminfo_errcnt;
  1519. static int
  1520. no_viminfo()
  1521. {
  1522. /* "vim -i NONE" does not read or write a viminfo file */
  1523. return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
  1524. }
  1525. /*
  1526. * Report an error for reading a viminfo file.
  1527. * Count the number of errors. When there are more than 10, return TRUE.
  1528. */
  1529. int
  1530. viminfo_error(errnum, message, line)
  1531. char *errnum;
  1532. char *message;
  1533. char_u *line;
  1534. {
  1535. vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
  1536. errnum, message);
  1537. STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
  1538. if (IObuff[STRLEN(IObuff) - 1] == '\n')
  1539. IObuff[STRLEN(IObuff) - 1] = NUL;
  1540. emsg(IObuff);
  1541. if (++viminfo_errcnt >= 10)
  1542. {
  1543. EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
  1544. return TRUE;
  1545. }
  1546. return FALSE;
  1547. }
  1548. /*
  1549. * read_viminfo() -- Read the viminfo file. Registers etc. which are already
  1550. * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
  1551. */
  1552. int
  1553. read_viminfo(file, flags)
  1554. char_u *file; /* file name or NULL to use default name */
  1555. int flags; /* VIF_WANT_INFO et al. */
  1556. {
  1557. FILE *fp;
  1558. char_u *fname;
  1559. if (no_viminfo())
  1560. return FAIL;
  1561. fname = viminfo_filename(file); /* get file name in allocated buffer */
  1562. if (fname == NULL)
  1563. return FAIL;
  1564. fp = mch_fopen((char *)fname, READBIN);
  1565. if (p_verbose > 0)
  1566. {
  1567. verbose_enter();
  1568. smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
  1569. fname,
  1570. (flags & VIF_WANT_INFO) ? _(" info") : "",
  1571. (flags & VIF_WANT_MARKS) ? _(" marks") : "",
  1572. (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
  1573. fp == NULL ? _(" FAILED") : "");
  1574. verbose_leave();
  1575. }
  1576. vim_free(fname);
  1577. if (fp == NULL)
  1578. return FAIL;
  1579. viminfo_errcnt = 0;
  1580. do_viminfo(fp, NULL, flags);
  1581. fclose(fp);
  1582. return OK;
  1583. }
  1584. /*
  1585. * write_viminfo() -- Write the viminfo file. The old one is read in first so
  1586. * that effectively a merge of current info and old info is done. This allows
  1587. * multiple vims to run simultaneously, without losing any marks etc. If
  1588. * forceit is TRUE, then the old file is not read in, and only internal info is
  1589. * written to the file. -- webb
  1590. */
  1591. void
  1592. write_viminfo(file, forceit)
  1593. char_u *file;
  1594. int forceit;
  1595. {
  1596. char_u *fname;
  1597. FILE *fp_in = NULL; /* input viminfo file, if any */
  1598. FILE *fp_out = NULL; /* output viminfo file */
  1599. char_u *tempname = NULL; /* name of temp viminfo file */
  1600. struct stat st_new; /* mch_stat() of potential new file */
  1601. char_u *wp;
  1602. #if defined(UNIX) || defined(VMS)
  1603. mode_t umask_save;
  1604. #endif
  1605. #ifdef UNIX
  1606. int shortname = FALSE; /* use 8.3 file name */
  1607. struct stat st_old; /* mch_stat() of existing viminfo file */
  1608. #endif
  1609. #ifdef WIN3264
  1610. long perm = -1;
  1611. #endif
  1612. if (no_viminfo())
  1613. return;
  1614. fname = viminfo_filename(file); /* may set to default if NULL */
  1615. if (fname == NULL)
  1616. return;
  1617. fp_in = mch_fopen((char *)fname, READBIN);
  1618. if (fp_in == NULL)
  1619. {
  1620. /* if it does exist, but we can't read it, don't try writing */
  1621. if (mch_stat((char *)fname, &st_new) == 0)
  1622. goto end;
  1623. #if defined(UNIX) || defined(VMS)
  1624. /*
  1625. * For Unix we create the .viminfo non-accessible for others,
  1626. * because it may contain text from non-accessible documents.
  1627. */
  1628. umask_save = umask(077);
  1629. #endif
  1630. fp_out = mch_fopen((char *)fname, WRITEBIN);
  1631. #if defined(UNIX) || defined(VMS)
  1632. (void)umask(umask_save);
  1633. #endif
  1634. }
  1635. else
  1636. {
  1637. /*
  1638. * There is an existing viminfo file. Create a temporary file to
  1639. * write the new viminfo into, in the same directory as the
  1640. * existing viminfo file, which will be renamed later.
  1641. */
  1642. #ifdef UNIX
  1643. /*
  1644. * For Unix we check the owner of the file. It's not very nice to
  1645. * overwrite a user's viminfo file after a "su root", with a
  1646. * viminfo file that the user can't read.
  1647. */
  1648. st_old.st_dev = (dev_t)0;
  1649. st_old.st_ino = 0;
  1650. st_old.st_mode = 0600;
  1651. if (mch_stat((char *)fname, &st_old) == 0
  1652. && getuid() != ROOT_UID
  1653. && !(st_old.st_uid == getuid()
  1654. ? (st_old.st_mode & 0200)
  1655. : (st_old.st_gid == getgid()
  1656. ? (st_old.st_mode & 0020)
  1657. : (st_old.st_mode & 0002))))
  1658. {
  1659. int tt = msg_didany;
  1660. /* avoid a wait_return for this message, it's annoying */
  1661. EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
  1662. msg_didany = tt;
  1663. fclose(fp_in);
  1664. goto end;
  1665. }
  1666. #endif
  1667. #ifdef WIN3264
  1668. /* Get the file attributes of the existing viminfo file. */
  1669. perm = mch_getperm(fname);
  1670. #endif
  1671. /*
  1672. * Make tempname.
  1673. * May try twice: Once normal and once with shortname set, just in
  1674. * case somebody puts his viminfo file in an 8.3 filesystem.
  1675. */
  1676. for (;;)
  1677. {
  1678. tempname = buf_modname(
  1679. #ifdef UNIX
  1680. shortname,
  1681. #else
  1682. # ifdef SHORT_FNAME
  1683. TRUE,
  1684. # else
  1685. # ifdef FEAT_GUI_W32
  1686. gui_is_win32s(),
  1687. # else
  1688. FALSE,
  1689. # endif
  1690. # endif
  1691. #endif
  1692. fname,
  1693. #ifdef VMS
  1694. (char_u *)"-tmp",
  1695. #else
  1696. # ifdef RISCOS
  1697. (char_u *)"/tmp",
  1698. # else
  1699. (char_u *)".tmp",
  1700. # endif
  1701. #endif
  1702. FALSE);
  1703. if (tempname == NULL) /* out of memory */
  1704. break;
  1705. /*
  1706. * Check if tempfile already exists. Never overwrite an
  1707. * existing file!
  1708. */
  1709. if (mch_stat((char *)tempname, &st_new) == 0)
  1710. {
  1711. #ifdef UNIX
  1712. /*
  1713. * Check if tempfile is same as original file. May happen
  1714. * when modname() gave the same file back. E.g. silly
  1715. * link, or file name-length reached. Try again with
  1716. * shortname set.
  1717. */
  1718. if (!shortname && st_new.st_dev == st_old.st_dev
  1719. && st_new.st_ino == st_old.st_ino)
  1720. {
  1721. vim_free(tempname);
  1722. tempname = NULL;
  1723. shortname = TRUE;
  1724. continue;
  1725. }
  1726. #endif
  1727. /*
  1728. * Try another name. Change one character, just before
  1729. * the extension. This should also work for an 8.3
  1730. * file name, when after adding the extension it still is
  1731. * the same file as the original.
  1732. */
  1733. wp = tempname + STRLEN(tempname) - 5;
  1734. if (wp < gettail(tempname)) /* empty file name? */
  1735. wp = gettail(tempname);
  1736. for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
  1737. --*wp)
  1738. {
  1739. /*
  1740. * They all exist? Must be something wrong! Don't
  1741. * write the viminfo file then.
  1742. */
  1743. if (*wp == 'a')
  1744. {
  1745. vim_free(tempname);
  1746. tempname = NULL;
  1747. break;
  1748. }
  1749. }
  1750. }
  1751. break;
  1752. }
  1753. if (tempname != NULL)
  1754. {
  1755. #ifdef VMS
  1756. /* fdopen() fails for some reason */
  1757. umask_save = umask(077);
  1758. fp_out = mch_fopen((char *)tempname, WRITEBIN);
  1759. (void)umask(umask_save);
  1760. #else
  1761. int fd;
  1762. /* Use mch_open() to be able to use O_NOFOLLOW and set file
  1763. * protection:
  1764. * Unix: same as original file, but strip s-bit. Reset umask to
  1765. * avoid it getting in the way.
  1766. * Others: r&w for user only. */
  1767. # ifdef UNIX
  1768. umask_save = umask(0);
  1769. fd = mch_open((char *)tempname,
  1770. O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
  1771. (int)((st_old.st_mode & 0777) | 0600));
  1772. (void)umask(umask_save);
  1773. # else
  1774. fd = mch_open((char *)tempname,
  1775. O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
  1776. # endif
  1777. if (fd < 0)
  1778. fp_out = NULL;
  1779. else
  1780. fp_out = fdopen(fd, WRITEBIN);
  1781. #endif /* VMS */
  1782. /*
  1783. * If we can't create in the same directory, try creating a
  1784. * "normal" temp file.
  1785. */
  1786. if (fp_out == NULL)
  1787. {
  1788. vim_free(tempname);
  1789. if ((tempname = vim_tempname('o')) != NULL)
  1790. fp_out = mch_fopen((char *)tempname, WRITEBIN);
  1791. }
  1792. #if defined(UNIX) && defined(HAVE_FCHOWN)
  1793. /*
  1794. * Make sure the owner can read/write it. This only works for
  1795. * root.
  1796. */
  1797. if (fp_out != NULL)
  1798. ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
  1799. #endif
  1800. }
  1801. }
  1802. /*
  1803. * Check if the new viminfo file can be written to.
  1804. */
  1805. if (fp_out == NULL)
  1806. {
  1807. EMSG2(_("E138: Can't write viminfo file %s!"),
  1808. (fp_in == NULL || tempname == NULL) ? fname : tempname);
  1809. if (fp_in != NULL)
  1810. fclose(fp_in);
  1811. goto end;
  1812. }
  1813. if (p_verbose > 0)
  1814. {
  1815. verbose_enter();
  1816. smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
  1817. verbose_leave();
  1818. }
  1819. viminfo_errcnt = 0;
  1820. do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
  1821. fclose(fp_out); /* errors are ignored !? */
  1822. if (fp_in != NULL)
  1823. {
  1824. fclose(fp_in);
  1825. /*
  1826. * In case of an error keep the original viminfo file.
  1827. * Otherwise rename the newly written file.
  1828. */
  1829. if (viminfo_errcnt || vim_rename(tempname, fname) == -1)
  1830. mch_remove(tempname);
  1831. #ifdef WIN3264
  1832. /* If the viminfo file was hidden then also hide the new file. */
  1833. if (perm > 0 && (perm & FILE_ATTRIBUTE_HIDDEN))
  1834. mch_hide(fname);
  1835. #endif
  1836. }
  1837. end:
  1838. vim_free(fname);
  1839. vim_free(tempname);
  1840. }
  1841. /*
  1842. * Get the viminfo file name to use.
  1843. * If "file" is given and not empty, use it (has already been expanded by
  1844. * cmdline functions).
  1845. * Otherwise use "-i file_name", value from 'viminfo' or the default, and
  1846. * expand environment variables.
  1847. * Returns an allocated string. NULL when out of memory.
  1848. */
  1849. static char_u *
  1850. viminfo_filename(file)
  1851. char_u *file;
  1852. {
  1853. if (file == NULL || *file == NUL)
  1854. {
  1855. if (use_viminfo != NULL)
  1856. file = use_viminfo;
  1857. else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
  1858. {
  1859. #ifdef VIMINFO_FILE2
  1860. /* don't use $HOME when not defined (turned into "c:/"!). */
  1861. # ifdef VMS
  1862. if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
  1863. # else
  1864. if (mch_getenv((char_u *)"HOME") == NULL)
  1865. # endif
  1866. {
  1867. /* don't use $VIM when not available. */
  1868. expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
  1869. if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
  1870. file = (char_u *)VIMINFO_FILE2;
  1871. else
  1872. file = (char_u *)VIMINFO_FILE;
  1873. }
  1874. else
  1875. #endif
  1876. file = (char_u *)VIMINFO_FILE;
  1877. }
  1878. expand_env(file, NameBuff, MAXPATHL);
  1879. file = NameBuff;
  1880. }
  1881. return vim_strsave(file);
  1882. }
  1883. /*
  1884. * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
  1885. */
  1886. static void
  1887. do_viminfo(fp_in, fp_out, flags)
  1888. FILE *fp_in;
  1889. FILE *fp_out;
  1890. int flags;
  1891. {
  1892. int count = 0;
  1893. int eof = FALSE;
  1894. vir_T vir;
  1895. if ((vir.vir_line = alloc(LSIZE)) == NULL)
  1896. return;
  1897. vir.vir_fd = fp_in;
  1898. #ifdef FEAT_MBYTE
  1899. vir.vir_conv.vc_type = CONV_NONE;
  1900. #endif
  1901. if (fp_in != NULL)
  1902. {
  1903. if (flags & VIF_WANT_INFO)
  1904. eof = read_viminfo_up_to_marks(&vir,
  1905. flags & VIF_FORCEIT, fp_out != NULL);
  1906. else
  1907. /* Skip info, find start of marks */
  1908. while (!(eof = viminfo_readline(&vir))
  1909. && vir.vir_line[0] != '>')
  1910. ;
  1911. }
  1912. if (fp_out != NULL)
  1913. {
  1914. /* Write the info: */
  1915. fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
  1916. VIM_VERSION_MEDIUM);
  1917. fprintf(fp_out, _("# You may edit it if you're careful!\n\n"));
  1918. #ifdef FEAT_MBYTE
  1919. fprintf(fp_out, _("# Value of 'encoding' when this file was written\n"));
  1920. fprintf(fp_out, "*encoding=%s\n\n", p_enc);
  1921. #endif
  1922. write_viminfo_search_pattern(fp_out);
  1923. write_viminfo_sub_string(fp_out);
  1924. #ifdef FEAT_CMDHIST
  1925. write_viminfo_history(fp_out);
  1926. #endif
  1927. write_viminfo_registers(fp_out);
  1928. #ifdef FEAT_EVAL
  1929. write_viminfo_varlist(fp_out);
  1930. #endif
  1931. write_viminfo_filemarks(fp_out);
  1932. write_viminfo_bufferlist(fp_out);
  1933. count = write_viminfo_marks(fp_out);
  1934. }
  1935. if (fp_in != NULL
  1936. && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT)))
  1937. copy_viminfo_marks(&vir, fp_out, count, eof, flags);
  1938. vim_free(vir.vir_line);
  1939. #ifdef FEAT_MBYTE
  1940. if (vir.vir_conv.vc_type != CONV_NONE)
  1941. convert_setup(&vir.vir_conv, NULL, NULL);
  1942. #endif
  1943. }
  1944. /*
  1945. * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
  1946. * first part of the viminfo file which contains everything but the marks that
  1947. * are local to a file. Returns TRUE when end-of-file is reached. -- webb
  1948. */
  1949. static int
  1950. read_viminfo_up_to_marks(virp, forceit, writing)
  1951. vir_T *virp;
  1952. int forceit;
  1953. int writing;
  1954. {
  1955. int eof;
  1956. buf_T *buf;
  1957. #ifdef FEAT_CMDHIST
  1958. prepare_viminfo_history(forceit ? 9999 : 0);
  1959. #endif
  1960. eof = viminfo_readline(virp);
  1961. while (!eof && virp->vir_line[0] != '>')
  1962. {
  1963. switch (virp->vir_line[0])
  1964. {
  1965. /* Characters reserved for future expansion, ignored now */
  1966. case '+': /* "+40 /path/dir file", for running vim without args */
  1967. case '|': /* to be defined */
  1968. case '^': /* to be defined */
  1969. case '<': /* long line - ignored */
  1970. /* A comment or empty line. */
  1971. case NUL:
  1972. case '\r':
  1973. case '\n':
  1974. case '#':
  1975. eof = viminfo_readline(virp);
  1976. break;
  1977. case '*': /* "*encoding=value" */
  1978. eof = viminfo_encoding(virp);
  1979. break;
  1980. case '!': /* global variable */
  1981. #ifdef FEAT_EVAL
  1982. eof = read_viminfo_varlist(virp, writing);
  1983. #else
  1984. eof = viminfo_readline(virp);
  1985. #endif
  1986. break;
  1987. case '%': /* entry for buffer list */
  1988. eof = read_viminfo_bufferlist(virp, writing);
  1989. break;
  1990. case '"':
  1991. eof = read_viminfo_register(virp, forceit);
  1992. break;
  1993. case '/': /* Search string */
  1994. case '&': /* Substitute search string */
  1995. case '~': /* Last search string, followed by '/' or '&' */
  1996. eof = read_viminfo_search_pattern(virp, forceit);
  1997. break;
  1998. case '$':
  1999. eof = read_viminfo_sub_string(virp, forceit);
  2000. break;
  2001. case ':':
  2002. case '?':
  2003. case '=':
  2004. case '@':
  2005. #ifdef FEAT_CMDHIST
  2006. eof = read_viminfo_history(virp);
  2007. #else
  2008. eof = viminfo_readline(virp);
  2009. #endif
  2010. break;
  2011. case '-':
  2012. case '\'':
  2013. eof = read_viminfo_filemark(virp, forceit);
  2014. break;
  2015. default:
  2016. if (viminfo_error("E575: ", _("Illegal starting char"),
  2017. virp->vir_line))
  2018. eof = TRUE;
  2019. else
  2020. eof = viminfo_readline(virp);
  2021. break;
  2022. }
  2023. }
  2024. #ifdef FEAT_CMDHIST
  2025. /* Finish reading history items. */
  2026. finish_viminfo_history();
  2027. #endif
  2028. /* Change file names to buffer numbers for fmarks. */
  2029. for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  2030. fmarks_check_names(buf);
  2031. return eof;
  2032. }
  2033. /*
  2034. * Compare the 'encoding' value in the viminfo file with the current value of
  2035. * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
  2036. * conversion of text with iconv() in viminfo_readstring().
  2037. */
  2038. static int
  2039. viminfo_encoding(virp)
  2040. vir_T *virp;
  2041. {
  2042. #ifdef FEAT_MBYTE
  2043. char_u *p;
  2044. int i;
  2045. if (get_viminfo_parameter('c') != 0)
  2046. {
  2047. p = vim_strchr(virp->vir_line, '=');
  2048. if (p != NULL)
  2049. {
  2050. /* remove trailing newline */
  2051. ++p;
  2052. for (i = 0; vim_isprintc(p[i]); ++i)
  2053. ;
  2054. p[i] = NUL;
  2055. convert_setup(&virp->vir_conv, p, p_enc);
  2056. }
  2057. }
  2058. #endif
  2059. return viminfo_readline(virp);
  2060. }
  2061. /*
  2062. * Read a line from the viminfo file.
  2063. * Returns TRUE for end-of-file;
  2064. */
  2065. int
  2066. viminfo_readline(virp)
  2067. vir_T *virp;
  2068. {
  2069. return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
  2070. }
  2071. /*
  2072. * check string read from viminfo file
  2073. * remove '\n' at the end of the line
  2074. * - replace CTRL-V CTRL-V with CTRL-V
  2075. * - replace CTRL-V 'n' with '\n'
  2076. *
  2077. * Check for a long line as written by viminfo_writestring().
  2078. *
  2079. * Return the string in allocated memory (NULL when out of memory).
  2080. */
  2081. char_u *
  2082. viminfo_readstring(virp, off, convert)
  2083. vir_T *virp;
  2084. int off; /* offset for virp->vir_line */
  2085. int convert UNUSED; /* convert the string */
  2086. {
  2087. char_u *retval;
  2088. char_u *s, *d;
  2089. long len;
  2090. if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
  2091. {
  2092. len = atol((char *)virp->vir_line + off + 1);
  2093. retval = lalloc(len, TRUE);
  2094. if (retval == NULL)
  2095. {
  2096. /* Line too long? File messed up? Skip next line. */
  2097. (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
  2098. return NULL;
  2099. }
  2100. (void)vim_fgets(retval, (int)len, virp->vir_fd);
  2101. s = retval + 1; /* Skip the leading '<' */
  2102. }
  2103. else
  2104. {
  2105. retval = vim_strsave(virp->vir_line + off);
  2106. if (retval == NULL)
  2107. return NULL;
  2108. s = retval;
  2109. }
  2110. /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */
  2111. d = retval;
  2112. while (*s != NUL && *s != '\n')
  2113. {
  2114. if (s[0] == Ctrl_V && s[1] != NUL)
  2115. {
  2116. if (s[1] == 'n')
  2117. *d++ = '\n';
  2118. else
  2119. *d++ = Ctrl_V;
  2120. s += 2;
  2121. }
  2122. else
  2123. *d++ = *s++;
  2124. }
  2125. *d = NUL;
  2126. #ifdef FEAT_MBYTE
  2127. if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
  2128. {
  2129. d = string_convert(&virp->vir_conv, retval, NULL);
  2130. if (d != NULL)
  2131. {
  2132. vim_free(retval);
  2133. retval = d;
  2134. }
  2135. }
  2136. #endif
  2137. return retval;
  2138. }
  2139. /*
  2140. * write string to viminfo file
  2141. * - replace CTRL-V with CTRL-V CTRL-V
  2142. * - replace '\n' with CTRL-V 'n'
  2143. * - add a '\n' at the end
  2144. *
  2145. * For a long line:
  2146. * - write " CTRL-V <length> \n " in first line
  2147. * - write " < <string> \n " in second line
  2148. */
  2149. void
  2150. viminfo_writestring(fd, p)
  2151. FILE *fd;
  2152. char_u *p;
  2153. {
  2154. int c;
  2155. char_u *s;
  2156. int len = 0;
  2157. for (s = p; *s != NUL; ++s)
  2158. {
  2159. if (*s == Ctrl_V || *s == '\n')
  2160. ++len;
  2161. ++len;
  2162. }
  2163. /* If the string will be too long, write its length and put it in the next
  2164. * line. Take into account that some room is needed for what comes before
  2165. * the string (e.g., variable name). Add something to the length for the
  2166. * '<', NL and trailing NUL. */
  2167. if (len > LSIZE / 2)
  2168. fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3);
  2169. while ((c = *p++) != NUL)
  2170. {
  2171. if (c == Ctrl_V || c == '\n')
  2172. {
  2173. putc(Ctrl_V, fd);
  2174. if (c == '\n')
  2175. c = 'n';
  2176. }
  2177. putc(c, fd);
  2178. }
  2179. putc('\n', fd);
  2180. }
  2181. #endif /* FEAT_VIMINFO */
  2182. /*
  2183. * Implementation of ":fixdel", also used by get_stty().
  2184. * <BS> resulting <Del>
  2185. * ^? ^H
  2186. * not ^? ^?
  2187. */
  2188. void
  2189. do_fixdel(eap)
  2190. exarg_T *eap UNUSED;
  2191. {
  2192. char_u *p;
  2193. p = find_termcode((char_u *)"kb");
  2194. add_termcode((char_u *)"kD", p != NULL
  2195. && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE);
  2196. }
  2197. void
  2198. print_line_no_prefix(lnum, use_number, list)
  2199. linenr_T lnum;
  2200. int use_number;
  2201. int list;
  2202. {
  2203. char_u numbuf[30];
  2204. if (curwin->w_p_nu || use_number)
  2205. {
  2206. vim_snprintf((char *)numbuf, sizeof(numbuf),
  2207. "%*ld ", number_width(curwin), (long)lnum);
  2208. msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */
  2209. }
  2210. msg_prt_line(ml_get(lnum), list);
  2211. }
  2212. /*
  2213. * Print a text line. Also in silent mode ("ex -s").
  2214. */
  2215. void
  2216. print_line(lnum, use_number, list)
  2217. linenr_T lnum;
  2218. int use_number;
  2219. int list;
  2220. {
  2221. int save_silent = silent_mode;
  2222. msg_start();
  2223. silent_mode = FALSE;
  2224. info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
  2225. print_line_no_prefix(lnum, use_number, list);
  2226. if (save_silent)
  2227. {
  2228. msg_putchar('\n');
  2229. cursor_on(); /* msg_start() switches it off */
  2230. out_flush();
  2231. silent_mode = save_silent;
  2232. }
  2233. info_message = FALSE;
  2234. }
  2235. /*
  2236. * ":file[!] [fname]".
  2237. */
  2238. void
  2239. ex_file(eap)
  2240. exarg_T *eap;
  2241. {
  2242. char_u *fname, *sfname, *xfname;
  2243. buf_T *buf;
  2244. /* ":0file" removes the file name. Check for illegal uses ":3file",
  2245. * "0file name", etc. */
  2246. if (eap->addr_count > 0
  2247. && (*eap->arg != NUL
  2248. || eap->line2 > 0
  2249. || eap->addr_count > 1))
  2250. {
  2251. EMSG(_(e_invarg));
  2252. return;
  2253. }
  2254. if (*eap->arg != NUL || eap->addr_count == 1)
  2255. {
  2256. #ifdef FEAT_AUTOCMD
  2257. buf = curbuf;
  2258. apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
  2259. /* buffer changed, don't change name now */
  2260. if (buf != curbuf)
  2261. return;
  2262. # ifdef FEAT_EVAL
  2263. if (aborting()) /* autocmds may abort script processing */
  2264. return;
  2265. # endif
  2266. #endif
  2267. /*
  2268. * The name of the current buffer will be changed.
  2269. * A new (unlisted) buffer entry needs to be made to hold the old file
  2270. * name, which will become the alternate file name.
  2271. * But don't set the alternate file name if the buffer didn't have a
  2272. * name.
  2273. */
  2274. fname = curbuf->b_ffname;
  2275. sfname = curbuf->b_sfname;
  2276. xfname = curbuf->b_fname;
  2277. curbuf->b_ffname = NULL;
  2278. curbuf->b_sfname = NULL;
  2279. if (setfname(curbuf, eap->arg, NULL, TRUE) == FAIL)
  2280. {
  2281. curbuf->b_ffname = fname;
  2282. curbuf->b_sfname = sfname;
  2283. return;
  2284. }
  2285. curbuf->b_flags |= BF_NOTEDITED;
  2286. if (xfname != NULL && *xfname != NUL)
  2287. {
  2288. buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0);
  2289. if (buf != NULL && !cmdmod.keepalt)
  2290. curwin->w_alt_fnum = buf->b_fnum;
  2291. }
  2292. vim_free(fname);
  2293. vim_free(sfname);
  2294. #ifdef FEAT_AUTOCMD
  2295. apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
  2296. #endif
  2297. /* Change directories when the 'acd' option is set. */
  2298. DO_AUTOCHDIR
  2299. }
  2300. /* print full file name if :cd used */
  2301. fileinfo(FALSE, FALSE, eap->forceit);
  2302. }
  2303. /*
  2304. * ":update".
  2305. */
  2306. void
  2307. ex_update(eap)
  2308. exarg_T *eap;
  2309. {
  2310. if (curbufIsChanged())
  2311. (void)do_write(eap);
  2312. }
  2313. /*
  2314. * ":write" and ":saveas".
  2315. */
  2316. void
  2317. ex_write(eap)
  2318. exarg_T *eap;
  2319. {
  2320. if (eap->usefilter) /* input lines to shell command */
  2321. do_bang(1, eap, FALSE, TRUE, FALSE);
  2322. else
  2323. (void)do_write(eap);
  2324. }
  2325. /*
  2326. * write current buffer to file 'eap->arg'
  2327. * if 'eap->append' is TRUE, append to the file
  2328. *
  2329. * if *eap->arg == NUL write to current file
  2330. *
  2331. * return FAIL for failure, OK otherwise
  2332. */
  2333. int
  2334. do_write(eap)
  2335. exarg_T *eap;
  2336. {
  2337. int other;
  2338. char_u *fname = NULL; /* init to shut up gcc */
  2339. char_u *ffname;
  2340. int retval = FAIL;
  2341. char_u *free_fname = NULL;
  2342. #ifdef FEAT_BROWSE
  2343. char_u *browse_file = NULL;
  2344. #endif
  2345. buf_T *alt_buf = NULL;
  2346. if (not_writing()) /* check 'write' option */
  2347. return FAIL;
  2348. ffname = eap->arg;
  2349. #ifdef FEAT_BROWSE
  2350. if (cmdmod.browse)
  2351. {
  2352. browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname,
  2353. NULL, NULL, NULL, curbuf);
  2354. if (browse_file == NULL)
  2355. goto theend;
  2356. ffname = browse_file;
  2357. }
  2358. #endif
  2359. if (*ffname == NUL)
  2360. {
  2361. if (eap->cmdidx == CMD_saveas)
  2362. {
  2363. EMSG(_(e_argreq));
  2364. goto theend;
  2365. }
  2366. other = FALSE;
  2367. }
  2368. else
  2369. {
  2370. fname = ffname;
  2371. free_fname = fix_fname(ffname);
  2372. /*
  2373. * When out-of-memory, keep unexpanded file name, because we MUST be
  2374. * able to write the file in this situation.
  2375. */
  2376. if (free_fname != NULL)
  2377. ffname = free_fname;
  2378. other = otherfile(ffname);
  2379. }
  2380. /*
  2381. * If we have a new file, put its name in the list of alternate file names.
  2382. */
  2383. if (other)
  2384. {
  2385. if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
  2386. || eap->cmdidx == CMD_saveas)
  2387. alt_buf = setaltfname(ffname, fname, (linenr_T)1);
  2388. else
  2389. alt_buf = buflist_findname(ffname);
  2390. if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL)
  2391. {
  2392. /* Overwriting a file that is loaded in another buffer is not a
  2393. * good idea. */
  2394. EMSG(_(e_bufloaded));
  2395. goto theend;
  2396. }
  2397. }
  2398. /*
  2399. * Writing to the current file is not allowed in readonly mode
  2400. * and a file name is required.
  2401. * "nofile" and "nowrite" buffers cannot be written implicitly either.
  2402. */
  2403. if (!other && (
  2404. #ifdef FEAT_QUICKFIX
  2405. bt_dontwrite_msg(curbuf) ||
  2406. #endif
  2407. check_fname() == FAIL || check_readonly(&eap->forceit, curbuf)))
  2408. goto theend;
  2409. if (!other)
  2410. {
  2411. ffname = curbuf->b_ffname;
  2412. fname = curbuf->b_fname;
  2413. /*
  2414. * Not writing the whole file is only allowed with '!'.
  2415. */
  2416. if ( (eap->line1 != 1
  2417. || eap->line2 != curbuf->b_ml.ml_line_count)
  2418. && !eap->forceit
  2419. && !eap->append
  2420. && !p_wa)
  2421. {
  2422. #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
  2423. if (p_confirm || cmdmod.confirm)
  2424. {
  2425. if (vim_dialog_yesno(VIM_QUESTION, NULL,
  2426. (char_u *)_("Write partial file?"), 2) != VIM_YES)
  2427. goto theend;
  2428. eap->forceit = TRUE;
  2429. }
  2430. else
  2431. #endif
  2432. {
  2433. EMSG(_("E140: Use ! to write partial buffer"));
  2434. goto theend;
  2435. }
  2436. }
  2437. }
  2438. if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
  2439. {
  2440. if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
  2441. {
  2442. #ifdef FEAT_AUTOCMD
  2443. buf_T *was_curbuf = curbuf;
  2444. apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
  2445. apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
  2446. # ifdef FEAT_EVAL
  2447. if (curbuf != was_curbuf || aborting())
  2448. # else
  2449. if (curbuf != was_curbuf)
  2450. # endif
  2451. {
  2452. /* buffer changed, don't change name now */
  2453. retval = FAIL;
  2454. goto theend;
  2455. }
  2456. #endif
  2457. /* Exchange the file names for the current and the alternate
  2458. * buffer. This makes it look like we are now editing the buffer
  2459. * under the new name. Must be done before buf_write(), because
  2460. * if there is no file name and 'cpo' contains 'F', it will set
  2461. * the file name. */
  2462. fname = alt_buf->b_fname;
  2463. alt_buf->b_fname = curbuf->b_fname;
  2464. curbuf->b_fname = fname;
  2465. fname = alt_buf->b_ffname;
  2466. alt_buf->b_ffname = curbuf->b_ffname;
  2467. curbuf->b_ffname = fname;
  2468. fname = alt_buf->b_sfname;
  2469. alt_buf->b_sfname = curbuf->b_sfname;
  2470. curbuf->b_sfname = fname;
  2471. buf_name_changed(curbuf);
  2472. #ifdef FEAT_AUTOCMD
  2473. apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
  2474. apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
  2475. if (!alt_buf->b_p_bl)
  2476. {
  2477. alt_buf->b_p_bl = TRUE;
  2478. apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
  2479. }
  2480. # ifdef FEAT_EVAL
  2481. if (curbuf != was_curb