blob: 0d69b9d66b813cce295261aab46865df6f4d00f5 [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
596 /* Return when a window is displaying the buffer or when it's not
597 * unloaded. */
598 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 /* Always remove the buffer when there is no file name. */
602 if (buf->b_ffname == NULL)
603 del_buf = TRUE;
604
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200605 /* When closing the current buffer stop Visual mode before freeing
606 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200607 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200608#if defined(EXITFREE)
609 && !entered_free_all_mem
610#endif
611 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200612 end_visual_mode();
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 /*
615 * Free all things allocated for this buffer.
616 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
617 */
618#ifdef FEAT_AUTOCMD
619 /* Remember if we are closing the current buffer. Restore the number of
620 * windows, so that autocommands in buf_freeall() don't get confused. */
621 is_curbuf = (buf == curbuf);
622 buf->b_nwindows = nwindows;
623#endif
624
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200625 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627#ifdef FEAT_AUTOCMD
628 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200629 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 return;
631# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000632 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 return;
634# endif
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 /*
637 * It's possible that autocommands change curbuf to the one being deleted.
638 * This might cause the previous curbuf to be deleted unexpectedly. But
639 * in some cases it's OK to delete the curbuf, because a new one is
640 * obtained anyway. Therefore only return if curbuf changed to the
641 * deleted buffer.
642 */
643 if (buf == curbuf && !is_curbuf)
644 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200645
Bram Moolenaar4033c552017-09-16 20:54:51 +0200646 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200647 win->w_buffer = NULL; /* make sure we don't use the buffer now */
648
649 /* Autocommands may have opened or closed windows for this buffer.
650 * Decrement the count for the close we do here. */
651 if (buf->b_nwindows > 0)
652 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653#endif
654
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000655 /* Change directories when the 'acd' option is set. */
656 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
658 /*
659 * Remove the buffer from the list.
660 */
661 if (wipe_buf)
662 {
663#ifdef FEAT_SUN_WORKSHOP
664 if (usingSunWorkShop)
665 workshop_file_closed_lineno((char *)buf->b_ffname,
666 (int)buf->b_last_cursor.lnum);
667#endif
668 vim_free(buf->b_ffname);
669 vim_free(buf->b_sfname);
670 if (buf->b_prev == NULL)
671 firstbuf = buf->b_next;
672 else
673 buf->b_prev->b_next = buf->b_next;
674 if (buf->b_next == NULL)
675 lastbuf = buf->b_prev;
676 else
677 buf->b_next->b_prev = buf->b_prev;
678 free_buffer(buf);
679 }
680 else
681 {
682 if (del_buf)
683 {
684 /* Free all internal variables and reset option values, to make
685 * ":bdel" compatible with Vim 5.7. */
686 free_buffer_stuff(buf, TRUE);
687
688 /* Make it look like a new buffer. */
689 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
690
691 /* Init the options when loaded again. */
692 buf->b_p_initialized = FALSE;
693 }
694 buf_clear_file(buf);
695 if (del_buf)
696 buf->b_p_bl = FALSE;
697 }
698}
699
700/*
701 * Make buffer not contain a file.
702 */
703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100704buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
706 buf->b_ml.ml_line_count = 1;
707 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 buf->b_p_eol = TRUE;
710 buf->b_start_eol = TRUE;
711#ifdef FEAT_MBYTE
712 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000713 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#endif
715 buf->b_ml.ml_mfp = NULL;
716 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
717#ifdef FEAT_NETBEANS_INTG
718 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#endif
720}
721
722/*
723 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200724 * the file. Careful: get here with "curwin" NULL when exiting.
725 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200726 * BFA_DEL buffer is going to be deleted
727 * BFA_WIPE buffer is going to be wiped out
728 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100731buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733#ifdef FEAT_AUTOCMD
734 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200735 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200736 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200737 win_T *the_curwin = curwin;
738 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar5a497892016-09-03 16:29:04 +0200740 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200741 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200742 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200743 if (buf->b_ml.ml_mfp != NULL)
744 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200745 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
746 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200747 && !bufref_valid(&bufref))
748 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200749 return;
750 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200751 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200753 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
754 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200755 && !bufref_valid(&bufref))
756 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 return;
758 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200759 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200761 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
762 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200763 && !bufref_valid(&bufref))
764 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 return;
766 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200767 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200768
Bram Moolenaar5a497892016-09-03 16:29:04 +0200769 /* If the buffer was in curwin and the window has changed, go back to that
770 * window, if it still exists. This avoids that ":edit x" triggering a
771 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
772 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
773 {
774 block_autocmds();
775 goto_tabpage_win(the_curtab, the_curwin);
776 unblock_autocmds();
777 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000780 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 return;
782# endif
783
784 /*
785 * It's possible that autocommands change curbuf to the one being deleted.
786 * This might cause curbuf to be deleted unexpectedly. But in some cases
787 * it's OK to delete the curbuf, because a new one is obtained anyway.
788 * Therefore only return if curbuf changed to the deleted buffer.
789 */
790 if (buf == curbuf && !is_curbuf)
791 return;
792#endif
793#ifdef FEAT_DIFF
794 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
795#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200796#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100797 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200798 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100799 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200800#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000801
802#ifdef FEAT_FOLDING
803 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000804 {
805 win_T *win;
806 tabpage_T *tp;
807
808 FOR_ALL_TAB_WINDOWS(tp, win)
809 if (win->w_buffer == buf)
810 clearFolding(win);
811 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000812#endif
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#ifdef FEAT_TCL
815 tcl_buffer_free(buf);
816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 ml_close(buf, TRUE); /* close and delete the memline/memfile */
818 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200819 if ((flags & BFA_KEEP_UNDO) == 0)
820 {
821 u_blockfree(buf); /* free the memory allocated for undo */
822 u_clearall(buf); /* reset all undo information */
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200825 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000827 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828}
829
830/*
831 * Free a buffer structure and the things it contains related to the buffer
832 * itself (not the file, that must have been done already).
833 */
834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200837 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200839#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200840 /* b:changedtick uses an item in buf_T, remove it now */
841 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200842 unref_var_dict(buf->b_vars);
843#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200844#ifdef FEAT_LUA
845 lua_buffer_free(buf);
846#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000847#ifdef FEAT_MZSCHEME
848 mzscheme_buffer_free(buf);
849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#ifdef FEAT_PERL
851 perl_buf_free(buf);
852#endif
853#ifdef FEAT_PYTHON
854 python_buffer_free(buf);
855#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200856#ifdef FEAT_PYTHON3
857 python3_buffer_free(buf);
858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859#ifdef FEAT_RUBY
860 ruby_buffer_free(buf);
861#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200862#ifdef FEAT_JOB_CHANNEL
863 channel_buffer_free(buf);
864#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200865#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200866 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200867#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200868
869 buf_hashtab_remove(buf);
870
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200871#ifdef FEAT_AUTOCMD
872 aubuflocal_remove(buf);
873
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200874 if (autocmd_busy)
875 {
876 /* Do not free the buffer structure while autocommands are executing,
877 * it's still needed. Free it when autocmd_busy is reset. */
878 buf->b_next = au_pending_free_buf;
879 au_pending_free_buf = buf;
880 }
881 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000882#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200883 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884}
885
886/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100887 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100888 */
889 static void
890init_changedtick(buf_T *buf)
891{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100892 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100893
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100894 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
895 di->di_tv.v_type = VAR_NUMBER;
896 di->di_tv.v_lock = VAR_FIXED;
897 di->di_tv.vval.v_number = 0;
898
Bram Moolenaar92769c32017-02-25 15:41:37 +0100899#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100900 STRCPY(buf->b_ct_di.di_key, "changedtick");
901 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100902#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100903}
904
905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
907 */
908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100909free_buffer_stuff(
910 buf_T *buf,
911 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
913 if (free_options)
914 {
915 clear_wininfo(buf); /* including window-local options */
916 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200917#ifdef FEAT_SPELL
918 ga_clear(&buf->b_s.b_langp);
919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100922 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100923 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100924
925 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
926 hash_init(&buf->b_vars->dv_hashtab);
927 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100928 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#endif
931#ifdef FEAT_USR_CMDS
932 uc_clear(&buf->b_ucmds); /* clear local user commands */
933#endif
934#ifdef FEAT_SIGNS
935 buf_delete_signs(buf); /* delete any signs */
936#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000937#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200938 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#ifdef FEAT_LOCALMAP
941 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
942 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
943#endif
944#ifdef FEAT_MBYTE
945 vim_free(buf->b_start_fenc);
946 buf->b_start_fenc = NULL;
947#endif
948}
949
950/*
951 * Free the b_wininfo list for buffer "buf".
952 */
953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100954clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
956 wininfo_T *wip;
957
958 while (buf->b_wininfo != NULL)
959 {
960 wip = buf->b_wininfo;
961 buf->b_wininfo = wip->wi_next;
962 if (wip->wi_optset)
963 {
964 clear_winopt(&wip->wi_opt);
965#ifdef FEAT_FOLDING
966 deleteFoldRecurse(&wip->wi_folds);
967#endif
968 }
969 vim_free(wip);
970 }
971}
972
973#if defined(FEAT_LISTCMDS) || defined(PROTO)
974/*
975 * Go to another buffer. Handles the result of the ATTENTION dialog.
976 */
977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100978goto_buffer(
979 exarg_T *eap,
980 int start,
981 int dir,
982 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
Bram Moolenaar4033c552017-09-16 20:54:51 +0200984# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200985 bufref_T old_curbuf;
986
987 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
989 swap_exists_action = SEA_DIALOG;
990# endif
991 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
992 start, dir, count, eap->forceit);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200993# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
995 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000996# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
997 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000998
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000999 /* Reset the error/interrupt/exception state here so that
1000 * aborting() returns FALSE when closing a window. */
1001 enter_cleanup(&cs);
1002# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001003
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001004 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 win_close(curwin, TRUE);
1006 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001007 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001008
1009# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1010 /* Restore the error/interrupt/exception state if not discarded by a
1011 * new aborting error, interrupt, or uncaught exception. */
1012 leave_cleanup(&cs);
1013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001016 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017# endif
1018}
1019#endif
1020
Bram Moolenaare64ac772005-12-07 20:54:59 +00001021#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022/*
1023 * Handle the situation of swap_exists_action being set.
1024 * It is allowed for "old_curbuf" to be NULL or invalid.
1025 */
1026 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001027handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001029# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1030 cleanup_T cs;
1031# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001032#ifdef FEAT_SYN_HL
1033 long old_tw = curbuf->b_p_tw;
1034#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001035 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (swap_exists_action == SEA_QUIT)
1038 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001039# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1040 /* Reset the error/interrupt/exception state here so that
1041 * aborting() returns FALSE when closing a buffer. */
1042 enter_cleanup(&cs);
1043# endif
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 /* User selected Quit at ATTENTION prompt. Go back to previous
1046 * buffer. If that buffer is gone or the same as the current one,
1047 * open a new, empty buffer. */
1048 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001049 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001050 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001051 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1052 || old_curbuf->br_buf == curbuf)
1053 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1054 else
1055 buf = old_curbuf->br_buf;
1056 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001057 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001058 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001059#ifdef FEAT_SYN_HL
1060 if (old_tw != curbuf->b_p_tw)
1061 check_colorcolumn(curwin);
1062#endif
1063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001065
1066# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1067 /* Restore the error/interrupt/exception state if not discarded by a
1068 * new aborting error, interrupt, or uncaught exception. */
1069 leave_cleanup(&cs);
1070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
1072 else if (swap_exists_action == SEA_RECOVER)
1073 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001074# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1075 /* Reset the error/interrupt/exception state here so that
1076 * aborting() returns FALSE when closing a buffer. */
1077 enter_cleanup(&cs);
1078# endif
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 /* User selected Recover at ATTENTION prompt. */
1081 msg_scroll = TRUE;
1082 ml_recover();
1083 MSG_PUTS("\n"); /* don't overwrite the last message */
1084 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001085 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001086
1087# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1088 /* Restore the error/interrupt/exception state if not discarded by a
1089 * new aborting error, interrupt, or uncaught exception. */
1090 leave_cleanup(&cs);
1091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
1093 swap_exists_action = SEA_NONE;
1094}
1095#endif
1096
1097#if defined(FEAT_LISTCMDS) || defined(PROTO)
1098/*
1099 * do_bufdel() - delete or unload buffer(s)
1100 *
1101 * addr_count == 0: ":bdel" - delete current buffer
1102 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1103 * buffer "end_bnr", then any other arguments.
1104 * addr_count == 2: ":N,N bdel" - delete buffers in range
1105 *
1106 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1107 * DOBUF_DEL (":bdel")
1108 *
1109 * Returns error message or NULL
1110 */
1111 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001112do_bufdel(
1113 int command,
1114 char_u *arg, /* pointer to extra arguments */
1115 int addr_count,
1116 int start_bnr, /* first buffer number in a range */
1117 int end_bnr, /* buffer nr or last buffer nr in a range */
1118 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
1120 int do_current = 0; /* delete current buffer? */
1121 int deleted = 0; /* number of buffers deleted */
1122 char_u *errormsg = NULL; /* return value */
1123 int bnr; /* buffer number */
1124 char_u *p;
1125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (addr_count == 0)
1127 {
1128 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1129 }
1130 else
1131 {
1132 if (addr_count == 2)
1133 {
1134 if (*arg) /* both range and argument is not allowed */
1135 return (char_u *)_(e_trailing);
1136 bnr = start_bnr;
1137 }
1138 else /* addr_count == 1 */
1139 bnr = end_bnr;
1140
1141 for ( ;!got_int; ui_breakcheck())
1142 {
1143 /*
1144 * delete the current buffer last, otherwise when the
1145 * current buffer is deleted, the next buffer becomes
1146 * the current one and will be loaded, which may then
1147 * also be deleted, etc.
1148 */
1149 if (bnr == curbuf->b_fnum)
1150 do_current = bnr;
1151 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1152 forceit) == OK)
1153 ++deleted;
1154
1155 /*
1156 * find next buffer number to delete/unload
1157 */
1158 if (addr_count == 2)
1159 {
1160 if (++bnr > end_bnr)
1161 break;
1162 }
1163 else /* addr_count == 1 */
1164 {
1165 arg = skipwhite(arg);
1166 if (*arg == NUL)
1167 break;
1168 if (!VIM_ISDIGIT(*arg))
1169 {
1170 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001171 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1172 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (bnr < 0) /* failed */
1174 break;
1175 arg = p;
1176 }
1177 else
1178 bnr = getdigits(&arg);
1179 }
1180 }
1181 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1182 FORWARD, do_current, forceit) == OK)
1183 ++deleted;
1184
1185 if (deleted == 0)
1186 {
1187 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001188 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001190 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001192 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 errormsg = IObuff;
1194 }
1195 else if (deleted >= p_report)
1196 {
1197 if (command == DOBUF_UNLOAD)
1198 {
1199 if (deleted == 1)
1200 MSG(_("1 buffer unloaded"));
1201 else
1202 smsg((char_u *)_("%d buffers unloaded"), deleted);
1203 }
1204 else if (command == DOBUF_DEL)
1205 {
1206 if (deleted == 1)
1207 MSG(_("1 buffer deleted"));
1208 else
1209 smsg((char_u *)_("%d buffers deleted"), deleted);
1210 }
1211 else
1212 {
1213 if (deleted == 1)
1214 MSG(_("1 buffer wiped out"));
1215 else
1216 smsg((char_u *)_("%d buffers wiped out"), deleted);
1217 }
1218 }
1219 }
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
1222 return errormsg;
1223}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001224#endif /* FEAT_LISTCMDS */
1225
1226#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1227 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001229static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001230
1231/*
1232 * Make the current buffer empty.
1233 * Used when it is wiped out and it's the last buffer.
1234 */
1235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001236empty_curbuf(
1237 int close_others,
1238 int forceit,
1239 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001240{
1241 int retval;
1242 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001243 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001244
1245 if (action == DOBUF_UNLOAD)
1246 {
1247 EMSG(_("E90: Cannot unload last buffer"));
1248 return FAIL;
1249 }
1250
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001251 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001252 if (close_others)
1253 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001254 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001255
1256 setpcmark();
1257 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1258 forceit ? ECMD_FORCEIT : 0, curwin);
1259
1260 /*
1261 * do_ecmd() may create a new buffer, then we have to delete
1262 * the old one. But do_ecmd() may have done that already, check
1263 * if the buffer still exists.
1264 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001265 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001266 close_buffer(NULL, buf, action, FALSE);
1267 if (!close_others)
1268 need_fileinfo = FALSE;
1269 return retval;
1270}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271/*
1272 * Implementation of the commands for the buffer list.
1273 *
1274 * action == DOBUF_GOTO go to specified buffer
1275 * action == DOBUF_SPLIT split window and go to specified buffer
1276 * action == DOBUF_UNLOAD unload specified buffer(s)
1277 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1278 * action == DOBUF_WIPE delete specified buffer(s) really
1279 *
1280 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1281 * start == DOBUF_FIRST go to "count" buffer from first buffer
1282 * start == DOBUF_LAST go to "count" buffer from last buffer
1283 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1284 *
1285 * Return FAIL or OK.
1286 */
1287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001288do_buffer(
1289 int action,
1290 int start,
1291 int dir, /* FORWARD or BACKWARD */
1292 int count, /* buffer number or number of buffers */
1293 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 buf_T *buf;
1296 buf_T *bp;
1297 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1298 || action == DOBUF_WIPE);
1299
1300 switch (start)
1301 {
1302 case DOBUF_FIRST: buf = firstbuf; break;
1303 case DOBUF_LAST: buf = lastbuf; break;
1304 default: buf = curbuf; break;
1305 }
1306 if (start == DOBUF_MOD) /* find next modified buffer */
1307 {
1308 while (count-- > 0)
1309 {
1310 do
1311 {
1312 buf = buf->b_next;
1313 if (buf == NULL)
1314 buf = firstbuf;
1315 }
1316 while (buf != curbuf && !bufIsChanged(buf));
1317 }
1318 if (!bufIsChanged(buf))
1319 {
1320 EMSG(_("E84: No modified buffer found"));
1321 return FAIL;
1322 }
1323 }
1324 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1325 {
1326 while (buf != NULL && buf->b_fnum != count)
1327 buf = buf->b_next;
1328 }
1329 else
1330 {
1331 bp = NULL;
1332 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1333 {
1334 /* remember the buffer where we start, we come back there when all
1335 * buffers are unlisted. */
1336 if (bp == NULL)
1337 bp = buf;
1338 if (dir == FORWARD)
1339 {
1340 buf = buf->b_next;
1341 if (buf == NULL)
1342 buf = firstbuf;
1343 }
1344 else
1345 {
1346 buf = buf->b_prev;
1347 if (buf == NULL)
1348 buf = lastbuf;
1349 }
1350 /* don't count unlisted buffers */
1351 if (unload || buf->b_p_bl)
1352 {
1353 --count;
1354 bp = NULL; /* use this buffer as new starting point */
1355 }
1356 if (bp == buf)
1357 {
1358 /* back where we started, didn't find anything. */
1359 EMSG(_("E85: There is no listed buffer"));
1360 return FAIL;
1361 }
1362 }
1363 }
1364
1365 if (buf == NULL) /* could not find it */
1366 {
1367 if (start == DOBUF_FIRST)
1368 {
1369 /* don't warn when deleting */
1370 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001371 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373 else if (dir == FORWARD)
1374 EMSG(_("E87: Cannot go beyond last buffer"));
1375 else
1376 EMSG(_("E88: Cannot go before first buffer"));
1377 return FAIL;
1378 }
1379
1380#ifdef FEAT_GUI
1381 need_mouse_correct = TRUE;
1382#endif
1383
1384#ifdef FEAT_LISTCMDS
1385 /*
1386 * delete buffer buf from memory and/or the list
1387 */
1388 if (unload)
1389 {
1390 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001391 bufref_T bufref;
1392
1393 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 /* When unloading or deleting a buffer that's already unloaded and
1396 * unlisted: fail silently. */
1397 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1398 return FAIL;
1399
1400 if (!forceit && bufIsChanged(buf))
1401 {
1402#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1403 if ((p_confirm || cmdmod.confirm) && p_write)
1404 {
1405 dialog_changed(buf, FALSE);
1406# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001407 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /* Autocommand deleted buffer, oops! It's not changed
1409 * now. */
1410 return FAIL;
1411# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001412 /* If it's still changed fail silently, the dialog already
1413 * mentioned why it fails. */
1414 if (bufIsChanged(buf))
1415 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418#endif
1419 {
1420 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1421 buf->b_fnum);
1422 return FAIL;
1423 }
1424 }
1425
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001426 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001427 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001428 end_visual_mode();
1429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 /*
1431 * If deleting the last (listed) buffer, make it empty.
1432 * The last (listed) buffer cannot be unloaded.
1433 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001434 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (bp->b_p_bl && bp != buf)
1436 break;
1437 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001438 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /*
1441 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001442 * (unless it's the only window). Repeat this so long as we end up in
1443 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001445 while (buf == curbuf
Bram Moolenaar4033c552017-09-16 20:54:51 +02001446#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001447 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar4033c552017-09-16 20:54:51 +02001448#endif
Bram Moolenaar459ca562016-11-10 18:16:33 +01001449 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001450 {
1451 if (win_close(curwin, FALSE) == FAIL)
1452 break;
1453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
1455 /*
1456 * If the buffer to be deleted is not the current one, delete it here.
1457 */
1458 if (buf != curbuf)
1459 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001460 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001461 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001462 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 return OK;
1464 }
1465
1466 /*
1467 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001468 * There should be another, otherwise it would have been handled
1469 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001470 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 * Then prefer the buffer we most recently visited.
1472 * Else try to find one that is loaded, after the current buffer,
1473 * then before the current buffer.
1474 * Finally use any buffer.
1475 */
1476 buf = NULL; /* selected buffer */
1477 bp = NULL; /* used when no loaded buffer found */
1478#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001479 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1480 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481# ifdef FEAT_JUMPLIST
1482 else
1483# endif
1484#endif
1485#ifdef FEAT_JUMPLIST
1486 if (curwin->w_jumplistlen > 0)
1487 {
1488 int jumpidx;
1489
1490 jumpidx = curwin->w_jumplistidx - 1;
1491 if (jumpidx < 0)
1492 jumpidx = curwin->w_jumplistlen - 1;
1493
1494 forward = jumpidx;
1495 while (jumpidx != curwin->w_jumplistidx)
1496 {
1497 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1498 if (buf != NULL)
1499 {
1500 if (buf == curbuf || !buf->b_p_bl)
1501 buf = NULL; /* skip current and unlisted bufs */
1502 else if (buf->b_ml.ml_mfp == NULL)
1503 {
1504 /* skip unloaded buf, but may keep it for later */
1505 if (bp == NULL)
1506 bp = buf;
1507 buf = NULL;
1508 }
1509 }
1510 if (buf != NULL) /* found a valid buffer: stop searching */
1511 break;
1512 /* advance to older entry in jump list */
1513 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1514 break;
1515 if (--jumpidx < 0)
1516 jumpidx = curwin->w_jumplistlen - 1;
1517 if (jumpidx == forward) /* List exhausted for sure */
1518 break;
1519 }
1520 }
1521#endif
1522
1523 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1524 {
1525 forward = TRUE;
1526 buf = curbuf->b_next;
1527 for (;;)
1528 {
1529 if (buf == NULL)
1530 {
1531 if (!forward) /* tried both directions */
1532 break;
1533 buf = curbuf->b_prev;
1534 forward = FALSE;
1535 continue;
1536 }
1537 /* in non-help buffer, try to skip help buffers, and vv */
1538 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1539 {
1540 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1541 break;
1542 if (bp == NULL) /* remember unloaded buf for later */
1543 bp = buf;
1544 }
1545 if (forward)
1546 buf = buf->b_next;
1547 else
1548 buf = buf->b_prev;
1549 }
1550 }
1551 if (buf == NULL) /* No loaded buffer, use unloaded one */
1552 buf = bp;
1553 if (buf == NULL) /* No loaded buffer, find listed one */
1554 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001555 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (buf->b_p_bl && buf != curbuf)
1557 break;
1558 }
1559 if (buf == NULL) /* Still no buffer, just take one */
1560 {
1561 if (curbuf->b_next != NULL)
1562 buf = curbuf->b_next;
1563 else
1564 buf = curbuf->b_prev;
1565 }
1566 }
1567
Bram Moolenaara02471e2014-01-10 16:43:14 +01001568 if (buf == NULL)
1569 {
1570 /* Autocommands must have wiped out all other buffers. Only option
1571 * now is to make the current buffer empty. */
1572 return empty_curbuf(FALSE, forceit, action);
1573 }
1574
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 /*
1576 * make buf current buffer
1577 */
1578 if (action == DOBUF_SPLIT) /* split window first */
1579 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001580 /* If 'switchbuf' contains "useopen": jump to first window containing
1581 * "buf" if one exists */
1582 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001583 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001584 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001585 * page containing "buf" if one exists */
1586 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 return OK;
1588 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 return FAIL;
1590 }
1591#endif
1592
1593 /* go to current buffer - nothing to do */
1594 if (buf == curbuf)
1595 return OK;
1596
1597 /*
1598 * Check if the current buffer may be abandoned.
1599 */
1600 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1601 {
1602#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1603 if ((p_confirm || cmdmod.confirm) && p_write)
1604 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001605# ifdef FEAT_AUTOCMD
1606 bufref_T bufref;
1607
1608 set_bufref(&bufref, buf);
1609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 dialog_changed(curbuf, FALSE);
1611# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001612 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 /* Autocommand deleted buffer, oops! */
1614 return FAIL;
1615# endif
1616 }
1617 if (bufIsChanged(curbuf))
1618#endif
1619 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001620 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 return FAIL;
1622 }
1623 }
1624
1625 /* Go to the other buffer. */
1626 set_curbuf(buf, action);
1627
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001628#if defined(FEAT_LISTCMDS) \
1629 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001631 {
1632 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635
1636#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1637 if (aborting()) /* autocmds may abort script processing */
1638 return FAIL;
1639#endif
1640
1641 return OK;
1642}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645/*
1646 * Set current buffer to "buf". Executes autocommands and closes current
1647 * buffer. "action" tells how to close the current buffer:
1648 * DOBUF_GOTO free or hide it
1649 * DOBUF_SPLIT nothing
1650 * DOBUF_UNLOAD unload it
1651 * DOBUF_DEL delete it
1652 * DOBUF_WIPE wipe it out
1653 */
1654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001655set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 buf_T *prevbuf;
1658 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1659 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001660#ifdef FEAT_SYN_HL
1661 long old_tw = curbuf->b_p_tw;
1662#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001663 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001666 if (!cmdmod.keepalt)
1667 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001668 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 /* Don't restart Select mode after switching to another buffer. */
1671 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
1673 /* close_windows() or apply_autocmds() may change curbuf */
1674 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001675 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001678 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679# ifdef FEAT_EVAL
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001680 || (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681# else
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001682 || bufref_valid(&bufref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683# endif
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001684 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#endif
1686 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001687#ifdef FEAT_SYN_HL
1688 if (prevbuf == curwin->w_buffer)
1689 reset_synblock(curwin);
1690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001692 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001694 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001696 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001698 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001699 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001700 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001701 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1703 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001704 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001705 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001706 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001707 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001708 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001712 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001713 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001714 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1715 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001716# ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001717 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001718# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001719 ) || curwin->w_buffer == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001723#ifdef FEAT_SYN_HL
1724 if (old_tw != curbuf->b_p_tw)
1725 check_colorcolumn(curwin);
1726#endif
1727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728}
1729
1730/*
1731 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001732 * Old curbuf must have been abandoned already! This also means "curbuf" may
1733 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 */
1735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001736enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
1738 /* Copy buffer and window local option values. Not for a help buffer. */
1739 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1740 if (!buf->b_help)
1741 get_winopts(buf);
1742#ifdef FEAT_FOLDING
1743 else
1744 /* Remove all folds in the window. */
1745 clearFolding(curwin);
1746 foldUpdateAll(curwin); /* update folds (later). */
1747#endif
1748
1749 /* Get the buffer in the current window. */
1750 curwin->w_buffer = buf;
1751 curbuf = buf;
1752 ++curbuf->b_nwindows;
1753
1754#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001755 if (curwin->w_p_diff)
1756 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757#endif
1758
Bram Moolenaara971b822011-09-14 14:43:25 +02001759#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001760 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001761#endif
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 /* Cursor on first line by default. */
1764 curwin->w_cursor.lnum = 1;
1765 curwin->w_cursor.col = 0;
1766#ifdef FEAT_VIRTUALEDIT
1767 curwin->w_cursor.coladd = 0;
1768#endif
1769 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001770#ifdef FEAT_AUTOCMD
1771 curwin->w_topline_was_set = FALSE;
1772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001774 /* mark cursor position as being invalid */
1775 curwin->w_valid = 0;
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 /* Make sure the buffer is loaded. */
1778 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001779 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001780#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001781 /* If there is no filetype, allow for detecting one. Esp. useful for
1782 * ":ball" used in a autocommand. If there already is a filetype we
1783 * might prefer to keep it. */
1784 if (*curbuf->b_p_ft == NUL)
1785 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001786#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001787
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001788 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 else
1791 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001792 if (!msg_silent)
1793 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1795#ifdef FEAT_AUTOCMD
1796 curwin->w_topline = 1;
1797# ifdef FEAT_DIFF
1798 curwin->w_topfill = 0;
1799# endif
1800 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1801 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1802#endif
1803 }
1804
1805 /* If autocommands did not change the cursor position, restore cursor lnum
1806 * and possibly cursor col. */
1807 if (curwin->w_cursor.lnum == 1 && inindent(0))
1808 buflist_getfpos();
1809
1810 check_arg_idx(curwin); /* check for valid arg_idx */
1811#ifdef FEAT_TITLE
1812 maketitle();
1813#endif
1814#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001815 /* when autocmds didn't change it */
1816 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#endif
1818 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1819
Bram Moolenaar009b2592004-10-24 19:18:58 +00001820#ifdef FEAT_NETBEANS_INTG
1821 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001822 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001823#endif
1824
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001825 /* Change directories when the 'acd' option is set. */
1826 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828#ifdef FEAT_KEYMAP
1829 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001830 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001832#ifdef FEAT_SPELL
1833 /* May need to set the spell language. Can only do this after the buffer
1834 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001835 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1836 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001837#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001838#ifdef FEAT_VIMINFO
1839 curbuf->b_last_used = vim_time();
1840#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 redraw_later(NOT_VALID);
1843}
1844
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001845#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1846/*
1847 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001848 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001849 */
1850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001851do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001852{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001853 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001854 && curbuf->b_ffname != NULL
1855 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001856 shorten_fnames(TRUE);
1857}
1858#endif
1859
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001860 void
1861no_write_message(void)
1862{
1863#ifdef FEAT_TERMINAL
1864 if (term_job_running(curbuf->b_term))
1865 EMSG(_("E948: Job still running (add ! to end the job)"));
1866 else
1867#endif
1868 EMSG(_("E37: No write since last change (add ! to override)"));
1869}
1870
1871 void
1872no_write_message_nobang(void)
1873{
1874#ifdef FEAT_TERMINAL
1875 if (term_job_running(curbuf->b_term))
1876 EMSG(_("E948: Job still running"));
1877 else
1878#endif
1879 EMSG(_("E37: No write since last change"));
1880}
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882/*
1883 * functions for dealing with the buffer list
1884 */
1885
Bram Moolenaar480778b2016-07-14 22:09:39 +02001886static int top_file_num = 1; /* highest file number */
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888/*
1889 * Add a file name to the buffer list. Return a pointer to the buffer.
1890 * If the same file name already exists return a pointer to that buffer.
1891 * If it does not exist, or if fname == NULL, a new entry is created.
1892 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1893 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1894 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001895 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001896 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1897 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 * This is the ONLY way to create a new buffer.
1899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001901buflist_new(
1902 char_u *ffname, /* full path of fname or relative */
1903 char_u *sfname, /* short fname or NULL */
1904 linenr_T lnum, /* preferred cursor line */
1905 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 buf_T *buf;
1908#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001909 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910#endif
1911
Bram Moolenaar480778b2016-07-14 22:09:39 +02001912 if (top_file_num == 1)
1913 hash_init(&buf_hashtab);
1914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1916
1917 /*
1918 * If file name already exists in the list, update the entry.
1919 */
1920#ifdef UNIX
1921 /* On Unix we can use inode numbers when the file exists. Works better
1922 * for hard links. */
1923 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1924 st.st_dev = (dev_T)-1;
1925#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001926 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#ifdef UNIX
1928 buflist_findname_stat(ffname, &st)
1929#else
1930 buflist_findname(ffname)
1931#endif
1932 ) != NULL)
1933 {
1934 vim_free(ffname);
1935 if (lnum != 0)
1936 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001937
1938 if ((flags & BLN_NOOPT) == 0)
1939 /* copy the options now, if 'cpo' doesn't have 's' and not done
1940 * already */
1941 buf_copy_options(buf, 0);
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1944 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001945#ifdef FEAT_AUTOCMD
1946 bufref_T bufref;
1947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 buf->b_p_bl = TRUE;
1949#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001950 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001952 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001953 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001954 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001955 return NULL;
1956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957#endif
1958 }
1959 return buf;
1960 }
1961
1962 /*
1963 * If the current buffer has no name and no contents, use the current
1964 * buffer. Otherwise: Need to allocate a new buffer structure.
1965 *
1966 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001967 * (A spell file buffer is allocated in spell.c, but that's not a normal
1968 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 */
1970 buf = NULL;
1971 if ((flags & BLN_CURBUF)
1972 && curbuf != NULL
1973 && curbuf->b_ffname == NULL
1974 && curbuf->b_nwindows <= 1
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001975 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 buf = curbuf;
1978#ifdef FEAT_AUTOCMD
1979 /* It's like this buffer is deleted. Watch out for autocommands that
1980 * change curbuf! If that happens, allocate a new buffer anyway. */
1981 if (curbuf->b_p_bl)
1982 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1983 if (buf == curbuf)
1984 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1985# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001986 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 return NULL;
1988# endif
1989#endif
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001990#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (buf == curbuf)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 /* Make sure 'bufhidden' and 'buftype' are empty */
1995 clear_string_option(&buf->b_p_bh);
1996 clear_string_option(&buf->b_p_bt);
1997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 if (buf != curbuf || curbuf == NULL)
2000 {
2001 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
2002 if (buf == NULL)
2003 {
2004 vim_free(ffname);
2005 return NULL;
2006 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002007#ifdef FEAT_EVAL
2008 /* init b: variables */
2009 buf->b_vars = dict_alloc();
2010 if (buf->b_vars == NULL)
2011 {
2012 vim_free(ffname);
2013 vim_free(buf);
2014 return NULL;
2015 }
2016 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2017#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002018 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020
2021 if (ffname != NULL)
2022 {
2023 buf->b_ffname = ffname;
2024 buf->b_sfname = vim_strsave(sfname);
2025 }
2026
2027 clear_wininfo(buf);
2028 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2029
2030 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2031 || buf->b_wininfo == NULL)
2032 {
2033 vim_free(buf->b_ffname);
2034 buf->b_ffname = NULL;
2035 vim_free(buf->b_sfname);
2036 buf->b_sfname = NULL;
2037 if (buf != curbuf)
2038 free_buffer(buf);
2039 return NULL;
2040 }
2041
2042 if (buf == curbuf)
2043 {
2044 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002045 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if (buf != curbuf) /* autocommands deleted the buffer! */
2047 return NULL;
2048#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002049 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 return NULL;
2051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002053
2054 /* Init the options. */
2055 buf->b_p_initialized = FALSE;
2056 buf_copy_options(buf, BCO_ENTER);
2057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058#ifdef FEAT_KEYMAP
2059 /* need to reload lmaps and set b:keymap_name */
2060 curbuf->b_kmap_state |= KEYMAP_INIT;
2061#endif
2062 }
2063 else
2064 {
2065 /*
2066 * put new buffer at the end of the buffer list
2067 */
2068 buf->b_next = NULL;
2069 if (firstbuf == NULL) /* buffer list is empty */
2070 {
2071 buf->b_prev = NULL;
2072 firstbuf = buf;
2073 }
2074 else /* append new buffer at end of list */
2075 {
2076 lastbuf->b_next = buf;
2077 buf->b_prev = lastbuf;
2078 }
2079 lastbuf = buf;
2080
2081 buf->b_fnum = top_file_num++;
2082 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2083 {
2084 EMSG(_("W14: Warning: List of file names overflow"));
2085 if (emsg_silent == 0)
2086 {
2087 out_flush();
2088 ui_delay(3000L, TRUE); /* make sure it is noticed */
2089 }
2090 top_file_num = 1;
2091 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002092 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
2094 /*
2095 * Always copy the options from the current buffer.
2096 */
2097 buf_copy_options(buf, BCO_ALWAYS);
2098 }
2099
2100 buf->b_wininfo->wi_fpos.lnum = lnum;
2101 buf->b_wininfo->wi_win = curwin;
2102
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002103#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002104 hash_init(&buf->b_s.b_keywtab);
2105 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106#endif
2107
2108 buf->b_fname = buf->b_sfname;
2109#ifdef UNIX
2110 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002111 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 else
2113 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002114 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 buf->b_dev = st.st_dev;
2116 buf->b_ino = st.st_ino;
2117 }
2118#endif
2119 buf->b_u_synced = TRUE;
2120 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002121 if (flags & BLN_DUMMY)
2122 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 buf_clear_file(buf);
2124 clrallmarks(buf); /* clear marks */
2125 fmarks_check_names(buf); /* check file marks for this file */
2126 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2127#ifdef FEAT_AUTOCMD
2128 if (!(flags & BLN_DUMMY))
2129 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002130 bufref_T bufref;
2131
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002132 /* Tricky: these autocommands may change the buffer list. They could
2133 * also split the window with re-using the one empty buffer. This may
2134 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002135 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002136 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002137 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002138 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002140 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002141 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002142 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002143 return NULL;
2144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002146 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 return NULL;
2148# endif
2149 }
2150#endif
2151
2152 return buf;
2153}
2154
2155/*
2156 * Free the memory for the options of a buffer.
2157 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2158 * 'fileencoding'.
2159 */
2160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161free_buf_options(
2162 buf_T *buf,
2163 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 if (free_p_ff)
2166 {
2167#ifdef FEAT_MBYTE
2168 clear_string_option(&buf->b_p_fenc);
2169#endif
2170 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 clear_string_option(&buf->b_p_bh);
2172 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
2174#ifdef FEAT_FIND_ID
2175 clear_string_option(&buf->b_p_def);
2176 clear_string_option(&buf->b_p_inc);
2177# ifdef FEAT_EVAL
2178 clear_string_option(&buf->b_p_inex);
2179# endif
2180#endif
2181#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2182 clear_string_option(&buf->b_p_inde);
2183 clear_string_option(&buf->b_p_indk);
2184#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002185#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2186 clear_string_option(&buf->b_p_bexpr);
2187#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002188#if defined(FEAT_CRYPT)
2189 clear_string_option(&buf->b_p_cm);
2190#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002191 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002192#if defined(FEAT_EVAL)
2193 clear_string_option(&buf->b_p_fex);
2194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#ifdef FEAT_CRYPT
2196 clear_string_option(&buf->b_p_key);
2197#endif
2198 clear_string_option(&buf->b_p_kp);
2199 clear_string_option(&buf->b_p_mps);
2200 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002201 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 clear_string_option(&buf->b_p_isk);
2203#ifdef FEAT_KEYMAP
2204 clear_string_option(&buf->b_p_keymap);
2205 ga_clear(&buf->b_kmap_ga);
2206#endif
2207#ifdef FEAT_COMMENTS
2208 clear_string_option(&buf->b_p_com);
2209#endif
2210#ifdef FEAT_FOLDING
2211 clear_string_option(&buf->b_p_cms);
2212#endif
2213 clear_string_option(&buf->b_p_nf);
2214#ifdef FEAT_SYN_HL
2215 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002216 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002217#endif
2218#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002219 clear_string_option(&buf->b_s.b_p_spc);
2220 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002221 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002222 buf->b_s.b_cap_prog = NULL;
2223 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#endif
2225#ifdef FEAT_SEARCHPATH
2226 clear_string_option(&buf->b_p_sua);
2227#endif
2228#ifdef FEAT_AUTOCMD
2229 clear_string_option(&buf->b_p_ft);
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#ifdef FEAT_CINDENT
2232 clear_string_option(&buf->b_p_cink);
2233 clear_string_option(&buf->b_p_cino);
2234#endif
2235#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2236 clear_string_option(&buf->b_p_cinw);
2237#endif
2238#ifdef FEAT_INS_EXPAND
2239 clear_string_option(&buf->b_p_cpt);
2240#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002241#ifdef FEAT_COMPL_FUNC
2242 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002243 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245#ifdef FEAT_QUICKFIX
2246 clear_string_option(&buf->b_p_gp);
2247 clear_string_option(&buf->b_p_mp);
2248 clear_string_option(&buf->b_p_efm);
2249#endif
2250 clear_string_option(&buf->b_p_ep);
2251 clear_string_option(&buf->b_p_path);
2252 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002253 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#ifdef FEAT_INS_EXPAND
2255 clear_string_option(&buf->b_p_dict);
2256 clear_string_option(&buf->b_p_tsr);
2257#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002258#ifdef FEAT_TEXTOBJ
2259 clear_string_option(&buf->b_p_qe);
2260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002262 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002263#ifdef FEAT_LISP
2264 clear_string_option(&buf->b_p_lw);
2265#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002266 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002267#ifdef FEAT_MBYTE
2268 clear_string_option(&buf->b_p_menc);
2269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270}
2271
2272/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002273 * Get alternate file "n".
2274 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2275 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 * if (options & GETF_SETMARK) call setpcmark()
2277 * if (options & GETF_ALT) we are jumping to an alternate file.
2278 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2279 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002280 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 */
2282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002283buflist_getfile(
2284 int n,
2285 linenr_T lnum,
2286 int options,
2287 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 pos_T *fpos;
2292 colnr_T col;
2293
2294 buf = buflist_findnr(n);
2295 if (buf == NULL)
2296 {
2297 if ((options & GETF_ALT) && n == 0)
2298 EMSG(_(e_noalt));
2299 else
2300 EMSGN(_("E92: Buffer %ld not found"), n);
2301 return FAIL;
2302 }
2303
2304 /* if alternate file is the current buffer, nothing to do */
2305 if (buf == curbuf)
2306 return OK;
2307
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002308 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002309 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002310 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002312 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002313#ifdef FEAT_AUTOCMD
2314 if (curbuf_locked())
2315 return FAIL;
2316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 /* altfpos may be changed by getfile(), get it now */
2319 if (lnum == 0)
2320 {
2321 fpos = buflist_findfpos(buf);
2322 lnum = fpos->lnum;
2323 col = fpos->col;
2324 }
2325 else
2326 col = 0;
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (options & GETF_SWITCH)
2329 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002330 /* If 'switchbuf' contains "useopen": jump to first window containing
2331 * "buf" if one exists */
2332 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002334
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002335 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002336 * page containing "buf" if one exists */
2337 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002338 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002339
2340 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2341 * current buffer isn't empty: open new tab or window */
2342 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002343 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002345 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002346 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002347 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2348 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002350 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
2354 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002355 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2356 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 --RedrawingDisabled;
2359
2360 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2361 if (!p_sol && col != 0)
2362 {
2363 curwin->w_cursor.col = col;
2364 check_cursor_col();
2365#ifdef FEAT_VIRTUALEDIT
2366 curwin->w_cursor.coladd = 0;
2367#endif
2368 curwin->w_set_curswant = TRUE;
2369 }
2370 return OK;
2371 }
2372 --RedrawingDisabled;
2373 return FAIL;
2374}
2375
2376/*
2377 * go to the last know line number for the current buffer
2378 */
2379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002380buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 pos_T *fpos;
2383
2384 fpos = buflist_findfpos(curbuf);
2385
2386 curwin->w_cursor.lnum = fpos->lnum;
2387 check_cursor_lnum();
2388
2389 if (p_sol)
2390 curwin->w_cursor.col = 0;
2391 else
2392 {
2393 curwin->w_cursor.col = fpos->col;
2394 check_cursor_col();
2395#ifdef FEAT_VIRTUALEDIT
2396 curwin->w_cursor.coladd = 0;
2397#endif
2398 curwin->w_set_curswant = TRUE;
2399 }
2400}
2401
Bram Moolenaar81695252004-12-29 20:58:21 +00002402#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2403/*
2404 * Find file in buffer list by name (it has to be for the current window).
2405 * Returns NULL if not found.
2406 */
2407 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002408buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002409{
2410 char_u *ffname;
2411 buf_T *buf = NULL;
2412
2413 /* First make the name into a full path name */
2414 ffname = FullName_save(fname,
2415#ifdef UNIX
2416 TRUE /* force expansion, get rid of symbolic links */
2417#else
2418 FALSE
2419#endif
2420 );
2421 if (ffname != NULL)
2422 {
2423 buf = buflist_findname(ffname);
2424 vim_free(ffname);
2425 }
2426 return buf;
2427}
2428#endif
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430/*
2431 * Find file in buffer list by name (it has to be for the current window).
2432 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002433 * Skips dummy buffers.
2434 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
2436 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002437buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002440 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 if (mch_stat((char *)ffname, &st) < 0)
2443 st.st_dev = (dev_T)-1;
2444 return buflist_findname_stat(ffname, &st);
2445}
2446
2447/*
2448 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2449 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002450 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
2452 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002453buflist_findname_stat(
2454 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002455 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457#endif
2458 buf_T *buf;
2459
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002460 /* Start at the last buffer, expect to find a match sooner. */
2461 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002462 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463#ifdef UNIX
2464 , stp
2465#endif
2466 ))
2467 return buf;
2468 return NULL;
2469}
2470
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002471#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2472 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473/*
2474 * Find file in buffer list by a regexp pattern.
2475 * Return fnum of the found buffer.
2476 * Return < 0 for error.
2477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002479buflist_findpat(
2480 char_u *pattern,
2481 char_u *pattern_end, /* pointer to first char after pattern */
2482 int unlisted, /* find unlisted buffers */
2483 int diffmode UNUSED, /* find diff-mode buffers only */
2484 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 int match = -1;
2488 int find_listed;
2489 char_u *pat;
2490 char_u *patend;
2491 int attempt;
2492 char_u *p;
2493 int toggledollar;
2494
2495 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2496 {
2497 if (*pattern == '%')
2498 match = curbuf->b_fnum;
2499 else
2500 match = curwin->w_alt_fnum;
2501#ifdef FEAT_DIFF
2502 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2503 match = -1;
2504#endif
2505 }
2506
2507 /*
2508 * Try four ways of matching a listed buffer:
2509 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002510 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 * attempt == 2: with '$' at end (only match at end)
2512 * attempt == 3: with '^' at start and '$' at end (only full match)
2513 * Repeat this for finding an unlisted buffer if there was no matching
2514 * listed buffer.
2515 */
2516 else
2517 {
2518 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2519 if (pat == NULL)
2520 return -1;
2521 patend = pat + STRLEN(pat) - 1;
2522 toggledollar = (patend > pat && *patend == '$');
2523
2524 /* First try finding a listed buffer. If not found and "unlisted"
2525 * is TRUE, try finding an unlisted buffer. */
2526 find_listed = TRUE;
2527 for (;;)
2528 {
2529 for (attempt = 0; attempt <= 3; ++attempt)
2530 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002531 regmatch_T regmatch;
2532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 /* may add '^' and '$' */
2534 if (toggledollar)
2535 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2536 p = pat;
2537 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2538 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002539 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2540 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 vim_free(pat);
2543 return -1;
2544 }
2545
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002546 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (buf->b_p_bl == find_listed
2548#ifdef FEAT_DIFF
2549 && (!diffmode || diff_mode_buf(buf))
2550#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002551 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002553 if (curtab_only)
2554 {
2555 /* Ignore the match if the buffer is not open in
2556 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002557 win_T *wp;
2558
Bram Moolenaar29323592016-07-24 22:04:11 +02002559 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002560 if (wp->w_buffer == buf)
2561 break;
2562 if (wp == NULL)
2563 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 if (match >= 0) /* already found a match */
2566 {
2567 match = -2;
2568 break;
2569 }
2570 match = buf->b_fnum; /* remember first match */
2571 }
2572
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002573 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (match >= 0) /* found one match */
2575 break;
2576 }
2577
2578 /* Only search for unlisted buffers if there was no match with
2579 * a listed buffer. */
2580 if (!unlisted || !find_listed || match != -1)
2581 break;
2582 find_listed = FALSE;
2583 }
2584
2585 vim_free(pat);
2586 }
2587
2588 if (match == -2)
2589 EMSG2(_("E93: More than one match for %s"), pattern);
2590 else if (match < 0)
2591 EMSG2(_("E94: No matching buffer for %s"), pattern);
2592 return match;
2593}
2594#endif
2595
2596#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2597
2598/*
2599 * Find all buffer names that match.
2600 * For command line expansion of ":buf" and ":sbuf".
2601 * Return OK if matches found, FAIL otherwise.
2602 */
2603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002604ExpandBufnames(
2605 char_u *pat,
2606 int *num_file,
2607 char_u ***file,
2608 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 int count = 0;
2611 buf_T *buf;
2612 int round;
2613 char_u *p;
2614 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002615 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 *num_file = 0; /* return values in case of FAIL */
2618 *file = NULL;
2619
Bram Moolenaar05159a02005-02-26 23:04:13 +00002620 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2621 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002623 patc = alloc((unsigned)STRLEN(pat) + 11);
2624 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002626 STRCPY(patc, "\\(^\\|[\\/]\\)");
2627 STRCPY(patc + 11, pat + 1);
2628 }
2629 else
2630 patc = pat;
2631
2632 /*
2633 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002634 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002635 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002636 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002637 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002638 regmatch_T regmatch;
2639
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002640 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002641 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002642 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2643 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002644 {
2645 if (patc != pat)
2646 vim_free(patc);
2647 return FAIL;
2648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
2650 /*
2651 * round == 1: Count the matches.
2652 * round == 2: Build the array to keep the matches.
2653 */
2654 for (round = 1; round <= 2; ++round)
2655 {
2656 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002657 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 if (!buf->b_p_bl) /* skip unlisted buffers */
2660 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002661 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (p != NULL)
2663 {
2664 if (round == 1)
2665 ++count;
2666 else
2667 {
2668 if (options & WILD_HOME_REPLACE)
2669 p = home_replace_save(buf, p);
2670 else
2671 p = vim_strsave(p);
2672 (*file)[count++] = p;
2673 }
2674 }
2675 }
2676 if (count == 0) /* no match found, break here */
2677 break;
2678 if (round == 1)
2679 {
2680 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2681 if (*file == NULL)
2682 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002683 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002684 if (patc != pat)
2685 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 return FAIL;
2687 }
2688 }
2689 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002690 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (count) /* match(es) found, break here */
2692 break;
2693 }
2694
Bram Moolenaar05159a02005-02-26 23:04:13 +00002695 if (patc != pat)
2696 vim_free(patc);
2697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 *num_file = count;
2699 return (count == 0 ? FAIL : OK);
2700}
2701
2702#endif /* FEAT_CMDL_COMPL */
2703
2704#ifdef HAVE_BUFLIST_MATCH
2705/*
2706 * Check for a match on the file name for buffer "buf" with regprog "prog".
2707 */
2708 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002709buflist_match(
2710 regmatch_T *rmp,
2711 buf_T *buf,
2712 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 char_u *match;
2715
2716 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002717 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002719 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 return match;
2722}
2723
2724/*
2725 * Try matching the regexp in "prog" with file name "name".
2726 * Return "name" when there is a match, NULL when not.
2727 */
2728 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002729fname_match(
2730 regmatch_T *rmp,
2731 char_u *name,
2732 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
2734 char_u *match = NULL;
2735 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 if (name != NULL)
2738 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002739 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002740 rmp->rm_ic = p_fic || ignore_case;
2741 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 match = name;
2743 else
2744 {
2745 /* Replace $(HOME) with '~' and try matching again. */
2746 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002747 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 match = name;
2749 vim_free(p);
2750 }
2751 }
2752
2753 return match;
2754}
2755#endif
2756
2757/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002758 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 */
2760 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002761buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002763 char_u key[VIM_SIZEOF_INT * 2 + 1];
2764 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 if (nr == 0)
2767 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002768 sprintf((char *)key, "%x", nr);
2769 hi = hash_find(&buf_hashtab, key);
2770
2771 if (!HASHITEM_EMPTY(hi))
2772 return (buf_T *)(hi->hi_key
2773 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 return NULL;
2775}
2776
2777/*
2778 * Get name of file 'n' in the buffer list.
2779 * When the file has no name an empty string is returned.
2780 * home_replace() is used to shorten the file name (used for marks).
2781 * Returns a pointer to allocated memory, of NULL when failed.
2782 */
2783 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002784buflist_nr2name(
2785 int n,
2786 int fullname,
2787 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 buf_T *buf;
2790
2791 buf = buflist_findnr(n);
2792 if (buf == NULL)
2793 return NULL;
2794 return home_replace_save(helptail ? buf : NULL,
2795 fullname ? buf->b_ffname : buf->b_fname);
2796}
2797
2798/*
2799 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2800 * When "copy_options" is TRUE save the local window option values.
2801 * When "lnum" is 0 only do the options.
2802 */
2803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002804buflist_setfpos(
2805 buf_T *buf,
2806 win_T *win,
2807 linenr_T lnum,
2808 colnr_T col,
2809 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
2811 wininfo_T *wip;
2812
2813 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2814 if (wip->wi_win == win)
2815 break;
2816 if (wip == NULL)
2817 {
2818 /* allocate a new entry */
2819 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2820 if (wip == NULL)
2821 return;
2822 wip->wi_win = win;
2823 if (lnum == 0) /* set lnum even when it's 0 */
2824 lnum = 1;
2825 }
2826 else
2827 {
2828 /* remove the entry from the list */
2829 if (wip->wi_prev)
2830 wip->wi_prev->wi_next = wip->wi_next;
2831 else
2832 buf->b_wininfo = wip->wi_next;
2833 if (wip->wi_next)
2834 wip->wi_next->wi_prev = wip->wi_prev;
2835 if (copy_options && wip->wi_optset)
2836 {
2837 clear_winopt(&wip->wi_opt);
2838#ifdef FEAT_FOLDING
2839 deleteFoldRecurse(&wip->wi_folds);
2840#endif
2841 }
2842 }
2843 if (lnum != 0)
2844 {
2845 wip->wi_fpos.lnum = lnum;
2846 wip->wi_fpos.col = col;
2847 }
2848 if (copy_options)
2849 {
2850 /* Save the window-specific option values. */
2851 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2852#ifdef FEAT_FOLDING
2853 wip->wi_fold_manual = win->w_fold_manual;
2854 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2855#endif
2856 wip->wi_optset = TRUE;
2857 }
2858
2859 /* insert the entry in front of the list */
2860 wip->wi_next = buf->b_wininfo;
2861 buf->b_wininfo = wip;
2862 wip->wi_prev = NULL;
2863 if (wip->wi_next)
2864 wip->wi_next->wi_prev = wip;
2865
2866 return;
2867}
2868
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002869#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002870static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002871
2872/*
2873 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2874 * page. That's because a diff is local to a tab page.
2875 */
2876 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002877wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002878{
2879 win_T *wp;
2880
2881 if (wip->wi_opt.wo_diff)
2882 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002883 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002884 /* return FALSE when it's a window in the current tab page, thus
2885 * the buffer was in diff mode here */
2886 if (wip->wi_win == wp)
2887 return FALSE;
2888 return TRUE;
2889 }
2890 return FALSE;
2891}
2892#endif
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894/*
2895 * Find info for the current window in buffer "buf".
2896 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002897 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2898 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 * Returns NULL when there isn't any info.
2900 */
2901 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002902find_wininfo(
2903 buf_T *buf,
2904 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 wininfo_T *wip;
2907
2908 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002909 if (wip->wi_win == curwin
2910#ifdef FEAT_DIFF
2911 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2912#endif
2913 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002915
2916 /* If no wininfo for curwin, use the first in the list (that doesn't have
2917 * 'diff' set and is in another tab page). */
2918 if (wip == NULL)
2919 {
2920#ifdef FEAT_DIFF
2921 if (skip_diff_buffer)
2922 {
2923 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2924 if (!wininfo_other_tab_diff(wip))
2925 break;
2926 }
2927 else
2928#endif
2929 wip = buf->b_wininfo;
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 return wip;
2932}
2933
2934/*
2935 * Reset the local window options to the values last used in this window.
2936 * If the buffer wasn't used in this window before, use the values from
2937 * the most recently used window. If the values were never set, use the
2938 * global values for the window.
2939 */
2940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002941get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
2943 wininfo_T *wip;
2944
2945 clear_winopt(&curwin->w_onebuf_opt);
2946#ifdef FEAT_FOLDING
2947 clearFolding(curwin);
2948#endif
2949
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002950 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 if (wip != NULL && wip->wi_optset)
2952 {
2953 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2954#ifdef FEAT_FOLDING
2955 curwin->w_fold_manual = wip->wi_fold_manual;
2956 curwin->w_foldinvalid = TRUE;
2957 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2958#endif
2959 }
2960 else
2961 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2962
2963#ifdef FEAT_FOLDING
2964 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2965 if (p_fdls >= 0)
2966 curwin->w_p_fdl = p_fdls;
2967#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002968#ifdef FEAT_SYN_HL
2969 check_colorcolumn(curwin);
2970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
2973/*
2974 * Find the position (lnum and col) for the buffer 'buf' for the current
2975 * window.
2976 * Returns a pointer to no_position if no position is found.
2977 */
2978 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002979buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002982 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002984 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 if (wip != NULL)
2986 return &(wip->wi_fpos);
2987 else
2988 return &no_position;
2989}
2990
2991/*
2992 * Find the lnum for the buffer 'buf' for the current window.
2993 */
2994 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002995buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 return buflist_findfpos(buf)->lnum;
2998}
2999
3000#if defined(FEAT_LISTCMDS) || defined(PROTO)
3001/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003002 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003005buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 buf_T *buf;
3008 int len;
3009 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003010 int ro_char;
3011 int changed_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3014 {
3015 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003016 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3017 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3018 || (vim_strchr(eap->arg, '+')
3019 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3020 || (vim_strchr(eap->arg, 'a')
3021 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
3022 || (vim_strchr(eap->arg, 'h')
3023 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3024 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3025 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3026 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3027 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3028 || (vim_strchr(eap->arg, '#')
3029 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003032 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else
3034 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003035 if (message_filtered(NameBuff))
3036 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003038 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3039 : (bufIsChanged(buf) ? '+' : ' ');
3040#ifdef FEAT_TERMINAL
3041 if (term_job_running(buf->b_term))
3042 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003043 if (term_none_open(buf->b_term))
3044 ro_char = '?';
3045 else
3046 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003047 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3048 * closing, but it's not actually changed. */
3049 }
3050 else if (buf->b_term != NULL)
3051 ro_char = 'F';
3052 else
3053#endif
3054 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3055
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003056 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003057 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 buf->b_fnum,
3059 buf->b_p_bl ? ' ' : 'u',
3060 buf == curbuf ? '%' :
3061 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3062 buf->b_ml.ml_mfp == NULL ? ' ' :
3063 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003064 ro_char,
3065 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003066 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003067 if (len > IOSIZE - 20)
3068 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
3070 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 i = 40 - vim_strsize(IObuff);
3072 do
3073 {
3074 IObuff[len++] = ' ';
3075 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003076 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3077 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003078 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 msg_outtrans(IObuff);
3080 out_flush(); /* output one line at a time */
3081 ui_breakcheck();
3082 }
3083}
3084#endif
3085
3086/*
3087 * Get file name and line number for file 'fnum'.
3088 * Used by DoOneCmd() for translating '%' and '#'.
3089 * Used by insert_reg() and cmdline_paste() for '#' register.
3090 * Return FAIL if not found, OK for success.
3091 */
3092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003093buflist_name_nr(
3094 int fnum,
3095 char_u **fname,
3096 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
3098 buf_T *buf;
3099
3100 buf = buflist_findnr(fnum);
3101 if (buf == NULL || buf->b_fname == NULL)
3102 return FAIL;
3103
3104 *fname = buf->b_fname;
3105 *lnum = buflist_findlnum(buf);
3106
3107 return OK;
3108}
3109
3110/*
3111 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3112 * The file name with the full path is also remembered, for when :cd is used.
3113 * Returns FAIL for failure (file name already in use by other buffer)
3114 * OK otherwise.
3115 */
3116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003117setfname(
3118 buf_T *buf,
3119 char_u *ffname,
3120 char_u *sfname,
3121 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
Bram Moolenaar81695252004-12-29 20:58:21 +00003123 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003125 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#endif
3127
3128 if (ffname == NULL || *ffname == NUL)
3129 {
3130 /* Removing the name. */
3131 vim_free(buf->b_ffname);
3132 vim_free(buf->b_sfname);
3133 buf->b_ffname = NULL;
3134 buf->b_sfname = NULL;
3135#ifdef UNIX
3136 st.st_dev = (dev_T)-1;
3137#endif
3138 }
3139 else
3140 {
3141 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3142 if (ffname == NULL) /* out of memory */
3143 return FAIL;
3144
3145 /*
3146 * if the file name is already used in another buffer:
3147 * - if the buffer is loaded, fail
3148 * - if the buffer is not loaded, delete it from the list
3149 */
3150#ifdef UNIX
3151 if (mch_stat((char *)ffname, &st) < 0)
3152 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003153#endif
3154 if (!(buf->b_flags & BF_DUMMY))
3155#ifdef UNIX
3156 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003158 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#endif
3160 if (obuf != NULL && obuf != buf)
3161 {
3162 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3163 {
3164 if (message)
3165 EMSG(_("E95: Buffer with this name already exists"));
3166 vim_free(ffname);
3167 return FAIL;
3168 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003169 /* delete from the list */
3170 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172 sfname = vim_strsave(sfname);
3173 if (ffname == NULL || sfname == NULL)
3174 {
3175 vim_free(sfname);
3176 vim_free(ffname);
3177 return FAIL;
3178 }
3179#ifdef USE_FNAME_CASE
3180# ifdef USE_LONG_FNAME
3181 if (USE_LONG_FNAME)
3182# endif
3183 fname_case(sfname, 0); /* set correct case for short file name */
3184#endif
3185 vim_free(buf->b_ffname);
3186 vim_free(buf->b_sfname);
3187 buf->b_ffname = ffname;
3188 buf->b_sfname = sfname;
3189 }
3190 buf->b_fname = buf->b_sfname;
3191#ifdef UNIX
3192 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003193 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 else
3195 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003196 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 buf->b_dev = st.st_dev;
3198 buf->b_ino = st.st_ino;
3199 }
3200#endif
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 buf_name_changed(buf);
3205 return OK;
3206}
3207
3208/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003209 * Crude way of changing the name of a buffer. Use with care!
3210 * The name should be relative to the current directory.
3211 */
3212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003214{
3215 buf_T *buf;
3216
3217 buf = buflist_findnr(fnum);
3218 if (buf != NULL)
3219 {
3220 vim_free(buf->b_sfname);
3221 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003222 buf->b_ffname = vim_strsave(name);
3223 buf->b_sfname = NULL;
3224 /* Allocate ffname and expand into full path. Also resolves .lnk
3225 * files on Win32. */
3226 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003227 buf->b_fname = buf->b_sfname;
3228 }
3229}
3230
3231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 * Take care of what needs to be done when the name of buffer "buf" has
3233 * changed.
3234 */
3235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003236buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 /*
3239 * If the file name changed, also change the name of the swapfile
3240 */
3241 if (buf->b_ml.ml_mfp != NULL)
3242 ml_setname(buf);
3243
3244 if (curwin->w_buffer == buf)
3245 check_arg_idx(curwin); /* check file name for arg list */
3246#ifdef FEAT_TITLE
3247 maketitle(); /* set window title */
3248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 fmarks_check_names(buf); /* check named file marks */
3251 ml_timestamp(buf); /* reset timestamp */
3252}
3253
3254/*
3255 * set alternate file name for current window
3256 *
3257 * Used by do_one_cmd(), do_write() and do_ecmd().
3258 * Return the buffer.
3259 */
3260 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003261setaltfname(
3262 char_u *ffname,
3263 char_u *sfname,
3264 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 buf_T *buf;
3267
3268 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3269 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003270 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 curwin->w_alt_fnum = buf->b_fnum;
3272 return buf;
3273}
3274
3275/*
3276 * Get alternate file name for current window.
3277 * Return NULL if there isn't any, and give error message if requested.
3278 */
3279 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003280getaltfname(
3281 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 char_u *fname;
3284 linenr_T dummy;
3285
3286 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3287 {
3288 if (errmsg)
3289 EMSG(_(e_noalt));
3290 return NULL;
3291 }
3292 return fname;
3293}
3294
3295/*
3296 * Add a file name to the buflist and return its number.
3297 * Uses same flags as buflist_new(), except BLN_DUMMY.
3298 *
3299 * used by qf_init(), main() and doarglist()
3300 */
3301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304 buf_T *buf;
3305
3306 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3307 if (buf != NULL)
3308 return buf->b_fnum;
3309 return 0;
3310}
3311
3312#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3313/*
3314 * Adjust slashes in file names. Called after 'shellslash' was set.
3315 */
3316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 buf_T *bp;
3320
Bram Moolenaar29323592016-07-24 22:04:11 +02003321 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
3323 if (bp->b_ffname != NULL)
3324 slash_adjust(bp->b_ffname);
3325 if (bp->b_sfname != NULL)
3326 slash_adjust(bp->b_sfname);
3327 }
3328}
3329#endif
3330
3331/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003332 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 * Also save the local window option values.
3334 */
3335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003338 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339}
3340
3341/*
3342 * Return TRUE if 'ffname' is not the same file as current file.
3343 * Fname must have a full path (expanded by mch_FullName()).
3344 */
3345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003346otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 return otherfile_buf(curbuf, ffname
3349#ifdef UNIX
3350 , NULL
3351#endif
3352 );
3353}
3354
3355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356otherfile_buf(
3357 buf_T *buf,
3358 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003360 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003362 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 /* no name is different */
3365 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3366 return TRUE;
3367 if (fnamecmp(ffname, buf->b_ffname) == 0)
3368 return FALSE;
3369#ifdef UNIX
3370 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003371 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
Bram Moolenaar8767f522016-07-01 17:17:39 +02003373 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (stp == NULL)
3375 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003376 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 st.st_dev = (dev_T)-1;
3378 stp = &st;
3379 }
3380 /* Use dev/ino to check if the files are the same, even when the names
3381 * are different (possible with links). Still need to compare the
3382 * name above, for when the file doesn't exist yet.
3383 * Problem: The dev/ino changes when a file is deleted (and created
3384 * again) and remains the same when renamed/moved. We don't want to
3385 * mch_stat() each buffer each time, that would be too slow. Get the
3386 * dev/ino again when they appear to match, but not when they appear
3387 * to be different: Could skip a buffer when it's actually the same
3388 * file. */
3389 if (buf_same_ino(buf, stp))
3390 {
3391 buf_setino(buf);
3392 if (buf_same_ino(buf, stp))
3393 return FALSE;
3394 }
3395 }
3396#endif
3397 return TRUE;
3398}
3399
3400#if defined(UNIX) || defined(PROTO)
3401/*
3402 * Set inode and device number for a buffer.
3403 * Must always be called when b_fname is changed!.
3404 */
3405 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003406buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003408 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
3410 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3411 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003412 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 buf->b_dev = st.st_dev;
3414 buf->b_ino = st.st_ino;
3415 }
3416 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003417 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418}
3419
3420/*
3421 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3422 */
3423 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003424buf_same_ino(
3425 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003426 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003428 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 && stp->st_dev == buf->b_dev
3430 && stp->st_ino == buf->b_ino);
3431}
3432#endif
3433
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434/*
3435 * Print info about the current buffer.
3436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003438fileinfo(
3439 int fullname, /* when non-zero print full path */
3440 int shorthelp,
3441 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 char_u *name;
3444 int n;
3445 char_u *p;
3446 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003447 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 buffer = alloc(IOSIZE);
3450 if (buffer == NULL)
3451 return;
3452
3453 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3454 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003455 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 p = buffer + STRLEN(buffer);
3457 }
3458 else
3459 p = buffer;
3460
3461 *p++ = '"';
3462 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003463 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 else
3465 {
3466 if (!fullname && curbuf->b_fname != NULL)
3467 name = curbuf->b_fname;
3468 else
3469 name = curbuf->b_ffname;
3470 home_replace(shorthelp ? curbuf : NULL, name, p,
3471 (int)(IOSIZE - (p - buffer)), TRUE);
3472 }
3473
Bram Moolenaara800b422010-06-27 01:15:55 +02003474 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 curbufIsChanged() ? (shortmess(SHM_MOD)
3476 ? " [+]" : _(" [Modified]")) : " ",
3477 (curbuf->b_flags & BF_NOTEDITED)
3478#ifdef FEAT_QUICKFIX
3479 && !bt_dontwrite(curbuf)
3480#endif
3481 ? _("[Not edited]") : "",
3482 (curbuf->b_flags & BF_NEW)
3483#ifdef FEAT_QUICKFIX
3484 && !bt_dontwrite(curbuf)
3485#endif
3486 ? _("[New file]") : "",
3487 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003488 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 : _("[readonly]")) : "",
3490 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3491 || curbuf->b_p_ro) ?
3492 " " : "");
3493 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3494 * causes an overflow, thus for large numbers divide instead. */
3495 if (curwin->w_cursor.lnum > 1000000L)
3496 n = (int)(((long)curwin->w_cursor.lnum) /
3497 ((long)curbuf->b_ml.ml_line_count / 100L));
3498 else
3499 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3500 (long)curbuf->b_ml.ml_line_count);
3501 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3502 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003503 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505#ifdef FEAT_CMDL_INFO
3506 else if (p_ru)
3507 {
3508 /* Current line and column are already on the screen -- webb */
3509 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003510 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003512 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 (long)curbuf->b_ml.ml_line_count, n);
3514 }
3515#endif
3516 else
3517 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003518 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 _("line %ld of %ld --%d%%-- col "),
3520 (long)curwin->w_cursor.lnum,
3521 (long)curbuf->b_ml.ml_line_count,
3522 n);
3523 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003524 len = STRLEN(buffer);
3525 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3527 }
3528
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003529 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 if (dont_truncate)
3532 {
3533 /* Temporarily set msg_scroll to avoid the message being truncated.
3534 * First call msg_start() to get the message in the right place. */
3535 msg_start();
3536 n = msg_scroll;
3537 msg_scroll = TRUE;
3538 msg(buffer);
3539 msg_scroll = n;
3540 }
3541 else
3542 {
3543 p = msg_trunc_attr(buffer, FALSE, 0);
3544 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* Need to repeat the message after redrawing when:
3546 * - When restart_edit is set (otherwise there will be a delay
3547 * before redrawing).
3548 * - When the screen was scrolled but there is no wait-return
3549 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003550 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552
3553 vim_free(buffer);
3554}
3555
3556 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003557col_print(
3558 char_u *buf,
3559 size_t buflen,
3560 int col,
3561 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
3563 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003564 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003566 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567}
3568
3569#if defined(FEAT_TITLE) || defined(PROTO)
3570/*
3571 * put file name in title bar of window and in icon title
3572 */
3573
3574static char_u *lasttitle = NULL;
3575static char_u *lasticon = NULL;
3576
3577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003578maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
3580 char_u *p;
3581 char_u *t_str = NULL;
3582 char_u *i_name;
3583 char_u *i_str = NULL;
3584 int maxlen = 0;
3585 int len;
3586 int mustset;
3587 char_u buf[IOSIZE];
3588 int off;
3589
3590 if (!redrawing())
3591 {
3592 /* Postpone updating the title when 'lazyredraw' is set. */
3593 need_maketitle = TRUE;
3594 return;
3595 }
3596
3597 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003598 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 return;
3600
3601 if (p_title)
3602 {
3603 if (p_titlelen > 0)
3604 {
3605 maxlen = p_titlelen * Columns / 100;
3606 if (maxlen < 10)
3607 maxlen = 10;
3608 }
3609
3610 t_str = buf;
3611 if (*p_titlestring != NUL)
3612 {
3613#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003614 if (stl_syntax & STL_IN_TITLE)
3615 {
3616 int use_sandbox = FALSE;
3617 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003618
3619# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003620 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003621# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003622 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003624 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003625 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003626 if (called_emsg)
3627 set_string_option_direct((char_u *)"titlestring", -1,
3628 (char_u *)"", OPT_FREE, SID_ERROR);
3629 called_emsg |= save_called_emsg;
3630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 else
3632#endif
3633 t_str = p_titlestring;
3634 }
3635 else
3636 {
3637 /* format: "fname + (path) (1 of 2) - VIM" */
3638
Bram Moolenaar2c666692012-09-05 13:30:40 +02003639#define SPACE_FOR_FNAME (IOSIZE - 100)
3640#define SPACE_FOR_DIR (IOSIZE - 20)
3641#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003643 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003644#ifdef FEAT_TERMINAL
3645 else if (curbuf->b_term != NULL)
3646 {
3647 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3648 SPACE_FOR_FNAME);
3649 }
3650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 else
3652 {
3653 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003654 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657
Bram Moolenaar21554412017-07-24 21:44:43 +02003658#ifdef FEAT_TERMINAL
3659 if (curbuf->b_term == NULL)
3660#endif
3661 switch (bufIsChanged(curbuf)
3662 + (curbuf->b_p_ro * 2)
3663 + (!curbuf->b_p_ma * 4))
3664 {
3665 case 1: STRCAT(buf, " +"); break;
3666 case 2: STRCAT(buf, " ="); break;
3667 case 3: STRCAT(buf, " =+"); break;
3668 case 4:
3669 case 6: STRCAT(buf, " -"); break;
3670 case 5:
3671 case 7: STRCAT(buf, " -+"); break;
3672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar21554412017-07-24 21:44:43 +02003674 if (curbuf->b_fname != NULL
3675#ifdef FEAT_TERMINAL
3676 && curbuf->b_term == NULL
3677#endif
3678 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
3680 /* Get path of file, replace home dir with ~ */
3681 off = (int)STRLEN(buf);
3682 buf[off++] = ' ';
3683 buf[off++] = '(';
3684 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003685 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686#ifdef BACKSLASH_IN_FILENAME
3687 /* avoid "c:/name" to be reduced to "c" */
3688 if (isalpha(buf[off]) && buf[off + 1] == ':')
3689 off += 2;
3690#endif
3691 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003692 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003694 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003695 /* must be a help buffer */
3696 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003697 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003701
Bram Moolenaar2c666692012-09-05 13:30:40 +02003702 /* Translate unprintable chars and concatenate. Keep some
3703 * room for the server name. When there is no room (very long
3704 * file name) use (...). */
3705 if (off < SPACE_FOR_DIR)
3706 {
3707 p = transstr(buf + off);
3708 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3709 vim_free(p);
3710 }
3711 else
3712 {
3713 vim_strncpy(buf + off, (char_u *)"...",
3714 (size_t)(SPACE_FOR_ARGNR - off));
3715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 STRCAT(buf, ")");
3717 }
3718
Bram Moolenaar2c666692012-09-05 13:30:40 +02003719 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721#if defined(FEAT_CLIENTSERVER)
3722 if (serverName != NULL)
3723 {
3724 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003725 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727 else
3728#endif
3729 STRCAT(buf, " - VIM");
3730
3731 if (maxlen > 0)
3732 {
3733 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003734 if (vim_strsize(buf) > maxlen)
3735 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 }
3738 }
3739 mustset = ti_change(t_str, &lasttitle);
3740
3741 if (p_icon)
3742 {
3743 i_str = buf;
3744 if (*p_iconstring != NUL)
3745 {
3746#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003747 if (stl_syntax & STL_IN_ICON)
3748 {
3749 int use_sandbox = FALSE;
3750 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003751
3752# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003753 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003754# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003755 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003757 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003758 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003759 if (called_emsg)
3760 set_string_option_direct((char_u *)"iconstring", -1,
3761 (char_u *)"", OPT_FREE, SID_ERROR);
3762 called_emsg |= save_called_emsg;
3763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 else
3765#endif
3766 i_str = p_iconstring;
3767 }
3768 else
3769 {
3770 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003771 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 else /* use file name only in icon */
3773 i_name = gettail(curbuf->b_ffname);
3774 *i_str = NUL;
3775 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003776 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 if (len > 100)
3778 {
3779 len -= 100;
3780#ifdef FEAT_MBYTE
3781 if (has_mbyte)
3782 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3783#endif
3784 i_name += len;
3785 }
3786 STRCPY(i_str, i_name);
3787 trans_characters(i_str, IOSIZE);
3788 }
3789 }
3790
3791 mustset |= ti_change(i_str, &lasticon);
3792
3793 if (mustset)
3794 resettitle();
3795}
3796
3797/*
3798 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3799 * from "str" if it does.
3800 * Return TRUE when "*last" changed.
3801 */
3802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003803ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
3805 if ((str == NULL) != (*last == NULL)
3806 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3807 {
3808 vim_free(*last);
3809 if (str == NULL)
3810 *last = NULL;
3811 else
3812 *last = vim_strsave(str);
3813 return TRUE;
3814 }
3815 return FALSE;
3816}
3817
3818/*
3819 * Put current window title back (used after calling a shell)
3820 */
3821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003822resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
3824 mch_settitle(lasttitle, lasticon);
3825}
Bram Moolenaarea408852005-06-25 22:49:46 +00003826
3827# if defined(EXITFREE) || defined(PROTO)
3828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003830{
3831 vim_free(lasttitle);
3832 vim_free(lasticon);
3833}
3834# endif
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#endif /* FEAT_TITLE */
3837
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003838#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003840 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 * Return length of string in screen cells.
3842 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003843 * Normally works for window "wp", except when working for 'tabline' then it
3844 * is "curwin".
3845 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 * Items are drawn interspersed with the text that surrounds it
3847 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3848 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3849 *
3850 * If maxwidth is not zero, the string will be filled at any middle marker
3851 * or truncated if too long, fillchar is used for all whitespace.
3852 */
3853 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003854build_stl_str_hl(
3855 win_T *wp,
3856 char_u *out, /* buffer to write into != NameBuff */
3857 size_t outlen, /* length of out[] */
3858 char_u *fmt,
3859 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3860 int fillchar,
3861 int maxwidth,
3862 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3863 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
3865 char_u *p;
3866 char_u *s;
3867 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003868 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003870 win_T *save_curwin;
3871 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872#endif
3873 int empty_line;
3874 colnr_T virtcol;
3875 long l;
3876 long n;
3877 int prevchar_isflag;
3878 int prevchar_isitem;
3879 int itemisflag;
3880 int fillable;
3881 char_u *str;
3882 long num;
3883 int width;
3884 int itemcnt;
3885 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003886 int group_end_userhl;
3887 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 int groupitem[STL_MAX_ITEM];
3889 int groupdepth;
3890 struct stl_item
3891 {
3892 char_u *start;
3893 int minwid;
3894 int maxwid;
3895 enum
3896 {
3897 Normal,
3898 Empty,
3899 Group,
3900 Middle,
3901 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003902 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 Trunc
3904 } type;
3905 } item[STL_MAX_ITEM];
3906 int minwid;
3907 int maxwid;
3908 int zeropad;
3909 char_u base;
3910 char_u opt;
3911#define TMPLEN 70
3912 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003913 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003914 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003915 int save_must_redraw = must_redraw;
3916 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003917
3918#ifdef FEAT_EVAL
3919 /*
3920 * When the format starts with "%!" then evaluate it as an expression and
3921 * use the result as the actual format string.
3922 */
3923 if (fmt[0] == '%' && fmt[1] == '!')
3924 {
3925 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3926 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003927 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003928 }
3929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931 if (fillchar == 0)
3932 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003933#ifdef FEAT_MBYTE
3934 /* Can't handle a multi-byte fill character yet. */
3935 else if (mb_char2len(fillchar) > 1)
3936 fillchar = '-';
3937#endif
3938
Bram Moolenaar567199b2013-04-24 16:52:36 +02003939 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3940 * p will become invalid when getting another buffer line. */
3941 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3942 empty_line = (*p == NUL);
3943
3944 /* Get the byte value now, in case we need it below. This is more
3945 * efficient than making a copy of the line. */
3946 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3947 byteval = 0;
3948 else
3949#ifdef FEAT_MBYTE
3950 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3951#else
3952 byteval = p[wp->w_cursor.col];
3953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
3955 groupdepth = 0;
3956 p = out;
3957 curitem = 0;
3958 prevchar_isflag = TRUE;
3959 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003960 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003962 if (curitem == STL_MAX_ITEM)
3963 {
3964 /* There are too many items. Add the error code to the statusline
3965 * to give the user a hint about what went wrong. */
3966 if (p + 6 < out + outlen)
3967 {
3968 mch_memmove(p, " E541", (size_t)5);
3969 p += 5;
3970 }
3971 break;
3972 }
3973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (*s != NUL && *s != '%')
3975 prevchar_isflag = prevchar_isitem = FALSE;
3976
3977 /*
3978 * Handle up to the next '%' or the end.
3979 */
3980 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3981 *p++ = *s++;
3982 if (*s == NUL || p + 1 >= out + outlen)
3983 break;
3984
3985 /*
3986 * Handle one '%' item.
3987 */
3988 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003989 if (*s == NUL) /* ignore trailing % */
3990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 if (*s == '%')
3992 {
3993 if (p + 1 >= out + outlen)
3994 break;
3995 *p++ = *s++;
3996 prevchar_isflag = prevchar_isitem = FALSE;
3997 continue;
3998 }
3999 if (*s == STL_MIDDLEMARK)
4000 {
4001 s++;
4002 if (groupdepth > 0)
4003 continue;
4004 item[curitem].type = Middle;
4005 item[curitem++].start = p;
4006 continue;
4007 }
4008 if (*s == STL_TRUNCMARK)
4009 {
4010 s++;
4011 item[curitem].type = Trunc;
4012 item[curitem++].start = p;
4013 continue;
4014 }
4015 if (*s == ')')
4016 {
4017 s++;
4018 if (groupdepth < 1)
4019 continue;
4020 groupdepth--;
4021
4022 t = item[groupitem[groupdepth]].start;
4023 *p = NUL;
4024 l = vim_strsize(t);
4025 if (curitem > groupitem[groupdepth] + 1
4026 && item[groupitem[groupdepth]].minwid == 0)
4027 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004028 /* remove group if all items are empty and highlight group
4029 * doesn't change */
4030 group_start_userhl = group_end_userhl = 0;
4031 for (n = 0; n < groupitem[groupdepth]; n++)
4032 if (item[n].type == Highlight)
4033 group_start_userhl = item[n].minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004035 {
4036 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004038 if (item[n].type == Highlight)
4039 group_end_userhl = item[n].minwid;
4040 }
4041 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 p = t;
4044 l = 0;
4045 }
4046 }
4047 if (l > item[groupitem[groupdepth]].maxwid)
4048 {
4049 /* truncate, remove n bytes of text at the start */
4050#ifdef FEAT_MBYTE
4051 if (has_mbyte)
4052 {
4053 /* Find the first character that should be included. */
4054 n = 0;
4055 while (l >= item[groupitem[groupdepth]].maxwid)
4056 {
4057 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004058 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060 }
4061 else
4062#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004063 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004066 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 p = p - n + 1;
4068#ifdef FEAT_MBYTE
4069 /* Fill up space left over by half a double-wide char. */
4070 while (++l < item[groupitem[groupdepth]].minwid)
4071 *p++ = fillchar;
4072#endif
4073
4074 /* correct the start of the items for the truncation */
4075 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4076 {
4077 item[l].start -= n;
4078 if (item[l].start < t)
4079 item[l].start = t;
4080 }
4081 }
4082 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4083 {
4084 /* fill */
4085 n = item[groupitem[groupdepth]].minwid;
4086 if (n < 0)
4087 {
4088 /* fill by appending characters */
4089 n = 0 - n;
4090 while (l++ < n && p + 1 < out + outlen)
4091 *p++ = fillchar;
4092 }
4093 else
4094 {
4095 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004096 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 l = n - l;
4098 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004099 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 p += l;
4101 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4102 item[n].start += l;
4103 for ( ; l > 0; l--)
4104 *t++ = fillchar;
4105 }
4106 }
4107 continue;
4108 }
4109 minwid = 0;
4110 maxwid = 9999;
4111 zeropad = FALSE;
4112 l = 1;
4113 if (*s == '0')
4114 {
4115 s++;
4116 zeropad = TRUE;
4117 }
4118 if (*s == '-')
4119 {
4120 s++;
4121 l = -1;
4122 }
4123 if (VIM_ISDIGIT(*s))
4124 {
4125 minwid = (int)getdigits(&s);
4126 if (minwid < 0) /* overflow */
4127 minwid = 0;
4128 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004129 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
4131 item[curitem].type = Highlight;
4132 item[curitem].start = p;
4133 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4134 s++;
4135 curitem++;
4136 continue;
4137 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004138 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4139 {
4140 if (*s == STL_TABCLOSENR)
4141 {
4142 if (minwid == 0)
4143 {
4144 /* %X ends the close label, go back to the previously
4145 * define tab label nr. */
4146 for (n = curitem - 1; n >= 0; --n)
4147 if (item[n].type == TabPage && item[n].minwid >= 0)
4148 {
4149 minwid = item[n].minwid;
4150 break;
4151 }
4152 }
4153 else
4154 /* close nrs are stored as negative values */
4155 minwid = - minwid;
4156 }
4157 item[curitem].type = TabPage;
4158 item[curitem].start = p;
4159 item[curitem].minwid = minwid;
4160 s++;
4161 curitem++;
4162 continue;
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 if (*s == '.')
4165 {
4166 s++;
4167 if (VIM_ISDIGIT(*s))
4168 {
4169 maxwid = (int)getdigits(&s);
4170 if (maxwid <= 0) /* overflow */
4171 maxwid = 50;
4172 }
4173 }
4174 minwid = (minwid > 50 ? 50 : minwid) * l;
4175 if (*s == '(')
4176 {
4177 groupitem[groupdepth++] = curitem;
4178 item[curitem].type = Group;
4179 item[curitem].start = p;
4180 item[curitem].minwid = minwid;
4181 item[curitem].maxwid = maxwid;
4182 s++;
4183 curitem++;
4184 continue;
4185 }
4186 if (vim_strchr(STL_ALL, *s) == NULL)
4187 {
4188 s++;
4189 continue;
4190 }
4191 opt = *s++;
4192
4193 /* OK - now for the real work */
4194 base = 'D';
4195 itemisflag = FALSE;
4196 fillable = TRUE;
4197 num = -1;
4198 str = NULL;
4199 switch (opt)
4200 {
4201 case STL_FILEPATH:
4202 case STL_FULLPATH:
4203 case STL_FILENAME:
4204 fillable = FALSE; /* don't change ' ' to fillchar */
4205 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004206 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 else
4208 {
4209 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004210 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4212 }
4213 trans_characters(NameBuff, MAXPATHL);
4214 if (opt != STL_FILENAME)
4215 str = NameBuff;
4216 else
4217 str = gettail(NameBuff);
4218 break;
4219
4220 case STL_VIM_EXPR: /* '{' */
4221 itemisflag = TRUE;
4222 t = p;
4223 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4224 *p++ = *s++;
4225 if (*s != '}') /* missing '}' or out of space */
4226 break;
4227 s++;
4228 *p = 0;
4229 p = t;
4230
4231#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004232 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4234
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004235 save_curbuf = curbuf;
4236 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 curwin = wp;
4238 curbuf = wp->w_buffer;
4239
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004240 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004242 curwin = save_curwin;
4243 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004244 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 if (str != NULL && *str != 0)
4247 {
4248 if (*skipdigits(str) == NUL)
4249 {
4250 num = atoi((char *)str);
4251 vim_free(str);
4252 str = NULL;
4253 itemisflag = FALSE;
4254 }
4255 }
4256#endif
4257 break;
4258
4259 case STL_LINE:
4260 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4261 ? 0L : (long)(wp->w_cursor.lnum);
4262 break;
4263
4264 case STL_NUMLINES:
4265 num = wp->w_buffer->b_ml.ml_line_count;
4266 break;
4267
4268 case STL_COLUMN:
4269 num = !(State & INSERT) && empty_line
4270 ? 0 : (int)wp->w_cursor.col + 1;
4271 break;
4272
4273 case STL_VIRTCOL:
4274 case STL_VIRTCOL_ALT:
4275 /* In list mode virtcol needs to be recomputed */
4276 virtcol = wp->w_virtcol;
4277 if (wp->w_p_list && lcs_tab1 == NUL)
4278 {
4279 wp->w_p_list = FALSE;
4280 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4281 wp->w_p_list = TRUE;
4282 }
4283 ++virtcol;
4284 /* Don't display %V if it's the same as %c. */
4285 if (opt == STL_VIRTCOL_ALT
4286 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4287 ? 0 : (int)wp->w_cursor.col + 1)))
4288 break;
4289 num = (long)virtcol;
4290 break;
4291
4292 case STL_PERCENTAGE:
4293 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4294 (long)wp->w_buffer->b_ml.ml_line_count);
4295 break;
4296
4297 case STL_ALTPERCENT:
4298 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004299 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 break;
4301
4302 case STL_ARGLISTSTAT:
4303 fillable = FALSE;
4304 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004305 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 str = tmp;
4307 break;
4308
4309 case STL_KEYMAP:
4310 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004311 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 str = tmp;
4313 break;
4314 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004315#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004316 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317#else
4318 num = 0;
4319#endif
4320 break;
4321
4322 case STL_BUFNO:
4323 num = wp->w_buffer->b_fnum;
4324 break;
4325
4326 case STL_OFFSET_X:
4327 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004328 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 case STL_OFFSET:
4330#ifdef FEAT_BYTEOFF
4331 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4332 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4333 0L : l + 1 + (!(State & INSERT) && empty_line ?
4334 0 : (int)wp->w_cursor.col);
4335#endif
4336 break;
4337
4338 case STL_BYTEVAL_X:
4339 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004340 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004342 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 if (num == NL)
4344 num = 0;
4345 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4346 num = NL;
4347 break;
4348
4349 case STL_ROFLAG:
4350 case STL_ROFLAG_ALT:
4351 itemisflag = TRUE;
4352 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004353 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 break;
4355
4356 case STL_HELPFLAG:
4357 case STL_HELPFLAG_ALT:
4358 itemisflag = TRUE;
4359 if (wp->w_buffer->b_help)
4360 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004361 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 break;
4363
4364#ifdef FEAT_AUTOCMD
4365 case STL_FILETYPE:
4366 if (*wp->w_buffer->b_p_ft != NUL
4367 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4368 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004369 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4370 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 str = tmp;
4372 }
4373 break;
4374
4375 case STL_FILETYPE_ALT:
4376 itemisflag = TRUE;
4377 if (*wp->w_buffer->b_p_ft != NUL
4378 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4379 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004380 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4381 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 for (t = tmp; *t != 0; t++)
4383 *t = TOUPPER_LOC(*t);
4384 str = tmp;
4385 }
4386 break;
4387#endif
4388
Bram Moolenaar4033c552017-09-16 20:54:51 +02004389#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 case STL_PREVIEWFLAG:
4391 case STL_PREVIEWFLAG_ALT:
4392 itemisflag = TRUE;
4393 if (wp->w_p_pvw)
4394 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4395 : _("[Preview]"));
4396 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004397
4398 case STL_QUICKFIX:
4399 if (bt_quickfix(wp->w_buffer))
4400 str = (char_u *)(wp->w_llist_ref
4401 ? _(msg_loclist)
4402 : _(msg_qflist));
4403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404#endif
4405
4406 case STL_MODIFIED:
4407 case STL_MODIFIED_ALT:
4408 itemisflag = TRUE;
4409 switch ((opt == STL_MODIFIED_ALT)
4410 + bufIsChanged(wp->w_buffer) * 2
4411 + (!wp->w_buffer->b_p_ma) * 4)
4412 {
4413 case 2: str = (char_u *)"[+]"; break;
4414 case 3: str = (char_u *)",+"; break;
4415 case 4: str = (char_u *)"[-]"; break;
4416 case 5: str = (char_u *)",-"; break;
4417 case 6: str = (char_u *)"[+-]"; break;
4418 case 7: str = (char_u *)",+-"; break;
4419 }
4420 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004421
4422 case STL_HIGHLIGHT:
4423 t = s;
4424 while (*s != '#' && *s != NUL)
4425 ++s;
4426 if (*s == '#')
4427 {
4428 item[curitem].type = Highlight;
4429 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004430 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004431 curitem++;
4432 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004433 if (*s != NUL)
4434 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004435 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437
4438 item[curitem].start = p;
4439 item[curitem].type = Normal;
4440 if (str != NULL && *str)
4441 {
4442 t = str;
4443 if (itemisflag)
4444 {
4445 if ((t[0] && t[1])
4446 && ((!prevchar_isitem && *t == ',')
4447 || (prevchar_isflag && *t == ' ')))
4448 t++;
4449 prevchar_isflag = TRUE;
4450 }
4451 l = vim_strsize(t);
4452 if (l > 0)
4453 prevchar_isitem = TRUE;
4454 if (l > maxwid)
4455 {
4456 while (l >= maxwid)
4457#ifdef FEAT_MBYTE
4458 if (has_mbyte)
4459 {
4460 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004461 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463 else
4464#endif
4465 l -= byte2cells(*t++);
4466 if (p + 1 >= out + outlen)
4467 break;
4468 *p++ = '<';
4469 }
4470 if (minwid > 0)
4471 {
4472 for (; l < minwid && p + 1 < out + outlen; l++)
4473 {
4474 /* Don't put a "-" in front of a digit. */
4475 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4476 *p++ = ' ';
4477 else
4478 *p++ = fillchar;
4479 }
4480 minwid = 0;
4481 }
4482 else
4483 minwid *= -1;
4484 while (*t && p + 1 < out + outlen)
4485 {
4486 *p++ = *t++;
4487 /* Change a space by fillchar, unless fillchar is '-' and a
4488 * digit follows. */
4489 if (fillable && p[-1] == ' '
4490 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4491 p[-1] = fillchar;
4492 }
4493 for (; l < minwid && p + 1 < out + outlen; l++)
4494 *p++ = fillchar;
4495 }
4496 else if (num >= 0)
4497 {
4498 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4499 char_u nstr[20];
4500
4501 if (p + 20 >= out + outlen)
4502 break; /* not sufficient space */
4503 prevchar_isitem = TRUE;
4504 t = nstr;
4505 if (opt == STL_VIRTCOL_ALT)
4506 {
4507 *t++ = '-';
4508 minwid--;
4509 }
4510 *t++ = '%';
4511 if (zeropad)
4512 *t++ = '0';
4513 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004514 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 *t = 0;
4516
4517 for (n = num, l = 1; n >= nbase; n /= nbase)
4518 l++;
4519 if (opt == STL_VIRTCOL_ALT)
4520 l++;
4521 if (l > maxwid)
4522 {
4523 l += 2;
4524 n = l - maxwid;
4525 while (l-- > maxwid)
4526 num /= nbase;
4527 *t++ = '>';
4528 *t++ = '%';
4529 *t = t[-3];
4530 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004531 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4532 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004535 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4536 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 p += STRLEN(p);
4538 }
4539 else
4540 item[curitem].type = Empty;
4541
4542 if (opt == STL_VIM_EXPR)
4543 vim_free(str);
4544
4545 if (num >= 0 || (!itemisflag && str && *str))
4546 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4547 curitem++;
4548 }
4549 *p = NUL;
4550 itemcnt = curitem;
4551
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004552#ifdef FEAT_EVAL
4553 if (usefmt != fmt)
4554 vim_free(usefmt);
4555#endif
4556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 width = vim_strsize(out);
4558 if (maxwidth > 0 && width > maxwidth)
4559 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004560 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 l = 0;
4562 if (itemcnt == 0)
4563 s = out;
4564 else
4565 {
4566 for ( ; l < itemcnt; l++)
4567 if (item[l].type == Trunc)
4568 {
4569 /* Truncate at %< item. */
4570 s = item[l].start;
4571 break;
4572 }
4573 if (l == itemcnt)
4574 {
4575 /* No %< item, truncate first item. */
4576 s = item[0].start;
4577 l = 0;
4578 }
4579 }
4580
4581 if (width - vim_strsize(s) >= maxwidth)
4582 {
4583 /* Truncation mark is beyond max length */
4584#ifdef FEAT_MBYTE
4585 if (has_mbyte)
4586 {
4587 s = out;
4588 width = 0;
4589 for (;;)
4590 {
4591 width += ptr2cells(s);
4592 if (width >= maxwidth)
4593 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004594 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 /* Fill up for half a double-wide character. */
4597 while (++width < maxwidth)
4598 *s++ = fillchar;
4599 }
4600 else
4601#endif
4602 s = out + maxwidth - 1;
4603 for (l = 0; l < itemcnt; l++)
4604 if (item[l].start > s)
4605 break;
4606 itemcnt = l;
4607 *s++ = '>';
4608 *s = 0;
4609 }
4610 else
4611 {
4612#ifdef FEAT_MBYTE
4613 if (has_mbyte)
4614 {
4615 n = 0;
4616 while (width >= maxwidth)
4617 {
4618 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004619 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621 }
4622 else
4623#endif
4624 n = width - maxwidth + 1;
4625 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004626 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 *s = '<';
4628
4629 /* Fill up for half a double-wide character. */
4630 while (++width < maxwidth)
4631 {
4632 s = s + STRLEN(s);
4633 *s++ = fillchar;
4634 *s = NUL;
4635 }
4636
4637 --n; /* count the '<' */
4638 for (; l < itemcnt; l++)
4639 {
4640 if (item[l].start - n >= s)
4641 item[l].start -= n;
4642 else
4643 item[l].start = s;
4644 }
4645 }
4646 width = maxwidth;
4647 }
4648 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4649 {
4650 /* Apply STL_MIDDLE if any */
4651 for (l = 0; l < itemcnt; l++)
4652 if (item[l].type == Middle)
4653 break;
4654 if (l < itemcnt)
4655 {
4656 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004657 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 for (s = item[l].start; s < p; s++)
4659 *s = fillchar;
4660 for (l++; l < itemcnt; l++)
4661 item[l].start += maxwidth - width;
4662 width = maxwidth;
4663 }
4664 }
4665
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004666 /* Store the info about highlighting. */
4667 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004669 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 for (l = 0; l < itemcnt; l++)
4671 {
4672 if (item[l].type == Highlight)
4673 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004674 sp->start = item[l].start;
4675 sp->userhl = item[l].minwid;
4676 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
4678 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004679 sp->start = NULL;
4680 sp->userhl = 0;
4681 }
4682
4683 /* Store the info about tab pages labels. */
4684 if (tabtab != NULL)
4685 {
4686 sp = tabtab;
4687 for (l = 0; l < itemcnt; l++)
4688 {
4689 if (item[l].type == TabPage)
4690 {
4691 sp->start = item[l].start;
4692 sp->userhl = item[l].minwid;
4693 sp++;
4694 }
4695 }
4696 sp->start = NULL;
4697 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
4699
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004700 /* When inside update_screen we do not want redrawing a stausline, ruler,
4701 * title, etc. to trigger another redraw, it may cause an endless loop. */
4702 if (updating_screen)
4703 {
4704 must_redraw = save_must_redraw;
4705 curwin->w_redr_type = save_redr_type;
4706 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004707
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return width;
4709}
4710#endif /* FEAT_STL_OPT */
4711
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004712#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4713 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004715 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4716 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 */
4718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004719get_rel_pos(
4720 win_T *wp,
4721 char_u *buf,
4722 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 long above; /* number of lines above window */
4725 long below; /* number of lines below window */
4726
Bram Moolenaar0027c212015-01-07 13:31:52 +01004727 if (buflen < 3) /* need at least 3 chars for writing */
4728 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 above = wp->w_topline - 1;
4730#ifdef FEAT_DIFF
4731 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004732 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4733 above = 0; /* All buffer lines are displayed and there is an
4734 * indication of filler lines, that can be considered
4735 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736#endif
4737 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4738 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004739 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4740 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004742 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004744 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 ? (int)(above / ((above + below) / 100L))
4746 : (int)(above * 100L / (above + below)));
4747}
4748#endif
4749
4750/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004751 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 * Return TRUE if it was appended.
4753 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004755append_arg_number(
4756 win_T *wp,
4757 char_u *buf,
4758 int buflen,
4759 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761 char_u *p;
4762
4763 if (ARGCOUNT <= 1) /* nothing to do */
4764 return FALSE;
4765
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004766 p = buf + STRLEN(buf); /* go to the end of the buffer */
4767 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 return FALSE;
4769 *p++ = ' ';
4770 *p++ = '(';
4771 if (add_file)
4772 {
4773 STRCPY(p, "file ");
4774 p += 5;
4775 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004776 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4777 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4779 return TRUE;
4780}
4781
4782/*
4783 * If fname is not a full path, make it a full path.
4784 * Returns pointer to allocated memory (NULL for failure).
4785 */
4786 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004787fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
4789 /*
4790 * Force expanding the path always for Unix, because symbolic links may
4791 * mess up the full path name, even though it starts with a '/'.
4792 * Also expand when there is ".." in the file name, try to remove it,
4793 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004794 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 * For MS-Windows also expand names like "longna~1" to "longname".
4796 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004797#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 return FullName_save(fname, TRUE);
4799#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004800 if (!vim_isAbsName(fname)
4801 || strstr((char *)fname, "..") != NULL
4802 || strstr((char *)fname, "//") != NULL
4803# ifdef BACKSLASH_IN_FILENAME
4804 || strstr((char *)fname, "\\\\") != NULL
4805# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004806# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 )
4810 return FullName_save(fname, FALSE);
4811
4812 fname = vim_strsave(fname);
4813
Bram Moolenaar9b942202007-10-03 12:31:33 +00004814# ifdef USE_FNAME_CASE
4815# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
4819 if (fname != NULL)
4820 fname_case(fname, 0); /* set correct case for file name */
4821 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823
4824 return fname;
4825#endif
4826}
4827
4828/*
4829 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4830 * "ffname" becomes a pointer to allocated memory (or NULL).
4831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004833fname_expand(
4834 buf_T *buf UNUSED,
4835 char_u **ffname,
4836 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
4838 if (*ffname == NULL) /* if no file name given, nothing to do */
4839 return;
4840 if (*sfname == NULL) /* if no short file name given, use ffname */
4841 *sfname = *ffname;
4842 *ffname = fix_fname(*ffname); /* expand to full path */
4843
4844#ifdef FEAT_SHORTCUT
4845 if (!buf->b_p_bin)
4846 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004847 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
4849 /* If the file name is a shortcut file, use the file it links to. */
4850 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004851 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 {
4853 vim_free(*ffname);
4854 *ffname = rfname;
4855 *sfname = rfname;
4856 }
4857 }
4858#endif
4859}
4860
4861/*
4862 * Get the file name for an argument list entry.
4863 */
4864 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004865alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
4867 buf_T *bp;
4868
4869 /* Use the name from the associated buffer if it exists. */
4870 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004871 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 return aep->ae_fname;
4873 return bp->b_fname;
4874}
4875
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876/*
4877 * do_arg_all(): Open up to 'count' windows, one for each argument.
4878 */
4879 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004880do_arg_all(
4881 int count,
4882 int forceit, /* hide buffers in current windows */
4883 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884{
4885 int i;
4886 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004887 char_u *opened; /* Array of weight for which args are open:
4888 * 0: not opened
4889 * 1: opened in other tab
4890 * 2: opened in curtab
4891 * 3: opened in curtab and curwin
4892 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004893 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 int use_firstwin = FALSE; /* use first window for arglist */
4895 int split_ret = OK;
4896 int p_ea_save;
4897 alist_T *alist; /* argument list to be used */
4898 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004899 tabpage_T *tpnext;
4900 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004901 win_T *old_curwin, *last_curwin;
4902 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004903 win_T *new_curwin = NULL;
4904 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
4906 if (ARGCOUNT <= 0)
4907 {
4908 /* Don't give an error message. We don't want it when the ":all"
4909 * command is in the .vimrc. */
4910 return;
4911 }
4912 setpcmark();
4913
4914 opened_len = ARGCOUNT;
4915 opened = alloc_clear((unsigned)opened_len);
4916 if (opened == NULL)
4917 return;
4918
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004919 /* Autocommands may do anything to the argument list. Make sure it's not
4920 * freed while we are working here by "locking" it. We still have to
4921 * watch out for its size to be changed. */
4922 alist = curwin->w_alist;
4923 ++alist->al_refcount;
4924
4925 old_curwin = curwin;
4926 old_curtab = curtab;
4927
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004928# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931
4932 /*
4933 * Try closing all windows that are not in the argument list.
4934 * Also close windows that are not full width;
4935 * When 'hidden' or "forceit" set the buffer becomes hidden.
4936 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004937 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004939 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004940 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004941 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004943 tpnext = curtab->tp_next;
4944 for (wp = firstwin; wp != NULL; wp = wpnext)
4945 {
4946 wpnext = wp->w_next;
4947 buf = wp->w_buffer;
4948 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004949 || (!keep_tabs && (buf->b_nwindows > 1
4950 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004951 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004952 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004954 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004955 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004957 if (i < alist->al_ga.ga_len
4958 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4959 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4960 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004962 int weight = 1;
4963
4964 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004965 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004966 ++weight;
4967 if (old_curwin == wp)
4968 ++weight;
4969 }
4970
4971 if (weight > (int)opened[i])
4972 {
4973 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004974 if (i == 0)
4975 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004976 if (new_curwin != NULL)
4977 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004978 new_curwin = wp;
4979 new_curtab = curtab;
4980 }
4981 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004982 else if (keep_tabs)
4983 i = opened_len;
4984
4985 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004986 {
4987 /* Use the current argument list for all windows
4988 * containing a file from it. */
4989 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004990 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004991 ++wp->w_alist->al_refcount;
4992 }
4993 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004997 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004999 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005001 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005002 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005004 /* If the buffer was changed, and we would like to hide it,
5005 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005006 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005007 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005009#ifdef FEAT_AUTOCMD
5010 bufref_T bufref;
5011
5012 set_bufref(&bufref, buf);
5013#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005014 (void)autowrite(buf, FALSE);
5015#ifdef FEAT_AUTOCMD
5016 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005017 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005018 {
5019 wpnext = firstwin; /* start all over... */
5020 continue;
5021 }
5022#endif
5023 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005024 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005025 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005026 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005027 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005028 else
5029 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005030 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaar4033c552017-09-16 20:54:51 +02005031#ifdef FEAT_AUTOCMD
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005032 /* check if autocommands removed the next window */
5033 if (!win_valid(wpnext))
5034 wpnext = firstwin; /* start all over... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005040
5041 /* Without the ":tab" modifier only do the current tab page. */
5042 if (had_tab == 0 || tpnext == NULL)
5043 break;
5044
5045# ifdef FEAT_AUTOCMD
5046 /* check if autocommands removed the next tab page */
5047 if (!valid_tabpage(tpnext))
5048 tpnext = first_tabpage; /* start all over...*/
5049# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005050 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052
5053 /*
5054 * Open a window for files in the argument list that don't have one.
5055 * ARGCOUNT may change while doing this, because of autocommands.
5056 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005057 if (count > opened_len || count <= 0)
5058 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060#ifdef FEAT_AUTOCMD
5061 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5062 ++autocmd_no_enter;
5063 ++autocmd_no_leave;
5064#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005065 last_curwin = curwin;
5066 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005068 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5069 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005070 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005071 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5072 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005073
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005074 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
5076 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5077 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005078 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 /* Move the already present window to below the current window */
5081 if (curwin->w_arg_idx != i)
5082 {
5083 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5084 {
5085 if (wpnext->w_arg_idx == i)
5086 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005087 if (keep_tabs)
5088 {
5089 new_curwin = wpnext;
5090 new_curtab = curtab;
5091 }
5092 else
5093 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 break;
5095 }
5096 }
5097 }
5098 }
5099 else if (split_ret == OK)
5100 {
5101 if (!use_firstwin) /* split current window */
5102 {
5103 p_ea_save = p_ea;
5104 p_ea = TRUE; /* use space from all windows */
5105 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5106 p_ea = p_ea_save;
5107 if (split_ret == FAIL)
5108 continue;
5109 }
5110#ifdef FEAT_AUTOCMD
5111 else /* first window: do autocmd for leaving this buffer */
5112 --autocmd_no_leave;
5113#endif
5114
5115 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005116 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 */
5118 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005119 if (i == 0)
5120 {
5121 new_curwin = curwin;
5122 new_curtab = curtab;
5123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5125 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005126 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005128 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#ifdef FEAT_AUTOCMD
5130 if (use_firstwin)
5131 ++autocmd_no_leave;
5132#endif
5133 use_firstwin = FALSE;
5134 }
5135 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005136
5137 /* When ":tab" was used open a new tab for a new window repeatedly. */
5138 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5139 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141
5142 /* Remove the "lock" on the argument list. */
5143 alist_unlink(alist);
5144
5145#ifdef FEAT_AUTOCMD
5146 --autocmd_no_enter;
5147#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005148 /* restore last referenced tabpage's curwin */
5149 if (last_curtab != new_curtab)
5150 {
5151 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005152 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005153 if (win_valid(last_curwin))
5154 win_enter(last_curwin, FALSE);
5155 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005156 /* to window with first arg */
5157 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005158 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005159 if (win_valid(new_curwin))
5160 win_enter(new_curwin, FALSE);
5161
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162#ifdef FEAT_AUTOCMD
5163 --autocmd_no_leave;
5164#endif
5165 vim_free(opened);
5166}
5167
5168# if defined(FEAT_LISTCMDS) || defined(PROTO)
5169/*
5170 * Open a window for a number of buffers.
5171 */
5172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005173ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174{
5175 buf_T *buf;
5176 win_T *wp, *wpnext;
5177 int split_ret = OK;
5178 int p_ea_save;
5179 int open_wins = 0;
5180 int r;
5181 int count; /* Maximum number of windows to open. */
5182 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005183 int had_tab = cmdmod.tab;
5184 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186 if (eap->addr_count == 0) /* make as many windows as possible */
5187 count = 9999;
5188 else
5189 count = eap->line2; /* make as many windows as specified */
5190 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5191 all = FALSE;
5192 else
5193 all = TRUE;
5194
5195 setpcmark();
5196
5197#ifdef FEAT_GUI
5198 need_mouse_correct = TRUE;
5199#endif
5200
5201 /*
5202 * Close superfluous windows (two windows for the same buffer).
5203 * Also close windows that are not full-width.
5204 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005205 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005206 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005207 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005209 tpnext = curtab->tp_next;
5210 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005212 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005213 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005214 || ((cmdmod.split & WSP_VERT)
5215 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005216 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005217 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005218 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaar362ce482012-06-06 19:02:45 +02005219#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005220 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005221#endif
5222 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005223 {
5224 win_close(wp, FALSE);
5225#ifdef FEAT_AUTOCMD
5226 wpnext = firstwin; /* just in case an autocommand does
5227 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005228 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005229 open_wins = 0;
5230#endif
5231 }
5232 else
5233 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005235
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005236 /* Without the ":tab" modifier only do the current tab page. */
5237 if (had_tab == 0 || tpnext == NULL)
5238 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005239 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241
5242 /*
5243 * Go through the buffer list. When a buffer doesn't have a window yet,
5244 * open one. Otherwise move the window to the right position.
5245 * Watch out for autocommands that delete buffers or windows!
5246 */
5247#ifdef FEAT_AUTOCMD
5248 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5249 ++autocmd_no_enter;
5250#endif
5251 win_enter(lastwin, FALSE);
5252#ifdef FEAT_AUTOCMD
5253 ++autocmd_no_leave;
5254#endif
5255 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5256 {
5257 /* Check if this buffer needs a window */
5258 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5259 continue;
5260
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005261 if (had_tab != 0)
5262 {
5263 /* With the ":tab" modifier don't move the window. */
5264 if (buf->b_nwindows > 0)
5265 wp = lastwin; /* buffer has a window, skip it */
5266 else
5267 wp = NULL;
5268 }
5269 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005270 {
5271 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005272 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005273 if (wp->w_buffer == buf)
5274 break;
5275 /* If the buffer already has a window, move it */
5276 if (wp != NULL)
5277 win_move_after(wp, curwin);
5278 }
5279
5280 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005282#ifdef FEAT_AUTOCMD
5283 bufref_T bufref;
5284
5285 set_bufref(&bufref, buf);
5286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 /* Split the window and put the buffer in it */
5288 p_ea_save = p_ea;
5289 p_ea = TRUE; /* use space from all windows */
5290 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5291 ++open_wins;
5292 p_ea = p_ea_save;
5293 if (split_ret == FAIL)
5294 continue;
5295
5296 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005297#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 swap_exists_action = SEA_DIALOG;
5299#endif
5300 set_curbuf(buf, DOBUF_GOTO);
5301#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005302 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005304 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005305#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 break;
5309 }
5310#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005311#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 if (swap_exists_action == SEA_QUIT)
5313 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005314# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5315 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005316
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005317 /* Reset the error/interrupt/exception state here so that
5318 * aborting() returns FALSE when closing a window. */
5319 enter_cleanup(&cs);
5320# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005321
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005322 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 win_close(curwin, TRUE);
5324 --open_wins;
5325 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005326 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005327
5328# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5329 /* Restore the error/interrupt/exception state if not
5330 * discarded by a new aborting error, interrupt, or uncaught
5331 * exception. */
5332 leave_cleanup(&cs);
5333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335 else
5336 handle_swap_exists(NULL);
5337#endif
5338 }
5339
5340 ui_breakcheck();
5341 if (got_int)
5342 {
5343 (void)vgetc(); /* only break the file loading, not the rest */
5344 break;
5345 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005346#ifdef FEAT_EVAL
5347 /* Autocommands deleted the buffer or aborted script processing!!! */
5348 if (aborting())
5349 break;
5350#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005351 /* When ":tab" was used open a new tab for a new window repeatedly. */
5352 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5353 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355#ifdef FEAT_AUTOCMD
5356 --autocmd_no_enter;
5357#endif
5358 win_enter(firstwin, FALSE); /* back to first window */
5359#ifdef FEAT_AUTOCMD
5360 --autocmd_no_leave;
5361#endif
5362
5363 /*
5364 * Close superfluous windows.
5365 */
5366 for (wp = lastwin; open_wins > count; )
5367 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005368 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 || autowrite(wp->w_buffer, FALSE) == OK);
5370#ifdef FEAT_AUTOCMD
5371 if (!win_valid(wp))
5372 {
5373 /* BufWrite Autocommands made the window invalid, start over */
5374 wp = lastwin;
5375 }
5376 else
5377#endif
5378 if (r)
5379 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005380 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 --open_wins;
5382 wp = lastwin;
5383 }
5384 else
5385 {
5386 wp = wp->w_prev;
5387 if (wp == NULL)
5388 break;
5389 }
5390 }
5391}
5392# endif /* FEAT_LISTCMDS */
5393
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005395static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005396
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397/*
5398 * do_modelines() - process mode lines for the current file
5399 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005400 * "flags" can be:
5401 * OPT_WINONLY only set options local to window
5402 * OPT_NOWIN don't set options local to window
5403 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 * Returns immediately if the "ml" option isn't set.
5405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005407do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005409 linenr_T lnum;
5410 int nmlines;
5411 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
5413 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5414 return;
5415
5416 /* Disallow recursive entry here. Can happen when executing a modeline
5417 * triggers an autocommand, which reloads modelines with a ":do". */
5418 if (entered)
5419 return;
5420
5421 ++entered;
5422 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5423 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005424 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 nmlines = 0;
5426
5427 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5428 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005429 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 nmlines = 0;
5431 --entered;
5432}
5433
5434#include "version.h" /* for version number */
5435
5436/*
5437 * chk_modeline() - check a single line for a mode string
5438 * Return FAIL if an error encountered.
5439 */
5440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005441chk_modeline(
5442 linenr_T lnum,
5443 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
5445 char_u *s;
5446 char_u *e;
5447 char_u *linecopy; /* local copy of any modeline found */
5448 int prev;
5449 int vers;
5450 int end;
5451 int retval = OK;
5452 char_u *save_sourcing_name;
5453 linenr_T save_sourcing_lnum;
5454#ifdef FEAT_EVAL
5455 scid_T save_SID;
5456#endif
5457
5458 prev = -1;
5459 for (s = ml_get(lnum); *s != NUL; ++s)
5460 {
5461 if (prev == -1 || vim_isspace(prev))
5462 {
5463 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5464 || STRNCMP(s, "vi:", (size_t)3) == 0)
5465 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005466 /* Accept both "vim" and "Vim". */
5467 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 {
5469 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5470 e = s + 4;
5471 else
5472 e = s + 3;
5473 vers = getdigits(&e);
5474 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005475 && (s[0] != 'V'
5476 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 && (s[3] == ':'
5478 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5479 || (VIM_VERSION_100 < vers && s[3] == '<')
5480 || (VIM_VERSION_100 > vers && s[3] == '>')
5481 || (VIM_VERSION_100 == vers && s[3] == '=')))
5482 break;
5483 }
5484 }
5485 prev = *s;
5486 }
5487
5488 if (*s)
5489 {
5490 do /* skip over "ex:", "vi:" or "vim:" */
5491 ++s;
5492 while (s[-1] != ':');
5493
5494 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5495 if (linecopy == NULL)
5496 return FAIL;
5497
5498 save_sourcing_lnum = sourcing_lnum;
5499 save_sourcing_name = sourcing_name;
5500 sourcing_lnum = lnum; /* prepare for emsg() */
5501 sourcing_name = (char_u *)"modelines";
5502
5503 end = FALSE;
5504 while (end == FALSE)
5505 {
5506 s = skipwhite(s);
5507 if (*s == NUL)
5508 break;
5509
5510 /*
5511 * Find end of set command: ':' or end of line.
5512 * Skip over "\:", replacing it with ":".
5513 */
5514 for (e = s; *e != ':' && *e != NUL; ++e)
5515 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005516 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (*e == NUL)
5518 end = TRUE;
5519
5520 /*
5521 * If there is a "set" command, require a terminating ':' and
5522 * ignore the stuff after the ':'.
5523 * "vi:set opt opt opt: foo" -- foo not interpreted
5524 * "vi:opt opt opt: foo" -- foo interpreted
5525 * Accept "se" for compatibility with Elvis.
5526 */
5527 if (STRNCMP(s, "set ", (size_t)4) == 0
5528 || STRNCMP(s, "se ", (size_t)3) == 0)
5529 {
5530 if (*e != ':') /* no terminating ':'? */
5531 break;
5532 end = TRUE;
5533 s = vim_strchr(s, ' ') + 1;
5534 }
5535 *e = NUL; /* truncate the set command */
5536
5537 if (*s != NUL) /* skip over an empty "::" */
5538 {
5539#ifdef FEAT_EVAL
5540 save_SID = current_SID;
5541 current_SID = SID_MODELINE;
5542#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005543 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544#ifdef FEAT_EVAL
5545 current_SID = save_SID;
5546#endif
5547 if (retval == FAIL) /* stop if error found */
5548 break;
5549 }
5550 s = e + 1; /* advance to next part */
5551 }
5552
5553 sourcing_lnum = save_sourcing_lnum;
5554 sourcing_name = save_sourcing_name;
5555
5556 vim_free(linecopy);
5557 }
5558 return retval;
5559}
5560
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005561#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005563read_viminfo_bufferlist(
5564 vir_T *virp,
5565 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 char_u *tab;
5568 linenr_T lnum;
5569 colnr_T col;
5570 buf_T *buf;
5571 char_u *sfname;
5572 char_u *xline;
5573
5574 /* Handle long line and escaped characters. */
5575 xline = viminfo_readstring(virp, 1, FALSE);
5576
5577 /* don't read in if there are files on the command-line or if writing: */
5578 if (xline != NULL && !writing && ARGCOUNT == 0
5579 && find_viminfo_parameter('%') != NULL)
5580 {
5581 /* Format is: <fname> Tab <lnum> Tab <col>.
5582 * Watch out for a Tab in the file name, work from the end. */
5583 lnum = 0;
5584 col = 0;
5585 tab = vim_strrchr(xline, '\t');
5586 if (tab != NULL)
5587 {
5588 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005589 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 tab = vim_strrchr(xline, '\t');
5591 if (tab != NULL)
5592 {
5593 *tab++ = '\0';
5594 lnum = atol((char *)tab);
5595 }
5596 }
5597
5598 /* Expand "~/" in the file name at "line + 1" to a full path.
5599 * Then try shortening it by comparing with the current directory */
5600 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005601 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
5603 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5604 if (buf != NULL) /* just in case... */
5605 {
5606 buf->b_last_cursor.lnum = lnum;
5607 buf->b_last_cursor.col = col;
5608 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5609 }
5610 }
5611 vim_free(xline);
5612
5613 return viminfo_readline(virp);
5614}
5615
5616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005617write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005621 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005623 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625 if (find_viminfo_parameter('%') == NULL)
5626 return;
5627
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005628 /* Without a number -1 is returned: do all buffers. */
5629 max_buffers = get_viminfo_parameter('%');
5630
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005632#define LINE_BUF_LEN (MAXPATHL + 40)
5633 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 if (line == NULL)
5635 return;
5636
Bram Moolenaarf740b292006-02-16 22:11:02 +00005637 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005640 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005641 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 if (buf->b_fname == NULL
5644 || !buf->b_p_bl
5645#ifdef FEAT_QUICKFIX
5646 || bt_quickfix(buf)
5647#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005648#ifdef FEAT_TERMINAL
5649 || bt_terminal(buf)
5650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 || removable(buf->b_ffname))
5652 continue;
5653
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005654 if (max_buffers-- == 0)
5655 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 putc('%', fp);
5657 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005658 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 (long)buf->b_last_cursor.lnum,
5660 buf->b_last_cursor.col);
5661 viminfo_writestring(fp, line);
5662 }
5663 vim_free(line);
5664}
5665#endif
5666
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005667/*
5668 * Return TRUE if "buf" is the quickfix buffer.
5669 */
5670 int
5671bt_quickfix(buf_T *buf)
5672{
5673 return buf != NULL && buf->b_p_bt[0] == 'q';
5674}
5675
5676/*
5677 * Return TRUE if "buf" is a terminal buffer.
5678 */
5679 int
5680bt_terminal(buf_T *buf)
5681{
5682 return buf != NULL && buf->b_p_bt[0] == 't';
5683}
5684
5685/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005686 * Return TRUE if "buf" is a help buffer.
5687 */
5688 int
5689bt_help(buf_T *buf)
5690{
5691 return buf != NULL && buf->b_help;
5692}
5693
5694/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005695 * Return TRUE if "buf" is a "nofile", "acwrite" or "terminal" buffer.
5696 * This means the buffer name is not a file name.
5697 */
5698 int
5699bt_nofile(buf_T *buf)
5700{
5701 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5702 || buf->b_p_bt[0] == 'a'
5703 || buf->b_p_bt[0] == 't');
5704}
5705
5706/*
5707 * Return TRUE if "buf" is a "nowrite", "nofile" or "terminal" buffer.
5708 */
5709 int
5710bt_dontwrite(buf_T *buf)
5711{
5712 return buf != NULL && (buf->b_p_bt[0] == 'n' || buf->b_p_bt[0] == 't');
5713}
5714
5715 int
5716bt_dontwrite_msg(buf_T *buf)
5717{
5718 if (bt_dontwrite(buf))
5719 {
5720 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5721 return TRUE;
5722 }
5723 return FALSE;
5724}
5725
5726/*
5727 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5728 * and 'bufhidden'.
5729 */
5730 int
5731buf_hide(buf_T *buf)
5732{
5733 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5734 switch (buf->b_p_bh[0])
5735 {
5736 case 'u': /* "unload" */
5737 case 'w': /* "wipe" */
5738 case 'd': return FALSE; /* "delete" */
5739 case 'h': return TRUE; /* "hide" */
5740 }
5741 return (p_hid || cmdmod.hide);
5742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
5744/*
5745 * Return special buffer name.
5746 * Returns NULL when the buffer has a normal file name.
5747 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005748 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005749buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005751#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005753 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005754 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005755 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005756
5757 /*
5758 * For location list window, w_llist_ref points to the location list.
5759 * For quickfix window, w_llist_ref is NULL.
5760 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005761 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005762 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005763 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005764 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005767
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005769 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 if (bt_nofile(buf))
5771 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005772#ifdef FEAT_TERMINAL
5773 if (buf->b_term != NULL)
5774 return term_get_status_text(buf->b_term);
5775#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005776 if (buf->b_fname != NULL)
5777 return buf->b_fname;
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005778 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005780
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005782 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 return NULL;
5784}
5785
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005786#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005787 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5788 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005789# define SWITCH_TO_WIN
5790
5791/*
5792 * Find a window that contains "buf" and switch to it.
5793 * If there is no such window, use the current window and change "curbuf".
5794 * Caller must initialize save_curbuf to NULL.
5795 * restore_win_for_buf() MUST be called later!
5796 */
5797 void
5798switch_to_win_for_buf(
5799 buf_T *buf,
5800 win_T **save_curwinp,
5801 tabpage_T **save_curtabp,
5802 bufref_T *save_curbuf)
5803{
5804 win_T *wp;
5805 tabpage_T *tp;
5806
5807 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5808 switch_buffer(save_curbuf, buf);
5809 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5810 {
5811 restore_win(*save_curwinp, *save_curtabp, TRUE);
5812 switch_buffer(save_curbuf, buf);
5813 }
5814}
5815
5816 void
5817restore_win_for_buf(
5818 win_T *save_curwin,
5819 tabpage_T *save_curtab,
5820 bufref_T *save_curbuf)
5821{
5822 if (save_curbuf->br_buf == NULL)
5823 restore_win(save_curwin, save_curtab, TRUE);
5824 else
5825 restore_buffer(save_curbuf);
5826}
5827#endif
5828
Bram Moolenaar4033c552017-09-16 20:54:51 +02005829#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005830/*
5831 * Find a window for buffer "buf".
5832 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5833 * If not found FAIL is returned.
5834 */
5835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005836find_win_for_buf(
5837 buf_T *buf,
5838 win_T **wp,
5839 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005840{
5841 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5842 if ((*wp)->w_buffer == buf)
5843 goto win_found;
5844 return FAIL;
5845win_found:
5846 return OK;
5847}
5848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
5850#if defined(FEAT_SIGNS) || defined(PROTO)
5851/*
5852 * Insert the sign into the signlist.
5853 */
5854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005855insert_sign(
5856 buf_T *buf, /* buffer to store sign in */
5857 signlist_T *prev, /* previous sign entry */
5858 signlist_T *next, /* next sign entry */
5859 int id, /* sign ID */
5860 linenr_T lnum, /* line number which gets the mark */
5861 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 signlist_T *newsign;
5864
5865 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5866 if (newsign != NULL)
5867 {
5868 newsign->id = id;
5869 newsign->lnum = lnum;
5870 newsign->typenr = typenr;
5871 newsign->next = next;
5872#ifdef FEAT_NETBEANS_INTG
5873 newsign->prev = prev;
5874 if (next != NULL)
5875 next->prev = newsign;
5876#endif
5877
5878 if (prev == NULL)
5879 {
5880 /* When adding first sign need to redraw the windows to create the
5881 * column for signs. */
5882 if (buf->b_signlist == NULL)
5883 {
5884 redraw_buf_later(buf, NOT_VALID);
5885 changed_cline_bef_curs();
5886 }
5887
5888 /* first sign in signlist */
5889 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005890#ifdef FEAT_NETBEANS_INTG
5891 if (netbeans_active())
5892 buf->b_has_sign_column = TRUE;
5893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 }
5895 else
5896 prev->next = newsign;
5897 }
5898}
5899
5900/*
5901 * Add the sign into the signlist. Find the right spot to do it though.
5902 */
5903 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005904buf_addsign(
5905 buf_T *buf, /* buffer to store sign in */
5906 int id, /* sign ID */
5907 linenr_T lnum, /* line number which gets the mark */
5908 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 signlist_T *sign; /* a sign in the signlist */
5911 signlist_T *prev; /* the previous sign */
5912
5913 prev = NULL;
5914 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5915 {
5916 if (lnum == sign->lnum && id == sign->id)
5917 {
5918 sign->typenr = typenr;
5919 return;
5920 }
5921 else if (
5922#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5923 id < 0 &&
5924#endif
5925 lnum < sign->lnum)
5926 {
5927#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5928 /* XXX - GRP: Is this because of sign slide problem? Or is it
5929 * really needed? Or is it because we allow multiple signs per
5930 * line? If so, should I add that feature to FEAT_SIGNS?
5931 */
5932 while (prev != NULL && prev->lnum == lnum)
5933 prev = prev->prev;
5934 if (prev == NULL)
5935 sign = buf->b_signlist;
5936 else
5937 sign = prev->next;
5938#endif
5939 insert_sign(buf, prev, sign, id, lnum, typenr);
5940 return;
5941 }
5942 prev = sign;
5943 }
5944#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5945 /* XXX - GRP: See previous comment */
5946 while (prev != NULL && prev->lnum == lnum)
5947 prev = prev->prev;
5948 if (prev == NULL)
5949 sign = buf->b_signlist;
5950 else
5951 sign = prev->next;
5952#endif
5953 insert_sign(buf, prev, sign, id, lnum, typenr);
5954
5955 return;
5956}
5957
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005958/*
5959 * For an existing, placed sign "markId" change the type to "typenr".
5960 * Returns the line number of the sign, or zero if the sign is not found.
5961 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005962 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005963buf_change_sign_type(
5964 buf_T *buf, /* buffer to store sign in */
5965 int markId, /* sign ID */
5966 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967{
5968 signlist_T *sign; /* a sign in the signlist */
5969
5970 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5971 {
5972 if (sign->id == markId)
5973 {
5974 sign->typenr = typenr;
5975 return sign->lnum;
5976 }
5977 }
5978
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005979 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980}
5981
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005983buf_getsigntype(
5984 buf_T *buf,
5985 linenr_T lnum,
5986 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
5988 signlist_T *sign; /* a sign in a b_signlist */
5989
5990 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5991 if (sign->lnum == lnum
5992 && (type == SIGN_ANY
5993# ifdef FEAT_SIGN_ICONS
5994 || (type == SIGN_ICON
5995 && sign_get_image(sign->typenr) != NULL)
5996# endif
5997 || (type == SIGN_TEXT
5998 && sign_get_text(sign->typenr) != NULL)
5999 || (type == SIGN_LINEHL
6000 && sign_get_attr(sign->typenr, TRUE) != 0)))
6001 return sign->typenr;
6002 return 0;
6003}
6004
6005
6006 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006007buf_delsign(
6008 buf_T *buf, /* buffer sign is stored in */
6009 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
6011 signlist_T **lastp; /* pointer to pointer to current sign */
6012 signlist_T *sign; /* a sign in a b_signlist */
6013 signlist_T *next; /* the next sign in a b_signlist */
6014 linenr_T lnum; /* line number whose sign was deleted */
6015
6016 lastp = &buf->b_signlist;
6017 lnum = 0;
6018 for (sign = buf->b_signlist; sign != NULL; sign = next)
6019 {
6020 next = sign->next;
6021 if (sign->id == id)
6022 {
6023 *lastp = next;
6024#ifdef FEAT_NETBEANS_INTG
6025 if (next != NULL)
6026 next->prev = sign->prev;
6027#endif
6028 lnum = sign->lnum;
6029 vim_free(sign);
6030 break;
6031 }
6032 else
6033 lastp = &sign->next;
6034 }
6035
6036 /* When deleted the last sign need to redraw the windows to remove the
6037 * sign column. */
6038 if (buf->b_signlist == NULL)
6039 {
6040 redraw_buf_later(buf, NOT_VALID);
6041 changed_cline_bef_curs();
6042 }
6043
6044 return lnum;
6045}
6046
6047
6048/*
6049 * Find the line number of the sign with the requested id. If the sign does
6050 * not exist, return 0 as the line number. This will still let the correct file
6051 * get loaded.
6052 */
6053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006054buf_findsign(
6055 buf_T *buf, /* buffer to store sign in */
6056 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 signlist_T *sign; /* a sign in the signlist */
6059
6060 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6061 if (sign->id == id)
6062 return sign->lnum;
6063
6064 return 0;
6065}
6066
6067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006068buf_findsign_id(
6069 buf_T *buf, /* buffer whose sign we are searching for */
6070 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 signlist_T *sign; /* a sign in the signlist */
6073
6074 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6075 if (sign->lnum == lnum)
6076 return sign->id;
6077
6078 return 0;
6079}
6080
6081
6082# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
6083/* see if a given type of sign exists on a specific line */
6084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085buf_findsigntype_id(
6086 buf_T *buf, /* buffer whose sign we are searching for */
6087 linenr_T lnum, /* line number of sign */
6088 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
6090 signlist_T *sign; /* a sign in the signlist */
6091
6092 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6093 if (sign->lnum == lnum && sign->typenr == typenr)
6094 return sign->id;
6095
6096 return 0;
6097}
6098
6099
6100# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6101/* return the number of icons on the given line */
6102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006103buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 signlist_T *sign; /* a sign in the signlist */
6106 int count = 0;
6107
6108 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6109 if (sign->lnum == lnum)
6110 if (sign_get_image(sign->typenr) != NULL)
6111 count++;
6112
6113 return count;
6114}
6115# endif /* FEAT_SIGN_ICONS */
6116# endif /* FEAT_NETBEANS_INTG */
6117
6118
6119/*
6120 * Delete signs in buffer "buf".
6121 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006123buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 signlist_T *next;
6126
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006127 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006128 * sign column. Not when curwin is NULL (this means we're exiting). */
6129 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006130 {
6131 redraw_buf_later(buf, NOT_VALID);
6132 changed_cline_bef_curs();
6133 }
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 while (buf->b_signlist != NULL)
6136 {
6137 next = buf->b_signlist->next;
6138 vim_free(buf->b_signlist);
6139 buf->b_signlist = next;
6140 }
6141}
6142
6143/*
6144 * Delete all signs in all buffers.
6145 */
6146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006147buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 buf_T *buf; /* buffer we are checking for signs */
6150
Bram Moolenaar29323592016-07-24 22:04:11 +02006151 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154}
6155
6156/*
6157 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6158 */
6159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006160sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
6162 buf_T *buf;
6163 signlist_T *p;
6164 char lbuf[BUFSIZ];
6165
6166 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6167 msg_putchar('\n');
6168 if (rbuf == NULL)
6169 buf = firstbuf;
6170 else
6171 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006172 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 {
6174 if (buf->b_signlist != NULL)
6175 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006176 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006177 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 msg_putchar('\n');
6179 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006180 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006182 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6184 MSG_PUTS(lbuf);
6185 msg_putchar('\n');
6186 }
6187 if (rbuf != NULL)
6188 break;
6189 buf = buf->b_next;
6190 }
6191}
6192
6193/*
6194 * Adjust a placed sign for inserted/deleted lines.
6195 */
6196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006197sign_mark_adjust(
6198 linenr_T line1,
6199 linenr_T line2,
6200 long amount,
6201 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 signlist_T *sign; /* a sign in a b_signlist */
6204
6205 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6206 {
6207 if (sign->lnum >= line1 && sign->lnum <= line2)
6208 {
6209 if (amount == MAXLNUM)
6210 sign->lnum = line1;
6211 else
6212 sign->lnum += amount;
6213 }
6214 else if (sign->lnum > line2)
6215 sign->lnum += amount_after;
6216 }
6217}
6218#endif /* FEAT_SIGNS */
6219
6220/*
6221 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6222 */
6223 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006224set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
6226 if (on != curbuf->b_p_bl)
6227 {
6228 curbuf->b_p_bl = on;
6229#ifdef FEAT_AUTOCMD
6230 if (on)
6231 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6232 else
6233 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6234#endif
6235 }
6236}
6237
6238/*
6239 * Read the file for "buf" again and check if the contents changed.
6240 * Return TRUE if it changed or this could not be checked.
6241 */
6242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006243buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
6245 buf_T *newbuf;
6246 int differ = TRUE;
6247 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 exarg_T ea;
6250
6251 /* Allocate a buffer without putting it in the buffer list. */
6252 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6253 if (newbuf == NULL)
6254 return TRUE;
6255
6256 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6257 if (prep_exarg(&ea, buf) == FAIL)
6258 {
6259 wipe_buffer(newbuf, FALSE);
6260 return TRUE;
6261 }
6262
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 /* set curwin/curbuf to buf and save a few things */
6264 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
Bram Moolenaar4770d092006-01-12 23:22:24 +00006266 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 && readfile(buf->b_ffname, buf->b_fname,
6268 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6269 &ea, READ_NEW | READ_DUMMY) == OK)
6270 {
6271 /* compare the two files line by line */
6272 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6273 {
6274 differ = FALSE;
6275 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6276 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6277 {
6278 differ = TRUE;
6279 break;
6280 }
6281 }
6282 }
6283 vim_free(ea.cmd);
6284
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 /* restore curwin/curbuf and a few other things */
6286 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287
6288 if (curbuf != newbuf) /* safety check */
6289 wipe_buffer(newbuf, FALSE);
6290
6291 return differ;
6292}
6293
6294/*
6295 * Wipe out a buffer and decrement the last buffer number if it was used for
6296 * this buffer. Call this to wipe out a temp buffer that does not contain any
6297 * marks.
6298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006300wipe_buffer(
6301 buf_T *buf,
6302 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
6304 if (buf->b_fnum == top_file_num - 1)
6305 --top_file_num;
6306
6307#ifdef FEAT_AUTOCMD
6308 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006309 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006311 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312#ifdef FEAT_AUTOCMD
6313 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006314 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315#endif
6316}