-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
6068 lines (5545 loc) · 148 KB
/
buffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* buffer.c: functions for dealing with the buffer structure
*/
/*
* The buffer list is a double linked list of all buffers.
* Each buffer can be in one of these states:
* never loaded: BF_NEVERLOADED is set, only the file name is valid
* not loaded: b_ml.ml_mfp == NULL, no memfile allocated
* hidden: b_nwindows == 0, loaded but not displayed in a window
* normal: loaded and displayed in a window
*
* Instead of storing file names all over the place, each file name is
* stored in the buffer list. It can be referenced by a number.
*
* The current implementation remembers all file names ever used.
*/
#include "vim.h"
#ifdef FEAT_EVAL
// Determines how deeply nested %{} blocks will be evaluated in statusline.
# define MAX_STL_EVAL_DEPTH 100
#endif
static void enter_buffer(buf_T *buf);
static void buflist_getfpos(void);
static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
#ifdef UNIX
static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
static int buf_same_ino(buf_T *buf, stat_T *stp);
#else
static int otherfile_buf(buf_T *buf, char_u *ffname);
#endif
static int value_changed(char_u *str, char_u **last);
static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
static void free_buffer(buf_T *);
static void free_buffer_stuff(buf_T *buf, int free_options);
static int bt_nofileread(buf_T *buf);
static void no_write_message_buf(buf_T *buf);
#ifdef UNIX
# define dev_T dev_t
#else
# define dev_T unsigned
#endif
#define FOR_ALL_BUFS_FROM_LAST(buf) \
for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
#if defined(FEAT_QUICKFIX)
static char *msg_loclist = N_("[Location List]");
static char *msg_qflist = N_("[Quickfix List]");
#endif
// Number of times free_buffer() was called.
static int buf_free_count = 0;
static int top_file_num = 1; // highest file number
static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
/*
* Return the highest possible buffer number.
*/
int
get_highest_fnum(void)
{
return top_file_num - 1;
}
/*
* Read data from buffer for retrying.
*/
static int
read_buffer(
int read_stdin, // read file from stdin, otherwise fifo
exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
int flags) // extra flags for readfile()
{
int retval = OK;
linenr_T line_count;
// Read from the buffer which the text is already filled in and append at
// the end. This makes it possible to retry when 'fileformat' or
// 'fileencoding' was guessed wrong.
line_count = curbuf->b_ml.ml_line_count;
retval = readfile(
read_stdin ? NULL : curbuf->b_ffname,
read_stdin ? NULL : curbuf->b_fname,
line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
flags | READ_BUFFER);
if (retval == OK)
{
// Delete the binary lines.
while (--line_count >= 0)
ml_delete((linenr_T)1);
}
else
{
// Delete the converted lines.
while (curbuf->b_ml.ml_line_count > line_count)
ml_delete(line_count);
}
// Put the cursor on the first line.
curwin->w_cursor.lnum = 1;
curwin->w_cursor.col = 0;
if (read_stdin)
{
// Set or reset 'modified' before executing autocommands, so that
// it can be changed there.
if (!readonlymode && !BUFEMPTY())
changed();
else if (retval == OK)
unchanged(curbuf, FALSE, TRUE);
if (retval == OK)
{
#ifdef FEAT_EVAL
apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
curbuf, &retval);
#else
apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
#endif
}
}
return retval;
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
*/
void
buffer_ensure_loaded(buf_T *buf)
{
if (buf->b_ml.ml_mfp != NULL)
return;
aco_save_T aco;
// Make sure the buffer is in a window. If not then skip it.
aucmd_prepbuf(&aco, buf);
if (curbuf == buf)
{
if (swap_exists_action != SEA_READONLY)
swap_exists_action = SEA_NONE;
open_buffer(FALSE, NULL, 0);
aucmd_restbuf(&aco);
}
}
#endif
/*
* Open current buffer, that is: open the memfile and read the file into
* memory.
* Return FAIL for failure, OK otherwise.
*/
int
open_buffer(
int read_stdin, // read file from stdin
exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
int flags_arg) // extra flags for readfile()
{
int flags = flags_arg;
int retval = OK;
bufref_T old_curbuf;
#ifdef FEAT_SYN_HL
long old_tw = curbuf->b_p_tw;
#endif
int read_fifo = FALSE;
// The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
// When re-entering the same buffer, it should not change, because the
// user may have reset the flag by hand.
if (readonlymode && curbuf->b_ffname != NULL
&& (curbuf->b_flags & BF_NEVERLOADED))
curbuf->b_p_ro = TRUE;
if (ml_open(curbuf) == FAIL)
{
// There MUST be a memfile, otherwise we can't do anything
// If we can't create one for the current buffer, take another buffer
close_buffer(NULL, curbuf, 0, FALSE, FALSE);
FOR_ALL_BUFFERS(curbuf)
if (curbuf->b_ml.ml_mfp != NULL)
break;
// If there is no memfile at all, exit.
// This is OK, since there are no changes to lose.
if (curbuf == NULL)
{
emsg(_(e_cannot_allocate_any_buffer_exiting));
// Don't try to do any saving, with "curbuf" NULL almost nothing
// will work.
v_dying = 2;
getout(2);
}
emsg(_(e_cannot_allocate_buffer_using_other_one));
enter_buffer(curbuf);
#ifdef FEAT_SYN_HL
if (old_tw != curbuf->b_p_tw)
check_colorcolumn(curwin);
#endif
return FAIL;
}
// Do not sync this buffer yet, may first want to read the file.
if (curbuf->b_ml.ml_mfp != NULL)
curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
// The autocommands in readfile() may change the buffer, but only AFTER
// reading the file.
set_bufref(&old_curbuf, curbuf);
modified_was_set = FALSE;
// mark cursor position as being invalid
curwin->w_valid = 0;
// A buffer without an actual file should not use the buffer name to read a
// file.
if (bt_nofileread(curbuf))
flags |= READ_NOFILE;
// Read the file if there is one.
if (curbuf->b_ffname != NULL
#ifdef FEAT_NETBEANS_INTG
&& netbeansReadFile
#endif
)
{
int old_msg_silent = msg_silent;
#ifdef UNIX
int save_bin = curbuf->b_p_bin;
int perm;
#endif
#ifdef FEAT_NETBEANS_INTG
int oldFire = netbeansFireChanges;
netbeansFireChanges = 0;
#endif
#ifdef UNIX
perm = mch_getperm(curbuf->b_ffname);
if (perm >= 0 && (S_ISFIFO(perm)
|| S_ISSOCK(perm)
# ifdef OPEN_CHR_FILES
|| (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
# endif
))
read_fifo = TRUE;
if (read_fifo)
curbuf->b_p_bin = TRUE;
#endif
if (shortmess(SHM_FILEINFO))
msg_silent = 1;
retval = readfile(curbuf->b_ffname, curbuf->b_fname,
(linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
#ifdef UNIX
if (read_fifo)
{
curbuf->b_p_bin = save_bin;
if (retval == OK)
retval = read_buffer(FALSE, eap, flags);
}
#endif
msg_silent = old_msg_silent;
#ifdef FEAT_NETBEANS_INTG
netbeansFireChanges = oldFire;
#endif
// Help buffer is filtered.
if (bt_help(curbuf))
fix_help_buffer();
}
else if (read_stdin)
{
int save_bin = curbuf->b_p_bin;
// First read the text in binary mode into the buffer.
// Then read from that same buffer and append at the end. This makes
// it possible to retry when 'fileformat' or 'fileencoding' was
// guessed wrong.
curbuf->b_p_bin = TRUE;
retval = readfile(NULL, NULL, (linenr_T)0,
(linenr_T)0, (linenr_T)MAXLNUM, NULL,
flags | (READ_NEW + READ_STDIN));
curbuf->b_p_bin = save_bin;
if (retval == OK)
retval = read_buffer(TRUE, eap, flags);
}
// Can now sync this buffer in ml_sync_all().
if (curbuf->b_ml.ml_mfp != NULL
&& curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
// if first time loading this buffer, init b_chartab[]
if (curbuf->b_flags & BF_NEVERLOADED)
{
(void)buf_init_chartab(curbuf, FALSE);
parse_cino(curbuf);
}
// Set/reset the Changed flag first, autocmds may change the buffer.
// Apply the automatic commands, before processing the modelines.
// So the modelines have priority over autocommands.
//
// When reading stdin, the buffer contents always needs writing, so set
// the changed flag. Unless in readonly mode: "ls | gview -".
// When interrupted and 'cpoptions' contains 'i' set changed flag.
if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
|| modified_was_set // ":set modified" used in autocmd
#ifdef FEAT_EVAL
|| (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
#endif
)
changed();
else if (retval == OK && !read_stdin && !read_fifo)
unchanged(curbuf, FALSE, TRUE);
save_file_ff(curbuf); // keep this fileformat
// Set last_changedtick to avoid triggering a TextChanged autocommand right
// after it was added.
curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
// require "!" to overwrite the file, because it wasn't read completely
#ifdef FEAT_EVAL
if (aborting())
#else
if (got_int)
#endif
curbuf->b_flags |= BF_READERR;
#ifdef FEAT_FOLDING
// Need to update automatic folding. Do this before the autocommands,
// they may use the fold info.
foldUpdateAll(curwin);
#endif
// need to set w_topline, unless some autocommand already did that.
if (!(curwin->w_valid & VALID_TOPLINE))
{
curwin->w_topline = 1;
#ifdef FEAT_DIFF
curwin->w_topfill = 0;
#endif
}
#ifdef FEAT_EVAL
apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
#else
apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
#endif
if (retval != OK)
return retval;
// The autocommands may have changed the current buffer. Apply the
// modelines to the correct buffer, if it still exists and is loaded.
if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
{
aco_save_T aco;
// Go to the buffer that was opened, make sure it is in a window.
// If not then skip it.
aucmd_prepbuf(&aco, old_curbuf.br_buf);
if (curbuf == old_curbuf.br_buf)
{
do_modelines(0);
curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
if ((flags & READ_NOWINENTER) == 0)
#ifdef FEAT_EVAL
apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
FALSE, curbuf, &retval);
#else
apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
FALSE, curbuf);
#endif
// restore curwin/curbuf and a few other things
aucmd_restbuf(&aco);
}
}
return retval;
}
/*
* Store "buf" in "bufref" and set the free count.
*/
void
set_bufref(bufref_T *bufref, buf_T *buf)
{
bufref->br_buf = buf;
bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
bufref->br_buf_free_count = buf_free_count;
}
/*
* Return TRUE if "bufref->br_buf" points to the same buffer as when
* set_bufref() was called and it is a valid buffer.
* Only goes through the buffer list if buf_free_count changed.
* Also checks if b_fnum is still the same, a :bwipe followed by :new might get
* the same allocated memory, but it's a different buffer.
*/
int
bufref_valid(bufref_T *bufref)
{
return bufref->br_buf_free_count == buf_free_count
? TRUE : buf_valid(bufref->br_buf)
&& bufref->br_fnum == bufref->br_buf->b_fnum;
}
/*
* Return TRUE if "buf" points to a valid buffer (in the buffer list).
* This can be slow if there are many buffers, prefer using bufref_valid().
*/
int
buf_valid(buf_T *buf)
{
buf_T *bp;
// Assume that we more often have a recent buffer, start with the last
// one.
FOR_ALL_BUFS_FROM_LAST(bp)
if (bp == buf)
return TRUE;
return FALSE;
}
/*
* A hash table used to quickly lookup a buffer by its number.
*/
static hashtab_T buf_hashtab;
static void
buf_hashtab_add(buf_T *buf)
{
sprintf((char *)buf->b_key, "%x", buf->b_fnum);
if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
emsg(_(e_buffer_cannot_be_registered));
}
static void
buf_hashtab_remove(buf_T *buf)
{
hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
if (!HASHITEM_EMPTY(hi))
hash_remove(&buf_hashtab, hi, "close buffer");
}
/*
* Return TRUE when buffer "buf" can be unloaded.
* Give an error message and return FALSE when the buffer is locked or the
* screen is being redrawn and the buffer is in a window.
*/
static int
can_unload_buffer(buf_T *buf)
{
int can_unload = !buf->b_locked;
if (can_unload && updating_screen)
{
win_T *wp;
FOR_ALL_WINDOWS(wp)
if (wp->w_buffer == buf)
{
can_unload = FALSE;
break;
}
}
if (!can_unload)
{
char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
fname != NULL ? fname : (char_u *)"[No Name]");
}
return can_unload;
}
/*
* Close the link to a buffer.
* "action" is used when there is no longer a window for the buffer.
* It can be:
* 0 buffer becomes hidden
* DOBUF_UNLOAD buffer is unloaded
* DOBUF_DEL buffer is unloaded and removed from buffer list
* DOBUF_WIPE buffer is unloaded and really deleted
* DOBUF_WIPE_REUSE idem, and add to buf_reuse list
* When doing all but the first one on the current buffer, the caller should
* get a new buffer very soon!
*
* The 'bufhidden' option can force freeing and deleting.
*
* When "abort_if_last" is TRUE then do not close the buffer if autocommands
* cause there to be only one window with this buffer. e.g. when ":quit" is
* supposed to close the window but autocommands close all other windows.
*
* When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
*
* Return TRUE when we got to the end and b_nwindows was decremented.
*/
int
close_buffer(
win_T *win, // if not NULL, set b_last_cursor
buf_T *buf,
int action,
int abort_if_last,
int ignore_abort)
{
int is_curbuf;
int nwindows;
bufref_T bufref;
int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
win_T *the_curwin = curwin;
tabpage_T *the_curtab = curtab;
int unload_buf = (action != 0);
int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
int del_buf = (action == DOBUF_DEL || wipe_buf);
CHECK_CURBUF;
// Force unloading or deleting when 'bufhidden' says so.
// The caller must take care of NOT deleting/freeing when 'bufhidden' is
// "hide" (otherwise we could never free or delete a buffer).
if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
{
del_buf = TRUE;
unload_buf = TRUE;
}
else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
{
del_buf = TRUE;
unload_buf = TRUE;
wipe_buf = TRUE;
}
else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
unload_buf = TRUE;
#ifdef FEAT_TERMINAL
// depending on how we get here b_nwindows may already be zero
if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
{
CHECK_CURBUF;
if (term_job_running(buf->b_term))
{
if (wipe_buf || unload_buf)
{
if (!can_unload_buffer(buf))
return FALSE;
// Wiping out or unloading a terminal buffer kills the job.
free_terminal(buf);
// A terminal buffer is wiped out when job has finished.
del_buf = TRUE;
unload_buf = TRUE;
wipe_buf = TRUE;
}
else
{
// The job keeps running, hide the buffer.
del_buf = FALSE;
unload_buf = FALSE;
}
}
else if (buf->b_p_bh[0] == 'h' && !del_buf)
{
// Hide a terminal buffer.
unload_buf = FALSE;
}
else
{
if (del_buf || unload_buf)
{
// A terminal buffer is wiped out if the job has finished.
// We only do this when there's an intention to unload the
// buffer. This way, :hide and other similar commands won't
// wipe the buffer.
del_buf = TRUE;
unload_buf = TRUE;
wipe_buf = TRUE;
}
}
CHECK_CURBUF;
}
#endif
// Disallow deleting the buffer when it is locked (already being closed or
// halfway a command that relies on it). Unloading is allowed.
if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
return FALSE;
// check no autocommands closed the window
if (win != NULL && win_valid_any_tab(win))
{
// Set b_last_cursor when closing the last window for the buffer.
// Remember the last cursor position and window options of the buffer.
// This used to be only for the current window, but then options like
// 'foldmethod' may be lost with a ":only" command.
if (buf->b_nwindows == 1)
set_last_cursor(win);
buflist_setfpos(buf, win,
win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
win->w_cursor.col, TRUE);
}
set_bufref(&bufref, buf);
// When the buffer is no longer in a window, trigger BufWinLeave
if (buf->b_nwindows == 1)
{
++buf->b_locked;
++buf->b_locked_split;
if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
{
// Autocommands deleted the buffer.
aucmd_abort:
emsg(_(e_autocommands_caused_command_to_abort));
return FALSE;
}
--buf->b_locked;
--buf->b_locked_split;
if (abort_if_last && one_window())
// Autocommands made this the only window.
goto aucmd_abort;
// When the buffer becomes hidden, but is not unloaded, trigger
// BufHidden
if (!unload_buf)
{
++buf->b_locked;
++buf->b_locked_split;
if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
// Autocommands deleted the buffer.
goto aucmd_abort;
--buf->b_locked;
--buf->b_locked_split;
if (abort_if_last && one_window())
// Autocommands made this the only window.
goto aucmd_abort;
}
#ifdef FEAT_EVAL
// autocmds may abort script processing
if (!ignore_abort && aborting())
return FALSE;
#endif
}
// If the buffer was in curwin and the window has changed, go back to that
// window, if it still exists. This avoids that ":edit x" triggering a
// "tabnext" BufUnload autocmd leaves a window behind without a buffer.
if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
{
block_autocmds();
goto_tabpage_win(the_curtab, the_curwin);
unblock_autocmds();
}
nwindows = buf->b_nwindows;
// decrease the link count from windows (unless not in any window)
if (buf->b_nwindows > 0)
--buf->b_nwindows;
#ifdef FEAT_DIFF
if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
#endif
// Return when a window is displaying the buffer or when it's not
// unloaded.
if (buf->b_nwindows > 0 || !unload_buf)
return FALSE;
// Always remove the buffer when there is no file name.
if (buf->b_ffname == NULL)
del_buf = TRUE;
// When closing the current buffer stop Visual mode before freeing
// anything.
if (buf == curbuf && VIsual_active
#if defined(EXITFREE)
&& !entered_free_all_mem
#endif
)
end_visual_mode();
// Free all things allocated for this buffer.
// Also calls the "BufDelete" autocommands when del_buf is TRUE.
//
// Remember if we are closing the current buffer. Restore the number of
// windows, so that autocommands in buf_freeall() don't get confused.
is_curbuf = (buf == curbuf);
buf->b_nwindows = nwindows;
buf_freeall(buf, (del_buf ? BFA_DEL : 0)
+ (wipe_buf ? BFA_WIPE : 0)
+ (ignore_abort ? BFA_IGNORE_ABORT : 0));
// Autocommands may have deleted the buffer.
if (!bufref_valid(&bufref))
return FALSE;
#ifdef FEAT_EVAL
// autocmds may abort script processing
if (!ignore_abort && aborting())
return FALSE;
#endif
// It's possible that autocommands change curbuf to the one being deleted.
// This might cause the previous curbuf to be deleted unexpectedly. But
// in some cases it's OK to delete the curbuf, because a new one is
// obtained anyway. Therefore only return if curbuf changed to the
// deleted buffer.
if (buf == curbuf && !is_curbuf)
return FALSE;
if (win_valid_any_tab(win) && win->w_buffer == buf)
win->w_buffer = NULL; // make sure we don't use the buffer now
// Autocommands may have opened or closed windows for this buffer.
// Decrement the count for the close we do here.
if (buf->b_nwindows > 0)
--buf->b_nwindows;
/*
* Remove the buffer from the list.
*/
if (wipe_buf)
{
// Do not wipe out the buffer if it is used in a window.
if (buf->b_nwindows > 0)
return FALSE;
if (action == DOBUF_WIPE_REUSE)
{
// we can re-use this buffer number, store it
if (buf_reuse.ga_itemsize == 0)
ga_init2(&buf_reuse, sizeof(int), 50);
if (ga_grow(&buf_reuse, 1) == OK)
((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
}
if (buf->b_sfname != buf->b_ffname)
VIM_CLEAR(buf->b_sfname);
else
buf->b_sfname = NULL;
VIM_CLEAR(buf->b_ffname);
if (buf->b_prev == NULL)
firstbuf = buf->b_next;
else
buf->b_prev->b_next = buf->b_next;
if (buf->b_next == NULL)
lastbuf = buf->b_prev;
else
buf->b_next->b_prev = buf->b_prev;
free_buffer(buf);
}
else
{
if (del_buf)
{
// Free all internal variables and reset option values, to make
// ":bdel" compatible with Vim 5.7.
free_buffer_stuff(buf, TRUE);
// Make it look like a new buffer.
buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
// Init the options when loaded again.
buf->b_p_initialized = FALSE;
}
buf_clear_file(buf);
if (del_buf)
buf->b_p_bl = FALSE;
}
// NOTE: at this point "curbuf" may be invalid!
return TRUE;
}
/*
* Make buffer not contain a file.
*/
void
buf_clear_file(buf_T *buf)
{
buf->b_ml.ml_line_count = 1;
unchanged(buf, TRUE, TRUE);
buf->b_shortname = FALSE;
buf->b_p_eof = FALSE;
buf->b_start_eof = FALSE;
buf->b_p_eol = TRUE;
buf->b_start_eol = TRUE;
buf->b_p_bomb = FALSE;
buf->b_start_bomb = FALSE;
buf->b_ml.ml_mfp = NULL;
buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
#ifdef FEAT_NETBEANS_INTG
netbeans_deleted_all_lines(buf);
#endif
}
/*
* buf_freeall() - free all things allocated for a buffer that are related to
* the file. Careful: get here with "curwin" NULL when exiting.
* flags:
* BFA_DEL buffer is going to be deleted
* BFA_WIPE buffer is going to be wiped out
* BFA_KEEP_UNDO do not free undo information
* BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
*/
void
buf_freeall(buf_T *buf, int flags)
{
int is_curbuf = (buf == curbuf);
bufref_T bufref;
int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
win_T *the_curwin = curwin;
tabpage_T *the_curtab = curtab;
// Make sure the buffer isn't closed by autocommands.
++buf->b_locked;
++buf->b_locked_split;
set_bufref(&bufref, buf);
if (buf->b_ml.ml_mfp != NULL)
{
if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
// autocommands deleted the buffer
return;
}
if ((flags & BFA_DEL) && buf->b_p_bl)
{
if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
// autocommands deleted the buffer
return;
}
if (flags & BFA_WIPE)
{
if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
// autocommands deleted the buffer
return;
}
--buf->b_locked;
--buf->b_locked_split;
// If the buffer was in curwin and the window has changed, go back to that
// window, if it still exists. This avoids that ":edit x" triggering a
// "tabnext" BufUnload autocmd leaves a window behind without a buffer.
if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
{
block_autocmds();
goto_tabpage_win(the_curtab, the_curwin);
unblock_autocmds();
}
#ifdef FEAT_EVAL
// autocmds may abort script processing
if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
return;
#endif
// It's possible that autocommands change curbuf to the one being deleted.
// This might cause curbuf to be deleted unexpectedly. But in some cases
// it's OK to delete the curbuf, because a new one is obtained anyway.
// Therefore only return if curbuf changed to the deleted buffer.
if (buf == curbuf && !is_curbuf)
return;
#ifdef FEAT_DIFF
diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
#endif
#ifdef FEAT_SYN_HL
// Remove any ownsyntax, unless exiting.
if (curwin != NULL && curwin->w_buffer == buf)
reset_synblock(curwin);
#endif
#ifdef FEAT_FOLDING
// No folds in an empty buffer.
{
win_T *win;
tabpage_T *tp;
FOR_ALL_TAB_WINDOWS(tp, win)
if (win->w_buffer == buf)
clearFolding(win);
}
#endif
#ifdef FEAT_TCL
tcl_buffer_free(buf);
#endif
ml_close(buf, TRUE); // close and delete the memline/memfile
buf->b_ml.ml_line_count = 0; // no lines in buffer
if ((flags & BFA_KEEP_UNDO) == 0)
// free the memory allocated for undo
// and reset all undo information
u_clearallandblockfree(buf);
#ifdef FEAT_SYN_HL
syntax_clear(&buf->b_s); // reset syntax info
#endif
#ifdef FEAT_PROP_POPUP
clear_buf_prop_types(buf);
#endif
buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
}
/*
* Free a buffer structure and the things it contains related to the buffer
* itself (not the file, that must have been done already).
*/
static void
free_buffer(buf_T *buf)
{
++buf_free_count;
free_buffer_stuff(buf, TRUE);
#ifdef FEAT_EVAL
// b:changedtick uses an item in buf_T, remove it now
dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
unref_var_dict(buf->b_vars);
remove_listeners(buf);
#endif
#ifdef FEAT_LUA
lua_buffer_free(buf);
#endif
#ifdef FEAT_MZSCHEME
mzscheme_buffer_free(buf);
#endif
#ifdef FEAT_PERL
perl_buf_free(buf);
#endif
#ifdef FEAT_PYTHON
python_buffer_free(buf);
#endif
#ifdef FEAT_PYTHON3
python3_buffer_free(buf);
#endif
#ifdef FEAT_RUBY
ruby_buffer_free(buf);
#endif
#ifdef FEAT_JOB_CHANNEL
channel_buffer_free(buf);
#endif
#ifdef FEAT_TERMINAL
free_terminal(buf);
#endif
#ifdef FEAT_JOB_CHANNEL
vim_free(buf->b_prompt_text);
free_callback(&buf->b_prompt_callback);
free_callback(&buf->b_prompt_interrupt);
#endif
buf_hashtab_remove(buf);
aubuflocal_remove(buf);
if (autocmd_busy)
{
// Do not free the buffer structure while autocommands are executing,
// it's still needed. Free it when autocmd_busy is reset.
buf->b_next = au_pending_free_buf;
au_pending_free_buf = buf;
}
else
{
vim_free(buf);
if (curbuf == buf)
curbuf = NULL; // make clear it's not to be used
}
}
/*
* Initializes b:changedtick.
*/