PageRenderTime 33ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/vim7.0.0/src/ex_cmds.c

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