blob: eeba5301255c449dfb1b69279895382f973ae0b9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
36static wininfo_T *find_wininfo(buf_T *buf, int skip_diff_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020038static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
40static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
48static void free_buffer(buf_T *);
49static void free_buffer_stuff(buf_T *buf, int free_options);
50static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar4033c552017-09-16 20:54:51 +020062#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066#ifdef FEAT_AUTOCMD
67static char *e_auabort = N_("E855: Autocommands caused command to abort");
68#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020070/* Number of times free_buffer() was called. */
71static int buf_free_count = 0;
72
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020073/* Read data from buffer for retrying. */
74 static int
75read_buffer(
76 int read_stdin, /* read file from stdin, otherwise fifo */
77 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
78 int flags) /* extra flags for readfile() */
79{
80 int retval = OK;
81 linenr_T line_count;
82
83 /*
84 * Read from the buffer which the text is already filled in and append at
85 * the end. This makes it possible to retry when 'fileformat' or
86 * 'fileencoding' was guessed wrong.
87 */
88 line_count = curbuf->b_ml.ml_line_count;
89 retval = readfile(
90 read_stdin ? NULL : curbuf->b_ffname,
91 read_stdin ? NULL : curbuf->b_fname,
92 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
93 flags | READ_BUFFER);
94 if (retval == OK)
95 {
96 /* Delete the binary lines. */
97 while (--line_count >= 0)
98 ml_delete((linenr_T)1, FALSE);
99 }
100 else
101 {
102 /* Delete the converted lines. */
103 while (curbuf->b_ml.ml_line_count > line_count)
104 ml_delete(line_count, FALSE);
105 }
106 /* Put the cursor on the first line. */
107 curwin->w_cursor.lnum = 1;
108 curwin->w_cursor.col = 0;
109
110 if (read_stdin)
111 {
112 /* Set or reset 'modified' before executing autocommands, so that
113 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100114 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 else if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117 unchanged(curbuf, FALSE);
118
119#ifdef FEAT_AUTOCMD
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100120 if (retval == OK)
121 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200122# ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100123 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200124 curbuf, &retval);
125# else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100126 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200127# endif
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100128 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200129#endif
130 }
131 return retval;
132}
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200135 * Open current buffer, that is: open the memfile and read the file into
136 * memory.
137 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 */
139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100140open_buffer(
141 int read_stdin, /* read file from stdin */
142 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
143 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145 int retval = OK;
146#ifdef FEAT_AUTOCMD
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200147 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100149#ifdef FEAT_SYN_HL
150 long old_tw = curbuf->b_p_tw;
151#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200152 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154 /*
155 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
156 * When re-entering the same buffer, it should not change, because the
157 * user may have reset the flag by hand.
158 */
159 if (readonlymode && curbuf->b_ffname != NULL
160 && (curbuf->b_flags & BF_NEVERLOADED))
161 curbuf->b_p_ro = TRUE;
162
Bram Moolenaar4770d092006-01-12 23:22:24 +0000163 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 {
165 /*
166 * There MUST be a memfile, otherwise we can't do anything
167 * If we can't create one for the current buffer, take another buffer
168 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100169 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200170 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (curbuf->b_ml.ml_mfp != NULL)
172 break;
173 /*
174 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000175 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 */
177 if (curbuf == NULL)
178 {
179 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
180 getout(2);
181 }
182 EMSG(_("E83: Cannot allocate buffer, using other one..."));
183 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100184#ifdef FEAT_SYN_HL
185 if (old_tw != curbuf->b_p_tw)
186 check_colorcolumn(curwin);
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 return FAIL;
189 }
190
191#ifdef FEAT_AUTOCMD
192 /* The autocommands in readfile() may change the buffer, but only AFTER
193 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200194 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 modified_was_set = FALSE;
196#endif
197
198 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100199 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
201 if (curbuf->b_ffname != NULL
202#ifdef FEAT_NETBEANS_INTG
203 && netbeansReadFile
204#endif
205 )
206 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100207 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200208#ifdef UNIX
209 int save_bin = curbuf->b_p_bin;
210 int perm;
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifdef FEAT_NETBEANS_INTG
213 int oldFire = netbeansFireChanges;
214
215 netbeansFireChanges = 0;
216#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200217#ifdef UNIX
218 perm = mch_getperm(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
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200226# ifdef OPEN_CHR_FILES
227 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
228# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200229 ))
230 read_fifo = TRUE;
231 if (read_fifo)
232 curbuf->b_p_bin = TRUE;
233#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100234 if (shortmess(SHM_FILEINFO))
235 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200237 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200238 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
239#ifdef UNIX
240 if (read_fifo)
241 {
242 curbuf->b_p_bin = save_bin;
243 if (retval == OK)
244 retval = read_buffer(FALSE, eap, flags);
245 }
246#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100247 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_NETBEANS_INTG
249 netbeansFireChanges = oldFire;
250#endif
251 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200252 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 fix_help_buffer();
254 }
255 else if (read_stdin)
256 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200257 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259 /*
260 * First read the text in binary mode into the buffer.
261 * Then read from that same buffer and append at the end. This makes
262 * it possible to retry when 'fileformat' or 'fileencoding' was
263 * guessed wrong.
264 */
265 curbuf->b_p_bin = TRUE;
266 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200267 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
268 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 curbuf->b_p_bin = save_bin;
270 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200271 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273
274 /* if first time loading this buffer, init b_chartab[] */
275 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100278#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100279 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100280#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
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 | gview -".
290 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000291 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#ifdef FEAT_AUTOCMD
293 || modified_was_set /* ":set modified" used in autocmd */
294# ifdef FEAT_EVAL
295 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
296# endif
297#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000298 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100300 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 unchanged(curbuf, FALSE);
302 save_file_ff(curbuf); /* keep this fileformat */
303
304 /* require "!" to overwrite the file, because it wasn't read completely */
305#ifdef FEAT_EVAL
306 if (aborting())
307#else
308 if (got_int)
309#endif
310 curbuf->b_flags |= BF_READERR;
311
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000312#ifdef FEAT_FOLDING
313 /* Need to update automatic folding. Do this before the autocommands,
314 * they may use the fold info. */
315 foldUpdateAll(curwin);
316#endif
317
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#ifdef FEAT_AUTOCMD
319 /* need to set w_topline, unless some autocommand already did that. */
320 if (!(curwin->w_valid & VALID_TOPLINE))
321 {
322 curwin->w_topline = 1;
323# ifdef FEAT_DIFF
324 curwin->w_topfill = 0;
325# endif
326 }
327# ifdef FEAT_EVAL
328 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
329# else
330 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
331# endif
332#endif
333
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100334 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
336#ifdef FEAT_AUTOCMD
337 /*
338 * The autocommands may have changed the current buffer. Apply the
339 * modelines to the correct buffer, if it still exists and is loaded.
340 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200341 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
343 aco_save_T aco;
344
345 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200346 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000348 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
350
351#ifdef FEAT_AUTOCMD
352# ifdef FEAT_EVAL
353 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
354 &retval);
355# else
356 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
357# endif
358
359 /* restore curwin/curbuf and a few other things */
360 aucmd_restbuf(&aco);
361 }
362#endif
363 }
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 return retval;
366}
367
368/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200369 * Store "buf" in "bufref" and set the free count.
370 */
371 void
372set_bufref(bufref_T *bufref, buf_T *buf)
373{
374 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200375 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200376 bufref->br_buf_free_count = buf_free_count;
377}
378
379/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200380 * Return TRUE if "bufref->br_buf" points to the same buffer as when
381 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200382 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200383 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
384 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200385 */
386 int
387bufref_valid(bufref_T *bufref)
388{
389 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200390 ? TRUE : buf_valid(bufref->br_buf)
391 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200392}
393
394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200396 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100399buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 buf_T *bp;
402
Bram Moolenaar82404332016-07-10 17:00:38 +0200403 /* Assume that we more often have a recent buffer, start with the last
404 * one. */
405 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (bp == buf)
407 return TRUE;
408 return FALSE;
409}
410
411/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200412 * A hash table used to quickly lookup a buffer by its number.
413 */
414static hashtab_T buf_hashtab;
415
416 static void
417buf_hashtab_add(buf_T *buf)
418{
419 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
420 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
421 EMSG(_("E931: Buffer cannot be registered"));
422}
423
424 static void
425buf_hashtab_remove(buf_T *buf)
426{
427 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
428
429 if (!HASHITEM_EMPTY(hi))
430 hash_remove(&buf_hashtab, hi);
431}
432
433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 * Close the link to a buffer.
435 * "action" is used when there is no longer a window for the buffer.
436 * It can be:
437 * 0 buffer becomes hidden
438 * DOBUF_UNLOAD buffer is unloaded
439 * DOBUF_DELETE buffer is unloaded and removed from buffer list
440 * DOBUF_WIPE buffer is unloaded and really deleted
441 * When doing all but the first one on the current buffer, the caller should
442 * get a new buffer very soon!
443 *
444 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100445 *
446 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
447 * cause there to be only one window with this buffer. e.g. when ":quit" is
448 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 */
450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100451close_buffer(
452 win_T *win, /* if not NULL, set b_last_cursor */
453 buf_T *buf,
454 int action,
455 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_AUTOCMD
458 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100459 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200460 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100461 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200462 win_T *the_curwin = curwin;
463 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465 int unload_buf = (action != 0);
466 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
467 int wipe_buf = (action == DOBUF_WIPE);
468
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200469 /*
470 * Force unloading or deleting when 'bufhidden' says so.
471 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
472 * "hide" (otherwise we could never free or delete a buffer).
473 */
474 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
475 {
476 del_buf = TRUE;
477 unload_buf = TRUE;
478 }
479 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
480 {
481 del_buf = TRUE;
482 unload_buf = TRUE;
483 wipe_buf = TRUE;
484 }
485 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
486 unload_buf = TRUE;
487
Bram Moolenaar94053a52017-08-01 21:44:33 +0200488#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200489 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200490 {
491 if (term_job_running(buf->b_term))
492 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200493 if (wipe_buf || unload_buf)
494 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200495 free_terminal(buf);
496 else
497 {
498 /* The job keeps running, hide the buffer. */
499 del_buf = FALSE;
500 unload_buf = FALSE;
501 }
502 }
503 else
504 {
505 /* A terminal buffer is wiped out if the job has finished. */
506 del_buf = TRUE;
507 unload_buf = TRUE;
508 wipe_buf = TRUE;
509 }
510 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
Bram Moolenaar30180b82016-09-04 19:57:56 +0200513#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200514 /* Disallow deleting the buffer when it is locked (already being closed or
515 * halfway a command that relies on it). Unloading is allowed. */
516 if (buf->b_locked > 0 && (del_buf || wipe_buf))
517 {
518 EMSG(_("E937: Attempt to delete a buffer that is in use"));
519 return;
520 }
Bram Moolenaar30180b82016-09-04 19:57:56 +0200521#endif
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200522
Bram Moolenaar4033c552017-09-16 20:54:51 +0200523 /* check no autocommands closed the window */
524 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
526 /* Set b_last_cursor when closing the last window for the buffer.
527 * Remember the last cursor position and window options of the buffer.
528 * This used to be only for the current window, but then options like
529 * 'foldmethod' may be lost with a ":only" command. */
530 if (buf->b_nwindows == 1)
531 set_last_cursor(win);
532 buflist_setfpos(buf, win,
533 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
534 win->w_cursor.col, TRUE);
535 }
536
537#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200538 set_bufref(&bufref, buf);
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 /* When the buffer is no longer in a window, trigger BufWinLeave */
541 if (buf->b_nwindows == 1)
542 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200543 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200544 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
545 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200546 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100547 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200548 /* Autocommands deleted the buffer. */
549aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100550 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100552 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200553 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200554 if (abort_if_last && one_window())
555 /* Autocommands made this the only window. */
556 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
558 /* When the buffer becomes hidden, but is not unloaded, trigger
559 * BufHidden */
560 if (!unload_buf)
561 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200562 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200563 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
564 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200565 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200566 /* Autocommands deleted the buffer. */
567 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200568 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200569 if (abort_if_last && one_window())
570 /* Autocommands made this the only window. */
571 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
573# ifdef FEAT_EVAL
574 if (aborting()) /* autocmds may abort script processing */
575 return;
576# endif
577 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200578
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200579 /* If the buffer was in curwin and the window has changed, go back to that
580 * window, if it still exists. This avoids that ":edit x" triggering a
581 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
582 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
583 {
584 block_autocmds();
585 goto_tabpage_win(the_curtab, the_curwin);
586 unblock_autocmds();
587 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 nwindows = buf->b_nwindows;
590#endif
591
592 /* decrease the link count from windows (unless not in any window) */
593 if (buf->b_nwindows > 0)
594 --buf->b_nwindows;
595
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100596#ifdef FEAT_DIFF
597 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
598 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
599#endif
600
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 /* Return when a window is displaying the buffer or when it's not
602 * unloaded. */
603 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606 /* Always remove the buffer when there is no file name. */
607 if (buf->b_ffname == NULL)
608 del_buf = TRUE;
609
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200610 /* When closing the current buffer stop Visual mode before freeing
611 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200612 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200613#if defined(EXITFREE)
614 && !entered_free_all_mem
615#endif
616 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200617 end_visual_mode();
618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 /*
620 * Free all things allocated for this buffer.
621 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
622 */
623#ifdef FEAT_AUTOCMD
624 /* Remember if we are closing the current buffer. Restore the number of
625 * windows, so that autocommands in buf_freeall() don't get confused. */
626 is_curbuf = (buf == curbuf);
627 buf->b_nwindows = nwindows;
628#endif
629
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200630 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632#ifdef FEAT_AUTOCMD
633 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200634 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 return;
636# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000637 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 return;
639# endif
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 /*
642 * It's possible that autocommands change curbuf to the one being deleted.
643 * This might cause the previous curbuf to be deleted unexpectedly. But
644 * in some cases it's OK to delete the curbuf, because a new one is
645 * obtained anyway. Therefore only return if curbuf changed to the
646 * deleted buffer.
647 */
648 if (buf == curbuf && !is_curbuf)
649 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200650
Bram Moolenaar4033c552017-09-16 20:54:51 +0200651 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200652 win->w_buffer = NULL; /* make sure we don't use the buffer now */
653
654 /* Autocommands may have opened or closed windows for this buffer.
655 * Decrement the count for the close we do here. */
656 if (buf->b_nwindows > 0)
657 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658#endif
659
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000660 /* Change directories when the 'acd' option is set. */
661 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662
663 /*
664 * Remove the buffer from the list.
665 */
666 if (wipe_buf)
667 {
668#ifdef FEAT_SUN_WORKSHOP
669 if (usingSunWorkShop)
670 workshop_file_closed_lineno((char *)buf->b_ffname,
671 (int)buf->b_last_cursor.lnum);
672#endif
673 vim_free(buf->b_ffname);
674 vim_free(buf->b_sfname);
675 if (buf->b_prev == NULL)
676 firstbuf = buf->b_next;
677 else
678 buf->b_prev->b_next = buf->b_next;
679 if (buf->b_next == NULL)
680 lastbuf = buf->b_prev;
681 else
682 buf->b_next->b_prev = buf->b_prev;
683 free_buffer(buf);
684 }
685 else
686 {
687 if (del_buf)
688 {
689 /* Free all internal variables and reset option values, to make
690 * ":bdel" compatible with Vim 5.7. */
691 free_buffer_stuff(buf, TRUE);
692
693 /* Make it look like a new buffer. */
694 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
695
696 /* Init the options when loaded again. */
697 buf->b_p_initialized = FALSE;
698 }
699 buf_clear_file(buf);
700 if (del_buf)
701 buf->b_p_bl = FALSE;
702 }
703}
704
705/*
706 * Make buffer not contain a file.
707 */
708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100709buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
711 buf->b_ml.ml_line_count = 1;
712 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 buf->b_p_eol = TRUE;
715 buf->b_start_eol = TRUE;
716#ifdef FEAT_MBYTE
717 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000718 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#endif
720 buf->b_ml.ml_mfp = NULL;
721 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
722#ifdef FEAT_NETBEANS_INTG
723 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#endif
725}
726
727/*
728 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200729 * the file. Careful: get here with "curwin" NULL when exiting.
730 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200731 * BFA_DEL buffer is going to be deleted
732 * BFA_WIPE buffer is going to be wiped out
733 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100736buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
738#ifdef FEAT_AUTOCMD
739 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200740 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200741 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200742 win_T *the_curwin = curwin;
743 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
Bram Moolenaar5a497892016-09-03 16:29:04 +0200745 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200746 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200747 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200748 if (buf->b_ml.ml_mfp != NULL)
749 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200750 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
751 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200752 && !bufref_valid(&bufref))
753 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200754 return;
755 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200756 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200758 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
759 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200760 && !bufref_valid(&bufref))
761 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 return;
763 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200764 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200766 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
767 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200768 && !bufref_valid(&bufref))
769 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 return;
771 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200772 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200773
Bram Moolenaar5a497892016-09-03 16:29:04 +0200774 /* If the buffer was in curwin and the window has changed, go back to that
775 * window, if it still exists. This avoids that ":edit x" triggering a
776 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
777 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
778 {
779 block_autocmds();
780 goto_tabpage_win(the_curtab, the_curwin);
781 unblock_autocmds();
782 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000785 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 return;
787# endif
788
789 /*
790 * It's possible that autocommands change curbuf to the one being deleted.
791 * This might cause curbuf to be deleted unexpectedly. But in some cases
792 * it's OK to delete the curbuf, because a new one is obtained anyway.
793 * Therefore only return if curbuf changed to the deleted buffer.
794 */
795 if (buf == curbuf && !is_curbuf)
796 return;
797#endif
798#ifdef FEAT_DIFF
799 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
800#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200801#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100802 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200803 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100804 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200805#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000806
807#ifdef FEAT_FOLDING
808 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000809 {
810 win_T *win;
811 tabpage_T *tp;
812
813 FOR_ALL_TAB_WINDOWS(tp, win)
814 if (win->w_buffer == buf)
815 clearFolding(win);
816 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000817#endif
818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#ifdef FEAT_TCL
820 tcl_buffer_free(buf);
821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ml_close(buf, TRUE); /* close and delete the memline/memfile */
823 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200824 if ((flags & BFA_KEEP_UNDO) == 0)
825 {
826 u_blockfree(buf); /* free the memory allocated for undo */
827 u_clearall(buf); /* reset all undo information */
828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200830 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000832 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833}
834
835/*
836 * Free a buffer structure and the things it contains related to the buffer
837 * itself (not the file, that must have been done already).
838 */
839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100840free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200842 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200844#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200845 /* b:changedtick uses an item in buf_T, remove it now */
846 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200847 unref_var_dict(buf->b_vars);
848#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200849#ifdef FEAT_LUA
850 lua_buffer_free(buf);
851#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000852#ifdef FEAT_MZSCHEME
853 mzscheme_buffer_free(buf);
854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#ifdef FEAT_PERL
856 perl_buf_free(buf);
857#endif
858#ifdef FEAT_PYTHON
859 python_buffer_free(buf);
860#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200861#ifdef FEAT_PYTHON3
862 python3_buffer_free(buf);
863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#ifdef FEAT_RUBY
865 ruby_buffer_free(buf);
866#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200867#ifdef FEAT_JOB_CHANNEL
868 channel_buffer_free(buf);
869#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200870#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200871 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200872#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200873
874 buf_hashtab_remove(buf);
875
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200876#ifdef FEAT_AUTOCMD
877 aubuflocal_remove(buf);
878
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200879 if (autocmd_busy)
880 {
881 /* Do not free the buffer structure while autocommands are executing,
882 * it's still needed. Free it when autocmd_busy is reset. */
883 buf->b_next = au_pending_free_buf;
884 au_pending_free_buf = buf;
885 }
886 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000887#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200888 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889}
890
891/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100892 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100893 */
894 static void
895init_changedtick(buf_T *buf)
896{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100897 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100898
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100899 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
900 di->di_tv.v_type = VAR_NUMBER;
901 di->di_tv.v_lock = VAR_FIXED;
902 di->di_tv.vval.v_number = 0;
903
Bram Moolenaar92769c32017-02-25 15:41:37 +0100904#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100905 STRCPY(buf->b_ct_di.di_key, "changedtick");
906 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100907#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100908}
909
910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
912 */
913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100914free_buffer_stuff(
915 buf_T *buf,
916 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
918 if (free_options)
919 {
920 clear_wininfo(buf); /* including window-local options */
921 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200922#ifdef FEAT_SPELL
923 ga_clear(&buf->b_s.b_langp);
924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100927 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100928 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100929
930 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
931 hash_init(&buf->b_vars->dv_hashtab);
932 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100933 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935#endif
936#ifdef FEAT_USR_CMDS
937 uc_clear(&buf->b_ucmds); /* clear local user commands */
938#endif
939#ifdef FEAT_SIGNS
940 buf_delete_signs(buf); /* delete any signs */
941#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000942#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200943 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#ifdef FEAT_LOCALMAP
946 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
947 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
948#endif
949#ifdef FEAT_MBYTE
950 vim_free(buf->b_start_fenc);
951 buf->b_start_fenc = NULL;
952#endif
953}
954
955/*
956 * Free the b_wininfo list for buffer "buf".
957 */
958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100959clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960{
961 wininfo_T *wip;
962
963 while (buf->b_wininfo != NULL)
964 {
965 wip = buf->b_wininfo;
966 buf->b_wininfo = wip->wi_next;
967 if (wip->wi_optset)
968 {
969 clear_winopt(&wip->wi_opt);
970#ifdef FEAT_FOLDING
971 deleteFoldRecurse(&wip->wi_folds);
972#endif
973 }
974 vim_free(wip);
975 }
976}
977
978#if defined(FEAT_LISTCMDS) || defined(PROTO)
979/*
980 * Go to another buffer. Handles the result of the ATTENTION dialog.
981 */
982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100983goto_buffer(
984 exarg_T *eap,
985 int start,
986 int dir,
987 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
Bram Moolenaar4033c552017-09-16 20:54:51 +0200989# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200990 bufref_T old_curbuf;
991
992 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 swap_exists_action = SEA_DIALOG;
995# endif
996 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
997 start, dir, count, eap->forceit);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200998# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1000 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001001# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1002 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001003
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001004 /* Reset the error/interrupt/exception state here so that
1005 * aborting() returns FALSE when closing a window. */
1006 enter_cleanup(&cs);
1007# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001008
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001009 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 win_close(curwin, TRUE);
1011 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001012 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001013
1014# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1015 /* Restore the error/interrupt/exception state if not discarded by a
1016 * new aborting error, interrupt, or uncaught exception. */
1017 leave_cleanup(&cs);
1018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001021 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022# endif
1023}
1024#endif
1025
Bram Moolenaare64ac772005-12-07 20:54:59 +00001026#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027/*
1028 * Handle the situation of swap_exists_action being set.
1029 * It is allowed for "old_curbuf" to be NULL or invalid.
1030 */
1031 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001032handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001034# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1035 cleanup_T cs;
1036# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001037#ifdef FEAT_SYN_HL
1038 long old_tw = curbuf->b_p_tw;
1039#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001040 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (swap_exists_action == SEA_QUIT)
1043 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001044# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1045 /* Reset the error/interrupt/exception state here so that
1046 * aborting() returns FALSE when closing a buffer. */
1047 enter_cleanup(&cs);
1048# endif
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 /* User selected Quit at ATTENTION prompt. Go back to previous
1051 * buffer. If that buffer is gone or the same as the current one,
1052 * open a new, empty buffer. */
1053 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001054 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001055 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001056 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1057 || old_curbuf->br_buf == curbuf)
1058 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1059 else
1060 buf = old_curbuf->br_buf;
1061 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001062 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001063 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001064#ifdef FEAT_SYN_HL
1065 if (old_tw != curbuf->b_p_tw)
1066 check_colorcolumn(curwin);
1067#endif
1068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001070
1071# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1072 /* Restore the error/interrupt/exception state if not discarded by a
1073 * new aborting error, interrupt, or uncaught exception. */
1074 leave_cleanup(&cs);
1075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 }
1077 else if (swap_exists_action == SEA_RECOVER)
1078 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001079# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1080 /* Reset the error/interrupt/exception state here so that
1081 * aborting() returns FALSE when closing a buffer. */
1082 enter_cleanup(&cs);
1083# endif
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 /* User selected Recover at ATTENTION prompt. */
1086 msg_scroll = TRUE;
1087 ml_recover();
1088 MSG_PUTS("\n"); /* don't overwrite the last message */
1089 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001090 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001091
1092# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1093 /* Restore the error/interrupt/exception state if not discarded by a
1094 * new aborting error, interrupt, or uncaught exception. */
1095 leave_cleanup(&cs);
1096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 swap_exists_action = SEA_NONE;
1099}
1100#endif
1101
1102#if defined(FEAT_LISTCMDS) || defined(PROTO)
1103/*
1104 * do_bufdel() - delete or unload buffer(s)
1105 *
1106 * addr_count == 0: ":bdel" - delete current buffer
1107 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1108 * buffer "end_bnr", then any other arguments.
1109 * addr_count == 2: ":N,N bdel" - delete buffers in range
1110 *
1111 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1112 * DOBUF_DEL (":bdel")
1113 *
1114 * Returns error message or NULL
1115 */
1116 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001117do_bufdel(
1118 int command,
1119 char_u *arg, /* pointer to extra arguments */
1120 int addr_count,
1121 int start_bnr, /* first buffer number in a range */
1122 int end_bnr, /* buffer nr or last buffer nr in a range */
1123 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 int do_current = 0; /* delete current buffer? */
1126 int deleted = 0; /* number of buffers deleted */
1127 char_u *errormsg = NULL; /* return value */
1128 int bnr; /* buffer number */
1129 char_u *p;
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (addr_count == 0)
1132 {
1133 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1134 }
1135 else
1136 {
1137 if (addr_count == 2)
1138 {
1139 if (*arg) /* both range and argument is not allowed */
1140 return (char_u *)_(e_trailing);
1141 bnr = start_bnr;
1142 }
1143 else /* addr_count == 1 */
1144 bnr = end_bnr;
1145
1146 for ( ;!got_int; ui_breakcheck())
1147 {
1148 /*
1149 * delete the current buffer last, otherwise when the
1150 * current buffer is deleted, the next buffer becomes
1151 * the current one and will be loaded, which may then
1152 * also be deleted, etc.
1153 */
1154 if (bnr == curbuf->b_fnum)
1155 do_current = bnr;
1156 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1157 forceit) == OK)
1158 ++deleted;
1159
1160 /*
1161 * find next buffer number to delete/unload
1162 */
1163 if (addr_count == 2)
1164 {
1165 if (++bnr > end_bnr)
1166 break;
1167 }
1168 else /* addr_count == 1 */
1169 {
1170 arg = skipwhite(arg);
1171 if (*arg == NUL)
1172 break;
1173 if (!VIM_ISDIGIT(*arg))
1174 {
1175 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001176 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1177 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (bnr < 0) /* failed */
1179 break;
1180 arg = p;
1181 }
1182 else
1183 bnr = getdigits(&arg);
1184 }
1185 }
1186 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1187 FORWARD, do_current, forceit) == OK)
1188 ++deleted;
1189
1190 if (deleted == 0)
1191 {
1192 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001193 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001195 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001197 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 errormsg = IObuff;
1199 }
1200 else if (deleted >= p_report)
1201 {
1202 if (command == DOBUF_UNLOAD)
1203 {
1204 if (deleted == 1)
1205 MSG(_("1 buffer unloaded"));
1206 else
1207 smsg((char_u *)_("%d buffers unloaded"), deleted);
1208 }
1209 else if (command == DOBUF_DEL)
1210 {
1211 if (deleted == 1)
1212 MSG(_("1 buffer deleted"));
1213 else
1214 smsg((char_u *)_("%d buffers deleted"), deleted);
1215 }
1216 else
1217 {
1218 if (deleted == 1)
1219 MSG(_("1 buffer wiped out"));
1220 else
1221 smsg((char_u *)_("%d buffers wiped out"), deleted);
1222 }
1223 }
1224 }
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
1227 return errormsg;
1228}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001229#endif /* FEAT_LISTCMDS */
1230
1231#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1232 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001234static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001235
1236/*
1237 * Make the current buffer empty.
1238 * Used when it is wiped out and it's the last buffer.
1239 */
1240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001241empty_curbuf(
1242 int close_others,
1243 int forceit,
1244 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001245{
1246 int retval;
1247 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001248 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001249
1250 if (action == DOBUF_UNLOAD)
1251 {
1252 EMSG(_("E90: Cannot unload last buffer"));
1253 return FAIL;
1254 }
1255
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001256 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001257 if (close_others)
1258 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001259 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001260
1261 setpcmark();
1262 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1263 forceit ? ECMD_FORCEIT : 0, curwin);
1264
1265 /*
1266 * do_ecmd() may create a new buffer, then we have to delete
1267 * the old one. But do_ecmd() may have done that already, check
1268 * if the buffer still exists.
1269 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001270 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001271 close_buffer(NULL, buf, action, FALSE);
1272 if (!close_others)
1273 need_fileinfo = FALSE;
1274 return retval;
1275}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276/*
1277 * Implementation of the commands for the buffer list.
1278 *
1279 * action == DOBUF_GOTO go to specified buffer
1280 * action == DOBUF_SPLIT split window and go to specified buffer
1281 * action == DOBUF_UNLOAD unload specified buffer(s)
1282 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1283 * action == DOBUF_WIPE delete specified buffer(s) really
1284 *
1285 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1286 * start == DOBUF_FIRST go to "count" buffer from first buffer
1287 * start == DOBUF_LAST go to "count" buffer from last buffer
1288 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1289 *
1290 * Return FAIL or OK.
1291 */
1292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001293do_buffer(
1294 int action,
1295 int start,
1296 int dir, /* FORWARD or BACKWARD */
1297 int count, /* buffer number or number of buffers */
1298 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 buf_T *buf;
1301 buf_T *bp;
1302 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1303 || action == DOBUF_WIPE);
1304
1305 switch (start)
1306 {
1307 case DOBUF_FIRST: buf = firstbuf; break;
1308 case DOBUF_LAST: buf = lastbuf; break;
1309 default: buf = curbuf; break;
1310 }
1311 if (start == DOBUF_MOD) /* find next modified buffer */
1312 {
1313 while (count-- > 0)
1314 {
1315 do
1316 {
1317 buf = buf->b_next;
1318 if (buf == NULL)
1319 buf = firstbuf;
1320 }
1321 while (buf != curbuf && !bufIsChanged(buf));
1322 }
1323 if (!bufIsChanged(buf))
1324 {
1325 EMSG(_("E84: No modified buffer found"));
1326 return FAIL;
1327 }
1328 }
1329 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1330 {
1331 while (buf != NULL && buf->b_fnum != count)
1332 buf = buf->b_next;
1333 }
1334 else
1335 {
1336 bp = NULL;
1337 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1338 {
1339 /* remember the buffer where we start, we come back there when all
1340 * buffers are unlisted. */
1341 if (bp == NULL)
1342 bp = buf;
1343 if (dir == FORWARD)
1344 {
1345 buf = buf->b_next;
1346 if (buf == NULL)
1347 buf = firstbuf;
1348 }
1349 else
1350 {
1351 buf = buf->b_prev;
1352 if (buf == NULL)
1353 buf = lastbuf;
1354 }
1355 /* don't count unlisted buffers */
1356 if (unload || buf->b_p_bl)
1357 {
1358 --count;
1359 bp = NULL; /* use this buffer as new starting point */
1360 }
1361 if (bp == buf)
1362 {
1363 /* back where we started, didn't find anything. */
1364 EMSG(_("E85: There is no listed buffer"));
1365 return FAIL;
1366 }
1367 }
1368 }
1369
1370 if (buf == NULL) /* could not find it */
1371 {
1372 if (start == DOBUF_FIRST)
1373 {
1374 /* don't warn when deleting */
1375 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001376 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378 else if (dir == FORWARD)
1379 EMSG(_("E87: Cannot go beyond last buffer"));
1380 else
1381 EMSG(_("E88: Cannot go before first buffer"));
1382 return FAIL;
1383 }
1384
1385#ifdef FEAT_GUI
1386 need_mouse_correct = TRUE;
1387#endif
1388
1389#ifdef FEAT_LISTCMDS
1390 /*
1391 * delete buffer buf from memory and/or the list
1392 */
1393 if (unload)
1394 {
1395 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001396 bufref_T bufref;
1397
1398 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
1400 /* When unloading or deleting a buffer that's already unloaded and
1401 * unlisted: fail silently. */
1402 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1403 return FAIL;
1404
1405 if (!forceit && bufIsChanged(buf))
1406 {
1407#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1408 if ((p_confirm || cmdmod.confirm) && p_write)
1409 {
1410 dialog_changed(buf, FALSE);
1411# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001412 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 /* Autocommand deleted buffer, oops! It's not changed
1414 * now. */
1415 return FAIL;
1416# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001417 /* If it's still changed fail silently, the dialog already
1418 * mentioned why it fails. */
1419 if (bufIsChanged(buf))
1420 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423#endif
1424 {
1425 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1426 buf->b_fnum);
1427 return FAIL;
1428 }
1429 }
1430
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001431 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001432 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001433 end_visual_mode();
1434
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 /*
1436 * If deleting the last (listed) buffer, make it empty.
1437 * The last (listed) buffer cannot be unloaded.
1438 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001439 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if (bp->b_p_bl && bp != buf)
1441 break;
1442 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001443 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 /*
1446 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001447 * (unless it's the only window). Repeat this so long as we end up in
1448 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001450 while (buf == curbuf
Bram Moolenaar4033c552017-09-16 20:54:51 +02001451#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001452 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar4033c552017-09-16 20:54:51 +02001453#endif
Bram Moolenaar459ca562016-11-10 18:16:33 +01001454 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001455 {
1456 if (win_close(curwin, FALSE) == FAIL)
1457 break;
1458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
1460 /*
1461 * If the buffer to be deleted is not the current one, delete it here.
1462 */
1463 if (buf != curbuf)
1464 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001465 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001466 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001467 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 return OK;
1469 }
1470
1471 /*
1472 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001473 * There should be another, otherwise it would have been handled
1474 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001475 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 * Then prefer the buffer we most recently visited.
1477 * Else try to find one that is loaded, after the current buffer,
1478 * then before the current buffer.
1479 * Finally use any buffer.
1480 */
1481 buf = NULL; /* selected buffer */
1482 bp = NULL; /* used when no loaded buffer found */
1483#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001484 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1485 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486# ifdef FEAT_JUMPLIST
1487 else
1488# endif
1489#endif
1490#ifdef FEAT_JUMPLIST
1491 if (curwin->w_jumplistlen > 0)
1492 {
1493 int jumpidx;
1494
1495 jumpidx = curwin->w_jumplistidx - 1;
1496 if (jumpidx < 0)
1497 jumpidx = curwin->w_jumplistlen - 1;
1498
1499 forward = jumpidx;
1500 while (jumpidx != curwin->w_jumplistidx)
1501 {
1502 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1503 if (buf != NULL)
1504 {
1505 if (buf == curbuf || !buf->b_p_bl)
1506 buf = NULL; /* skip current and unlisted bufs */
1507 else if (buf->b_ml.ml_mfp == NULL)
1508 {
1509 /* skip unloaded buf, but may keep it for later */
1510 if (bp == NULL)
1511 bp = buf;
1512 buf = NULL;
1513 }
1514 }
1515 if (buf != NULL) /* found a valid buffer: stop searching */
1516 break;
1517 /* advance to older entry in jump list */
1518 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1519 break;
1520 if (--jumpidx < 0)
1521 jumpidx = curwin->w_jumplistlen - 1;
1522 if (jumpidx == forward) /* List exhausted for sure */
1523 break;
1524 }
1525 }
1526#endif
1527
1528 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1529 {
1530 forward = TRUE;
1531 buf = curbuf->b_next;
1532 for (;;)
1533 {
1534 if (buf == NULL)
1535 {
1536 if (!forward) /* tried both directions */
1537 break;
1538 buf = curbuf->b_prev;
1539 forward = FALSE;
1540 continue;
1541 }
1542 /* in non-help buffer, try to skip help buffers, and vv */
1543 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1544 {
1545 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1546 break;
1547 if (bp == NULL) /* remember unloaded buf for later */
1548 bp = buf;
1549 }
1550 if (forward)
1551 buf = buf->b_next;
1552 else
1553 buf = buf->b_prev;
1554 }
1555 }
1556 if (buf == NULL) /* No loaded buffer, use unloaded one */
1557 buf = bp;
1558 if (buf == NULL) /* No loaded buffer, find listed one */
1559 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001560 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (buf->b_p_bl && buf != curbuf)
1562 break;
1563 }
1564 if (buf == NULL) /* Still no buffer, just take one */
1565 {
1566 if (curbuf->b_next != NULL)
1567 buf = curbuf->b_next;
1568 else
1569 buf = curbuf->b_prev;
1570 }
1571 }
1572
Bram Moolenaara02471e2014-01-10 16:43:14 +01001573 if (buf == NULL)
1574 {
1575 /* Autocommands must have wiped out all other buffers. Only option
1576 * now is to make the current buffer empty. */
1577 return empty_curbuf(FALSE, forceit, action);
1578 }
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 /*
1581 * make buf current buffer
1582 */
1583 if (action == DOBUF_SPLIT) /* split window first */
1584 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001585 /* If 'switchbuf' contains "useopen": jump to first window containing
1586 * "buf" if one exists */
1587 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001588 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001589 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001590 * page containing "buf" if one exists */
1591 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 return OK;
1593 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 return FAIL;
1595 }
1596#endif
1597
1598 /* go to current buffer - nothing to do */
1599 if (buf == curbuf)
1600 return OK;
1601
1602 /*
1603 * Check if the current buffer may be abandoned.
1604 */
1605 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1606 {
1607#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1608 if ((p_confirm || cmdmod.confirm) && p_write)
1609 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001610# ifdef FEAT_AUTOCMD
1611 bufref_T bufref;
1612
1613 set_bufref(&bufref, buf);
1614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 dialog_changed(curbuf, FALSE);
1616# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001617 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 /* Autocommand deleted buffer, oops! */
1619 return FAIL;
1620# endif
1621 }
1622 if (bufIsChanged(curbuf))
1623#endif
1624 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001625 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 return FAIL;
1627 }
1628 }
1629
1630 /* Go to the other buffer. */
1631 set_curbuf(buf, action);
1632
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001633#if defined(FEAT_LISTCMDS) \
1634 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001636 {
1637 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639#endif
1640
1641#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1642 if (aborting()) /* autocmds may abort script processing */
1643 return FAIL;
1644#endif
1645
1646 return OK;
1647}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650/*
1651 * Set current buffer to "buf". Executes autocommands and closes current
1652 * buffer. "action" tells how to close the current buffer:
1653 * DOBUF_GOTO free or hide it
1654 * DOBUF_SPLIT nothing
1655 * DOBUF_UNLOAD unload it
1656 * DOBUF_DEL delete it
1657 * DOBUF_WIPE wipe it out
1658 */
1659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001660set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
1662 buf_T *prevbuf;
1663 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1664 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001665#ifdef FEAT_SYN_HL
1666 long old_tw = curbuf->b_p_tw;
1667#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001668 bufref_T newbufref;
1669 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001672 if (!cmdmod.keepalt)
1673 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001674 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 /* Don't restart Select mode after switching to another buffer. */
1677 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001679 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001682 set_bufref(&prevbufref, prevbuf);
1683 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
1685#ifdef FEAT_AUTOCMD
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001686 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1687 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001688 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001689 || (bufref_valid(&prevbufref)
1690 && bufref_valid(&newbufref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691# ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001692 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693# endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001694 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#endif
1696 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001697#ifdef FEAT_SYN_HL
1698 if (prevbuf == curwin->w_buffer)
1699 reset_synblock(curwin);
1700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001702 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001704 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001706 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001708 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001709 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001710 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001711 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1713 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001714 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001715 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001716 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001717 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001718 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
1721#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001722 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001723 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001724 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1725 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001726# ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001727 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001728# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001729 ) || curwin->w_buffer == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001731 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001733#ifdef FEAT_SYN_HL
1734 if (old_tw != curbuf->b_p_tw)
1735 check_colorcolumn(curwin);
1736#endif
1737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738}
1739
1740/*
1741 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001742 * Old curbuf must have been abandoned already! This also means "curbuf" may
1743 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 */
1745 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001746enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 /* Copy buffer and window local option values. Not for a help buffer. */
1749 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1750 if (!buf->b_help)
1751 get_winopts(buf);
1752#ifdef FEAT_FOLDING
1753 else
1754 /* Remove all folds in the window. */
1755 clearFolding(curwin);
1756 foldUpdateAll(curwin); /* update folds (later). */
1757#endif
1758
1759 /* Get the buffer in the current window. */
1760 curwin->w_buffer = buf;
1761 curbuf = buf;
1762 ++curbuf->b_nwindows;
1763
1764#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001765 if (curwin->w_p_diff)
1766 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768
Bram Moolenaara971b822011-09-14 14:43:25 +02001769#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001770 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001771#endif
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 /* Cursor on first line by default. */
1774 curwin->w_cursor.lnum = 1;
1775 curwin->w_cursor.col = 0;
1776#ifdef FEAT_VIRTUALEDIT
1777 curwin->w_cursor.coladd = 0;
1778#endif
1779 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001780#ifdef FEAT_AUTOCMD
1781 curwin->w_topline_was_set = FALSE;
1782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001784 /* mark cursor position as being invalid */
1785 curwin->w_valid = 0;
1786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 /* Make sure the buffer is loaded. */
1788 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001789 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001790#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001791 /* If there is no filetype, allow for detecting one. Esp. useful for
1792 * ":ball" used in a autocommand. If there already is a filetype we
1793 * might prefer to keep it. */
1794 if (*curbuf->b_p_ft == NUL)
1795 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001796#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001797
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001798 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 else
1801 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001802 if (!msg_silent)
1803 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1805#ifdef FEAT_AUTOCMD
1806 curwin->w_topline = 1;
1807# ifdef FEAT_DIFF
1808 curwin->w_topfill = 0;
1809# endif
1810 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1811 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1812#endif
1813 }
1814
1815 /* If autocommands did not change the cursor position, restore cursor lnum
1816 * and possibly cursor col. */
1817 if (curwin->w_cursor.lnum == 1 && inindent(0))
1818 buflist_getfpos();
1819
1820 check_arg_idx(curwin); /* check for valid arg_idx */
1821#ifdef FEAT_TITLE
1822 maketitle();
1823#endif
1824#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001825 /* when autocmds didn't change it */
1826 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#endif
1828 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1829
Bram Moolenaar009b2592004-10-24 19:18:58 +00001830#ifdef FEAT_NETBEANS_INTG
1831 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001832 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001833#endif
1834
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001835 /* Change directories when the 'acd' option is set. */
1836 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838#ifdef FEAT_KEYMAP
1839 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001840 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001842#ifdef FEAT_SPELL
1843 /* May need to set the spell language. Can only do this after the buffer
1844 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001845 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1846 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001847#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001848#ifdef FEAT_VIMINFO
1849 curbuf->b_last_used = vim_time();
1850#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 redraw_later(NOT_VALID);
1853}
1854
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001855#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1856/*
1857 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001858 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001859 */
1860 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001861do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001862{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001863 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001864 && curbuf->b_ffname != NULL
1865 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001866 shorten_fnames(TRUE);
1867}
1868#endif
1869
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001870 void
1871no_write_message(void)
1872{
1873#ifdef FEAT_TERMINAL
1874 if (term_job_running(curbuf->b_term))
1875 EMSG(_("E948: Job still running (add ! to end the job)"));
1876 else
1877#endif
1878 EMSG(_("E37: No write since last change (add ! to override)"));
1879}
1880
1881 void
1882no_write_message_nobang(void)
1883{
1884#ifdef FEAT_TERMINAL
1885 if (term_job_running(curbuf->b_term))
1886 EMSG(_("E948: Job still running"));
1887 else
1888#endif
1889 EMSG(_("E37: No write since last change"));
1890}
1891
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892/*
1893 * functions for dealing with the buffer list
1894 */
1895
Bram Moolenaar480778b2016-07-14 22:09:39 +02001896static int top_file_num = 1; /* highest file number */
1897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898/*
1899 * Add a file name to the buffer list. Return a pointer to the buffer.
1900 * If the same file name already exists return a pointer to that buffer.
1901 * If it does not exist, or if fname == NULL, a new entry is created.
1902 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1903 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1904 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001905 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001906 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1907 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 * This is the ONLY way to create a new buffer.
1909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001911buflist_new(
1912 char_u *ffname, /* full path of fname or relative */
1913 char_u *sfname, /* short fname or NULL */
1914 linenr_T lnum, /* preferred cursor line */
1915 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 buf_T *buf;
1918#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001919 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920#endif
1921
Bram Moolenaar480778b2016-07-14 22:09:39 +02001922 if (top_file_num == 1)
1923 hash_init(&buf_hashtab);
1924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1926
1927 /*
1928 * If file name already exists in the list, update the entry.
1929 */
1930#ifdef UNIX
1931 /* On Unix we can use inode numbers when the file exists. Works better
1932 * for hard links. */
1933 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1934 st.st_dev = (dev_T)-1;
1935#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001936 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937#ifdef UNIX
1938 buflist_findname_stat(ffname, &st)
1939#else
1940 buflist_findname(ffname)
1941#endif
1942 ) != NULL)
1943 {
1944 vim_free(ffname);
1945 if (lnum != 0)
1946 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001947
1948 if ((flags & BLN_NOOPT) == 0)
1949 /* copy the options now, if 'cpo' doesn't have 's' and not done
1950 * already */
1951 buf_copy_options(buf, 0);
1952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1954 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001955#ifdef FEAT_AUTOCMD
1956 bufref_T bufref;
1957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 buf->b_p_bl = TRUE;
1959#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001960 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001962 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001963 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001964 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001965 return NULL;
1966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#endif
1968 }
1969 return buf;
1970 }
1971
1972 /*
1973 * If the current buffer has no name and no contents, use the current
1974 * buffer. Otherwise: Need to allocate a new buffer structure.
1975 *
1976 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001977 * (A spell file buffer is allocated in spell.c, but that's not a normal
1978 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 */
1980 buf = NULL;
1981 if ((flags & BLN_CURBUF)
1982 && curbuf != NULL
1983 && curbuf->b_ffname == NULL
1984 && curbuf->b_nwindows <= 1
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001985 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 buf = curbuf;
1988#ifdef FEAT_AUTOCMD
1989 /* It's like this buffer is deleted. Watch out for autocommands that
1990 * change curbuf! If that happens, allocate a new buffer anyway. */
1991 if (curbuf->b_p_bl)
1992 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1993 if (buf == curbuf)
1994 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1995# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001996 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 return NULL;
1998# endif
1999#endif
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02002000#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (buf == curbuf)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02002002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
2004 /* Make sure 'bufhidden' and 'buftype' are empty */
2005 clear_string_option(&buf->b_p_bh);
2006 clear_string_option(&buf->b_p_bt);
2007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009 if (buf != curbuf || curbuf == NULL)
2010 {
2011 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
2012 if (buf == NULL)
2013 {
2014 vim_free(ffname);
2015 return NULL;
2016 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002017#ifdef FEAT_EVAL
2018 /* init b: variables */
2019 buf->b_vars = dict_alloc();
2020 if (buf->b_vars == NULL)
2021 {
2022 vim_free(ffname);
2023 vim_free(buf);
2024 return NULL;
2025 }
2026 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2027#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002028 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030
2031 if (ffname != NULL)
2032 {
2033 buf->b_ffname = ffname;
2034 buf->b_sfname = vim_strsave(sfname);
2035 }
2036
2037 clear_wininfo(buf);
2038 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2039
2040 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2041 || buf->b_wininfo == NULL)
2042 {
2043 vim_free(buf->b_ffname);
2044 buf->b_ffname = NULL;
2045 vim_free(buf->b_sfname);
2046 buf->b_sfname = NULL;
2047 if (buf != curbuf)
2048 free_buffer(buf);
2049 return NULL;
2050 }
2051
2052 if (buf == curbuf)
2053 {
2054 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002055 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (buf != curbuf) /* autocommands deleted the buffer! */
2057 return NULL;
2058#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002059 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 return NULL;
2061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002063
2064 /* Init the options. */
2065 buf->b_p_initialized = FALSE;
2066 buf_copy_options(buf, BCO_ENTER);
2067
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068#ifdef FEAT_KEYMAP
2069 /* need to reload lmaps and set b:keymap_name */
2070 curbuf->b_kmap_state |= KEYMAP_INIT;
2071#endif
2072 }
2073 else
2074 {
2075 /*
2076 * put new buffer at the end of the buffer list
2077 */
2078 buf->b_next = NULL;
2079 if (firstbuf == NULL) /* buffer list is empty */
2080 {
2081 buf->b_prev = NULL;
2082 firstbuf = buf;
2083 }
2084 else /* append new buffer at end of list */
2085 {
2086 lastbuf->b_next = buf;
2087 buf->b_prev = lastbuf;
2088 }
2089 lastbuf = buf;
2090
2091 buf->b_fnum = top_file_num++;
2092 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2093 {
2094 EMSG(_("W14: Warning: List of file names overflow"));
2095 if (emsg_silent == 0)
2096 {
2097 out_flush();
2098 ui_delay(3000L, TRUE); /* make sure it is noticed */
2099 }
2100 top_file_num = 1;
2101 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002102 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
2104 /*
2105 * Always copy the options from the current buffer.
2106 */
2107 buf_copy_options(buf, BCO_ALWAYS);
2108 }
2109
2110 buf->b_wininfo->wi_fpos.lnum = lnum;
2111 buf->b_wininfo->wi_win = curwin;
2112
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002113#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002114 hash_init(&buf->b_s.b_keywtab);
2115 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#endif
2117
2118 buf->b_fname = buf->b_sfname;
2119#ifdef UNIX
2120 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002121 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 else
2123 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002124 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 buf->b_dev = st.st_dev;
2126 buf->b_ino = st.st_ino;
2127 }
2128#endif
2129 buf->b_u_synced = TRUE;
2130 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002131 if (flags & BLN_DUMMY)
2132 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 buf_clear_file(buf);
2134 clrallmarks(buf); /* clear marks */
2135 fmarks_check_names(buf); /* check file marks for this file */
2136 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2137#ifdef FEAT_AUTOCMD
2138 if (!(flags & BLN_DUMMY))
2139 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002140 bufref_T bufref;
2141
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002142 /* Tricky: these autocommands may change the buffer list. They could
2143 * also split the window with re-using the one empty buffer. This may
2144 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002145 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002146 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002147 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002148 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002150 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002151 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002152 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002153 return NULL;
2154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002156 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 return NULL;
2158# endif
2159 }
2160#endif
2161
2162 return buf;
2163}
2164
2165/*
2166 * Free the memory for the options of a buffer.
2167 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2168 * 'fileencoding'.
2169 */
2170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171free_buf_options(
2172 buf_T *buf,
2173 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
2175 if (free_p_ff)
2176 {
2177#ifdef FEAT_MBYTE
2178 clear_string_option(&buf->b_p_fenc);
2179#endif
2180 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 clear_string_option(&buf->b_p_bh);
2182 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 }
2184#ifdef FEAT_FIND_ID
2185 clear_string_option(&buf->b_p_def);
2186 clear_string_option(&buf->b_p_inc);
2187# ifdef FEAT_EVAL
2188 clear_string_option(&buf->b_p_inex);
2189# endif
2190#endif
2191#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2192 clear_string_option(&buf->b_p_inde);
2193 clear_string_option(&buf->b_p_indk);
2194#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002195#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2196 clear_string_option(&buf->b_p_bexpr);
2197#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002198#if defined(FEAT_CRYPT)
2199 clear_string_option(&buf->b_p_cm);
2200#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002201 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002202#if defined(FEAT_EVAL)
2203 clear_string_option(&buf->b_p_fex);
2204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205#ifdef FEAT_CRYPT
2206 clear_string_option(&buf->b_p_key);
2207#endif
2208 clear_string_option(&buf->b_p_kp);
2209 clear_string_option(&buf->b_p_mps);
2210 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002211 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 clear_string_option(&buf->b_p_isk);
2213#ifdef FEAT_KEYMAP
2214 clear_string_option(&buf->b_p_keymap);
2215 ga_clear(&buf->b_kmap_ga);
2216#endif
2217#ifdef FEAT_COMMENTS
2218 clear_string_option(&buf->b_p_com);
2219#endif
2220#ifdef FEAT_FOLDING
2221 clear_string_option(&buf->b_p_cms);
2222#endif
2223 clear_string_option(&buf->b_p_nf);
2224#ifdef FEAT_SYN_HL
2225 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002226 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002227#endif
2228#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002229 clear_string_option(&buf->b_s.b_p_spc);
2230 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002231 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002232 buf->b_s.b_cap_prog = NULL;
2233 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#endif
2235#ifdef FEAT_SEARCHPATH
2236 clear_string_option(&buf->b_p_sua);
2237#endif
2238#ifdef FEAT_AUTOCMD
2239 clear_string_option(&buf->b_p_ft);
2240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#ifdef FEAT_CINDENT
2242 clear_string_option(&buf->b_p_cink);
2243 clear_string_option(&buf->b_p_cino);
2244#endif
2245#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2246 clear_string_option(&buf->b_p_cinw);
2247#endif
2248#ifdef FEAT_INS_EXPAND
2249 clear_string_option(&buf->b_p_cpt);
2250#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002251#ifdef FEAT_COMPL_FUNC
2252 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002253 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255#ifdef FEAT_QUICKFIX
2256 clear_string_option(&buf->b_p_gp);
2257 clear_string_option(&buf->b_p_mp);
2258 clear_string_option(&buf->b_p_efm);
2259#endif
2260 clear_string_option(&buf->b_p_ep);
2261 clear_string_option(&buf->b_p_path);
2262 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002263 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#ifdef FEAT_INS_EXPAND
2265 clear_string_option(&buf->b_p_dict);
2266 clear_string_option(&buf->b_p_tsr);
2267#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002268#ifdef FEAT_TEXTOBJ
2269 clear_string_option(&buf->b_p_qe);
2270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002272 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002273#ifdef FEAT_LISP
2274 clear_string_option(&buf->b_p_lw);
2275#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002276 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002277#ifdef FEAT_MBYTE
2278 clear_string_option(&buf->b_p_menc);
2279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280}
2281
2282/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002283 * Get alternate file "n".
2284 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2285 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 * if (options & GETF_SETMARK) call setpcmark()
2287 * if (options & GETF_ALT) we are jumping to an alternate file.
2288 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2289 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002290 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 */
2292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002293buflist_getfile(
2294 int n,
2295 linenr_T lnum,
2296 int options,
2297 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 pos_T *fpos;
2302 colnr_T col;
2303
2304 buf = buflist_findnr(n);
2305 if (buf == NULL)
2306 {
2307 if ((options & GETF_ALT) && n == 0)
2308 EMSG(_(e_noalt));
2309 else
2310 EMSGN(_("E92: Buffer %ld not found"), n);
2311 return FAIL;
2312 }
2313
2314 /* if alternate file is the current buffer, nothing to do */
2315 if (buf == curbuf)
2316 return OK;
2317
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002318 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002319 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002320 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002322 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002323#ifdef FEAT_AUTOCMD
2324 if (curbuf_locked())
2325 return FAIL;
2326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 /* altfpos may be changed by getfile(), get it now */
2329 if (lnum == 0)
2330 {
2331 fpos = buflist_findfpos(buf);
2332 lnum = fpos->lnum;
2333 col = fpos->col;
2334 }
2335 else
2336 col = 0;
2337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if (options & GETF_SWITCH)
2339 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002340 /* If 'switchbuf' contains "useopen": jump to first window containing
2341 * "buf" if one exists */
2342 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002344
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002345 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002346 * page containing "buf" if one exists */
2347 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002348 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002349
2350 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2351 * current buffer isn't empty: open new tab or window */
2352 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002353 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002355 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002356 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002357 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2358 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002360 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
2362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002365 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2366 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
2368 --RedrawingDisabled;
2369
2370 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2371 if (!p_sol && col != 0)
2372 {
2373 curwin->w_cursor.col = col;
2374 check_cursor_col();
2375#ifdef FEAT_VIRTUALEDIT
2376 curwin->w_cursor.coladd = 0;
2377#endif
2378 curwin->w_set_curswant = TRUE;
2379 }
2380 return OK;
2381 }
2382 --RedrawingDisabled;
2383 return FAIL;
2384}
2385
2386/*
2387 * go to the last know line number for the current buffer
2388 */
2389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002390buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
2392 pos_T *fpos;
2393
2394 fpos = buflist_findfpos(curbuf);
2395
2396 curwin->w_cursor.lnum = fpos->lnum;
2397 check_cursor_lnum();
2398
2399 if (p_sol)
2400 curwin->w_cursor.col = 0;
2401 else
2402 {
2403 curwin->w_cursor.col = fpos->col;
2404 check_cursor_col();
2405#ifdef FEAT_VIRTUALEDIT
2406 curwin->w_cursor.coladd = 0;
2407#endif
2408 curwin->w_set_curswant = TRUE;
2409 }
2410}
2411
Bram Moolenaar81695252004-12-29 20:58:21 +00002412#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2413/*
2414 * Find file in buffer list by name (it has to be for the current window).
2415 * Returns NULL if not found.
2416 */
2417 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002418buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002419{
2420 char_u *ffname;
2421 buf_T *buf = NULL;
2422
2423 /* First make the name into a full path name */
2424 ffname = FullName_save(fname,
2425#ifdef UNIX
2426 TRUE /* force expansion, get rid of symbolic links */
2427#else
2428 FALSE
2429#endif
2430 );
2431 if (ffname != NULL)
2432 {
2433 buf = buflist_findname(ffname);
2434 vim_free(ffname);
2435 }
2436 return buf;
2437}
2438#endif
2439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440/*
2441 * Find file in buffer list by name (it has to be for the current window).
2442 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002443 * Skips dummy buffers.
2444 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 */
2446 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002447buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
2449#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002450 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452 if (mch_stat((char *)ffname, &st) < 0)
2453 st.st_dev = (dev_T)-1;
2454 return buflist_findname_stat(ffname, &st);
2455}
2456
2457/*
2458 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2459 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002460 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 */
2462 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002463buflist_findname_stat(
2464 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002465 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467#endif
2468 buf_T *buf;
2469
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002470 /* Start at the last buffer, expect to find a match sooner. */
2471 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002472 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473#ifdef UNIX
2474 , stp
2475#endif
2476 ))
2477 return buf;
2478 return NULL;
2479}
2480
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002481#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2482 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483/*
2484 * Find file in buffer list by a regexp pattern.
2485 * Return fnum of the found buffer.
2486 * Return < 0 for error.
2487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002489buflist_findpat(
2490 char_u *pattern,
2491 char_u *pattern_end, /* pointer to first char after pattern */
2492 int unlisted, /* find unlisted buffers */
2493 int diffmode UNUSED, /* find diff-mode buffers only */
2494 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
2496 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 int match = -1;
2498 int find_listed;
2499 char_u *pat;
2500 char_u *patend;
2501 int attempt;
2502 char_u *p;
2503 int toggledollar;
2504
2505 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2506 {
2507 if (*pattern == '%')
2508 match = curbuf->b_fnum;
2509 else
2510 match = curwin->w_alt_fnum;
2511#ifdef FEAT_DIFF
2512 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2513 match = -1;
2514#endif
2515 }
2516
2517 /*
2518 * Try four ways of matching a listed buffer:
2519 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002520 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 * attempt == 2: with '$' at end (only match at end)
2522 * attempt == 3: with '^' at start and '$' at end (only full match)
2523 * Repeat this for finding an unlisted buffer if there was no matching
2524 * listed buffer.
2525 */
2526 else
2527 {
2528 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2529 if (pat == NULL)
2530 return -1;
2531 patend = pat + STRLEN(pat) - 1;
2532 toggledollar = (patend > pat && *patend == '$');
2533
2534 /* First try finding a listed buffer. If not found and "unlisted"
2535 * is TRUE, try finding an unlisted buffer. */
2536 find_listed = TRUE;
2537 for (;;)
2538 {
2539 for (attempt = 0; attempt <= 3; ++attempt)
2540 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002541 regmatch_T regmatch;
2542
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /* may add '^' and '$' */
2544 if (toggledollar)
2545 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2546 p = pat;
2547 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2548 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002549 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2550 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 vim_free(pat);
2553 return -1;
2554 }
2555
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002556 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (buf->b_p_bl == find_listed
2558#ifdef FEAT_DIFF
2559 && (!diffmode || diff_mode_buf(buf))
2560#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002561 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002563 if (curtab_only)
2564 {
2565 /* Ignore the match if the buffer is not open in
2566 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002567 win_T *wp;
2568
Bram Moolenaar29323592016-07-24 22:04:11 +02002569 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002570 if (wp->w_buffer == buf)
2571 break;
2572 if (wp == NULL)
2573 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (match >= 0) /* already found a match */
2576 {
2577 match = -2;
2578 break;
2579 }
2580 match = buf->b_fnum; /* remember first match */
2581 }
2582
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002583 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (match >= 0) /* found one match */
2585 break;
2586 }
2587
2588 /* Only search for unlisted buffers if there was no match with
2589 * a listed buffer. */
2590 if (!unlisted || !find_listed || match != -1)
2591 break;
2592 find_listed = FALSE;
2593 }
2594
2595 vim_free(pat);
2596 }
2597
2598 if (match == -2)
2599 EMSG2(_("E93: More than one match for %s"), pattern);
2600 else if (match < 0)
2601 EMSG2(_("E94: No matching buffer for %s"), pattern);
2602 return match;
2603}
2604#endif
2605
2606#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2607
2608/*
2609 * Find all buffer names that match.
2610 * For command line expansion of ":buf" and ":sbuf".
2611 * Return OK if matches found, FAIL otherwise.
2612 */
2613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002614ExpandBufnames(
2615 char_u *pat,
2616 int *num_file,
2617 char_u ***file,
2618 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
2620 int count = 0;
2621 buf_T *buf;
2622 int round;
2623 char_u *p;
2624 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002625 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 *num_file = 0; /* return values in case of FAIL */
2628 *file = NULL;
2629
Bram Moolenaar05159a02005-02-26 23:04:13 +00002630 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2631 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002633 patc = alloc((unsigned)STRLEN(pat) + 11);
2634 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002636 STRCPY(patc, "\\(^\\|[\\/]\\)");
2637 STRCPY(patc + 11, pat + 1);
2638 }
2639 else
2640 patc = pat;
2641
2642 /*
2643 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002644 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002645 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002646 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002647 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002648 regmatch_T regmatch;
2649
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002650 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002651 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002652 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2653 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002654 {
2655 if (patc != pat)
2656 vim_free(patc);
2657 return FAIL;
2658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 /*
2661 * round == 1: Count the matches.
2662 * round == 2: Build the array to keep the matches.
2663 */
2664 for (round = 1; round <= 2; ++round)
2665 {
2666 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002667 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
2669 if (!buf->b_p_bl) /* skip unlisted buffers */
2670 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002671 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (p != NULL)
2673 {
2674 if (round == 1)
2675 ++count;
2676 else
2677 {
2678 if (options & WILD_HOME_REPLACE)
2679 p = home_replace_save(buf, p);
2680 else
2681 p = vim_strsave(p);
2682 (*file)[count++] = p;
2683 }
2684 }
2685 }
2686 if (count == 0) /* no match found, break here */
2687 break;
2688 if (round == 1)
2689 {
2690 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2691 if (*file == NULL)
2692 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002693 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002694 if (patc != pat)
2695 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 return FAIL;
2697 }
2698 }
2699 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002700 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (count) /* match(es) found, break here */
2702 break;
2703 }
2704
Bram Moolenaar05159a02005-02-26 23:04:13 +00002705 if (patc != pat)
2706 vim_free(patc);
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 *num_file = count;
2709 return (count == 0 ? FAIL : OK);
2710}
2711
2712#endif /* FEAT_CMDL_COMPL */
2713
2714#ifdef HAVE_BUFLIST_MATCH
2715/*
2716 * Check for a match on the file name for buffer "buf" with regprog "prog".
2717 */
2718 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002719buflist_match(
2720 regmatch_T *rmp,
2721 buf_T *buf,
2722 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 char_u *match;
2725
2726 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002727 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002729 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 return match;
2732}
2733
2734/*
2735 * Try matching the regexp in "prog" with file name "name".
2736 * Return "name" when there is a match, NULL when not.
2737 */
2738 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002739fname_match(
2740 regmatch_T *rmp,
2741 char_u *name,
2742 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 char_u *match = NULL;
2745 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 if (name != NULL)
2748 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002749 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002750 rmp->rm_ic = p_fic || ignore_case;
2751 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 match = name;
2753 else
2754 {
2755 /* Replace $(HOME) with '~' and try matching again. */
2756 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002757 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 match = name;
2759 vim_free(p);
2760 }
2761 }
2762
2763 return match;
2764}
2765#endif
2766
2767/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002768 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 */
2770 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002771buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002773 char_u key[VIM_SIZEOF_INT * 2 + 1];
2774 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776 if (nr == 0)
2777 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002778 sprintf((char *)key, "%x", nr);
2779 hi = hash_find(&buf_hashtab, key);
2780
2781 if (!HASHITEM_EMPTY(hi))
2782 return (buf_T *)(hi->hi_key
2783 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 return NULL;
2785}
2786
2787/*
2788 * Get name of file 'n' in the buffer list.
2789 * When the file has no name an empty string is returned.
2790 * home_replace() is used to shorten the file name (used for marks).
2791 * Returns a pointer to allocated memory, of NULL when failed.
2792 */
2793 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794buflist_nr2name(
2795 int n,
2796 int fullname,
2797 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 buf_T *buf;
2800
2801 buf = buflist_findnr(n);
2802 if (buf == NULL)
2803 return NULL;
2804 return home_replace_save(helptail ? buf : NULL,
2805 fullname ? buf->b_ffname : buf->b_fname);
2806}
2807
2808/*
2809 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2810 * When "copy_options" is TRUE save the local window option values.
2811 * When "lnum" is 0 only do the options.
2812 */
2813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002814buflist_setfpos(
2815 buf_T *buf,
2816 win_T *win,
2817 linenr_T lnum,
2818 colnr_T col,
2819 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
2821 wininfo_T *wip;
2822
2823 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2824 if (wip->wi_win == win)
2825 break;
2826 if (wip == NULL)
2827 {
2828 /* allocate a new entry */
2829 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2830 if (wip == NULL)
2831 return;
2832 wip->wi_win = win;
2833 if (lnum == 0) /* set lnum even when it's 0 */
2834 lnum = 1;
2835 }
2836 else
2837 {
2838 /* remove the entry from the list */
2839 if (wip->wi_prev)
2840 wip->wi_prev->wi_next = wip->wi_next;
2841 else
2842 buf->b_wininfo = wip->wi_next;
2843 if (wip->wi_next)
2844 wip->wi_next->wi_prev = wip->wi_prev;
2845 if (copy_options && wip->wi_optset)
2846 {
2847 clear_winopt(&wip->wi_opt);
2848#ifdef FEAT_FOLDING
2849 deleteFoldRecurse(&wip->wi_folds);
2850#endif
2851 }
2852 }
2853 if (lnum != 0)
2854 {
2855 wip->wi_fpos.lnum = lnum;
2856 wip->wi_fpos.col = col;
2857 }
2858 if (copy_options)
2859 {
2860 /* Save the window-specific option values. */
2861 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2862#ifdef FEAT_FOLDING
2863 wip->wi_fold_manual = win->w_fold_manual;
2864 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2865#endif
2866 wip->wi_optset = TRUE;
2867 }
2868
2869 /* insert the entry in front of the list */
2870 wip->wi_next = buf->b_wininfo;
2871 buf->b_wininfo = wip;
2872 wip->wi_prev = NULL;
2873 if (wip->wi_next)
2874 wip->wi_next->wi_prev = wip;
2875
2876 return;
2877}
2878
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002879#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002880static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002881
2882/*
2883 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2884 * page. That's because a diff is local to a tab page.
2885 */
2886 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002887wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002888{
2889 win_T *wp;
2890
2891 if (wip->wi_opt.wo_diff)
2892 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002893 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002894 /* return FALSE when it's a window in the current tab page, thus
2895 * the buffer was in diff mode here */
2896 if (wip->wi_win == wp)
2897 return FALSE;
2898 return TRUE;
2899 }
2900 return FALSE;
2901}
2902#endif
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904/*
2905 * Find info for the current window in buffer "buf".
2906 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002907 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2908 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 * Returns NULL when there isn't any info.
2910 */
2911 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002912find_wininfo(
2913 buf_T *buf,
2914 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
2916 wininfo_T *wip;
2917
2918 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002919 if (wip->wi_win == curwin
2920#ifdef FEAT_DIFF
2921 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2922#endif
2923 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002925
2926 /* If no wininfo for curwin, use the first in the list (that doesn't have
2927 * 'diff' set and is in another tab page). */
2928 if (wip == NULL)
2929 {
2930#ifdef FEAT_DIFF
2931 if (skip_diff_buffer)
2932 {
2933 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2934 if (!wininfo_other_tab_diff(wip))
2935 break;
2936 }
2937 else
2938#endif
2939 wip = buf->b_wininfo;
2940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 return wip;
2942}
2943
2944/*
2945 * Reset the local window options to the values last used in this window.
2946 * If the buffer wasn't used in this window before, use the values from
2947 * the most recently used window. If the values were never set, use the
2948 * global values for the window.
2949 */
2950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002951get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 wininfo_T *wip;
2954
2955 clear_winopt(&curwin->w_onebuf_opt);
2956#ifdef FEAT_FOLDING
2957 clearFolding(curwin);
2958#endif
2959
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002960 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (wip != NULL && wip->wi_optset)
2962 {
2963 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2964#ifdef FEAT_FOLDING
2965 curwin->w_fold_manual = wip->wi_fold_manual;
2966 curwin->w_foldinvalid = TRUE;
2967 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2968#endif
2969 }
2970 else
2971 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2972
2973#ifdef FEAT_FOLDING
2974 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2975 if (p_fdls >= 0)
2976 curwin->w_p_fdl = p_fdls;
2977#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002978#ifdef FEAT_SYN_HL
2979 check_colorcolumn(curwin);
2980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981}
2982
2983/*
2984 * Find the position (lnum and col) for the buffer 'buf' for the current
2985 * window.
2986 * Returns a pointer to no_position if no position is found.
2987 */
2988 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002989buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002992 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002994 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 if (wip != NULL)
2996 return &(wip->wi_fpos);
2997 else
2998 return &no_position;
2999}
3000
3001/*
3002 * Find the lnum for the buffer 'buf' for the current window.
3003 */
3004 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003005buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 return buflist_findfpos(buf)->lnum;
3008}
3009
3010#if defined(FEAT_LISTCMDS) || defined(PROTO)
3011/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003012 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003015buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 buf_T *buf;
3018 int len;
3019 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003020 int ro_char;
3021 int changed_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3024 {
3025 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003026 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3027 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3028 || (vim_strchr(eap->arg, '+')
3029 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3030 || (vim_strchr(eap->arg, 'a')
3031 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
3032 || (vim_strchr(eap->arg, 'h')
3033 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3034 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3035 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3036 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3037 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3038 || (vim_strchr(eap->arg, '#')
3039 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003042 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else
3044 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003045 if (message_filtered(NameBuff))
3046 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003048 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3049 : (bufIsChanged(buf) ? '+' : ' ');
3050#ifdef FEAT_TERMINAL
3051 if (term_job_running(buf->b_term))
3052 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003053 if (term_none_open(buf->b_term))
3054 ro_char = '?';
3055 else
3056 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003057 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3058 * closing, but it's not actually changed. */
3059 }
3060 else if (buf->b_term != NULL)
3061 ro_char = 'F';
3062 else
3063#endif
3064 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3065
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003066 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003067 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 buf->b_fnum,
3069 buf->b_p_bl ? ' ' : 'u',
3070 buf == curbuf ? '%' :
3071 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3072 buf->b_ml.ml_mfp == NULL ? ' ' :
3073 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003074 ro_char,
3075 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003076 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003077 if (len > IOSIZE - 20)
3078 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 i = 40 - vim_strsize(IObuff);
3082 do
3083 {
3084 IObuff[len++] = ' ';
3085 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003086 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3087 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003088 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 msg_outtrans(IObuff);
3090 out_flush(); /* output one line at a time */
3091 ui_breakcheck();
3092 }
3093}
3094#endif
3095
3096/*
3097 * Get file name and line number for file 'fnum'.
3098 * Used by DoOneCmd() for translating '%' and '#'.
3099 * Used by insert_reg() and cmdline_paste() for '#' register.
3100 * Return FAIL if not found, OK for success.
3101 */
3102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003103buflist_name_nr(
3104 int fnum,
3105 char_u **fname,
3106 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 buf_T *buf;
3109
3110 buf = buflist_findnr(fnum);
3111 if (buf == NULL || buf->b_fname == NULL)
3112 return FAIL;
3113
3114 *fname = buf->b_fname;
3115 *lnum = buflist_findlnum(buf);
3116
3117 return OK;
3118}
3119
3120/*
3121 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3122 * The file name with the full path is also remembered, for when :cd is used.
3123 * Returns FAIL for failure (file name already in use by other buffer)
3124 * OK otherwise.
3125 */
3126 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003127setfname(
3128 buf_T *buf,
3129 char_u *ffname,
3130 char_u *sfname,
3131 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
Bram Moolenaar81695252004-12-29 20:58:21 +00003133 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003135 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136#endif
3137
3138 if (ffname == NULL || *ffname == NUL)
3139 {
3140 /* Removing the name. */
3141 vim_free(buf->b_ffname);
3142 vim_free(buf->b_sfname);
3143 buf->b_ffname = NULL;
3144 buf->b_sfname = NULL;
3145#ifdef UNIX
3146 st.st_dev = (dev_T)-1;
3147#endif
3148 }
3149 else
3150 {
3151 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3152 if (ffname == NULL) /* out of memory */
3153 return FAIL;
3154
3155 /*
3156 * if the file name is already used in another buffer:
3157 * - if the buffer is loaded, fail
3158 * - if the buffer is not loaded, delete it from the list
3159 */
3160#ifdef UNIX
3161 if (mch_stat((char *)ffname, &st) < 0)
3162 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003163#endif
3164 if (!(buf->b_flags & BF_DUMMY))
3165#ifdef UNIX
3166 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003168 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#endif
3170 if (obuf != NULL && obuf != buf)
3171 {
3172 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3173 {
3174 if (message)
3175 EMSG(_("E95: Buffer with this name already exists"));
3176 vim_free(ffname);
3177 return FAIL;
3178 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003179 /* delete from the list */
3180 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 }
3182 sfname = vim_strsave(sfname);
3183 if (ffname == NULL || sfname == NULL)
3184 {
3185 vim_free(sfname);
3186 vim_free(ffname);
3187 return FAIL;
3188 }
3189#ifdef USE_FNAME_CASE
3190# ifdef USE_LONG_FNAME
3191 if (USE_LONG_FNAME)
3192# endif
3193 fname_case(sfname, 0); /* set correct case for short file name */
3194#endif
3195 vim_free(buf->b_ffname);
3196 vim_free(buf->b_sfname);
3197 buf->b_ffname = ffname;
3198 buf->b_sfname = sfname;
3199 }
3200 buf->b_fname = buf->b_sfname;
3201#ifdef UNIX
3202 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003203 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 else
3205 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003206 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 buf->b_dev = st.st_dev;
3208 buf->b_ino = st.st_ino;
3209 }
3210#endif
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214 buf_name_changed(buf);
3215 return OK;
3216}
3217
3218/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003219 * Crude way of changing the name of a buffer. Use with care!
3220 * The name should be relative to the current directory.
3221 */
3222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003223buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003224{
3225 buf_T *buf;
3226
3227 buf = buflist_findnr(fnum);
3228 if (buf != NULL)
3229 {
3230 vim_free(buf->b_sfname);
3231 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003232 buf->b_ffname = vim_strsave(name);
3233 buf->b_sfname = NULL;
3234 /* Allocate ffname and expand into full path. Also resolves .lnk
3235 * files on Win32. */
3236 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003237 buf->b_fname = buf->b_sfname;
3238 }
3239}
3240
3241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 * Take care of what needs to be done when the name of buffer "buf" has
3243 * changed.
3244 */
3245 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003246buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 /*
3249 * If the file name changed, also change the name of the swapfile
3250 */
3251 if (buf->b_ml.ml_mfp != NULL)
3252 ml_setname(buf);
3253
3254 if (curwin->w_buffer == buf)
3255 check_arg_idx(curwin); /* check file name for arg list */
3256#ifdef FEAT_TITLE
3257 maketitle(); /* set window title */
3258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 fmarks_check_names(buf); /* check named file marks */
3261 ml_timestamp(buf); /* reset timestamp */
3262}
3263
3264/*
3265 * set alternate file name for current window
3266 *
3267 * Used by do_one_cmd(), do_write() and do_ecmd().
3268 * Return the buffer.
3269 */
3270 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271setaltfname(
3272 char_u *ffname,
3273 char_u *sfname,
3274 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 buf_T *buf;
3277
3278 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3279 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003280 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 curwin->w_alt_fnum = buf->b_fnum;
3282 return buf;
3283}
3284
3285/*
3286 * Get alternate file name for current window.
3287 * Return NULL if there isn't any, and give error message if requested.
3288 */
3289 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003290getaltfname(
3291 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 char_u *fname;
3294 linenr_T dummy;
3295
3296 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3297 {
3298 if (errmsg)
3299 EMSG(_(e_noalt));
3300 return NULL;
3301 }
3302 return fname;
3303}
3304
3305/*
3306 * Add a file name to the buflist and return its number.
3307 * Uses same flags as buflist_new(), except BLN_DUMMY.
3308 *
3309 * used by qf_init(), main() and doarglist()
3310 */
3311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003312buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 buf_T *buf;
3315
3316 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3317 if (buf != NULL)
3318 return buf->b_fnum;
3319 return 0;
3320}
3321
3322#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3323/*
3324 * Adjust slashes in file names. Called after 'shellslash' was set.
3325 */
3326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 buf_T *bp;
3330
Bram Moolenaar29323592016-07-24 22:04:11 +02003331 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
3333 if (bp->b_ffname != NULL)
3334 slash_adjust(bp->b_ffname);
3335 if (bp->b_sfname != NULL)
3336 slash_adjust(bp->b_sfname);
3337 }
3338}
3339#endif
3340
3341/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003342 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 * Also save the local window option values.
3344 */
3345 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003346buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003348 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349}
3350
3351/*
3352 * Return TRUE if 'ffname' is not the same file as current file.
3353 * Fname must have a full path (expanded by mch_FullName()).
3354 */
3355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 return otherfile_buf(curbuf, ffname
3359#ifdef UNIX
3360 , NULL
3361#endif
3362 );
3363}
3364
3365 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003366otherfile_buf(
3367 buf_T *buf,
3368 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003370 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003372 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 /* no name is different */
3375 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3376 return TRUE;
3377 if (fnamecmp(ffname, buf->b_ffname) == 0)
3378 return FALSE;
3379#ifdef UNIX
3380 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003381 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaar8767f522016-07-01 17:17:39 +02003383 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 if (stp == NULL)
3385 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003386 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 st.st_dev = (dev_T)-1;
3388 stp = &st;
3389 }
3390 /* Use dev/ino to check if the files are the same, even when the names
3391 * are different (possible with links). Still need to compare the
3392 * name above, for when the file doesn't exist yet.
3393 * Problem: The dev/ino changes when a file is deleted (and created
3394 * again) and remains the same when renamed/moved. We don't want to
3395 * mch_stat() each buffer each time, that would be too slow. Get the
3396 * dev/ino again when they appear to match, but not when they appear
3397 * to be different: Could skip a buffer when it's actually the same
3398 * file. */
3399 if (buf_same_ino(buf, stp))
3400 {
3401 buf_setino(buf);
3402 if (buf_same_ino(buf, stp))
3403 return FALSE;
3404 }
3405 }
3406#endif
3407 return TRUE;
3408}
3409
3410#if defined(UNIX) || defined(PROTO)
3411/*
3412 * Set inode and device number for a buffer.
3413 * Must always be called when b_fname is changed!.
3414 */
3415 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003416buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003418 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3421 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003422 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 buf->b_dev = st.st_dev;
3424 buf->b_ino = st.st_ino;
3425 }
3426 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003427 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428}
3429
3430/*
3431 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3432 */
3433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003434buf_same_ino(
3435 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003436 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003438 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 && stp->st_dev == buf->b_dev
3440 && stp->st_ino == buf->b_ino);
3441}
3442#endif
3443
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003444/*
3445 * Print info about the current buffer.
3446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448fileinfo(
3449 int fullname, /* when non-zero print full path */
3450 int shorthelp,
3451 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
3453 char_u *name;
3454 int n;
3455 char_u *p;
3456 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003457 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
3459 buffer = alloc(IOSIZE);
3460 if (buffer == NULL)
3461 return;
3462
3463 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3464 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003465 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 p = buffer + STRLEN(buffer);
3467 }
3468 else
3469 p = buffer;
3470
3471 *p++ = '"';
3472 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003473 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 else
3475 {
3476 if (!fullname && curbuf->b_fname != NULL)
3477 name = curbuf->b_fname;
3478 else
3479 name = curbuf->b_ffname;
3480 home_replace(shorthelp ? curbuf : NULL, name, p,
3481 (int)(IOSIZE - (p - buffer)), TRUE);
3482 }
3483
Bram Moolenaara800b422010-06-27 01:15:55 +02003484 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 curbufIsChanged() ? (shortmess(SHM_MOD)
3486 ? " [+]" : _(" [Modified]")) : " ",
3487 (curbuf->b_flags & BF_NOTEDITED)
3488#ifdef FEAT_QUICKFIX
3489 && !bt_dontwrite(curbuf)
3490#endif
3491 ? _("[Not edited]") : "",
3492 (curbuf->b_flags & BF_NEW)
3493#ifdef FEAT_QUICKFIX
3494 && !bt_dontwrite(curbuf)
3495#endif
3496 ? _("[New file]") : "",
3497 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003498 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 : _("[readonly]")) : "",
3500 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3501 || curbuf->b_p_ro) ?
3502 " " : "");
3503 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3504 * causes an overflow, thus for large numbers divide instead. */
3505 if (curwin->w_cursor.lnum > 1000000L)
3506 n = (int)(((long)curwin->w_cursor.lnum) /
3507 ((long)curbuf->b_ml.ml_line_count / 100L));
3508 else
3509 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3510 (long)curbuf->b_ml.ml_line_count);
3511 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3512 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003513 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515#ifdef FEAT_CMDL_INFO
3516 else if (p_ru)
3517 {
3518 /* Current line and column are already on the screen -- webb */
3519 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003520 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003522 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 (long)curbuf->b_ml.ml_line_count, n);
3524 }
3525#endif
3526 else
3527 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003528 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 _("line %ld of %ld --%d%%-- col "),
3530 (long)curwin->w_cursor.lnum,
3531 (long)curbuf->b_ml.ml_line_count,
3532 n);
3533 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003534 len = STRLEN(buffer);
3535 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3537 }
3538
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003539 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
3541 if (dont_truncate)
3542 {
3543 /* Temporarily set msg_scroll to avoid the message being truncated.
3544 * First call msg_start() to get the message in the right place. */
3545 msg_start();
3546 n = msg_scroll;
3547 msg_scroll = TRUE;
3548 msg(buffer);
3549 msg_scroll = n;
3550 }
3551 else
3552 {
3553 p = msg_trunc_attr(buffer, FALSE, 0);
3554 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /* Need to repeat the message after redrawing when:
3556 * - When restart_edit is set (otherwise there will be a delay
3557 * before redrawing).
3558 * - When the screen was scrolled but there is no wait-return
3559 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003560 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562
3563 vim_free(buffer);
3564}
3565
3566 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003567col_print(
3568 char_u *buf,
3569 size_t buflen,
3570 int col,
3571 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
3573 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003574 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003576 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577}
3578
3579#if defined(FEAT_TITLE) || defined(PROTO)
3580/*
3581 * put file name in title bar of window and in icon title
3582 */
3583
3584static char_u *lasttitle = NULL;
3585static char_u *lasticon = NULL;
3586
3587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003588maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
3590 char_u *p;
3591 char_u *t_str = NULL;
3592 char_u *i_name;
3593 char_u *i_str = NULL;
3594 int maxlen = 0;
3595 int len;
3596 int mustset;
3597 char_u buf[IOSIZE];
3598 int off;
3599
3600 if (!redrawing())
3601 {
3602 /* Postpone updating the title when 'lazyredraw' is set. */
3603 need_maketitle = TRUE;
3604 return;
3605 }
3606
3607 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003608 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 return;
3610
3611 if (p_title)
3612 {
3613 if (p_titlelen > 0)
3614 {
3615 maxlen = p_titlelen * Columns / 100;
3616 if (maxlen < 10)
3617 maxlen = 10;
3618 }
3619
3620 t_str = buf;
3621 if (*p_titlestring != NUL)
3622 {
3623#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003624 if (stl_syntax & STL_IN_TITLE)
3625 {
3626 int use_sandbox = FALSE;
3627 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003628
3629# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003630 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003631# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003632 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003634 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003635 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003636 if (called_emsg)
3637 set_string_option_direct((char_u *)"titlestring", -1,
3638 (char_u *)"", OPT_FREE, SID_ERROR);
3639 called_emsg |= save_called_emsg;
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 else
3642#endif
3643 t_str = p_titlestring;
3644 }
3645 else
3646 {
3647 /* format: "fname + (path) (1 of 2) - VIM" */
3648
Bram Moolenaar2c666692012-09-05 13:30:40 +02003649#define SPACE_FOR_FNAME (IOSIZE - 100)
3650#define SPACE_FOR_DIR (IOSIZE - 20)
3651#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003653 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003654#ifdef FEAT_TERMINAL
3655 else if (curbuf->b_term != NULL)
3656 {
3657 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3658 SPACE_FOR_FNAME);
3659 }
3660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 else
3662 {
3663 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003664 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667
Bram Moolenaar21554412017-07-24 21:44:43 +02003668#ifdef FEAT_TERMINAL
3669 if (curbuf->b_term == NULL)
3670#endif
3671 switch (bufIsChanged(curbuf)
3672 + (curbuf->b_p_ro * 2)
3673 + (!curbuf->b_p_ma * 4))
3674 {
3675 case 1: STRCAT(buf, " +"); break;
3676 case 2: STRCAT(buf, " ="); break;
3677 case 3: STRCAT(buf, " =+"); break;
3678 case 4:
3679 case 6: STRCAT(buf, " -"); break;
3680 case 5:
3681 case 7: STRCAT(buf, " -+"); break;
3682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
Bram Moolenaar21554412017-07-24 21:44:43 +02003684 if (curbuf->b_fname != NULL
3685#ifdef FEAT_TERMINAL
3686 && curbuf->b_term == NULL
3687#endif
3688 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
3690 /* Get path of file, replace home dir with ~ */
3691 off = (int)STRLEN(buf);
3692 buf[off++] = ' ';
3693 buf[off++] = '(';
3694 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003695 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696#ifdef BACKSLASH_IN_FILENAME
3697 /* avoid "c:/name" to be reduced to "c" */
3698 if (isalpha(buf[off]) && buf[off + 1] == ':')
3699 off += 2;
3700#endif
3701 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003702 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003704 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003705 /* must be a help buffer */
3706 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003707 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003711
Bram Moolenaar2c666692012-09-05 13:30:40 +02003712 /* Translate unprintable chars and concatenate. Keep some
3713 * room for the server name. When there is no room (very long
3714 * file name) use (...). */
3715 if (off < SPACE_FOR_DIR)
3716 {
3717 p = transstr(buf + off);
3718 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3719 vim_free(p);
3720 }
3721 else
3722 {
3723 vim_strncpy(buf + off, (char_u *)"...",
3724 (size_t)(SPACE_FOR_ARGNR - off));
3725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 STRCAT(buf, ")");
3727 }
3728
Bram Moolenaar2c666692012-09-05 13:30:40 +02003729 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
3731#if defined(FEAT_CLIENTSERVER)
3732 if (serverName != NULL)
3733 {
3734 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003735 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 else
3738#endif
3739 STRCAT(buf, " - VIM");
3740
3741 if (maxlen > 0)
3742 {
3743 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003744 if (vim_strsize(buf) > maxlen)
3745 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747 }
3748 }
3749 mustset = ti_change(t_str, &lasttitle);
3750
3751 if (p_icon)
3752 {
3753 i_str = buf;
3754 if (*p_iconstring != NUL)
3755 {
3756#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003757 if (stl_syntax & STL_IN_ICON)
3758 {
3759 int use_sandbox = FALSE;
3760 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003761
3762# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003763 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003764# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003765 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003767 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003768 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003769 if (called_emsg)
3770 set_string_option_direct((char_u *)"iconstring", -1,
3771 (char_u *)"", OPT_FREE, SID_ERROR);
3772 called_emsg |= save_called_emsg;
3773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 else
3775#endif
3776 i_str = p_iconstring;
3777 }
3778 else
3779 {
3780 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003781 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 else /* use file name only in icon */
3783 i_name = gettail(curbuf->b_ffname);
3784 *i_str = NUL;
3785 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003786 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (len > 100)
3788 {
3789 len -= 100;
3790#ifdef FEAT_MBYTE
3791 if (has_mbyte)
3792 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3793#endif
3794 i_name += len;
3795 }
3796 STRCPY(i_str, i_name);
3797 trans_characters(i_str, IOSIZE);
3798 }
3799 }
3800
3801 mustset |= ti_change(i_str, &lasticon);
3802
3803 if (mustset)
3804 resettitle();
3805}
3806
3807/*
3808 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3809 * from "str" if it does.
3810 * Return TRUE when "*last" changed.
3811 */
3812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003813ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
3815 if ((str == NULL) != (*last == NULL)
3816 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3817 {
3818 vim_free(*last);
3819 if (str == NULL)
3820 *last = NULL;
3821 else
3822 *last = vim_strsave(str);
3823 return TRUE;
3824 }
3825 return FALSE;
3826}
3827
3828/*
3829 * Put current window title back (used after calling a shell)
3830 */
3831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003832resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833{
3834 mch_settitle(lasttitle, lasticon);
3835}
Bram Moolenaarea408852005-06-25 22:49:46 +00003836
3837# if defined(EXITFREE) || defined(PROTO)
3838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003839free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003840{
3841 vim_free(lasttitle);
3842 vim_free(lasticon);
3843}
3844# endif
3845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#endif /* FEAT_TITLE */
3847
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003848#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003850 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 * Return length of string in screen cells.
3852 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003853 * Normally works for window "wp", except when working for 'tabline' then it
3854 * is "curwin".
3855 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 * Items are drawn interspersed with the text that surrounds it
3857 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3858 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3859 *
3860 * If maxwidth is not zero, the string will be filled at any middle marker
3861 * or truncated if too long, fillchar is used for all whitespace.
3862 */
3863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003864build_stl_str_hl(
3865 win_T *wp,
3866 char_u *out, /* buffer to write into != NameBuff */
3867 size_t outlen, /* length of out[] */
3868 char_u *fmt,
3869 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3870 int fillchar,
3871 int maxwidth,
3872 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3873 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
3875 char_u *p;
3876 char_u *s;
3877 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003878 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003880 win_T *save_curwin;
3881 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882#endif
3883 int empty_line;
3884 colnr_T virtcol;
3885 long l;
3886 long n;
3887 int prevchar_isflag;
3888 int prevchar_isitem;
3889 int itemisflag;
3890 int fillable;
3891 char_u *str;
3892 long num;
3893 int width;
3894 int itemcnt;
3895 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003896 int group_end_userhl;
3897 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 int groupitem[STL_MAX_ITEM];
3899 int groupdepth;
3900 struct stl_item
3901 {
3902 char_u *start;
3903 int minwid;
3904 int maxwid;
3905 enum
3906 {
3907 Normal,
3908 Empty,
3909 Group,
3910 Middle,
3911 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003912 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 Trunc
3914 } type;
3915 } item[STL_MAX_ITEM];
3916 int minwid;
3917 int maxwid;
3918 int zeropad;
3919 char_u base;
3920 char_u opt;
3921#define TMPLEN 70
3922 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003923 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003924 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003925 int save_must_redraw = must_redraw;
3926 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003927
3928#ifdef FEAT_EVAL
3929 /*
3930 * When the format starts with "%!" then evaluate it as an expression and
3931 * use the result as the actual format string.
3932 */
3933 if (fmt[0] == '%' && fmt[1] == '!')
3934 {
3935 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3936 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003937 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003938 }
3939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
3941 if (fillchar == 0)
3942 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003943#ifdef FEAT_MBYTE
3944 /* Can't handle a multi-byte fill character yet. */
3945 else if (mb_char2len(fillchar) > 1)
3946 fillchar = '-';
3947#endif
3948
Bram Moolenaar567199b2013-04-24 16:52:36 +02003949 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3950 * p will become invalid when getting another buffer line. */
3951 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3952 empty_line = (*p == NUL);
3953
3954 /* Get the byte value now, in case we need it below. This is more
3955 * efficient than making a copy of the line. */
3956 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3957 byteval = 0;
3958 else
3959#ifdef FEAT_MBYTE
3960 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3961#else
3962 byteval = p[wp->w_cursor.col];
3963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
3965 groupdepth = 0;
3966 p = out;
3967 curitem = 0;
3968 prevchar_isflag = TRUE;
3969 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003970 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003972 if (curitem == STL_MAX_ITEM)
3973 {
3974 /* There are too many items. Add the error code to the statusline
3975 * to give the user a hint about what went wrong. */
3976 if (p + 6 < out + outlen)
3977 {
3978 mch_memmove(p, " E541", (size_t)5);
3979 p += 5;
3980 }
3981 break;
3982 }
3983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 if (*s != NUL && *s != '%')
3985 prevchar_isflag = prevchar_isitem = FALSE;
3986
3987 /*
3988 * Handle up to the next '%' or the end.
3989 */
3990 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3991 *p++ = *s++;
3992 if (*s == NUL || p + 1 >= out + outlen)
3993 break;
3994
3995 /*
3996 * Handle one '%' item.
3997 */
3998 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003999 if (*s == NUL) /* ignore trailing % */
4000 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 if (*s == '%')
4002 {
4003 if (p + 1 >= out + outlen)
4004 break;
4005 *p++ = *s++;
4006 prevchar_isflag = prevchar_isitem = FALSE;
4007 continue;
4008 }
4009 if (*s == STL_MIDDLEMARK)
4010 {
4011 s++;
4012 if (groupdepth > 0)
4013 continue;
4014 item[curitem].type = Middle;
4015 item[curitem++].start = p;
4016 continue;
4017 }
4018 if (*s == STL_TRUNCMARK)
4019 {
4020 s++;
4021 item[curitem].type = Trunc;
4022 item[curitem++].start = p;
4023 continue;
4024 }
4025 if (*s == ')')
4026 {
4027 s++;
4028 if (groupdepth < 1)
4029 continue;
4030 groupdepth--;
4031
4032 t = item[groupitem[groupdepth]].start;
4033 *p = NUL;
4034 l = vim_strsize(t);
4035 if (curitem > groupitem[groupdepth] + 1
4036 && item[groupitem[groupdepth]].minwid == 0)
4037 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004038 /* remove group if all items are empty and highlight group
4039 * doesn't change */
4040 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004041 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4042 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004043 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004044 {
4045 group_start_userhl = group_end_userhl = item[n].minwid;
4046 break;
4047 }
4048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004050 {
4051 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004053 if (item[n].type == Highlight)
4054 group_end_userhl = item[n].minwid;
4055 }
4056 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
4058 p = t;
4059 l = 0;
4060 }
4061 }
4062 if (l > item[groupitem[groupdepth]].maxwid)
4063 {
4064 /* truncate, remove n bytes of text at the start */
4065#ifdef FEAT_MBYTE
4066 if (has_mbyte)
4067 {
4068 /* Find the first character that should be included. */
4069 n = 0;
4070 while (l >= item[groupitem[groupdepth]].maxwid)
4071 {
4072 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004073 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 }
4076 else
4077#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004078 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
4080 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004081 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 p = p - n + 1;
4083#ifdef FEAT_MBYTE
4084 /* Fill up space left over by half a double-wide char. */
4085 while (++l < item[groupitem[groupdepth]].minwid)
4086 *p++ = fillchar;
4087#endif
4088
4089 /* correct the start of the items for the truncation */
4090 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4091 {
4092 item[l].start -= n;
4093 if (item[l].start < t)
4094 item[l].start = t;
4095 }
4096 }
4097 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4098 {
4099 /* fill */
4100 n = item[groupitem[groupdepth]].minwid;
4101 if (n < 0)
4102 {
4103 /* fill by appending characters */
4104 n = 0 - n;
4105 while (l++ < n && p + 1 < out + outlen)
4106 *p++ = fillchar;
4107 }
4108 else
4109 {
4110 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004111 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 l = n - l;
4113 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004114 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 p += l;
4116 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4117 item[n].start += l;
4118 for ( ; l > 0; l--)
4119 *t++ = fillchar;
4120 }
4121 }
4122 continue;
4123 }
4124 minwid = 0;
4125 maxwid = 9999;
4126 zeropad = FALSE;
4127 l = 1;
4128 if (*s == '0')
4129 {
4130 s++;
4131 zeropad = TRUE;
4132 }
4133 if (*s == '-')
4134 {
4135 s++;
4136 l = -1;
4137 }
4138 if (VIM_ISDIGIT(*s))
4139 {
4140 minwid = (int)getdigits(&s);
4141 if (minwid < 0) /* overflow */
4142 minwid = 0;
4143 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004144 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 item[curitem].type = Highlight;
4147 item[curitem].start = p;
4148 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4149 s++;
4150 curitem++;
4151 continue;
4152 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004153 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4154 {
4155 if (*s == STL_TABCLOSENR)
4156 {
4157 if (minwid == 0)
4158 {
4159 /* %X ends the close label, go back to the previously
4160 * define tab label nr. */
4161 for (n = curitem - 1; n >= 0; --n)
4162 if (item[n].type == TabPage && item[n].minwid >= 0)
4163 {
4164 minwid = item[n].minwid;
4165 break;
4166 }
4167 }
4168 else
4169 /* close nrs are stored as negative values */
4170 minwid = - minwid;
4171 }
4172 item[curitem].type = TabPage;
4173 item[curitem].start = p;
4174 item[curitem].minwid = minwid;
4175 s++;
4176 curitem++;
4177 continue;
4178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 if (*s == '.')
4180 {
4181 s++;
4182 if (VIM_ISDIGIT(*s))
4183 {
4184 maxwid = (int)getdigits(&s);
4185 if (maxwid <= 0) /* overflow */
4186 maxwid = 50;
4187 }
4188 }
4189 minwid = (minwid > 50 ? 50 : minwid) * l;
4190 if (*s == '(')
4191 {
4192 groupitem[groupdepth++] = curitem;
4193 item[curitem].type = Group;
4194 item[curitem].start = p;
4195 item[curitem].minwid = minwid;
4196 item[curitem].maxwid = maxwid;
4197 s++;
4198 curitem++;
4199 continue;
4200 }
4201 if (vim_strchr(STL_ALL, *s) == NULL)
4202 {
4203 s++;
4204 continue;
4205 }
4206 opt = *s++;
4207
4208 /* OK - now for the real work */
4209 base = 'D';
4210 itemisflag = FALSE;
4211 fillable = TRUE;
4212 num = -1;
4213 str = NULL;
4214 switch (opt)
4215 {
4216 case STL_FILEPATH:
4217 case STL_FULLPATH:
4218 case STL_FILENAME:
4219 fillable = FALSE; /* don't change ' ' to fillchar */
4220 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004221 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 else
4223 {
4224 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004225 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4227 }
4228 trans_characters(NameBuff, MAXPATHL);
4229 if (opt != STL_FILENAME)
4230 str = NameBuff;
4231 else
4232 str = gettail(NameBuff);
4233 break;
4234
4235 case STL_VIM_EXPR: /* '{' */
4236 itemisflag = TRUE;
4237 t = p;
4238 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4239 *p++ = *s++;
4240 if (*s != '}') /* missing '}' or out of space */
4241 break;
4242 s++;
4243 *p = 0;
4244 p = t;
4245
4246#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004247 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4249
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004250 save_curbuf = curbuf;
4251 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 curwin = wp;
4253 curbuf = wp->w_buffer;
4254
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004255 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004257 curwin = save_curwin;
4258 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004259 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
4261 if (str != NULL && *str != 0)
4262 {
4263 if (*skipdigits(str) == NUL)
4264 {
4265 num = atoi((char *)str);
4266 vim_free(str);
4267 str = NULL;
4268 itemisflag = FALSE;
4269 }
4270 }
4271#endif
4272 break;
4273
4274 case STL_LINE:
4275 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4276 ? 0L : (long)(wp->w_cursor.lnum);
4277 break;
4278
4279 case STL_NUMLINES:
4280 num = wp->w_buffer->b_ml.ml_line_count;
4281 break;
4282
4283 case STL_COLUMN:
4284 num = !(State & INSERT) && empty_line
4285 ? 0 : (int)wp->w_cursor.col + 1;
4286 break;
4287
4288 case STL_VIRTCOL:
4289 case STL_VIRTCOL_ALT:
4290 /* In list mode virtcol needs to be recomputed */
4291 virtcol = wp->w_virtcol;
4292 if (wp->w_p_list && lcs_tab1 == NUL)
4293 {
4294 wp->w_p_list = FALSE;
4295 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4296 wp->w_p_list = TRUE;
4297 }
4298 ++virtcol;
4299 /* Don't display %V if it's the same as %c. */
4300 if (opt == STL_VIRTCOL_ALT
4301 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4302 ? 0 : (int)wp->w_cursor.col + 1)))
4303 break;
4304 num = (long)virtcol;
4305 break;
4306
4307 case STL_PERCENTAGE:
4308 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4309 (long)wp->w_buffer->b_ml.ml_line_count);
4310 break;
4311
4312 case STL_ALTPERCENT:
4313 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004314 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 break;
4316
4317 case STL_ARGLISTSTAT:
4318 fillable = FALSE;
4319 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004320 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 str = tmp;
4322 break;
4323
4324 case STL_KEYMAP:
4325 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004326 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 str = tmp;
4328 break;
4329 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004330#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004331 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332#else
4333 num = 0;
4334#endif
4335 break;
4336
4337 case STL_BUFNO:
4338 num = wp->w_buffer->b_fnum;
4339 break;
4340
4341 case STL_OFFSET_X:
4342 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004343 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 case STL_OFFSET:
4345#ifdef FEAT_BYTEOFF
4346 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4347 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4348 0L : l + 1 + (!(State & INSERT) && empty_line ?
4349 0 : (int)wp->w_cursor.col);
4350#endif
4351 break;
4352
4353 case STL_BYTEVAL_X:
4354 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004355 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004357 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (num == NL)
4359 num = 0;
4360 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4361 num = NL;
4362 break;
4363
4364 case STL_ROFLAG:
4365 case STL_ROFLAG_ALT:
4366 itemisflag = TRUE;
4367 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004368 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 break;
4370
4371 case STL_HELPFLAG:
4372 case STL_HELPFLAG_ALT:
4373 itemisflag = TRUE;
4374 if (wp->w_buffer->b_help)
4375 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004376 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 break;
4378
4379#ifdef FEAT_AUTOCMD
4380 case STL_FILETYPE:
4381 if (*wp->w_buffer->b_p_ft != NUL
4382 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4383 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004384 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4385 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 str = tmp;
4387 }
4388 break;
4389
4390 case STL_FILETYPE_ALT:
4391 itemisflag = TRUE;
4392 if (*wp->w_buffer->b_p_ft != NUL
4393 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4394 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004395 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4396 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 for (t = tmp; *t != 0; t++)
4398 *t = TOUPPER_LOC(*t);
4399 str = tmp;
4400 }
4401 break;
4402#endif
4403
Bram Moolenaar4033c552017-09-16 20:54:51 +02004404#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 case STL_PREVIEWFLAG:
4406 case STL_PREVIEWFLAG_ALT:
4407 itemisflag = TRUE;
4408 if (wp->w_p_pvw)
4409 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4410 : _("[Preview]"));
4411 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004412
4413 case STL_QUICKFIX:
4414 if (bt_quickfix(wp->w_buffer))
4415 str = (char_u *)(wp->w_llist_ref
4416 ? _(msg_loclist)
4417 : _(msg_qflist));
4418 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#endif
4420
4421 case STL_MODIFIED:
4422 case STL_MODIFIED_ALT:
4423 itemisflag = TRUE;
4424 switch ((opt == STL_MODIFIED_ALT)
4425 + bufIsChanged(wp->w_buffer) * 2
4426 + (!wp->w_buffer->b_p_ma) * 4)
4427 {
4428 case 2: str = (char_u *)"[+]"; break;
4429 case 3: str = (char_u *)",+"; break;
4430 case 4: str = (char_u *)"[-]"; break;
4431 case 5: str = (char_u *)",-"; break;
4432 case 6: str = (char_u *)"[+-]"; break;
4433 case 7: str = (char_u *)",+-"; break;
4434 }
4435 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004436
4437 case STL_HIGHLIGHT:
4438 t = s;
4439 while (*s != '#' && *s != NUL)
4440 ++s;
4441 if (*s == '#')
4442 {
4443 item[curitem].type = Highlight;
4444 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004445 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004446 curitem++;
4447 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004448 if (*s != NUL)
4449 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004450 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452
4453 item[curitem].start = p;
4454 item[curitem].type = Normal;
4455 if (str != NULL && *str)
4456 {
4457 t = str;
4458 if (itemisflag)
4459 {
4460 if ((t[0] && t[1])
4461 && ((!prevchar_isitem && *t == ',')
4462 || (prevchar_isflag && *t == ' ')))
4463 t++;
4464 prevchar_isflag = TRUE;
4465 }
4466 l = vim_strsize(t);
4467 if (l > 0)
4468 prevchar_isitem = TRUE;
4469 if (l > maxwid)
4470 {
4471 while (l >= maxwid)
4472#ifdef FEAT_MBYTE
4473 if (has_mbyte)
4474 {
4475 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004476 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 else
4479#endif
4480 l -= byte2cells(*t++);
4481 if (p + 1 >= out + outlen)
4482 break;
4483 *p++ = '<';
4484 }
4485 if (minwid > 0)
4486 {
4487 for (; l < minwid && p + 1 < out + outlen; l++)
4488 {
4489 /* Don't put a "-" in front of a digit. */
4490 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4491 *p++ = ' ';
4492 else
4493 *p++ = fillchar;
4494 }
4495 minwid = 0;
4496 }
4497 else
4498 minwid *= -1;
4499 while (*t && p + 1 < out + outlen)
4500 {
4501 *p++ = *t++;
4502 /* Change a space by fillchar, unless fillchar is '-' and a
4503 * digit follows. */
4504 if (fillable && p[-1] == ' '
4505 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4506 p[-1] = fillchar;
4507 }
4508 for (; l < minwid && p + 1 < out + outlen; l++)
4509 *p++ = fillchar;
4510 }
4511 else if (num >= 0)
4512 {
4513 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4514 char_u nstr[20];
4515
4516 if (p + 20 >= out + outlen)
4517 break; /* not sufficient space */
4518 prevchar_isitem = TRUE;
4519 t = nstr;
4520 if (opt == STL_VIRTCOL_ALT)
4521 {
4522 *t++ = '-';
4523 minwid--;
4524 }
4525 *t++ = '%';
4526 if (zeropad)
4527 *t++ = '0';
4528 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004529 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 *t = 0;
4531
4532 for (n = num, l = 1; n >= nbase; n /= nbase)
4533 l++;
4534 if (opt == STL_VIRTCOL_ALT)
4535 l++;
4536 if (l > maxwid)
4537 {
4538 l += 2;
4539 n = l - maxwid;
4540 while (l-- > maxwid)
4541 num /= nbase;
4542 *t++ = '>';
4543 *t++ = '%';
4544 *t = t[-3];
4545 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004546 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4547 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004550 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4551 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 p += STRLEN(p);
4553 }
4554 else
4555 item[curitem].type = Empty;
4556
4557 if (opt == STL_VIM_EXPR)
4558 vim_free(str);
4559
4560 if (num >= 0 || (!itemisflag && str && *str))
4561 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4562 curitem++;
4563 }
4564 *p = NUL;
4565 itemcnt = curitem;
4566
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004567#ifdef FEAT_EVAL
4568 if (usefmt != fmt)
4569 vim_free(usefmt);
4570#endif
4571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 width = vim_strsize(out);
4573 if (maxwidth > 0 && width > maxwidth)
4574 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004575 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 l = 0;
4577 if (itemcnt == 0)
4578 s = out;
4579 else
4580 {
4581 for ( ; l < itemcnt; l++)
4582 if (item[l].type == Trunc)
4583 {
4584 /* Truncate at %< item. */
4585 s = item[l].start;
4586 break;
4587 }
4588 if (l == itemcnt)
4589 {
4590 /* No %< item, truncate first item. */
4591 s = item[0].start;
4592 l = 0;
4593 }
4594 }
4595
4596 if (width - vim_strsize(s) >= maxwidth)
4597 {
4598 /* Truncation mark is beyond max length */
4599#ifdef FEAT_MBYTE
4600 if (has_mbyte)
4601 {
4602 s = out;
4603 width = 0;
4604 for (;;)
4605 {
4606 width += ptr2cells(s);
4607 if (width >= maxwidth)
4608 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004609 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 /* Fill up for half a double-wide character. */
4612 while (++width < maxwidth)
4613 *s++ = fillchar;
4614 }
4615 else
4616#endif
4617 s = out + maxwidth - 1;
4618 for (l = 0; l < itemcnt; l++)
4619 if (item[l].start > s)
4620 break;
4621 itemcnt = l;
4622 *s++ = '>';
4623 *s = 0;
4624 }
4625 else
4626 {
4627#ifdef FEAT_MBYTE
4628 if (has_mbyte)
4629 {
4630 n = 0;
4631 while (width >= maxwidth)
4632 {
4633 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004634 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 }
4637 else
4638#endif
4639 n = width - maxwidth + 1;
4640 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004641 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 *s = '<';
4643
4644 /* Fill up for half a double-wide character. */
4645 while (++width < maxwidth)
4646 {
4647 s = s + STRLEN(s);
4648 *s++ = fillchar;
4649 *s = NUL;
4650 }
4651
4652 --n; /* count the '<' */
4653 for (; l < itemcnt; l++)
4654 {
4655 if (item[l].start - n >= s)
4656 item[l].start -= n;
4657 else
4658 item[l].start = s;
4659 }
4660 }
4661 width = maxwidth;
4662 }
4663 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4664 {
4665 /* Apply STL_MIDDLE if any */
4666 for (l = 0; l < itemcnt; l++)
4667 if (item[l].type == Middle)
4668 break;
4669 if (l < itemcnt)
4670 {
4671 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004672 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 for (s = item[l].start; s < p; s++)
4674 *s = fillchar;
4675 for (l++; l < itemcnt; l++)
4676 item[l].start += maxwidth - width;
4677 width = maxwidth;
4678 }
4679 }
4680
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004681 /* Store the info about highlighting. */
4682 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004684 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 for (l = 0; l < itemcnt; l++)
4686 {
4687 if (item[l].type == Highlight)
4688 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004689 sp->start = item[l].start;
4690 sp->userhl = item[l].minwid;
4691 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
4693 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004694 sp->start = NULL;
4695 sp->userhl = 0;
4696 }
4697
4698 /* Store the info about tab pages labels. */
4699 if (tabtab != NULL)
4700 {
4701 sp = tabtab;
4702 for (l = 0; l < itemcnt; l++)
4703 {
4704 if (item[l].type == TabPage)
4705 {
4706 sp->start = item[l].start;
4707 sp->userhl = item[l].minwid;
4708 sp++;
4709 }
4710 }
4711 sp->start = NULL;
4712 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
4714
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004715 /* When inside update_screen we do not want redrawing a stausline, ruler,
4716 * title, etc. to trigger another redraw, it may cause an endless loop. */
4717 if (updating_screen)
4718 {
4719 must_redraw = save_must_redraw;
4720 curwin->w_redr_type = save_redr_type;
4721 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return width;
4724}
4725#endif /* FEAT_STL_OPT */
4726
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004727#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4728 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004730 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4731 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 */
4733 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004734get_rel_pos(
4735 win_T *wp,
4736 char_u *buf,
4737 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
4739 long above; /* number of lines above window */
4740 long below; /* number of lines below window */
4741
Bram Moolenaar0027c212015-01-07 13:31:52 +01004742 if (buflen < 3) /* need at least 3 chars for writing */
4743 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 above = wp->w_topline - 1;
4745#ifdef FEAT_DIFF
4746 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004747 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4748 above = 0; /* All buffer lines are displayed and there is an
4749 * indication of filler lines, that can be considered
4750 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751#endif
4752 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4753 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004754 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4755 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004757 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004759 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 ? (int)(above / ((above + below) / 100L))
4761 : (int)(above * 100L / (above + below)));
4762}
4763#endif
4764
4765/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004766 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 * Return TRUE if it was appended.
4768 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004770append_arg_number(
4771 win_T *wp,
4772 char_u *buf,
4773 int buflen,
4774 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 char_u *p;
4777
4778 if (ARGCOUNT <= 1) /* nothing to do */
4779 return FALSE;
4780
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004781 p = buf + STRLEN(buf); /* go to the end of the buffer */
4782 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 return FALSE;
4784 *p++ = ' ';
4785 *p++ = '(';
4786 if (add_file)
4787 {
4788 STRCPY(p, "file ");
4789 p += 5;
4790 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004791 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4792 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4794 return TRUE;
4795}
4796
4797/*
4798 * If fname is not a full path, make it a full path.
4799 * Returns pointer to allocated memory (NULL for failure).
4800 */
4801 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004802fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
4804 /*
4805 * Force expanding the path always for Unix, because symbolic links may
4806 * mess up the full path name, even though it starts with a '/'.
4807 * Also expand when there is ".." in the file name, try to remove it,
4808 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004809 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 * For MS-Windows also expand names like "longna~1" to "longname".
4811 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004812#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 return FullName_save(fname, TRUE);
4814#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004815 if (!vim_isAbsName(fname)
4816 || strstr((char *)fname, "..") != NULL
4817 || strstr((char *)fname, "//") != NULL
4818# ifdef BACKSLASH_IN_FILENAME
4819 || strstr((char *)fname, "\\\\") != NULL
4820# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004821# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 )
4825 return FullName_save(fname, FALSE);
4826
4827 fname = vim_strsave(fname);
4828
Bram Moolenaar9b942202007-10-03 12:31:33 +00004829# ifdef USE_FNAME_CASE
4830# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
4834 if (fname != NULL)
4835 fname_case(fname, 0); /* set correct case for file name */
4836 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
4839 return fname;
4840#endif
4841}
4842
4843/*
4844 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4845 * "ffname" becomes a pointer to allocated memory (or NULL).
4846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004848fname_expand(
4849 buf_T *buf UNUSED,
4850 char_u **ffname,
4851 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
4853 if (*ffname == NULL) /* if no file name given, nothing to do */
4854 return;
4855 if (*sfname == NULL) /* if no short file name given, use ffname */
4856 *sfname = *ffname;
4857 *ffname = fix_fname(*ffname); /* expand to full path */
4858
4859#ifdef FEAT_SHORTCUT
4860 if (!buf->b_p_bin)
4861 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004862 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
4864 /* If the file name is a shortcut file, use the file it links to. */
4865 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004866 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 vim_free(*ffname);
4869 *ffname = rfname;
4870 *sfname = rfname;
4871 }
4872 }
4873#endif
4874}
4875
4876/*
4877 * Get the file name for an argument list entry.
4878 */
4879 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004880alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
4882 buf_T *bp;
4883
4884 /* Use the name from the associated buffer if it exists. */
4885 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004886 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 return aep->ae_fname;
4888 return bp->b_fname;
4889}
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891/*
4892 * do_arg_all(): Open up to 'count' windows, one for each argument.
4893 */
4894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004895do_arg_all(
4896 int count,
4897 int forceit, /* hide buffers in current windows */
4898 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
4900 int i;
4901 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004902 char_u *opened; /* Array of weight for which args are open:
4903 * 0: not opened
4904 * 1: opened in other tab
4905 * 2: opened in curtab
4906 * 3: opened in curtab and curwin
4907 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004908 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 int use_firstwin = FALSE; /* use first window for arglist */
4910 int split_ret = OK;
4911 int p_ea_save;
4912 alist_T *alist; /* argument list to be used */
4913 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004914 tabpage_T *tpnext;
4915 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004916 win_T *old_curwin, *last_curwin;
4917 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004918 win_T *new_curwin = NULL;
4919 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921 if (ARGCOUNT <= 0)
4922 {
4923 /* Don't give an error message. We don't want it when the ":all"
4924 * command is in the .vimrc. */
4925 return;
4926 }
4927 setpcmark();
4928
4929 opened_len = ARGCOUNT;
4930 opened = alloc_clear((unsigned)opened_len);
4931 if (opened == NULL)
4932 return;
4933
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004934 /* Autocommands may do anything to the argument list. Make sure it's not
4935 * freed while we are working here by "locking" it. We still have to
4936 * watch out for its size to be changed. */
4937 alist = curwin->w_alist;
4938 ++alist->al_refcount;
4939
4940 old_curwin = curwin;
4941 old_curtab = curtab;
4942
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004943# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
4947 /*
4948 * Try closing all windows that are not in the argument list.
4949 * Also close windows that are not full width;
4950 * When 'hidden' or "forceit" set the buffer becomes hidden.
4951 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004952 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004954 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004955 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004956 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004958 tpnext = curtab->tp_next;
4959 for (wp = firstwin; wp != NULL; wp = wpnext)
4960 {
4961 wpnext = wp->w_next;
4962 buf = wp->w_buffer;
4963 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004964 || (!keep_tabs && (buf->b_nwindows > 1
4965 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004966 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004969 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004970 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004972 if (i < alist->al_ga.ga_len
4973 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4974 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4975 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004977 int weight = 1;
4978
4979 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004980 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004981 ++weight;
4982 if (old_curwin == wp)
4983 ++weight;
4984 }
4985
4986 if (weight > (int)opened[i])
4987 {
4988 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004989 if (i == 0)
4990 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004991 if (new_curwin != NULL)
4992 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004993 new_curwin = wp;
4994 new_curtab = curtab;
4995 }
4996 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004997 else if (keep_tabs)
4998 i = opened_len;
4999
5000 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005001 {
5002 /* Use the current argument list for all windows
5003 * containing a file from it. */
5004 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005005 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005006 ++wp->w_alist->al_refcount;
5007 }
5008 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005012 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005014 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005016 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005017 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005019 /* If the buffer was changed, and we would like to hide it,
5020 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005021 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005022 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005024#ifdef FEAT_AUTOCMD
5025 bufref_T bufref;
5026
5027 set_bufref(&bufref, buf);
5028#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005029 (void)autowrite(buf, FALSE);
5030#ifdef FEAT_AUTOCMD
5031 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005032 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005033 {
5034 wpnext = firstwin; /* start all over... */
5035 continue;
5036 }
5037#endif
5038 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005039 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005040 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005041 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005042 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005043 else
5044 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005045 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaar4033c552017-09-16 20:54:51 +02005046#ifdef FEAT_AUTOCMD
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005047 /* check if autocommands removed the next window */
5048 if (!win_valid(wpnext))
5049 wpnext = firstwin; /* start all over... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005055
5056 /* Without the ":tab" modifier only do the current tab page. */
5057 if (had_tab == 0 || tpnext == NULL)
5058 break;
5059
5060# ifdef FEAT_AUTOCMD
5061 /* check if autocommands removed the next tab page */
5062 if (!valid_tabpage(tpnext))
5063 tpnext = first_tabpage; /* start all over...*/
5064# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005065 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067
5068 /*
5069 * Open a window for files in the argument list that don't have one.
5070 * ARGCOUNT may change while doing this, because of autocommands.
5071 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005072 if (count > opened_len || count <= 0)
5073 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075#ifdef FEAT_AUTOCMD
5076 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5077 ++autocmd_no_enter;
5078 ++autocmd_no_leave;
5079#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005080 last_curwin = curwin;
5081 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005083 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5084 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005085 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005086 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5087 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005088
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005089 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5092 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005093 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 /* Move the already present window to below the current window */
5096 if (curwin->w_arg_idx != i)
5097 {
5098 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5099 {
5100 if (wpnext->w_arg_idx == i)
5101 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005102 if (keep_tabs)
5103 {
5104 new_curwin = wpnext;
5105 new_curtab = curtab;
5106 }
5107 else
5108 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110 }
5111 }
5112 }
5113 }
5114 else if (split_ret == OK)
5115 {
5116 if (!use_firstwin) /* split current window */
5117 {
5118 p_ea_save = p_ea;
5119 p_ea = TRUE; /* use space from all windows */
5120 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5121 p_ea = p_ea_save;
5122 if (split_ret == FAIL)
5123 continue;
5124 }
5125#ifdef FEAT_AUTOCMD
5126 else /* first window: do autocmd for leaving this buffer */
5127 --autocmd_no_leave;
5128#endif
5129
5130 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005131 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 */
5133 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005134 if (i == 0)
5135 {
5136 new_curwin = curwin;
5137 new_curtab = curtab;
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5140 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005141 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005143 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144#ifdef FEAT_AUTOCMD
5145 if (use_firstwin)
5146 ++autocmd_no_leave;
5147#endif
5148 use_firstwin = FALSE;
5149 }
5150 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005151
5152 /* When ":tab" was used open a new tab for a new window repeatedly. */
5153 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5154 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156
5157 /* Remove the "lock" on the argument list. */
5158 alist_unlink(alist);
5159
5160#ifdef FEAT_AUTOCMD
5161 --autocmd_no_enter;
5162#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005163 /* restore last referenced tabpage's curwin */
5164 if (last_curtab != new_curtab)
5165 {
5166 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005167 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005168 if (win_valid(last_curwin))
5169 win_enter(last_curwin, FALSE);
5170 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005171 /* to window with first arg */
5172 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005173 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005174 if (win_valid(new_curwin))
5175 win_enter(new_curwin, FALSE);
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177#ifdef FEAT_AUTOCMD
5178 --autocmd_no_leave;
5179#endif
5180 vim_free(opened);
5181}
5182
5183# if defined(FEAT_LISTCMDS) || defined(PROTO)
5184/*
5185 * Open a window for a number of buffers.
5186 */
5187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005188ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 buf_T *buf;
5191 win_T *wp, *wpnext;
5192 int split_ret = OK;
5193 int p_ea_save;
5194 int open_wins = 0;
5195 int r;
5196 int count; /* Maximum number of windows to open. */
5197 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005198 int had_tab = cmdmod.tab;
5199 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200
5201 if (eap->addr_count == 0) /* make as many windows as possible */
5202 count = 9999;
5203 else
5204 count = eap->line2; /* make as many windows as specified */
5205 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5206 all = FALSE;
5207 else
5208 all = TRUE;
5209
5210 setpcmark();
5211
5212#ifdef FEAT_GUI
5213 need_mouse_correct = TRUE;
5214#endif
5215
5216 /*
5217 * Close superfluous windows (two windows for the same buffer).
5218 * Also close windows that are not full-width.
5219 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005220 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005221 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005222 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005224 tpnext = curtab->tp_next;
5225 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005227 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005228 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005229 || ((cmdmod.split & WSP_VERT)
5230 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005231 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005232 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005233 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaar362ce482012-06-06 19:02:45 +02005234#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005235 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005236#endif
5237 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005238 {
5239 win_close(wp, FALSE);
5240#ifdef FEAT_AUTOCMD
5241 wpnext = firstwin; /* just in case an autocommand does
5242 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005243 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005244 open_wins = 0;
5245#endif
5246 }
5247 else
5248 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005250
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005251 /* Without the ":tab" modifier only do the current tab page. */
5252 if (had_tab == 0 || tpnext == NULL)
5253 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005254 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
5256
5257 /*
5258 * Go through the buffer list. When a buffer doesn't have a window yet,
5259 * open one. Otherwise move the window to the right position.
5260 * Watch out for autocommands that delete buffers or windows!
5261 */
5262#ifdef FEAT_AUTOCMD
5263 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5264 ++autocmd_no_enter;
5265#endif
5266 win_enter(lastwin, FALSE);
5267#ifdef FEAT_AUTOCMD
5268 ++autocmd_no_leave;
5269#endif
5270 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5271 {
5272 /* Check if this buffer needs a window */
5273 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5274 continue;
5275
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005276 if (had_tab != 0)
5277 {
5278 /* With the ":tab" modifier don't move the window. */
5279 if (buf->b_nwindows > 0)
5280 wp = lastwin; /* buffer has a window, skip it */
5281 else
5282 wp = NULL;
5283 }
5284 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005285 {
5286 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005287 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005288 if (wp->w_buffer == buf)
5289 break;
5290 /* If the buffer already has a window, move it */
5291 if (wp != NULL)
5292 win_move_after(wp, curwin);
5293 }
5294
5295 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005297#ifdef FEAT_AUTOCMD
5298 bufref_T bufref;
5299
5300 set_bufref(&bufref, buf);
5301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 /* Split the window and put the buffer in it */
5303 p_ea_save = p_ea;
5304 p_ea = TRUE; /* use space from all windows */
5305 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5306 ++open_wins;
5307 p_ea = p_ea_save;
5308 if (split_ret == FAIL)
5309 continue;
5310
5311 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005312#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 swap_exists_action = SEA_DIALOG;
5314#endif
5315 set_curbuf(buf, DOBUF_GOTO);
5316#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005317 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005319 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005320#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 break;
5324 }
5325#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005326#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 if (swap_exists_action == SEA_QUIT)
5328 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005329# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5330 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005331
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005332 /* Reset the error/interrupt/exception state here so that
5333 * aborting() returns FALSE when closing a window. */
5334 enter_cleanup(&cs);
5335# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005336
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005337 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 win_close(curwin, TRUE);
5339 --open_wins;
5340 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005341 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005342
5343# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5344 /* Restore the error/interrupt/exception state if not
5345 * discarded by a new aborting error, interrupt, or uncaught
5346 * exception. */
5347 leave_cleanup(&cs);
5348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
5350 else
5351 handle_swap_exists(NULL);
5352#endif
5353 }
5354
5355 ui_breakcheck();
5356 if (got_int)
5357 {
5358 (void)vgetc(); /* only break the file loading, not the rest */
5359 break;
5360 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005361#ifdef FEAT_EVAL
5362 /* Autocommands deleted the buffer or aborted script processing!!! */
5363 if (aborting())
5364 break;
5365#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005366 /* When ":tab" was used open a new tab for a new window repeatedly. */
5367 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5368 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370#ifdef FEAT_AUTOCMD
5371 --autocmd_no_enter;
5372#endif
5373 win_enter(firstwin, FALSE); /* back to first window */
5374#ifdef FEAT_AUTOCMD
5375 --autocmd_no_leave;
5376#endif
5377
5378 /*
5379 * Close superfluous windows.
5380 */
5381 for (wp = lastwin; open_wins > count; )
5382 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005383 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 || autowrite(wp->w_buffer, FALSE) == OK);
5385#ifdef FEAT_AUTOCMD
5386 if (!win_valid(wp))
5387 {
5388 /* BufWrite Autocommands made the window invalid, start over */
5389 wp = lastwin;
5390 }
5391 else
5392#endif
5393 if (r)
5394 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005395 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 --open_wins;
5397 wp = lastwin;
5398 }
5399 else
5400 {
5401 wp = wp->w_prev;
5402 if (wp == NULL)
5403 break;
5404 }
5405 }
5406}
5407# endif /* FEAT_LISTCMDS */
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005410static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412/*
5413 * do_modelines() - process mode lines for the current file
5414 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005415 * "flags" can be:
5416 * OPT_WINONLY only set options local to window
5417 * OPT_NOWIN don't set options local to window
5418 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 * Returns immediately if the "ml" option isn't set.
5420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005422do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005424 linenr_T lnum;
5425 int nmlines;
5426 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427
5428 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5429 return;
5430
5431 /* Disallow recursive entry here. Can happen when executing a modeline
5432 * triggers an autocommand, which reloads modelines with a ":do". */
5433 if (entered)
5434 return;
5435
5436 ++entered;
5437 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5438 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005439 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 nmlines = 0;
5441
5442 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5443 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005444 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 nmlines = 0;
5446 --entered;
5447}
5448
5449#include "version.h" /* for version number */
5450
5451/*
5452 * chk_modeline() - check a single line for a mode string
5453 * Return FAIL if an error encountered.
5454 */
5455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005456chk_modeline(
5457 linenr_T lnum,
5458 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 char_u *s;
5461 char_u *e;
5462 char_u *linecopy; /* local copy of any modeline found */
5463 int prev;
5464 int vers;
5465 int end;
5466 int retval = OK;
5467 char_u *save_sourcing_name;
5468 linenr_T save_sourcing_lnum;
5469#ifdef FEAT_EVAL
5470 scid_T save_SID;
5471#endif
5472
5473 prev = -1;
5474 for (s = ml_get(lnum); *s != NUL; ++s)
5475 {
5476 if (prev == -1 || vim_isspace(prev))
5477 {
5478 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5479 || STRNCMP(s, "vi:", (size_t)3) == 0)
5480 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005481 /* Accept both "vim" and "Vim". */
5482 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
5484 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5485 e = s + 4;
5486 else
5487 e = s + 3;
5488 vers = getdigits(&e);
5489 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005490 && (s[0] != 'V'
5491 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 && (s[3] == ':'
5493 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5494 || (VIM_VERSION_100 < vers && s[3] == '<')
5495 || (VIM_VERSION_100 > vers && s[3] == '>')
5496 || (VIM_VERSION_100 == vers && s[3] == '=')))
5497 break;
5498 }
5499 }
5500 prev = *s;
5501 }
5502
5503 if (*s)
5504 {
5505 do /* skip over "ex:", "vi:" or "vim:" */
5506 ++s;
5507 while (s[-1] != ':');
5508
5509 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5510 if (linecopy == NULL)
5511 return FAIL;
5512
5513 save_sourcing_lnum = sourcing_lnum;
5514 save_sourcing_name = sourcing_name;
5515 sourcing_lnum = lnum; /* prepare for emsg() */
5516 sourcing_name = (char_u *)"modelines";
5517
5518 end = FALSE;
5519 while (end == FALSE)
5520 {
5521 s = skipwhite(s);
5522 if (*s == NUL)
5523 break;
5524
5525 /*
5526 * Find end of set command: ':' or end of line.
5527 * Skip over "\:", replacing it with ":".
5528 */
5529 for (e = s; *e != ':' && *e != NUL; ++e)
5530 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005531 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 if (*e == NUL)
5533 end = TRUE;
5534
5535 /*
5536 * If there is a "set" command, require a terminating ':' and
5537 * ignore the stuff after the ':'.
5538 * "vi:set opt opt opt: foo" -- foo not interpreted
5539 * "vi:opt opt opt: foo" -- foo interpreted
5540 * Accept "se" for compatibility with Elvis.
5541 */
5542 if (STRNCMP(s, "set ", (size_t)4) == 0
5543 || STRNCMP(s, "se ", (size_t)3) == 0)
5544 {
5545 if (*e != ':') /* no terminating ':'? */
5546 break;
5547 end = TRUE;
5548 s = vim_strchr(s, ' ') + 1;
5549 }
5550 *e = NUL; /* truncate the set command */
5551
5552 if (*s != NUL) /* skip over an empty "::" */
5553 {
5554#ifdef FEAT_EVAL
5555 save_SID = current_SID;
5556 current_SID = SID_MODELINE;
5557#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005558 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559#ifdef FEAT_EVAL
5560 current_SID = save_SID;
5561#endif
5562 if (retval == FAIL) /* stop if error found */
5563 break;
5564 }
5565 s = e + 1; /* advance to next part */
5566 }
5567
5568 sourcing_lnum = save_sourcing_lnum;
5569 sourcing_name = save_sourcing_name;
5570
5571 vim_free(linecopy);
5572 }
5573 return retval;
5574}
5575
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005576#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005578read_viminfo_bufferlist(
5579 vir_T *virp,
5580 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 char_u *tab;
5583 linenr_T lnum;
5584 colnr_T col;
5585 buf_T *buf;
5586 char_u *sfname;
5587 char_u *xline;
5588
5589 /* Handle long line and escaped characters. */
5590 xline = viminfo_readstring(virp, 1, FALSE);
5591
5592 /* don't read in if there are files on the command-line or if writing: */
5593 if (xline != NULL && !writing && ARGCOUNT == 0
5594 && find_viminfo_parameter('%') != NULL)
5595 {
5596 /* Format is: <fname> Tab <lnum> Tab <col>.
5597 * Watch out for a Tab in the file name, work from the end. */
5598 lnum = 0;
5599 col = 0;
5600 tab = vim_strrchr(xline, '\t');
5601 if (tab != NULL)
5602 {
5603 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005604 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 tab = vim_strrchr(xline, '\t');
5606 if (tab != NULL)
5607 {
5608 *tab++ = '\0';
5609 lnum = atol((char *)tab);
5610 }
5611 }
5612
5613 /* Expand "~/" in the file name at "line + 1" to a full path.
5614 * Then try shortening it by comparing with the current directory */
5615 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005616 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5619 if (buf != NULL) /* just in case... */
5620 {
5621 buf->b_last_cursor.lnum = lnum;
5622 buf->b_last_cursor.col = col;
5623 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5624 }
5625 }
5626 vim_free(xline);
5627
5628 return viminfo_readline(virp);
5629}
5630
5631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005632write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005636 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005638 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 if (find_viminfo_parameter('%') == NULL)
5641 return;
5642
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005643 /* Without a number -1 is returned: do all buffers. */
5644 max_buffers = get_viminfo_parameter('%');
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005647#define LINE_BUF_LEN (MAXPATHL + 40)
5648 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 if (line == NULL)
5650 return;
5651
Bram Moolenaarf740b292006-02-16 22:11:02 +00005652 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005655 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005656 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
5658 if (buf->b_fname == NULL
5659 || !buf->b_p_bl
5660#ifdef FEAT_QUICKFIX
5661 || bt_quickfix(buf)
5662#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005663#ifdef FEAT_TERMINAL
5664 || bt_terminal(buf)
5665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 || removable(buf->b_ffname))
5667 continue;
5668
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005669 if (max_buffers-- == 0)
5670 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 putc('%', fp);
5672 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005673 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 (long)buf->b_last_cursor.lnum,
5675 buf->b_last_cursor.col);
5676 viminfo_writestring(fp, line);
5677 }
5678 vim_free(line);
5679}
5680#endif
5681
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005682/*
5683 * Return TRUE if "buf" is the quickfix buffer.
5684 */
5685 int
5686bt_quickfix(buf_T *buf)
5687{
5688 return buf != NULL && buf->b_p_bt[0] == 'q';
5689}
5690
5691/*
5692 * Return TRUE if "buf" is a terminal buffer.
5693 */
5694 int
5695bt_terminal(buf_T *buf)
5696{
5697 return buf != NULL && buf->b_p_bt[0] == 't';
5698}
5699
5700/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005701 * Return TRUE if "buf" is a help buffer.
5702 */
5703 int
5704bt_help(buf_T *buf)
5705{
5706 return buf != NULL && buf->b_help;
5707}
5708
5709/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005710 * Return TRUE if "buf" is a "nofile", "acwrite" or "terminal" buffer.
5711 * This means the buffer name is not a file name.
5712 */
5713 int
5714bt_nofile(buf_T *buf)
5715{
5716 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5717 || buf->b_p_bt[0] == 'a'
5718 || buf->b_p_bt[0] == 't');
5719}
5720
5721/*
5722 * Return TRUE if "buf" is a "nowrite", "nofile" or "terminal" buffer.
5723 */
5724 int
5725bt_dontwrite(buf_T *buf)
5726{
5727 return buf != NULL && (buf->b_p_bt[0] == 'n' || buf->b_p_bt[0] == 't');
5728}
5729
5730 int
5731bt_dontwrite_msg(buf_T *buf)
5732{
5733 if (bt_dontwrite(buf))
5734 {
5735 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5736 return TRUE;
5737 }
5738 return FALSE;
5739}
5740
5741/*
5742 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5743 * and 'bufhidden'.
5744 */
5745 int
5746buf_hide(buf_T *buf)
5747{
5748 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5749 switch (buf->b_p_bh[0])
5750 {
5751 case 'u': /* "unload" */
5752 case 'w': /* "wipe" */
5753 case 'd': return FALSE; /* "delete" */
5754 case 'h': return TRUE; /* "hide" */
5755 }
5756 return (p_hid || cmdmod.hide);
5757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758
5759/*
5760 * Return special buffer name.
5761 * Returns NULL when the buffer has a normal file name.
5762 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005763 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005764buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005766#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005768 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005769 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005770 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005771
5772 /*
5773 * For location list window, w_llist_ref points to the location list.
5774 * For quickfix window, w_llist_ref is NULL.
5775 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005776 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005777 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005778 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005779 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005784 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 if (bt_nofile(buf))
5786 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005787#ifdef FEAT_TERMINAL
5788 if (buf->b_term != NULL)
5789 return term_get_status_text(buf->b_term);
5790#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005791 if (buf->b_fname != NULL)
5792 return buf->b_fname;
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005793 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005795
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005797 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 return NULL;
5799}
5800
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005801#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005802 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5803 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005804# define SWITCH_TO_WIN
5805
5806/*
5807 * Find a window that contains "buf" and switch to it.
5808 * If there is no such window, use the current window and change "curbuf".
5809 * Caller must initialize save_curbuf to NULL.
5810 * restore_win_for_buf() MUST be called later!
5811 */
5812 void
5813switch_to_win_for_buf(
5814 buf_T *buf,
5815 win_T **save_curwinp,
5816 tabpage_T **save_curtabp,
5817 bufref_T *save_curbuf)
5818{
5819 win_T *wp;
5820 tabpage_T *tp;
5821
5822 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5823 switch_buffer(save_curbuf, buf);
5824 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5825 {
5826 restore_win(*save_curwinp, *save_curtabp, TRUE);
5827 switch_buffer(save_curbuf, buf);
5828 }
5829}
5830
5831 void
5832restore_win_for_buf(
5833 win_T *save_curwin,
5834 tabpage_T *save_curtab,
5835 bufref_T *save_curbuf)
5836{
5837 if (save_curbuf->br_buf == NULL)
5838 restore_win(save_curwin, save_curtab, TRUE);
5839 else
5840 restore_buffer(save_curbuf);
5841}
5842#endif
5843
Bram Moolenaar4033c552017-09-16 20:54:51 +02005844#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005845/*
5846 * Find a window for buffer "buf".
5847 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5848 * If not found FAIL is returned.
5849 */
5850 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005851find_win_for_buf(
5852 buf_T *buf,
5853 win_T **wp,
5854 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005855{
5856 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5857 if ((*wp)->w_buffer == buf)
5858 goto win_found;
5859 return FAIL;
5860win_found:
5861 return OK;
5862}
5863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
5865#if defined(FEAT_SIGNS) || defined(PROTO)
5866/*
5867 * Insert the sign into the signlist.
5868 */
5869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005870insert_sign(
5871 buf_T *buf, /* buffer to store sign in */
5872 signlist_T *prev, /* previous sign entry */
5873 signlist_T *next, /* next sign entry */
5874 int id, /* sign ID */
5875 linenr_T lnum, /* line number which gets the mark */
5876 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
5878 signlist_T *newsign;
5879
5880 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5881 if (newsign != NULL)
5882 {
5883 newsign->id = id;
5884 newsign->lnum = lnum;
5885 newsign->typenr = typenr;
5886 newsign->next = next;
5887#ifdef FEAT_NETBEANS_INTG
5888 newsign->prev = prev;
5889 if (next != NULL)
5890 next->prev = newsign;
5891#endif
5892
5893 if (prev == NULL)
5894 {
5895 /* When adding first sign need to redraw the windows to create the
5896 * column for signs. */
5897 if (buf->b_signlist == NULL)
5898 {
5899 redraw_buf_later(buf, NOT_VALID);
5900 changed_cline_bef_curs();
5901 }
5902
5903 /* first sign in signlist */
5904 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005905#ifdef FEAT_NETBEANS_INTG
5906 if (netbeans_active())
5907 buf->b_has_sign_column = TRUE;
5908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
5910 else
5911 prev->next = newsign;
5912 }
5913}
5914
5915/*
5916 * Add the sign into the signlist. Find the right spot to do it though.
5917 */
5918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005919buf_addsign(
5920 buf_T *buf, /* buffer to store sign in */
5921 int id, /* sign ID */
5922 linenr_T lnum, /* line number which gets the mark */
5923 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 signlist_T *sign; /* a sign in the signlist */
5926 signlist_T *prev; /* the previous sign */
5927
5928 prev = NULL;
5929 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5930 {
5931 if (lnum == sign->lnum && id == sign->id)
5932 {
5933 sign->typenr = typenr;
5934 return;
5935 }
5936 else if (
5937#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5938 id < 0 &&
5939#endif
5940 lnum < sign->lnum)
5941 {
5942#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5943 /* XXX - GRP: Is this because of sign slide problem? Or is it
5944 * really needed? Or is it because we allow multiple signs per
5945 * line? If so, should I add that feature to FEAT_SIGNS?
5946 */
5947 while (prev != NULL && prev->lnum == lnum)
5948 prev = prev->prev;
5949 if (prev == NULL)
5950 sign = buf->b_signlist;
5951 else
5952 sign = prev->next;
5953#endif
5954 insert_sign(buf, prev, sign, id, lnum, typenr);
5955 return;
5956 }
5957 prev = sign;
5958 }
5959#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5960 /* XXX - GRP: See previous comment */
5961 while (prev != NULL && prev->lnum == lnum)
5962 prev = prev->prev;
5963 if (prev == NULL)
5964 sign = buf->b_signlist;
5965 else
5966 sign = prev->next;
5967#endif
5968 insert_sign(buf, prev, sign, id, lnum, typenr);
5969
5970 return;
5971}
5972
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005973/*
5974 * For an existing, placed sign "markId" change the type to "typenr".
5975 * Returns the line number of the sign, or zero if the sign is not found.
5976 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005977 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005978buf_change_sign_type(
5979 buf_T *buf, /* buffer to store sign in */
5980 int markId, /* sign ID */
5981 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
5983 signlist_T *sign; /* a sign in the signlist */
5984
5985 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5986 {
5987 if (sign->id == markId)
5988 {
5989 sign->typenr = typenr;
5990 return sign->lnum;
5991 }
5992 }
5993
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005994 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995}
5996
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998buf_getsigntype(
5999 buf_T *buf,
6000 linenr_T lnum,
6001 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 signlist_T *sign; /* a sign in a b_signlist */
6004
6005 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6006 if (sign->lnum == lnum
6007 && (type == SIGN_ANY
6008# ifdef FEAT_SIGN_ICONS
6009 || (type == SIGN_ICON
6010 && sign_get_image(sign->typenr) != NULL)
6011# endif
6012 || (type == SIGN_TEXT
6013 && sign_get_text(sign->typenr) != NULL)
6014 || (type == SIGN_LINEHL
6015 && sign_get_attr(sign->typenr, TRUE) != 0)))
6016 return sign->typenr;
6017 return 0;
6018}
6019
6020
6021 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022buf_delsign(
6023 buf_T *buf, /* buffer sign is stored in */
6024 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
6026 signlist_T **lastp; /* pointer to pointer to current sign */
6027 signlist_T *sign; /* a sign in a b_signlist */
6028 signlist_T *next; /* the next sign in a b_signlist */
6029 linenr_T lnum; /* line number whose sign was deleted */
6030
6031 lastp = &buf->b_signlist;
6032 lnum = 0;
6033 for (sign = buf->b_signlist; sign != NULL; sign = next)
6034 {
6035 next = sign->next;
6036 if (sign->id == id)
6037 {
6038 *lastp = next;
6039#ifdef FEAT_NETBEANS_INTG
6040 if (next != NULL)
6041 next->prev = sign->prev;
6042#endif
6043 lnum = sign->lnum;
6044 vim_free(sign);
6045 break;
6046 }
6047 else
6048 lastp = &sign->next;
6049 }
6050
6051 /* When deleted the last sign need to redraw the windows to remove the
6052 * sign column. */
6053 if (buf->b_signlist == NULL)
6054 {
6055 redraw_buf_later(buf, NOT_VALID);
6056 changed_cline_bef_curs();
6057 }
6058
6059 return lnum;
6060}
6061
6062
6063/*
6064 * Find the line number of the sign with the requested id. If the sign does
6065 * not exist, return 0 as the line number. This will still let the correct file
6066 * get loaded.
6067 */
6068 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006069buf_findsign(
6070 buf_T *buf, /* buffer to store sign in */
6071 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 signlist_T *sign; /* a sign in the signlist */
6074
6075 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6076 if (sign->id == id)
6077 return sign->lnum;
6078
6079 return 0;
6080}
6081
6082 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006083buf_findsign_id(
6084 buf_T *buf, /* buffer whose sign we are searching for */
6085 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 signlist_T *sign; /* a sign in the signlist */
6088
6089 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6090 if (sign->lnum == lnum)
6091 return sign->id;
6092
6093 return 0;
6094}
6095
6096
6097# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
6098/* see if a given type of sign exists on a specific line */
6099 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100buf_findsigntype_id(
6101 buf_T *buf, /* buffer whose sign we are searching for */
6102 linenr_T lnum, /* line number of sign */
6103 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 signlist_T *sign; /* a sign in the signlist */
6106
6107 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6108 if (sign->lnum == lnum && sign->typenr == typenr)
6109 return sign->id;
6110
6111 return 0;
6112}
6113
6114
6115# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6116/* return the number of icons on the given line */
6117 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 signlist_T *sign; /* a sign in the signlist */
6121 int count = 0;
6122
6123 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6124 if (sign->lnum == lnum)
6125 if (sign_get_image(sign->typenr) != NULL)
6126 count++;
6127
6128 return count;
6129}
6130# endif /* FEAT_SIGN_ICONS */
6131# endif /* FEAT_NETBEANS_INTG */
6132
6133
6134/*
6135 * Delete signs in buffer "buf".
6136 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 signlist_T *next;
6141
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006142 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006143 * sign column. Not when curwin is NULL (this means we're exiting). */
6144 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006145 {
6146 redraw_buf_later(buf, NOT_VALID);
6147 changed_cline_bef_curs();
6148 }
6149
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 while (buf->b_signlist != NULL)
6151 {
6152 next = buf->b_signlist->next;
6153 vim_free(buf->b_signlist);
6154 buf->b_signlist = next;
6155 }
6156}
6157
6158/*
6159 * Delete all signs in all buffers.
6160 */
6161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006162buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
6164 buf_T *buf; /* buffer we are checking for signs */
6165
Bram Moolenaar29323592016-07-24 22:04:11 +02006166 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169}
6170
6171/*
6172 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6173 */
6174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006175sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
6177 buf_T *buf;
6178 signlist_T *p;
6179 char lbuf[BUFSIZ];
6180
6181 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6182 msg_putchar('\n');
6183 if (rbuf == NULL)
6184 buf = firstbuf;
6185 else
6186 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006187 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 {
6189 if (buf->b_signlist != NULL)
6190 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006191 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006192 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 msg_putchar('\n');
6194 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006195 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006197 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6199 MSG_PUTS(lbuf);
6200 msg_putchar('\n');
6201 }
6202 if (rbuf != NULL)
6203 break;
6204 buf = buf->b_next;
6205 }
6206}
6207
6208/*
6209 * Adjust a placed sign for inserted/deleted lines.
6210 */
6211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006212sign_mark_adjust(
6213 linenr_T line1,
6214 linenr_T line2,
6215 long amount,
6216 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217{
6218 signlist_T *sign; /* a sign in a b_signlist */
6219
6220 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6221 {
6222 if (sign->lnum >= line1 && sign->lnum <= line2)
6223 {
6224 if (amount == MAXLNUM)
6225 sign->lnum = line1;
6226 else
6227 sign->lnum += amount;
6228 }
6229 else if (sign->lnum > line2)
6230 sign->lnum += amount_after;
6231 }
6232}
6233#endif /* FEAT_SIGNS */
6234
6235/*
6236 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6237 */
6238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006239set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 if (on != curbuf->b_p_bl)
6242 {
6243 curbuf->b_p_bl = on;
6244#ifdef FEAT_AUTOCMD
6245 if (on)
6246 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6247 else
6248 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6249#endif
6250 }
6251}
6252
6253/*
6254 * Read the file for "buf" again and check if the contents changed.
6255 * Return TRUE if it changed or this could not be checked.
6256 */
6257 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006258buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 buf_T *newbuf;
6261 int differ = TRUE;
6262 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 exarg_T ea;
6265
6266 /* Allocate a buffer without putting it in the buffer list. */
6267 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6268 if (newbuf == NULL)
6269 return TRUE;
6270
6271 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6272 if (prep_exarg(&ea, buf) == FAIL)
6273 {
6274 wipe_buffer(newbuf, FALSE);
6275 return TRUE;
6276 }
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 /* set curwin/curbuf to buf and save a few things */
6279 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
Bram Moolenaar4770d092006-01-12 23:22:24 +00006281 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 && readfile(buf->b_ffname, buf->b_fname,
6283 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6284 &ea, READ_NEW | READ_DUMMY) == OK)
6285 {
6286 /* compare the two files line by line */
6287 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6288 {
6289 differ = FALSE;
6290 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6291 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6292 {
6293 differ = TRUE;
6294 break;
6295 }
6296 }
6297 }
6298 vim_free(ea.cmd);
6299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 /* restore curwin/curbuf and a few other things */
6301 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303 if (curbuf != newbuf) /* safety check */
6304 wipe_buffer(newbuf, FALSE);
6305
6306 return differ;
6307}
6308
6309/*
6310 * Wipe out a buffer and decrement the last buffer number if it was used for
6311 * this buffer. Call this to wipe out a temp buffer that does not contain any
6312 * marks.
6313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006315wipe_buffer(
6316 buf_T *buf,
6317 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
6319 if (buf->b_fnum == top_file_num - 1)
6320 --top_file_num;
6321
6322#ifdef FEAT_AUTOCMD
6323 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006324 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006326 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327#ifdef FEAT_AUTOCMD
6328 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006329 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330#endif
6331}