PageRenderTime 78ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/vim7.0.243/src/ex_cmds.c

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

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