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