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

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

#
C | 2685 lines | 2460 code | 72 blank | 153 comment | 228 complexity | 0ad278ed4269fab95116e2829c6a1238 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /* vi:set ts=8 sts=4 sw=4:
  2. *
  3. * VIM - Vi IMproved by Bram Moolenaar
  4. *
  5. * Do ":help uganda" in Vim to read copying and usage conditions.
  6. * Do ":help credits" in Vim to see a list of people who contributed.
  7. * See README.txt for an overview of the Vim source code.
  8. */
  9. /*
  10. * ex_cmds.c: some functions for command line commands
  11. */
  12. #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
  13. # include "vimio.h" /* for mch_open(), must be before vim.h */
  14. #endif
  15. #include "vim.h"
  16. #include "version.h"
  17. #ifdef FEAT_EX_EXTRA
  18. static int linelen __ARGS((int *has_tab));
  19. #endif
  20. static void do_filter __ARGS((linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out));
  21. #ifdef FEAT_VIMINFO
  22. static char_u *viminfo_filename __ARGS((char_u *));
  23. static void do_viminfo __ARGS((FILE *fp_in, FILE *fp_out, int flags));
  24. static int viminfo_encoding __ARGS((vir_T *virp));
  25. static int read_viminfo_up_to_marks __ARGS((vir_T *virp, int forceit, int writing));
  26. #endif
  27. static int check_overwrite __ARGS((exarg_T *eap, buf_T *buf, char_u *fname, char_u *ffname, int other));
  28. static int check_readonly __ARGS((int *forceit, buf_T *buf));
  29. #ifdef FEAT_AUTOCMD
  30. static void delbuf_msg __ARGS((char_u *name));
  31. #endif
  32. static int
  33. #ifdef __BORLANDC__
  34. _RTLENTRYF
  35. #endif
  36. help_compare __ARGS((const void *s1, const void *s2));
  37. /*
  38. * ":ascii" and "ga".
  39. */
  40. void
  41. do_ascii(eap)
  42. exarg_T *eap UNUSED;
  43. {
  44. int c;
  45. int cval;
  46. char buf1[20];
  47. char buf2[20];
  48. char_u buf3[7];
  49. #ifdef FEAT_MBYTE
  50. int cc[MAX_MCO];
  51. int ci = 0;
  52. int len;
  53. if (enc_utf8)
  54. c = utfc_ptr2char(ml_get_cursor(), cc);
  55. else
  56. #endif
  57. c = gchar_cursor();
  58. if (c == NUL)
  59. {
  60. MSG("NUL");
  61. return;
  62. }
  63. #ifdef FEAT_MBYTE
  64. IObuff[0] = NUL;
  65. if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
  66. #endif
  67. {
  68. if (c == NL) /* NUL is stored as NL */
  69. c = NUL;
  70. if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
  71. cval = NL; /* NL is stored as CR */
  72. else
  73. cval = c;
  74. if (vim_isprintc_strict(c) && (c < ' '
  75. #ifndef EBCDIC
  76. || c > '~'
  77. #endif
  78. ))
  79. {
  80. transchar_nonprint(buf3, c);
  81. vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
  82. }
  83. else
  84. buf1[0] = NUL;
  85. #ifndef EBCDIC
  86. if (c >= 0x80)
  87. vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
  88. (char *)transchar(c & 0x7f));
  89. else
  90. #endif
  91. buf2[0] = NUL;
  92. vim_snprintf((char *)IObuff, IOSIZE,
  93. _("<%s>%s%s %d, Hex %02x, Octal %03o"),
  94. transchar(c), buf1, buf2, cval, cval, cval);
  95. #ifdef FEAT_MBYTE
  96. if (enc_utf8)
  97. c = cc[ci++];
  98. else
  99. c = 0;
  100. #endif
  101. }
  102. #ifdef FEAT_MBYTE
  103. /* Repeat for combining characters. */
  104. while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
  105. {
  106. len = (int)STRLEN(IObuff);
  107. /* This assumes every multi-byte char is printable... */
  108. if (len > 0)
  109. IObuff[len++] = ' ';
  110. IObuff[len++] = '<';
  111. if (enc_utf8 && utf_iscomposing(c)
  112. # ifdef USE_GUI
  113. && !gui.in_use
  114. # endif
  115. )
  116. IObuff[len++] = ' '; /* draw composing char on top of a space */
  117. len += (*mb_char2bytes)(c, IObuff + len);
  118. vim_snprintf((char *)IObuff + len, IOSIZE - len,
  119. c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
  120. : _("> %d, Hex %08x, Octal %o"), c, c, c);
  121. if (ci == MAX_MCO)
  122. break;
  123. if (enc_utf8)
  124. c = cc[ci++];
  125. else
  126. c = 0;
  127. }
  128. #endif
  129. msg(IObuff);
  130. }
  131. #if defined(FEAT_EX_EXTRA) || defined(PROTO)
  132. /*
  133. * ":left", ":center" and ":right": align text.
  134. */
  135. void
  136. ex_align(eap)
  137. exarg_T *eap;
  138. {
  139. pos_T save_curpos;
  140. int len;
  141. int indent = 0;
  142. int new_indent;
  143. int has_tab;
  144. int width;
  145. #ifdef FEAT_RIGHTLEFT
  146. if (curwin->w_p_rl)
  147. {
  148. /* switch left and right aligning */
  149. if (eap->cmdidx == CMD_right)
  150. eap->cmdidx = CMD_left;
  151. else if (eap->cmdidx == CMD_left)
  152. eap->cmdidx = CMD_right;
  153. }
  154. #endif
  155. width = atoi((char *)eap->arg);
  156. save_curpos = curwin->w_cursor;
  157. if (eap->cmdidx == CMD_left) /* width is used for new indent */
  158. {
  159. if (width >= 0)
  160. indent = width;
  161. }
  162. else
  163. {
  164. /*
  165. * if 'textwidth' set, use it
  166. * else if 'wrapmargin' set, use it
  167. * if invalid value, use 80
  168. */
  169. if (width <= 0)
  170. width = curbuf->b_p_tw;
  171. if (width == 0 && curbuf->b_p_wm > 0)
  172. width = W_WIDTH(curwin) - curbuf->b_p_wm;
  173. if (width <= 0)
  174. width = 80;
  175. }
  176. if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
  177. return;
  178. for (curwin->w_cursor.lnum = eap->line1;
  179. curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
  180. {
  181. if (eap->cmdidx == CMD_left) /* left align */
  182. new_indent = indent;
  183. else
  184. {
  185. has_tab = FALSE; /* avoid uninit warnings */
  186. len = linelen(eap->cmdidx == CMD_right ? &has_tab
  187. : NULL) - get_indent();
  188. if (len <= 0) /* skip blank lines */
  189. continue;
  190. if (eap->cmdidx == CMD_center)
  191. new_indent = (width - len) / 2;
  192. else
  193. {
  194. new_indent = width - len; /* right align */
  195. /*
  196. * Make sure that embedded TABs don't make the text go too far
  197. * to the right.
  198. */
  199. if (has_tab)
  200. while (new_indent > 0)
  201. {
  202. (void)set_indent(new_indent, 0);
  203. if (linelen(NULL) <= width)
  204. {
  205. /*
  206. * Now try to move the line as much as possible to
  207. * the right. Stop when it moves too far.
  208. */
  209. do
  210. (void)set_indent(++new_indent, 0);
  211. while (linelen(NULL) <= width);
  212. --new_indent;
  213. break;
  214. }
  215. --new_indent;
  216. }
  217. }
  218. }
  219. if (new_indent < 0)
  220. new_indent = 0;
  221. (void)set_indent(new_indent, 0); /* set indent */
  222. }
  223. changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
  224. curwin->w_cursor = save_curpos;
  225. beginline(BL_WHITE | BL_FIX);
  226. }
  227. /*
  228. * Get the length of the current line, excluding trailing white space.
  229. */
  230. static int
  231. linelen(has_tab)
  232. int *has_tab;
  233. {
  234. char_u *line;
  235. char_u *first;
  236. char_u *last;
  237. int save;
  238. int len;
  239. /* find the first non-blank character */
  240. line = ml_get_curline();
  241. first = skipwhite(line);
  242. /* find the character after the last non-blank character */
  243. for (last = first + STRLEN(first);
  244. last > first && vim_iswhite(last[-1]); --last)
  245. ;
  246. save = *last;
  247. *last = NUL;
  248. len = linetabsize(line); /* get line length */
  249. if (has_tab != NULL) /* check for embedded TAB */
  250. *has_tab = (vim_strrchr(first, TAB) != NULL);
  251. *last = save;
  252. return len;
  253. }
  254. /* Buffer for two lines used during sorting. They are allocated to
  255. * contain the longest line being sorted. */
  256. static char_u *sortbuf1;
  257. static char_u *sortbuf2;
  258. static int sort_ic; /* ignore case */
  259. static int sort_nr; /* sort on number */
  260. static int sort_rx; /* sort on regex instead of skipping it */
  261. static int sort_abort; /* flag to indicate if sorting has been interrupted */
  262. /* Struct to store info to be sorted. */
  263. typedef struct
  264. {
  265. linenr_T lnum; /* line number */
  266. long start_col_nr; /* starting column number or number */
  267. long end_col_nr; /* ending column number */
  268. } sorti_T;
  269. static int
  270. #ifdef __BORLANDC__
  271. _RTLENTRYF
  272. #endif
  273. sort_compare __ARGS((const void *s1, const void *s2));
  274. static int
  275. #ifdef __BORLANDC__
  276. _RTLENTRYF
  277. #endif
  278. sort_compare(s1, s2)
  279. const void *s1;
  280. const void *s2;
  281. {
  282. sorti_T l1 = *(sorti_T *)s1;
  283. sorti_T l2 = *(sorti_T *)s2;
  284. int result = 0;
  285. /* If the user interrupts, there's no way to stop qsort() immediately, but
  286. * if we return 0 every time, qsort will assume it's done sorting and
  287. * exit. */
  288. if (sort_abort)
  289. return 0;
  290. fast_breakcheck();
  291. if (got_int)
  292. sort_abort = TRUE;
  293. /* When sorting numbers "start_col_nr" is the number, not the column
  294. * number. */
  295. if (sort_nr)
  296. result = l1.start_col_nr - l2.start_col_nr;
  297. else
  298. {
  299. /* We need to copy one line into "sortbuf1", because there is no
  300. * guarantee that the first pointer becomes invalid when obtaining the
  301. * second one. */
  302. STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.start_col_nr,
  303. l1.end_col_nr - l1.start_col_nr + 1);
  304. sortbuf1[l1.end_col_nr - l1.start_col_nr] = 0;
  305. STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.start_col_nr,
  306. l2.end_col_nr - l2.start_col_nr + 1);
  307. sortbuf2[l2.end_col_nr - l2.start_col_nr] = 0;
  308. result = sort_ic ? STRICMP(sortbuf1, sortbuf2)
  309. : STRCMP(sortbuf1, sortbuf2);
  310. }
  311. /* If two lines have the same value, preserve the original line order. */
  312. if (result == 0)
  313. return (int)(l1.lnum - l2.lnum);
  314. return result;
  315. }
  316. /*
  317. * ":sort".
  318. */
  319. void
  320. ex_sort(eap)
  321. exarg_T *eap;
  322. {
  323. regmatch_T regmatch;
  324. int len;
  325. linenr_T lnum;
  326. long maxlen = 0;
  327. sorti_T *nrs;
  328. size_t count = (size_t)(eap->line2 - eap->line1 + 1);
  329. size_t i;
  330. char_u *p;
  331. char_u *s;
  332. char_u *s2;
  333. char_u c; /* temporary character storage */
  334. int unique = FALSE;
  335. long deleted;
  336. colnr_T start_col;
  337. colnr_T end_col;
  338. int sort_oct; /* sort on octal number */
  339. int sort_hex; /* sort on hex number */
  340. /* Sorting one line is really quick! */
  341. if (count <= 1)
  342. return;
  343. if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
  344. return;
  345. sortbuf1 = NULL;
  346. sortbuf2 = NULL;
  347. regmatch.regprog = NULL;
  348. nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE);
  349. if (nrs == NULL)
  350. goto sortend;
  351. sort_abort = sort_ic = sort_rx = sort_nr = sort_oct = sort_hex = 0;
  352. for (p = eap->arg; *p != NUL; ++p)
  353. {
  354. if (vim_iswhite(*p))
  355. ;
  356. else if (*p == 'i')
  357. sort_ic = TRUE;
  358. else if (*p == 'r')
  359. sort_rx = TRUE;
  360. else if (*p == 'n')
  361. sort_nr = 2;
  362. else if (*p == 'o')
  363. sort_oct = 2;
  364. else if (*p == 'x')
  365. sort_hex = 2;
  366. else if (*p == 'u')
  367. unique = TRUE;
  368. else if (*p == '"') /* comment start */
  369. break;
  370. else if (check_nextcmd(p) != NULL)
  371. {
  372. eap->nextcmd = check_nextcmd(p);
  373. break;
  374. }
  375. else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
  376. {
  377. s = skip_regexp(p + 1, *p, TRUE, NULL);
  378. if (*s != *p)
  379. {
  380. EMSG(_(e_invalpat));
  381. goto sortend;
  382. }
  383. *s = NUL;
  384. /* Use last search pattern if sort pattern is empty. */
  385. if (s == p + 1 && last_search_pat() != NULL)
  386. regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
  387. else
  388. regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
  389. if (regmatch.regprog == NULL)
  390. goto sortend;
  391. p = s; /* continue after the regexp */
  392. regmatch.rm_ic = p_ic;
  393. }
  394. else
  395. {
  396. EMSG2(_(e_invarg2), p);
  397. goto sortend;
  398. }
  399. }
  400. /* Can only have one of 'n', 'o' and 'x'. */
  401. if (sort_nr + sort_oct + sort_hex > 2)
  402. {
  403. EMSG(_(e_invarg));
  404. goto sortend;
  405. }
  406. /* From here on "sort_nr" is used as a flag for any number sorting. */
  407. sort_nr += sort_oct + sort_hex;
  408. /*
  409. * Make an array with all line numbers. This avoids having to copy all
  410. * the lines into allocated memory.
  411. * When sorting on strings "start_col_nr" is the offset in the line, for
  412. * numbers sorting it's the number to sort on. This means the pattern
  413. * matching and number conversion only has to be done once per line.
  414. * Also get the longest line length for allocating "sortbuf".
  415. */
  416. for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
  417. {
  418. s = ml_get(lnum);
  419. len = (int)STRLEN(s);
  420. if (maxlen < len)
  421. maxlen = len;
  422. start_col = 0;
  423. end_col = len;
  424. if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0))
  425. {
  426. if (sort_rx)
  427. {
  428. start_col = (colnr_T)(regmatch.startp[0] - s);
  429. end_col = (colnr_T)(regmatch.endp[0] - s);
  430. }
  431. else
  432. start_col = (colnr_T)(regmatch.endp[0] - s);
  433. }
  434. else
  435. if (regmatch.regprog != NULL)
  436. end_col = 0;
  437. if (sort_nr)
  438. {
  439. /* Make sure vim_str2nr doesn't read any digits past the end
  440. * of the match, by temporarily terminating the string there */
  441. s2 = s + end_col;
  442. c = *s2;
  443. (*s2) = 0;
  444. /* Sorting on number: Store the number itself. */
  445. p = s + start_col;
  446. if (sort_hex)
  447. s = skiptohex(p);
  448. else
  449. s = skiptodigit(p);
  450. if (s > p && s[-1] == '-')
  451. --s; /* include preceding negative sign */
  452. vim_str2nr(s, NULL, NULL, sort_oct, sort_hex,
  453. &nrs[lnum - eap->line1].start_col_nr, NULL);
  454. (*s2) = c;
  455. }
  456. else
  457. {
  458. /* Store the column to sort at. */
  459. nrs[lnum - eap->line1].start_col_nr = start_col;
  460. nrs[lnum - eap->line1].end_col_nr = end_col;
  461. }
  462. nrs[lnum - eap->line1].lnum = lnum;
  463. if (regmatch.regprog != NULL)
  464. fast_breakcheck();
  465. if (got_int)
  466. goto sortend;
  467. }
  468. /* Allocate a buffer that can hold the longest line. */
  469. sortbuf1 = alloc((unsigned)maxlen + 1);
  470. if (sortbuf1 == NULL)
  471. goto sortend;
  472. sortbuf2 = alloc((unsigned)maxlen + 1);
  473. if (sortbuf2 == NULL)
  474. goto sortend;
  475. /* Sort the array of line numbers. Note: can't be interrupted! */
  476. qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
  477. if (sort_abort)
  478. goto sortend;
  479. /* Insert the lines in the sorted order below the last one. */
  480. lnum = eap->line2;
  481. for (i = 0; i < count; ++i)
  482. {
  483. s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
  484. if (!unique || i == 0
  485. || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
  486. {
  487. if (ml_append(lnum++, s, (colnr_T)0, FALSE) == FAIL)
  488. break;
  489. if (unique)
  490. STRCPY(sortbuf1, s);
  491. }
  492. fast_breakcheck();
  493. if (got_int)
  494. goto sortend;
  495. }
  496. /* delete the original lines if appending worked */
  497. if (i == count)
  498. for (i = 0; i < count; ++i)
  499. ml_delete(eap->line1, FALSE);
  500. else
  501. count = 0;
  502. /* Adjust marks for deleted (or added) lines and prepare for displaying. */
  503. deleted = (long)(count - (lnum - eap->line2));
  504. if (deleted > 0)
  505. mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
  506. else if (deleted < 0)
  507. mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
  508. changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
  509. curwin->w_cursor.lnum = eap->line1;
  510. beginline(BL_WHITE | BL_FIX);
  511. sortend:
  512. vim_free(nrs);
  513. vim_free(sortbuf1);
  514. vim_free(sortbuf2);
  515. vim_free(regmatch.regprog);
  516. if (got_int)
  517. EMSG(_(e_interr));
  518. }
  519. /*
  520. * ":retab".
  521. */
  522. void
  523. ex_retab(eap)
  524. exarg_T *eap;
  525. {
  526. linenr_T lnum;
  527. int got_tab = FALSE;
  528. long num_spaces = 0;
  529. long num_tabs;
  530. long len;
  531. long col;
  532. long vcol;
  533. long start_col = 0; /* For start of white-space string */
  534. long start_vcol = 0; /* For start of white-space string */
  535. int temp;
  536. long old_len;
  537. char_u *ptr;
  538. char_u *new_line = (char_u *)1; /* init to non-NULL */
  539. int did_undo; /* called u_save for current line */
  540. int new_ts;
  541. int save_list;
  542. linenr_T first_line = 0; /* first changed line */
  543. linenr_T last_line = 0; /* last changed line */
  544. save_list = curwin->w_p_list;
  545. curwin->w_p_list = 0; /* don't want list mode here */
  546. new_ts = getdigits(&(eap->arg));
  547. if (new_ts < 0)
  548. {
  549. EMSG(_(e_positive));
  550. return;
  551. }
  552. if (new_ts == 0)
  553. new_ts = curbuf->b_p_ts;
  554. for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum)
  555. {
  556. ptr = ml_get(lnum);
  557. col = 0;
  558. vcol = 0;
  559. did_undo = FALSE;
  560. for (;;)
  561. {
  562. if (vim_iswhite(ptr[col]))
  563. {
  564. if (!got_tab && num_spaces == 0)
  565. {
  566. /* First consecutive white-space */
  567. start_vcol = vcol;
  568. start_col = col;
  569. }
  570. if (ptr[col] == ' ')
  571. num_spaces++;
  572. else
  573. got_tab = TRUE;
  574. }
  575. else
  576. {
  577. if (got_tab || (eap->forceit && num_spaces > 1))
  578. {
  579. /* Retabulate this string of white-space */
  580. /* len is virtual length of white string */
  581. len = num_spaces = vcol - start_vcol;
  582. num_tabs = 0;
  583. if (!curbuf->b_p_et)
  584. {
  585. temp = new_ts - (start_vcol % new_ts);
  586. if (num_spaces >= temp)
  587. {
  588. num_spaces -= temp;
  589. num_tabs++;
  590. }
  591. num_tabs += num_spaces / new_ts;
  592. num_spaces -= (num_spaces / new_ts) * new_ts;
  593. }
  594. if (curbuf->b_p_et || got_tab ||
  595. (num_spaces + num_tabs < len))
  596. {
  597. if (did_undo == FALSE)
  598. {
  599. did_undo = TRUE;
  600. if (u_save((linenr_T)(lnum - 1),
  601. (linenr_T)(lnum + 1)) == FAIL)
  602. {
  603. new_line = NULL; /* flag out-of-memory */
  604. break;
  605. }
  606. }
  607. /* len is actual number of white characters used */
  608. len = num_spaces + num_tabs;
  609. old_len = (long)STRLEN(ptr);
  610. new_line = lalloc(old_len - col + start_col + len + 1,
  611. TRUE);
  612. if (new_line == NULL)
  613. break;
  614. if (start_col > 0)
  615. mch_memmove(new_line, ptr, (size_t)start_col);
  616. mch_memmove(new_line + start_col + len,
  617. ptr + col, (size_t)(old_len - col + 1));
  618. ptr = new_line + start_col;
  619. for (col = 0; col < len; col++)
  620. ptr[col] = (col < num_tabs) ? '\t' : ' ';
  621. ml_replace(lnum, new_line, FALSE);
  622. if (first_line == 0)
  623. first_line = lnum;
  624. last_line = lnum;
  625. ptr = new_line;
  626. col = start_col + len;
  627. }
  628. }
  629. got_tab = FALSE;
  630. num_spaces = 0;
  631. }
  632. if (ptr[col] == NUL)
  633. break;
  634. vcol += chartabsize(ptr + col, (colnr_T)vcol);
  635. #ifdef FEAT_MBYTE
  636. if (has_mbyte)
  637. col += (*mb_ptr2len)(ptr + col);
  638. else
  639. #endif
  640. ++col;
  641. }
  642. if (new_line == NULL) /* out of memory */
  643. break;
  644. line_breakcheck();
  645. }
  646. if (got_int)
  647. EMSG(_(e_interr));
  648. if (curbuf->b_p_ts != new_ts)
  649. redraw_curbuf_later(NOT_VALID);
  650. if (first_line != 0)
  651. changed_lines(first_line, 0, last_line + 1, 0L);
  652. curwin->w_p_list = save_list; /* restore 'list' */
  653. curbuf->b_p_ts = new_ts;
  654. coladvance(curwin->w_curswant);
  655. u_clearline();
  656. }
  657. #endif
  658. /*
  659. * :move command - move lines line1-line2 to line dest
  660. *
  661. * return FAIL for failure, OK otherwise
  662. */
  663. int
  664. do_move(line1, line2, dest)
  665. linenr_T line1;
  666. linenr_T line2;
  667. linenr_T dest;
  668. {
  669. char_u *str;
  670. linenr_T l;
  671. linenr_T extra; /* Num lines added before line1 */
  672. linenr_T num_lines; /* Num lines moved */
  673. linenr_T last_line; /* Last line in file after adding new text */
  674. if (dest >= line1 && dest < line2)
  675. {
  676. EMSG(_("E134: Move lines into themselves"));
  677. return FAIL;
  678. }
  679. num_lines = line2 - line1 + 1;
  680. /*
  681. * First we copy the old text to its new location -- webb
  682. * Also copy the flag that ":global" command uses.
  683. */
  684. if (u_save(dest, dest + 1) == FAIL)
  685. return FAIL;
  686. for (extra = 0, l = line1; l <= line2; l++)
  687. {
  688. str = vim_strsave(ml_get(l + extra));
  689. if (str != NULL)
  690. {
  691. ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
  692. vim_free(str);
  693. if (dest < line1)
  694. extra++;
  695. }
  696. }
  697. /*
  698. * Now we must be careful adjusting our marks so that we don't overlap our
  699. * mark_adjust() calls.
  700. *
  701. * We adjust the marks within the old text so that they refer to the
  702. * last lines of the file (temporarily), because we know no other marks
  703. * will be set there since these line numbers did not exist until we added
  704. * our new lines.
  705. *
  706. * Then we adjust the marks on lines between the old and new text positions
  707. * (either forwards or backwards).
  708. *
  709. * And Finally we adjust the marks we put at the end of the file back to
  710. * their final destination at the new text position -- webb
  711. */
  712. last_line = curbuf->b_ml.ml_line_count;
  713. mark_adjust(line1, line2, last_line - line2, 0L);
  714. if (dest >= line2)
  715. {
  716. mark_adjust(line2 + 1, dest, -num_lines, 0L);
  717. curbuf->b_op_start.lnum = dest - num_lines + 1;
  718. curbuf->b_op_end.lnum = dest;
  719. }
  720. else
  721. {
  722. mark_adjust(dest + 1, line1 - 1, num_lines, 0L);
  723. curbuf->b_op_start.lnum = dest + 1;
  724. curbuf->b_op_end.lnum = dest + num_lines;
  725. }
  726. curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
  727. mark_adjust(last_line - num_lines + 1, last_line,
  728. -(last_line - dest - extra), 0L);
  729. /*
  730. * Now we delete the original text -- webb
  731. */
  732. if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
  733. return FAIL;
  734. for (l = line1; l <= line2; l++)
  735. ml_delete(line1 + extra, TRUE);
  736. if (!global_busy && num_lines > p_report)
  737. {
  738. if (num_lines == 1)
  739. MSG(_("1 line moved"));
  740. else
  741. smsg((char_u *)_("%ld lines moved"), num_lines);
  742. }
  743. /*
  744. * Leave the cursor on the last of the moved lines.
  745. */
  746. if (dest >= line1)
  747. curwin->w_cursor.lnum = dest;
  748. else
  749. curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
  750. if (line1 < dest)
  751. changed_lines(line1, 0, dest + num_lines + 1, 0L);
  752. else
  753. changed_lines(dest + 1, 0, line1 + num_lines, 0L);
  754. return OK;
  755. }
  756. /*
  757. * ":copy"
  758. */
  759. void
  760. ex_copy(line1, line2, n)
  761. linenr_T line1;
  762. linenr_T line2;
  763. linenr_T n;
  764. {
  765. linenr_T count;
  766. char_u *p;
  767. count = line2 - line1 + 1;
  768. curbuf->b_op_start.lnum = n + 1;
  769. curbuf->b_op_end.lnum = n + count;
  770. curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
  771. /*
  772. * there are three situations:
  773. * 1. destination is above line1
  774. * 2. destination is between line1 and line2
  775. * 3. destination is below line2
  776. *
  777. * n = destination (when starting)
  778. * curwin->w_cursor.lnum = destination (while copying)
  779. * line1 = start of source (while copying)
  780. * line2 = end of source (while copying)
  781. */
  782. if (u_save(n, n + 1) == FAIL)
  783. return;
  784. curwin->w_cursor.lnum = n;
  785. while (line1 <= line2)
  786. {
  787. /* need to use vim_strsave() because the line will be unlocked within
  788. * ml_append() */
  789. p = vim_strsave(ml_get(line1));
  790. if (p != NULL)
  791. {
  792. ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
  793. vim_free(p);
  794. }
  795. /* situation 2: skip already copied lines */
  796. if (line1 == n)
  797. line1 = curwin->w_cursor.lnum;
  798. ++line1;
  799. if (curwin->w_cursor.lnum < line1)
  800. ++line1;
  801. if (curwin->w_cursor.lnum < line2)
  802. ++line2;
  803. ++curwin->w_cursor.lnum;
  804. }
  805. appended_lines_mark(n, count);
  806. msgmore((long)count);
  807. }
  808. static char_u *prevcmd = NULL; /* the previous command */
  809. #if defined(EXITFREE) || defined(PROTO)
  810. void
  811. free_prev_shellcmd()
  812. {
  813. vim_free(prevcmd);
  814. }
  815. #endif
  816. /*
  817. * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
  818. * Bangs in the argument are replaced with the previously entered command.
  819. * Remember the argument.
  820. *
  821. * RISCOS: Bangs only replaced when followed by a space, since many
  822. * pathnames contain one.
  823. */
  824. void
  825. do_bang(addr_count, eap, forceit, do_in, do_out)
  826. int addr_count;
  827. exarg_T *eap;
  828. int forceit;
  829. int do_in, do_out;
  830. {
  831. char_u *arg = eap->arg; /* command */
  832. linenr_T line1 = eap->line1; /* start of range */
  833. linenr_T line2 = eap->line2; /* end of range */
  834. char_u *newcmd = NULL; /* the new command */
  835. int free_newcmd = FALSE; /* need to free() newcmd */
  836. int ins_prevcmd;
  837. char_u *t;
  838. char_u *p;
  839. char_u *trailarg;
  840. int len;
  841. int scroll_save = msg_scroll;
  842. /*
  843. * Disallow shell commands for "rvim".
  844. * Disallow shell commands from .exrc and .vimrc in current directory for
  845. * security reasons.
  846. */
  847. if (check_restricted() || check_secure())
  848. return;
  849. if (addr_count == 0) /* :! */
  850. {
  851. msg_scroll = FALSE; /* don't scroll here */
  852. autowrite_all();
  853. msg_scroll = scroll_save;
  854. }
  855. /*
  856. * Try to find an embedded bang, like in :!<cmd> ! [args]
  857. * (:!! is indicated by the 'forceit' variable)
  858. */
  859. ins_prevcmd = forceit;
  860. trailarg = arg;
  861. do
  862. {
  863. len = (int)STRLEN(trailarg) + 1;
  864. if (newcmd != NULL)
  865. len += (int)STRLEN(newcmd);
  866. if (ins_prevcmd)
  867. {
  868. if (prevcmd == NULL)
  869. {
  870. EMSG(_(e_noprev));
  871. vim_free(newcmd);
  872. return;
  873. }
  874. len += (int)STRLEN(prevcmd);
  875. }
  876. if ((t = alloc((unsigned)len)) == NULL)
  877. {
  878. vim_free(newcmd);
  879. return;
  880. }
  881. *t = NUL;
  882. if (newcmd != NULL)
  883. STRCAT(t, newcmd);
  884. if (ins_prevcmd)
  885. STRCAT(t, prevcmd);
  886. p = t + STRLEN(t);
  887. STRCAT(t, trailarg);
  888. vim_free(newcmd);
  889. newcmd = t;
  890. /*
  891. * Scan the rest of the argument for '!', which is replaced by the
  892. * previous command. "\!" is replaced by "!" (this is vi compatible).
  893. */
  894. trailarg = NULL;
  895. while (*p)
  896. {
  897. if (*p == '!'
  898. #ifdef RISCOS
  899. && (p[1] == ' ' || p[1] == NUL)
  900. #endif
  901. )
  902. {
  903. if (p > newcmd && p[-1] == '\\')
  904. STRMOVE(p - 1, p);
  905. else
  906. {
  907. trailarg = p;
  908. *trailarg++ = NUL;
  909. ins_prevcmd = TRUE;
  910. break;
  911. }
  912. }
  913. ++p;
  914. }
  915. } while (trailarg != NULL);
  916. vim_free(prevcmd);
  917. prevcmd = newcmd;
  918. if (bangredo) /* put cmd in redo buffer for ! command */
  919. {
  920. AppendToRedobuffLit(prevcmd, -1);
  921. AppendToRedobuff((char_u *)"\n");
  922. bangredo = FALSE;
  923. }
  924. /*
  925. * Add quotes around the command, for shells that need them.
  926. */
  927. if (*p_shq != NUL)
  928. {
  929. newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1));
  930. if (newcmd == NULL)
  931. return;
  932. STRCPY(newcmd, p_shq);
  933. STRCAT(newcmd, prevcmd);
  934. STRCAT(newcmd, p_shq);
  935. free_newcmd = TRUE;
  936. }
  937. if (addr_count == 0) /* :! */
  938. {
  939. /* echo the command */
  940. msg_start();
  941. msg_putchar(':');
  942. msg_putchar('!');
  943. msg_outtrans(newcmd);
  944. msg_clr_eos();
  945. windgoto(msg_row, msg_col);
  946. do_shell(newcmd, 0);
  947. }
  948. else /* :range! */
  949. {
  950. /* Careful: This may recursively call do_bang() again! (because of
  951. * autocommands) */
  952. do_filter(line1, line2, eap, newcmd, do_in, do_out);
  953. #ifdef FEAT_AUTOCMD
  954. apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
  955. #endif
  956. }
  957. if (free_newcmd)
  958. vim_free(newcmd);
  959. }
  960. /*
  961. * do_filter: filter lines through a command given by the user
  962. *
  963. * We mostly use temp files and the call_shell() routine here. This would
  964. * normally be done using pipes on a UNIX machine, but this is more portable
  965. * to non-unix machines. The call_shell() routine needs to be able
  966. * to deal with redirection somehow, and should handle things like looking
  967. * at the PATH env. variable, and adding reasonable extensions to the
  968. * command name given by the user. All reasonable versions of call_shell()
  969. * do this.
  970. * Alternatively, if on Unix and redirecting input or output, but not both,
  971. * and the 'shelltemp' option isn't set, use pipes.
  972. * We use input redirection if do_in is TRUE.
  973. * We use output redirection if do_out is TRUE.
  974. */
  975. static void
  976. do_filter(line1, line2, eap, cmd, do_in, do_out)
  977. linenr_T line1, line2;
  978. exarg_T *eap; /* for forced 'ff' and 'fenc' */
  979. char_u *cmd;
  980. int do_in, do_out;
  981. {
  982. char_u *itmp = NULL;
  983. char_u *otmp = NULL;
  984. linenr_T linecount;
  985. linenr_T read_linecount;
  986. pos_T cursor_save;
  987. char_u *cmd_buf;
  988. #ifdef FEAT_AUTOCMD
  989. buf_T *old_curbuf = curbuf;
  990. #endif
  991. int shell_flags = 0;
  992. if (*cmd == NUL) /* no filter command */
  993. return;
  994. #ifdef WIN3264
  995. /*
  996. * Check if external commands are allowed now.
  997. */
  998. if (can_end_termcap_mode(TRUE) == FALSE)
  999. return;
  1000. #endif
  1001. cursor_save = curwin->w_cursor;
  1002. linecount = line2 - line1 + 1;
  1003. curwin->w_cursor.lnum = line1;
  1004. curwin->w_cursor.col = 0;
  1005. changed_line_abv_curs();
  1006. invalidate_botline();
  1007. /*
  1008. * When using temp files:
  1009. * 1. * Form temp file names
  1010. * 2. * Write the lines to a temp file
  1011. * 3. Run the filter command on the temp file
  1012. * 4. * Read the output of the command into the buffer
  1013. * 5. * Delete the original lines to be filtered
  1014. * 6. * Remove the temp files
  1015. *
  1016. * When writing the input with a pipe or when catching the output with a
  1017. * pipe only need to do 3.
  1018. */
  1019. if (do_out)
  1020. shell_flags |= SHELL_DOOUT;
  1021. #if !defined(USE_SYSTEM) && defined(UNIX)
  1022. if (!do_in && do_out && !p_stmp)
  1023. {
  1024. /* Use a pipe to fetch stdout of the command, do not use a temp file. */
  1025. shell_flags |= SHELL_READ;
  1026. curwin->w_cursor.lnum = line2;
  1027. }
  1028. else if (do_in && !do_out && !p_stmp)
  1029. {
  1030. /* Use a pipe to write stdin of the command, do not use a temp file. */
  1031. shell_flags |= SHELL_WRITE;
  1032. curbuf->b_op_start.lnum = line1;
  1033. curbuf->b_op_end.lnum = line2;
  1034. }
  1035. else if (do_in && do_out && !p_stmp)
  1036. {
  1037. /* Use a pipe to write stdin and fetch stdout of the command, do not
  1038. * use a temp file. */
  1039. shell_flags |= SHELL_READ|SHELL_WRITE;
  1040. curbuf->b_op_start.lnum = line1;
  1041. curbuf->b_op_end.lnum = line2;
  1042. curwin->w_cursor.lnum = line2;
  1043. }
  1044. else
  1045. #endif
  1046. if ((do_in && (itmp = vim_tempname('i')) == NULL)
  1047. || (do_out && (otmp = vim_tempname('o')) == NULL))
  1048. {
  1049. EMSG(_(e_notmp));
  1050. goto filterend;
  1051. }
  1052. /*
  1053. * The writing and reading of temp files will not be shown.
  1054. * Vi also doesn't do this and the messages are not very informative.
  1055. */
  1056. ++no_wait_return; /* don't call wait_return() while busy */
  1057. if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
  1058. FALSE, FALSE, FALSE, TRUE) == FAIL)
  1059. {
  1060. msg_putchar('\n'); /* keep message from buf_write() */
  1061. --no_wait_return;
  1062. #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  1063. if (!aborting())
  1064. #endif
  1065. (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
  1066. goto filterend;
  1067. }
  1068. #ifdef FEAT_AUTOCMD
  1069. if (curbuf != old_curbuf)
  1070. goto filterend;
  1071. #endif
  1072. if (!do_out)
  1073. msg_putchar('\n');
  1074. /* Create the shell command in allocated memory. */
  1075. cmd_buf = make_filter_cmd(cmd, itmp, otmp);
  1076. if (cmd_buf == NULL)
  1077. goto filterend;
  1078. windgoto((int)Rows - 1, 0);
  1079. cursor_on();
  1080. /*
  1081. * When not redirecting the output the command can write anything to the
  1082. * screen. If 'shellredir' is equal to ">", screen may be messed up by
  1083. * stderr output of external command. Clear the screen later.
  1084. * If do_in is FALSE, this could be something like ":r !cat", which may
  1085. * also mess up the screen, clear it later.
  1086. */
  1087. if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in)
  1088. redraw_later_clear();
  1089. if (do_out)
  1090. {
  1091. if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL)
  1092. {
  1093. vim_free(cmd_buf);
  1094. goto error;
  1095. }
  1096. redraw_curbuf_later(VALID);
  1097. }
  1098. read_linecount = curbuf->b_ml.ml_line_count;
  1099. /*
  1100. * When call_shell() fails wait_return() is called to give the user a
  1101. * chance to read the error messages. Otherwise errors are ignored, so you
  1102. * can see the error messages from the command that appear on stdout; use
  1103. * 'u' to fix the text
  1104. * Switch to cooked mode when not redirecting stdin, avoids that something
  1105. * like ":r !cat" hangs.
  1106. * Pass on the SHELL_DOOUT flag when the output is being redirected.
  1107. */
  1108. if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags))
  1109. {
  1110. redraw_later_clear();
  1111. wait_return(FALSE);
  1112. }
  1113. vim_free(cmd_buf);
  1114. did_check_timestamps = FALSE;
  1115. need_check_timestamps = TRUE;
  1116. /* When interrupting the shell command, it may still have produced some
  1117. * useful output. Reset got_int here, so that readfile() won't cancel
  1118. * reading. */
  1119. ui_breakcheck();
  1120. got_int = FALSE;
  1121. if (do_out)
  1122. {
  1123. if (otmp != NULL)
  1124. {
  1125. if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
  1126. eap, READ_FILTER) == FAIL)
  1127. {
  1128. #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  1129. if (!aborting())
  1130. #endif
  1131. {
  1132. msg_putchar('\n');
  1133. EMSG2(_(e_notread), otmp);
  1134. }
  1135. goto error;
  1136. }
  1137. #ifdef FEAT_AUTOCMD
  1138. if (curbuf != old_curbuf)
  1139. goto filterend;
  1140. #endif
  1141. }
  1142. read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
  1143. if (shell_flags & SHELL_READ)
  1144. {
  1145. curbuf->b_op_start.lnum = line2 + 1;
  1146. curbuf->b_op_end.lnum = curwin->w_cursor.lnum;
  1147. appended_lines_mark(line2, read_linecount);
  1148. }
  1149. if (do_in)
  1150. {
  1151. if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL)
  1152. {
  1153. if (read_linecount >= linecount)
  1154. /* move all marks from old lines to new lines */
  1155. mark_adjust(line1, line2, linecount, 0L);
  1156. else
  1157. {
  1158. /* move marks from old lines to new lines, delete marks
  1159. * that are in deleted lines */
  1160. mark_adjust(line1, line1 + read_linecount - 1,
  1161. linecount, 0L);
  1162. mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L);
  1163. }
  1164. }
  1165. /*
  1166. * Put cursor on first filtered line for ":range!cmd".
  1167. * Adjust '[ and '] (set by buf_write()).
  1168. */
  1169. curwin->w_cursor.lnum = line1;
  1170. del_lines(linecount, TRUE);
  1171. curbuf->b_op_start.lnum -= linecount; /* adjust '[ */
  1172. curbuf->b_op_end.lnum -= linecount; /* adjust '] */
  1173. write_lnum_adjust(-linecount); /* adjust last line
  1174. for next write */
  1175. #ifdef FEAT_FOLDING
  1176. foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
  1177. #endif
  1178. }
  1179. else
  1180. {
  1181. /*
  1182. * Put cursor on last new line for ":r !cmd".
  1183. */
  1184. linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
  1185. curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
  1186. }
  1187. beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */
  1188. --no_wait_return;
  1189. if (linecount > p_report)
  1190. {
  1191. if (do_in)
  1192. {
  1193. vim_snprintf((char *)msg_buf, sizeof(msg_buf),
  1194. _("%ld lines filtered"), (long)linecount);
  1195. if (msg(msg_buf) && !msg_scroll)
  1196. /* save message to display it after redraw */
  1197. set_keep_msg(msg_buf, 0);
  1198. }
  1199. else
  1200. msgmore((long)linecount);
  1201. }
  1202. }
  1203. else
  1204. {
  1205. error:
  1206. /* put cursor back in same position for ":w !cmd" */
  1207. curwin->w_cursor = cursor_save;
  1208. --no_wait_return;
  1209. wait_return(FALSE);
  1210. }
  1211. filterend:
  1212. #ifdef FEAT_AUTOCMD
  1213. if (curbuf != old_curbuf)
  1214. {
  1215. --no_wait_return;
  1216. EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
  1217. }
  1218. #endif
  1219. if (itmp != NULL)
  1220. mch_remove(itmp);
  1221. if (otmp != NULL)
  1222. mch_remove(otmp);
  1223. vim_free(itmp);
  1224. vim_free(otmp);
  1225. }
  1226. /*
  1227. * Call a shell to execute a command.
  1228. * When "cmd" is NULL start an interactive shell.
  1229. */
  1230. void
  1231. do_shell(cmd, flags)
  1232. char_u *cmd;
  1233. int flags; /* may be SHELL_DOOUT when output is redirected */
  1234. {
  1235. buf_T *buf;
  1236. #ifndef FEAT_GUI_MSWIN
  1237. int save_nwr;
  1238. #endif
  1239. #ifdef MSWIN
  1240. int winstart = FALSE;
  1241. #endif
  1242. /*
  1243. * Disallow shell commands for "rvim".
  1244. * Disallow shell commands from .exrc and .vimrc in current directory for
  1245. * security reasons.
  1246. */
  1247. if (check_restricted() || check_secure())
  1248. {
  1249. msg_end();
  1250. return;
  1251. }
  1252. #ifdef MSWIN
  1253. /*
  1254. * Check if external commands are allowed now.
  1255. */
  1256. if (can_end_termcap_mode(TRUE) == FALSE)
  1257. return;
  1258. /*
  1259. * Check if ":!start" is used.
  1260. */
  1261. if (cmd != NULL)
  1262. winstart = (STRNICMP(cmd, "start ", 6) == 0);
  1263. #endif
  1264. /*
  1265. * For autocommands we want to get the output on the current screen, to
  1266. * avoid having to type return below.
  1267. */
  1268. msg_putchar('\r'); /* put cursor at start of line */
  1269. #ifdef FEAT_AUTOCMD
  1270. if (!autocmd_busy)
  1271. #endif
  1272. {
  1273. #ifdef MSWIN
  1274. if (!winstart)
  1275. #endif
  1276. stoptermcap();
  1277. }
  1278. #ifdef MSWIN
  1279. if (!winstart)
  1280. #endif
  1281. msg_putchar('\n'); /* may shift screen one line up */
  1282. /* warning message before calling the shell */
  1283. if (p_warn
  1284. #ifdef FEAT_AUTOCMD
  1285. && !autocmd_busy
  1286. #endif
  1287. && msg_silent == 0)
  1288. for (buf = firstbuf; buf; buf = buf->b_next)
  1289. if (bufIsChanged(buf))
  1290. {
  1291. #ifdef FEAT_GUI_MSWIN
  1292. if (!winstart)
  1293. starttermcap(); /* don't want a message box here */
  1294. #endif
  1295. MSG_PUTS(_("[No write since last change]\n"));
  1296. #ifdef FEAT_GUI_MSWIN
  1297. if (!winstart)
  1298. stoptermcap();
  1299. #endif
  1300. break;
  1301. }
  1302. /* This windgoto is required for when the '\n' resulted in a "delete line
  1303. * 1" command to the terminal. */
  1304. if (!swapping_screen())
  1305. windgoto(msg_row, msg_col);
  1306. cursor_on();
  1307. (void)call_shell(cmd, SHELL_COOKED | flags);
  1308. did_check_timestamps = FALSE;
  1309. need_check_timestamps = TRUE;
  1310. /*
  1311. * put the message cursor at the end of the screen, avoids wait_return()
  1312. * to overwrite the text that the external command showed
  1313. */
  1314. if (!swapping_screen())
  1315. {
  1316. msg_row = Rows - 1;
  1317. msg_col = 0;
  1318. }
  1319. #ifdef FEAT_AUTOCMD
  1320. if (autocmd_busy)
  1321. {
  1322. if (msg_silent == 0)
  1323. redraw_later_clear();
  1324. }
  1325. else
  1326. #endif
  1327. {
  1328. /*
  1329. * For ":sh" there is no need to call wait_return(), just redraw.
  1330. * Also for the Win32 GUI (the output is in a console window).
  1331. * Otherwise there is probably text on the screen that the user wants
  1332. * to read before redrawing, so call wait_return().
  1333. */
  1334. #ifndef FEAT_GUI_MSWIN
  1335. if (cmd == NULL
  1336. # ifdef WIN3264
  1337. || (winstart && !need_wait_return)
  1338. # endif
  1339. )
  1340. {
  1341. if (msg_silent == 0)
  1342. redraw_later_clear();
  1343. need_wait_return = FALSE;
  1344. }
  1345. else
  1346. {
  1347. /*
  1348. * If we switch screens when starttermcap() is called, we really
  1349. * want to wait for "hit return to continue".
  1350. */
  1351. save_nwr = no_wait_return;
  1352. if (swapping_screen())
  1353. no_wait_return = FALSE;
  1354. # ifdef AMIGA
  1355. wait_return(term_console ? -1 : msg_silent == 0); /* see below */
  1356. # else
  1357. wait_return(msg_silent == 0);
  1358. # endif
  1359. no_wait_return = save_nwr;
  1360. }
  1361. #endif /* FEAT_GUI_W32 */
  1362. #ifdef MSWIN
  1363. if (!winstart) /* if winstart==TRUE, never stopped termcap! */
  1364. #endif
  1365. starttermcap(); /* start termcap if not done by wait_return() */
  1366. /*
  1367. * In an Amiga window redrawing is caused by asking the window size.
  1368. * If we got an interrupt this will not work. The chance that the
  1369. * window size is wrong is very small, but we need to redraw the
  1370. * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY
  1371. * but it saves an extra redraw.
  1372. */
  1373. #ifdef AMIGA
  1374. if (skip_redraw) /* ':' hit in wait_return() */
  1375. {
  1376. if (msg_silent == 0)
  1377. redraw_later_clear();
  1378. }
  1379. else if (term_console)
  1380. {
  1381. OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */
  1382. if (got_int && msg_silent == 0)
  1383. redraw_later_clear(); /* if got_int is TRUE, redraw needed */
  1384. else
  1385. must_redraw = 0; /* no extra redraw needed */
  1386. }
  1387. #endif
  1388. }
  1389. /* display any error messages now */
  1390. display_errors();
  1391. #ifdef FEAT_AUTOCMD
  1392. apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
  1393. #endif
  1394. }
  1395. /*
  1396. * Create a shell command from a command string, input redirection file and
  1397. * output redirection file.
  1398. * Returns an allocated string with the shell command, or NULL for failure.
  1399. */
  1400. char_u *
  1401. make_filter_cmd(cmd, itmp, otmp)
  1402. char_u *cmd; /* command */
  1403. char_u *itmp; /* NULL or name of input file */
  1404. char_u *otmp; /* NULL or name of output file */
  1405. {
  1406. char_u *buf;
  1407. long_u len;
  1408. len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */
  1409. if (itmp != NULL)
  1410. len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */
  1411. if (otmp != NULL)
  1412. len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */
  1413. buf = lalloc(len, TRUE);
  1414. if (buf == NULL)
  1415. return NULL;
  1416. #if (defined(UNIX) && !defined(ARCHIE)) || defined(OS2)
  1417. /*
  1418. * Put braces around the command (for concatenated commands) when
  1419. * redirecting input and/or output.
  1420. */
  1421. if (itmp != NULL || otmp != NULL)
  1422. vim_snprintf((char *)buf, len, "(%s)", (char *)cmd);
  1423. else
  1424. STRCPY(buf, cmd);
  1425. if (itmp != NULL)
  1426. {
  1427. STRCAT(buf, " < ");
  1428. STRCAT(buf, itmp);
  1429. }
  1430. #else
  1431. /*
  1432. * for shells that don't understand braces around commands, at least allow
  1433. * the use of commands in a pipe.
  1434. */
  1435. STRCPY(buf, cmd);
  1436. if (itmp != NULL)
  1437. {
  1438. char_u *p;
  1439. /*
  1440. * If there is a pipe, we have to put the '<' in front of it.
  1441. * Don't do this when 'shellquote' is not empty, otherwise the
  1442. * redirection would be inside the quotes.
  1443. */
  1444. if (*p_shq == NUL)
  1445. {
  1446. p = vim_strchr(buf, '|');
  1447. if (p != NULL)
  1448. *p = NUL;
  1449. }
  1450. # ifdef RISCOS
  1451. STRCAT(buf, " { < "); /* Use RISC OS notation for input. */
  1452. STRCAT(buf, itmp);
  1453. STRCAT(buf, " } ");
  1454. # else
  1455. STRCAT(buf, " <"); /* " < " causes problems on Amiga */
  1456. STRCAT(buf, itmp);
  1457. # endif
  1458. if (*p_shq == NUL)
  1459. {
  1460. p = vim_strchr(cmd, '|');
  1461. if (p != NULL)
  1462. {
  1463. STRCAT(buf, " "); /* insert a space before the '|' for DOS */
  1464. STRCAT(buf, p);
  1465. }
  1466. }
  1467. }
  1468. #endif
  1469. if (otmp != NULL)
  1470. append_redir(buf, (int)len, p_srr, otmp);
  1471. return buf;
  1472. }
  1473. /*
  1474. * Append output redirection for file "fname" to the end of string buffer
  1475. * "buf[buflen]"
  1476. * Works with the 'shellredir' and 'shellpipe' options.
  1477. * The caller should make sure that there is enough room:
  1478. * STRLEN(opt) + STRLEN(fname) + 3
  1479. */
  1480. void
  1481. append_redir(buf, buflen, opt, fname)
  1482. char_u *buf;
  1483. int buflen;
  1484. char_u *opt;
  1485. char_u *fname;
  1486. {
  1487. char_u *p;
  1488. char_u *end;
  1489. end = buf + STRLEN(buf);
  1490. /* find "%s", skipping "%%" */
  1491. for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p)
  1492. if (p[1] == 's')
  1493. break;
  1494. if (p != NULL)
  1495. {
  1496. *end = ' '; /* not really needed? Not with sh, ksh or bash */
  1497. vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)),
  1498. (char *)opt, (char *)fname);
  1499. }
  1500. else
  1501. vim_snprintf((char *)end, (size_t)(buflen - (end - buf)),
  1502. #ifdef FEAT_QUICKFIX
  1503. # ifndef RISCOS
  1504. opt != p_sp ? " %s%s" :
  1505. # endif
  1506. " %s %s",
  1507. #else
  1508. # ifndef RISCOS
  1509. " %s%s", /* " > %s" causes problems on Amiga */
  1510. # else
  1511. " %s %s", /* But is needed for 'shellpipe' and RISC OS */
  1512. # endif
  1513. #endif
  1514. (char *)opt, (char *)fname);
  1515. }
  1516. #ifdef FEAT_VIMINFO
  1517. static int no_viminfo __ARGS((void));
  1518. static int viminfo_errcnt;
  1519. static int
  1520. no_viminfo()
  1521. {
  1522. /* "vim -i NONE" does not read or write a viminfo file */
  1523. return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0);
  1524. }
  1525. /*
  1526. * Report an error for reading a viminfo file.
  1527. * Count the number of errors. When there are more than 10, return TRUE.
  1528. */
  1529. int
  1530. viminfo_error(errnum, message, line)
  1531. char *errnum;
  1532. char *message;
  1533. char_u *line;
  1534. {
  1535. vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
  1536. errnum, message);
  1537. STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
  1538. if (IObuff[STRLEN(IObuff) - 1] == '\n')
  1539. IObuff[STRLEN(IObuff) - 1] = NUL;
  1540. emsg(IObuff);
  1541. if (++viminfo_errcnt >= 10)
  1542. {
  1543. EMSG(_("E136: viminfo: Too many errors, skipping rest of file"));
  1544. return TRUE;
  1545. }
  1546. return FALSE;
  1547. }
  1548. /*
  1549. * read_viminfo() -- Read the viminfo file. Registers etc. which are already
  1550. * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
  1551. */
  1552. int
  1553. read_viminfo(file, flags)
  1554. char_u *file; /* file name or NULL to use default name */
  1555. int flags; /* VIF_WANT_INFO et al. */
  1556. {
  1557. FILE *fp;
  1558. char_u *fname;
  1559. if (no_viminfo())
  1560. return FAIL;
  1561. fname = viminfo_filename(file); /* get file name in allocated buffer */
  1562. if (fname == NULL)
  1563. return FAIL;
  1564. fp = mch_fopen((char *)fname, READBIN);
  1565. if (p_verbose > 0)
  1566. {
  1567. verbose_enter();
  1568. smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"),
  1569. fname,
  1570. (flags & VIF_WANT_INFO) ? _(" info") : "",
  1571. (flags & VIF_WANT_MARKS) ? _(" marks") : "",
  1572. (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
  1573. fp == NULL ? _(" FAILED") : "");
  1574. verbose_leave();
  1575. }
  1576. vim_free(fname);
  1577. if (fp == NULL)
  1578. return FAIL;
  1579. viminfo_errcnt = 0;
  1580. do_viminfo(fp, NULL, flags);
  1581. fclose(fp);
  1582. return OK;
  1583. }
  1584. /*
  1585. * write_viminfo() -- Write the viminfo file. The old one is read in first so
  1586. * that effectively a merge of current info and old info is done. This allows
  1587. * multiple vims to run simultaneously, without losing any marks etc. If
  1588. * forceit is TRUE, then the old file is not read in, and only internal info is
  1589. * written to the file. -- webb
  1590. */
  1591. void
  1592. write_viminfo(file, forceit)
  1593. char_u *file;
  1594. int forceit;
  1595. {
  1596. char_u *fname;
  1597. FILE *fp_in = NULL; /* input viminfo file, if any */
  1598. FILE *fp_out = NULL; /* output viminfo file */
  1599. char_u *tempname = NULL; /* name of temp viminfo file */
  1600. struct stat st_new; /* mch_stat() of potential new file */
  1601. char_u *wp;
  1602. #if defined(UNIX) || defined(VMS)
  1603. mode_t umask_save;
  1604. #endif
  1605. #ifdef UNIX
  1606. int shortname = FALSE; /* use 8.3 file name */
  1607. struct stat st_old; /* mch_stat() of existing viminfo file */
  1608. #endif
  1609. #ifdef WIN3264
  1610. long perm = -1;
  1611. #endif
  1612. if (no_viminfo())
  1613. return;
  1614. fname = viminfo_filename(file); /* may set to default if NULL */
  1615. if (fname == NULL)
  1616. return;
  1617. fp_in = mch_fopen((char *)fname, READBIN);
  1618. if (fp_in == NULL)
  1619. {
  1620. /* if it does exist, but we can't read it, don't try writing */
  1621. if (mch_stat((char *)fname, &st_new) == 0)
  1622. goto end;
  1623. #if defined(UNIX) || defined(VMS)
  1624. /*
  1625. * For Unix we create the .viminfo non-accessible for others,
  1626. * because it may contain text from non-accessible documents.
  1627. */
  1628. umask_save = umask(077);
  1629. #endif
  1630. fp_out = mch_fopen((char *)fname, WRITEBIN);
  1631. #if defined(UNIX) || defined(VMS)
  1632. (void)umask(umask_save);
  1633. #endif
  1634. }
  1635. else
  1636. {
  1637. /*
  1638. * There is an existing viminfo file. Create a temporary file to
  1639. * write the new viminfo into, in the same directory as the
  1640. * existing viminfo file, which will be renamed later.
  1641. */
  1642. #ifdef UNIX
  1643. /*
  1644. * For Unix we check the owner of the file. It's not very nice to
  1645. * overwrite a user's viminfo file after a "su root", with a
  1646. * viminfo file that the user can't read.
  1647. */
  1648. st_old.st_dev = (dev_t)0;
  1649. st_old.st_ino = 0;
  1650. st_old.st_mode = 0600;
  1651. if (mch_stat((char *)fname, &st_old) == 0
  1652. && getuid() != ROOT_UID
  1653. && !(st_old.st_uid == getuid()
  1654. ? (st_old.st_mode & 0200)
  1655. : (st_old.st_gid == getgid()
  1656. ? (st_old.st_mode & 0020)
  1657. : (st_old.st_mode & 0002))))
  1658. {
  1659. int tt = msg_didany;
  1660. /* avoid a wait_return for this message, it's annoying */
  1661. EMSG2(_("E137: Viminfo file is not writable: %s"), fname);
  1662. msg_didany = tt;
  1663. fclose(fp_in);
  1664. goto end;
  1665. }
  1666. #endif
  1667. #ifdef WIN3264
  1668. /* Get the file attributes of the existing viminfo file. */
  1669. perm = mch_getperm(fname);
  1670. #endif
  1671. /*
  1672. * Make tempname.
  1673. * May try twice: Once normal and once with shortname set, just in
  1674. * case somebody puts his viminfo file in an 8.3 filesystem.
  1675. */
  1676. for (;;)
  1677. {
  1678. tempname = buf_modname(
  1679. #ifdef UNIX
  1680. shortname,
  1681. #else
  1682. # ifdef SHORT_FNAME
  1683. TRUE,
  1684. # else
  1685. # ifdef FEAT_GUI_W32
  1686. gui_is_win32s(),
  1687. # else
  1688. FALSE,
  1689. # endif
  1690. # endif
  1691. #endif
  1692. fname,
  1693. #ifdef VMS
  1694. (char_u *)"-tmp",
  1695. #else
  1696. # ifdef RISCOS
  1697. (char_u *)"/tmp",
  1698. # else
  1699. (char_u *)".tmp",
  1700. # endif
  1701. #endif
  1702. FALSE);
  1703. if (tempname == NULL) /* out of memory */
  1704. break;
  1705. /*
  1706. * Check if tempfile already exists. Never overwrite an
  1707. * existing file!
  1708. */
  1709. if (mch_stat((char *)tempname, &st_new) == 0)
  1710. {
  1711. #ifdef UNIX
  1712. /*
  1713. * Check if tempfile is same as original file. May happen
  1714. * when modname() gave the same file back. E.g. silly
  1715. * link, or file name-length reached. Try again with
  1716. * shortname set.
  1717. */
  1718. if (!shortname && st_new.st_dev == st_old.st_dev
  1719. && st_new.st_ino == st_old.st_ino)
  1720. {
  1721. vim_free(tempname);
  1722. tempname = NULL;
  1723. shortname = TRUE;
  1724. continue;
  1725. }
  1726. #endif
  1727. /*
  1728. * Try another name. Change one character, just before
  1729. * the extension. This should also work for an 8.3
  1730. * file name, when after adding the extension it still is
  1731. * the same file as the original.
  1732. */
  1733. wp = tempname + STRLEN(tempname) - 5;
  1734. if (wp < gettail(tempname)) /* empty file name? */
  1735. wp = gettail(tempname);
  1736. for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0;
  1737. --*wp)
  1738. {
  1739. /*
  1740. * They all exist? Must be something wrong! Don't
  1741. * write the viminfo file then.
  1742. */
  1743. if (*wp == 'a')
  1744. {
  1745. vim_free(tempname);
  1746. tempname = NULL;
  1747. break;
  1748. }
  1749. }
  1750. }
  1751. break;
  1752. }
  1753. if (tempname != NULL)
  1754. {
  1755. #ifdef VMS
  1756. /* fdopen() fails for some reason */
  1757. umask_save = umask(077);
  1758. fp_out = mch_fopen((char *)tempname, WRITEBIN);
  1759. (void)umask(umask_save);
  1760. #else
  1761. int fd;
  1762. /* Use mch_open() to be able to use O_NOFOLLOW and set file
  1763. * protection:
  1764. * Unix: same as original file, but strip s-bit. Reset umask to
  1765. * avoid it getting in the way.
  1766. * Others: r&w for user only. */
  1767. # ifdef UNIX
  1768. umask_save = umask(0);
  1769. fd = mch_open((char *)tempname,
  1770. O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
  1771. (int)((st_old.st_mode & 0777) | 0600));
  1772. (void)umask(umask_save);
  1773. # else
  1774. fd = mch_open((char *)tempname,
  1775. O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
  1776. # endif
  1777. if (fd < 0)
  1778. fp_out = NULL;
  1779. else
  1780. fp_out = fdopen(fd, WRITEBIN);
  1781. #endif /* VMS */
  1782. /*
  1783. * If we can't create in the same directory, try creating a
  1784. * "normal" temp file.
  1785. */
  1786. if (fp_out == NULL)
  1787. {
  1788. vim_free(tempname);
  1789. if ((tempname = vim_tempname('o')) != NULL)
  1790. fp_out = mch_fopen((char *)tempname, WRITEBIN);
  1791. }
  1792. #if defined(UNIX) && defined(HAVE_FCHOWN)
  1793. /*
  1794. * Make sure the owner can read/write it. This only works for
  1795. * root.
  1796. */
  1797. if (fp_out != NULL)
  1798. ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid);
  1799. #endif
  1800. }
  1801. }
  1802. /*
  1803. * Check if the new viminfo file can be written to.
  1804. */
  1805. if (fp_out == NULL)
  1806. {
  1807. EMSG2(_("E138: Can't write viminfo file %s!"),
  1808. (fp_in == NULL || tempname == NULL) ? fname : tempname);
  1809. if (fp_in != NULL)
  1810. fclose(fp_in);
  1811. goto end;
  1812. }
  1813. if (p_verbose > 0)
  1814. {
  1815. verbose_enter();
  1816. smsg((char_u *)_("Writing viminfo file \"%s\""), fname);
  1817. verbose_leave();
  1818. }
  1819. viminfo_errcnt = 0;
  1820. do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
  1821. fclose(fp_out); /* errors are ignored !? */
  1822. if (fp_in != NULL)
  1823. {
  1824. fclose(fp_in);
  1825. /*
  1826. * In case of an error keep the original viminfo file.
  1827. * Otherwise rename the newly written file.
  1828. */
  1829. if (viminfo_errcnt || vim_rename(tempname, fname) == -1)
  1830. mch_remove(tempname);
  1831. #ifdef WIN3264
  1832. /* If the viminfo file was hidden then also hide the new file. */
  1833. if (perm > 0 && (perm & FILE_ATTRIBUTE_HIDDEN))
  1834. mch_hide(fname);
  1835. #endif
  1836. }
  1837. end:
  1838. vim_free(fname);
  1839. vim_free(tempname);
  1840. }
  1841. /*
  1842. * Get the viminfo file name to use.
  1843. * If "file" is given and not empty, use it (has already been expanded by
  1844. * cmdline functions).
  1845. * Otherwise use "-i file_name", value from 'viminfo' or the default, and
  1846. * expand environment variables.
  1847. * Returns an allocated string. NULL when out of memory.
  1848. */
  1849. static char_u *
  1850. viminfo_filename(file)
  1851. char_u *file;
  1852. {
  1853. if (file == NULL || *file == NUL)
  1854. {
  1855. if (use_viminfo != NULL)
  1856. file = use_viminfo;
  1857. else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
  1858. {
  1859. #ifdef VIMINFO_FILE2
  1860. /* don't use $HOME when no…

Large files files are truncated, but you can click here to view the full file