PageRenderTime 70ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/vim7.0/src/ops.c

#
C | 2620 lines | 2369 code | 81 blank | 170 comment | 200 complexity | fb88e3a9d9493fafc551190e91b63e0f 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. * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
  11. * op_change, op_yank, do_put, do_join
  12. */
  13. #include "vim.h"
  14. /*
  15. * Number of registers.
  16. * 0 = unnamed register, for normal yanks and puts
  17. * 1..9 = registers '1' to '9', for deletes
  18. * 10..35 = registers 'a' to 'z'
  19. * 36 = delete register '-'
  20. * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
  21. * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
  22. */
  23. /*
  24. * Symbolic names for some registers.
  25. */
  26. #define DELETION_REGISTER 36
  27. #ifdef FEAT_CLIPBOARD
  28. # define STAR_REGISTER 37
  29. # ifdef FEAT_X11
  30. # define PLUS_REGISTER 38
  31. # else
  32. # define PLUS_REGISTER STAR_REGISTER /* there is only one */
  33. # endif
  34. #endif
  35. #ifdef FEAT_DND
  36. # define TILDE_REGISTER (PLUS_REGISTER + 1)
  37. #endif
  38. #ifdef FEAT_CLIPBOARD
  39. # ifdef FEAT_DND
  40. # define NUM_REGISTERS (TILDE_REGISTER + 1)
  41. # else
  42. # define NUM_REGISTERS (PLUS_REGISTER + 1)
  43. # endif
  44. #else
  45. # define NUM_REGISTERS 37
  46. #endif
  47. /*
  48. * Each yank register is an array of pointers to lines.
  49. */
  50. static struct yankreg
  51. {
  52. char_u **y_array; /* pointer to array of line pointers */
  53. linenr_T y_size; /* number of lines in y_array */
  54. char_u y_type; /* MLINE, MCHAR or MBLOCK */
  55. #ifdef FEAT_VISUAL
  56. colnr_T y_width; /* only set if y_type == MBLOCK */
  57. #endif
  58. } y_regs[NUM_REGISTERS];
  59. static struct yankreg *y_current; /* ptr to current yankreg */
  60. static int y_append; /* TRUE when appending */
  61. static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
  62. /*
  63. * structure used by block_prep, op_delete and op_yank for blockwise operators
  64. * also op_change, op_shift, op_insert, op_replace - AKelly
  65. */
  66. struct block_def
  67. {
  68. int startspaces; /* 'extra' cols of first char */
  69. int endspaces; /* 'extra' cols of first char */
  70. int textlen; /* chars in block */
  71. char_u *textstart; /* pointer to 1st char in block */
  72. colnr_T textcol; /* cols of chars (at least part.) in block */
  73. colnr_T start_vcol; /* start col of 1st char wholly inside block */
  74. colnr_T end_vcol; /* start col of 1st char wholly after block */
  75. #ifdef FEAT_VISUALEXTRA
  76. int is_short; /* TRUE if line is too short to fit in block */
  77. int is_MAX; /* TRUE if curswant==MAXCOL when starting */
  78. int is_oneChar; /* TRUE if block within one character */
  79. int pre_whitesp; /* screen cols of ws before block */
  80. int pre_whitesp_c; /* chars of ws before block */
  81. colnr_T end_char_vcols; /* number of vcols of post-block char */
  82. #endif
  83. colnr_T start_char_vcols; /* number of vcols of pre-block char */
  84. };
  85. #ifdef FEAT_VISUALEXTRA
  86. static void shift_block __ARGS((oparg_T *oap, int amount));
  87. static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
  88. #endif
  89. static int stuff_yank __ARGS((int, char_u *));
  90. static void put_reedit_in_typebuf __ARGS((int silent));
  91. static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
  92. int silent));
  93. static void stuffescaped __ARGS((char_u *arg, int literally));
  94. #ifdef FEAT_MBYTE
  95. static void mb_adjust_opend __ARGS((oparg_T *oap));
  96. #endif
  97. static void free_yank __ARGS((long));
  98. static void free_yank_all __ARGS((void));
  99. static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
  100. #ifdef FEAT_CLIPBOARD
  101. static void copy_yank_reg __ARGS((struct yankreg *reg));
  102. # if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
  103. static void may_set_selection __ARGS((void));
  104. # endif
  105. #endif
  106. static void dis_msg __ARGS((char_u *p, int skip_esc));
  107. #ifdef FEAT_VISUAL
  108. static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
  109. #endif
  110. #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
  111. static void str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen));
  112. #endif
  113. static int ends_in_white __ARGS((linenr_T lnum));
  114. #ifdef FEAT_COMMENTS
  115. static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
  116. static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
  117. #else
  118. static int fmt_check_par __ARGS((linenr_T));
  119. #endif
  120. /*
  121. * The names of operators.
  122. * IMPORTANT: Index must correspond with defines in vim.h!!!
  123. * The third field indicates whether the operator always works on lines.
  124. */
  125. static char opchars[][3] =
  126. {
  127. {NUL, NUL, FALSE}, /* OP_NOP */
  128. {'d', NUL, FALSE}, /* OP_DELETE */
  129. {'y', NUL, FALSE}, /* OP_YANK */
  130. {'c', NUL, FALSE}, /* OP_CHANGE */
  131. {'<', NUL, TRUE}, /* OP_LSHIFT */
  132. {'>', NUL, TRUE}, /* OP_RSHIFT */
  133. {'!', NUL, TRUE}, /* OP_FILTER */
  134. {'g', '~', FALSE}, /* OP_TILDE */
  135. {'=', NUL, TRUE}, /* OP_INDENT */
  136. {'g', 'q', TRUE}, /* OP_FORMAT */
  137. {':', NUL, TRUE}, /* OP_COLON */
  138. {'g', 'U', FALSE}, /* OP_UPPER */
  139. {'g', 'u', FALSE}, /* OP_LOWER */
  140. {'J', NUL, TRUE}, /* DO_JOIN */
  141. {'g', 'J', TRUE}, /* DO_JOIN_NS */
  142. {'g', '?', FALSE}, /* OP_ROT13 */
  143. {'r', NUL, FALSE}, /* OP_REPLACE */
  144. {'I', NUL, FALSE}, /* OP_INSERT */
  145. {'A', NUL, FALSE}, /* OP_APPEND */
  146. {'z', 'f', TRUE}, /* OP_FOLD */
  147. {'z', 'o', TRUE}, /* OP_FOLDOPEN */
  148. {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
  149. {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
  150. {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
  151. {'z', 'd', TRUE}, /* OP_FOLDDEL */
  152. {'z', 'D', TRUE}, /* OP_FOLDDELREC */
  153. {'g', 'w', TRUE}, /* OP_FORMAT2 */
  154. {'g', '@', FALSE}, /* OP_FUNCTION */
  155. };
  156. /*
  157. * Translate a command name into an operator type.
  158. * Must only be called with a valid operator name!
  159. */
  160. int
  161. get_op_type(char1, char2)
  162. int char1;
  163. int char2;
  164. {
  165. int i;
  166. if (char1 == 'r') /* ignore second character */
  167. return OP_REPLACE;
  168. if (char1 == '~') /* when tilde is an operator */
  169. return OP_TILDE;
  170. for (i = 0; ; ++i)
  171. if (opchars[i][0] == char1 && opchars[i][1] == char2)
  172. break;
  173. return i;
  174. }
  175. #if defined(FEAT_VISUAL) || defined(PROTO)
  176. /*
  177. * Return TRUE if operator "op" always works on whole lines.
  178. */
  179. int
  180. op_on_lines(op)
  181. int op;
  182. {
  183. return opchars[op][2];
  184. }
  185. #endif
  186. /*
  187. * Get first operator command character.
  188. * Returns 'g' or 'z' if there is another command character.
  189. */
  190. int
  191. get_op_char(optype)
  192. int optype;
  193. {
  194. return opchars[optype][0];
  195. }
  196. /*
  197. * Get second operator command character.
  198. */
  199. int
  200. get_extra_op_char(optype)
  201. int optype;
  202. {
  203. return opchars[optype][1];
  204. }
  205. /*
  206. * op_shift - handle a shift operation
  207. */
  208. void
  209. op_shift(oap, curs_top, amount)
  210. oparg_T *oap;
  211. int curs_top;
  212. int amount;
  213. {
  214. long i;
  215. int first_char;
  216. char_u *s;
  217. #ifdef FEAT_VISUAL
  218. int block_col = 0;
  219. #endif
  220. if (u_save((linenr_T)(oap->start.lnum - 1),
  221. (linenr_T)(oap->end.lnum + 1)) == FAIL)
  222. return;
  223. #ifdef FEAT_VISUAL
  224. if (oap->block_mode)
  225. block_col = curwin->w_cursor.col;
  226. #endif
  227. for (i = oap->line_count; --i >= 0; )
  228. {
  229. first_char = *ml_get_curline();
  230. if (first_char == NUL) /* empty line */
  231. curwin->w_cursor.col = 0;
  232. #ifdef FEAT_VISUALEXTRA
  233. else if (oap->block_mode)
  234. shift_block(oap, amount);
  235. #endif
  236. else
  237. /* Move the line right if it doesn't start with '#', 'smartindent'
  238. * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
  239. #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
  240. if (first_char != '#' || !preprocs_left())
  241. #endif
  242. {
  243. shift_line(oap->op_type == OP_LSHIFT, p_sr, amount);
  244. }
  245. ++curwin->w_cursor.lnum;
  246. }
  247. changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
  248. #ifdef FEAT_VISUAL
  249. if (oap->block_mode)
  250. {
  251. curwin->w_cursor.lnum = oap->start.lnum;
  252. curwin->w_cursor.col = block_col;
  253. }
  254. else
  255. #endif
  256. if (curs_top) /* put cursor on first line, for ">>" */
  257. {
  258. curwin->w_cursor.lnum = oap->start.lnum;
  259. beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
  260. }
  261. else
  262. --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
  263. if (oap->line_count > p_report)
  264. {
  265. if (oap->op_type == OP_RSHIFT)
  266. s = (char_u *)">";
  267. else
  268. s = (char_u *)"<";
  269. if (oap->line_count == 1)
  270. {
  271. if (amount == 1)
  272. sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
  273. else
  274. sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
  275. }
  276. else
  277. {
  278. if (amount == 1)
  279. sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
  280. oap->line_count, s);
  281. else
  282. sprintf((char *)IObuff, _("%ld lines %sed %d times"),
  283. oap->line_count, s, amount);
  284. }
  285. msg(IObuff);
  286. }
  287. /*
  288. * Set "'[" and "']" marks.
  289. */
  290. curbuf->b_op_start = oap->start;
  291. curbuf->b_op_end.lnum = oap->end.lnum;
  292. curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
  293. if (curbuf->b_op_end.col > 0)
  294. --curbuf->b_op_end.col;
  295. }
  296. /*
  297. * shift the current line one shiftwidth left (if left != 0) or right
  298. * leaves cursor on first blank in the line
  299. */
  300. void
  301. shift_line(left, round, amount)
  302. int left;
  303. int round;
  304. int amount;
  305. {
  306. int count;
  307. int i, j;
  308. int p_sw = (int)curbuf->b_p_sw;
  309. count = get_indent(); /* get current indent */
  310. if (round) /* round off indent */
  311. {
  312. i = count / p_sw; /* number of p_sw rounded down */
  313. j = count % p_sw; /* extra spaces */
  314. if (j && left) /* first remove extra spaces */
  315. --amount;
  316. if (left)
  317. {
  318. i -= amount;
  319. if (i < 0)
  320. i = 0;
  321. }
  322. else
  323. i += amount;
  324. count = i * p_sw;
  325. }
  326. else /* original vi indent */
  327. {
  328. if (left)
  329. {
  330. count -= p_sw * amount;
  331. if (count < 0)
  332. count = 0;
  333. }
  334. else
  335. count += p_sw * amount;
  336. }
  337. /* Set new indent */
  338. #ifdef FEAT_VREPLACE
  339. if (State & VREPLACE_FLAG)
  340. change_indent(INDENT_SET, count, FALSE, NUL);
  341. else
  342. #endif
  343. (void)set_indent(count, SIN_CHANGED);
  344. }
  345. #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
  346. /*
  347. * Shift one line of the current block one shiftwidth right or left.
  348. * Leaves cursor on first character in block.
  349. */
  350. static void
  351. shift_block(oap, amount)
  352. oparg_T *oap;
  353. int amount;
  354. {
  355. int left = (oap->op_type == OP_LSHIFT);
  356. int oldstate = State;
  357. int total, split;
  358. char_u *newp, *oldp, *midp, *ptr;
  359. int oldcol = curwin->w_cursor.col;
  360. int p_sw = (int)curbuf->b_p_sw;
  361. int p_ts = (int)curbuf->b_p_ts;
  362. struct block_def bd;
  363. int internal = 0;
  364. int incr;
  365. colnr_T vcol, col = 0, ws_vcol;
  366. int i = 0, j = 0;
  367. int len;
  368. #ifdef FEAT_RIGHTLEFT
  369. int old_p_ri = p_ri;
  370. p_ri = 0; /* don't want revins in ident */
  371. #endif
  372. State = INSERT; /* don't want REPLACE for State */
  373. block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
  374. if (bd.is_short)
  375. return;
  376. /* total is number of screen columns to be inserted/removed */
  377. total = amount * p_sw;
  378. oldp = ml_get_curline();
  379. if (!left)
  380. {
  381. /*
  382. * 1. Get start vcol
  383. * 2. Total ws vcols
  384. * 3. Divvy into TABs & spp
  385. * 4. Construct new string
  386. */
  387. total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
  388. ws_vcol = bd.start_vcol - bd.pre_whitesp;
  389. if (bd.startspaces)
  390. {
  391. #ifdef FEAT_MBYTE
  392. if (has_mbyte)
  393. bd.textstart += (*mb_ptr2len)(bd.textstart);
  394. #endif
  395. ++bd.textstart;
  396. }
  397. for ( ; vim_iswhite(*bd.textstart); )
  398. {
  399. incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
  400. total += incr;
  401. bd.start_vcol += incr;
  402. }
  403. /* OK, now total=all the VWS reqd, and textstart points at the 1st
  404. * non-ws char in the block. */
  405. if (!curbuf->b_p_et)
  406. i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
  407. if (i)
  408. j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
  409. else
  410. j = total;
  411. /* if we're splitting a TAB, allow for it */
  412. bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
  413. len = (int)STRLEN(bd.textstart) + 1;
  414. newp = alloc_check((unsigned)(bd.textcol + i + j + len));
  415. if (newp == NULL)
  416. return;
  417. vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
  418. mch_memmove(newp, oldp, (size_t)bd.textcol);
  419. copy_chars(newp + bd.textcol, (size_t)i, TAB);
  420. copy_spaces(newp + bd.textcol + i, (size_t)j);
  421. /* the end */
  422. mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
  423. }
  424. else /* left */
  425. {
  426. vcol = oap->start_vcol;
  427. /* walk vcol past ws to be removed */
  428. for (midp = oldp + bd.textcol;
  429. vcol < (oap->start_vcol + total) && vim_iswhite(*midp); )
  430. {
  431. incr = lbr_chartabsize_adv(&midp, (colnr_T)vcol);
  432. vcol += incr;
  433. }
  434. /* internal is the block-internal ws replacing a split TAB */
  435. if (vcol > (oap->start_vcol + total))
  436. {
  437. /* we have to split the TAB *(midp-1) */
  438. internal = vcol - (oap->start_vcol + total);
  439. }
  440. /* if 'expandtab' is not set, use TABs */
  441. split = bd.startspaces + internal;
  442. if (split > 0)
  443. {
  444. if (!curbuf->b_p_et)
  445. {
  446. for (ptr = oldp, col = 0; ptr < oldp+bd.textcol; )
  447. col += lbr_chartabsize_adv(&ptr, (colnr_T)col);
  448. /* col+1 now equals the start col of the first char of the
  449. * block (may be < oap.start_vcol if we're splitting a TAB) */
  450. i = ((col % p_ts) + split) / p_ts; /* number of tabs */
  451. }
  452. if (i)
  453. j = ((col % p_ts) + split) % p_ts; /* number of spp */
  454. else
  455. j = split;
  456. }
  457. newp = alloc_check(bd.textcol + i + j + (unsigned)STRLEN(midp) + 1);
  458. if (newp == NULL)
  459. return;
  460. vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + STRLEN(midp) + 1));
  461. /* copy first part we want to keep */
  462. mch_memmove(newp, oldp, (size_t)bd.textcol);
  463. /* Now copy any TABS and spp to ensure correct alignment! */
  464. while (vim_iswhite(*midp))
  465. {
  466. if (*midp == TAB)
  467. i++;
  468. else /*space */
  469. j++;
  470. midp++;
  471. }
  472. /* We might have an extra TAB worth of spp now! */
  473. if (j / p_ts && !curbuf->b_p_et)
  474. {
  475. i++;
  476. j -= p_ts;
  477. }
  478. copy_chars(newp + bd.textcol, (size_t)i, TAB);
  479. copy_spaces(newp + bd.textcol + i, (size_t)j);
  480. /* the end */
  481. mch_memmove(newp + STRLEN(newp), midp, (size_t)STRLEN(midp) + 1);
  482. }
  483. /* replace the line */
  484. ml_replace(curwin->w_cursor.lnum, newp, FALSE);
  485. changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
  486. State = oldstate;
  487. curwin->w_cursor.col = oldcol;
  488. #ifdef FEAT_RIGHTLEFT
  489. p_ri = old_p_ri;
  490. #endif
  491. }
  492. #endif
  493. #ifdef FEAT_VISUALEXTRA
  494. /*
  495. * Insert string "s" (b_insert ? before : after) block :AKelly
  496. * Caller must prepare for undo.
  497. */
  498. static void
  499. block_insert(oap, s, b_insert, bdp)
  500. oparg_T *oap;
  501. char_u *s;
  502. int b_insert;
  503. struct block_def *bdp;
  504. {
  505. int p_ts;
  506. int count = 0; /* extra spaces to replace a cut TAB */
  507. int spaces = 0; /* non-zero if cutting a TAB */
  508. colnr_T offset; /* pointer along new line */
  509. unsigned s_len; /* STRLEN(s) */
  510. char_u *newp, *oldp; /* new, old lines */
  511. linenr_T lnum; /* loop var */
  512. int oldstate = State;
  513. State = INSERT; /* don't want REPLACE for State */
  514. s_len = (unsigned)STRLEN(s);
  515. for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
  516. {
  517. block_prep(oap, bdp, lnum, TRUE);
  518. if (bdp->is_short && b_insert)
  519. continue; /* OP_INSERT, line ends before block start */
  520. oldp = ml_get(lnum);
  521. if (b_insert)
  522. {
  523. p_ts = bdp->start_char_vcols;
  524. spaces = bdp->startspaces;
  525. if (spaces != 0)
  526. count = p_ts - 1; /* we're cutting a TAB */
  527. offset = bdp->textcol;
  528. }
  529. else /* append */
  530. {
  531. p_ts = bdp->end_char_vcols;
  532. if (!bdp->is_short) /* spaces = padding after block */
  533. {
  534. spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
  535. if (spaces != 0)
  536. count = p_ts - 1; /* we're cutting a TAB */
  537. offset = bdp->textcol + bdp->textlen - (spaces != 0);
  538. }
  539. else /* spaces = padding to block edge */
  540. {
  541. /* if $ used, just append to EOL (ie spaces==0) */
  542. if (!bdp->is_MAX)
  543. spaces = (oap->end_vcol - bdp->end_vcol) + 1;
  544. count = spaces;
  545. offset = bdp->textcol + bdp->textlen;
  546. }
  547. }
  548. newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
  549. if (newp == NULL)
  550. continue;
  551. /* copy up to shifted part */
  552. mch_memmove(newp, oldp, (size_t)(offset));
  553. oldp += offset;
  554. /* insert pre-padding */
  555. copy_spaces(newp + offset, (size_t)spaces);
  556. /* copy the new text */
  557. mch_memmove(newp + offset + spaces, s, (size_t)s_len);
  558. offset += s_len;
  559. if (spaces && !bdp->is_short)
  560. {
  561. /* insert post-padding */
  562. copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
  563. /* We're splitting a TAB, don't copy it. */
  564. oldp++;
  565. /* We allowed for that TAB, remember this now */
  566. count++;
  567. }
  568. if (spaces > 0)
  569. offset += count;
  570. mch_memmove(newp + offset, oldp, (size_t)(STRLEN(oldp) + 1));
  571. ml_replace(lnum, newp, FALSE);
  572. if (lnum == oap->end.lnum)
  573. {
  574. /* Set "']" mark to the end of the block instead of the end of
  575. * the insert in the first line. */
  576. curbuf->b_op_end.lnum = oap->end.lnum;
  577. curbuf->b_op_end.col = offset;
  578. }
  579. } /* for all lnum */
  580. changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
  581. State = oldstate;
  582. }
  583. #endif
  584. #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
  585. /*
  586. * op_reindent - handle reindenting a block of lines.
  587. */
  588. void
  589. op_reindent(oap, how)
  590. oparg_T *oap;
  591. int (*how) __ARGS((void));
  592. {
  593. long i;
  594. char_u *l;
  595. int count;
  596. linenr_T first_changed = 0;
  597. linenr_T last_changed = 0;
  598. linenr_T start_lnum = curwin->w_cursor.lnum;
  599. /* Don't even try when 'modifiable' is off. */
  600. if (!curbuf->b_p_ma)
  601. {
  602. EMSG(_(e_modifiable));
  603. return;
  604. }
  605. for (i = oap->line_count; --i >= 0 && !got_int; )
  606. {
  607. /* it's a slow thing to do, so give feedback so there's no worry that
  608. * the computer's just hung. */
  609. if (i > 1
  610. && (i % 50 == 0 || i == oap->line_count - 1)
  611. && oap->line_count > p_report)
  612. smsg((char_u *)_("%ld lines to indent... "), i);
  613. /*
  614. * Be vi-compatible: For lisp indenting the first line is not
  615. * indented, unless there is only one line.
  616. */
  617. #ifdef FEAT_LISP
  618. if (i != oap->line_count - 1 || oap->line_count == 1
  619. || how != get_lisp_indent)
  620. #endif
  621. {
  622. l = skipwhite(ml_get_curline());
  623. if (*l == NUL) /* empty or blank line */
  624. count = 0;
  625. else
  626. count = how(); /* get the indent for this line */
  627. if (set_indent(count, SIN_UNDO))
  628. {
  629. /* did change the indent, call changed_lines() later */
  630. if (first_changed == 0)
  631. first_changed = curwin->w_cursor.lnum;
  632. last_changed = curwin->w_cursor.lnum;
  633. }
  634. }
  635. ++curwin->w_cursor.lnum;
  636. }
  637. /* put cursor on first non-blank of indented line */
  638. curwin->w_cursor.lnum = start_lnum;
  639. beginline(BL_SOL | BL_FIX);
  640. /* Mark changed lines so that they will be redrawn. When Visual
  641. * highlighting was present, need to continue until the last line. When
  642. * there is no change still need to remove the Visual highlighting. */
  643. if (last_changed != 0)
  644. changed_lines(first_changed, 0,
  645. #ifdef FEAT_VISUAL
  646. oap->is_VIsual ? start_lnum + oap->line_count :
  647. #endif
  648. last_changed + 1, 0L);
  649. #ifdef FEAT_VISUAL
  650. else if (oap->is_VIsual)
  651. redraw_curbuf_later(INVERTED);
  652. #endif
  653. if (oap->line_count > p_report)
  654. {
  655. i = oap->line_count - (i + 1);
  656. if (i == 1)
  657. MSG(_("1 line indented "));
  658. else
  659. smsg((char_u *)_("%ld lines indented "), i);
  660. }
  661. /* set '[ and '] marks */
  662. curbuf->b_op_start = oap->start;
  663. curbuf->b_op_end = oap->end;
  664. }
  665. #endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
  666. #if defined(FEAT_EVAL) || defined(PROTO)
  667. /*
  668. * Keep the last expression line here, for repeating.
  669. */
  670. static char_u *expr_line = NULL;
  671. /*
  672. * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
  673. * Returns '=' when OK, NUL otherwise.
  674. */
  675. int
  676. get_expr_register()
  677. {
  678. char_u *new_line;
  679. new_line = getcmdline('=', 0L, 0);
  680. if (new_line == NULL)
  681. return NUL;
  682. if (*new_line == NUL) /* use previous line */
  683. vim_free(new_line);
  684. else
  685. set_expr_line(new_line);
  686. return '=';
  687. }
  688. /*
  689. * Set the expression for the '=' register.
  690. * Argument must be an allocated string.
  691. */
  692. void
  693. set_expr_line(new_line)
  694. char_u *new_line;
  695. {
  696. vim_free(expr_line);
  697. expr_line = new_line;
  698. }
  699. /*
  700. * Get the result of the '=' register expression.
  701. * Returns a pointer to allocated memory, or NULL for failure.
  702. */
  703. char_u *
  704. get_expr_line()
  705. {
  706. char_u *expr_copy;
  707. char_u *rv;
  708. static int nested = 0;
  709. if (expr_line == NULL)
  710. return NULL;
  711. /* Make a copy of the expression, because evaluating it may cause it to be
  712. * changed. */
  713. expr_copy = vim_strsave(expr_line);
  714. if (expr_copy == NULL)
  715. return NULL;
  716. /* When we are invoked recursively limit the evaluation to 10 levels.
  717. * Then return the string as-is. */
  718. if (nested >= 10)
  719. return expr_copy;
  720. ++nested;
  721. rv = eval_to_string(expr_copy, NULL, TRUE);
  722. --nested;
  723. vim_free(expr_copy);
  724. return rv;
  725. }
  726. /*
  727. * Get the '=' register expression itself, without evaluating it.
  728. */
  729. char_u *
  730. get_expr_line_src()
  731. {
  732. if (expr_line == NULL)
  733. return NULL;
  734. return vim_strsave(expr_line);
  735. }
  736. #endif /* FEAT_EVAL */
  737. /*
  738. * Check if 'regname' is a valid name of a yank register.
  739. * Note: There is no check for 0 (default register), caller should do this
  740. */
  741. int
  742. valid_yank_reg(regname, writing)
  743. int regname;
  744. int writing; /* if TRUE check for writable registers */
  745. {
  746. if ( (regname > 0 && ASCII_ISALNUM(regname))
  747. || (!writing && vim_strchr((char_u *)
  748. #ifdef FEAT_EVAL
  749. "/.%#:="
  750. #else
  751. "/.%#:"
  752. #endif
  753. , regname) != NULL)
  754. || regname == '"'
  755. || regname == '-'
  756. || regname == '_'
  757. #ifdef FEAT_CLIPBOARD
  758. || regname == '*'
  759. || regname == '+'
  760. #endif
  761. #ifdef FEAT_DND
  762. || (!writing && regname == '~')
  763. #endif
  764. )
  765. return TRUE;
  766. return FALSE;
  767. }
  768. /*
  769. * Set y_current and y_append, according to the value of "regname".
  770. * Cannot handle the '_' register.
  771. * Must only be called with a valid register name!
  772. *
  773. * If regname is 0 and writing, use register 0
  774. * If regname is 0 and reading, use previous register
  775. */
  776. void
  777. get_yank_register(regname, writing)
  778. int regname;
  779. int writing;
  780. {
  781. int i;
  782. y_append = FALSE;
  783. if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
  784. {
  785. y_current = y_previous;
  786. return;
  787. }
  788. i = regname;
  789. if (VIM_ISDIGIT(i))
  790. i -= '0';
  791. else if (ASCII_ISLOWER(i))
  792. i = CharOrdLow(i) + 10;
  793. else if (ASCII_ISUPPER(i))
  794. {
  795. i = CharOrdUp(i) + 10;
  796. y_append = TRUE;
  797. }
  798. else if (regname == '-')
  799. i = DELETION_REGISTER;
  800. #ifdef FEAT_CLIPBOARD
  801. /* When selection is not available, use register 0 instead of '*' */
  802. else if (clip_star.available && regname == '*')
  803. i = STAR_REGISTER;
  804. /* When clipboard is not available, use register 0 instead of '+' */
  805. else if (clip_plus.available && regname == '+')
  806. i = PLUS_REGISTER;
  807. #endif
  808. #ifdef FEAT_DND
  809. else if (!writing && regname == '~')
  810. i = TILDE_REGISTER;
  811. #endif
  812. else /* not 0-9, a-z, A-Z or '-': use register 0 */
  813. i = 0;
  814. y_current = &(y_regs[i]);
  815. if (writing) /* remember the register we write into for do_put() */
  816. y_previous = y_current;
  817. }
  818. #if defined(FEAT_CLIPBOARD) || defined(PROTO)
  819. /*
  820. * When "regname" is a clipboard register, obtain the selection. If it's not
  821. * available return zero, otherwise return "regname".
  822. */
  823. int
  824. may_get_selection(regname)
  825. int regname;
  826. {
  827. if (regname == '*')
  828. {
  829. if (!clip_star.available)
  830. regname = 0;
  831. else
  832. clip_get_selection(&clip_star);
  833. }
  834. else if (regname == '+')
  835. {
  836. if (!clip_plus.available)
  837. regname = 0;
  838. else
  839. clip_get_selection(&clip_plus);
  840. }
  841. return regname;
  842. }
  843. #endif
  844. #if defined(FEAT_VISUAL) || defined(PROTO)
  845. /*
  846. * Obtain the contents of a "normal" register. The register is made empty.
  847. * The returned pointer has allocated memory, use put_register() later.
  848. */
  849. void *
  850. get_register(name, copy)
  851. int name;
  852. int copy; /* make a copy, if FALSE make register empty. */
  853. {
  854. static struct yankreg *reg;
  855. int i;
  856. #ifdef FEAT_CLIPBOARD
  857. /* When Visual area changed, may have to update selection. Obtain the
  858. * selection too. */
  859. if (name == '*' && clip_star.available && clip_isautosel())
  860. {
  861. clip_update_selection();
  862. may_get_selection(name);
  863. }
  864. #endif
  865. get_yank_register(name, 0);
  866. reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
  867. if (reg != NULL)
  868. {
  869. *reg = *y_current;
  870. if (copy)
  871. {
  872. /* If we run out of memory some or all of the lines are empty. */
  873. if (reg->y_size == 0)
  874. reg->y_array = NULL;
  875. else
  876. reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
  877. * reg->y_size));
  878. if (reg->y_array != NULL)
  879. {
  880. for (i = 0; i < reg->y_size; ++i)
  881. reg->y_array[i] = vim_strsave(y_current->y_array[i]);
  882. }
  883. }
  884. else
  885. y_current->y_array = NULL;
  886. }
  887. return (void *)reg;
  888. }
  889. /*
  890. * Put "reg" into register "name". Free any previous contents.
  891. */
  892. void
  893. put_register(name, reg)
  894. int name;
  895. void *reg;
  896. {
  897. get_yank_register(name, 0);
  898. free_yank_all();
  899. *y_current = *(struct yankreg *)reg;
  900. # ifdef FEAT_CLIPBOARD
  901. /* Send text written to clipboard register to the clipboard. */
  902. may_set_selection();
  903. # endif
  904. }
  905. #endif
  906. #if defined(FEAT_MOUSE) || defined(PROTO)
  907. /*
  908. * return TRUE if the current yank register has type MLINE
  909. */
  910. int
  911. yank_register_mline(regname)
  912. int regname;
  913. {
  914. if (regname != 0 && !valid_yank_reg(regname, FALSE))
  915. return FALSE;
  916. if (regname == '_') /* black hole is always empty */
  917. return FALSE;
  918. get_yank_register(regname, FALSE);
  919. return (y_current->y_type == MLINE);
  920. }
  921. #endif
  922. /*
  923. * start or stop recording into a yank register
  924. *
  925. * return FAIL for failure, OK otherwise
  926. */
  927. int
  928. do_record(c)
  929. int c;
  930. {
  931. char_u *p;
  932. static int regname;
  933. struct yankreg *old_y_previous, *old_y_current;
  934. int retval;
  935. if (Recording == FALSE) /* start recording */
  936. {
  937. /* registers 0-9, a-z and " are allowed */
  938. if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
  939. retval = FAIL;
  940. else
  941. {
  942. Recording = TRUE;
  943. showmode();
  944. regname = c;
  945. retval = OK;
  946. }
  947. }
  948. else /* stop recording */
  949. {
  950. /*
  951. * Get the recorded key hits. K_SPECIAL and CSI will be escaped, so
  952. * that the register can be put into the typeahead buffer without
  953. * translation.
  954. */
  955. Recording = FALSE;
  956. MSG("");
  957. p = get_recorded();
  958. if (p == NULL)
  959. retval = FAIL;
  960. else
  961. {
  962. /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
  963. vim_unescape_csi(p);
  964. /*
  965. * We don't want to change the default register here, so save and
  966. * restore the current register name.
  967. */
  968. old_y_previous = y_previous;
  969. old_y_current = y_current;
  970. retval = stuff_yank(regname, p);
  971. y_previous = old_y_previous;
  972. y_current = old_y_current;
  973. }
  974. }
  975. return retval;
  976. }
  977. /*
  978. * Stuff string "p" into yank register "regname" as a single line (append if
  979. * uppercase). "p" must have been alloced.
  980. *
  981. * return FAIL for failure, OK otherwise
  982. */
  983. static int
  984. stuff_yank(regname, p)
  985. int regname;
  986. char_u *p;
  987. {
  988. char_u *lp;
  989. char_u **pp;
  990. /* check for read-only register */
  991. if (regname != 0 && !valid_yank_reg(regname, TRUE))
  992. {
  993. vim_free(p);
  994. return FAIL;
  995. }
  996. if (regname == '_') /* black hole: don't do anything */
  997. {
  998. vim_free(p);
  999. return OK;
  1000. }
  1001. get_yank_register(regname, TRUE);
  1002. if (y_append && y_current->y_array != NULL)
  1003. {
  1004. pp = &(y_current->y_array[y_current->y_size - 1]);
  1005. lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
  1006. if (lp == NULL)
  1007. {
  1008. vim_free(p);
  1009. return FAIL;
  1010. }
  1011. STRCPY(lp, *pp);
  1012. STRCAT(lp, p);
  1013. vim_free(p);
  1014. vim_free(*pp);
  1015. *pp = lp;
  1016. }
  1017. else
  1018. {
  1019. free_yank_all();
  1020. if ((y_current->y_array =
  1021. (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
  1022. {
  1023. vim_free(p);
  1024. return FAIL;
  1025. }
  1026. y_current->y_array[0] = p;
  1027. y_current->y_size = 1;
  1028. y_current->y_type = MCHAR; /* used to be MLINE, why? */
  1029. }
  1030. return OK;
  1031. }
  1032. /*
  1033. * execute a yank register: copy it into the stuff buffer
  1034. *
  1035. * return FAIL for failure, OK otherwise
  1036. */
  1037. int
  1038. do_execreg(regname, colon, addcr, silent)
  1039. int regname;
  1040. int colon; /* insert ':' before each line */
  1041. int addcr; /* always add '\n' to end of line */
  1042. int silent; /* set "silent" flag in typeahead buffer */
  1043. {
  1044. static int lastc = NUL;
  1045. long i;
  1046. char_u *p;
  1047. int retval = OK;
  1048. int remap;
  1049. if (regname == '@') /* repeat previous one */
  1050. {
  1051. if (lastc == NUL)
  1052. {
  1053. EMSG(_("E748: No previously used register"));
  1054. return FAIL;
  1055. }
  1056. regname = lastc;
  1057. }
  1058. /* check for valid regname */
  1059. if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
  1060. {
  1061. emsg_invreg(regname);
  1062. return FAIL;
  1063. }
  1064. lastc = regname;
  1065. #ifdef FEAT_CLIPBOARD
  1066. regname = may_get_selection(regname);
  1067. #endif
  1068. if (regname == '_') /* black hole: don't stuff anything */
  1069. return OK;
  1070. #ifdef FEAT_CMDHIST
  1071. if (regname == ':') /* use last command line */
  1072. {
  1073. if (last_cmdline == NULL)
  1074. {
  1075. EMSG(_(e_nolastcmd));
  1076. return FAIL;
  1077. }
  1078. vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
  1079. new_last_cmdline = NULL;
  1080. /* Escape all control characters with a CTRL-V */
  1081. p = vim_strsave_escaped_ext(last_cmdline,
  1082. (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE);
  1083. if (p != NULL)
  1084. {
  1085. /* When in Visual mode "'<,'>" will be prepended to the command.
  1086. * Remove it when it's already there. */
  1087. if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
  1088. retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
  1089. else
  1090. retval = put_in_typebuf(p, TRUE, TRUE, silent);
  1091. }
  1092. vim_free(p);
  1093. }
  1094. #endif
  1095. #ifdef FEAT_EVAL
  1096. else if (regname == '=')
  1097. {
  1098. p = get_expr_line();
  1099. if (p == NULL)
  1100. return FAIL;
  1101. retval = put_in_typebuf(p, TRUE, colon, silent);
  1102. vim_free(p);
  1103. }
  1104. #endif
  1105. else if (regname == '.') /* use last inserted text */
  1106. {
  1107. p = get_last_insert_save();
  1108. if (p == NULL)
  1109. {
  1110. EMSG(_(e_noinstext));
  1111. return FAIL;
  1112. }
  1113. retval = put_in_typebuf(p, FALSE, colon, silent);
  1114. vim_free(p);
  1115. }
  1116. else
  1117. {
  1118. get_yank_register(regname, FALSE);
  1119. if (y_current->y_array == NULL)
  1120. return FAIL;
  1121. /* Disallow remaping for ":@r". */
  1122. remap = colon ? REMAP_NONE : REMAP_YES;
  1123. /*
  1124. * Insert lines into typeahead buffer, from last one to first one.
  1125. */
  1126. put_reedit_in_typebuf(silent);
  1127. for (i = y_current->y_size; --i >= 0; )
  1128. {
  1129. char_u *escaped;
  1130. /* insert NL between lines and after last line if type is MLINE */
  1131. if (y_current->y_type == MLINE || i < y_current->y_size - 1
  1132. || addcr)
  1133. {
  1134. if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
  1135. return FAIL;
  1136. }
  1137. escaped = vim_strsave_escape_csi(y_current->y_array[i]);
  1138. if (escaped == NULL)
  1139. return FAIL;
  1140. retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
  1141. vim_free(escaped);
  1142. if (retval == FAIL)
  1143. return FAIL;
  1144. if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
  1145. == FAIL)
  1146. return FAIL;
  1147. }
  1148. Exec_reg = TRUE; /* disable the 'q' command */
  1149. }
  1150. return retval;
  1151. }
  1152. /*
  1153. * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
  1154. * used only after other typeahead has been processed.
  1155. */
  1156. static void
  1157. put_reedit_in_typebuf(silent)
  1158. int silent;
  1159. {
  1160. char_u buf[3];
  1161. if (restart_edit != NUL)
  1162. {
  1163. if (restart_edit == 'V')
  1164. {
  1165. buf[0] = 'g';
  1166. buf[1] = 'R';
  1167. buf[2] = NUL;
  1168. }
  1169. else
  1170. {
  1171. buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
  1172. buf[1] = NUL;
  1173. }
  1174. if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
  1175. restart_edit = NUL;
  1176. }
  1177. }
  1178. static int
  1179. put_in_typebuf(s, esc, colon, silent)
  1180. char_u *s;
  1181. int esc; /* Escape CSI characters */
  1182. int colon; /* add ':' before the line */
  1183. int silent;
  1184. {
  1185. int retval = OK;
  1186. put_reedit_in_typebuf(silent);
  1187. if (colon)
  1188. retval = ins_typebuf((char_u *)"\n", REMAP_YES, 0, TRUE, silent);
  1189. if (retval == OK)
  1190. {
  1191. char_u *p;
  1192. if (esc)
  1193. p = vim_strsave_escape_csi(s);
  1194. else
  1195. p = s;
  1196. if (p == NULL)
  1197. retval = FAIL;
  1198. else
  1199. retval = ins_typebuf(p, REMAP_YES, 0, TRUE, silent);
  1200. if (esc)
  1201. vim_free(p);
  1202. }
  1203. if (colon && retval == OK)
  1204. retval = ins_typebuf((char_u *)":", REMAP_YES, 0, TRUE, silent);
  1205. return retval;
  1206. }
  1207. /*
  1208. * Insert a yank register: copy it into the Read buffer.
  1209. * Used by CTRL-R command and middle mouse button in insert mode.
  1210. *
  1211. * return FAIL for failure, OK otherwise
  1212. */
  1213. int
  1214. insert_reg(regname, literally)
  1215. int regname;
  1216. int literally; /* insert literally, not as if typed */
  1217. {
  1218. long i;
  1219. int retval = OK;
  1220. char_u *arg;
  1221. int allocated;
  1222. /*
  1223. * It is possible to get into an endless loop by having CTRL-R a in
  1224. * register a and then, in insert mode, doing CTRL-R a.
  1225. * If you hit CTRL-C, the loop will be broken here.
  1226. */
  1227. ui_breakcheck();
  1228. if (got_int)
  1229. return FAIL;
  1230. /* check for valid regname */
  1231. if (regname != NUL && !valid_yank_reg(regname, FALSE))
  1232. return FAIL;
  1233. #ifdef FEAT_CLIPBOARD
  1234. regname = may_get_selection(regname);
  1235. #endif
  1236. if (regname == '.') /* insert last inserted text */
  1237. retval = stuff_inserted(NUL, 1L, TRUE);
  1238. else if (get_spec_reg(regname, &arg, &allocated, TRUE))
  1239. {
  1240. if (arg == NULL)
  1241. return FAIL;
  1242. stuffescaped(arg, literally);
  1243. if (allocated)
  1244. vim_free(arg);
  1245. }
  1246. else /* name or number register */
  1247. {
  1248. get_yank_register(regname, FALSE);
  1249. if (y_current->y_array == NULL)
  1250. retval = FAIL;
  1251. else
  1252. {
  1253. for (i = 0; i < y_current->y_size; ++i)
  1254. {
  1255. stuffescaped(y_current->y_array[i], literally);
  1256. /*
  1257. * Insert a newline between lines and after last line if
  1258. * y_type is MLINE.
  1259. */
  1260. if (y_current->y_type == MLINE || i < y_current->y_size - 1)
  1261. stuffcharReadbuff('\n');
  1262. }
  1263. }
  1264. }
  1265. return retval;
  1266. }
  1267. /*
  1268. * Stuff a string into the typeahead buffer, such that edit() will insert it
  1269. * literally ("literally" TRUE) or interpret is as typed characters.
  1270. */
  1271. static void
  1272. stuffescaped(arg, literally)
  1273. char_u *arg;
  1274. int literally;
  1275. {
  1276. int c;
  1277. char_u *start;
  1278. while (*arg != NUL)
  1279. {
  1280. /* Stuff a sequence of normal ASCII characters, that's fast. Also
  1281. * stuff K_SPECIAL to get the effect of a special key when "literally"
  1282. * is TRUE. */
  1283. start = arg;
  1284. while ((*arg >= ' '
  1285. #ifndef EBCDIC
  1286. && *arg < DEL /* EBCDIC: chars above space are normal */
  1287. #endif
  1288. )
  1289. || (*arg == K_SPECIAL && !literally))
  1290. ++arg;
  1291. if (arg > start)
  1292. stuffReadbuffLen(start, (long)(arg - start));
  1293. /* stuff a single special character */
  1294. if (*arg != NUL)
  1295. {
  1296. #ifdef FEAT_MBYTE
  1297. if (has_mbyte)
  1298. c = mb_ptr2char_adv(&arg);
  1299. else
  1300. #endif
  1301. c = *arg++;
  1302. if (literally && ((c < ' ' && c != TAB) || c == DEL))
  1303. stuffcharReadbuff(Ctrl_V);
  1304. stuffcharReadbuff(c);
  1305. }
  1306. }
  1307. }
  1308. /*
  1309. * If "regname" is a special register, return a pointer to its value.
  1310. */
  1311. int
  1312. get_spec_reg(regname, argp, allocated, errmsg)
  1313. int regname;
  1314. char_u **argp;
  1315. int *allocated;
  1316. int errmsg; /* give error message when failing */
  1317. {
  1318. int cnt;
  1319. *argp = NULL;
  1320. *allocated = FALSE;
  1321. switch (regname)
  1322. {
  1323. case '%': /* file name */
  1324. if (errmsg)
  1325. check_fname(); /* will give emsg if not set */
  1326. *argp = curbuf->b_fname;
  1327. return TRUE;
  1328. case '#': /* alternate file name */
  1329. *argp = getaltfname(errmsg); /* may give emsg if not set */
  1330. return TRUE;
  1331. #ifdef FEAT_EVAL
  1332. case '=': /* result of expression */
  1333. *argp = get_expr_line();
  1334. *allocated = TRUE;
  1335. return TRUE;
  1336. #endif
  1337. case ':': /* last command line */
  1338. if (last_cmdline == NULL && errmsg)
  1339. EMSG(_(e_nolastcmd));
  1340. *argp = last_cmdline;
  1341. return TRUE;
  1342. case '/': /* last search-pattern */
  1343. if (last_search_pat() == NULL && errmsg)
  1344. EMSG(_(e_noprevre));
  1345. *argp = last_search_pat();
  1346. return TRUE;
  1347. case '.': /* last inserted text */
  1348. *argp = get_last_insert_save();
  1349. *allocated = TRUE;
  1350. if (*argp == NULL && errmsg)
  1351. EMSG(_(e_noinstext));
  1352. return TRUE;
  1353. #ifdef FEAT_SEARCHPATH
  1354. case Ctrl_F: /* Filename under cursor */
  1355. case Ctrl_P: /* Path under cursor, expand via "path" */
  1356. if (!errmsg)
  1357. return FALSE;
  1358. *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
  1359. | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
  1360. *allocated = TRUE;
  1361. return TRUE;
  1362. #endif
  1363. case Ctrl_W: /* word under cursor */
  1364. case Ctrl_A: /* WORD (mnemonic All) under cursor */
  1365. if (!errmsg)
  1366. return FALSE;
  1367. cnt = find_ident_under_cursor(argp, regname == Ctrl_W
  1368. ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
  1369. *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
  1370. *allocated = TRUE;
  1371. return TRUE;
  1372. case '_': /* black hole: always empty */
  1373. *argp = (char_u *)"";
  1374. return TRUE;
  1375. }
  1376. return FALSE;
  1377. }
  1378. /*
  1379. * Paste a yank register into the command line.
  1380. * Only for non-special registers.
  1381. * Used by CTRL-R command in command-line mode
  1382. * insert_reg() can't be used here, because special characters from the
  1383. * register contents will be interpreted as commands.
  1384. *
  1385. * return FAIL for failure, OK otherwise
  1386. */
  1387. int
  1388. cmdline_paste_reg(regname, literally, remcr)
  1389. int regname;
  1390. int literally; /* Insert text literally instead of "as typed" */
  1391. int remcr; /* don't add trailing CR */
  1392. {
  1393. long i;
  1394. get_yank_register(regname, FALSE);
  1395. if (y_current->y_array == NULL)
  1396. return FAIL;
  1397. for (i = 0; i < y_current->y_size; ++i)
  1398. {
  1399. cmdline_paste_str(y_current->y_array[i], literally);
  1400. /* Insert ^M between lines and after last line if type is MLINE.
  1401. * Don't do this when "remcr" is TRUE and the next line is empty. */
  1402. if (y_current->y_type == MLINE
  1403. || (i < y_current->y_size - 1
  1404. && !(remcr
  1405. && i == y_current->y_size - 2
  1406. && *y_current->y_array[i + 1] == NUL)))
  1407. cmdline_paste_str((char_u *)"\r", literally);
  1408. /* Check for CTRL-C, in case someone tries to paste a few thousand
  1409. * lines and gets bored. */
  1410. ui_breakcheck();
  1411. if (got_int)
  1412. return FAIL;
  1413. }
  1414. return OK;
  1415. }
  1416. #if defined(FEAT_CLIPBOARD) || defined(PROTO)
  1417. /*
  1418. * Adjust the register name pointed to with "rp" for the clipboard being
  1419. * used always and the clipboard being available.
  1420. */
  1421. void
  1422. adjust_clip_reg(rp)
  1423. int *rp;
  1424. {
  1425. /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */
  1426. if (*rp == 0 && clip_unnamed)
  1427. *rp = '*';
  1428. if (!clip_star.available && *rp == '*')
  1429. *rp = 0;
  1430. if (!clip_plus.available && *rp == '+')
  1431. *rp = 0;
  1432. }
  1433. #endif
  1434. /*
  1435. * op_delete - handle a delete operation
  1436. *
  1437. * return FAIL if undo failed, OK otherwise.
  1438. */
  1439. int
  1440. op_delete(oap)
  1441. oparg_T *oap;
  1442. {
  1443. int n;
  1444. linenr_T lnum;
  1445. char_u *ptr;
  1446. #ifdef FEAT_VISUAL
  1447. char_u *newp, *oldp;
  1448. struct block_def bd;
  1449. #endif
  1450. linenr_T old_lcount = curbuf->b_ml.ml_line_count;
  1451. int did_yank = FALSE;
  1452. if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
  1453. return OK;
  1454. /* Nothing to delete, return here. Do prepare undo, for op_change(). */
  1455. if (oap->empty)
  1456. return u_save_cursor();
  1457. if (!curbuf->b_p_ma)
  1458. {
  1459. EMSG(_(e_modifiable));
  1460. return FAIL;
  1461. }
  1462. #ifdef FEAT_CLIPBOARD
  1463. adjust_clip_reg(&oap->regname);
  1464. #endif
  1465. #ifdef FEAT_MBYTE
  1466. if (has_mbyte)
  1467. mb_adjust_opend(oap);
  1468. #endif
  1469. /*
  1470. * Imitate the strange Vi behaviour: If the delete spans more than one line
  1471. * and motion_type == MCHAR and the result is a blank line, make the delete
  1472. * linewise. Don't do this for the change command or Visual mode.
  1473. */
  1474. if ( oap->motion_type == MCHAR
  1475. #ifdef FEAT_VISUAL
  1476. && !oap->is_VIsual
  1477. && !oap->block_mode
  1478. #endif
  1479. && oap->line_count > 1
  1480. && oap->op_type == OP_DELETE)
  1481. {
  1482. ptr = ml_get(oap->end.lnum) + oap->end.col + oap->inclusive;
  1483. ptr = skipwhite(ptr);
  1484. if (*ptr == NUL && inindent(0))
  1485. oap->motion_type = MLINE;
  1486. }
  1487. /*
  1488. * Check for trying to delete (e.g. "D") in an empty line.
  1489. * Note: For the change operator it is ok.
  1490. */
  1491. if ( oap->motion_type == MCHAR
  1492. && oap->line_count == 1
  1493. && oap->op_type == OP_DELETE
  1494. && *ml_get(oap->start.lnum) == NUL)
  1495. {
  1496. /*
  1497. * It's an error to operate on an empty region, when 'E' included in
  1498. * 'cpoptions' (Vi compatible).
  1499. */
  1500. #ifdef FEAT_VIRTUALEDIT
  1501. if (virtual_op)
  1502. /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
  1503. * marks as if it happened. */
  1504. goto setmarks;
  1505. #endif
  1506. if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
  1507. beep_flush();
  1508. return OK;
  1509. }
  1510. /*
  1511. * Do a yank of whatever we're about to delete.
  1512. * If a yank register was specified, put the deleted text into that register.
  1513. * For the black hole register '_' don't yank anything.
  1514. */
  1515. if (oap->regname != '_')
  1516. {
  1517. if (oap->regname != 0)
  1518. {
  1519. /* check for read-only register */
  1520. if (!valid_yank_reg(oap->regname, TRUE))
  1521. {
  1522. beep_flush();
  1523. return OK;
  1524. }
  1525. get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
  1526. if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
  1527. did_yank = TRUE;
  1528. }
  1529. /*
  1530. * Put deleted text into register 1 and shift number registers if the
  1531. * delete contains a line break, or when a regname has been specified.
  1532. */
  1533. if (oap->regname != 0 || oap->motion_type == MLINE
  1534. || oap->line_count > 1 || oap->use_reg_one)
  1535. {
  1536. y_current = &y_regs[9];
  1537. free_yank_all(); /* free register nine */
  1538. for (n = 9; n > 1; --n)
  1539. y_regs[n] = y_regs[n - 1];
  1540. y_previous = y_current = &y_regs[1];
  1541. y_regs[1].y_array = NULL; /* set register one to empty */
  1542. if (op_yank(oap, TRUE, FALSE) == OK)
  1543. did_yank = TRUE;
  1544. }
  1545. /* Yank into small delete register when no register specified and the
  1546. * delete is within one line. */
  1547. if (oap->regname == 0 && oap->motion_type != MLINE
  1548. && oap->line_count == 1)
  1549. {
  1550. oap->regname = '-';
  1551. get_yank_register(oap->regname, TRUE);
  1552. if (op_yank(oap, TRUE, FALSE) == OK)
  1553. did_yank = TRUE;
  1554. oap->regname = 0;
  1555. }
  1556. /*
  1557. * If there's too much stuff to fit in the yank register, then get a
  1558. * confirmation before doing the delete. This is crude, but simple.
  1559. * And it avoids doing a delete of something we can't put back if we
  1560. * want.
  1561. */
  1562. if (!did_yank)
  1563. {
  1564. int msg_silent_save = msg_silent;
  1565. msg_silent = 0; /* must display the prompt */
  1566. n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
  1567. msg_silent = msg_silent_save;
  1568. if (n != 'y')
  1569. {
  1570. EMSG(_(e_abort));
  1571. return FAIL;
  1572. }
  1573. }
  1574. }
  1575. #ifdef FEAT_VISUAL
  1576. /*
  1577. * block mode delete
  1578. */
  1579. if (oap->block_mode)
  1580. {
  1581. if (u_save((linenr_T)(oap->start.lnum - 1),
  1582. (linenr_T)(oap->end.lnum + 1)) == FAIL)
  1583. return FAIL;
  1584. for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
  1585. {
  1586. block_prep(oap, &bd, lnum, TRUE);
  1587. if (bd.textlen == 0) /* nothing to delete */
  1588. continue;
  1589. /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
  1590. if (lnum == curwin->w_cursor.lnum)
  1591. {
  1592. curwin->w_cursor.col = bd.textcol + bd.startspaces;
  1593. # ifdef FEAT_VIRTUALEDIT
  1594. curwin->w_cursor.coladd = 0;
  1595. # endif
  1596. }
  1597. /* n == number of chars deleted
  1598. * If we delete a TAB, it may be replaced by several characters.
  1599. * Thus the number of characters may increase!
  1600. */
  1601. n = bd.textlen - bd.startspaces - bd.endspaces;
  1602. oldp = ml_get(lnum);
  1603. newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
  1604. if (newp == NULL)
  1605. continue;
  1606. /* copy up to deleted part */
  1607. mch_memmove(newp, oldp, (size_t)bd.textcol);
  1608. /* insert spaces */
  1609. copy_spaces(newp + bd.textcol,
  1610. (size_t)(bd.startspaces + bd.endspaces));
  1611. /* copy the part after the deleted part */
  1612. oldp += bd.textcol + bd.textlen;
  1613. mch_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces,
  1614. oldp, STRLEN(oldp) + 1);
  1615. /* replace the line */
  1616. ml_replace(lnum, newp, FALSE);
  1617. }
  1618. check_cursor_col();
  1619. changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
  1620. oap->end.lnum + 1, 0L);
  1621. oap->line_count = 0; /* no lines deleted */
  1622. }
  1623. else
  1624. #endif
  1625. if (oap->motion_type == MLINE)
  1626. {
  1627. if (oap->op_type == OP_CHANGE)
  1628. {
  1629. /* Delete the lines except the first one. Temporarily move the
  1630. * cursor to the next line. Save the current line number, if the
  1631. * last line is deleted it may be changed.
  1632. */
  1633. if (oap->line_count > 1)
  1634. {
  1635. lnum = curwin->w_cursor.lnum;
  1636. ++curwin->w_cursor.lnum;
  1637. del_lines((long)(oap->line_count - 1), TRUE);
  1638. curwin->w_cursor.lnum = lnum;
  1639. }
  1640. if (u_save_cursor() == FAIL)
  1641. return FAIL;
  1642. if (curbuf->b_p_ai) /* don't delete indent */
  1643. {
  1644. beginline(BL_WHITE); /* cursor on first non-white */
  1645. did_ai = TRUE; /* delete the indent when ESC hit */
  1646. ai_col = curwin->w_cursor.col;
  1647. }
  1648. else
  1649. beginline(0); /* cursor in column 0 */
  1650. truncate_line(FALSE); /* delete the rest of the line */
  1651. /* leave cursor past last char in line */
  1652. if (oap->line_count > 1)
  1653. u_clearline(); /* "U" command not possible after "2cc" */
  1654. }
  1655. else
  1656. {
  1657. del_lines(oap->line_count, TRUE);
  1658. beginline(BL_WHITE | BL_FIX);
  1659. u_clearline(); /* "U" command not possible after "dd" */
  1660. }
  1661. }
  1662. else
  1663. {
  1664. #ifdef FEAT_VIRTUALEDIT
  1665. if (virtual_op)
  1666. {
  1667. int endcol = 0;
  1668. /* For virtualedit: break the tabs that are partly included. */
  1669. if (gchar_pos(&oap->start) == '\t')
  1670. {
  1671. if (u_save_cursor() == FAIL) /* save first line for undo */
  1672. return FAIL;
  1673. if (oap->line_count == 1)
  1674. endcol = getviscol2(oap->end.col, oap->end.coladd);
  1675. coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
  1676. oap->start = curwin->w_cursor;
  1677. if (oap->line_count == 1)
  1678. {
  1679. coladvance(endcol);
  1680. oap->end.col = curwin->w_cursor.col;
  1681. oap->end.coladd = curwin->w_cursor.coladd;
  1682. curwin->w_cursor = oap->start;
  1683. }
  1684. }
  1685. /* Break a tab only when it's included in the area. */
  1686. if (gchar_pos(&oap->end) == '\t'
  1687. && (int)oap->end.coladd < oap->inclusive)
  1688. {
  1689. /* save last line for undo */
  1690. if (u_save((linenr_T)(oap->end.lnum - 1),
  1691. (linenr_T)(oap->end.lnum + 1)) == FAIL)
  1692. return FAIL;
  1693. curwin->w_cursor = oap->end;
  1694. coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
  1695. oap->end = curwin->w_cursor;
  1696. curwin->w_cursor = oap->start;
  1697. }
  1698. }
  1699. #endif
  1700. if (oap->line_count == 1) /* delete characters within one line */
  1701. {
  1702. if (u_save_cursor() == FAIL) /* save line for undo */
  1703. return FAIL;
  1704. /* if 'cpoptions' contains '$', display '$' at end of change */
  1705. if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
  1706. && oap->op_type == OP_CHANGE
  1707. && oap->end.lnum == curwin->w_cursor.lnum
  1708. #ifdef FEAT_VISUAL
  1709. && !oap->is_VIsual
  1710. #endif
  1711. )
  1712. display_dollar(oap->end.col - !oap->inclusive);
  1713. n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
  1714. #ifdef FEAT_VIRTUALEDIT
  1715. if (virtual_op)
  1716. {
  1717. /* fix up things for virtualedit-delete:
  1718. * break the tabs which are going to get in our way
  1719. */
  1720. char_u *curline = ml_get_curline();
  1721. int len = (int)STRLEN(curline);
  1722. if (oap->end.coladd != 0
  1723. && (int)oap->end.col >= len - 1
  1724. && !(oap->start.coladd && (int)oap->end.col >= len - 1))
  1725. n++;
  1726. /* Delete at least one char (e.g, when on a control char). */
  1727. if (n == 0 && oap->start.coladd != oap->end.coladd)
  1728. n = 1;
  1729. /* When deleted a char in the line, reset coladd. */
  1730. if (gchar_cursor() != NUL)
  1731. curwin->w_cursor.coladd = 0;
  1732. }
  1733. #endif
  1734. (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
  1735. #ifdef FEAT_VISUAL
  1736. && !oap->is_VIsual
  1737. #endif
  1738. );
  1739. }
  1740. else /* delete characters between lines */
  1741. {
  1742. pos_T curpos;
  1743. /* save deleted and changed lines for undo */
  1744. if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
  1745. (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
  1746. return FAIL;
  1747. truncate_line(TRUE); /* delete from cursor to end of line */
  1748. curpos = curwin->w_cursor; /* remember curwin->w_cursor */
  1749. ++curwin->w_cursor.lnum;
  1750. del_lines((long)(oap->line_count - 2), FALSE);
  1751. /* delete from start of line until op_end */
  1752. curwin->w_cursor.col = 0;
  1753. (void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive),
  1754. !virtual_op, oap->op_type == OP_DELETE
  1755. #ifdef FEAT_VISUAL
  1756. && !oap->is_VIsual
  1757. #endif
  1758. );
  1759. curwin->w_cursor = curpos; /* restore curwin->w_cursor */
  1760. (void)do_join(FALSE);
  1761. }
  1762. }
  1763. msgmore(curbuf->b_ml.ml_line_count - old_lcount);
  1764. #ifdef FEAT_VIRTUALEDIT
  1765. setmarks:
  1766. #endif
  1767. #ifdef FEAT_VISUAL
  1768. if (oap->block_mode)
  1769. {
  1770. curbuf->b_op_end.lnum = oap->end.lnum;
  1771. curbuf->b_op_end.col = oap->start.col;
  1772. }
  1773. else
  1774. #endif
  1775. curbuf->b_op_end = oap->start;
  1776. curbuf->b_op_start = oap->start;
  1777. return OK;
  1778. }
  1779. #ifdef FEAT_MBYTE
  1780. /*
  1781. * Adjust end of operating area for ending on a multi-byte character.
  1782. * Used for deletion.
  1783. */
  1784. static void
  1785. mb_adjust_opend(oap)
  1786. oparg_T *oap;
  1787. {
  1788. char_u *p;
  1789. if (oap->inclusive)
  1790. {
  1791. p = ml_get(oap->end.lnum);
  1792. oap->end.col += mb_tail_off(p, p + oap->end.col);
  1793. }
  1794. }
  1795. #endif
  1796. #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
  1797. /*
  1798. * Replace a whole area with one character.
  1799. */
  1800. int
  1801. op_replace(oap, c)
  1802. oparg_T *oap;
  1803. int c;
  1804. {
  1805. int n, numc;
  1806. #ifdef FEAT_MBYTE
  1807. int num_chars;
  1808. #endif
  1809. char_u *newp, *oldp;
  1810. size_t oldlen;
  1811. struct block_def bd;
  1812. if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
  1813. return OK; /* nothing to do */
  1814. #ifdef FEAT_MBYTE
  1815. if (has_mbyte)
  1816. mb_adjust_opend(oap);
  1817. #endif
  1818. if (u_save((linenr_T)(oap->start.lnum - 1),
  1819. (linenr_T)(oap->end.lnum + 1)) == FAIL)
  1820. return FAIL;
  1821. /*
  1822. * block mode replace
  1823. */
  1824. if (oap->block_mode)
  1825. {
  1826. bd.is_MAX = (curwin->w_curswant == MAX

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