PageRenderTime 66ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/vim7.1.330/src/ex_cmds.c

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