blob: e95abfa5af72f7e5287eec83c844e2142641f718 [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 Moolenaar7fd73202010-07-25 16:58:46 +020062#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
63static 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 Moolenaarf9e687e2016-09-04 21:33:09 +0200461# ifdef FEAT_WINDOWS
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100462 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200463 win_T *the_curwin = curwin;
464 tabpage_T *the_curtab = curtab;
465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467 int unload_buf = (action != 0);
468 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
469 int wipe_buf = (action == DOBUF_WIPE);
470
Bram Moolenaar94053a52017-08-01 21:44:33 +0200471#ifdef FEAT_TERMINAL
472 if (bt_terminal(buf))
473 {
474 if (term_job_running(buf->b_term))
475 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200476 if (wipe_buf || unload_buf)
477 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200478 free_terminal(buf);
479 else
480 {
481 /* The job keeps running, hide the buffer. */
482 del_buf = FALSE;
483 unload_buf = FALSE;
484 }
485 }
486 else
487 {
488 /* A terminal buffer is wiped out if the job has finished. */
489 del_buf = TRUE;
490 unload_buf = TRUE;
491 wipe_buf = TRUE;
492 }
493 }
494 else
495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 /*
497 * Force unloading or deleting when 'bufhidden' says so.
498 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
499 * "hide" (otherwise we could never free or delete a buffer).
500 */
501 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
502 {
503 del_buf = TRUE;
504 unload_buf = TRUE;
505 }
506 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
507 {
508 del_buf = TRUE;
509 unload_buf = TRUE;
510 wipe_buf = TRUE;
511 }
512 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
513 unload_buf = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
Bram Moolenaar30180b82016-09-04 19:57:56 +0200515#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200516 /* Disallow deleting the buffer when it is locked (already being closed or
517 * halfway a command that relies on it). Unloading is allowed. */
518 if (buf->b_locked > 0 && (del_buf || wipe_buf))
519 {
520 EMSG(_("E937: Attempt to delete a buffer that is in use"));
521 return;
522 }
Bram Moolenaar30180b82016-09-04 19:57:56 +0200523#endif
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200524
Bram Moolenaar3be85852014-06-12 14:01:31 +0200525 if (win != NULL
526#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200527 && win_valid_any_tab(win) /* in case autocommands closed the window */
Bram Moolenaar3be85852014-06-12 14:01:31 +0200528#endif
529 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
531 /* Set b_last_cursor when closing the last window for the buffer.
532 * Remember the last cursor position and window options of the buffer.
533 * This used to be only for the current window, but then options like
534 * 'foldmethod' may be lost with a ":only" command. */
535 if (buf->b_nwindows == 1)
536 set_last_cursor(win);
537 buflist_setfpos(buf, win,
538 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
539 win->w_cursor.col, TRUE);
540 }
541
542#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200543 set_bufref(&bufref, buf);
544
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 /* When the buffer is no longer in a window, trigger BufWinLeave */
546 if (buf->b_nwindows == 1)
547 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200548 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200549 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
550 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200551 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100552 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200553 /* Autocommands deleted the buffer. */
554aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100555 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100557 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200558 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200559 if (abort_if_last && one_window())
560 /* Autocommands made this the only window. */
561 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
563 /* When the buffer becomes hidden, but is not unloaded, trigger
564 * BufHidden */
565 if (!unload_buf)
566 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200567 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200568 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
569 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200570 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200571 /* Autocommands deleted the buffer. */
572 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200573 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200574 if (abort_if_last && one_window())
575 /* Autocommands made this the only window. */
576 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
578# ifdef FEAT_EVAL
579 if (aborting()) /* autocmds may abort script processing */
580 return;
581# endif
582 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200583
584# ifdef FEAT_WINDOWS
585 /* If the buffer was in curwin and the window has changed, go back to that
586 * window, if it still exists. This avoids that ":edit x" triggering a
587 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
588 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
589 {
590 block_autocmds();
591 goto_tabpage_win(the_curtab, the_curwin);
592 unblock_autocmds();
593 }
594# endif
595
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 nwindows = buf->b_nwindows;
597#endif
598
599 /* decrease the link count from windows (unless not in any window) */
600 if (buf->b_nwindows > 0)
601 --buf->b_nwindows;
602
603 /* Return when a window is displaying the buffer or when it's not
604 * unloaded. */
605 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 /* Always remove the buffer when there is no file name. */
609 if (buf->b_ffname == NULL)
610 del_buf = TRUE;
611
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200612 /* When closing the current buffer stop Visual mode before freeing
613 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200614 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200615#if defined(EXITFREE)
616 && !entered_free_all_mem
617#endif
618 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200619 end_visual_mode();
620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 /*
622 * Free all things allocated for this buffer.
623 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
624 */
625#ifdef FEAT_AUTOCMD
626 /* Remember if we are closing the current buffer. Restore the number of
627 * windows, so that autocommands in buf_freeall() don't get confused. */
628 is_curbuf = (buf == curbuf);
629 buf->b_nwindows = nwindows;
630#endif
631
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200632 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
634#ifdef FEAT_AUTOCMD
635 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200636 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 return;
638# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000639 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 return;
641# endif
642
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 /*
644 * It's possible that autocommands change curbuf to the one being deleted.
645 * This might cause the previous curbuf to be deleted unexpectedly. But
646 * in some cases it's OK to delete the curbuf, because a new one is
647 * obtained anyway. Therefore only return if curbuf changed to the
648 * deleted buffer.
649 */
650 if (buf == curbuf && !is_curbuf)
651 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200652
653 if (
654#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200655 win_valid_any_tab(win) &&
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200656#else
657 win != NULL &&
658#endif
659 win->w_buffer == buf)
660 win->w_buffer = NULL; /* make sure we don't use the buffer now */
661
662 /* Autocommands may have opened or closed windows for this buffer.
663 * Decrement the count for the close we do here. */
664 if (buf->b_nwindows > 0)
665 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#endif
667
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000668 /* Change directories when the 'acd' option is set. */
669 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671 /*
672 * Remove the buffer from the list.
673 */
674 if (wipe_buf)
675 {
676#ifdef FEAT_SUN_WORKSHOP
677 if (usingSunWorkShop)
678 workshop_file_closed_lineno((char *)buf->b_ffname,
679 (int)buf->b_last_cursor.lnum);
680#endif
681 vim_free(buf->b_ffname);
682 vim_free(buf->b_sfname);
683 if (buf->b_prev == NULL)
684 firstbuf = buf->b_next;
685 else
686 buf->b_prev->b_next = buf->b_next;
687 if (buf->b_next == NULL)
688 lastbuf = buf->b_prev;
689 else
690 buf->b_next->b_prev = buf->b_prev;
691 free_buffer(buf);
692 }
693 else
694 {
695 if (del_buf)
696 {
697 /* Free all internal variables and reset option values, to make
698 * ":bdel" compatible with Vim 5.7. */
699 free_buffer_stuff(buf, TRUE);
700
701 /* Make it look like a new buffer. */
702 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
703
704 /* Init the options when loaded again. */
705 buf->b_p_initialized = FALSE;
706 }
707 buf_clear_file(buf);
708 if (del_buf)
709 buf->b_p_bl = FALSE;
710 }
711}
712
713/*
714 * Make buffer not contain a file.
715 */
716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100717buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 buf->b_ml.ml_line_count = 1;
720 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 buf->b_p_eol = TRUE;
723 buf->b_start_eol = TRUE;
724#ifdef FEAT_MBYTE
725 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000726 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#endif
728 buf->b_ml.ml_mfp = NULL;
729 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
730#ifdef FEAT_NETBEANS_INTG
731 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733}
734
735/*
736 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200737 * the file. Careful: get here with "curwin" NULL when exiting.
738 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200739 * BFA_DEL buffer is going to be deleted
740 * BFA_WIPE buffer is going to be wiped out
741 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100744buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746#ifdef FEAT_AUTOCMD
747 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200748 bufref_T bufref;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200749# ifdef FEAT_WINDOWS
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200750 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200751 win_T *the_curwin = curwin;
752 tabpage_T *the_curtab = curtab;
753# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaar5a497892016-09-03 16:29:04 +0200755 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200756 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200757 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200758 if (buf->b_ml.ml_mfp != NULL)
759 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200760 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
761 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200762 && !bufref_valid(&bufref))
763 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200764 return;
765 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200766 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200768 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
769 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200770 && !bufref_valid(&bufref))
771 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 return;
773 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200774 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200776 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
777 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200778 && !bufref_valid(&bufref))
779 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 return;
781 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200782 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200783
784# ifdef FEAT_WINDOWS
785 /* If the buffer was in curwin and the window has changed, go back to that
786 * window, if it still exists. This avoids that ":edit x" triggering a
787 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
788 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
789 {
790 block_autocmds();
791 goto_tabpage_win(the_curtab, the_curwin);
792 unblock_autocmds();
793 }
794# endif
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000797 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 return;
799# endif
800
801 /*
802 * It's possible that autocommands change curbuf to the one being deleted.
803 * This might cause curbuf to be deleted unexpectedly. But in some cases
804 * it's OK to delete the curbuf, because a new one is obtained anyway.
805 * Therefore only return if curbuf changed to the deleted buffer.
806 */
807 if (buf == curbuf && !is_curbuf)
808 return;
809#endif
810#ifdef FEAT_DIFF
811 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
812#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200813#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100814 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200815 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100816 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200817#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000818
819#ifdef FEAT_FOLDING
820 /* No folds in an empty buffer. */
821# ifdef FEAT_WINDOWS
822 {
823 win_T *win;
824 tabpage_T *tp;
825
826 FOR_ALL_TAB_WINDOWS(tp, win)
827 if (win->w_buffer == buf)
828 clearFolding(win);
829 }
830# else
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200831 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000832 clearFolding(curwin);
833# endif
834#endif
835
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#ifdef FEAT_TCL
837 tcl_buffer_free(buf);
838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 ml_close(buf, TRUE); /* close and delete the memline/memfile */
840 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200841 if ((flags & BFA_KEEP_UNDO) == 0)
842 {
843 u_blockfree(buf); /* free the memory allocated for undo */
844 u_clearall(buf); /* reset all undo information */
845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200847 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000849 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
852/*
853 * Free a buffer structure and the things it contains related to the buffer
854 * itself (not the file, that must have been done already).
855 */
856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100857free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200859 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200861#ifdef FEAT_EVAL
862 unref_var_dict(buf->b_vars);
863#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200864#ifdef FEAT_LUA
865 lua_buffer_free(buf);
866#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000867#ifdef FEAT_MZSCHEME
868 mzscheme_buffer_free(buf);
869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#ifdef FEAT_PERL
871 perl_buf_free(buf);
872#endif
873#ifdef FEAT_PYTHON
874 python_buffer_free(buf);
875#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200876#ifdef FEAT_PYTHON3
877 python3_buffer_free(buf);
878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#ifdef FEAT_RUBY
880 ruby_buffer_free(buf);
881#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200882#ifdef FEAT_JOB_CHANNEL
883 channel_buffer_free(buf);
884#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200885#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200886 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200887#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200888
889 buf_hashtab_remove(buf);
890
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200891#ifdef FEAT_AUTOCMD
892 aubuflocal_remove(buf);
893
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200894 if (autocmd_busy)
895 {
896 /* Do not free the buffer structure while autocommands are executing,
897 * it's still needed. Free it when autocmd_busy is reset. */
898 buf->b_next = au_pending_free_buf;
899 au_pending_free_buf = buf;
900 }
901 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000902#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200903 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904}
905
906/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100907 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100908 */
909 static void
910init_changedtick(buf_T *buf)
911{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100912 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100913
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100914 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
915 di->di_tv.v_type = VAR_NUMBER;
916 di->di_tv.v_lock = VAR_FIXED;
917 di->di_tv.vval.v_number = 0;
918
Bram Moolenaar92769c32017-02-25 15:41:37 +0100919#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100920 STRCPY(buf->b_ct_di.di_key, "changedtick");
921 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100922#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100923}
924
925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
927 */
928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100929free_buffer_stuff(
930 buf_T *buf,
931 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
933 if (free_options)
934 {
935 clear_wininfo(buf); /* including window-local options */
936 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200937#ifdef FEAT_SPELL
938 ga_clear(&buf->b_s.b_langp);
939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
941#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100942 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100943 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100944
945 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
946 hash_init(&buf->b_vars->dv_hashtab);
947 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100948 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951#ifdef FEAT_USR_CMDS
952 uc_clear(&buf->b_ucmds); /* clear local user commands */
953#endif
954#ifdef FEAT_SIGNS
955 buf_delete_signs(buf); /* delete any signs */
956#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000957#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200958 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef FEAT_LOCALMAP
961 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
962 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
963#endif
964#ifdef FEAT_MBYTE
965 vim_free(buf->b_start_fenc);
966 buf->b_start_fenc = NULL;
967#endif
968}
969
970/*
971 * Free the b_wininfo list for buffer "buf".
972 */
973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100974clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 wininfo_T *wip;
977
978 while (buf->b_wininfo != NULL)
979 {
980 wip = buf->b_wininfo;
981 buf->b_wininfo = wip->wi_next;
982 if (wip->wi_optset)
983 {
984 clear_winopt(&wip->wi_opt);
985#ifdef FEAT_FOLDING
986 deleteFoldRecurse(&wip->wi_folds);
987#endif
988 }
989 vim_free(wip);
990 }
991}
992
993#if defined(FEAT_LISTCMDS) || defined(PROTO)
994/*
995 * Go to another buffer. Handles the result of the ATTENTION dialog.
996 */
997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100998goto_buffer(
999 exarg_T *eap,
1000 int start,
1001 int dir,
1002 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
Bram Moolenaare64ac772005-12-07 20:54:59 +00001004# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001005 bufref_T old_curbuf;
1006
1007 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009 swap_exists_action = SEA_DIALOG;
1010# endif
1011 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1012 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +00001013# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1015 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001016# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1017 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001018
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001019 /* Reset the error/interrupt/exception state here so that
1020 * aborting() returns FALSE when closing a window. */
1021 enter_cleanup(&cs);
1022# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001023
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001024 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 win_close(curwin, TRUE);
1026 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001027 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001028
1029# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1030 /* Restore the error/interrupt/exception state if not discarded by a
1031 * new aborting error, interrupt, or uncaught exception. */
1032 leave_cleanup(&cs);
1033# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001036 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037# endif
1038}
1039#endif
1040
Bram Moolenaare64ac772005-12-07 20:54:59 +00001041#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042/*
1043 * Handle the situation of swap_exists_action being set.
1044 * It is allowed for "old_curbuf" to be NULL or invalid.
1045 */
1046 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001047handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001049# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1050 cleanup_T cs;
1051# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001052#ifdef FEAT_SYN_HL
1053 long old_tw = curbuf->b_p_tw;
1054#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001055 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (swap_exists_action == SEA_QUIT)
1058 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001059# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1060 /* Reset the error/interrupt/exception state here so that
1061 * aborting() returns FALSE when closing a buffer. */
1062 enter_cleanup(&cs);
1063# endif
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 /* User selected Quit at ATTENTION prompt. Go back to previous
1066 * buffer. If that buffer is gone or the same as the current one,
1067 * open a new, empty buffer. */
1068 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001069 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001070 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001071 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1072 || old_curbuf->br_buf == curbuf)
1073 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1074 else
1075 buf = old_curbuf->br_buf;
1076 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001077 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001078 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001079#ifdef FEAT_SYN_HL
1080 if (old_tw != curbuf->b_p_tw)
1081 check_colorcolumn(curwin);
1082#endif
1083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001085
1086# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1087 /* Restore the error/interrupt/exception state if not discarded by a
1088 * new aborting error, interrupt, or uncaught exception. */
1089 leave_cleanup(&cs);
1090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 else if (swap_exists_action == SEA_RECOVER)
1093 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001094# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1095 /* Reset the error/interrupt/exception state here so that
1096 * aborting() returns FALSE when closing a buffer. */
1097 enter_cleanup(&cs);
1098# endif
1099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 /* User selected Recover at ATTENTION prompt. */
1101 msg_scroll = TRUE;
1102 ml_recover();
1103 MSG_PUTS("\n"); /* don't overwrite the last message */
1104 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001105 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001106
1107# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1108 /* Restore the error/interrupt/exception state if not discarded by a
1109 * new aborting error, interrupt, or uncaught exception. */
1110 leave_cleanup(&cs);
1111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
1113 swap_exists_action = SEA_NONE;
1114}
1115#endif
1116
1117#if defined(FEAT_LISTCMDS) || defined(PROTO)
1118/*
1119 * do_bufdel() - delete or unload buffer(s)
1120 *
1121 * addr_count == 0: ":bdel" - delete current buffer
1122 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1123 * buffer "end_bnr", then any other arguments.
1124 * addr_count == 2: ":N,N bdel" - delete buffers in range
1125 *
1126 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1127 * DOBUF_DEL (":bdel")
1128 *
1129 * Returns error message or NULL
1130 */
1131 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001132do_bufdel(
1133 int command,
1134 char_u *arg, /* pointer to extra arguments */
1135 int addr_count,
1136 int start_bnr, /* first buffer number in a range */
1137 int end_bnr, /* buffer nr or last buffer nr in a range */
1138 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140 int do_current = 0; /* delete current buffer? */
1141 int deleted = 0; /* number of buffers deleted */
1142 char_u *errormsg = NULL; /* return value */
1143 int bnr; /* buffer number */
1144 char_u *p;
1145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (addr_count == 0)
1147 {
1148 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1149 }
1150 else
1151 {
1152 if (addr_count == 2)
1153 {
1154 if (*arg) /* both range and argument is not allowed */
1155 return (char_u *)_(e_trailing);
1156 bnr = start_bnr;
1157 }
1158 else /* addr_count == 1 */
1159 bnr = end_bnr;
1160
1161 for ( ;!got_int; ui_breakcheck())
1162 {
1163 /*
1164 * delete the current buffer last, otherwise when the
1165 * current buffer is deleted, the next buffer becomes
1166 * the current one and will be loaded, which may then
1167 * also be deleted, etc.
1168 */
1169 if (bnr == curbuf->b_fnum)
1170 do_current = bnr;
1171 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1172 forceit) == OK)
1173 ++deleted;
1174
1175 /*
1176 * find next buffer number to delete/unload
1177 */
1178 if (addr_count == 2)
1179 {
1180 if (++bnr > end_bnr)
1181 break;
1182 }
1183 else /* addr_count == 1 */
1184 {
1185 arg = skipwhite(arg);
1186 if (*arg == NUL)
1187 break;
1188 if (!VIM_ISDIGIT(*arg))
1189 {
1190 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001191 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1192 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (bnr < 0) /* failed */
1194 break;
1195 arg = p;
1196 }
1197 else
1198 bnr = getdigits(&arg);
1199 }
1200 }
1201 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1202 FORWARD, do_current, forceit) == OK)
1203 ++deleted;
1204
1205 if (deleted == 0)
1206 {
1207 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001208 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001210 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001212 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 errormsg = IObuff;
1214 }
1215 else if (deleted >= p_report)
1216 {
1217 if (command == DOBUF_UNLOAD)
1218 {
1219 if (deleted == 1)
1220 MSG(_("1 buffer unloaded"));
1221 else
1222 smsg((char_u *)_("%d buffers unloaded"), deleted);
1223 }
1224 else if (command == DOBUF_DEL)
1225 {
1226 if (deleted == 1)
1227 MSG(_("1 buffer deleted"));
1228 else
1229 smsg((char_u *)_("%d buffers deleted"), deleted);
1230 }
1231 else
1232 {
1233 if (deleted == 1)
1234 MSG(_("1 buffer wiped out"));
1235 else
1236 smsg((char_u *)_("%d buffers wiped out"), deleted);
1237 }
1238 }
1239 }
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
1242 return errormsg;
1243}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001244#endif /* FEAT_LISTCMDS */
1245
1246#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1247 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001249static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001250
1251/*
1252 * Make the current buffer empty.
1253 * Used when it is wiped out and it's the last buffer.
1254 */
1255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001256empty_curbuf(
1257 int close_others,
1258 int forceit,
1259 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001260{
1261 int retval;
1262 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001263 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001264
1265 if (action == DOBUF_UNLOAD)
1266 {
1267 EMSG(_("E90: Cannot unload last buffer"));
1268 return FAIL;
1269 }
1270
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001271 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001272#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001273 if (close_others)
1274 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001275 close_windows(buf, TRUE);
1276#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001277
1278 setpcmark();
1279 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1280 forceit ? ECMD_FORCEIT : 0, curwin);
1281
1282 /*
1283 * do_ecmd() may create a new buffer, then we have to delete
1284 * the old one. But do_ecmd() may have done that already, check
1285 * if the buffer still exists.
1286 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001287 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001288 close_buffer(NULL, buf, action, FALSE);
1289 if (!close_others)
1290 need_fileinfo = FALSE;
1291 return retval;
1292}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293/*
1294 * Implementation of the commands for the buffer list.
1295 *
1296 * action == DOBUF_GOTO go to specified buffer
1297 * action == DOBUF_SPLIT split window and go to specified buffer
1298 * action == DOBUF_UNLOAD unload specified buffer(s)
1299 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1300 * action == DOBUF_WIPE delete specified buffer(s) really
1301 *
1302 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1303 * start == DOBUF_FIRST go to "count" buffer from first buffer
1304 * start == DOBUF_LAST go to "count" buffer from last buffer
1305 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1306 *
1307 * Return FAIL or OK.
1308 */
1309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001310do_buffer(
1311 int action,
1312 int start,
1313 int dir, /* FORWARD or BACKWARD */
1314 int count, /* buffer number or number of buffers */
1315 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 buf_T *buf;
1318 buf_T *bp;
1319 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1320 || action == DOBUF_WIPE);
1321
1322 switch (start)
1323 {
1324 case DOBUF_FIRST: buf = firstbuf; break;
1325 case DOBUF_LAST: buf = lastbuf; break;
1326 default: buf = curbuf; break;
1327 }
1328 if (start == DOBUF_MOD) /* find next modified buffer */
1329 {
1330 while (count-- > 0)
1331 {
1332 do
1333 {
1334 buf = buf->b_next;
1335 if (buf == NULL)
1336 buf = firstbuf;
1337 }
1338 while (buf != curbuf && !bufIsChanged(buf));
1339 }
1340 if (!bufIsChanged(buf))
1341 {
1342 EMSG(_("E84: No modified buffer found"));
1343 return FAIL;
1344 }
1345 }
1346 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1347 {
1348 while (buf != NULL && buf->b_fnum != count)
1349 buf = buf->b_next;
1350 }
1351 else
1352 {
1353 bp = NULL;
1354 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1355 {
1356 /* remember the buffer where we start, we come back there when all
1357 * buffers are unlisted. */
1358 if (bp == NULL)
1359 bp = buf;
1360 if (dir == FORWARD)
1361 {
1362 buf = buf->b_next;
1363 if (buf == NULL)
1364 buf = firstbuf;
1365 }
1366 else
1367 {
1368 buf = buf->b_prev;
1369 if (buf == NULL)
1370 buf = lastbuf;
1371 }
1372 /* don't count unlisted buffers */
1373 if (unload || buf->b_p_bl)
1374 {
1375 --count;
1376 bp = NULL; /* use this buffer as new starting point */
1377 }
1378 if (bp == buf)
1379 {
1380 /* back where we started, didn't find anything. */
1381 EMSG(_("E85: There is no listed buffer"));
1382 return FAIL;
1383 }
1384 }
1385 }
1386
1387 if (buf == NULL) /* could not find it */
1388 {
1389 if (start == DOBUF_FIRST)
1390 {
1391 /* don't warn when deleting */
1392 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001393 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395 else if (dir == FORWARD)
1396 EMSG(_("E87: Cannot go beyond last buffer"));
1397 else
1398 EMSG(_("E88: Cannot go before first buffer"));
1399 return FAIL;
1400 }
1401
1402#ifdef FEAT_GUI
1403 need_mouse_correct = TRUE;
1404#endif
1405
1406#ifdef FEAT_LISTCMDS
1407 /*
1408 * delete buffer buf from memory and/or the list
1409 */
1410 if (unload)
1411 {
1412 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001413# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1414 bufref_T bufref;
1415
1416 set_bufref(&bufref, buf);
1417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
1419 /* When unloading or deleting a buffer that's already unloaded and
1420 * unlisted: fail silently. */
1421 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1422 return FAIL;
1423
1424 if (!forceit && bufIsChanged(buf))
1425 {
1426#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1427 if ((p_confirm || cmdmod.confirm) && p_write)
1428 {
1429 dialog_changed(buf, FALSE);
1430# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001431 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 /* Autocommand deleted buffer, oops! It's not changed
1433 * now. */
1434 return FAIL;
1435# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001436 /* If it's still changed fail silently, the dialog already
1437 * mentioned why it fails. */
1438 if (bufIsChanged(buf))
1439 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001441 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442#endif
1443 {
1444 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1445 buf->b_fnum);
1446 return FAIL;
1447 }
1448 }
1449
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001450 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001451 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001452 end_visual_mode();
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 /*
1455 * If deleting the last (listed) buffer, make it empty.
1456 * The last (listed) buffer cannot be unloaded.
1457 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001458 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 if (bp->b_p_bl && bp != buf)
1460 break;
1461 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001462 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
1464#ifdef FEAT_WINDOWS
1465 /*
1466 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001467 * (unless it's the only window). Repeat this so long as we end up in
1468 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001470 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001471# ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001472 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02001473# endif
Bram Moolenaar459ca562016-11-10 18:16:33 +01001474 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001475 {
1476 if (win_close(curwin, FALSE) == FAIL)
1477 break;
1478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479#endif
1480
1481 /*
1482 * If the buffer to be deleted is not the current one, delete it here.
1483 */
1484 if (buf != curbuf)
1485 {
1486#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001487 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001488 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001490 if (buf->b_nwindows <= 0)
1491 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 return OK;
1493 }
1494
1495 /*
1496 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001497 * There should be another, otherwise it would have been handled
1498 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001499 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * Then prefer the buffer we most recently visited.
1501 * Else try to find one that is loaded, after the current buffer,
1502 * then before the current buffer.
1503 * Finally use any buffer.
1504 */
1505 buf = NULL; /* selected buffer */
1506 bp = NULL; /* used when no loaded buffer found */
1507#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001508 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1509 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510# ifdef FEAT_JUMPLIST
1511 else
1512# endif
1513#endif
1514#ifdef FEAT_JUMPLIST
1515 if (curwin->w_jumplistlen > 0)
1516 {
1517 int jumpidx;
1518
1519 jumpidx = curwin->w_jumplistidx - 1;
1520 if (jumpidx < 0)
1521 jumpidx = curwin->w_jumplistlen - 1;
1522
1523 forward = jumpidx;
1524 while (jumpidx != curwin->w_jumplistidx)
1525 {
1526 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1527 if (buf != NULL)
1528 {
1529 if (buf == curbuf || !buf->b_p_bl)
1530 buf = NULL; /* skip current and unlisted bufs */
1531 else if (buf->b_ml.ml_mfp == NULL)
1532 {
1533 /* skip unloaded buf, but may keep it for later */
1534 if (bp == NULL)
1535 bp = buf;
1536 buf = NULL;
1537 }
1538 }
1539 if (buf != NULL) /* found a valid buffer: stop searching */
1540 break;
1541 /* advance to older entry in jump list */
1542 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1543 break;
1544 if (--jumpidx < 0)
1545 jumpidx = curwin->w_jumplistlen - 1;
1546 if (jumpidx == forward) /* List exhausted for sure */
1547 break;
1548 }
1549 }
1550#endif
1551
1552 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1553 {
1554 forward = TRUE;
1555 buf = curbuf->b_next;
1556 for (;;)
1557 {
1558 if (buf == NULL)
1559 {
1560 if (!forward) /* tried both directions */
1561 break;
1562 buf = curbuf->b_prev;
1563 forward = FALSE;
1564 continue;
1565 }
1566 /* in non-help buffer, try to skip help buffers, and vv */
1567 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1568 {
1569 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1570 break;
1571 if (bp == NULL) /* remember unloaded buf for later */
1572 bp = buf;
1573 }
1574 if (forward)
1575 buf = buf->b_next;
1576 else
1577 buf = buf->b_prev;
1578 }
1579 }
1580 if (buf == NULL) /* No loaded buffer, use unloaded one */
1581 buf = bp;
1582 if (buf == NULL) /* No loaded buffer, find listed one */
1583 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001584 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (buf->b_p_bl && buf != curbuf)
1586 break;
1587 }
1588 if (buf == NULL) /* Still no buffer, just take one */
1589 {
1590 if (curbuf->b_next != NULL)
1591 buf = curbuf->b_next;
1592 else
1593 buf = curbuf->b_prev;
1594 }
1595 }
1596
Bram Moolenaara02471e2014-01-10 16:43:14 +01001597 if (buf == NULL)
1598 {
1599 /* Autocommands must have wiped out all other buffers. Only option
1600 * now is to make the current buffer empty. */
1601 return empty_curbuf(FALSE, forceit, action);
1602 }
1603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 /*
1605 * make buf current buffer
1606 */
1607 if (action == DOBUF_SPLIT) /* split window first */
1608 {
1609# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001610 /* If 'switchbuf' contains "useopen": jump to first window containing
1611 * "buf" if one exists */
1612 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001613 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001614 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001615 * page containing "buf" if one exists */
1616 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 return OK;
1618 if (win_split(0, 0) == FAIL)
1619# endif
1620 return FAIL;
1621 }
1622#endif
1623
1624 /* go to current buffer - nothing to do */
1625 if (buf == curbuf)
1626 return OK;
1627
1628 /*
1629 * Check if the current buffer may be abandoned.
1630 */
1631 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1632 {
1633#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1634 if ((p_confirm || cmdmod.confirm) && p_write)
1635 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001636# ifdef FEAT_AUTOCMD
1637 bufref_T bufref;
1638
1639 set_bufref(&bufref, buf);
1640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 dialog_changed(curbuf, FALSE);
1642# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001643 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 /* Autocommand deleted buffer, oops! */
1645 return FAIL;
1646# endif
1647 }
1648 if (bufIsChanged(curbuf))
1649#endif
1650 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001651 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 return FAIL;
1653 }
1654 }
1655
1656 /* Go to the other buffer. */
1657 set_curbuf(buf, action);
1658
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001659#if defined(FEAT_LISTCMDS) \
1660 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001662 {
1663 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665#endif
1666
1667#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1668 if (aborting()) /* autocmds may abort script processing */
1669 return FAIL;
1670#endif
1671
1672 return OK;
1673}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
1676/*
1677 * Set current buffer to "buf". Executes autocommands and closes current
1678 * buffer. "action" tells how to close the current buffer:
1679 * DOBUF_GOTO free or hide it
1680 * DOBUF_SPLIT nothing
1681 * DOBUF_UNLOAD unload it
1682 * DOBUF_DEL delete it
1683 * DOBUF_WIPE wipe it out
1684 */
1685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001686set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
1688 buf_T *prevbuf;
1689 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1690 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001691#ifdef FEAT_SYN_HL
1692 long old_tw = curbuf->b_p_tw;
1693#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001694 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
1696 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001697 if (!cmdmod.keepalt)
1698 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001699 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 /* Don't restart Select mode after switching to another buffer. */
1702 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704 /* close_windows() or apply_autocmds() may change curbuf */
1705 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001706 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001709 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710# ifdef FEAT_EVAL
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001711 || (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712# else
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001713 || bufref_valid(&bufref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714# endif
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001715 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716#endif
1717 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001718#ifdef FEAT_SYN_HL
1719 if (prevbuf == curwin->w_buffer)
1720 reset_synblock(curwin);
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#ifdef FEAT_WINDOWS
1723 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001724 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
1726#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001727 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001729 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001731 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001732#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001733 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001734#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001735 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001736 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1738 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001739 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001740 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001741#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001742 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001743 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001744 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001745#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
1748#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001749 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001750 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001751 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1752 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001753# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001754 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001755# endif
1756# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001757 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001758# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001759 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001763#ifdef FEAT_SYN_HL
1764 if (old_tw != curbuf->b_p_tw)
1765 check_colorcolumn(curwin);
1766#endif
1767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769
1770/*
1771 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001772 * Old curbuf must have been abandoned already! This also means "curbuf" may
1773 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 */
1775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 /* Copy buffer and window local option values. Not for a help buffer. */
1779 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1780 if (!buf->b_help)
1781 get_winopts(buf);
1782#ifdef FEAT_FOLDING
1783 else
1784 /* Remove all folds in the window. */
1785 clearFolding(curwin);
1786 foldUpdateAll(curwin); /* update folds (later). */
1787#endif
1788
1789 /* Get the buffer in the current window. */
1790 curwin->w_buffer = buf;
1791 curbuf = buf;
1792 ++curbuf->b_nwindows;
1793
1794#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001795 if (curwin->w_p_diff)
1796 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797#endif
1798
Bram Moolenaara971b822011-09-14 14:43:25 +02001799#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001800 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001801#endif
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 /* Cursor on first line by default. */
1804 curwin->w_cursor.lnum = 1;
1805 curwin->w_cursor.col = 0;
1806#ifdef FEAT_VIRTUALEDIT
1807 curwin->w_cursor.coladd = 0;
1808#endif
1809 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001810#ifdef FEAT_AUTOCMD
1811 curwin->w_topline_was_set = FALSE;
1812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001814 /* mark cursor position as being invalid */
1815 curwin->w_valid = 0;
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 /* Make sure the buffer is loaded. */
1818 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001819 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001820#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001821 /* If there is no filetype, allow for detecting one. Esp. useful for
1822 * ":ball" used in a autocommand. If there already is a filetype we
1823 * might prefer to keep it. */
1824 if (*curbuf->b_p_ft == NUL)
1825 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001826#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001827
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001828 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 else
1831 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001832 if (!msg_silent)
1833 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1835#ifdef FEAT_AUTOCMD
1836 curwin->w_topline = 1;
1837# ifdef FEAT_DIFF
1838 curwin->w_topfill = 0;
1839# endif
1840 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1841 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1842#endif
1843 }
1844
1845 /* If autocommands did not change the cursor position, restore cursor lnum
1846 * and possibly cursor col. */
1847 if (curwin->w_cursor.lnum == 1 && inindent(0))
1848 buflist_getfpos();
1849
1850 check_arg_idx(curwin); /* check for valid arg_idx */
1851#ifdef FEAT_TITLE
1852 maketitle();
1853#endif
1854#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001855 /* when autocmds didn't change it */
1856 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857#endif
1858 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1859
Bram Moolenaar009b2592004-10-24 19:18:58 +00001860#ifdef FEAT_NETBEANS_INTG
1861 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001862 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001863#endif
1864
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001865 /* Change directories when the 'acd' option is set. */
1866 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868#ifdef FEAT_KEYMAP
1869 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001870 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001872#ifdef FEAT_SPELL
1873 /* May need to set the spell language. Can only do this after the buffer
1874 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001875 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1876 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001877#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001878#ifdef FEAT_VIMINFO
1879 curbuf->b_last_used = vim_time();
1880#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 redraw_later(NOT_VALID);
1883}
1884
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001885#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1886/*
1887 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001888 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001889 */
1890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001891do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001892{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001893 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001894 && curbuf->b_ffname != NULL
1895 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001896 shorten_fnames(TRUE);
1897}
1898#endif
1899
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001900 void
1901no_write_message(void)
1902{
1903#ifdef FEAT_TERMINAL
1904 if (term_job_running(curbuf->b_term))
1905 EMSG(_("E948: Job still running (add ! to end the job)"));
1906 else
1907#endif
1908 EMSG(_("E37: No write since last change (add ! to override)"));
1909}
1910
1911 void
1912no_write_message_nobang(void)
1913{
1914#ifdef FEAT_TERMINAL
1915 if (term_job_running(curbuf->b_term))
1916 EMSG(_("E948: Job still running"));
1917 else
1918#endif
1919 EMSG(_("E37: No write since last change"));
1920}
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922/*
1923 * functions for dealing with the buffer list
1924 */
1925
Bram Moolenaar480778b2016-07-14 22:09:39 +02001926static int top_file_num = 1; /* highest file number */
1927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928/*
1929 * Add a file name to the buffer list. Return a pointer to the buffer.
1930 * If the same file name already exists return a pointer to that buffer.
1931 * If it does not exist, or if fname == NULL, a new entry is created.
1932 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1933 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1934 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001935 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001936 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1937 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 * This is the ONLY way to create a new buffer.
1939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001941buflist_new(
1942 char_u *ffname, /* full path of fname or relative */
1943 char_u *sfname, /* short fname or NULL */
1944 linenr_T lnum, /* preferred cursor line */
1945 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946{
1947 buf_T *buf;
1948#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001949 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950#endif
1951
Bram Moolenaar480778b2016-07-14 22:09:39 +02001952 if (top_file_num == 1)
1953 hash_init(&buf_hashtab);
1954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1956
1957 /*
1958 * If file name already exists in the list, update the entry.
1959 */
1960#ifdef UNIX
1961 /* On Unix we can use inode numbers when the file exists. Works better
1962 * for hard links. */
1963 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1964 st.st_dev = (dev_T)-1;
1965#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001966 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#ifdef UNIX
1968 buflist_findname_stat(ffname, &st)
1969#else
1970 buflist_findname(ffname)
1971#endif
1972 ) != NULL)
1973 {
1974 vim_free(ffname);
1975 if (lnum != 0)
1976 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001977
1978 if ((flags & BLN_NOOPT) == 0)
1979 /* copy the options now, if 'cpo' doesn't have 's' and not done
1980 * already */
1981 buf_copy_options(buf, 0);
1982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1984 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001985#ifdef FEAT_AUTOCMD
1986 bufref_T bufref;
1987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 buf->b_p_bl = TRUE;
1989#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001990 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001992 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001993 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001994 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001995 return NULL;
1996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#endif
1998 }
1999 return buf;
2000 }
2001
2002 /*
2003 * If the current buffer has no name and no contents, use the current
2004 * buffer. Otherwise: Need to allocate a new buffer structure.
2005 *
2006 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002007 * (A spell file buffer is allocated in spell.c, but that's not a normal
2008 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 */
2010 buf = NULL;
2011 if ((flags & BLN_CURBUF)
2012 && curbuf != NULL
2013 && curbuf->b_ffname == NULL
2014 && curbuf->b_nwindows <= 1
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002015 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
2017 buf = curbuf;
2018#ifdef FEAT_AUTOCMD
2019 /* It's like this buffer is deleted. Watch out for autocommands that
2020 * change curbuf! If that happens, allocate a new buffer anyway. */
2021 if (curbuf->b_p_bl)
2022 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2023 if (buf == curbuf)
2024 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
2025# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002026 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 return NULL;
2028# endif
2029#endif
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02002030#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 if (buf == curbuf)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02002032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
2034 /* Make sure 'bufhidden' and 'buftype' are empty */
2035 clear_string_option(&buf->b_p_bh);
2036 clear_string_option(&buf->b_p_bt);
2037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 if (buf != curbuf || curbuf == NULL)
2040 {
2041 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
2042 if (buf == NULL)
2043 {
2044 vim_free(ffname);
2045 return NULL;
2046 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002047#ifdef FEAT_EVAL
2048 /* init b: variables */
2049 buf->b_vars = dict_alloc();
2050 if (buf->b_vars == NULL)
2051 {
2052 vim_free(ffname);
2053 vim_free(buf);
2054 return NULL;
2055 }
2056 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2057#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002058 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060
2061 if (ffname != NULL)
2062 {
2063 buf->b_ffname = ffname;
2064 buf->b_sfname = vim_strsave(sfname);
2065 }
2066
2067 clear_wininfo(buf);
2068 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2069
2070 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2071 || buf->b_wininfo == NULL)
2072 {
2073 vim_free(buf->b_ffname);
2074 buf->b_ffname = NULL;
2075 vim_free(buf->b_sfname);
2076 buf->b_sfname = NULL;
2077 if (buf != curbuf)
2078 free_buffer(buf);
2079 return NULL;
2080 }
2081
2082 if (buf == curbuf)
2083 {
2084 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002085 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (buf != curbuf) /* autocommands deleted the buffer! */
2087 return NULL;
2088#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002089 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 return NULL;
2091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002093
2094 /* Init the options. */
2095 buf->b_p_initialized = FALSE;
2096 buf_copy_options(buf, BCO_ENTER);
2097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098#ifdef FEAT_KEYMAP
2099 /* need to reload lmaps and set b:keymap_name */
2100 curbuf->b_kmap_state |= KEYMAP_INIT;
2101#endif
2102 }
2103 else
2104 {
2105 /*
2106 * put new buffer at the end of the buffer list
2107 */
2108 buf->b_next = NULL;
2109 if (firstbuf == NULL) /* buffer list is empty */
2110 {
2111 buf->b_prev = NULL;
2112 firstbuf = buf;
2113 }
2114 else /* append new buffer at end of list */
2115 {
2116 lastbuf->b_next = buf;
2117 buf->b_prev = lastbuf;
2118 }
2119 lastbuf = buf;
2120
2121 buf->b_fnum = top_file_num++;
2122 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2123 {
2124 EMSG(_("W14: Warning: List of file names overflow"));
2125 if (emsg_silent == 0)
2126 {
2127 out_flush();
2128 ui_delay(3000L, TRUE); /* make sure it is noticed */
2129 }
2130 top_file_num = 1;
2131 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002132 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 /*
2135 * Always copy the options from the current buffer.
2136 */
2137 buf_copy_options(buf, BCO_ALWAYS);
2138 }
2139
2140 buf->b_wininfo->wi_fpos.lnum = lnum;
2141 buf->b_wininfo->wi_win = curwin;
2142
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002143#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002144 hash_init(&buf->b_s.b_keywtab);
2145 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146#endif
2147
2148 buf->b_fname = buf->b_sfname;
2149#ifdef UNIX
2150 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002151 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 else
2153 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002154 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 buf->b_dev = st.st_dev;
2156 buf->b_ino = st.st_ino;
2157 }
2158#endif
2159 buf->b_u_synced = TRUE;
2160 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002161 if (flags & BLN_DUMMY)
2162 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 buf_clear_file(buf);
2164 clrallmarks(buf); /* clear marks */
2165 fmarks_check_names(buf); /* check file marks for this file */
2166 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2167#ifdef FEAT_AUTOCMD
2168 if (!(flags & BLN_DUMMY))
2169 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002170 bufref_T bufref;
2171
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002172 /* Tricky: these autocommands may change the buffer list. They could
2173 * also split the window with re-using the one empty buffer. This may
2174 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002175 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002176 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002177 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002178 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002180 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002181 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002182 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002183 return NULL;
2184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002186 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return NULL;
2188# endif
2189 }
2190#endif
2191
2192 return buf;
2193}
2194
2195/*
2196 * Free the memory for the options of a buffer.
2197 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2198 * 'fileencoding'.
2199 */
2200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201free_buf_options(
2202 buf_T *buf,
2203 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
2205 if (free_p_ff)
2206 {
2207#ifdef FEAT_MBYTE
2208 clear_string_option(&buf->b_p_fenc);
2209#endif
2210 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 clear_string_option(&buf->b_p_bh);
2212 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 }
2214#ifdef FEAT_FIND_ID
2215 clear_string_option(&buf->b_p_def);
2216 clear_string_option(&buf->b_p_inc);
2217# ifdef FEAT_EVAL
2218 clear_string_option(&buf->b_p_inex);
2219# endif
2220#endif
2221#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2222 clear_string_option(&buf->b_p_inde);
2223 clear_string_option(&buf->b_p_indk);
2224#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002225#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2226 clear_string_option(&buf->b_p_bexpr);
2227#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002228#if defined(FEAT_CRYPT)
2229 clear_string_option(&buf->b_p_cm);
2230#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002231 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002232#if defined(FEAT_EVAL)
2233 clear_string_option(&buf->b_p_fex);
2234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235#ifdef FEAT_CRYPT
2236 clear_string_option(&buf->b_p_key);
2237#endif
2238 clear_string_option(&buf->b_p_kp);
2239 clear_string_option(&buf->b_p_mps);
2240 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002241 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 clear_string_option(&buf->b_p_isk);
2243#ifdef FEAT_KEYMAP
2244 clear_string_option(&buf->b_p_keymap);
2245 ga_clear(&buf->b_kmap_ga);
2246#endif
2247#ifdef FEAT_COMMENTS
2248 clear_string_option(&buf->b_p_com);
2249#endif
2250#ifdef FEAT_FOLDING
2251 clear_string_option(&buf->b_p_cms);
2252#endif
2253 clear_string_option(&buf->b_p_nf);
2254#ifdef FEAT_SYN_HL
2255 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002256 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002257#endif
2258#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002259 clear_string_option(&buf->b_s.b_p_spc);
2260 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002261 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002262 buf->b_s.b_cap_prog = NULL;
2263 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#endif
2265#ifdef FEAT_SEARCHPATH
2266 clear_string_option(&buf->b_p_sua);
2267#endif
2268#ifdef FEAT_AUTOCMD
2269 clear_string_option(&buf->b_p_ft);
2270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271#ifdef FEAT_CINDENT
2272 clear_string_option(&buf->b_p_cink);
2273 clear_string_option(&buf->b_p_cino);
2274#endif
2275#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2276 clear_string_option(&buf->b_p_cinw);
2277#endif
2278#ifdef FEAT_INS_EXPAND
2279 clear_string_option(&buf->b_p_cpt);
2280#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002281#ifdef FEAT_COMPL_FUNC
2282 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002283 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#ifdef FEAT_QUICKFIX
2286 clear_string_option(&buf->b_p_gp);
2287 clear_string_option(&buf->b_p_mp);
2288 clear_string_option(&buf->b_p_efm);
2289#endif
2290 clear_string_option(&buf->b_p_ep);
2291 clear_string_option(&buf->b_p_path);
2292 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002293 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294#ifdef FEAT_INS_EXPAND
2295 clear_string_option(&buf->b_p_dict);
2296 clear_string_option(&buf->b_p_tsr);
2297#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002298#ifdef FEAT_TEXTOBJ
2299 clear_string_option(&buf->b_p_qe);
2300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002302 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002303#ifdef FEAT_LISP
2304 clear_string_option(&buf->b_p_lw);
2305#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002306 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002307#ifdef FEAT_MBYTE
2308 clear_string_option(&buf->b_p_menc);
2309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310}
2311
2312/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002313 * Get alternate file "n".
2314 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2315 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 * if (options & GETF_SETMARK) call setpcmark()
2317 * if (options & GETF_ALT) we are jumping to an alternate file.
2318 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2319 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002320 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 */
2322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002323buflist_getfile(
2324 int n,
2325 linenr_T lnum,
2326 int options,
2327 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 buf_T *buf;
2330#ifdef FEAT_WINDOWS
2331 win_T *wp = NULL;
2332#endif
2333 pos_T *fpos;
2334 colnr_T col;
2335
2336 buf = buflist_findnr(n);
2337 if (buf == NULL)
2338 {
2339 if ((options & GETF_ALT) && n == 0)
2340 EMSG(_(e_noalt));
2341 else
2342 EMSGN(_("E92: Buffer %ld not found"), n);
2343 return FAIL;
2344 }
2345
2346 /* if alternate file is the current buffer, nothing to do */
2347 if (buf == curbuf)
2348 return OK;
2349
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002350 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002351 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002352 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002354 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002355#ifdef FEAT_AUTOCMD
2356 if (curbuf_locked())
2357 return FAIL;
2358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 /* altfpos may be changed by getfile(), get it now */
2361 if (lnum == 0)
2362 {
2363 fpos = buflist_findfpos(buf);
2364 lnum = fpos->lnum;
2365 col = fpos->col;
2366 }
2367 else
2368 col = 0;
2369
2370#ifdef FEAT_WINDOWS
2371 if (options & GETF_SWITCH)
2372 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002373 /* If 'switchbuf' contains "useopen": jump to first window containing
2374 * "buf" if one exists */
2375 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002377
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002378 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002379 * page containing "buf" if one exists */
2380 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002381 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002382
2383 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2384 * current buffer isn't empty: open new tab or window */
2385 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002386 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002388 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002389 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002390 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2391 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002393 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395 }
2396#endif
2397
2398 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002399 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2400 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
2402 --RedrawingDisabled;
2403
2404 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2405 if (!p_sol && col != 0)
2406 {
2407 curwin->w_cursor.col = col;
2408 check_cursor_col();
2409#ifdef FEAT_VIRTUALEDIT
2410 curwin->w_cursor.coladd = 0;
2411#endif
2412 curwin->w_set_curswant = TRUE;
2413 }
2414 return OK;
2415 }
2416 --RedrawingDisabled;
2417 return FAIL;
2418}
2419
2420/*
2421 * go to the last know line number for the current buffer
2422 */
2423 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002424buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
2426 pos_T *fpos;
2427
2428 fpos = buflist_findfpos(curbuf);
2429
2430 curwin->w_cursor.lnum = fpos->lnum;
2431 check_cursor_lnum();
2432
2433 if (p_sol)
2434 curwin->w_cursor.col = 0;
2435 else
2436 {
2437 curwin->w_cursor.col = fpos->col;
2438 check_cursor_col();
2439#ifdef FEAT_VIRTUALEDIT
2440 curwin->w_cursor.coladd = 0;
2441#endif
2442 curwin->w_set_curswant = TRUE;
2443 }
2444}
2445
Bram Moolenaar81695252004-12-29 20:58:21 +00002446#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2447/*
2448 * Find file in buffer list by name (it has to be for the current window).
2449 * Returns NULL if not found.
2450 */
2451 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002452buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002453{
2454 char_u *ffname;
2455 buf_T *buf = NULL;
2456
2457 /* First make the name into a full path name */
2458 ffname = FullName_save(fname,
2459#ifdef UNIX
2460 TRUE /* force expansion, get rid of symbolic links */
2461#else
2462 FALSE
2463#endif
2464 );
2465 if (ffname != NULL)
2466 {
2467 buf = buflist_findname(ffname);
2468 vim_free(ffname);
2469 }
2470 return buf;
2471}
2472#endif
2473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474/*
2475 * Find file in buffer list by name (it has to be for the current window).
2476 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002477 * Skips dummy buffers.
2478 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 */
2480 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002481buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002484 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486 if (mch_stat((char *)ffname, &st) < 0)
2487 st.st_dev = (dev_T)-1;
2488 return buflist_findname_stat(ffname, &st);
2489}
2490
2491/*
2492 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2493 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002494 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 */
2496 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002497buflist_findname_stat(
2498 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002499 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501#endif
2502 buf_T *buf;
2503
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002504 /* Start at the last buffer, expect to find a match sooner. */
2505 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002506 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507#ifdef UNIX
2508 , stp
2509#endif
2510 ))
2511 return buf;
2512 return NULL;
2513}
2514
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002515#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2516 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517/*
2518 * Find file in buffer list by a regexp pattern.
2519 * Return fnum of the found buffer.
2520 * Return < 0 for error.
2521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002523buflist_findpat(
2524 char_u *pattern,
2525 char_u *pattern_end, /* pointer to first char after pattern */
2526 int unlisted, /* find unlisted buffers */
2527 int diffmode UNUSED, /* find diff-mode buffers only */
2528 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 int match = -1;
2532 int find_listed;
2533 char_u *pat;
2534 char_u *patend;
2535 int attempt;
2536 char_u *p;
2537 int toggledollar;
2538
2539 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2540 {
2541 if (*pattern == '%')
2542 match = curbuf->b_fnum;
2543 else
2544 match = curwin->w_alt_fnum;
2545#ifdef FEAT_DIFF
2546 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2547 match = -1;
2548#endif
2549 }
2550
2551 /*
2552 * Try four ways of matching a listed buffer:
2553 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002554 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 * attempt == 2: with '$' at end (only match at end)
2556 * attempt == 3: with '^' at start and '$' at end (only full match)
2557 * Repeat this for finding an unlisted buffer if there was no matching
2558 * listed buffer.
2559 */
2560 else
2561 {
2562 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2563 if (pat == NULL)
2564 return -1;
2565 patend = pat + STRLEN(pat) - 1;
2566 toggledollar = (patend > pat && *patend == '$');
2567
2568 /* First try finding a listed buffer. If not found and "unlisted"
2569 * is TRUE, try finding an unlisted buffer. */
2570 find_listed = TRUE;
2571 for (;;)
2572 {
2573 for (attempt = 0; attempt <= 3; ++attempt)
2574 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002575 regmatch_T regmatch;
2576
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 /* may add '^' and '$' */
2578 if (toggledollar)
2579 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2580 p = pat;
2581 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2582 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002583 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2584 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 vim_free(pat);
2587 return -1;
2588 }
2589
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002590 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (buf->b_p_bl == find_listed
2592#ifdef FEAT_DIFF
2593 && (!diffmode || diff_mode_buf(buf))
2594#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002595 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002597 if (curtab_only)
2598 {
2599 /* Ignore the match if the buffer is not open in
2600 * the current tab. */
2601#ifdef FEAT_WINDOWS
2602 win_T *wp;
2603
Bram Moolenaar29323592016-07-24 22:04:11 +02002604 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002605 if (wp->w_buffer == buf)
2606 break;
2607 if (wp == NULL)
2608 continue;
2609#else
2610 if (curwin->w_buffer != buf)
2611 continue;
2612#endif
2613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 if (match >= 0) /* already found a match */
2615 {
2616 match = -2;
2617 break;
2618 }
2619 match = buf->b_fnum; /* remember first match */
2620 }
2621
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002622 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 if (match >= 0) /* found one match */
2624 break;
2625 }
2626
2627 /* Only search for unlisted buffers if there was no match with
2628 * a listed buffer. */
2629 if (!unlisted || !find_listed || match != -1)
2630 break;
2631 find_listed = FALSE;
2632 }
2633
2634 vim_free(pat);
2635 }
2636
2637 if (match == -2)
2638 EMSG2(_("E93: More than one match for %s"), pattern);
2639 else if (match < 0)
2640 EMSG2(_("E94: No matching buffer for %s"), pattern);
2641 return match;
2642}
2643#endif
2644
2645#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2646
2647/*
2648 * Find all buffer names that match.
2649 * For command line expansion of ":buf" and ":sbuf".
2650 * Return OK if matches found, FAIL otherwise.
2651 */
2652 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002653ExpandBufnames(
2654 char_u *pat,
2655 int *num_file,
2656 char_u ***file,
2657 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
2659 int count = 0;
2660 buf_T *buf;
2661 int round;
2662 char_u *p;
2663 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002664 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 *num_file = 0; /* return values in case of FAIL */
2667 *file = NULL;
2668
Bram Moolenaar05159a02005-02-26 23:04:13 +00002669 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2670 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002672 patc = alloc((unsigned)STRLEN(pat) + 11);
2673 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002675 STRCPY(patc, "\\(^\\|[\\/]\\)");
2676 STRCPY(patc + 11, pat + 1);
2677 }
2678 else
2679 patc = pat;
2680
2681 /*
2682 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002683 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002684 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002685 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002686 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002687 regmatch_T regmatch;
2688
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002689 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002690 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002691 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2692 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002693 {
2694 if (patc != pat)
2695 vim_free(patc);
2696 return FAIL;
2697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 /*
2700 * round == 1: Count the matches.
2701 * round == 2: Build the array to keep the matches.
2702 */
2703 for (round = 1; round <= 2; ++round)
2704 {
2705 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002706 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
2708 if (!buf->b_p_bl) /* skip unlisted buffers */
2709 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002710 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (p != NULL)
2712 {
2713 if (round == 1)
2714 ++count;
2715 else
2716 {
2717 if (options & WILD_HOME_REPLACE)
2718 p = home_replace_save(buf, p);
2719 else
2720 p = vim_strsave(p);
2721 (*file)[count++] = p;
2722 }
2723 }
2724 }
2725 if (count == 0) /* no match found, break here */
2726 break;
2727 if (round == 1)
2728 {
2729 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2730 if (*file == NULL)
2731 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002732 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002733 if (patc != pat)
2734 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 return FAIL;
2736 }
2737 }
2738 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002739 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 if (count) /* match(es) found, break here */
2741 break;
2742 }
2743
Bram Moolenaar05159a02005-02-26 23:04:13 +00002744 if (patc != pat)
2745 vim_free(patc);
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 *num_file = count;
2748 return (count == 0 ? FAIL : OK);
2749}
2750
2751#endif /* FEAT_CMDL_COMPL */
2752
2753#ifdef HAVE_BUFLIST_MATCH
2754/*
2755 * Check for a match on the file name for buffer "buf" with regprog "prog".
2756 */
2757 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002758buflist_match(
2759 regmatch_T *rmp,
2760 buf_T *buf,
2761 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
2763 char_u *match;
2764
2765 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002766 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002768 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
2770 return match;
2771}
2772
2773/*
2774 * Try matching the regexp in "prog" with file name "name".
2775 * Return "name" when there is a match, NULL when not.
2776 */
2777 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002778fname_match(
2779 regmatch_T *rmp,
2780 char_u *name,
2781 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
2783 char_u *match = NULL;
2784 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786 if (name != NULL)
2787 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002788 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002789 rmp->rm_ic = p_fic || ignore_case;
2790 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 match = name;
2792 else
2793 {
2794 /* Replace $(HOME) with '~' and try matching again. */
2795 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002796 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 match = name;
2798 vim_free(p);
2799 }
2800 }
2801
2802 return match;
2803}
2804#endif
2805
2806/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002807 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002810buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002812 char_u key[VIM_SIZEOF_INT * 2 + 1];
2813 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815 if (nr == 0)
2816 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002817 sprintf((char *)key, "%x", nr);
2818 hi = hash_find(&buf_hashtab, key);
2819
2820 if (!HASHITEM_EMPTY(hi))
2821 return (buf_T *)(hi->hi_key
2822 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 return NULL;
2824}
2825
2826/*
2827 * Get name of file 'n' in the buffer list.
2828 * When the file has no name an empty string is returned.
2829 * home_replace() is used to shorten the file name (used for marks).
2830 * Returns a pointer to allocated memory, of NULL when failed.
2831 */
2832 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002833buflist_nr2name(
2834 int n,
2835 int fullname,
2836 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
2838 buf_T *buf;
2839
2840 buf = buflist_findnr(n);
2841 if (buf == NULL)
2842 return NULL;
2843 return home_replace_save(helptail ? buf : NULL,
2844 fullname ? buf->b_ffname : buf->b_fname);
2845}
2846
2847/*
2848 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2849 * When "copy_options" is TRUE save the local window option values.
2850 * When "lnum" is 0 only do the options.
2851 */
2852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002853buflist_setfpos(
2854 buf_T *buf,
2855 win_T *win,
2856 linenr_T lnum,
2857 colnr_T col,
2858 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860 wininfo_T *wip;
2861
2862 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2863 if (wip->wi_win == win)
2864 break;
2865 if (wip == NULL)
2866 {
2867 /* allocate a new entry */
2868 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2869 if (wip == NULL)
2870 return;
2871 wip->wi_win = win;
2872 if (lnum == 0) /* set lnum even when it's 0 */
2873 lnum = 1;
2874 }
2875 else
2876 {
2877 /* remove the entry from the list */
2878 if (wip->wi_prev)
2879 wip->wi_prev->wi_next = wip->wi_next;
2880 else
2881 buf->b_wininfo = wip->wi_next;
2882 if (wip->wi_next)
2883 wip->wi_next->wi_prev = wip->wi_prev;
2884 if (copy_options && wip->wi_optset)
2885 {
2886 clear_winopt(&wip->wi_opt);
2887#ifdef FEAT_FOLDING
2888 deleteFoldRecurse(&wip->wi_folds);
2889#endif
2890 }
2891 }
2892 if (lnum != 0)
2893 {
2894 wip->wi_fpos.lnum = lnum;
2895 wip->wi_fpos.col = col;
2896 }
2897 if (copy_options)
2898 {
2899 /* Save the window-specific option values. */
2900 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2901#ifdef FEAT_FOLDING
2902 wip->wi_fold_manual = win->w_fold_manual;
2903 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2904#endif
2905 wip->wi_optset = TRUE;
2906 }
2907
2908 /* insert the entry in front of the list */
2909 wip->wi_next = buf->b_wininfo;
2910 buf->b_wininfo = wip;
2911 wip->wi_prev = NULL;
2912 if (wip->wi_next)
2913 wip->wi_next->wi_prev = wip;
2914
2915 return;
2916}
2917
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002918#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002919static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002920
2921/*
2922 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2923 * page. That's because a diff is local to a tab page.
2924 */
2925 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002927{
2928 win_T *wp;
2929
2930 if (wip->wi_opt.wo_diff)
2931 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002932 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002933 /* return FALSE when it's a window in the current tab page, thus
2934 * the buffer was in diff mode here */
2935 if (wip->wi_win == wp)
2936 return FALSE;
2937 return TRUE;
2938 }
2939 return FALSE;
2940}
2941#endif
2942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943/*
2944 * Find info for the current window in buffer "buf".
2945 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002946 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2947 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 * Returns NULL when there isn't any info.
2949 */
2950 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002951find_wininfo(
2952 buf_T *buf,
2953 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 wininfo_T *wip;
2956
2957 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002958 if (wip->wi_win == curwin
2959#ifdef FEAT_DIFF
2960 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2961#endif
2962 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002964
2965 /* If no wininfo for curwin, use the first in the list (that doesn't have
2966 * 'diff' set and is in another tab page). */
2967 if (wip == NULL)
2968 {
2969#ifdef FEAT_DIFF
2970 if (skip_diff_buffer)
2971 {
2972 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2973 if (!wininfo_other_tab_diff(wip))
2974 break;
2975 }
2976 else
2977#endif
2978 wip = buf->b_wininfo;
2979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 return wip;
2981}
2982
2983/*
2984 * Reset the local window options to the values last used in this window.
2985 * If the buffer wasn't used in this window before, use the values from
2986 * the most recently used window. If the values were never set, use the
2987 * global values for the window.
2988 */
2989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002990get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
2992 wininfo_T *wip;
2993
2994 clear_winopt(&curwin->w_onebuf_opt);
2995#ifdef FEAT_FOLDING
2996 clearFolding(curwin);
2997#endif
2998
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002999 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if (wip != NULL && wip->wi_optset)
3001 {
3002 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3003#ifdef FEAT_FOLDING
3004 curwin->w_fold_manual = wip->wi_fold_manual;
3005 curwin->w_foldinvalid = TRUE;
3006 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3007#endif
3008 }
3009 else
3010 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
3011
3012#ifdef FEAT_FOLDING
3013 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
3014 if (p_fdls >= 0)
3015 curwin->w_p_fdl = p_fdls;
3016#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02003017#ifdef FEAT_SYN_HL
3018 check_colorcolumn(curwin);
3019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020}
3021
3022/*
3023 * Find the position (lnum and col) for the buffer 'buf' for the current
3024 * window.
3025 * Returns a pointer to no_position if no position is found.
3026 */
3027 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003028buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003031 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003033 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 if (wip != NULL)
3035 return &(wip->wi_fpos);
3036 else
3037 return &no_position;
3038}
3039
3040/*
3041 * Find the lnum for the buffer 'buf' for the current window.
3042 */
3043 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003044buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 return buflist_findfpos(buf)->lnum;
3047}
3048
3049#if defined(FEAT_LISTCMDS) || defined(PROTO)
3050/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003051 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003054buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
3056 buf_T *buf;
3057 int len;
3058 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003059 int ro_char;
3060 int changed_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3063 {
3064 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003065 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3066 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3067 || (vim_strchr(eap->arg, '+')
3068 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3069 || (vim_strchr(eap->arg, 'a')
3070 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
3071 || (vim_strchr(eap->arg, 'h')
3072 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3073 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3074 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3075 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3076 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3077 || (vim_strchr(eap->arg, '#')
3078 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003081 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 else
3083 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003084 if (message_filtered(NameBuff))
3085 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003087 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3088 : (bufIsChanged(buf) ? '+' : ' ');
3089#ifdef FEAT_TERMINAL
3090 if (term_job_running(buf->b_term))
3091 {
3092 ro_char = 'R';
3093 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3094 * closing, but it's not actually changed. */
3095 }
3096 else if (buf->b_term != NULL)
3097 ro_char = 'F';
3098 else
3099#endif
3100 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3101
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003102 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003103 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 buf->b_fnum,
3105 buf->b_p_bl ? ' ' : 'u',
3106 buf == curbuf ? '%' :
3107 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3108 buf->b_ml.ml_mfp == NULL ? ' ' :
3109 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003110 ro_char,
3111 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003112 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003113 if (len > IOSIZE - 20)
3114 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 i = 40 - vim_strsize(IObuff);
3118 do
3119 {
3120 IObuff[len++] = ' ';
3121 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003122 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3123 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003124 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 msg_outtrans(IObuff);
3126 out_flush(); /* output one line at a time */
3127 ui_breakcheck();
3128 }
3129}
3130#endif
3131
3132/*
3133 * Get file name and line number for file 'fnum'.
3134 * Used by DoOneCmd() for translating '%' and '#'.
3135 * Used by insert_reg() and cmdline_paste() for '#' register.
3136 * Return FAIL if not found, OK for success.
3137 */
3138 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003139buflist_name_nr(
3140 int fnum,
3141 char_u **fname,
3142 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
3144 buf_T *buf;
3145
3146 buf = buflist_findnr(fnum);
3147 if (buf == NULL || buf->b_fname == NULL)
3148 return FAIL;
3149
3150 *fname = buf->b_fname;
3151 *lnum = buflist_findlnum(buf);
3152
3153 return OK;
3154}
3155
3156/*
3157 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3158 * The file name with the full path is also remembered, for when :cd is used.
3159 * Returns FAIL for failure (file name already in use by other buffer)
3160 * OK otherwise.
3161 */
3162 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003163setfname(
3164 buf_T *buf,
3165 char_u *ffname,
3166 char_u *sfname,
3167 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
Bram Moolenaar81695252004-12-29 20:58:21 +00003169 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003171 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#endif
3173
3174 if (ffname == NULL || *ffname == NUL)
3175 {
3176 /* Removing the name. */
3177 vim_free(buf->b_ffname);
3178 vim_free(buf->b_sfname);
3179 buf->b_ffname = NULL;
3180 buf->b_sfname = NULL;
3181#ifdef UNIX
3182 st.st_dev = (dev_T)-1;
3183#endif
3184 }
3185 else
3186 {
3187 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3188 if (ffname == NULL) /* out of memory */
3189 return FAIL;
3190
3191 /*
3192 * if the file name is already used in another buffer:
3193 * - if the buffer is loaded, fail
3194 * - if the buffer is not loaded, delete it from the list
3195 */
3196#ifdef UNIX
3197 if (mch_stat((char *)ffname, &st) < 0)
3198 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003199#endif
3200 if (!(buf->b_flags & BF_DUMMY))
3201#ifdef UNIX
3202 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003204 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#endif
3206 if (obuf != NULL && obuf != buf)
3207 {
3208 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3209 {
3210 if (message)
3211 EMSG(_("E95: Buffer with this name already exists"));
3212 vim_free(ffname);
3213 return FAIL;
3214 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003215 /* delete from the list */
3216 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
3218 sfname = vim_strsave(sfname);
3219 if (ffname == NULL || sfname == NULL)
3220 {
3221 vim_free(sfname);
3222 vim_free(ffname);
3223 return FAIL;
3224 }
3225#ifdef USE_FNAME_CASE
3226# ifdef USE_LONG_FNAME
3227 if (USE_LONG_FNAME)
3228# endif
3229 fname_case(sfname, 0); /* set correct case for short file name */
3230#endif
3231 vim_free(buf->b_ffname);
3232 vim_free(buf->b_sfname);
3233 buf->b_ffname = ffname;
3234 buf->b_sfname = sfname;
3235 }
3236 buf->b_fname = buf->b_sfname;
3237#ifdef UNIX
3238 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003239 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 else
3241 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003242 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 buf->b_dev = st.st_dev;
3244 buf->b_ino = st.st_ino;
3245 }
3246#endif
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250 buf_name_changed(buf);
3251 return OK;
3252}
3253
3254/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003255 * Crude way of changing the name of a buffer. Use with care!
3256 * The name should be relative to the current directory.
3257 */
3258 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003259buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003260{
3261 buf_T *buf;
3262
3263 buf = buflist_findnr(fnum);
3264 if (buf != NULL)
3265 {
3266 vim_free(buf->b_sfname);
3267 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003268 buf->b_ffname = vim_strsave(name);
3269 buf->b_sfname = NULL;
3270 /* Allocate ffname and expand into full path. Also resolves .lnk
3271 * files on Win32. */
3272 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003273 buf->b_fname = buf->b_sfname;
3274 }
3275}
3276
3277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 * Take care of what needs to be done when the name of buffer "buf" has
3279 * changed.
3280 */
3281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003282buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 /*
3285 * If the file name changed, also change the name of the swapfile
3286 */
3287 if (buf->b_ml.ml_mfp != NULL)
3288 ml_setname(buf);
3289
3290 if (curwin->w_buffer == buf)
3291 check_arg_idx(curwin); /* check file name for arg list */
3292#ifdef FEAT_TITLE
3293 maketitle(); /* set window title */
3294#endif
3295#ifdef FEAT_WINDOWS
3296 status_redraw_all(); /* status lines need to be redrawn */
3297#endif
3298 fmarks_check_names(buf); /* check named file marks */
3299 ml_timestamp(buf); /* reset timestamp */
3300}
3301
3302/*
3303 * set alternate file name for current window
3304 *
3305 * Used by do_one_cmd(), do_write() and do_ecmd().
3306 * Return the buffer.
3307 */
3308 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003309setaltfname(
3310 char_u *ffname,
3311 char_u *sfname,
3312 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 buf_T *buf;
3315
3316 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3317 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003318 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 curwin->w_alt_fnum = buf->b_fnum;
3320 return buf;
3321}
3322
3323/*
3324 * Get alternate file name for current window.
3325 * Return NULL if there isn't any, and give error message if requested.
3326 */
3327 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328getaltfname(
3329 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
3331 char_u *fname;
3332 linenr_T dummy;
3333
3334 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3335 {
3336 if (errmsg)
3337 EMSG(_(e_noalt));
3338 return NULL;
3339 }
3340 return fname;
3341}
3342
3343/*
3344 * Add a file name to the buflist and return its number.
3345 * Uses same flags as buflist_new(), except BLN_DUMMY.
3346 *
3347 * used by qf_init(), main() and doarglist()
3348 */
3349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003350buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 buf_T *buf;
3353
3354 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3355 if (buf != NULL)
3356 return buf->b_fnum;
3357 return 0;
3358}
3359
3360#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3361/*
3362 * Adjust slashes in file names. Called after 'shellslash' was set.
3363 */
3364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003365buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 buf_T *bp;
3368
Bram Moolenaar29323592016-07-24 22:04:11 +02003369 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
3371 if (bp->b_ffname != NULL)
3372 slash_adjust(bp->b_ffname);
3373 if (bp->b_sfname != NULL)
3374 slash_adjust(bp->b_sfname);
3375 }
3376}
3377#endif
3378
3379/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003380 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 * Also save the local window option values.
3382 */
3383 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003384buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003386 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387}
3388
3389/*
3390 * Return TRUE if 'ffname' is not the same file as current file.
3391 * Fname must have a full path (expanded by mch_FullName()).
3392 */
3393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003394otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
3396 return otherfile_buf(curbuf, ffname
3397#ifdef UNIX
3398 , NULL
3399#endif
3400 );
3401}
3402
3403 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003404otherfile_buf(
3405 buf_T *buf,
3406 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003408 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003410 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
3412 /* no name is different */
3413 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3414 return TRUE;
3415 if (fnamecmp(ffname, buf->b_ffname) == 0)
3416 return FALSE;
3417#ifdef UNIX
3418 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003419 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaar8767f522016-07-01 17:17:39 +02003421 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (stp == NULL)
3423 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003424 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 st.st_dev = (dev_T)-1;
3426 stp = &st;
3427 }
3428 /* Use dev/ino to check if the files are the same, even when the names
3429 * are different (possible with links). Still need to compare the
3430 * name above, for when the file doesn't exist yet.
3431 * Problem: The dev/ino changes when a file is deleted (and created
3432 * again) and remains the same when renamed/moved. We don't want to
3433 * mch_stat() each buffer each time, that would be too slow. Get the
3434 * dev/ino again when they appear to match, but not when they appear
3435 * to be different: Could skip a buffer when it's actually the same
3436 * file. */
3437 if (buf_same_ino(buf, stp))
3438 {
3439 buf_setino(buf);
3440 if (buf_same_ino(buf, stp))
3441 return FALSE;
3442 }
3443 }
3444#endif
3445 return TRUE;
3446}
3447
3448#if defined(UNIX) || defined(PROTO)
3449/*
3450 * Set inode and device number for a buffer.
3451 * Must always be called when b_fname is changed!.
3452 */
3453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003454buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003456 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3459 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003460 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 buf->b_dev = st.st_dev;
3462 buf->b_ino = st.st_ino;
3463 }
3464 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003465 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466}
3467
3468/*
3469 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3470 */
3471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003472buf_same_ino(
3473 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003474 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003476 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 && stp->st_dev == buf->b_dev
3478 && stp->st_ino == buf->b_ino);
3479}
3480#endif
3481
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003482/*
3483 * Print info about the current buffer.
3484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003486fileinfo(
3487 int fullname, /* when non-zero print full path */
3488 int shorthelp,
3489 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
3491 char_u *name;
3492 int n;
3493 char_u *p;
3494 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003495 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 buffer = alloc(IOSIZE);
3498 if (buffer == NULL)
3499 return;
3500
3501 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3502 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003503 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 p = buffer + STRLEN(buffer);
3505 }
3506 else
3507 p = buffer;
3508
3509 *p++ = '"';
3510 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003511 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 else
3513 {
3514 if (!fullname && curbuf->b_fname != NULL)
3515 name = curbuf->b_fname;
3516 else
3517 name = curbuf->b_ffname;
3518 home_replace(shorthelp ? curbuf : NULL, name, p,
3519 (int)(IOSIZE - (p - buffer)), TRUE);
3520 }
3521
Bram Moolenaara800b422010-06-27 01:15:55 +02003522 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 curbufIsChanged() ? (shortmess(SHM_MOD)
3524 ? " [+]" : _(" [Modified]")) : " ",
3525 (curbuf->b_flags & BF_NOTEDITED)
3526#ifdef FEAT_QUICKFIX
3527 && !bt_dontwrite(curbuf)
3528#endif
3529 ? _("[Not edited]") : "",
3530 (curbuf->b_flags & BF_NEW)
3531#ifdef FEAT_QUICKFIX
3532 && !bt_dontwrite(curbuf)
3533#endif
3534 ? _("[New file]") : "",
3535 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003536 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 : _("[readonly]")) : "",
3538 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3539 || curbuf->b_p_ro) ?
3540 " " : "");
3541 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3542 * causes an overflow, thus for large numbers divide instead. */
3543 if (curwin->w_cursor.lnum > 1000000L)
3544 n = (int)(((long)curwin->w_cursor.lnum) /
3545 ((long)curbuf->b_ml.ml_line_count / 100L));
3546 else
3547 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3548 (long)curbuf->b_ml.ml_line_count);
3549 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3550 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003551 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
3553#ifdef FEAT_CMDL_INFO
3554 else if (p_ru)
3555 {
3556 /* Current line and column are already on the screen -- webb */
3557 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003558 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003560 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 (long)curbuf->b_ml.ml_line_count, n);
3562 }
3563#endif
3564 else
3565 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003566 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 _("line %ld of %ld --%d%%-- col "),
3568 (long)curwin->w_cursor.lnum,
3569 (long)curbuf->b_ml.ml_line_count,
3570 n);
3571 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003572 len = STRLEN(buffer);
3573 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3575 }
3576
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003577 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
3579 if (dont_truncate)
3580 {
3581 /* Temporarily set msg_scroll to avoid the message being truncated.
3582 * First call msg_start() to get the message in the right place. */
3583 msg_start();
3584 n = msg_scroll;
3585 msg_scroll = TRUE;
3586 msg(buffer);
3587 msg_scroll = n;
3588 }
3589 else
3590 {
3591 p = msg_trunc_attr(buffer, FALSE, 0);
3592 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 /* Need to repeat the message after redrawing when:
3594 * - When restart_edit is set (otherwise there will be a delay
3595 * before redrawing).
3596 * - When the screen was scrolled but there is no wait-return
3597 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003598 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
3600
3601 vim_free(buffer);
3602}
3603
3604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003605col_print(
3606 char_u *buf,
3607 size_t buflen,
3608 int col,
3609 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003612 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003614 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615}
3616
3617#if defined(FEAT_TITLE) || defined(PROTO)
3618/*
3619 * put file name in title bar of window and in icon title
3620 */
3621
3622static char_u *lasttitle = NULL;
3623static char_u *lasticon = NULL;
3624
3625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
3628 char_u *p;
3629 char_u *t_str = NULL;
3630 char_u *i_name;
3631 char_u *i_str = NULL;
3632 int maxlen = 0;
3633 int len;
3634 int mustset;
3635 char_u buf[IOSIZE];
3636 int off;
3637
3638 if (!redrawing())
3639 {
3640 /* Postpone updating the title when 'lazyredraw' is set. */
3641 need_maketitle = TRUE;
3642 return;
3643 }
3644
3645 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003646 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 return;
3648
3649 if (p_title)
3650 {
3651 if (p_titlelen > 0)
3652 {
3653 maxlen = p_titlelen * Columns / 100;
3654 if (maxlen < 10)
3655 maxlen = 10;
3656 }
3657
3658 t_str = buf;
3659 if (*p_titlestring != NUL)
3660 {
3661#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003662 if (stl_syntax & STL_IN_TITLE)
3663 {
3664 int use_sandbox = FALSE;
3665 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003666
3667# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003668 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003669# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003670 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003672 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003673 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003674 if (called_emsg)
3675 set_string_option_direct((char_u *)"titlestring", -1,
3676 (char_u *)"", OPT_FREE, SID_ERROR);
3677 called_emsg |= save_called_emsg;
3678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 else
3680#endif
3681 t_str = p_titlestring;
3682 }
3683 else
3684 {
3685 /* format: "fname + (path) (1 of 2) - VIM" */
3686
Bram Moolenaar2c666692012-09-05 13:30:40 +02003687#define SPACE_FOR_FNAME (IOSIZE - 100)
3688#define SPACE_FOR_DIR (IOSIZE - 20)
3689#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003691 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003692#ifdef FEAT_TERMINAL
3693 else if (curbuf->b_term != NULL)
3694 {
3695 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3696 SPACE_FOR_FNAME);
3697 }
3698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 else
3700 {
3701 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003702 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705
Bram Moolenaar21554412017-07-24 21:44:43 +02003706#ifdef FEAT_TERMINAL
3707 if (curbuf->b_term == NULL)
3708#endif
3709 switch (bufIsChanged(curbuf)
3710 + (curbuf->b_p_ro * 2)
3711 + (!curbuf->b_p_ma * 4))
3712 {
3713 case 1: STRCAT(buf, " +"); break;
3714 case 2: STRCAT(buf, " ="); break;
3715 case 3: STRCAT(buf, " =+"); break;
3716 case 4:
3717 case 6: STRCAT(buf, " -"); break;
3718 case 5:
3719 case 7: STRCAT(buf, " -+"); break;
3720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
Bram Moolenaar21554412017-07-24 21:44:43 +02003722 if (curbuf->b_fname != NULL
3723#ifdef FEAT_TERMINAL
3724 && curbuf->b_term == NULL
3725#endif
3726 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
3728 /* Get path of file, replace home dir with ~ */
3729 off = (int)STRLEN(buf);
3730 buf[off++] = ' ';
3731 buf[off++] = '(';
3732 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003733 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#ifdef BACKSLASH_IN_FILENAME
3735 /* avoid "c:/name" to be reduced to "c" */
3736 if (isalpha(buf[off]) && buf[off + 1] == ':')
3737 off += 2;
3738#endif
3739 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003740 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003742 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003743 /* must be a help buffer */
3744 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003745 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003749
Bram Moolenaar2c666692012-09-05 13:30:40 +02003750 /* Translate unprintable chars and concatenate. Keep some
3751 * room for the server name. When there is no room (very long
3752 * file name) use (...). */
3753 if (off < SPACE_FOR_DIR)
3754 {
3755 p = transstr(buf + off);
3756 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3757 vim_free(p);
3758 }
3759 else
3760 {
3761 vim_strncpy(buf + off, (char_u *)"...",
3762 (size_t)(SPACE_FOR_ARGNR - off));
3763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 STRCAT(buf, ")");
3765 }
3766
Bram Moolenaar2c666692012-09-05 13:30:40 +02003767 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769#if defined(FEAT_CLIENTSERVER)
3770 if (serverName != NULL)
3771 {
3772 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003773 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775 else
3776#endif
3777 STRCAT(buf, " - VIM");
3778
3779 if (maxlen > 0)
3780 {
3781 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003782 if (vim_strsize(buf) > maxlen)
3783 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
3785 }
3786 }
3787 mustset = ti_change(t_str, &lasttitle);
3788
3789 if (p_icon)
3790 {
3791 i_str = buf;
3792 if (*p_iconstring != NUL)
3793 {
3794#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003795 if (stl_syntax & STL_IN_ICON)
3796 {
3797 int use_sandbox = FALSE;
3798 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003799
3800# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003801 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003802# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003803 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003805 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003806 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003807 if (called_emsg)
3808 set_string_option_direct((char_u *)"iconstring", -1,
3809 (char_u *)"", OPT_FREE, SID_ERROR);
3810 called_emsg |= save_called_emsg;
3811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 else
3813#endif
3814 i_str = p_iconstring;
3815 }
3816 else
3817 {
3818 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003819 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 else /* use file name only in icon */
3821 i_name = gettail(curbuf->b_ffname);
3822 *i_str = NUL;
3823 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003824 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (len > 100)
3826 {
3827 len -= 100;
3828#ifdef FEAT_MBYTE
3829 if (has_mbyte)
3830 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3831#endif
3832 i_name += len;
3833 }
3834 STRCPY(i_str, i_name);
3835 trans_characters(i_str, IOSIZE);
3836 }
3837 }
3838
3839 mustset |= ti_change(i_str, &lasticon);
3840
3841 if (mustset)
3842 resettitle();
3843}
3844
3845/*
3846 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3847 * from "str" if it does.
3848 * Return TRUE when "*last" changed.
3849 */
3850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003851ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
3853 if ((str == NULL) != (*last == NULL)
3854 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3855 {
3856 vim_free(*last);
3857 if (str == NULL)
3858 *last = NULL;
3859 else
3860 *last = vim_strsave(str);
3861 return TRUE;
3862 }
3863 return FALSE;
3864}
3865
3866/*
3867 * Put current window title back (used after calling a shell)
3868 */
3869 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003870resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
3872 mch_settitle(lasttitle, lasticon);
3873}
Bram Moolenaarea408852005-06-25 22:49:46 +00003874
3875# if defined(EXITFREE) || defined(PROTO)
3876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003877free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003878{
3879 vim_free(lasttitle);
3880 vim_free(lasticon);
3881}
3882# endif
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884#endif /* FEAT_TITLE */
3885
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003886#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003888 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 * Return length of string in screen cells.
3890 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003891 * Normally works for window "wp", except when working for 'tabline' then it
3892 * is "curwin".
3893 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 * Items are drawn interspersed with the text that surrounds it
3895 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3896 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3897 *
3898 * If maxwidth is not zero, the string will be filled at any middle marker
3899 * or truncated if too long, fillchar is used for all whitespace.
3900 */
3901 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003902build_stl_str_hl(
3903 win_T *wp,
3904 char_u *out, /* buffer to write into != NameBuff */
3905 size_t outlen, /* length of out[] */
3906 char_u *fmt,
3907 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3908 int fillchar,
3909 int maxwidth,
3910 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3911 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
3913 char_u *p;
3914 char_u *s;
3915 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003916 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917#ifdef FEAT_EVAL
3918 win_T *o_curwin;
3919 buf_T *o_curbuf;
3920#endif
3921 int empty_line;
3922 colnr_T virtcol;
3923 long l;
3924 long n;
3925 int prevchar_isflag;
3926 int prevchar_isitem;
3927 int itemisflag;
3928 int fillable;
3929 char_u *str;
3930 long num;
3931 int width;
3932 int itemcnt;
3933 int curitem;
3934 int groupitem[STL_MAX_ITEM];
3935 int groupdepth;
3936 struct stl_item
3937 {
3938 char_u *start;
3939 int minwid;
3940 int maxwid;
3941 enum
3942 {
3943 Normal,
3944 Empty,
3945 Group,
3946 Middle,
3947 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003948 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 Trunc
3950 } type;
3951 } item[STL_MAX_ITEM];
3952 int minwid;
3953 int maxwid;
3954 int zeropad;
3955 char_u base;
3956 char_u opt;
3957#define TMPLEN 70
3958 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003959 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003960 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003961
3962#ifdef FEAT_EVAL
3963 /*
3964 * When the format starts with "%!" then evaluate it as an expression and
3965 * use the result as the actual format string.
3966 */
3967 if (fmt[0] == '%' && fmt[1] == '!')
3968 {
3969 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3970 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003971 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003972 }
3973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 if (fillchar == 0)
3976 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003977#ifdef FEAT_MBYTE
3978 /* Can't handle a multi-byte fill character yet. */
3979 else if (mb_char2len(fillchar) > 1)
3980 fillchar = '-';
3981#endif
3982
Bram Moolenaar567199b2013-04-24 16:52:36 +02003983 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3984 * p will become invalid when getting another buffer line. */
3985 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3986 empty_line = (*p == NUL);
3987
3988 /* Get the byte value now, in case we need it below. This is more
3989 * efficient than making a copy of the line. */
3990 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3991 byteval = 0;
3992 else
3993#ifdef FEAT_MBYTE
3994 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3995#else
3996 byteval = p[wp->w_cursor.col];
3997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 groupdepth = 0;
4000 p = out;
4001 curitem = 0;
4002 prevchar_isflag = TRUE;
4003 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004004 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004006 if (curitem == STL_MAX_ITEM)
4007 {
4008 /* There are too many items. Add the error code to the statusline
4009 * to give the user a hint about what went wrong. */
4010 if (p + 6 < out + outlen)
4011 {
4012 mch_memmove(p, " E541", (size_t)5);
4013 p += 5;
4014 }
4015 break;
4016 }
4017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (*s != NUL && *s != '%')
4019 prevchar_isflag = prevchar_isitem = FALSE;
4020
4021 /*
4022 * Handle up to the next '%' or the end.
4023 */
4024 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4025 *p++ = *s++;
4026 if (*s == NUL || p + 1 >= out + outlen)
4027 break;
4028
4029 /*
4030 * Handle one '%' item.
4031 */
4032 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004033 if (*s == NUL) /* ignore trailing % */
4034 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (*s == '%')
4036 {
4037 if (p + 1 >= out + outlen)
4038 break;
4039 *p++ = *s++;
4040 prevchar_isflag = prevchar_isitem = FALSE;
4041 continue;
4042 }
4043 if (*s == STL_MIDDLEMARK)
4044 {
4045 s++;
4046 if (groupdepth > 0)
4047 continue;
4048 item[curitem].type = Middle;
4049 item[curitem++].start = p;
4050 continue;
4051 }
4052 if (*s == STL_TRUNCMARK)
4053 {
4054 s++;
4055 item[curitem].type = Trunc;
4056 item[curitem++].start = p;
4057 continue;
4058 }
4059 if (*s == ')')
4060 {
4061 s++;
4062 if (groupdepth < 1)
4063 continue;
4064 groupdepth--;
4065
4066 t = item[groupitem[groupdepth]].start;
4067 *p = NUL;
4068 l = vim_strsize(t);
4069 if (curitem > groupitem[groupdepth] + 1
4070 && item[groupitem[groupdepth]].minwid == 0)
4071 {
4072 /* remove group if all items are empty */
4073 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01004074 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 break;
4076 if (n == curitem)
4077 {
4078 p = t;
4079 l = 0;
4080 }
4081 }
4082 if (l > item[groupitem[groupdepth]].maxwid)
4083 {
4084 /* truncate, remove n bytes of text at the start */
4085#ifdef FEAT_MBYTE
4086 if (has_mbyte)
4087 {
4088 /* Find the first character that should be included. */
4089 n = 0;
4090 while (l >= item[groupitem[groupdepth]].maxwid)
4091 {
4092 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004093 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095 }
4096 else
4097#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004098 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004101 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 p = p - n + 1;
4103#ifdef FEAT_MBYTE
4104 /* Fill up space left over by half a double-wide char. */
4105 while (++l < item[groupitem[groupdepth]].minwid)
4106 *p++ = fillchar;
4107#endif
4108
4109 /* correct the start of the items for the truncation */
4110 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4111 {
4112 item[l].start -= n;
4113 if (item[l].start < t)
4114 item[l].start = t;
4115 }
4116 }
4117 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4118 {
4119 /* fill */
4120 n = item[groupitem[groupdepth]].minwid;
4121 if (n < 0)
4122 {
4123 /* fill by appending characters */
4124 n = 0 - n;
4125 while (l++ < n && p + 1 < out + outlen)
4126 *p++ = fillchar;
4127 }
4128 else
4129 {
4130 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004131 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 l = n - l;
4133 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004134 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 p += l;
4136 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4137 item[n].start += l;
4138 for ( ; l > 0; l--)
4139 *t++ = fillchar;
4140 }
4141 }
4142 continue;
4143 }
4144 minwid = 0;
4145 maxwid = 9999;
4146 zeropad = FALSE;
4147 l = 1;
4148 if (*s == '0')
4149 {
4150 s++;
4151 zeropad = TRUE;
4152 }
4153 if (*s == '-')
4154 {
4155 s++;
4156 l = -1;
4157 }
4158 if (VIM_ISDIGIT(*s))
4159 {
4160 minwid = (int)getdigits(&s);
4161 if (minwid < 0) /* overflow */
4162 minwid = 0;
4163 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004164 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 item[curitem].type = Highlight;
4167 item[curitem].start = p;
4168 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4169 s++;
4170 curitem++;
4171 continue;
4172 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004173 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4174 {
4175 if (*s == STL_TABCLOSENR)
4176 {
4177 if (minwid == 0)
4178 {
4179 /* %X ends the close label, go back to the previously
4180 * define tab label nr. */
4181 for (n = curitem - 1; n >= 0; --n)
4182 if (item[n].type == TabPage && item[n].minwid >= 0)
4183 {
4184 minwid = item[n].minwid;
4185 break;
4186 }
4187 }
4188 else
4189 /* close nrs are stored as negative values */
4190 minwid = - minwid;
4191 }
4192 item[curitem].type = TabPage;
4193 item[curitem].start = p;
4194 item[curitem].minwid = minwid;
4195 s++;
4196 curitem++;
4197 continue;
4198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 if (*s == '.')
4200 {
4201 s++;
4202 if (VIM_ISDIGIT(*s))
4203 {
4204 maxwid = (int)getdigits(&s);
4205 if (maxwid <= 0) /* overflow */
4206 maxwid = 50;
4207 }
4208 }
4209 minwid = (minwid > 50 ? 50 : minwid) * l;
4210 if (*s == '(')
4211 {
4212 groupitem[groupdepth++] = curitem;
4213 item[curitem].type = Group;
4214 item[curitem].start = p;
4215 item[curitem].minwid = minwid;
4216 item[curitem].maxwid = maxwid;
4217 s++;
4218 curitem++;
4219 continue;
4220 }
4221 if (vim_strchr(STL_ALL, *s) == NULL)
4222 {
4223 s++;
4224 continue;
4225 }
4226 opt = *s++;
4227
4228 /* OK - now for the real work */
4229 base = 'D';
4230 itemisflag = FALSE;
4231 fillable = TRUE;
4232 num = -1;
4233 str = NULL;
4234 switch (opt)
4235 {
4236 case STL_FILEPATH:
4237 case STL_FULLPATH:
4238 case STL_FILENAME:
4239 fillable = FALSE; /* don't change ' ' to fillchar */
4240 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004241 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 else
4243 {
4244 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004245 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4247 }
4248 trans_characters(NameBuff, MAXPATHL);
4249 if (opt != STL_FILENAME)
4250 str = NameBuff;
4251 else
4252 str = gettail(NameBuff);
4253 break;
4254
4255 case STL_VIM_EXPR: /* '{' */
4256 itemisflag = TRUE;
4257 t = p;
4258 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4259 *p++ = *s++;
4260 if (*s != '}') /* missing '}' or out of space */
4261 break;
4262 s++;
4263 *p = 0;
4264 p = t;
4265
4266#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004267 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4269
4270 o_curbuf = curbuf;
4271 o_curwin = curwin;
4272 curwin = wp;
4273 curbuf = wp->w_buffer;
4274
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004275 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 curwin = o_curwin;
4278 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004279 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
4281 if (str != NULL && *str != 0)
4282 {
4283 if (*skipdigits(str) == NUL)
4284 {
4285 num = atoi((char *)str);
4286 vim_free(str);
4287 str = NULL;
4288 itemisflag = FALSE;
4289 }
4290 }
4291#endif
4292 break;
4293
4294 case STL_LINE:
4295 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4296 ? 0L : (long)(wp->w_cursor.lnum);
4297 break;
4298
4299 case STL_NUMLINES:
4300 num = wp->w_buffer->b_ml.ml_line_count;
4301 break;
4302
4303 case STL_COLUMN:
4304 num = !(State & INSERT) && empty_line
4305 ? 0 : (int)wp->w_cursor.col + 1;
4306 break;
4307
4308 case STL_VIRTCOL:
4309 case STL_VIRTCOL_ALT:
4310 /* In list mode virtcol needs to be recomputed */
4311 virtcol = wp->w_virtcol;
4312 if (wp->w_p_list && lcs_tab1 == NUL)
4313 {
4314 wp->w_p_list = FALSE;
4315 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4316 wp->w_p_list = TRUE;
4317 }
4318 ++virtcol;
4319 /* Don't display %V if it's the same as %c. */
4320 if (opt == STL_VIRTCOL_ALT
4321 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4322 ? 0 : (int)wp->w_cursor.col + 1)))
4323 break;
4324 num = (long)virtcol;
4325 break;
4326
4327 case STL_PERCENTAGE:
4328 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4329 (long)wp->w_buffer->b_ml.ml_line_count);
4330 break;
4331
4332 case STL_ALTPERCENT:
4333 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004334 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 break;
4336
4337 case STL_ARGLISTSTAT:
4338 fillable = FALSE;
4339 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004340 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 str = tmp;
4342 break;
4343
4344 case STL_KEYMAP:
4345 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004346 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 str = tmp;
4348 break;
4349 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004350#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004351 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352#else
4353 num = 0;
4354#endif
4355 break;
4356
4357 case STL_BUFNO:
4358 num = wp->w_buffer->b_fnum;
4359 break;
4360
4361 case STL_OFFSET_X:
4362 base = 'X';
4363 case STL_OFFSET:
4364#ifdef FEAT_BYTEOFF
4365 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4366 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4367 0L : l + 1 + (!(State & INSERT) && empty_line ?
4368 0 : (int)wp->w_cursor.col);
4369#endif
4370 break;
4371
4372 case STL_BYTEVAL_X:
4373 base = 'X';
4374 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004375 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 if (num == NL)
4377 num = 0;
4378 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4379 num = NL;
4380 break;
4381
4382 case STL_ROFLAG:
4383 case STL_ROFLAG_ALT:
4384 itemisflag = TRUE;
4385 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004386 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 break;
4388
4389 case STL_HELPFLAG:
4390 case STL_HELPFLAG_ALT:
4391 itemisflag = TRUE;
4392 if (wp->w_buffer->b_help)
4393 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004394 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 break;
4396
4397#ifdef FEAT_AUTOCMD
4398 case STL_FILETYPE:
4399 if (*wp->w_buffer->b_p_ft != NUL
4400 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4401 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004402 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4403 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 str = tmp;
4405 }
4406 break;
4407
4408 case STL_FILETYPE_ALT:
4409 itemisflag = TRUE;
4410 if (*wp->w_buffer->b_p_ft != NUL
4411 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4412 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004413 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4414 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 for (t = tmp; *t != 0; t++)
4416 *t = TOUPPER_LOC(*t);
4417 str = tmp;
4418 }
4419 break;
4420#endif
4421
4422#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4423 case STL_PREVIEWFLAG:
4424 case STL_PREVIEWFLAG_ALT:
4425 itemisflag = TRUE;
4426 if (wp->w_p_pvw)
4427 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4428 : _("[Preview]"));
4429 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004430
4431 case STL_QUICKFIX:
4432 if (bt_quickfix(wp->w_buffer))
4433 str = (char_u *)(wp->w_llist_ref
4434 ? _(msg_loclist)
4435 : _(msg_qflist));
4436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437#endif
4438
4439 case STL_MODIFIED:
4440 case STL_MODIFIED_ALT:
4441 itemisflag = TRUE;
4442 switch ((opt == STL_MODIFIED_ALT)
4443 + bufIsChanged(wp->w_buffer) * 2
4444 + (!wp->w_buffer->b_p_ma) * 4)
4445 {
4446 case 2: str = (char_u *)"[+]"; break;
4447 case 3: str = (char_u *)",+"; break;
4448 case 4: str = (char_u *)"[-]"; break;
4449 case 5: str = (char_u *)",-"; break;
4450 case 6: str = (char_u *)"[+-]"; break;
4451 case 7: str = (char_u *)",+-"; break;
4452 }
4453 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004454
4455 case STL_HIGHLIGHT:
4456 t = s;
4457 while (*s != '#' && *s != NUL)
4458 ++s;
4459 if (*s == '#')
4460 {
4461 item[curitem].type = Highlight;
4462 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004463 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004464 curitem++;
4465 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004466 if (*s != NUL)
4467 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004468 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470
4471 item[curitem].start = p;
4472 item[curitem].type = Normal;
4473 if (str != NULL && *str)
4474 {
4475 t = str;
4476 if (itemisflag)
4477 {
4478 if ((t[0] && t[1])
4479 && ((!prevchar_isitem && *t == ',')
4480 || (prevchar_isflag && *t == ' ')))
4481 t++;
4482 prevchar_isflag = TRUE;
4483 }
4484 l = vim_strsize(t);
4485 if (l > 0)
4486 prevchar_isitem = TRUE;
4487 if (l > maxwid)
4488 {
4489 while (l >= maxwid)
4490#ifdef FEAT_MBYTE
4491 if (has_mbyte)
4492 {
4493 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004494 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496 else
4497#endif
4498 l -= byte2cells(*t++);
4499 if (p + 1 >= out + outlen)
4500 break;
4501 *p++ = '<';
4502 }
4503 if (minwid > 0)
4504 {
4505 for (; l < minwid && p + 1 < out + outlen; l++)
4506 {
4507 /* Don't put a "-" in front of a digit. */
4508 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4509 *p++ = ' ';
4510 else
4511 *p++ = fillchar;
4512 }
4513 minwid = 0;
4514 }
4515 else
4516 minwid *= -1;
4517 while (*t && p + 1 < out + outlen)
4518 {
4519 *p++ = *t++;
4520 /* Change a space by fillchar, unless fillchar is '-' and a
4521 * digit follows. */
4522 if (fillable && p[-1] == ' '
4523 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4524 p[-1] = fillchar;
4525 }
4526 for (; l < minwid && p + 1 < out + outlen; l++)
4527 *p++ = fillchar;
4528 }
4529 else if (num >= 0)
4530 {
4531 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4532 char_u nstr[20];
4533
4534 if (p + 20 >= out + outlen)
4535 break; /* not sufficient space */
4536 prevchar_isitem = TRUE;
4537 t = nstr;
4538 if (opt == STL_VIRTCOL_ALT)
4539 {
4540 *t++ = '-';
4541 minwid--;
4542 }
4543 *t++ = '%';
4544 if (zeropad)
4545 *t++ = '0';
4546 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004547 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 *t = 0;
4549
4550 for (n = num, l = 1; n >= nbase; n /= nbase)
4551 l++;
4552 if (opt == STL_VIRTCOL_ALT)
4553 l++;
4554 if (l > maxwid)
4555 {
4556 l += 2;
4557 n = l - maxwid;
4558 while (l-- > maxwid)
4559 num /= nbase;
4560 *t++ = '>';
4561 *t++ = '%';
4562 *t = t[-3];
4563 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004564 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4565 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004568 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4569 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 p += STRLEN(p);
4571 }
4572 else
4573 item[curitem].type = Empty;
4574
4575 if (opt == STL_VIM_EXPR)
4576 vim_free(str);
4577
4578 if (num >= 0 || (!itemisflag && str && *str))
4579 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4580 curitem++;
4581 }
4582 *p = NUL;
4583 itemcnt = curitem;
4584
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004585#ifdef FEAT_EVAL
4586 if (usefmt != fmt)
4587 vim_free(usefmt);
4588#endif
4589
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 width = vim_strsize(out);
4591 if (maxwidth > 0 && width > maxwidth)
4592 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004593 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 l = 0;
4595 if (itemcnt == 0)
4596 s = out;
4597 else
4598 {
4599 for ( ; l < itemcnt; l++)
4600 if (item[l].type == Trunc)
4601 {
4602 /* Truncate at %< item. */
4603 s = item[l].start;
4604 break;
4605 }
4606 if (l == itemcnt)
4607 {
4608 /* No %< item, truncate first item. */
4609 s = item[0].start;
4610 l = 0;
4611 }
4612 }
4613
4614 if (width - vim_strsize(s) >= maxwidth)
4615 {
4616 /* Truncation mark is beyond max length */
4617#ifdef FEAT_MBYTE
4618 if (has_mbyte)
4619 {
4620 s = out;
4621 width = 0;
4622 for (;;)
4623 {
4624 width += ptr2cells(s);
4625 if (width >= maxwidth)
4626 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004627 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 /* Fill up for half a double-wide character. */
4630 while (++width < maxwidth)
4631 *s++ = fillchar;
4632 }
4633 else
4634#endif
4635 s = out + maxwidth - 1;
4636 for (l = 0; l < itemcnt; l++)
4637 if (item[l].start > s)
4638 break;
4639 itemcnt = l;
4640 *s++ = '>';
4641 *s = 0;
4642 }
4643 else
4644 {
4645#ifdef FEAT_MBYTE
4646 if (has_mbyte)
4647 {
4648 n = 0;
4649 while (width >= maxwidth)
4650 {
4651 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004652 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 }
4655 else
4656#endif
4657 n = width - maxwidth + 1;
4658 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004659 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 *s = '<';
4661
4662 /* Fill up for half a double-wide character. */
4663 while (++width < maxwidth)
4664 {
4665 s = s + STRLEN(s);
4666 *s++ = fillchar;
4667 *s = NUL;
4668 }
4669
4670 --n; /* count the '<' */
4671 for (; l < itemcnt; l++)
4672 {
4673 if (item[l].start - n >= s)
4674 item[l].start -= n;
4675 else
4676 item[l].start = s;
4677 }
4678 }
4679 width = maxwidth;
4680 }
4681 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4682 {
4683 /* Apply STL_MIDDLE if any */
4684 for (l = 0; l < itemcnt; l++)
4685 if (item[l].type == Middle)
4686 break;
4687 if (l < itemcnt)
4688 {
4689 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004690 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 for (s = item[l].start; s < p; s++)
4692 *s = fillchar;
4693 for (l++; l < itemcnt; l++)
4694 item[l].start += maxwidth - width;
4695 width = maxwidth;
4696 }
4697 }
4698
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004699 /* Store the info about highlighting. */
4700 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004702 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 for (l = 0; l < itemcnt; l++)
4704 {
4705 if (item[l].type == Highlight)
4706 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004707 sp->start = item[l].start;
4708 sp->userhl = item[l].minwid;
4709 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
4711 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004712 sp->start = NULL;
4713 sp->userhl = 0;
4714 }
4715
4716 /* Store the info about tab pages labels. */
4717 if (tabtab != NULL)
4718 {
4719 sp = tabtab;
4720 for (l = 0; l < itemcnt; l++)
4721 {
4722 if (item[l].type == TabPage)
4723 {
4724 sp->start = item[l].start;
4725 sp->userhl = item[l].minwid;
4726 sp++;
4727 }
4728 }
4729 sp->start = NULL;
4730 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732
4733 return width;
4734}
4735#endif /* FEAT_STL_OPT */
4736
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004737#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4738 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004740 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4741 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 */
4743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004744get_rel_pos(
4745 win_T *wp,
4746 char_u *buf,
4747 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 long above; /* number of lines above window */
4750 long below; /* number of lines below window */
4751
Bram Moolenaar0027c212015-01-07 13:31:52 +01004752 if (buflen < 3) /* need at least 3 chars for writing */
4753 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 above = wp->w_topline - 1;
4755#ifdef FEAT_DIFF
4756 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004757 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4758 above = 0; /* All buffer lines are displayed and there is an
4759 * indication of filler lines, that can be considered
4760 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#endif
4762 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4763 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004764 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4765 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004767 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004769 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 ? (int)(above / ((above + below) / 100L))
4771 : (int)(above * 100L / (above + below)));
4772}
4773#endif
4774
4775/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004776 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 * Return TRUE if it was appended.
4778 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004779 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004780append_arg_number(
4781 win_T *wp,
4782 char_u *buf,
4783 int buflen,
4784 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
4786 char_u *p;
4787
4788 if (ARGCOUNT <= 1) /* nothing to do */
4789 return FALSE;
4790
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004791 p = buf + STRLEN(buf); /* go to the end of the buffer */
4792 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 return FALSE;
4794 *p++ = ' ';
4795 *p++ = '(';
4796 if (add_file)
4797 {
4798 STRCPY(p, "file ");
4799 p += 5;
4800 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004801 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4802 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4804 return TRUE;
4805}
4806
4807/*
4808 * If fname is not a full path, make it a full path.
4809 * Returns pointer to allocated memory (NULL for failure).
4810 */
4811 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004812fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 /*
4815 * Force expanding the path always for Unix, because symbolic links may
4816 * mess up the full path name, even though it starts with a '/'.
4817 * Also expand when there is ".." in the file name, try to remove it,
4818 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004819 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 * For MS-Windows also expand names like "longna~1" to "longname".
4821 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004822#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return FullName_save(fname, TRUE);
4824#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004825 if (!vim_isAbsName(fname)
4826 || strstr((char *)fname, "..") != NULL
4827 || strstr((char *)fname, "//") != NULL
4828# ifdef BACKSLASH_IN_FILENAME
4829 || strstr((char *)fname, "\\\\") != NULL
4830# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004831# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 )
4835 return FullName_save(fname, FALSE);
4836
4837 fname = vim_strsave(fname);
4838
Bram Moolenaar9b942202007-10-03 12:31:33 +00004839# ifdef USE_FNAME_CASE
4840# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
4844 if (fname != NULL)
4845 fname_case(fname, 0); /* set correct case for file name */
4846 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
4849 return fname;
4850#endif
4851}
4852
4853/*
4854 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4855 * "ffname" becomes a pointer to allocated memory (or NULL).
4856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004858fname_expand(
4859 buf_T *buf UNUSED,
4860 char_u **ffname,
4861 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 if (*ffname == NULL) /* if no file name given, nothing to do */
4864 return;
4865 if (*sfname == NULL) /* if no short file name given, use ffname */
4866 *sfname = *ffname;
4867 *ffname = fix_fname(*ffname); /* expand to full path */
4868
4869#ifdef FEAT_SHORTCUT
4870 if (!buf->b_p_bin)
4871 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004872 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 /* If the file name is a shortcut file, use the file it links to. */
4875 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004876 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 {
4878 vim_free(*ffname);
4879 *ffname = rfname;
4880 *sfname = rfname;
4881 }
4882 }
4883#endif
4884}
4885
4886/*
4887 * Get the file name for an argument list entry.
4888 */
4889 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004890alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
4892 buf_T *bp;
4893
4894 /* Use the name from the associated buffer if it exists. */
4895 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004896 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 return aep->ae_fname;
4898 return bp->b_fname;
4899}
4900
4901#if defined(FEAT_WINDOWS) || defined(PROTO)
4902/*
4903 * do_arg_all(): Open up to 'count' windows, one for each argument.
4904 */
4905 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004906do_arg_all(
4907 int count,
4908 int forceit, /* hide buffers in current windows */
4909 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
4911 int i;
4912 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004913 char_u *opened; /* Array of weight for which args are open:
4914 * 0: not opened
4915 * 1: opened in other tab
4916 * 2: opened in curtab
4917 * 3: opened in curtab and curwin
4918 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004919 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 int use_firstwin = FALSE; /* use first window for arglist */
4921 int split_ret = OK;
4922 int p_ea_save;
4923 alist_T *alist; /* argument list to be used */
4924 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004925 tabpage_T *tpnext;
4926 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004927 win_T *old_curwin, *last_curwin;
4928 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004929 win_T *new_curwin = NULL;
4930 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931
4932 if (ARGCOUNT <= 0)
4933 {
4934 /* Don't give an error message. We don't want it when the ":all"
4935 * command is in the .vimrc. */
4936 return;
4937 }
4938 setpcmark();
4939
4940 opened_len = ARGCOUNT;
4941 opened = alloc_clear((unsigned)opened_len);
4942 if (opened == NULL)
4943 return;
4944
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004945 /* Autocommands may do anything to the argument list. Make sure it's not
4946 * freed while we are working here by "locking" it. We still have to
4947 * watch out for its size to be changed. */
4948 alist = curwin->w_alist;
4949 ++alist->al_refcount;
4950
4951 old_curwin = curwin;
4952 old_curtab = curtab;
4953
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004954# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
4958 /*
4959 * Try closing all windows that are not in the argument list.
4960 * Also close windows that are not full width;
4961 * When 'hidden' or "forceit" set the buffer becomes hidden.
4962 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004963 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004965 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004966 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004967 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004969 tpnext = curtab->tp_next;
4970 for (wp = firstwin; wp != NULL; wp = wpnext)
4971 {
4972 wpnext = wp->w_next;
4973 buf = wp->w_buffer;
4974 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004975 || (!keep_tabs && (buf->b_nwindows > 1
4976 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004977 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004978 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004980 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004981 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004983 if (i < alist->al_ga.ga_len
4984 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4985 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4986 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004988 int weight = 1;
4989
4990 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004991 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004992 ++weight;
4993 if (old_curwin == wp)
4994 ++weight;
4995 }
4996
4997 if (weight > (int)opened[i])
4998 {
4999 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005000 if (i == 0)
5001 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005002 if (new_curwin != NULL)
5003 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005004 new_curwin = wp;
5005 new_curtab = curtab;
5006 }
5007 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005008 else if (keep_tabs)
5009 i = opened_len;
5010
5011 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005012 {
5013 /* Use the current argument list for all windows
5014 * containing a file from it. */
5015 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005016 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005017 ++wp->w_alist->al_refcount;
5018 }
5019 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005023 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005025 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005027 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005028 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005030 /* If the buffer was changed, and we would like to hide it,
5031 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005032 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005033 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005035#ifdef FEAT_AUTOCMD
5036 bufref_T bufref;
5037
5038 set_bufref(&bufref, buf);
5039#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005040 (void)autowrite(buf, FALSE);
5041#ifdef FEAT_AUTOCMD
5042 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005043 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005044 {
5045 wpnext = firstwin; /* start all over... */
5046 continue;
5047 }
5048#endif
5049 }
5050#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005051 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005052 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005053 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005054#endif
5055 use_firstwin = TRUE;
5056#ifdef FEAT_WINDOWS
5057 else
5058 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005059 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005060# ifdef FEAT_AUTOCMD
5061 /* check if autocommands removed the next window */
5062 if (!win_valid(wpnext))
5063 wpnext = firstwin; /* start all over... */
5064# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066#endif
5067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005070
5071 /* Without the ":tab" modifier only do the current tab page. */
5072 if (had_tab == 0 || tpnext == NULL)
5073 break;
5074
5075# ifdef FEAT_AUTOCMD
5076 /* check if autocommands removed the next tab page */
5077 if (!valid_tabpage(tpnext))
5078 tpnext = first_tabpage; /* start all over...*/
5079# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005080 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082
5083 /*
5084 * Open a window for files in the argument list that don't have one.
5085 * ARGCOUNT may change while doing this, because of autocommands.
5086 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005087 if (count > opened_len || count <= 0)
5088 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089
5090#ifdef FEAT_AUTOCMD
5091 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5092 ++autocmd_no_enter;
5093 ++autocmd_no_leave;
5094#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005095 last_curwin = curwin;
5096 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005098#ifdef FEAT_WINDOWS
5099 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5100 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005101 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005102 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5103 use_firstwin = TRUE;
5104#endif
5105
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005106 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5109 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005110 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 /* Move the already present window to below the current window */
5113 if (curwin->w_arg_idx != i)
5114 {
5115 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5116 {
5117 if (wpnext->w_arg_idx == i)
5118 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005119 if (keep_tabs)
5120 {
5121 new_curwin = wpnext;
5122 new_curtab = curtab;
5123 }
5124 else
5125 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 break;
5127 }
5128 }
5129 }
5130 }
5131 else if (split_ret == OK)
5132 {
5133 if (!use_firstwin) /* split current window */
5134 {
5135 p_ea_save = p_ea;
5136 p_ea = TRUE; /* use space from all windows */
5137 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5138 p_ea = p_ea_save;
5139 if (split_ret == FAIL)
5140 continue;
5141 }
5142#ifdef FEAT_AUTOCMD
5143 else /* first window: do autocmd for leaving this buffer */
5144 --autocmd_no_leave;
5145#endif
5146
5147 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005148 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 */
5150 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005151 if (i == 0)
5152 {
5153 new_curwin = curwin;
5154 new_curtab = curtab;
5155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5157 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005158 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005160 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161#ifdef FEAT_AUTOCMD
5162 if (use_firstwin)
5163 ++autocmd_no_leave;
5164#endif
5165 use_firstwin = FALSE;
5166 }
5167 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005168
5169 /* When ":tab" was used open a new tab for a new window repeatedly. */
5170 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5171 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173
5174 /* Remove the "lock" on the argument list. */
5175 alist_unlink(alist);
5176
5177#ifdef FEAT_AUTOCMD
5178 --autocmd_no_enter;
5179#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005180 /* restore last referenced tabpage's curwin */
5181 if (last_curtab != new_curtab)
5182 {
5183 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005184 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005185 if (win_valid(last_curwin))
5186 win_enter(last_curwin, FALSE);
5187 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005188 /* to window with first arg */
5189 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005190 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005191 if (win_valid(new_curwin))
5192 win_enter(new_curwin, FALSE);
5193
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194#ifdef FEAT_AUTOCMD
5195 --autocmd_no_leave;
5196#endif
5197 vim_free(opened);
5198}
5199
5200# if defined(FEAT_LISTCMDS) || defined(PROTO)
5201/*
5202 * Open a window for a number of buffers.
5203 */
5204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005205ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
5207 buf_T *buf;
5208 win_T *wp, *wpnext;
5209 int split_ret = OK;
5210 int p_ea_save;
5211 int open_wins = 0;
5212 int r;
5213 int count; /* Maximum number of windows to open. */
5214 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005215#ifdef FEAT_WINDOWS
5216 int had_tab = cmdmod.tab;
5217 tabpage_T *tpnext;
5218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
5220 if (eap->addr_count == 0) /* make as many windows as possible */
5221 count = 9999;
5222 else
5223 count = eap->line2; /* make as many windows as specified */
5224 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5225 all = FALSE;
5226 else
5227 all = TRUE;
5228
5229 setpcmark();
5230
5231#ifdef FEAT_GUI
5232 need_mouse_correct = TRUE;
5233#endif
5234
5235 /*
5236 * Close superfluous windows (two windows for the same buffer).
5237 * Also close windows that are not full-width.
5238 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005239#ifdef FEAT_WINDOWS
5240 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005241 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005242 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005245 tpnext = curtab->tp_next;
5246 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005248 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005249 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005250#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005251 || ((cmdmod.split & WSP_VERT)
5252 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005253 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005254 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005255 || (had_tab > 0 && wp != firstwin)
5256#endif
Bram Moolenaar459ca562016-11-10 18:16:33 +01005257 ) && !ONE_WINDOW
Bram Moolenaar362ce482012-06-06 19:02:45 +02005258#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005259 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005260#endif
5261 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005262 {
5263 win_close(wp, FALSE);
5264#ifdef FEAT_AUTOCMD
5265 wpnext = firstwin; /* just in case an autocommand does
5266 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005267 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005268 open_wins = 0;
5269#endif
5270 }
5271 else
5272 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005274
5275#ifdef FEAT_WINDOWS
5276 /* Without the ":tab" modifier only do the current tab page. */
5277 if (had_tab == 0 || tpnext == NULL)
5278 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005279 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 /*
5284 * Go through the buffer list. When a buffer doesn't have a window yet,
5285 * open one. Otherwise move the window to the right position.
5286 * Watch out for autocommands that delete buffers or windows!
5287 */
5288#ifdef FEAT_AUTOCMD
5289 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5290 ++autocmd_no_enter;
5291#endif
5292 win_enter(lastwin, FALSE);
5293#ifdef FEAT_AUTOCMD
5294 ++autocmd_no_leave;
5295#endif
5296 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5297 {
5298 /* Check if this buffer needs a window */
5299 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5300 continue;
5301
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005302#ifdef FEAT_WINDOWS
5303 if (had_tab != 0)
5304 {
5305 /* With the ":tab" modifier don't move the window. */
5306 if (buf->b_nwindows > 0)
5307 wp = lastwin; /* buffer has a window, skip it */
5308 else
5309 wp = NULL;
5310 }
5311 else
5312#endif
5313 {
5314 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005315 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005316 if (wp->w_buffer == buf)
5317 break;
5318 /* If the buffer already has a window, move it */
5319 if (wp != NULL)
5320 win_move_after(wp, curwin);
5321 }
5322
5323 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005325#ifdef FEAT_AUTOCMD
5326 bufref_T bufref;
5327
5328 set_bufref(&bufref, buf);
5329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 /* Split the window and put the buffer in it */
5331 p_ea_save = p_ea;
5332 p_ea = TRUE; /* use space from all windows */
5333 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5334 ++open_wins;
5335 p_ea = p_ea_save;
5336 if (split_ret == FAIL)
5337 continue;
5338
5339 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005340#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 swap_exists_action = SEA_DIALOG;
5342#endif
5343 set_curbuf(buf, DOBUF_GOTO);
5344#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005345 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005347 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005348#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 break;
5352 }
5353#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005354#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (swap_exists_action == SEA_QUIT)
5356 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005357# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5358 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005359
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005360 /* Reset the error/interrupt/exception state here so that
5361 * aborting() returns FALSE when closing a window. */
5362 enter_cleanup(&cs);
5363# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005364
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005365 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 win_close(curwin, TRUE);
5367 --open_wins;
5368 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005369 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005370
5371# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5372 /* Restore the error/interrupt/exception state if not
5373 * discarded by a new aborting error, interrupt, or uncaught
5374 * exception. */
5375 leave_cleanup(&cs);
5376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378 else
5379 handle_swap_exists(NULL);
5380#endif
5381 }
5382
5383 ui_breakcheck();
5384 if (got_int)
5385 {
5386 (void)vgetc(); /* only break the file loading, not the rest */
5387 break;
5388 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005389#ifdef FEAT_EVAL
5390 /* Autocommands deleted the buffer or aborted script processing!!! */
5391 if (aborting())
5392 break;
5393#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005394#ifdef FEAT_WINDOWS
5395 /* When ":tab" was used open a new tab for a new window repeatedly. */
5396 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5397 cmdmod.tab = 9999;
5398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400#ifdef FEAT_AUTOCMD
5401 --autocmd_no_enter;
5402#endif
5403 win_enter(firstwin, FALSE); /* back to first window */
5404#ifdef FEAT_AUTOCMD
5405 --autocmd_no_leave;
5406#endif
5407
5408 /*
5409 * Close superfluous windows.
5410 */
5411 for (wp = lastwin; open_wins > count; )
5412 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005413 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 || autowrite(wp->w_buffer, FALSE) == OK);
5415#ifdef FEAT_AUTOCMD
5416 if (!win_valid(wp))
5417 {
5418 /* BufWrite Autocommands made the window invalid, start over */
5419 wp = lastwin;
5420 }
5421 else
5422#endif
5423 if (r)
5424 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005425 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 --open_wins;
5427 wp = lastwin;
5428 }
5429 else
5430 {
5431 wp = wp->w_prev;
5432 if (wp == NULL)
5433 break;
5434 }
5435 }
5436}
5437# endif /* FEAT_LISTCMDS */
5438
5439#endif /* FEAT_WINDOWS */
5440
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005441static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005442
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443/*
5444 * do_modelines() - process mode lines for the current file
5445 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005446 * "flags" can be:
5447 * OPT_WINONLY only set options local to window
5448 * OPT_NOWIN don't set options local to window
5449 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 * Returns immediately if the "ml" option isn't set.
5451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005453do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005455 linenr_T lnum;
5456 int nmlines;
5457 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
5459 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5460 return;
5461
5462 /* Disallow recursive entry here. Can happen when executing a modeline
5463 * triggers an autocommand, which reloads modelines with a ":do". */
5464 if (entered)
5465 return;
5466
5467 ++entered;
5468 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5469 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005470 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 nmlines = 0;
5472
5473 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5474 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005475 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 nmlines = 0;
5477 --entered;
5478}
5479
5480#include "version.h" /* for version number */
5481
5482/*
5483 * chk_modeline() - check a single line for a mode string
5484 * Return FAIL if an error encountered.
5485 */
5486 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005487chk_modeline(
5488 linenr_T lnum,
5489 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
5491 char_u *s;
5492 char_u *e;
5493 char_u *linecopy; /* local copy of any modeline found */
5494 int prev;
5495 int vers;
5496 int end;
5497 int retval = OK;
5498 char_u *save_sourcing_name;
5499 linenr_T save_sourcing_lnum;
5500#ifdef FEAT_EVAL
5501 scid_T save_SID;
5502#endif
5503
5504 prev = -1;
5505 for (s = ml_get(lnum); *s != NUL; ++s)
5506 {
5507 if (prev == -1 || vim_isspace(prev))
5508 {
5509 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5510 || STRNCMP(s, "vi:", (size_t)3) == 0)
5511 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005512 /* Accept both "vim" and "Vim". */
5513 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 {
5515 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5516 e = s + 4;
5517 else
5518 e = s + 3;
5519 vers = getdigits(&e);
5520 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005521 && (s[0] != 'V'
5522 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 && (s[3] == ':'
5524 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5525 || (VIM_VERSION_100 < vers && s[3] == '<')
5526 || (VIM_VERSION_100 > vers && s[3] == '>')
5527 || (VIM_VERSION_100 == vers && s[3] == '=')))
5528 break;
5529 }
5530 }
5531 prev = *s;
5532 }
5533
5534 if (*s)
5535 {
5536 do /* skip over "ex:", "vi:" or "vim:" */
5537 ++s;
5538 while (s[-1] != ':');
5539
5540 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5541 if (linecopy == NULL)
5542 return FAIL;
5543
5544 save_sourcing_lnum = sourcing_lnum;
5545 save_sourcing_name = sourcing_name;
5546 sourcing_lnum = lnum; /* prepare for emsg() */
5547 sourcing_name = (char_u *)"modelines";
5548
5549 end = FALSE;
5550 while (end == FALSE)
5551 {
5552 s = skipwhite(s);
5553 if (*s == NUL)
5554 break;
5555
5556 /*
5557 * Find end of set command: ':' or end of line.
5558 * Skip over "\:", replacing it with ":".
5559 */
5560 for (e = s; *e != ':' && *e != NUL; ++e)
5561 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005562 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 if (*e == NUL)
5564 end = TRUE;
5565
5566 /*
5567 * If there is a "set" command, require a terminating ':' and
5568 * ignore the stuff after the ':'.
5569 * "vi:set opt opt opt: foo" -- foo not interpreted
5570 * "vi:opt opt opt: foo" -- foo interpreted
5571 * Accept "se" for compatibility with Elvis.
5572 */
5573 if (STRNCMP(s, "set ", (size_t)4) == 0
5574 || STRNCMP(s, "se ", (size_t)3) == 0)
5575 {
5576 if (*e != ':') /* no terminating ':'? */
5577 break;
5578 end = TRUE;
5579 s = vim_strchr(s, ' ') + 1;
5580 }
5581 *e = NUL; /* truncate the set command */
5582
5583 if (*s != NUL) /* skip over an empty "::" */
5584 {
5585#ifdef FEAT_EVAL
5586 save_SID = current_SID;
5587 current_SID = SID_MODELINE;
5588#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005589 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590#ifdef FEAT_EVAL
5591 current_SID = save_SID;
5592#endif
5593 if (retval == FAIL) /* stop if error found */
5594 break;
5595 }
5596 s = e + 1; /* advance to next part */
5597 }
5598
5599 sourcing_lnum = save_sourcing_lnum;
5600 sourcing_name = save_sourcing_name;
5601
5602 vim_free(linecopy);
5603 }
5604 return retval;
5605}
5606
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005607#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005609read_viminfo_bufferlist(
5610 vir_T *virp,
5611 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 char_u *tab;
5614 linenr_T lnum;
5615 colnr_T col;
5616 buf_T *buf;
5617 char_u *sfname;
5618 char_u *xline;
5619
5620 /* Handle long line and escaped characters. */
5621 xline = viminfo_readstring(virp, 1, FALSE);
5622
5623 /* don't read in if there are files on the command-line or if writing: */
5624 if (xline != NULL && !writing && ARGCOUNT == 0
5625 && find_viminfo_parameter('%') != NULL)
5626 {
5627 /* Format is: <fname> Tab <lnum> Tab <col>.
5628 * Watch out for a Tab in the file name, work from the end. */
5629 lnum = 0;
5630 col = 0;
5631 tab = vim_strrchr(xline, '\t');
5632 if (tab != NULL)
5633 {
5634 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005635 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 tab = vim_strrchr(xline, '\t');
5637 if (tab != NULL)
5638 {
5639 *tab++ = '\0';
5640 lnum = atol((char *)tab);
5641 }
5642 }
5643
5644 /* Expand "~/" in the file name at "line + 1" to a full path.
5645 * Then try shortening it by comparing with the current directory */
5646 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005647 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5650 if (buf != NULL) /* just in case... */
5651 {
5652 buf->b_last_cursor.lnum = lnum;
5653 buf->b_last_cursor.col = col;
5654 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5655 }
5656 }
5657 vim_free(xline);
5658
5659 return viminfo_readline(virp);
5660}
5661
5662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005663write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664{
5665 buf_T *buf;
5666#ifdef FEAT_WINDOWS
5667 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005668 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669#endif
5670 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005671 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672
5673 if (find_viminfo_parameter('%') == NULL)
5674 return;
5675
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005676 /* Without a number -1 is returned: do all buffers. */
5677 max_buffers = get_viminfo_parameter('%');
5678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005680#define LINE_BUF_LEN (MAXPATHL + 40)
5681 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (line == NULL)
5683 return;
5684
5685#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005686 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 set_last_cursor(win);
5688#else
5689 set_last_cursor(curwin);
5690#endif
5691
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005692 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005693 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
5695 if (buf->b_fname == NULL
5696 || !buf->b_p_bl
5697#ifdef FEAT_QUICKFIX
5698 || bt_quickfix(buf)
5699#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005700#ifdef FEAT_TERMINAL
5701 || bt_terminal(buf)
5702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 || removable(buf->b_ffname))
5704 continue;
5705
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005706 if (max_buffers-- == 0)
5707 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 putc('%', fp);
5709 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005710 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 (long)buf->b_last_cursor.lnum,
5712 buf->b_last_cursor.col);
5713 viminfo_writestring(fp, line);
5714 }
5715 vim_free(line);
5716}
5717#endif
5718
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005719/*
5720 * Return TRUE if "buf" is the quickfix buffer.
5721 */
5722 int
5723bt_quickfix(buf_T *buf)
5724{
5725 return buf != NULL && buf->b_p_bt[0] == 'q';
5726}
5727
5728/*
5729 * Return TRUE if "buf" is a terminal buffer.
5730 */
5731 int
5732bt_terminal(buf_T *buf)
5733{
5734 return buf != NULL && buf->b_p_bt[0] == 't';
5735}
5736
5737/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005738 * Return TRUE if "buf" is a help buffer.
5739 */
5740 int
5741bt_help(buf_T *buf)
5742{
5743 return buf != NULL && buf->b_help;
5744}
5745
5746/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005747 * Return TRUE if "buf" is a "nofile", "acwrite" or "terminal" buffer.
5748 * This means the buffer name is not a file name.
5749 */
5750 int
5751bt_nofile(buf_T *buf)
5752{
5753 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5754 || buf->b_p_bt[0] == 'a'
5755 || buf->b_p_bt[0] == 't');
5756}
5757
5758/*
5759 * Return TRUE if "buf" is a "nowrite", "nofile" or "terminal" buffer.
5760 */
5761 int
5762bt_dontwrite(buf_T *buf)
5763{
5764 return buf != NULL && (buf->b_p_bt[0] == 'n' || buf->b_p_bt[0] == 't');
5765}
5766
5767 int
5768bt_dontwrite_msg(buf_T *buf)
5769{
5770 if (bt_dontwrite(buf))
5771 {
5772 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5773 return TRUE;
5774 }
5775 return FALSE;
5776}
5777
5778/*
5779 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5780 * and 'bufhidden'.
5781 */
5782 int
5783buf_hide(buf_T *buf)
5784{
5785 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5786 switch (buf->b_p_bh[0])
5787 {
5788 case 'u': /* "unload" */
5789 case 'w': /* "wipe" */
5790 case 'd': return FALSE; /* "delete" */
5791 case 'h': return TRUE; /* "hide" */
5792 }
5793 return (p_hid || cmdmod.hide);
5794}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
5796/*
5797 * Return special buffer name.
5798 * Returns NULL when the buffer has a normal file name.
5799 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005800 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005801buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
5803#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5804 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005805 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005806 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005807 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005808
5809 /*
5810 * For location list window, w_llist_ref points to the location list.
5811 * For quickfix window, w_llist_ref is NULL.
5812 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005813 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005814 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005815 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005816 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005821 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 if (bt_nofile(buf))
5823 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005824#ifdef FEAT_TERMINAL
5825 if (buf->b_term != NULL)
5826 return term_get_status_text(buf->b_term);
5827#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005828 if (buf->b_fname != NULL)
5829 return buf->b_fname;
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005830 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005834 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 return NULL;
5836}
5837
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005838#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005839 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5840 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005841# define SWITCH_TO_WIN
5842
5843/*
5844 * Find a window that contains "buf" and switch to it.
5845 * If there is no such window, use the current window and change "curbuf".
5846 * Caller must initialize save_curbuf to NULL.
5847 * restore_win_for_buf() MUST be called later!
5848 */
5849 void
5850switch_to_win_for_buf(
5851 buf_T *buf,
5852 win_T **save_curwinp,
5853 tabpage_T **save_curtabp,
5854 bufref_T *save_curbuf)
5855{
5856 win_T *wp;
5857 tabpage_T *tp;
5858
5859 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5860 switch_buffer(save_curbuf, buf);
5861 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5862 {
5863 restore_win(*save_curwinp, *save_curtabp, TRUE);
5864 switch_buffer(save_curbuf, buf);
5865 }
5866}
5867
5868 void
5869restore_win_for_buf(
5870 win_T *save_curwin,
5871 tabpage_T *save_curtab,
5872 bufref_T *save_curbuf)
5873{
5874 if (save_curbuf->br_buf == NULL)
5875 restore_win(save_curwin, save_curtab, TRUE);
5876 else
5877 restore_buffer(save_curbuf);
5878}
5879#endif
5880
5881#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5882 || defined(SWITCH_TO_WIN) \
5883 || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005884/*
5885 * Find a window for buffer "buf".
5886 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5887 * If not found FAIL is returned.
5888 */
5889 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005890find_win_for_buf(
5891 buf_T *buf,
5892 win_T **wp,
5893 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005894{
5895 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5896 if ((*wp)->w_buffer == buf)
5897 goto win_found;
5898 return FAIL;
5899win_found:
5900 return OK;
5901}
5902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
5904#if defined(FEAT_SIGNS) || defined(PROTO)
5905/*
5906 * Insert the sign into the signlist.
5907 */
5908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005909insert_sign(
5910 buf_T *buf, /* buffer to store sign in */
5911 signlist_T *prev, /* previous sign entry */
5912 signlist_T *next, /* next sign entry */
5913 int id, /* sign ID */
5914 linenr_T lnum, /* line number which gets the mark */
5915 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
5917 signlist_T *newsign;
5918
5919 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5920 if (newsign != NULL)
5921 {
5922 newsign->id = id;
5923 newsign->lnum = lnum;
5924 newsign->typenr = typenr;
5925 newsign->next = next;
5926#ifdef FEAT_NETBEANS_INTG
5927 newsign->prev = prev;
5928 if (next != NULL)
5929 next->prev = newsign;
5930#endif
5931
5932 if (prev == NULL)
5933 {
5934 /* When adding first sign need to redraw the windows to create the
5935 * column for signs. */
5936 if (buf->b_signlist == NULL)
5937 {
5938 redraw_buf_later(buf, NOT_VALID);
5939 changed_cline_bef_curs();
5940 }
5941
5942 /* first sign in signlist */
5943 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005944#ifdef FEAT_NETBEANS_INTG
5945 if (netbeans_active())
5946 buf->b_has_sign_column = TRUE;
5947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 }
5949 else
5950 prev->next = newsign;
5951 }
5952}
5953
5954/*
5955 * Add the sign into the signlist. Find the right spot to do it though.
5956 */
5957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005958buf_addsign(
5959 buf_T *buf, /* buffer to store sign in */
5960 int id, /* sign ID */
5961 linenr_T lnum, /* line number which gets the mark */
5962 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
5964 signlist_T *sign; /* a sign in the signlist */
5965 signlist_T *prev; /* the previous sign */
5966
5967 prev = NULL;
5968 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5969 {
5970 if (lnum == sign->lnum && id == sign->id)
5971 {
5972 sign->typenr = typenr;
5973 return;
5974 }
5975 else if (
5976#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5977 id < 0 &&
5978#endif
5979 lnum < sign->lnum)
5980 {
5981#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5982 /* XXX - GRP: Is this because of sign slide problem? Or is it
5983 * really needed? Or is it because we allow multiple signs per
5984 * line? If so, should I add that feature to FEAT_SIGNS?
5985 */
5986 while (prev != NULL && prev->lnum == lnum)
5987 prev = prev->prev;
5988 if (prev == NULL)
5989 sign = buf->b_signlist;
5990 else
5991 sign = prev->next;
5992#endif
5993 insert_sign(buf, prev, sign, id, lnum, typenr);
5994 return;
5995 }
5996 prev = sign;
5997 }
5998#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5999 /* XXX - GRP: See previous comment */
6000 while (prev != NULL && prev->lnum == lnum)
6001 prev = prev->prev;
6002 if (prev == NULL)
6003 sign = buf->b_signlist;
6004 else
6005 sign = prev->next;
6006#endif
6007 insert_sign(buf, prev, sign, id, lnum, typenr);
6008
6009 return;
6010}
6011
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006012/*
6013 * For an existing, placed sign "markId" change the type to "typenr".
6014 * Returns the line number of the sign, or zero if the sign is not found.
6015 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006016 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006017buf_change_sign_type(
6018 buf_T *buf, /* buffer to store sign in */
6019 int markId, /* sign ID */
6020 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
6022 signlist_T *sign; /* a sign in the signlist */
6023
6024 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6025 {
6026 if (sign->id == markId)
6027 {
6028 sign->typenr = typenr;
6029 return sign->lnum;
6030 }
6031 }
6032
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006033 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034}
6035
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006037buf_getsigntype(
6038 buf_T *buf,
6039 linenr_T lnum,
6040 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 signlist_T *sign; /* a sign in a b_signlist */
6043
6044 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6045 if (sign->lnum == lnum
6046 && (type == SIGN_ANY
6047# ifdef FEAT_SIGN_ICONS
6048 || (type == SIGN_ICON
6049 && sign_get_image(sign->typenr) != NULL)
6050# endif
6051 || (type == SIGN_TEXT
6052 && sign_get_text(sign->typenr) != NULL)
6053 || (type == SIGN_LINEHL
6054 && sign_get_attr(sign->typenr, TRUE) != 0)))
6055 return sign->typenr;
6056 return 0;
6057}
6058
6059
6060 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061buf_delsign(
6062 buf_T *buf, /* buffer sign is stored in */
6063 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 signlist_T **lastp; /* pointer to pointer to current sign */
6066 signlist_T *sign; /* a sign in a b_signlist */
6067 signlist_T *next; /* the next sign in a b_signlist */
6068 linenr_T lnum; /* line number whose sign was deleted */
6069
6070 lastp = &buf->b_signlist;
6071 lnum = 0;
6072 for (sign = buf->b_signlist; sign != NULL; sign = next)
6073 {
6074 next = sign->next;
6075 if (sign->id == id)
6076 {
6077 *lastp = next;
6078#ifdef FEAT_NETBEANS_INTG
6079 if (next != NULL)
6080 next->prev = sign->prev;
6081#endif
6082 lnum = sign->lnum;
6083 vim_free(sign);
6084 break;
6085 }
6086 else
6087 lastp = &sign->next;
6088 }
6089
6090 /* When deleted the last sign need to redraw the windows to remove the
6091 * sign column. */
6092 if (buf->b_signlist == NULL)
6093 {
6094 redraw_buf_later(buf, NOT_VALID);
6095 changed_cline_bef_curs();
6096 }
6097
6098 return lnum;
6099}
6100
6101
6102/*
6103 * Find the line number of the sign with the requested id. If the sign does
6104 * not exist, return 0 as the line number. This will still let the correct file
6105 * get loaded.
6106 */
6107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006108buf_findsign(
6109 buf_T *buf, /* buffer to store sign in */
6110 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
6112 signlist_T *sign; /* a sign in the signlist */
6113
6114 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6115 if (sign->id == id)
6116 return sign->lnum;
6117
6118 return 0;
6119}
6120
6121 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006122buf_findsign_id(
6123 buf_T *buf, /* buffer whose sign we are searching for */
6124 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125{
6126 signlist_T *sign; /* a sign in the signlist */
6127
6128 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6129 if (sign->lnum == lnum)
6130 return sign->id;
6131
6132 return 0;
6133}
6134
6135
6136# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
6137/* see if a given type of sign exists on a specific line */
6138 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006139buf_findsigntype_id(
6140 buf_T *buf, /* buffer whose sign we are searching for */
6141 linenr_T lnum, /* line number of sign */
6142 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
6144 signlist_T *sign; /* a sign in the signlist */
6145
6146 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6147 if (sign->lnum == lnum && sign->typenr == typenr)
6148 return sign->id;
6149
6150 return 0;
6151}
6152
6153
6154# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6155/* return the number of icons on the given line */
6156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 signlist_T *sign; /* a sign in the signlist */
6160 int count = 0;
6161
6162 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6163 if (sign->lnum == lnum)
6164 if (sign_get_image(sign->typenr) != NULL)
6165 count++;
6166
6167 return count;
6168}
6169# endif /* FEAT_SIGN_ICONS */
6170# endif /* FEAT_NETBEANS_INTG */
6171
6172
6173/*
6174 * Delete signs in buffer "buf".
6175 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006177buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 signlist_T *next;
6180
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006181 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006182 * sign column. Not when curwin is NULL (this means we're exiting). */
6183 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006184 {
6185 redraw_buf_later(buf, NOT_VALID);
6186 changed_cline_bef_curs();
6187 }
6188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 while (buf->b_signlist != NULL)
6190 {
6191 next = buf->b_signlist->next;
6192 vim_free(buf->b_signlist);
6193 buf->b_signlist = next;
6194 }
6195}
6196
6197/*
6198 * Delete all signs in all buffers.
6199 */
6200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006201buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 buf_T *buf; /* buffer we are checking for signs */
6204
Bram Moolenaar29323592016-07-24 22:04:11 +02006205 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208}
6209
6210/*
6211 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6212 */
6213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
6216 buf_T *buf;
6217 signlist_T *p;
6218 char lbuf[BUFSIZ];
6219
6220 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6221 msg_putchar('\n');
6222 if (rbuf == NULL)
6223 buf = firstbuf;
6224 else
6225 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006226 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {
6228 if (buf->b_signlist != NULL)
6229 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006230 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006231 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 msg_putchar('\n');
6233 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006234 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006236 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6238 MSG_PUTS(lbuf);
6239 msg_putchar('\n');
6240 }
6241 if (rbuf != NULL)
6242 break;
6243 buf = buf->b_next;
6244 }
6245}
6246
6247/*
6248 * Adjust a placed sign for inserted/deleted lines.
6249 */
6250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006251sign_mark_adjust(
6252 linenr_T line1,
6253 linenr_T line2,
6254 long amount,
6255 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 signlist_T *sign; /* a sign in a b_signlist */
6258
6259 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6260 {
6261 if (sign->lnum >= line1 && sign->lnum <= line2)
6262 {
6263 if (amount == MAXLNUM)
6264 sign->lnum = line1;
6265 else
6266 sign->lnum += amount;
6267 }
6268 else if (sign->lnum > line2)
6269 sign->lnum += amount_after;
6270 }
6271}
6272#endif /* FEAT_SIGNS */
6273
6274/*
6275 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6276 */
6277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006278set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 if (on != curbuf->b_p_bl)
6281 {
6282 curbuf->b_p_bl = on;
6283#ifdef FEAT_AUTOCMD
6284 if (on)
6285 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6286 else
6287 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6288#endif
6289 }
6290}
6291
6292/*
6293 * Read the file for "buf" again and check if the contents changed.
6294 * Return TRUE if it changed or this could not be checked.
6295 */
6296 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006297buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
6299 buf_T *newbuf;
6300 int differ = TRUE;
6301 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 exarg_T ea;
6304
6305 /* Allocate a buffer without putting it in the buffer list. */
6306 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6307 if (newbuf == NULL)
6308 return TRUE;
6309
6310 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6311 if (prep_exarg(&ea, buf) == FAIL)
6312 {
6313 wipe_buffer(newbuf, FALSE);
6314 return TRUE;
6315 }
6316
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 /* set curwin/curbuf to buf and save a few things */
6318 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
Bram Moolenaar4770d092006-01-12 23:22:24 +00006320 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 && readfile(buf->b_ffname, buf->b_fname,
6322 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6323 &ea, READ_NEW | READ_DUMMY) == OK)
6324 {
6325 /* compare the two files line by line */
6326 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6327 {
6328 differ = FALSE;
6329 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6330 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6331 {
6332 differ = TRUE;
6333 break;
6334 }
6335 }
6336 }
6337 vim_free(ea.cmd);
6338
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 /* restore curwin/curbuf and a few other things */
6340 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341
6342 if (curbuf != newbuf) /* safety check */
6343 wipe_buffer(newbuf, FALSE);
6344
6345 return differ;
6346}
6347
6348/*
6349 * Wipe out a buffer and decrement the last buffer number if it was used for
6350 * this buffer. Call this to wipe out a temp buffer that does not contain any
6351 * marks.
6352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006354wipe_buffer(
6355 buf_T *buf,
6356 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 if (buf->b_fnum == top_file_num - 1)
6359 --top_file_num;
6360
6361#ifdef FEAT_AUTOCMD
6362 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006363 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006365 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366#ifdef FEAT_AUTOCMD
6367 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006368 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#endif
6370}