/src/buffer.c

https://bitbucket.org/ultra_iter/vim-qt · C · 5722 lines · 4589 code · 367 blank · 766 comment · 1200 complexity · 8a2df500a792f7f65efd467f4a83acac MD5 · raw file

Large files are truncated 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. * buffer.c: functions for dealing with the buffer structure
  11. */
  12. /*
  13. * The buffer list is a double linked list of all buffers.
  14. * Each buffer can be in one of these states:
  15. * never loaded: BF_NEVERLOADED is set, only the file name is valid
  16. * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
  17. * hidden: b_nwindows == 0, loaded but not displayed in a window
  18. * normal: loaded and displayed in a window
  19. *
  20. * Instead of storing file names all over the place, each file name is
  21. * stored in the buffer list. It can be referenced by a number.
  22. *
  23. * The current implementation remembers all file names ever used.
  24. */
  25. #include "vim.h"
  26. #if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
  27. static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
  28. # define HAVE_BUFLIST_MATCH
  29. static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
  30. #endif
  31. static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
  32. static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer));
  33. #ifdef UNIX
  34. static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
  35. static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
  36. static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
  37. #else
  38. static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
  39. #endif
  40. #ifdef FEAT_TITLE
  41. static int ti_change __ARGS((char_u *str, char_u **last));
  42. #endif
  43. static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file));
  44. static void free_buffer __ARGS((buf_T *));
  45. static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
  46. static void clear_wininfo __ARGS((buf_T *buf));
  47. #ifdef UNIX
  48. # define dev_T dev_t
  49. #else
  50. # define dev_T unsigned
  51. #endif
  52. #if defined(FEAT_SIGNS)
  53. static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
  54. static void buf_delete_signs __ARGS((buf_T *buf));
  55. #endif
  56. #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
  57. static char *msg_loclist = N_("[Location List]");
  58. static char *msg_qflist = N_("[Quickfix List]");
  59. #endif
  60. #ifdef FEAT_AUTOCMD
  61. static char *e_auabort = N_("E855: Autocommands caused command to abort");
  62. #endif
  63. /*
  64. * Open current buffer, that is: open the memfile and read the file into
  65. * memory.
  66. * Return FAIL for failure, OK otherwise.
  67. */
  68. int
  69. open_buffer(read_stdin, eap, flags)
  70. int read_stdin; /* read file from stdin */
  71. exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
  72. int flags; /* extra flags for readfile() */
  73. {
  74. int retval = OK;
  75. #ifdef FEAT_AUTOCMD
  76. buf_T *old_curbuf;
  77. #endif
  78. /*
  79. * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
  80. * When re-entering the same buffer, it should not change, because the
  81. * user may have reset the flag by hand.
  82. */
  83. if (readonlymode && curbuf->b_ffname != NULL
  84. && (curbuf->b_flags & BF_NEVERLOADED))
  85. curbuf->b_p_ro = TRUE;
  86. if (ml_open(curbuf) == FAIL)
  87. {
  88. /*
  89. * There MUST be a memfile, otherwise we can't do anything
  90. * If we can't create one for the current buffer, take another buffer
  91. */
  92. close_buffer(NULL, curbuf, 0, FALSE);
  93. for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
  94. if (curbuf->b_ml.ml_mfp != NULL)
  95. break;
  96. /*
  97. * if there is no memfile at all, exit
  98. * This is OK, since there are no changes to lose.
  99. */
  100. if (curbuf == NULL)
  101. {
  102. EMSG(_("E82: Cannot allocate any buffer, exiting..."));
  103. getout(2);
  104. }
  105. EMSG(_("E83: Cannot allocate buffer, using other one..."));
  106. enter_buffer(curbuf);
  107. return FAIL;
  108. }
  109. #ifdef FEAT_AUTOCMD
  110. /* The autocommands in readfile() may change the buffer, but only AFTER
  111. * reading the file. */
  112. old_curbuf = curbuf;
  113. modified_was_set = FALSE;
  114. #endif
  115. /* mark cursor position as being invalid */
  116. curwin->w_valid = 0;
  117. if (curbuf->b_ffname != NULL
  118. #ifdef FEAT_NETBEANS_INTG
  119. && netbeansReadFile
  120. #endif
  121. )
  122. {
  123. #ifdef FEAT_NETBEANS_INTG
  124. int oldFire = netbeansFireChanges;
  125. netbeansFireChanges = 0;
  126. #endif
  127. retval = readfile(curbuf->b_ffname, curbuf->b_fname,
  128. (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
  129. flags | READ_NEW);
  130. #ifdef FEAT_NETBEANS_INTG
  131. netbeansFireChanges = oldFire;
  132. #endif
  133. /* Help buffer is filtered. */
  134. if (curbuf->b_help)
  135. fix_help_buffer();
  136. }
  137. else if (read_stdin)
  138. {
  139. int save_bin = curbuf->b_p_bin;
  140. linenr_T line_count;
  141. /*
  142. * First read the text in binary mode into the buffer.
  143. * Then read from that same buffer and append at the end. This makes
  144. * it possible to retry when 'fileformat' or 'fileencoding' was
  145. * guessed wrong.
  146. */
  147. curbuf->b_p_bin = TRUE;
  148. retval = readfile(NULL, NULL, (linenr_T)0,
  149. (linenr_T)0, (linenr_T)MAXLNUM, NULL,
  150. flags | (READ_NEW + READ_STDIN));
  151. curbuf->b_p_bin = save_bin;
  152. if (retval == OK)
  153. {
  154. line_count = curbuf->b_ml.ml_line_count;
  155. retval = readfile(NULL, NULL, (linenr_T)line_count,
  156. (linenr_T)0, (linenr_T)MAXLNUM, eap,
  157. flags | READ_BUFFER);
  158. if (retval == OK)
  159. {
  160. /* Delete the binary lines. */
  161. while (--line_count >= 0)
  162. ml_delete((linenr_T)1, FALSE);
  163. }
  164. else
  165. {
  166. /* Delete the converted lines. */
  167. while (curbuf->b_ml.ml_line_count > line_count)
  168. ml_delete(line_count, FALSE);
  169. }
  170. /* Put the cursor on the first line. */
  171. curwin->w_cursor.lnum = 1;
  172. curwin->w_cursor.col = 0;
  173. /* Set or reset 'modified' before executing autocommands, so that
  174. * it can be changed there. */
  175. if (!readonlymode && !bufempty())
  176. changed();
  177. else if (retval != FAIL)
  178. unchanged(curbuf, FALSE);
  179. #ifdef FEAT_AUTOCMD
  180. # ifdef FEAT_EVAL
  181. apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
  182. curbuf, &retval);
  183. # else
  184. apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
  185. # endif
  186. #endif
  187. }
  188. }
  189. /* if first time loading this buffer, init b_chartab[] */
  190. if (curbuf->b_flags & BF_NEVERLOADED)
  191. (void)buf_init_chartab(curbuf, FALSE);
  192. /*
  193. * Set/reset the Changed flag first, autocmds may change the buffer.
  194. * Apply the automatic commands, before processing the modelines.
  195. * So the modelines have priority over auto commands.
  196. */
  197. /* When reading stdin, the buffer contents always needs writing, so set
  198. * the changed flag. Unless in readonly mode: "ls | gview -".
  199. * When interrupted and 'cpoptions' contains 'i' set changed flag. */
  200. if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
  201. #ifdef FEAT_AUTOCMD
  202. || modified_was_set /* ":set modified" used in autocmd */
  203. # ifdef FEAT_EVAL
  204. || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
  205. # endif
  206. #endif
  207. )
  208. changed();
  209. else if (retval != FAIL && !read_stdin)
  210. unchanged(curbuf, FALSE);
  211. save_file_ff(curbuf); /* keep this fileformat */
  212. /* require "!" to overwrite the file, because it wasn't read completely */
  213. #ifdef FEAT_EVAL
  214. if (aborting())
  215. #else
  216. if (got_int)
  217. #endif
  218. curbuf->b_flags |= BF_READERR;
  219. #ifdef FEAT_FOLDING
  220. /* Need to update automatic folding. Do this before the autocommands,
  221. * they may use the fold info. */
  222. foldUpdateAll(curwin);
  223. #endif
  224. #ifdef FEAT_AUTOCMD
  225. /* need to set w_topline, unless some autocommand already did that. */
  226. if (!(curwin->w_valid & VALID_TOPLINE))
  227. {
  228. curwin->w_topline = 1;
  229. # ifdef FEAT_DIFF
  230. curwin->w_topfill = 0;
  231. # endif
  232. }
  233. # ifdef FEAT_EVAL
  234. apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
  235. # else
  236. apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
  237. # endif
  238. #endif
  239. if (retval != FAIL)
  240. {
  241. #ifdef FEAT_AUTOCMD
  242. /*
  243. * The autocommands may have changed the current buffer. Apply the
  244. * modelines to the correct buffer, if it still exists and is loaded.
  245. */
  246. if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
  247. {
  248. aco_save_T aco;
  249. /* Go to the buffer that was opened. */
  250. aucmd_prepbuf(&aco, old_curbuf);
  251. #endif
  252. do_modelines(0);
  253. curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
  254. #ifdef FEAT_AUTOCMD
  255. # ifdef FEAT_EVAL
  256. apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
  257. &retval);
  258. # else
  259. apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
  260. # endif
  261. /* restore curwin/curbuf and a few other things */
  262. aucmd_restbuf(&aco);
  263. }
  264. #endif
  265. }
  266. return retval;
  267. }
  268. /*
  269. * Return TRUE if "buf" points to a valid buffer (in the buffer list).
  270. */
  271. int
  272. buf_valid(buf)
  273. buf_T *buf;
  274. {
  275. buf_T *bp;
  276. for (bp = firstbuf; bp != NULL; bp = bp->b_next)
  277. if (bp == buf)
  278. return TRUE;
  279. return FALSE;
  280. }
  281. /*
  282. * Close the link to a buffer.
  283. * "action" is used when there is no longer a window for the buffer.
  284. * It can be:
  285. * 0 buffer becomes hidden
  286. * DOBUF_UNLOAD buffer is unloaded
  287. * DOBUF_DELETE buffer is unloaded and removed from buffer list
  288. * DOBUF_WIPE buffer is unloaded and really deleted
  289. * When doing all but the first one on the current buffer, the caller should
  290. * get a new buffer very soon!
  291. *
  292. * The 'bufhidden' option can force freeing and deleting.
  293. *
  294. * When "abort_if_last" is TRUE then do not close the buffer if autocommands
  295. * cause there to be only one window with this buffer. e.g. when ":quit" is
  296. * supposed to close the window but autocommands close all other windows.
  297. */
  298. void
  299. close_buffer(win, buf, action, abort_if_last)
  300. win_T *win; /* if not NULL, set b_last_cursor */
  301. buf_T *buf;
  302. int action;
  303. int abort_if_last UNUSED;
  304. {
  305. #ifdef FEAT_AUTOCMD
  306. int is_curbuf;
  307. int nwindows;
  308. #endif
  309. int unload_buf = (action != 0);
  310. int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
  311. int wipe_buf = (action == DOBUF_WIPE);
  312. #ifdef FEAT_QUICKFIX
  313. /*
  314. * Force unloading or deleting when 'bufhidden' says so.
  315. * The caller must take care of NOT deleting/freeing when 'bufhidden' is
  316. * "hide" (otherwise we could never free or delete a buffer).
  317. */
  318. if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
  319. {
  320. del_buf = TRUE;
  321. unload_buf = TRUE;
  322. }
  323. else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
  324. {
  325. del_buf = TRUE;
  326. unload_buf = TRUE;
  327. wipe_buf = TRUE;
  328. }
  329. else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
  330. unload_buf = TRUE;
  331. #endif
  332. if (win != NULL)
  333. {
  334. /* Set b_last_cursor when closing the last window for the buffer.
  335. * Remember the last cursor position and window options of the buffer.
  336. * This used to be only for the current window, but then options like
  337. * 'foldmethod' may be lost with a ":only" command. */
  338. if (buf->b_nwindows == 1)
  339. set_last_cursor(win);
  340. buflist_setfpos(buf, win,
  341. win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
  342. win->w_cursor.col, TRUE);
  343. }
  344. #ifdef FEAT_AUTOCMD
  345. /* When the buffer is no longer in a window, trigger BufWinLeave */
  346. if (buf->b_nwindows == 1)
  347. {
  348. buf->b_closing = TRUE;
  349. apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
  350. FALSE, buf);
  351. if (!buf_valid(buf))
  352. {
  353. /* Autocommands deleted the buffer. */
  354. aucmd_abort:
  355. EMSG(_(e_auabort));
  356. return;
  357. }
  358. buf->b_closing = FALSE;
  359. if (abort_if_last && one_window())
  360. /* Autocommands made this the only window. */
  361. goto aucmd_abort;
  362. /* When the buffer becomes hidden, but is not unloaded, trigger
  363. * BufHidden */
  364. if (!unload_buf)
  365. {
  366. buf->b_closing = TRUE;
  367. apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
  368. FALSE, buf);
  369. if (!buf_valid(buf))
  370. /* Autocommands deleted the buffer. */
  371. goto aucmd_abort;
  372. buf->b_closing = FALSE;
  373. if (abort_if_last && one_window())
  374. /* Autocommands made this the only window. */
  375. goto aucmd_abort;
  376. }
  377. # ifdef FEAT_EVAL
  378. if (aborting()) /* autocmds may abort script processing */
  379. return;
  380. # endif
  381. }
  382. nwindows = buf->b_nwindows;
  383. #endif
  384. /* decrease the link count from windows (unless not in any window) */
  385. if (buf->b_nwindows > 0)
  386. --buf->b_nwindows;
  387. /* Return when a window is displaying the buffer or when it's not
  388. * unloaded. */
  389. if (buf->b_nwindows > 0 || !unload_buf)
  390. return;
  391. /* Always remove the buffer when there is no file name. */
  392. if (buf->b_ffname == NULL)
  393. del_buf = TRUE;
  394. /*
  395. * Free all things allocated for this buffer.
  396. * Also calls the "BufDelete" autocommands when del_buf is TRUE.
  397. */
  398. #ifdef FEAT_AUTOCMD
  399. /* Remember if we are closing the current buffer. Restore the number of
  400. * windows, so that autocommands in buf_freeall() don't get confused. */
  401. is_curbuf = (buf == curbuf);
  402. buf->b_nwindows = nwindows;
  403. #endif
  404. buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
  405. if (
  406. #ifdef FEAT_WINDOWS
  407. win_valid(win) &&
  408. #else
  409. win != NULL &&
  410. #endif
  411. win->w_buffer == buf)
  412. win->w_buffer = NULL; /* make sure we don't use the buffer now */
  413. #ifdef FEAT_AUTOCMD
  414. /* Autocommands may have deleted the buffer. */
  415. if (!buf_valid(buf))
  416. return;
  417. # ifdef FEAT_EVAL
  418. if (aborting()) /* autocmds may abort script processing */
  419. return;
  420. # endif
  421. /* Autocommands may have opened or closed windows for this buffer.
  422. * Decrement the count for the close we do here. */
  423. if (buf->b_nwindows > 0)
  424. --buf->b_nwindows;
  425. /*
  426. * It's possible that autocommands change curbuf to the one being deleted.
  427. * This might cause the previous curbuf to be deleted unexpectedly. But
  428. * in some cases it's OK to delete the curbuf, because a new one is
  429. * obtained anyway. Therefore only return if curbuf changed to the
  430. * deleted buffer.
  431. */
  432. if (buf == curbuf && !is_curbuf)
  433. return;
  434. #endif
  435. /* Change directories when the 'acd' option is set. */
  436. DO_AUTOCHDIR
  437. /*
  438. * Remove the buffer from the list.
  439. */
  440. if (wipe_buf)
  441. {
  442. #ifdef FEAT_SUN_WORKSHOP
  443. if (usingSunWorkShop)
  444. workshop_file_closed_lineno((char *)buf->b_ffname,
  445. (int)buf->b_last_cursor.lnum);
  446. #endif
  447. vim_free(buf->b_ffname);
  448. vim_free(buf->b_sfname);
  449. if (buf->b_prev == NULL)
  450. firstbuf = buf->b_next;
  451. else
  452. buf->b_prev->b_next = buf->b_next;
  453. if (buf->b_next == NULL)
  454. lastbuf = buf->b_prev;
  455. else
  456. buf->b_next->b_prev = buf->b_prev;
  457. free_buffer(buf);
  458. }
  459. else
  460. {
  461. if (del_buf)
  462. {
  463. /* Free all internal variables and reset option values, to make
  464. * ":bdel" compatible with Vim 5.7. */
  465. free_buffer_stuff(buf, TRUE);
  466. /* Make it look like a new buffer. */
  467. buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
  468. /* Init the options when loaded again. */
  469. buf->b_p_initialized = FALSE;
  470. }
  471. buf_clear_file(buf);
  472. if (del_buf)
  473. buf->b_p_bl = FALSE;
  474. }
  475. }
  476. /*
  477. * Make buffer not contain a file.
  478. */
  479. void
  480. buf_clear_file(buf)
  481. buf_T *buf;
  482. {
  483. buf->b_ml.ml_line_count = 1;
  484. unchanged(buf, TRUE);
  485. #ifndef SHORT_FNAME
  486. buf->b_shortname = FALSE;
  487. #endif
  488. buf->b_p_eol = TRUE;
  489. buf->b_start_eol = TRUE;
  490. #ifdef FEAT_MBYTE
  491. buf->b_p_bomb = FALSE;
  492. buf->b_start_bomb = FALSE;
  493. #endif
  494. buf->b_ml.ml_mfp = NULL;
  495. buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
  496. #ifdef FEAT_NETBEANS_INTG
  497. netbeans_deleted_all_lines(buf);
  498. #endif
  499. }
  500. /*
  501. * buf_freeall() - free all things allocated for a buffer that are related to
  502. * the file. flags:
  503. * BFA_DEL buffer is going to be deleted
  504. * BFA_WIPE buffer is going to be wiped out
  505. * BFA_KEEP_UNDO do not free undo information
  506. */
  507. void
  508. buf_freeall(buf, flags)
  509. buf_T *buf;
  510. int flags;
  511. {
  512. #ifdef FEAT_AUTOCMD
  513. int is_curbuf = (buf == curbuf);
  514. buf->b_closing = TRUE;
  515. apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
  516. if (!buf_valid(buf)) /* autocommands may delete the buffer */
  517. return;
  518. if ((flags & BFA_DEL) && buf->b_p_bl)
  519. {
  520. apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
  521. if (!buf_valid(buf)) /* autocommands may delete the buffer */
  522. return;
  523. }
  524. if (flags & BFA_WIPE)
  525. {
  526. apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
  527. FALSE, buf);
  528. if (!buf_valid(buf)) /* autocommands may delete the buffer */
  529. return;
  530. }
  531. buf->b_closing = FALSE;
  532. # ifdef FEAT_EVAL
  533. if (aborting()) /* autocmds may abort script processing */
  534. return;
  535. # endif
  536. /*
  537. * It's possible that autocommands change curbuf to the one being deleted.
  538. * This might cause curbuf to be deleted unexpectedly. But in some cases
  539. * it's OK to delete the curbuf, because a new one is obtained anyway.
  540. * Therefore only return if curbuf changed to the deleted buffer.
  541. */
  542. if (buf == curbuf && !is_curbuf)
  543. return;
  544. #endif
  545. #ifdef FEAT_DIFF
  546. diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
  547. #endif
  548. #ifdef FEAT_SYN_HL
  549. /* Remove any ownsyntax, unless exiting. */
  550. if (firstwin != NULL && curwin->w_buffer == buf)
  551. reset_synblock(curwin);
  552. #endif
  553. #ifdef FEAT_FOLDING
  554. /* No folds in an empty buffer. */
  555. # ifdef FEAT_WINDOWS
  556. {
  557. win_T *win;
  558. tabpage_T *tp;
  559. FOR_ALL_TAB_WINDOWS(tp, win)
  560. if (win->w_buffer == buf)
  561. clearFolding(win);
  562. }
  563. # else
  564. if (curwin->w_buffer == buf)
  565. clearFolding(curwin);
  566. # endif
  567. #endif
  568. #ifdef FEAT_TCL
  569. tcl_buffer_free(buf);
  570. #endif
  571. ml_close(buf, TRUE); /* close and delete the memline/memfile */
  572. buf->b_ml.ml_line_count = 0; /* no lines in buffer */
  573. if ((flags & BFA_KEEP_UNDO) == 0)
  574. {
  575. u_blockfree(buf); /* free the memory allocated for undo */
  576. u_clearall(buf); /* reset all undo information */
  577. }
  578. #ifdef FEAT_SYN_HL
  579. syntax_clear(&buf->b_s); /* reset syntax info */
  580. #endif
  581. buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
  582. }
  583. /*
  584. * Free a buffer structure and the things it contains related to the buffer
  585. * itself (not the file, that must have been done already).
  586. */
  587. static void
  588. free_buffer(buf)
  589. buf_T *buf;
  590. {
  591. free_buffer_stuff(buf, TRUE);
  592. #ifdef FEAT_LUA
  593. lua_buffer_free(buf);
  594. #endif
  595. #ifdef FEAT_MZSCHEME
  596. mzscheme_buffer_free(buf);
  597. #endif
  598. #ifdef FEAT_PERL
  599. perl_buf_free(buf);
  600. #endif
  601. #ifdef FEAT_PYTHON
  602. python_buffer_free(buf);
  603. #endif
  604. #ifdef FEAT_PYTHON3
  605. python3_buffer_free(buf);
  606. #endif
  607. #ifdef FEAT_RUBY
  608. ruby_buffer_free(buf);
  609. #endif
  610. #ifdef FEAT_AUTOCMD
  611. aubuflocal_remove(buf);
  612. #endif
  613. vim_free(buf);
  614. }
  615. /*
  616. * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
  617. */
  618. static void
  619. free_buffer_stuff(buf, free_options)
  620. buf_T *buf;
  621. int free_options; /* free options as well */
  622. {
  623. if (free_options)
  624. {
  625. clear_wininfo(buf); /* including window-local options */
  626. free_buf_options(buf, TRUE);
  627. #ifdef FEAT_SPELL
  628. ga_clear(&buf->b_s.b_langp);
  629. #endif
  630. }
  631. #ifdef FEAT_EVAL
  632. vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
  633. hash_init(&buf->b_vars.dv_hashtab);
  634. #endif
  635. #ifdef FEAT_USR_CMDS
  636. uc_clear(&buf->b_ucmds); /* clear local user commands */
  637. #endif
  638. #ifdef FEAT_SIGNS
  639. buf_delete_signs(buf); /* delete any signs */
  640. #endif
  641. #ifdef FEAT_NETBEANS_INTG
  642. netbeans_file_killed(buf);
  643. #endif
  644. #ifdef FEAT_LOCALMAP
  645. map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
  646. map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
  647. #endif
  648. #ifdef FEAT_MBYTE
  649. vim_free(buf->b_start_fenc);
  650. buf->b_start_fenc = NULL;
  651. #endif
  652. }
  653. /*
  654. * Free the b_wininfo list for buffer "buf".
  655. */
  656. static void
  657. clear_wininfo(buf)
  658. buf_T *buf;
  659. {
  660. wininfo_T *wip;
  661. while (buf->b_wininfo != NULL)
  662. {
  663. wip = buf->b_wininfo;
  664. buf->b_wininfo = wip->wi_next;
  665. if (wip->wi_optset)
  666. {
  667. clear_winopt(&wip->wi_opt);
  668. #ifdef FEAT_FOLDING
  669. deleteFoldRecurse(&wip->wi_folds);
  670. #endif
  671. }
  672. vim_free(wip);
  673. }
  674. }
  675. #if defined(FEAT_LISTCMDS) || defined(PROTO)
  676. /*
  677. * Go to another buffer. Handles the result of the ATTENTION dialog.
  678. */
  679. void
  680. goto_buffer(eap, start, dir, count)
  681. exarg_T *eap;
  682. int start;
  683. int dir;
  684. int count;
  685. {
  686. # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
  687. buf_T *old_curbuf = curbuf;
  688. swap_exists_action = SEA_DIALOG;
  689. # endif
  690. (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
  691. start, dir, count, eap->forceit);
  692. # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
  693. if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
  694. {
  695. # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  696. cleanup_T cs;
  697. /* Reset the error/interrupt/exception state here so that
  698. * aborting() returns FALSE when closing a window. */
  699. enter_cleanup(&cs);
  700. # endif
  701. /* Quitting means closing the split window, nothing else. */
  702. win_close(curwin, TRUE);
  703. swap_exists_action = SEA_NONE;
  704. swap_exists_did_quit = TRUE;
  705. # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  706. /* Restore the error/interrupt/exception state if not discarded by a
  707. * new aborting error, interrupt, or uncaught exception. */
  708. leave_cleanup(&cs);
  709. # endif
  710. }
  711. else
  712. handle_swap_exists(old_curbuf);
  713. # endif
  714. }
  715. #endif
  716. #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
  717. /*
  718. * Handle the situation of swap_exists_action being set.
  719. * It is allowed for "old_curbuf" to be NULL or invalid.
  720. */
  721. void
  722. handle_swap_exists(old_curbuf)
  723. buf_T *old_curbuf;
  724. {
  725. # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  726. cleanup_T cs;
  727. # endif
  728. if (swap_exists_action == SEA_QUIT)
  729. {
  730. # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  731. /* Reset the error/interrupt/exception state here so that
  732. * aborting() returns FALSE when closing a buffer. */
  733. enter_cleanup(&cs);
  734. # endif
  735. /* User selected Quit at ATTENTION prompt. Go back to previous
  736. * buffer. If that buffer is gone or the same as the current one,
  737. * open a new, empty buffer. */
  738. swap_exists_action = SEA_NONE; /* don't want it again */
  739. swap_exists_did_quit = TRUE;
  740. close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
  741. if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
  742. old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
  743. if (old_curbuf != NULL)
  744. enter_buffer(old_curbuf);
  745. /* If "old_curbuf" is NULL we are in big trouble here... */
  746. # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  747. /* Restore the error/interrupt/exception state if not discarded by a
  748. * new aborting error, interrupt, or uncaught exception. */
  749. leave_cleanup(&cs);
  750. # endif
  751. }
  752. else if (swap_exists_action == SEA_RECOVER)
  753. {
  754. # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  755. /* Reset the error/interrupt/exception state here so that
  756. * aborting() returns FALSE when closing a buffer. */
  757. enter_cleanup(&cs);
  758. # endif
  759. /* User selected Recover at ATTENTION prompt. */
  760. msg_scroll = TRUE;
  761. ml_recover();
  762. MSG_PUTS("\n"); /* don't overwrite the last message */
  763. cmdline_row = msg_row;
  764. do_modelines(0);
  765. # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  766. /* Restore the error/interrupt/exception state if not discarded by a
  767. * new aborting error, interrupt, or uncaught exception. */
  768. leave_cleanup(&cs);
  769. # endif
  770. }
  771. swap_exists_action = SEA_NONE;
  772. }
  773. #endif
  774. #if defined(FEAT_LISTCMDS) || defined(PROTO)
  775. /*
  776. * do_bufdel() - delete or unload buffer(s)
  777. *
  778. * addr_count == 0: ":bdel" - delete current buffer
  779. * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
  780. * buffer "end_bnr", then any other arguments.
  781. * addr_count == 2: ":N,N bdel" - delete buffers in range
  782. *
  783. * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
  784. * DOBUF_DEL (":bdel")
  785. *
  786. * Returns error message or NULL
  787. */
  788. char_u *
  789. do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
  790. int command;
  791. char_u *arg; /* pointer to extra arguments */
  792. int addr_count;
  793. int start_bnr; /* first buffer number in a range */
  794. int end_bnr; /* buffer nr or last buffer nr in a range */
  795. int forceit;
  796. {
  797. int do_current = 0; /* delete current buffer? */
  798. int deleted = 0; /* number of buffers deleted */
  799. char_u *errormsg = NULL; /* return value */
  800. int bnr; /* buffer number */
  801. char_u *p;
  802. if (addr_count == 0)
  803. {
  804. (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
  805. }
  806. else
  807. {
  808. if (addr_count == 2)
  809. {
  810. if (*arg) /* both range and argument is not allowed */
  811. return (char_u *)_(e_trailing);
  812. bnr = start_bnr;
  813. }
  814. else /* addr_count == 1 */
  815. bnr = end_bnr;
  816. for ( ;!got_int; ui_breakcheck())
  817. {
  818. /*
  819. * delete the current buffer last, otherwise when the
  820. * current buffer is deleted, the next buffer becomes
  821. * the current one and will be loaded, which may then
  822. * also be deleted, etc.
  823. */
  824. if (bnr == curbuf->b_fnum)
  825. do_current = bnr;
  826. else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
  827. forceit) == OK)
  828. ++deleted;
  829. /*
  830. * find next buffer number to delete/unload
  831. */
  832. if (addr_count == 2)
  833. {
  834. if (++bnr > end_bnr)
  835. break;
  836. }
  837. else /* addr_count == 1 */
  838. {
  839. arg = skipwhite(arg);
  840. if (*arg == NUL)
  841. break;
  842. if (!VIM_ISDIGIT(*arg))
  843. {
  844. p = skiptowhite_esc(arg);
  845. bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
  846. if (bnr < 0) /* failed */
  847. break;
  848. arg = p;
  849. }
  850. else
  851. bnr = getdigits(&arg);
  852. }
  853. }
  854. if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
  855. FORWARD, do_current, forceit) == OK)
  856. ++deleted;
  857. if (deleted == 0)
  858. {
  859. if (command == DOBUF_UNLOAD)
  860. STRCPY(IObuff, _("E515: No buffers were unloaded"));
  861. else if (command == DOBUF_DEL)
  862. STRCPY(IObuff, _("E516: No buffers were deleted"));
  863. else
  864. STRCPY(IObuff, _("E517: No buffers were wiped out"));
  865. errormsg = IObuff;
  866. }
  867. else if (deleted >= p_report)
  868. {
  869. if (command == DOBUF_UNLOAD)
  870. {
  871. if (deleted == 1)
  872. MSG(_("1 buffer unloaded"));
  873. else
  874. smsg((char_u *)_("%d buffers unloaded"), deleted);
  875. }
  876. else if (command == DOBUF_DEL)
  877. {
  878. if (deleted == 1)
  879. MSG(_("1 buffer deleted"));
  880. else
  881. smsg((char_u *)_("%d buffers deleted"), deleted);
  882. }
  883. else
  884. {
  885. if (deleted == 1)
  886. MSG(_("1 buffer wiped out"));
  887. else
  888. smsg((char_u *)_("%d buffers wiped out"), deleted);
  889. }
  890. }
  891. }
  892. return errormsg;
  893. }
  894. /*
  895. * Implementation of the commands for the buffer list.
  896. *
  897. * action == DOBUF_GOTO go to specified buffer
  898. * action == DOBUF_SPLIT split window and go to specified buffer
  899. * action == DOBUF_UNLOAD unload specified buffer(s)
  900. * action == DOBUF_DEL delete specified buffer(s) from buffer list
  901. * action == DOBUF_WIPE delete specified buffer(s) really
  902. *
  903. * start == DOBUF_CURRENT go to "count" buffer from current buffer
  904. * start == DOBUF_FIRST go to "count" buffer from first buffer
  905. * start == DOBUF_LAST go to "count" buffer from last buffer
  906. * start == DOBUF_MOD go to "count" modified buffer from current buffer
  907. *
  908. * Return FAIL or OK.
  909. */
  910. int
  911. do_buffer(action, start, dir, count, forceit)
  912. int action;
  913. int start;
  914. int dir; /* FORWARD or BACKWARD */
  915. int count; /* buffer number or number of buffers */
  916. int forceit; /* TRUE for :...! */
  917. {
  918. buf_T *buf;
  919. buf_T *bp;
  920. int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
  921. || action == DOBUF_WIPE);
  922. switch (start)
  923. {
  924. case DOBUF_FIRST: buf = firstbuf; break;
  925. case DOBUF_LAST: buf = lastbuf; break;
  926. default: buf = curbuf; break;
  927. }
  928. if (start == DOBUF_MOD) /* find next modified buffer */
  929. {
  930. while (count-- > 0)
  931. {
  932. do
  933. {
  934. buf = buf->b_next;
  935. if (buf == NULL)
  936. buf = firstbuf;
  937. }
  938. while (buf != curbuf && !bufIsChanged(buf));
  939. }
  940. if (!bufIsChanged(buf))
  941. {
  942. EMSG(_("E84: No modified buffer found"));
  943. return FAIL;
  944. }
  945. }
  946. else if (start == DOBUF_FIRST && count) /* find specified buffer number */
  947. {
  948. while (buf != NULL && buf->b_fnum != count)
  949. buf = buf->b_next;
  950. }
  951. else
  952. {
  953. bp = NULL;
  954. while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
  955. {
  956. /* remember the buffer where we start, we come back there when all
  957. * buffers are unlisted. */
  958. if (bp == NULL)
  959. bp = buf;
  960. if (dir == FORWARD)
  961. {
  962. buf = buf->b_next;
  963. if (buf == NULL)
  964. buf = firstbuf;
  965. }
  966. else
  967. {
  968. buf = buf->b_prev;
  969. if (buf == NULL)
  970. buf = lastbuf;
  971. }
  972. /* don't count unlisted buffers */
  973. if (unload || buf->b_p_bl)
  974. {
  975. --count;
  976. bp = NULL; /* use this buffer as new starting point */
  977. }
  978. if (bp == buf)
  979. {
  980. /* back where we started, didn't find anything. */
  981. EMSG(_("E85: There is no listed buffer"));
  982. return FAIL;
  983. }
  984. }
  985. }
  986. if (buf == NULL) /* could not find it */
  987. {
  988. if (start == DOBUF_FIRST)
  989. {
  990. /* don't warn when deleting */
  991. if (!unload)
  992. EMSGN(_("E86: Buffer %ld does not exist"), count);
  993. }
  994. else if (dir == FORWARD)
  995. EMSG(_("E87: Cannot go beyond last buffer"));
  996. else
  997. EMSG(_("E88: Cannot go before first buffer"));
  998. return FAIL;
  999. }
  1000. #ifdef FEAT_GUI
  1001. need_mouse_correct = TRUE;
  1002. #endif
  1003. #ifdef FEAT_LISTCMDS
  1004. /*
  1005. * delete buffer buf from memory and/or the list
  1006. */
  1007. if (unload)
  1008. {
  1009. int forward;
  1010. int retval;
  1011. /* When unloading or deleting a buffer that's already unloaded and
  1012. * unlisted: fail silently. */
  1013. if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
  1014. return FAIL;
  1015. if (!forceit && bufIsChanged(buf))
  1016. {
  1017. #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
  1018. if ((p_confirm || cmdmod.confirm) && p_write)
  1019. {
  1020. dialog_changed(buf, FALSE);
  1021. # ifdef FEAT_AUTOCMD
  1022. if (!buf_valid(buf))
  1023. /* Autocommand deleted buffer, oops! It's not changed
  1024. * now. */
  1025. return FAIL;
  1026. # endif
  1027. /* If it's still changed fail silently, the dialog already
  1028. * mentioned why it fails. */
  1029. if (bufIsChanged(buf))
  1030. return FAIL;
  1031. }
  1032. else
  1033. #endif
  1034. {
  1035. EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
  1036. buf->b_fnum);
  1037. return FAIL;
  1038. }
  1039. }
  1040. /*
  1041. * If deleting the last (listed) buffer, make it empty.
  1042. * The last (listed) buffer cannot be unloaded.
  1043. */
  1044. for (bp = firstbuf; bp != NULL; bp = bp->b_next)
  1045. if (bp->b_p_bl && bp != buf)
  1046. break;
  1047. if (bp == NULL && buf == curbuf)
  1048. {
  1049. if (action == DOBUF_UNLOAD)
  1050. {
  1051. EMSG(_("E90: Cannot unload last buffer"));
  1052. return FAIL;
  1053. }
  1054. /* Close any other windows on this buffer, then make it empty. */
  1055. #ifdef FEAT_WINDOWS
  1056. close_windows(buf, TRUE);
  1057. #endif
  1058. setpcmark();
  1059. retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
  1060. forceit ? ECMD_FORCEIT : 0, curwin);
  1061. /*
  1062. * do_ecmd() may create a new buffer, then we have to delete
  1063. * the old one. But do_ecmd() may have done that already, check
  1064. * if the buffer still exists.
  1065. */
  1066. if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
  1067. close_buffer(NULL, buf, action, FALSE);
  1068. return retval;
  1069. }
  1070. #ifdef FEAT_WINDOWS
  1071. /*
  1072. * If the deleted buffer is the current one, close the current window
  1073. * (unless it's the only window). Repeat this so long as we end up in
  1074. * a window with this buffer.
  1075. */
  1076. while (buf == curbuf
  1077. # ifdef FEAT_AUTOCMD
  1078. && !(curwin->w_closing || curwin->w_buffer->b_closing)
  1079. # endif
  1080. && (firstwin != lastwin || first_tabpage->tp_next != NULL))
  1081. win_close(curwin, FALSE);
  1082. #endif
  1083. /*
  1084. * If the buffer to be deleted is not the current one, delete it here.
  1085. */
  1086. if (buf != curbuf)
  1087. {
  1088. #ifdef FEAT_WINDOWS
  1089. close_windows(buf, FALSE);
  1090. #endif
  1091. if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
  1092. close_buffer(NULL, buf, action, FALSE);
  1093. return OK;
  1094. }
  1095. /*
  1096. * Deleting the current buffer: Need to find another buffer to go to.
  1097. * There must be another, otherwise it would have been handled above.
  1098. * First use au_new_curbuf, if it is valid.
  1099. * Then prefer the buffer we most recently visited.
  1100. * Else try to find one that is loaded, after the current buffer,
  1101. * then before the current buffer.
  1102. * Finally use any buffer.
  1103. */
  1104. buf = NULL; /* selected buffer */
  1105. bp = NULL; /* used when no loaded buffer found */
  1106. #ifdef FEAT_AUTOCMD
  1107. if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
  1108. buf = au_new_curbuf;
  1109. # ifdef FEAT_JUMPLIST
  1110. else
  1111. # endif
  1112. #endif
  1113. #ifdef FEAT_JUMPLIST
  1114. if (curwin->w_jumplistlen > 0)
  1115. {
  1116. int jumpidx;
  1117. jumpidx = curwin->w_jumplistidx - 1;
  1118. if (jumpidx < 0)
  1119. jumpidx = curwin->w_jumplistlen - 1;
  1120. forward = jumpidx;
  1121. while (jumpidx != curwin->w_jumplistidx)
  1122. {
  1123. buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
  1124. if (buf != NULL)
  1125. {
  1126. if (buf == curbuf || !buf->b_p_bl)
  1127. buf = NULL; /* skip current and unlisted bufs */
  1128. else if (buf->b_ml.ml_mfp == NULL)
  1129. {
  1130. /* skip unloaded buf, but may keep it for later */
  1131. if (bp == NULL)
  1132. bp = buf;
  1133. buf = NULL;
  1134. }
  1135. }
  1136. if (buf != NULL) /* found a valid buffer: stop searching */
  1137. break;
  1138. /* advance to older entry in jump list */
  1139. if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
  1140. break;
  1141. if (--jumpidx < 0)
  1142. jumpidx = curwin->w_jumplistlen - 1;
  1143. if (jumpidx == forward) /* List exhausted for sure */
  1144. break;
  1145. }
  1146. }
  1147. #endif
  1148. if (buf == NULL) /* No previous buffer, Try 2'nd approach */
  1149. {
  1150. forward = TRUE;
  1151. buf = curbuf->b_next;
  1152. for (;;)
  1153. {
  1154. if (buf == NULL)
  1155. {
  1156. if (!forward) /* tried both directions */
  1157. break;
  1158. buf = curbuf->b_prev;
  1159. forward = FALSE;
  1160. continue;
  1161. }
  1162. /* in non-help buffer, try to skip help buffers, and vv */
  1163. if (buf->b_help == curbuf->b_help && buf->b_p_bl)
  1164. {
  1165. if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
  1166. break;
  1167. if (bp == NULL) /* remember unloaded buf for later */
  1168. bp = buf;
  1169. }
  1170. if (forward)
  1171. buf = buf->b_next;
  1172. else
  1173. buf = buf->b_prev;
  1174. }
  1175. }
  1176. if (buf == NULL) /* No loaded buffer, use unloaded one */
  1177. buf = bp;
  1178. if (buf == NULL) /* No loaded buffer, find listed one */
  1179. {
  1180. for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  1181. if (buf->b_p_bl && buf != curbuf)
  1182. break;
  1183. }
  1184. if (buf == NULL) /* Still no buffer, just take one */
  1185. {
  1186. if (curbuf->b_next != NULL)
  1187. buf = curbuf->b_next;
  1188. else
  1189. buf = curbuf->b_prev;
  1190. }
  1191. }
  1192. /*
  1193. * make buf current buffer
  1194. */
  1195. if (action == DOBUF_SPLIT) /* split window first */
  1196. {
  1197. # ifdef FEAT_WINDOWS
  1198. /* If 'switchbuf' contains "useopen": jump to first window containing
  1199. * "buf" if one exists */
  1200. if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
  1201. return OK;
  1202. /* If 'switchbuf' contains "usetab": jump to first window in any tab
  1203. * page containing "buf" if one exists */
  1204. if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
  1205. return OK;
  1206. if (win_split(0, 0) == FAIL)
  1207. # endif
  1208. return FAIL;
  1209. }
  1210. #endif
  1211. /* go to current buffer - nothing to do */
  1212. if (buf == curbuf)
  1213. return OK;
  1214. /*
  1215. * Check if the current buffer may be abandoned.
  1216. */
  1217. if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
  1218. {
  1219. #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
  1220. if ((p_confirm || cmdmod.confirm) && p_write)
  1221. {
  1222. dialog_changed(curbuf, FALSE);
  1223. # ifdef FEAT_AUTOCMD
  1224. if (!buf_valid(buf))
  1225. /* Autocommand deleted buffer, oops! */
  1226. return FAIL;
  1227. # endif
  1228. }
  1229. if (bufIsChanged(curbuf))
  1230. #endif
  1231. {
  1232. EMSG(_(e_nowrtmsg));
  1233. return FAIL;
  1234. }
  1235. }
  1236. /* Go to the other buffer. */
  1237. set_curbuf(buf, action);
  1238. #if defined(FEAT_LISTCMDS) \
  1239. && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
  1240. if (action == DOBUF_SPLIT)
  1241. {
  1242. RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
  1243. }
  1244. #endif
  1245. #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  1246. if (aborting()) /* autocmds may abort script processing */
  1247. return FAIL;
  1248. #endif
  1249. return OK;
  1250. }
  1251. #endif /* FEAT_LISTCMDS */
  1252. /*
  1253. * Set current buffer to "buf". Executes autocommands and closes current
  1254. * buffer. "action" tells how to close the current buffer:
  1255. * DOBUF_GOTO free or hide it
  1256. * DOBUF_SPLIT nothing
  1257. * DOBUF_UNLOAD unload it
  1258. * DOBUF_DEL delete it
  1259. * DOBUF_WIPE wipe it out
  1260. */
  1261. void
  1262. set_curbuf(buf, action)
  1263. buf_T *buf;
  1264. int action;
  1265. {
  1266. buf_T *prevbuf;
  1267. int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
  1268. || action == DOBUF_WIPE);
  1269. setpcmark();
  1270. if (!cmdmod.keepalt)
  1271. curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
  1272. buflist_altfpos(curwin); /* remember curpos */
  1273. #ifdef FEAT_VISUAL
  1274. /* Don't restart Select mode after switching to another buffer. */
  1275. VIsual_reselect = FALSE;
  1276. #endif
  1277. /* close_windows() or apply_autocmds() may change curbuf */
  1278. prevbuf = curbuf;
  1279. #ifdef FEAT_AUTOCMD
  1280. apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
  1281. # ifdef FEAT_EVAL
  1282. if (buf_valid(prevbuf) && !aborting())
  1283. # else
  1284. if (buf_valid(prevbuf))
  1285. # endif
  1286. #endif
  1287. {
  1288. #ifdef FEAT_SYN_HL
  1289. if (prevbuf == curwin->w_buffer)
  1290. reset_synblock(curwin);
  1291. #endif
  1292. #ifdef FEAT_WINDOWS
  1293. if (unload)
  1294. close_windows(prevbuf, FALSE);
  1295. #endif
  1296. #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  1297. if (buf_valid(prevbuf) && !aborting())
  1298. #else
  1299. if (buf_valid(prevbuf))
  1300. #endif
  1301. {
  1302. if (prevbuf == curbuf)
  1303. u_sync(FALSE);
  1304. close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
  1305. unload ? action : (action == DOBUF_GOTO
  1306. && !P_HID(prevbuf)
  1307. && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
  1308. }
  1309. }
  1310. #ifdef FEAT_AUTOCMD
  1311. /* An autocommand may have deleted "buf", already entered it (e.g., when
  1312. * it did ":bunload") or aborted the script processing! */
  1313. # ifdef FEAT_EVAL
  1314. if (buf_valid(buf) && buf != curbuf && !aborting())
  1315. # else
  1316. if (buf_valid(buf) && buf != curbuf)
  1317. # endif
  1318. #endif
  1319. enter_buffer(buf);
  1320. }
  1321. /*
  1322. * Enter a new current buffer.
  1323. * Old curbuf must have been abandoned already!
  1324. */
  1325. void
  1326. enter_buffer(buf)
  1327. buf_T *buf;
  1328. {
  1329. /* Copy buffer and window local option values. Not for a help buffer. */
  1330. buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
  1331. if (!buf->b_help)
  1332. get_winopts(buf);
  1333. #ifdef FEAT_FOLDING
  1334. else
  1335. /* Remove all folds in the window. */
  1336. clearFolding(curwin);
  1337. foldUpdateAll(curwin); /* update folds (later). */
  1338. #endif
  1339. /* Get the buffer in the current window. */
  1340. curwin->w_buffer = buf;
  1341. curbuf = buf;
  1342. ++curbuf->b_nwindows;
  1343. #ifdef FEAT_DIFF
  1344. if (curwin->w_p_diff)
  1345. diff_buf_add(curbuf);
  1346. #endif
  1347. #ifdef FEAT_SYN_HL
  1348. curwin->w_s = &(buf->b_s);
  1349. #endif
  1350. /* Cursor on first line by default. */
  1351. curwin->w_cursor.lnum = 1;
  1352. curwin->w_cursor.col = 0;
  1353. #ifdef FEAT_VIRTUALEDIT
  1354. curwin->w_cursor.coladd = 0;
  1355. #endif
  1356. curwin->w_set_curswant = TRUE;
  1357. #ifdef FEAT_AUTOCMD
  1358. curwin->w_topline_was_set = FALSE;
  1359. #endif
  1360. /* mark cursor position as being invalid */
  1361. curwin->w_valid = 0;
  1362. /* Make sure the buffer is loaded. */
  1363. if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
  1364. {
  1365. #ifdef FEAT_AUTOCMD
  1366. /* If there is no filetype, allow for detecting one. Esp. useful for
  1367. * ":ball" used in a autocommand. If there already is a filetype we
  1368. * might prefer to keep it. */
  1369. if (*curbuf->b_p_ft == NUL)
  1370. did_filetype = FALSE;
  1371. #endif
  1372. open_buffer(FALSE, NULL, 0);
  1373. }
  1374. else
  1375. {
  1376. if (!msg_silent)
  1377. need_fileinfo = TRUE; /* display file info after redraw */
  1378. (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
  1379. #ifdef FEAT_AUTOCMD
  1380. curwin->w_topline = 1;
  1381. # ifdef FEAT_DIFF
  1382. curwin->w_topfill = 0;
  1383. # endif
  1384. apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
  1385. apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
  1386. #endif
  1387. }
  1388. /* If autocommands did not change the cursor position, restore cursor lnum
  1389. * and possibly cursor col. */
  1390. if (curwin->w_cursor.lnum == 1 && inindent(0))
  1391. buflist_getfpos();
  1392. check_arg_idx(curwin); /* check for valid arg_idx */
  1393. #ifdef FEAT_TITLE
  1394. maketitle();
  1395. #endif
  1396. #ifdef FEAT_AUTOCMD
  1397. /* when autocmds didn't change it */
  1398. if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
  1399. #endif
  1400. scroll_cursor_halfway(FALSE); /* redisplay at correct position */
  1401. #ifdef FEAT_NETBEANS_INTG
  1402. /* Send fileOpened event because we've changed buffers. */
  1403. netbeans_file_activated(curbuf);
  1404. #endif
  1405. /* Change directories when the 'acd' option is set. */
  1406. DO_AUTOCHDIR
  1407. #ifdef FEAT_KEYMAP
  1408. if (curbuf->b_kmap_state & KEYMAP_INIT)
  1409. (void)keymap_init();
  1410. #endif
  1411. #ifdef FEAT_SPELL
  1412. /* May need to set the spell language. Can only do this after the buffer
  1413. * has been properly setup. */
  1414. if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
  1415. (void)did_set_spelllang(curwin);
  1416. #endif
  1417. redraw_later(NOT_VALID);
  1418. }
  1419. #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
  1420. /*
  1421. * Change to the directory of the current buffer.
  1422. */
  1423. void
  1424. do_autochdir()
  1425. {
  1426. if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
  1427. shorten_fnames(TRUE);
  1428. }
  1429. #endif
  1430. /*
  1431. * functions for dealing with the buffer list
  1432. */
  1433. /*
  1434. * Add a file name to the buffer list. Return a pointer to the buffer.
  1435. * If the same file name already exists return a pointer to that buffer.
  1436. * If it does not exist, or if fname == NULL, a new entry is created.
  1437. * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
  1438. * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
  1439. * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
  1440. * This is the ONLY way to create a new buffer.
  1441. */
  1442. static int top_file_num = 1; /* highest file number */
  1443. buf_T *
  1444. buflist_new(ffname, sfname, lnum, flags)
  1445. char_u *ffname; /* full path of fname or relative */
  1446. char_u *sfname; /* short fname or NULL */
  1447. linenr_T lnum; /* preferred cursor line */
  1448. int flags; /* BLN_ defines */
  1449. {
  1450. buf_T *buf;
  1451. #ifdef UNIX
  1452. struct stat st;
  1453. #endif
  1454. fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
  1455. /*
  1456. * If file name already exists in the list, update the entry.
  1457. */
  1458. #ifdef UNIX
  1459. /* On Unix we can use inode numbers when the file exists. Works better
  1460. * for hard links. */
  1461. if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
  1462. st.st_dev = (dev_T)-1;
  1463. #endif
  1464. if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
  1465. #ifdef UNIX
  1466. buflist_findname_stat(ffname, &st)
  1467. #else
  1468. buflist_findname(ffname)
  1469. #endif
  1470. ) != NULL)
  1471. {
  1472. vim_free(ffname);
  1473. if (lnum != 0)
  1474. buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
  1475. /* copy the options now, if 'cpo' doesn't have 's' and not done
  1476. * already */
  1477. buf_copy_options(buf, 0);
  1478. if ((flags & BLN_LISTED) && !buf->b_p_bl)
  1479. {
  1480. buf->b_p_bl = TRUE;
  1481. #ifdef FEAT_AUTOCMD
  1482. if (!(flags & BLN_DUMMY))
  1483. apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
  1484. #endif
  1485. }
  1486. return buf;
  1487. }
  1488. /*
  1489. * If the current buffer has no name and no contents, use the current
  1490. * buffer. Otherwise: Need to allocate a new buffer structure.
  1491. *
  1492. * This is the ONLY place where a new buffer structure is allocated!
  1493. * (A spell file buffer is allocated in spell.c, but that's not a normal
  1494. * buffer.)
  1495. */
  1496. buf = NULL;
  1497. if ((flags & BLN_CURBUF)
  1498. && curbuf != NULL
  1499. && curbuf->b_ffname == NULL
  1500. && curbuf->b_nwindows <= 1
  1501. && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
  1502. {
  1503. buf = curbuf;
  1504. #ifdef FEAT_AUTOCMD
  1505. /* It's like this buffer is deleted. Watch out for autocommands that
  1506. * change curbuf! If that happens, allocate a new buffer anyway. */
  1507. if (curbuf->b_p_bl)
  1508. apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
  1509. if (buf == curbuf)
  1510. apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
  1511. # ifdef FEAT_EVAL
  1512. if (aborting()) /* autocmds may abort script processing */
  1513. return NULL;
  1514. # endif
  1515. #endif
  1516. #ifdef FEAT_QUICKFIX
  1517. # ifdef FEAT_AUTOCMD
  1518. if (buf == curbuf)
  1519. # endif
  1520. {
  1521. /* Make sure 'bufhidden' and 'buftype' are empty */
  1522. clear_string_option(&buf->b_p_bh);
  1523. clear_string_option(&buf->b_p_bt);
  1524. }
  1525. #endif
  1526. }
  1527. if (buf != curbuf || curbuf == NULL)
  1528. {
  1529. buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
  1530. if (buf == NULL)
  1531. {
  1532. vim_free(ffname);
  1533. return NULL;
  1534. }
  1535. }
  1536. if (ffname != NULL)
  1537. {
  1538. buf->b_ffname = ffname;
  1539. buf->b_sfname = vim_strsave(sfname);
  1540. }
  1541. clear_wininfo(buf);
  1542. buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
  1543. if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
  1544. || buf->b_wininfo == NULL)
  1545. {
  1546. vim_free(buf->b_ffname);
  1547. buf->b_ffname = NULL;
  1548. vim_free(buf->b_sfname);
  1549. buf->b_sfname = NULL;
  1550. if (buf != curbuf)
  1551. free_buffer(buf);
  1552. return NULL;
  1553. }
  1554. if (buf == curbuf)
  1555. {
  1556. /* free all things allocated for this buffer */
  1557. buf_freeall(buf, 0);
  1558. if (buf != curbuf) /* autocommands deleted the buffer! */
  1559. return NULL;
  1560. #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  1561. if (aborting()) /* autocmds may abort script processing */
  1562. return NULL;
  1563. #endif
  1564. /* buf->b_nwindows = 0; why was this here? */
  1565. free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
  1566. #ifdef FEAT_KEYMAP
  1567. /* need to reload lmaps and set b:keymap_name */
  1568. curbuf->b_kmap_state |= KEYMAP_INIT;
  1569. #endif
  1570. }
  1571. else
  1572. {
  1573. /*
  1574. * put new buffer at the end of the buffer list
  1575. */
  1576. buf->b_next = NULL;
  1577. if (firstbuf == NULL) /* buffer list is empty */
  1578. {
  1579. buf->b_prev = NULL;
  1580. firstbuf = buf;
  1581. }
  1582. else /* append new buffer at end of list */
  1583. {
  1584. lastbuf->b_next = buf;
  1585. buf->b_prev = lastbuf;
  1586. }
  1587. lastbuf = buf;
  1588. buf->b_fnum = top_file_num++;
  1589. if (top_file_num < 0) /* wrap around (may cause duplicates) */
  1590. {
  1591. EMSG(_("W14: Warning: List of file names overflow"));
  1592. if (emsg_silent == 0)
  1593. {
  1594. out_flush();
  1595. ui_delay(3000L, TRUE); /* make sure it is noticed */
  1596. }
  1597. top_file_num = 1;
  1598. }
  1599. /*
  1600. * Always copy the options from the current buffer.
  1601. */
  1602. buf_copy_options(buf, BCO_ALWAYS);
  1603. }
  1604. buf->b_wininfo->wi_fpos.lnum = lnum;
  1605. buf->b_wininfo->wi_win = curwin;
  1606. #ifdef FEAT_EVAL
  1607. init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
  1608. #endif
  1609. #ifdef FEAT_SYN_HL
  1610. hash_init(&buf->b_s.b_keywtab);
  1611. hash_init(&buf->b_s.b_keywtab_ic);
  1612. #endif
  1613. buf->b_fname = buf->b_sfname;
  1614. #ifdef UNIX
  1615. if (st.st_dev == (dev_T)-1)
  1616. buf->b_dev_valid = FALSE;
  1617. else
  1618. {
  1619. buf->b_dev_valid = TRUE;
  1620. buf->b_dev = st.st_dev;
  1621. buf->b_ino = st.st_ino;
  1622. }
  1623. #endif
  1624. buf->b_u_synced = TRUE;
  1625. buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
  1626. if (flags & BLN_DUMMY)
  1627. buf->b_flags |= BF_DUMMY;
  1628. buf_clear_file(buf);
  1629. clrallmarks(buf); /* clear marks */
  1630. fmarks_check_names(buf); /* check file marks for this file */
  1631. buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
  1632. #ifdef FEAT_AUTOCMD
  1633. if (!(flags & BLN_DUMMY))
  1634. {
  1635. apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
  1636. if (flags & BLN_LISTED)
  1637. apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
  1638. # ifdef FEAT_EVAL
  1639. if (aborting()) /* autocmds may abort script processing */
  1640. return NULL;
  1641. # endif
  1642. }
  1643. #endif
  1644. return buf;
  1645. }
  1646. /*
  1647. * Free the memory for the options of a buffer.
  1648. * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
  1649. * 'fileencoding'.
  1650. */
  1651. void
  1652. free_buf_options(buf, free_p_ff)
  1653. buf_T *buf;
  1654. int free_p_ff;
  1655. {
  1656. if (free_p_ff)
  1657. {
  1658. #ifdef FEAT_MBYTE
  1659. clear_string_option(&buf->b_p_fenc);
  1660. #endif
  1661. clear_string_option(&buf->b_p_ff);
  1662. #ifdef FEAT_QUICKFIX
  1663. clear_string_option(&buf->b_p_bh);
  1664. clear_string_option(&buf->b_p_bt);
  1665. #endif
  1666. }
  1667. #ifdef FEAT_FIND_ID
  1668. clear_string_option(&buf->b_p_def);
  1669. clear_string_option(&buf->b_p_inc);
  1670. # ifdef FEAT_EVAL
  1671. clear_string_option(&buf->b_p_inex);
  1672. # endif
  1673. #endif
  1674. #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
  1675. clear_string_option(&buf->b_p_inde);
  1676. clear_string_option(&buf->b_p_indk);
  1677. #endif
  1678. #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
  1679. clear_string_option(&buf->b_p_bexpr);
  1680. #endif
  1681. #if defined(FEAT_CRYPT)
  1682. clear_string_option(&buf->b_p_cm);
  1683. #endif
  1684. #if defined(FEAT_EVAL)
  1685. clear_string_option(&buf->b_p_fex);
  1686. #endif
  1687. #ifdef FEAT_CRYPT
  1688. clear_string_option(&buf->b_p_key);
  1689. #endif
  1690. clear_string_option(&buf->b_p_kp);
  1691. clear_string_option(&buf->b_p_mps);
  1692. clear_string_option(&buf->b_p_fo);
  1693. clear_string_option(&buf->b_p_flp);
  1694. clear_string_option(&buf->b_p_isk);
  1695. #ifdef FEAT_KEYMAP
  1696. clear_string_option(&buf->b_p_keymap);
  1697. ga_clear(&buf->b_kmap_ga);
  1698. #endif
  1699. #ifdef FEAT_COMMENTS
  1700. clear_string_option(&buf->b_p_com);
  1701. #endif
  1702. #ifdef FEAT_FOLDING
  1703. clear_string_option(&buf->b_p_cms);
  1704. #endif
  1705. clear_string_option(&buf->b_p_nf);
  1706. #ifdef FEAT_SYN_HL
  1707. clear_string_option(&buf->b_p_syn);
  1708. #endif
  1709. #ifdef FEAT_SPELL
  1710. clear_string_option(&buf->b_s.b_p_spc);
  1711. clear_string_option(&buf->b_s.b_p_spf);
  1712. vim_free(buf->b_s.b_cap_prog);
  1713. buf->b_s.b_cap_prog = NULL;
  1714. clear_string_option(&buf->b_s.b_p_spl);
  1715. #endif
  1716. #ifdef FEAT_SEARCHPATH
  1717. clear_string_option(&buf->b_p_sua);
  1718. #endif
  1719. #ifdef FEAT_AUTOCMD
  1720. clear_string_option(&buf->b_p_ft);
  1721. #endif
  1722. #ifdef FEAT_CINDENT
  1723. clear_string_option(&buf->b_p_cink);
  1724. clear_string_option(&buf->b_p_cino);
  1725. #endif
  1726. #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
  1727. clear_string_option(&buf->b_p_cinw);
  1728. #endif
  1729. #ifdef FEAT_INS_EXPAND
  1730. clear_string_option(&buf->b_p_cpt);
  1731. #endif
  1732. #ifdef FEAT_COMPL_FUNC
  1733. clear_string_option(&buf->b_p_cfu);
  1734. clear_string_option(&buf->b_p_ofu);
  1735. #endif
  1736. #ifdef FEAT_QUICKFIX
  1737. clear_string_option(&buf->b_p_gp);
  1738. clear_string_option(&buf->b_p_mp);
  1739. clear_string_option(&buf->b_p_efm);
  1740. #endif
  1741. clear_string_option(&buf->b_p_ep);
  1742. clear_string_option(&buf->b_p_path);
  1743. clear_string_option(&buf->b_p_tags);
  1744. #ifdef FEAT_INS_EXPAND
  1745. clear_string_option(&buf->b_p_dict);
  1746. clear_string_option(&buf->b_p_tsr);
  1747. #endif
  1748. #ifdef FEAT_TEXTOBJ
  1749. clear_string_option(&buf-