blob: 22effbb81f57fb38030d7bc6077a72826ed88e08 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
36static wininfo_T *find_wininfo(buf_T *buf, int skip_diff_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020038static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
40static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
48static void free_buffer(buf_T *);
49static void free_buffer_stuff(buf_T *buf, int free_options);
50static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar4033c552017-09-16 20:54:51 +020062#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066#ifdef FEAT_AUTOCMD
67static char *e_auabort = N_("E855: Autocommands caused command to abort");
68#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020070/* Number of times free_buffer() was called. */
71static int buf_free_count = 0;
72
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020073/* Read data from buffer for retrying. */
74 static int
75read_buffer(
76 int read_stdin, /* read file from stdin, otherwise fifo */
77 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
78 int flags) /* extra flags for readfile() */
79{
80 int retval = OK;
81 linenr_T line_count;
82
83 /*
84 * Read from the buffer which the text is already filled in and append at
85 * the end. This makes it possible to retry when 'fileformat' or
86 * 'fileencoding' was guessed wrong.
87 */
88 line_count = curbuf->b_ml.ml_line_count;
89 retval = readfile(
90 read_stdin ? NULL : curbuf->b_ffname,
91 read_stdin ? NULL : curbuf->b_fname,
92 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
93 flags | READ_BUFFER);
94 if (retval == OK)
95 {
96 /* Delete the binary lines. */
97 while (--line_count >= 0)
98 ml_delete((linenr_T)1, FALSE);
99 }
100 else
101 {
102 /* Delete the converted lines. */
103 while (curbuf->b_ml.ml_line_count > line_count)
104 ml_delete(line_count, FALSE);
105 }
106 /* Put the cursor on the first line. */
107 curwin->w_cursor.lnum = 1;
108 curwin->w_cursor.col = 0;
109
110 if (read_stdin)
111 {
112 /* Set or reset 'modified' before executing autocommands, so that
113 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100114 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 else if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117 unchanged(curbuf, FALSE);
118
119#ifdef FEAT_AUTOCMD
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100120 if (retval == OK)
121 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200122# ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100123 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200124 curbuf, &retval);
125# else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100126 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200127# endif
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100128 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200129#endif
130 }
131 return retval;
132}
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200135 * Open current buffer, that is: open the memfile and read the file into
136 * memory.
137 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 */
139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100140open_buffer(
141 int read_stdin, /* read file from stdin */
142 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
143 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145 int retval = OK;
146#ifdef FEAT_AUTOCMD
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200147 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100149#ifdef FEAT_SYN_HL
150 long old_tw = curbuf->b_p_tw;
151#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200152 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154 /*
155 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
156 * When re-entering the same buffer, it should not change, because the
157 * user may have reset the flag by hand.
158 */
159 if (readonlymode && curbuf->b_ffname != NULL
160 && (curbuf->b_flags & BF_NEVERLOADED))
161 curbuf->b_p_ro = TRUE;
162
Bram Moolenaar4770d092006-01-12 23:22:24 +0000163 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 {
165 /*
166 * There MUST be a memfile, otherwise we can't do anything
167 * If we can't create one for the current buffer, take another buffer
168 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100169 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200170 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (curbuf->b_ml.ml_mfp != NULL)
172 break;
173 /*
174 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000175 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 */
177 if (curbuf == NULL)
178 {
179 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
180 getout(2);
181 }
182 EMSG(_("E83: Cannot allocate buffer, using other one..."));
183 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100184#ifdef FEAT_SYN_HL
185 if (old_tw != curbuf->b_p_tw)
186 check_colorcolumn(curwin);
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 return FAIL;
189 }
190
191#ifdef FEAT_AUTOCMD
192 /* The autocommands in readfile() may change the buffer, but only AFTER
193 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200194 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 modified_was_set = FALSE;
196#endif
197
198 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100199 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
201 if (curbuf->b_ffname != NULL
202#ifdef FEAT_NETBEANS_INTG
203 && netbeansReadFile
204#endif
205 )
206 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100207 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200208#ifdef UNIX
209 int save_bin = curbuf->b_p_bin;
210 int perm;
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifdef FEAT_NETBEANS_INTG
213 int oldFire = netbeansFireChanges;
214
215 netbeansFireChanges = 0;
216#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200217#ifdef UNIX
218 perm = mch_getperm(curbuf->b_ffname);
219 if (perm >= 0 && (0
220# ifdef S_ISFIFO
221 || S_ISFIFO(perm)
222# endif
223# ifdef S_ISSOCK
224 || S_ISSOCK(perm)
225# endif
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200226# ifdef OPEN_CHR_FILES
227 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
228# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200229 ))
230 read_fifo = TRUE;
231 if (read_fifo)
232 curbuf->b_p_bin = TRUE;
233#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100234 if (shortmess(SHM_FILEINFO))
235 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200237 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200238 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
239#ifdef UNIX
240 if (read_fifo)
241 {
242 curbuf->b_p_bin = save_bin;
243 if (retval == OK)
244 retval = read_buffer(FALSE, eap, flags);
245 }
246#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100247 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_NETBEANS_INTG
249 netbeansFireChanges = oldFire;
250#endif
251 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200252 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 fix_help_buffer();
254 }
255 else if (read_stdin)
256 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200257 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259 /*
260 * First read the text in binary mode into the buffer.
261 * Then read from that same buffer and append at the end. This makes
262 * it possible to retry when 'fileformat' or 'fileencoding' was
263 * guessed wrong.
264 */
265 curbuf->b_p_bin = TRUE;
266 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200267 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
268 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 curbuf->b_p_bin = save_bin;
270 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200271 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273
274 /* if first time loading this buffer, init b_chartab[] */
275 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100278#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100279 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100280#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
283 /*
284 * Set/reset the Changed flag first, autocmds may change the buffer.
285 * Apply the automatic commands, before processing the modelines.
286 * So the modelines have priority over auto commands.
287 */
288 /* When reading stdin, the buffer contents always needs writing, so set
289 * the changed flag. Unless in readonly mode: "ls | gview -".
290 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000291 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#ifdef FEAT_AUTOCMD
293 || modified_was_set /* ":set modified" used in autocmd */
294# ifdef FEAT_EVAL
295 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
296# endif
297#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000298 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100300 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 unchanged(curbuf, FALSE);
302 save_file_ff(curbuf); /* keep this fileformat */
303
304 /* require "!" to overwrite the file, because it wasn't read completely */
305#ifdef FEAT_EVAL
306 if (aborting())
307#else
308 if (got_int)
309#endif
310 curbuf->b_flags |= BF_READERR;
311
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000312#ifdef FEAT_FOLDING
313 /* Need to update automatic folding. Do this before the autocommands,
314 * they may use the fold info. */
315 foldUpdateAll(curwin);
316#endif
317
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#ifdef FEAT_AUTOCMD
319 /* need to set w_topline, unless some autocommand already did that. */
320 if (!(curwin->w_valid & VALID_TOPLINE))
321 {
322 curwin->w_topline = 1;
323# ifdef FEAT_DIFF
324 curwin->w_topfill = 0;
325# endif
326 }
327# ifdef FEAT_EVAL
328 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
329# else
330 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
331# endif
332#endif
333
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100334 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
336#ifdef FEAT_AUTOCMD
337 /*
338 * The autocommands may have changed the current buffer. Apply the
339 * modelines to the correct buffer, if it still exists and is loaded.
340 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200341 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
343 aco_save_T aco;
344
345 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200346 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000348 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
350
351#ifdef FEAT_AUTOCMD
352# ifdef FEAT_EVAL
353 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
354 &retval);
355# else
356 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
357# endif
358
359 /* restore curwin/curbuf and a few other things */
360 aucmd_restbuf(&aco);
361 }
362#endif
363 }
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 return retval;
366}
367
368/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200369 * Store "buf" in "bufref" and set the free count.
370 */
371 void
372set_bufref(bufref_T *bufref, buf_T *buf)
373{
374 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200375 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200376 bufref->br_buf_free_count = buf_free_count;
377}
378
379/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200380 * Return TRUE if "bufref->br_buf" points to the same buffer as when
381 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200382 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200383 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
384 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200385 */
386 int
387bufref_valid(bufref_T *bufref)
388{
389 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200390 ? TRUE : buf_valid(bufref->br_buf)
391 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200392}
393
394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200396 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100399buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 buf_T *bp;
402
Bram Moolenaar82404332016-07-10 17:00:38 +0200403 /* Assume that we more often have a recent buffer, start with the last
404 * one. */
405 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (bp == buf)
407 return TRUE;
408 return FALSE;
409}
410
411/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200412 * A hash table used to quickly lookup a buffer by its number.
413 */
414static hashtab_T buf_hashtab;
415
416 static void
417buf_hashtab_add(buf_T *buf)
418{
419 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
420 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
421 EMSG(_("E931: Buffer cannot be registered"));
422}
423
424 static void
425buf_hashtab_remove(buf_T *buf)
426{
427 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
428
429 if (!HASHITEM_EMPTY(hi))
430 hash_remove(&buf_hashtab, hi);
431}
432
433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 * Close the link to a buffer.
435 * "action" is used when there is no longer a window for the buffer.
436 * It can be:
437 * 0 buffer becomes hidden
438 * DOBUF_UNLOAD buffer is unloaded
439 * DOBUF_DELETE buffer is unloaded and removed from buffer list
440 * DOBUF_WIPE buffer is unloaded and really deleted
441 * When doing all but the first one on the current buffer, the caller should
442 * get a new buffer very soon!
443 *
444 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100445 *
446 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
447 * cause there to be only one window with this buffer. e.g. when ":quit" is
448 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 */
450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100451close_buffer(
452 win_T *win, /* if not NULL, set b_last_cursor */
453 buf_T *buf,
454 int action,
455 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_AUTOCMD
458 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100459 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200460 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100461 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200462 win_T *the_curwin = curwin;
463 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465 int unload_buf = (action != 0);
466 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
467 int wipe_buf = (action == DOBUF_WIPE);
468
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200469 /*
470 * Force unloading or deleting when 'bufhidden' says so.
471 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
472 * "hide" (otherwise we could never free or delete a buffer).
473 */
474 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
475 {
476 del_buf = TRUE;
477 unload_buf = TRUE;
478 }
479 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
480 {
481 del_buf = TRUE;
482 unload_buf = TRUE;
483 wipe_buf = TRUE;
484 }
485 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
486 unload_buf = TRUE;
487
Bram Moolenaar94053a52017-08-01 21:44:33 +0200488#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200489 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200490 {
491 if (term_job_running(buf->b_term))
492 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200493 if (wipe_buf || unload_buf)
494 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200495 free_terminal(buf);
496 else
497 {
498 /* The job keeps running, hide the buffer. */
499 del_buf = FALSE;
500 unload_buf = FALSE;
501 }
502 }
503 else
504 {
505 /* A terminal buffer is wiped out if the job has finished. */
506 del_buf = TRUE;
507 unload_buf = TRUE;
508 wipe_buf = TRUE;
509 }
510 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
Bram Moolenaar30180b82016-09-04 19:57:56 +0200513#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200514 /* Disallow deleting the buffer when it is locked (already being closed or
515 * halfway a command that relies on it). Unloading is allowed. */
516 if (buf->b_locked > 0 && (del_buf || wipe_buf))
517 {
518 EMSG(_("E937: Attempt to delete a buffer that is in use"));
519 return;
520 }
Bram Moolenaar30180b82016-09-04 19:57:56 +0200521#endif
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200522
Bram Moolenaar4033c552017-09-16 20:54:51 +0200523 /* check no autocommands closed the window */
524 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
526 /* Set b_last_cursor when closing the last window for the buffer.
527 * Remember the last cursor position and window options of the buffer.
528 * This used to be only for the current window, but then options like
529 * 'foldmethod' may be lost with a ":only" command. */
530 if (buf->b_nwindows == 1)
531 set_last_cursor(win);
532 buflist_setfpos(buf, win,
533 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
534 win->w_cursor.col, TRUE);
535 }
536
537#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200538 set_bufref(&bufref, buf);
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 /* When the buffer is no longer in a window, trigger BufWinLeave */
541 if (buf->b_nwindows == 1)
542 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200543 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200544 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
545 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200546 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100547 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200548 /* Autocommands deleted the buffer. */
549aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100550 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100552 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200553 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200554 if (abort_if_last && one_window())
555 /* Autocommands made this the only window. */
556 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
558 /* When the buffer becomes hidden, but is not unloaded, trigger
559 * BufHidden */
560 if (!unload_buf)
561 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200562 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200563 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
564 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200565 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200566 /* Autocommands deleted the buffer. */
567 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200568 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200569 if (abort_if_last && one_window())
570 /* Autocommands made this the only window. */
571 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
573# ifdef FEAT_EVAL
574 if (aborting()) /* autocmds may abort script processing */
575 return;
576# endif
577 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200578
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200579 /* If the buffer was in curwin and the window has changed, go back to that
580 * window, if it still exists. This avoids that ":edit x" triggering a
581 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
582 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
583 {
584 block_autocmds();
585 goto_tabpage_win(the_curtab, the_curwin);
586 unblock_autocmds();
587 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 nwindows = buf->b_nwindows;
590#endif
591
592 /* decrease the link count from windows (unless not in any window) */
593 if (buf->b_nwindows > 0)
594 --buf->b_nwindows;
595
596 /* Return when a window is displaying the buffer or when it's not
597 * unloaded. */
598 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 /* Always remove the buffer when there is no file name. */
602 if (buf->b_ffname == NULL)
603 del_buf = TRUE;
604
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200605 /* When closing the current buffer stop Visual mode before freeing
606 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200607 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200608#if defined(EXITFREE)
609 && !entered_free_all_mem
610#endif
611 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200612 end_visual_mode();
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 /*
615 * Free all things allocated for this buffer.
616 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
617 */
618#ifdef FEAT_AUTOCMD
619 /* Remember if we are closing the current buffer. Restore the number of
620 * windows, so that autocommands in buf_freeall() don't get confused. */
621 is_curbuf = (buf == curbuf);
622 buf->b_nwindows = nwindows;
623#endif
624
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200625 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627#ifdef FEAT_AUTOCMD
628 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200629 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 return;
631# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000632 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 return;
634# endif
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 /*
637 * It's possible that autocommands change curbuf to the one being deleted.
638 * This might cause the previous curbuf to be deleted unexpectedly. But
639 * in some cases it's OK to delete the curbuf, because a new one is
640 * obtained anyway. Therefore only return if curbuf changed to the
641 * deleted buffer.
642 */
643 if (buf == curbuf && !is_curbuf)
644 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200645
Bram Moolenaar4033c552017-09-16 20:54:51 +0200646 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200647 win->w_buffer = NULL; /* make sure we don't use the buffer now */
648
649 /* Autocommands may have opened or closed windows for this buffer.
650 * Decrement the count for the close we do here. */
651 if (buf->b_nwindows > 0)
652 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653#endif
654
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000655 /* Change directories when the 'acd' option is set. */
656 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
658 /*
659 * Remove the buffer from the list.
660 */
661 if (wipe_buf)
662 {
663#ifdef FEAT_SUN_WORKSHOP
664 if (usingSunWorkShop)
665 workshop_file_closed_lineno((char *)buf->b_ffname,
666 (int)buf->b_last_cursor.lnum);
667#endif
668 vim_free(buf->b_ffname);
669 vim_free(buf->b_sfname);
670 if (buf->b_prev == NULL)
671 firstbuf = buf->b_next;
672 else
673 buf->b_prev->b_next = buf->b_next;
674 if (buf->b_next == NULL)
675 lastbuf = buf->b_prev;
676 else
677 buf->b_next->b_prev = buf->b_prev;
678 free_buffer(buf);
679 }
680 else
681 {
682 if (del_buf)
683 {
684 /* Free all internal variables and reset option values, to make
685 * ":bdel" compatible with Vim 5.7. */
686 free_buffer_stuff(buf, TRUE);
687
688 /* Make it look like a new buffer. */
689 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
690
691 /* Init the options when loaded again. */
692 buf->b_p_initialized = FALSE;
693 }
694 buf_clear_file(buf);
695 if (del_buf)
696 buf->b_p_bl = FALSE;
697 }
698}
699
700/*
701 * Make buffer not contain a file.
702 */
703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100704buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
706 buf->b_ml.ml_line_count = 1;
707 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 buf->b_p_eol = TRUE;
710 buf->b_start_eol = TRUE;
711#ifdef FEAT_MBYTE
712 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000713 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#endif
715 buf->b_ml.ml_mfp = NULL;
716 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
717#ifdef FEAT_NETBEANS_INTG
718 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#endif
720}
721
722/*
723 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200724 * the file. Careful: get here with "curwin" NULL when exiting.
725 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200726 * BFA_DEL buffer is going to be deleted
727 * BFA_WIPE buffer is going to be wiped out
728 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100731buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733#ifdef FEAT_AUTOCMD
734 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200735 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200736 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200737 win_T *the_curwin = curwin;
738 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar5a497892016-09-03 16:29:04 +0200740 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200741 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200742 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200743 if (buf->b_ml.ml_mfp != NULL)
744 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200745 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
746 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200747 && !bufref_valid(&bufref))
748 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200749 return;
750 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200751 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200753 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
754 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200755 && !bufref_valid(&bufref))
756 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 return;
758 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200759 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200761 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
762 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200763 && !bufref_valid(&bufref))
764 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 return;
766 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200767 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200768
Bram Moolenaar5a497892016-09-03 16:29:04 +0200769 /* If the buffer was in curwin and the window has changed, go back to that
770 * window, if it still exists. This avoids that ":edit x" triggering a
771 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
772 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
773 {
774 block_autocmds();
775 goto_tabpage_win(the_curtab, the_curwin);
776 unblock_autocmds();
777 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000780 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 return;
782# endif
783
784 /*
785 * It's possible that autocommands change curbuf to the one being deleted.
786 * This might cause curbuf to be deleted unexpectedly. But in some cases
787 * it's OK to delete the curbuf, because a new one is obtained anyway.
788 * Therefore only return if curbuf changed to the deleted buffer.
789 */
790 if (buf == curbuf && !is_curbuf)
791 return;
792#endif
793#ifdef FEAT_DIFF
794 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
795#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200796#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100797 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200798 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100799 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200800#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000801
802#ifdef FEAT_FOLDING
803 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000804 {
805 win_T *win;
806 tabpage_T *tp;
807
808 FOR_ALL_TAB_WINDOWS(tp, win)
809 if (win->w_buffer == buf)
810 clearFolding(win);
811 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000812#endif
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#ifdef FEAT_TCL
815 tcl_buffer_free(buf);
816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 ml_close(buf, TRUE); /* close and delete the memline/memfile */
818 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200819 if ((flags & BFA_KEEP_UNDO) == 0)
820 {
821 u_blockfree(buf); /* free the memory allocated for undo */
822 u_clearall(buf); /* reset all undo information */
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200825 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000827 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828}
829
830/*
831 * Free a buffer structure and the things it contains related to the buffer
832 * itself (not the file, that must have been done already).
833 */
834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200837 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200839#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200840 /* b:changedtick uses an item in buf_T, remove it now */
841 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200842 unref_var_dict(buf->b_vars);
843#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200844#ifdef FEAT_LUA
845 lua_buffer_free(buf);
846#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000847#ifdef FEAT_MZSCHEME
848 mzscheme_buffer_free(buf);
849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#ifdef FEAT_PERL
851 perl_buf_free(buf);
852#endif
853#ifdef FEAT_PYTHON
854 python_buffer_free(buf);
855#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200856#ifdef FEAT_PYTHON3
857 python3_buffer_free(buf);
858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859#ifdef FEAT_RUBY
860 ruby_buffer_free(buf);
861#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200862#ifdef FEAT_JOB_CHANNEL
863 channel_buffer_free(buf);
864#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200865#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200866 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200867#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200868
869 buf_hashtab_remove(buf);
870
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200871#ifdef FEAT_AUTOCMD
872 aubuflocal_remove(buf);
873
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200874 if (autocmd_busy)
875 {
876 /* Do not free the buffer structure while autocommands are executing,
877 * it's still needed. Free it when autocmd_busy is reset. */
878 buf->b_next = au_pending_free_buf;
879 au_pending_free_buf = buf;
880 }
881 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000882#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200883 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884}
885
886/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100887 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100888 */
889 static void
890init_changedtick(buf_T *buf)
891{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100892 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100893
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100894 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
895 di->di_tv.v_type = VAR_NUMBER;
896 di->di_tv.v_lock = VAR_FIXED;
897 di->di_tv.vval.v_number = 0;
898
Bram Moolenaar92769c32017-02-25 15:41:37 +0100899#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100900 STRCPY(buf->b_ct_di.di_key, "changedtick");
901 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100902#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100903}
904
905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
907 */
908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100909free_buffer_stuff(
910 buf_T *buf,
911 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
913 if (free_options)
914 {
915 clear_wininfo(buf); /* including window-local options */
916 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200917#ifdef FEAT_SPELL
918 ga_clear(&buf->b_s.b_langp);
919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100922 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100923 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100924
925 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
926 hash_init(&buf->b_vars->dv_hashtab);
927 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100928 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#endif
931#ifdef FEAT_USR_CMDS
932 uc_clear(&buf->b_ucmds); /* clear local user commands */
933#endif
934#ifdef FEAT_SIGNS
935 buf_delete_signs(buf); /* delete any signs */
936#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000937#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200938 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#ifdef FEAT_LOCALMAP
941 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
942 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
943#endif
944#ifdef FEAT_MBYTE
945 vim_free(buf->b_start_fenc);
946 buf->b_start_fenc = NULL;
947#endif
948}
949
950/*
951 * Free the b_wininfo list for buffer "buf".
952 */
953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100954clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
956 wininfo_T *wip;
957
958 while (buf->b_wininfo != NULL)
959 {
960 wip = buf->b_wininfo;
961 buf->b_wininfo = wip->wi_next;
962 if (wip->wi_optset)
963 {
964 clear_winopt(&wip->wi_opt);
965#ifdef FEAT_FOLDING
966 deleteFoldRecurse(&wip->wi_folds);
967#endif
968 }
969 vim_free(wip);
970 }
971}
972
973#if defined(FEAT_LISTCMDS) || defined(PROTO)
974/*
975 * Go to another buffer. Handles the result of the ATTENTION dialog.
976 */
977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100978goto_buffer(
979 exarg_T *eap,
980 int start,
981 int dir,
982 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
Bram Moolenaar4033c552017-09-16 20:54:51 +0200984# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200985 bufref_T old_curbuf;
986
987 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
989 swap_exists_action = SEA_DIALOG;
990# endif
991 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
992 start, dir, count, eap->forceit);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200993# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
995 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000996# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
997 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000998
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000999 /* Reset the error/interrupt/exception state here so that
1000 * aborting() returns FALSE when closing a window. */
1001 enter_cleanup(&cs);
1002# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001003
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001004 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 win_close(curwin, TRUE);
1006 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001007 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001008
1009# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1010 /* Restore the error/interrupt/exception state if not discarded by a
1011 * new aborting error, interrupt, or uncaught exception. */
1012 leave_cleanup(&cs);
1013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001016 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017# endif
1018}
1019#endif
1020
Bram Moolenaare64ac772005-12-07 20:54:59 +00001021#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022/*
1023 * Handle the situation of swap_exists_action being set.
1024 * It is allowed for "old_curbuf" to be NULL or invalid.
1025 */
1026 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001027handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001029# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1030 cleanup_T cs;
1031# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001032#ifdef FEAT_SYN_HL
1033 long old_tw = curbuf->b_p_tw;
1034#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001035 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (swap_exists_action == SEA_QUIT)
1038 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001039# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1040 /* Reset the error/interrupt/exception state here so that
1041 * aborting() returns FALSE when closing a buffer. */
1042 enter_cleanup(&cs);
1043# endif
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 /* User selected Quit at ATTENTION prompt. Go back to previous
1046 * buffer. If that buffer is gone or the same as the current one,
1047 * open a new, empty buffer. */
1048 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001049 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001050 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001051 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1052 || old_curbuf->br_buf == curbuf)
1053 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1054 else
1055 buf = old_curbuf->br_buf;
1056 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001057 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001058 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001059#ifdef FEAT_SYN_HL
1060 if (old_tw != curbuf->b_p_tw)
1061 check_colorcolumn(curwin);
1062#endif
1063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001065
1066# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1067 /* Restore the error/interrupt/exception state if not discarded by a
1068 * new aborting error, interrupt, or uncaught exception. */
1069 leave_cleanup(&cs);
1070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
1072 else if (swap_exists_action == SEA_RECOVER)
1073 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001074# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1075 /* Reset the error/interrupt/exception state here so that
1076 * aborting() returns FALSE when closing a buffer. */
1077 enter_cleanup(&cs);
1078# endif
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 /* User selected Recover at ATTENTION prompt. */
1081 msg_scroll = TRUE;
1082 ml_recover();
1083 MSG_PUTS("\n"); /* don't overwrite the last message */
1084 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001085 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001086
1087# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1088 /* Restore the error/interrupt/exception state if not discarded by a
1089 * new aborting error, interrupt, or uncaught exception. */
1090 leave_cleanup(&cs);
1091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
1093 swap_exists_action = SEA_NONE;
1094}
1095#endif
1096
1097#if defined(FEAT_LISTCMDS) || defined(PROTO)
1098/*
1099 * do_bufdel() - delete or unload buffer(s)
1100 *
1101 * addr_count == 0: ":bdel" - delete current buffer
1102 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1103 * buffer "end_bnr", then any other arguments.
1104 * addr_count == 2: ":N,N bdel" - delete buffers in range
1105 *
1106 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1107 * DOBUF_DEL (":bdel")
1108 *
1109 * Returns error message or NULL
1110 */
1111 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001112do_bufdel(
1113 int command,
1114 char_u *arg, /* pointer to extra arguments */
1115 int addr_count,
1116 int start_bnr, /* first buffer number in a range */
1117 int end_bnr, /* buffer nr or last buffer nr in a range */
1118 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
1120 int do_current = 0; /* delete current buffer? */
1121 int deleted = 0; /* number of buffers deleted */
1122 char_u *errormsg = NULL; /* return value */
1123 int bnr; /* buffer number */
1124 char_u *p;
1125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (addr_count == 0)
1127 {
1128 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1129 }
1130 else
1131 {
1132 if (addr_count == 2)
1133 {
1134 if (*arg) /* both range and argument is not allowed */
1135 return (char_u *)_(e_trailing);
1136 bnr = start_bnr;
1137 }
1138 else /* addr_count == 1 */
1139 bnr = end_bnr;
1140
1141 for ( ;!got_int; ui_breakcheck())
1142 {
1143 /*
1144 * delete the current buffer last, otherwise when the
1145 * current buffer is deleted, the next buffer becomes
1146 * the current one and will be loaded, which may then
1147 * also be deleted, etc.
1148 */
1149 if (bnr == curbuf->b_fnum)
1150 do_current = bnr;
1151 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1152 forceit) == OK)
1153 ++deleted;
1154
1155 /*
1156 * find next buffer number to delete/unload
1157 */
1158 if (addr_count == 2)
1159 {
1160 if (++bnr > end_bnr)
1161 break;
1162 }
1163 else /* addr_count == 1 */
1164 {
1165 arg = skipwhite(arg);
1166 if (*arg == NUL)
1167 break;
1168 if (!VIM_ISDIGIT(*arg))
1169 {
1170 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001171 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1172 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (bnr < 0) /* failed */
1174 break;
1175 arg = p;
1176 }
1177 else
1178 bnr = getdigits(&arg);
1179 }
1180 }
1181 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1182 FORWARD, do_current, forceit) == OK)
1183 ++deleted;
1184
1185 if (deleted == 0)
1186 {
1187 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001188 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001190 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001192 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 errormsg = IObuff;
1194 }
1195 else if (deleted >= p_report)
1196 {
1197 if (command == DOBUF_UNLOAD)
1198 {
1199 if (deleted == 1)
1200 MSG(_("1 buffer unloaded"));
1201 else
1202 smsg((char_u *)_("%d buffers unloaded"), deleted);
1203 }
1204 else if (command == DOBUF_DEL)
1205 {
1206 if (deleted == 1)
1207 MSG(_("1 buffer deleted"));
1208 else
1209 smsg((char_u *)_("%d buffers deleted"), deleted);
1210 }
1211 else
1212 {
1213 if (deleted == 1)
1214 MSG(_("1 buffer wiped out"));
1215 else
1216 smsg((char_u *)_("%d buffers wiped out"), deleted);
1217 }
1218 }
1219 }
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
1222 return errormsg;
1223}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001224#endif /* FEAT_LISTCMDS */
1225
1226#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1227 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001229static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001230
1231/*
1232 * Make the current buffer empty.
1233 * Used when it is wiped out and it's the last buffer.
1234 */
1235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001236empty_curbuf(
1237 int close_others,
1238 int forceit,
1239 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001240{
1241 int retval;
1242 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001243 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001244
1245 if (action == DOBUF_UNLOAD)
1246 {
1247 EMSG(_("E90: Cannot unload last buffer"));
1248 return FAIL;
1249 }
1250
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001251 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001252 if (close_others)
1253 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001254 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001255
1256 setpcmark();
1257 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1258 forceit ? ECMD_FORCEIT : 0, curwin);
1259
1260 /*
1261 * do_ecmd() may create a new buffer, then we have to delete
1262 * the old one. But do_ecmd() may have done that already, check
1263 * if the buffer still exists.
1264 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001265 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001266 close_buffer(NULL, buf, action, FALSE);
1267 if (!close_others)
1268 need_fileinfo = FALSE;
1269 return retval;
1270}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271/*
1272 * Implementation of the commands for the buffer list.
1273 *
1274 * action == DOBUF_GOTO go to specified buffer
1275 * action == DOBUF_SPLIT split window and go to specified buffer
1276 * action == DOBUF_UNLOAD unload specified buffer(s)
1277 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1278 * action == DOBUF_WIPE delete specified buffer(s) really
1279 *
1280 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1281 * start == DOBUF_FIRST go to "count" buffer from first buffer
1282 * start == DOBUF_LAST go to "count" buffer from last buffer
1283 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1284 *
1285 * Return FAIL or OK.
1286 */
1287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001288do_buffer(
1289 int action,
1290 int start,
1291 int dir, /* FORWARD or BACKWARD */
1292 int count, /* buffer number or number of buffers */
1293 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 buf_T *buf;
1296 buf_T *bp;
1297 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1298 || action == DOBUF_WIPE);
1299
1300 switch (start)
1301 {
1302 case DOBUF_FIRST: buf = firstbuf; break;
1303 case DOBUF_LAST: buf = lastbuf; break;
1304 default: buf = curbuf; break;
1305 }
1306 if (start == DOBUF_MOD) /* find next modified buffer */
1307 {
1308 while (count-- > 0)
1309 {
1310 do
1311 {
1312 buf = buf->b_next;
1313 if (buf == NULL)
1314 buf = firstbuf;
1315 }
1316 while (buf != curbuf && !bufIsChanged(buf));
1317 }
1318 if (!bufIsChanged(buf))
1319 {
1320 EMSG(_("E84: No modified buffer found"));
1321 return FAIL;
1322 }
1323 }
1324 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1325 {
1326 while (buf != NULL && buf->b_fnum != count)
1327 buf = buf->b_next;
1328 }
1329 else
1330 {
1331 bp = NULL;
1332 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1333 {
1334 /* remember the buffer where we start, we come back there when all
1335 * buffers are unlisted. */
1336 if (bp == NULL)
1337 bp = buf;
1338 if (dir == FORWARD)
1339 {
1340 buf = buf->b_next;
1341 if (buf == NULL)
1342 buf = firstbuf;
1343 }
1344 else
1345 {
1346 buf = buf->b_prev;
1347 if (buf == NULL)
1348 buf = lastbuf;
1349 }
1350 /* don't count unlisted buffers */
1351 if (unload || buf->b_p_bl)
1352 {
1353 --count;
1354 bp = NULL; /* use this buffer as new starting point */
1355 }
1356 if (bp == buf)
1357 {
1358 /* back where we started, didn't find anything. */
1359 EMSG(_("E85: There is no listed buffer"));
1360 return FAIL;
1361 }
1362 }
1363 }
1364
1365 if (buf == NULL) /* could not find it */
1366 {
1367 if (start == DOBUF_FIRST)
1368 {
1369 /* don't warn when deleting */
1370 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001371 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373 else if (dir == FORWARD)
1374 EMSG(_("E87: Cannot go beyond last buffer"));
1375 else
1376 EMSG(_("E88: Cannot go before first buffer"));
1377 return FAIL;
1378 }
1379
1380#ifdef FEAT_GUI
1381 need_mouse_correct = TRUE;
1382#endif
1383
1384#ifdef FEAT_LISTCMDS
1385 /*
1386 * delete buffer buf from memory and/or the list
1387 */
1388 if (unload)
1389 {
1390 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001391 bufref_T bufref;
1392
1393 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 /* When unloading or deleting a buffer that's already unloaded and
1396 * unlisted: fail silently. */
1397 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1398 return FAIL;
1399
1400 if (!forceit && bufIsChanged(buf))
1401 {
1402#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1403 if ((p_confirm || cmdmod.confirm) && p_write)
1404 {
1405 dialog_changed(buf, FALSE);
1406# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001407 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /* Autocommand deleted buffer, oops! It's not changed
1409 * now. */
1410 return FAIL;
1411# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001412 /* If it's still changed fail silently, the dialog already
1413 * mentioned why it fails. */
1414 if (bufIsChanged(buf))
1415 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418#endif
1419 {
1420 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1421 buf->b_fnum);
1422 return FAIL;
1423 }
1424 }
1425
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001426 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001427 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001428 end_visual_mode();
1429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 /*
1431 * If deleting the last (listed) buffer, make it empty.
1432 * The last (listed) buffer cannot be unloaded.
1433 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001434 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (bp->b_p_bl && bp != buf)
1436 break;
1437 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001438 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /*
1441 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001442 * (unless it's the only window). Repeat this so long as we end up in
1443 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001445 while (buf == curbuf
Bram Moolenaar4033c552017-09-16 20:54:51 +02001446#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001447 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar4033c552017-09-16 20:54:51 +02001448#endif
Bram Moolenaar459ca562016-11-10 18:16:33 +01001449 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001450 {
1451 if (win_close(curwin, FALSE) == FAIL)
1452 break;
1453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
1455 /*
1456 * If the buffer to be deleted is not the current one, delete it here.
1457 */
1458 if (buf != curbuf)
1459 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001460 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001461 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001462 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 return OK;
1464 }
1465
1466 /*
1467 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001468 * There should be another, otherwise it would have been handled
1469 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001470 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 * Then prefer the buffer we most recently visited.
1472 * Else try to find one that is loaded, after the current buffer,
1473 * then before the current buffer.
1474 * Finally use any buffer.
1475 */
1476 buf = NULL; /* selected buffer */
1477 bp = NULL; /* used when no loaded buffer found */
1478#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001479 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1480 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481# ifdef FEAT_JUMPLIST
1482 else
1483# endif
1484#endif
1485#ifdef FEAT_JUMPLIST
1486 if (curwin->w_jumplistlen > 0)
1487 {
1488 int jumpidx;
1489
1490 jumpidx = curwin->w_jumplistidx - 1;
1491 if (jumpidx < 0)
1492 jumpidx = curwin->w_jumplistlen - 1;
1493
1494 forward = jumpidx;
1495 while (jumpidx != curwin->w_jumplistidx)
1496 {
1497 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1498 if (buf != NULL)
1499 {
1500 if (buf == curbuf || !buf->b_p_bl)
1501 buf = NULL; /* skip current and unlisted bufs */
1502 else if (buf->b_ml.ml_mfp == NULL)
1503 {
1504 /* skip unloaded buf, but may keep it for later */
1505 if (bp == NULL)
1506 bp = buf;
1507 buf = NULL;
1508 }
1509 }
1510 if (buf != NULL) /* found a valid buffer: stop searching */
1511 break;
1512 /* advance to older entry in jump list */
1513 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1514 break;
1515 if (--jumpidx < 0)
1516 jumpidx = curwin->w_jumplistlen - 1;
1517 if (jumpidx == forward) /* List exhausted for sure */
1518 break;
1519 }
1520 }
1521#endif
1522
1523 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1524 {
1525 forward = TRUE;
1526 buf = curbuf->b_next;
1527 for (;;)
1528 {
1529 if (buf == NULL)
1530 {
1531 if (!forward) /* tried both directions */
1532 break;
1533 buf = curbuf->b_prev;
1534 forward = FALSE;
1535 continue;
1536 }
1537 /* in non-help buffer, try to skip help buffers, and vv */
1538 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1539 {
1540 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1541 break;
1542 if (bp == NULL) /* remember unloaded buf for later */
1543 bp = buf;
1544 }
1545 if (forward)
1546 buf = buf->b_next;
1547 else
1548 buf = buf->b_prev;
1549 }
1550 }
1551 if (buf == NULL) /* No loaded buffer, use unloaded one */
1552 buf = bp;
1553 if (buf == NULL) /* No loaded buffer, find listed one */
1554 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001555 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (buf->b_p_bl && buf != curbuf)
1557 break;
1558 }
1559 if (buf == NULL) /* Still no buffer, just take one */
1560 {
1561 if (curbuf->b_next != NULL)
1562 buf = curbuf->b_next;
1563 else
1564 buf = curbuf->b_prev;
1565 }
1566 }
1567
Bram Moolenaara02471e2014-01-10 16:43:14 +01001568 if (buf == NULL)
1569 {
1570 /* Autocommands must have wiped out all other buffers. Only option
1571 * now is to make the current buffer empty. */
1572 return empty_curbuf(FALSE, forceit, action);
1573 }
1574
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 /*
1576 * make buf current buffer
1577 */
1578 if (action == DOBUF_SPLIT) /* split window first */
1579 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001580 /* If 'switchbuf' contains "useopen": jump to first window containing
1581 * "buf" if one exists */
1582 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001583 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001584 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001585 * page containing "buf" if one exists */
1586 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 return OK;
1588 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 return FAIL;
1590 }
1591#endif
1592
1593 /* go to current buffer - nothing to do */
1594 if (buf == curbuf)
1595 return OK;
1596
1597 /*
1598 * Check if the current buffer may be abandoned.
1599 */
1600 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1601 {
1602#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1603 if ((p_confirm || cmdmod.confirm) && p_write)
1604 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001605# ifdef FEAT_AUTOCMD
1606 bufref_T bufref;
1607
1608 set_bufref(&bufref, buf);
1609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 dialog_changed(curbuf, FALSE);
1611# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001612 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 /* Autocommand deleted buffer, oops! */
1614 return FAIL;
1615# endif
1616 }
1617 if (bufIsChanged(curbuf))
1618#endif
1619 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001620 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 return FAIL;
1622 }
1623 }
1624
1625 /* Go to the other buffer. */
1626 set_curbuf(buf, action);
1627
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001628#if defined(FEAT_LISTCMDS) \
1629 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001631 {
1632 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635
1636#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1637 if (aborting()) /* autocmds may abort script processing */
1638 return FAIL;
1639#endif
1640
1641 return OK;
1642}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645/*
1646 * Set current buffer to "buf". Executes autocommands and closes current
1647 * buffer. "action" tells how to close the current buffer:
1648 * DOBUF_GOTO free or hide it
1649 * DOBUF_SPLIT nothing
1650 * DOBUF_UNLOAD unload it
1651 * DOBUF_DEL delete it
1652 * DOBUF_WIPE wipe it out
1653 */
1654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001655set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 buf_T *prevbuf;
1658 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1659 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001660#ifdef FEAT_SYN_HL
1661 long old_tw = curbuf->b_p_tw;
1662#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001663 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001666 if (!cmdmod.keepalt)
1667 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001668 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 /* Don't restart Select mode after switching to another buffer. */
1671 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
1673 /* close_windows() or apply_autocmds() may change curbuf */
1674 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001675 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001678 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679# ifdef FEAT_EVAL
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001680 || (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681# else
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001682 || bufref_valid(&bufref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683# endif
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001684 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#endif
1686 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001687#ifdef FEAT_SYN_HL
1688 if (prevbuf == curwin->w_buffer)
1689 reset_synblock(curwin);
1690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001692 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001694 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001696 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001698 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001699 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001700 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001701 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1703 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001704 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001705 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001706 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001707 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001708 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001712 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001713 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001714 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1715 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001716# ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001717 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001718# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001719 ) || curwin->w_buffer == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001723#ifdef FEAT_SYN_HL
1724 if (old_tw != curbuf->b_p_tw)
1725 check_colorcolumn(curwin);
1726#endif
1727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728}
1729
1730/*
1731 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001732 * Old curbuf must have been abandoned already! This also means "curbuf" may
1733 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 */
1735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001736enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
1738 /* Copy buffer and window local option values. Not for a help buffer. */
1739 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1740 if (!buf->b_help)
1741 get_winopts(buf);
1742#ifdef FEAT_FOLDING
1743 else
1744 /* Remove all folds in the window. */
1745 clearFolding(curwin);
1746 foldUpdateAll(curwin); /* update folds (later). */
1747#endif
1748
1749 /* Get the buffer in the current window. */
1750 curwin->w_buffer = buf;
1751 curbuf = buf;
1752 ++curbuf->b_nwindows;
1753
1754#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001755 if (curwin->w_p_diff)
1756 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757#endif
1758
Bram Moolenaara971b822011-09-14 14:43:25 +02001759#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001760 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001761#endif
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 /* Cursor on first line by default. */
1764 curwin->w_cursor.lnum = 1;
1765 curwin->w_cursor.col = 0;
1766#ifdef FEAT_VIRTUALEDIT
1767 curwin->w_cursor.coladd = 0;
1768#endif
1769 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001770#ifdef FEAT_AUTOCMD
1771 curwin->w_topline_was_set = FALSE;
1772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001774 /* mark cursor position as being invalid */
1775 curwin->w_valid = 0;
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 /* Make sure the buffer is loaded. */
1778 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001779 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001780#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001781 /* If there is no filetype, allow for detecting one. Esp. useful for
1782 * ":ball" used in a autocommand. If there already is a filetype we
1783 * might prefer to keep it. */
1784 if (*curbuf->b_p_ft == NUL)
1785 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001786#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001787
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001788 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 else
1791 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001792 if (!msg_silent)
1793 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1795#ifdef FEAT_AUTOCMD
1796 curwin->w_topline = 1;
1797# ifdef FEAT_DIFF
1798 curwin->w_topfill = 0;
1799# endif
1800 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1801 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1802#endif
1803 }
1804
1805 /* If autocommands did not change the cursor position, restore cursor lnum
1806 * and possibly cursor col. */
1807 if (curwin->w_cursor.lnum == 1 && inindent(0))
1808 buflist_getfpos();
1809
1810 check_arg_idx(curwin); /* check for valid arg_idx */
1811#ifdef FEAT_TITLE
1812 maketitle();
1813#endif
1814#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001815 /* when autocmds didn't change it */
1816 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#endif
1818 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1819
Bram Moolenaar009b2592004-10-24 19:18:58 +00001820#ifdef FEAT_NETBEANS_INTG
1821 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001822 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001823#endif
1824
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001825 /* Change directories when the 'acd' option is set. */
1826 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828#ifdef FEAT_KEYMAP
1829 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001830 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001832#ifdef FEAT_SPELL
1833 /* May need to set the spell language. Can only do this after the buffer
1834 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001835 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1836 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001837#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001838#ifdef FEAT_VIMINFO
1839 curbuf->b_last_used = vim_time();
1840#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 redraw_later(NOT_VALID);
1843}
1844
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001845#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1846/*
1847 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001848 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001849 */
1850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001851do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001852{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001853 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001854 && curbuf->b_ffname != NULL
1855 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001856 shorten_fnames(TRUE);
1857}
1858#endif
1859
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001860 void
1861no_write_message(void)
1862{
1863#ifdef FEAT_TERMINAL
1864 if (term_job_running(curbuf->b_term))
1865 EMSG(_("E948: Job still running (add ! to end the job)"));
1866 else
1867#endif
1868 EMSG(_("E37: No write since last change (add ! to override)"));
1869}
1870
1871 void
1872no_write_message_nobang(void)
1873{
1874#ifdef FEAT_TERMINAL
1875 if (term_job_running(curbuf->b_term))
1876 EMSG(_("E948: Job still running"));
1877 else
1878#endif
1879 EMSG(_("E37: No write since last change"));
1880}
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882/*
1883 * functions for dealing with the buffer list
1884 */
1885
Bram Moolenaar480778b2016-07-14 22:09:39 +02001886static int top_file_num = 1; /* highest file number */
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888/*
1889 * Add a file name to the buffer list. Return a pointer to the buffer.
1890 * If the same file name already exists return a pointer to that buffer.
1891 * If it does not exist, or if fname == NULL, a new entry is created.
1892 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1893 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1894 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001895 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001896 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1897 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 * This is the ONLY way to create a new buffer.
1899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001901buflist_new(
1902 char_u *ffname, /* full path of fname or relative */
1903 char_u *sfname, /* short fname or NULL */
1904 linenr_T lnum, /* preferred cursor line */
1905 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 buf_T *buf;
1908#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001909 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910#endif
1911
Bram Moolenaar480778b2016-07-14 22:09:39 +02001912 if (top_file_num == 1)
1913 hash_init(&buf_hashtab);
1914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1916
1917 /*
1918 * If file name already exists in the list, update the entry.
1919 */
1920#ifdef UNIX
1921 /* On Unix we can use inode numbers when the file exists. Works better
1922 * for hard links. */
1923 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1924 st.st_dev = (dev_T)-1;
1925#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001926 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#ifdef UNIX
1928 buflist_findname_stat(ffname, &st)
1929#else
1930 buflist_findname(ffname)
1931#endif
1932 ) != NULL)
1933 {
1934 vim_free(ffname);
1935 if (lnum != 0)
1936 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001937
1938 if ((flags & BLN_NOOPT) == 0)
1939 /* copy the options now, if 'cpo' doesn't have 's' and not done
1940 * already */
1941 buf_copy_options(buf, 0);
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1944 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001945#ifdef FEAT_AUTOCMD
1946 bufref_T bufref;
1947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 buf->b_p_bl = TRUE;
1949#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001950 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001952 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001953 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001954 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001955 return NULL;
1956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957#endif
1958 }
1959 return buf;
1960 }
1961
1962 /*
1963 * If the current buffer has no name and no contents, use the current
1964 * buffer. Otherwise: Need to allocate a new buffer structure.
1965 *
1966 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001967 * (A spell file buffer is allocated in spell.c, but that's not a normal
1968 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 */
1970 buf = NULL;
1971 if ((flags & BLN_CURBUF)
1972 && curbuf != NULL
1973 && curbuf->b_ffname == NULL
1974 && curbuf->b_nwindows <= 1
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001975 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 buf = curbuf;
1978#ifdef FEAT_AUTOCMD
1979 /* It's like this buffer is deleted. Watch out for autocommands that
1980 * change curbuf! If that happens, allocate a new buffer anyway. */
1981 if (curbuf->b_p_bl)
1982 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1983 if (buf == curbuf)
1984 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1985# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001986 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 return NULL;
1988# endif
1989#endif
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001990#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (buf == curbuf)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 /* Make sure 'bufhidden' and 'buftype' are empty */
1995 clear_string_option(&buf->b_p_bh);
1996 clear_string_option(&buf->b_p_bt);
1997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 if (buf != curbuf || curbuf == NULL)
2000 {
2001 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
2002 if (buf == NULL)
2003 {
2004 vim_free(ffname);
2005 return NULL;
2006 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002007#ifdef FEAT_EVAL
2008 /* init b: variables */
2009 buf->b_vars = dict_alloc();
2010 if (buf->b_vars == NULL)
2011 {
2012 vim_free(ffname);
2013 vim_free(buf);
2014 return NULL;
2015 }
2016 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2017#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002018 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020
2021 if (ffname != NULL)
2022 {
2023 buf->b_ffname = ffname;
2024 buf->b_sfname = vim_strsave(sfname);
2025 }
2026
2027 clear_wininfo(buf);
2028 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2029
2030 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2031 || buf->b_wininfo == NULL)
2032 {
2033 vim_free(buf->b_ffname);
2034 buf->b_ffname = NULL;
2035 vim_free(buf->b_sfname);
2036 buf->b_sfname = NULL;
2037 if (buf != curbuf)
2038 free_buffer(buf);
2039 return NULL;
2040 }
2041
2042 if (buf == curbuf)
2043 {
2044 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002045 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if (buf != curbuf) /* autocommands deleted the buffer! */
2047 return NULL;
2048#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002049 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 return NULL;
2051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002053
2054 /* Init the options. */
2055 buf->b_p_initialized = FALSE;
2056 buf_copy_options(buf, BCO_ENTER);
2057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058#ifdef FEAT_KEYMAP
2059 /* need to reload lmaps and set b:keymap_name */
2060 curbuf->b_kmap_state |= KEYMAP_INIT;
2061#endif
2062 }
2063 else
2064 {
2065 /*
2066 * put new buffer at the end of the buffer list
2067 */
2068 buf->b_next = NULL;
2069 if (firstbuf == NULL) /* buffer list is empty */
2070 {
2071 buf->b_prev = NULL;
2072 firstbuf = buf;
2073 }
2074 else /* append new buffer at end of list */
2075 {
2076 lastbuf->b_next = buf;
2077 buf->b_prev = lastbuf;
2078 }
2079 lastbuf = buf;
2080
2081 buf->b_fnum = top_file_num++;
2082 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2083 {
2084 EMSG(_("W14: Warning: List of file names overflow"));
2085 if (emsg_silent == 0)
2086 {
2087 out_flush();
2088 ui_delay(3000L, TRUE); /* make sure it is noticed */
2089 }
2090 top_file_num = 1;
2091 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002092 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
2094 /*
2095 * Always copy the options from the current buffer.
2096 */
2097 buf_copy_options(buf, BCO_ALWAYS);
2098 }
2099
2100 buf->b_wininfo->wi_fpos.lnum = lnum;
2101 buf->b_wininfo->wi_win = curwin;
2102
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002103#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002104 hash_init(&buf->b_s.b_keywtab);
2105 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106#endif
2107
2108 buf->b_fname = buf->b_sfname;
2109#ifdef UNIX
2110 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002111 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 else
2113 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002114 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 buf->b_dev = st.st_dev;
2116 buf->b_ino = st.st_ino;
2117 }
2118#endif
2119 buf->b_u_synced = TRUE;
2120 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002121 if (flags & BLN_DUMMY)
2122 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 buf_clear_file(buf);
2124 clrallmarks(buf); /* clear marks */
2125 fmarks_check_names(buf); /* check file marks for this file */
2126 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2127#ifdef FEAT_AUTOCMD
2128 if (!(flags & BLN_DUMMY))
2129 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002130 bufref_T bufref;
2131
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002132 /* Tricky: these autocommands may change the buffer list. They could
2133 * also split the window with re-using the one empty buffer. This may
2134 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002135 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002136 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002137 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002138 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002140 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002141 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002142 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002143 return NULL;
2144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002146 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 return NULL;
2148# endif
2149 }
2150#endif
2151
2152 return buf;
2153}
2154
2155/*
2156 * Free the memory for the options of a buffer.
2157 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2158 * 'fileencoding'.
2159 */
2160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161free_buf_options(
2162 buf_T *buf,
2163 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 if (free_p_ff)
2166 {
2167#ifdef FEAT_MBYTE
2168 clear_string_option(&buf->b_p_fenc);
2169#endif
2170 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 clear_string_option(&buf->b_p_bh);
2172 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
2174#ifdef FEAT_FIND_ID
2175 clear_string_option(&buf->b_p_def);
2176 clear_string_option(&buf->b_p_inc);
2177# ifdef FEAT_EVAL
2178 clear_string_option(&buf->b_p_inex);
2179# endif
2180#endif
2181#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2182 clear_string_option(&buf->b_p_inde);
2183 clear_string_option(&buf->b_p_indk);
2184#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002185#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2186 clear_string_option(&buf->b_p_bexpr);
2187#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002188#if defined(FEAT_CRYPT)
2189 clear_string_option(&buf->b_p_cm);
2190#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002191 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002192#if defined(FEAT_EVAL)
2193 clear_string_option(&buf->b_p_fex);
2194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#ifdef FEAT_CRYPT
2196 clear_string_option(&buf->b_p_key);
2197#endif
2198 clear_string_option(&buf->b_p_kp);
2199 clear_string_option(&buf->b_p_mps);
2200 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002201 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 clear_string_option(&buf->b_p_isk);
2203#ifdef FEAT_KEYMAP
2204 clear_string_option(&buf->b_p_keymap);
2205 ga_clear(&buf->b_kmap_ga);
2206#endif
2207#ifdef FEAT_COMMENTS
2208 clear_string_option(&buf->b_p_com);
2209#endif
2210#ifdef FEAT_FOLDING
2211 clear_string_option(&buf->b_p_cms);
2212#endif
2213 clear_string_option(&buf->b_p_nf);
2214#ifdef FEAT_SYN_HL
2215 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002216 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002217#endif
2218#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002219 clear_string_option(&buf->b_s.b_p_spc);
2220 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002221 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002222 buf->b_s.b_cap_prog = NULL;
2223 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#endif
2225#ifdef FEAT_SEARCHPATH
2226 clear_string_option(&buf->b_p_sua);
2227#endif
2228#ifdef FEAT_AUTOCMD
2229 clear_string_option(&buf->b_p_ft);
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#ifdef FEAT_CINDENT
2232 clear_string_option(&buf->b_p_cink);
2233 clear_string_option(&buf->b_p_cino);
2234#endif
2235#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2236 clear_string_option(&buf->b_p_cinw);
2237#endif
2238#ifdef FEAT_INS_EXPAND
2239 clear_string_option(&buf->b_p_cpt);
2240#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002241#ifdef FEAT_COMPL_FUNC
2242 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002243 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245#ifdef FEAT_QUICKFIX
2246 clear_string_option(&buf->b_p_gp);
2247 clear_string_option(&buf->b_p_mp);
2248 clear_string_option(&buf->b_p_efm);
2249#endif
2250 clear_string_option(&buf->b_p_ep);
2251 clear_string_option(&buf->b_p_path);
2252 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002253 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#ifdef FEAT_INS_EXPAND
2255 clear_string_option(&buf->b_p_dict);
2256 clear_string_option(&buf->b_p_tsr);
2257#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002258#ifdef FEAT_TEXTOBJ
2259 clear_string_option(&buf->b_p_qe);
2260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002262 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002263#ifdef FEAT_LISP
2264 clear_string_option(&buf->b_p_lw);
2265#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002266 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002267#ifdef FEAT_MBYTE
2268 clear_string_option(&buf->b_p_menc);
2269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270}
2271
2272/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002273 * Get alternate file "n".
2274 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2275 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 * if (options & GETF_SETMARK) call setpcmark()
2277 * if (options & GETF_ALT) we are jumping to an alternate file.
2278 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2279 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002280 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 */
2282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002283buflist_getfile(
2284 int n,
2285 linenr_T lnum,
2286 int options,
2287 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 pos_T *fpos;
2292 colnr_T col;
2293
2294 buf = buflist_findnr(n);
2295 if (buf == NULL)
2296 {
2297 if ((options & GETF_ALT) && n == 0)
2298 EMSG(_(e_noalt));
2299 else
2300 EMSGN(_("E92: Buffer %ld not found"), n);
2301 return FAIL;
2302 }
2303
2304 /* if alternate file is the current buffer, nothing to do */
2305 if (buf == curbuf)
2306 return OK;
2307
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002308 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002309 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002310 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002312 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002313#ifdef FEAT_AUTOCMD
2314 if (curbuf_locked())
2315 return FAIL;
2316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 /* altfpos may be changed by getfile(), get it now */
2319 if (lnum == 0)
2320 {
2321 fpos = buflist_findfpos(buf);
2322 lnum = fpos->lnum;
2323 col = fpos->col;
2324 }
2325 else
2326 col = 0;
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (options & GETF_SWITCH)
2329 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002330 /* If 'switchbuf' contains "useopen": jump to first window containing
2331 * "buf" if one exists */
2332 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002334
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002335 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002336 * page containing "buf" if one exists */
2337 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002338 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002339
2340 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2341 * current buffer isn't empty: open new tab or window */
2342 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002343 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002345 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002346 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002347 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2348 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002350 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
2354 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002355 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2356 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 --RedrawingDisabled;
2359
2360 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2361 if (!p_sol && col != 0)
2362 {
2363 curwin->w_cursor.col = col;
2364 check_cursor_col();
2365#ifdef FEAT_VIRTUALEDIT
2366 curwin->w_cursor.coladd = 0;
2367#endif
2368 curwin->w_set_curswant = TRUE;
2369 }
2370 return OK;
2371 }
2372 --RedrawingDisabled;
2373 return FAIL;
2374}
2375
2376/*
2377 * go to the last know line number for the current buffer
2378 */
2379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002380buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 pos_T *fpos;
2383
2384 fpos = buflist_findfpos(curbuf);
2385
2386 curwin->w_cursor.lnum = fpos->lnum;
2387 check_cursor_lnum();
2388
2389 if (p_sol)
2390 curwin->w_cursor.col = 0;
2391 else
2392 {
2393 curwin->w_cursor.col = fpos->col;
2394 check_cursor_col();
2395#ifdef FEAT_VIRTUALEDIT
2396 curwin->w_cursor.coladd = 0;
2397#endif
2398 curwin->w_set_curswant = TRUE;
2399 }
2400}
2401
Bram Moolenaar81695252004-12-29 20:58:21 +00002402#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2403/*
2404 * Find file in buffer list by name (it has to be for the current window).
2405 * Returns NULL if not found.
2406 */
2407 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002408buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002409{
2410 char_u *ffname;
2411 buf_T *buf = NULL;
2412
2413 /* First make the name into a full path name */
2414 ffname = FullName_save(fname,
2415#ifdef UNIX
2416 TRUE /* force expansion, get rid of symbolic links */
2417#else
2418 FALSE
2419#endif
2420 );
2421 if (ffname != NULL)
2422 {
2423 buf = buflist_findname(ffname);
2424 vim_free(ffname);
2425 }
2426 return buf;
2427}
2428#endif
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430/*
2431 * Find file in buffer list by name (it has to be for the current window).
2432 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002433 * Skips dummy buffers.
2434 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
2436 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002437buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002440 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 if (mch_stat((char *)ffname, &st) < 0)
2443 st.st_dev = (dev_T)-1;
2444 return buflist_findname_stat(ffname, &st);
2445}
2446
2447/*
2448 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2449 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002450 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
2452 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002453buflist_findname_stat(
2454 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002455 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457#endif
2458 buf_T *buf;
2459
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002460 /* Start at the last buffer, expect to find a match sooner. */
2461 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002462 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463#ifdef UNIX
2464 , stp
2465#endif
2466 ))
2467 return buf;
2468 return NULL;
2469}
2470
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002471#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2472 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473/*
2474 * Find file in buffer list by a regexp pattern.
2475 * Return fnum of the found buffer.
2476 * Return < 0 for error.
2477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002479buflist_findpat(
2480 char_u *pattern,
2481 char_u *pattern_end, /* pointer to first char after pattern */
2482 int unlisted, /* find unlisted buffers */
2483 int diffmode UNUSED, /* find diff-mode buffers only */
2484 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 int match = -1;
2488 int find_listed;
2489 char_u *pat;
2490 char_u *patend;
2491 int attempt;
2492 char_u *p;
2493 int toggledollar;
2494
2495 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2496 {
2497 if (*pattern == '%')
2498 match = curbuf->b_fnum;
2499 else
2500 match = curwin->w_alt_fnum;
2501#ifdef FEAT_DIFF
2502 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2503 match = -1;
2504#endif
2505 }
2506
2507 /*
2508 * Try four ways of matching a listed buffer:
2509 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002510 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 * attempt == 2: with '$' at end (only match at end)
2512 * attempt == 3: with '^' at start and '$' at end (only full match)
2513 * Repeat this for finding an unlisted buffer if there was no matching
2514 * listed buffer.
2515 */
2516 else
2517 {
2518 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2519 if (pat == NULL)
2520 return -1;
2521 patend = pat + STRLEN(pat) - 1;
2522 toggledollar = (patend > pat && *patend == '$');
2523
2524 /* First try finding a listed buffer. If not found and "unlisted"
2525 * is TRUE, try finding an unlisted buffer. */
2526 find_listed = TRUE;
2527 for (;;)
2528 {
2529 for (attempt = 0; attempt <= 3; ++attempt)
2530 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002531 regmatch_T regmatch;
2532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 /* may add '^' and '$' */
2534 if (toggledollar)
2535 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2536 p = pat;
2537 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2538 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002539 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2540 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 vim_free(pat);
2543 return -1;
2544 }
2545
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002546 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (buf->b_p_bl == find_listed
2548#ifdef FEAT_DIFF
2549 && (!diffmode || diff_mode_buf(buf))
2550#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002551 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002553 if (curtab_only)
2554 {
2555 /* Ignore the match if the buffer is not open in
2556 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002557 win_T *wp;
2558
Bram Moolenaar29323592016-07-24 22:04:11 +02002559 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002560 if (wp->w_buffer == buf)
2561 break;
2562 if (wp == NULL)
2563 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 if (match >= 0) /* already found a match */
2566 {
2567 match = -2;
2568 break;
2569 }
2570 match = buf->b_fnum; /* remember first match */
2571 }
2572
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002573 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (match >= 0) /* found one match */
2575 break;
2576 }
2577
2578 /* Only search for unlisted buffers if there was no match with
2579 * a listed buffer. */
2580 if (!unlisted || !find_listed || match != -1)
2581 break;
2582 find_listed = FALSE;
2583 }
2584
2585 vim_free(pat);
2586 }
2587
2588 if (match == -2)
2589 EMSG2(_("E93: More than one match for %s"), pattern);
2590 else if (match < 0)
2591 EMSG2(_("E94: No matching buffer for %s"), pattern);
2592 return match;
2593}
2594#endif
2595
2596#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2597
2598/*
2599 * Find all buffer names that match.
2600 * For command line expansion of ":buf" and ":sbuf".
2601 * Return OK if matches found, FAIL otherwise.
2602 */
2603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002604ExpandBufnames(
2605 char_u *pat,
2606 int *num_file,
2607 char_u ***file,
2608 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 int count = 0;
2611 buf_T *buf;
2612 int round;
2613 char_u *p;
2614 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002615 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 *num_file = 0; /* return values in case of FAIL */
2618 *file = NULL;
2619
Bram Moolenaar05159a02005-02-26 23:04:13 +00002620 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2621 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002623 patc = alloc((unsigned)STRLEN(pat) + 11);
2624 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002626 STRCPY(patc, "\\(^\\|[\\/]\\)");
2627 STRCPY(patc + 11, pat + 1);
2628 }
2629 else
2630 patc = pat;
2631
2632 /*
2633 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002634 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002635 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002636 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002637 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002638 regmatch_T regmatch;
2639
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002640 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002641 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002642 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2643 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002644 {
2645 if (patc != pat)
2646 vim_free(patc);
2647 return FAIL;
2648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
2650 /*
2651 * round == 1: Count the matches.
2652 * round == 2: Build the array to keep the matches.
2653 */
2654 for (round = 1; round <= 2; ++round)
2655 {
2656 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002657 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 if (!buf->b_p_bl) /* skip unlisted buffers */
2660 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002661 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (p != NULL)
2663 {
2664 if (round == 1)
2665 ++count;
2666 else
2667 {
2668 if (options & WILD_HOME_REPLACE)
2669 p = home_replace_save(buf, p);
2670 else
2671 p = vim_strsave(p);
2672 (*file)[count++] = p;
2673 }
2674 }
2675 }
2676 if (count == 0) /* no match found, break here */
2677 break;
2678 if (round == 1)
2679 {
2680 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2681 if (*file == NULL)
2682 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002683 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002684 if (patc != pat)
2685 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 return FAIL;
2687 }
2688 }
2689 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002690 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (count) /* match(es) found, break here */
2692 break;
2693 }
2694
Bram Moolenaar05159a02005-02-26 23:04:13 +00002695 if (patc != pat)
2696 vim_free(patc);
2697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 *num_file = count;
2699 return (count == 0 ? FAIL : OK);
2700}
2701
2702#endif /* FEAT_CMDL_COMPL */
2703
2704#ifdef HAVE_BUFLIST_MATCH
2705/*
2706 * Check for a match on the file name for buffer "buf" with regprog "prog".
2707 */
2708 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002709buflist_match(
2710 regmatch_T *rmp,
2711 buf_T *buf,
2712 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 char_u *match;
2715
2716 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002717 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002719 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 return match;
2722}
2723
2724/*
2725 * Try matching the regexp in "prog" with file name "name".
2726 * Return "name" when there is a match, NULL when not.
2727 */
2728 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002729fname_match(
2730 regmatch_T *rmp,
2731 char_u *name,
2732 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
2734 char_u *match = NULL;
2735 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 if (name != NULL)
2738 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002739 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002740 rmp->rm_ic = p_fic || ignore_case;
2741 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 match = name;
2743 else
2744 {
2745 /* Replace $(HOME) with '~' and try matching again. */
2746 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002747 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 match = name;
2749 vim_free(p);
2750 }
2751 }
2752
2753 return match;
2754}
2755#endif
2756
2757/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002758 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 */
2760 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002761buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002763 char_u key[VIM_SIZEOF_INT * 2 + 1];
2764 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 if (nr == 0)
2767 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002768 sprintf((char *)key, "%x", nr);
2769 hi = hash_find(&buf_hashtab, key);
2770
2771 if (!HASHITEM_EMPTY(hi))
2772 return (buf_T *)(hi->hi_key
2773 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 return NULL;
2775}
2776
2777/*
2778 * Get name of file 'n' in the buffer list.
2779 * When the file has no name an empty string is returned.
2780 * home_replace() is used to shorten the file name (used for marks).
2781 * Returns a pointer to allocated memory, of NULL when failed.
2782 */
2783 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002784buflist_nr2name(
2785 int n,
2786 int fullname,
2787 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 buf_T *buf;
2790
2791 buf = buflist_findnr(n);
2792 if (buf == NULL)
2793 return NULL;
2794 return home_replace_save(helptail ? buf : NULL,
2795 fullname ? buf->b_ffname : buf->b_fname);
2796}
2797
2798/*
2799 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2800 * When "copy_options" is TRUE save the local window option values.
2801 * When "lnum" is 0 only do the options.
2802 */
2803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002804buflist_setfpos(
2805 buf_T *buf,
2806 win_T *win,
2807 linenr_T lnum,
2808 colnr_T col,
2809 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
2811 wininfo_T *wip;
2812
2813 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2814 if (wip->wi_win == win)
2815 break;
2816 if (wip == NULL)
2817 {
2818 /* allocate a new entry */
2819 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2820 if (wip == NULL)
2821 return;
2822 wip->wi_win = win;
2823 if (lnum == 0) /* set lnum even when it's 0 */
2824 lnum = 1;
2825 }
2826 else
2827 {
2828 /* remove the entry from the list */
2829 if (wip->wi_prev)
2830 wip->wi_prev->wi_next = wip->wi_next;
2831 else
2832 buf->b_wininfo = wip->wi_next;
2833 if (wip->wi_next)
2834 wip->wi_next->wi_prev = wip->wi_prev;
2835 if (copy_options && wip->wi_optset)
2836 {
2837 clear_winopt(&wip->wi_opt);
2838#ifdef FEAT_FOLDING
2839 deleteFoldRecurse(&wip->wi_folds);
2840#endif
2841 }
2842 }
2843 if (lnum != 0)
2844 {
2845 wip->wi_fpos.lnum = lnum;
2846 wip->wi_fpos.col = col;
2847 }
2848 if (copy_options)
2849 {
2850 /* Save the window-specific option values. */
2851 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2852#ifdef FEAT_FOLDING
2853 wip->wi_fold_manual = win->w_fold_manual;
2854 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2855#endif
2856 wip->wi_optset = TRUE;
2857 }
2858
2859 /* insert the entry in front of the list */
2860 wip->wi_next = buf->b_wininfo;
2861 buf->b_wininfo = wip;
2862 wip->wi_prev = NULL;
2863 if (wip->wi_next)
2864 wip->wi_next->wi_prev = wip;
2865
2866 return;
2867}
2868
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002869#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002870static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002871
2872/*
2873 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2874 * page. That's because a diff is local to a tab page.
2875 */
2876 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002877wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002878{
2879 win_T *wp;
2880
2881 if (wip->wi_opt.wo_diff)
2882 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002883 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002884 /* return FALSE when it's a window in the current tab page, thus
2885 * the buffer was in diff mode here */
2886 if (wip->wi_win == wp)
2887 return FALSE;
2888 return TRUE;
2889 }
2890 return FALSE;
2891}
2892#endif
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894/*
2895 * Find info for the current window in buffer "buf".
2896 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002897 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2898 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 * Returns NULL when there isn't any info.
2900 */
2901 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002902find_wininfo(
2903 buf_T *buf,
2904 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 wininfo_T *wip;
2907
2908 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002909 if (wip->wi_win == curwin
2910#ifdef FEAT_DIFF
2911 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2912#endif
2913 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002915
2916 /* If no wininfo for curwin, use the first in the list (that doesn't have
2917 * 'diff' set and is in another tab page). */
2918 if (wip == NULL)
2919 {
2920#ifdef FEAT_DIFF
2921 if (skip_diff_buffer)
2922 {
2923 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2924 if (!wininfo_other_tab_diff(wip))
2925 break;
2926 }
2927 else
2928#endif
2929 wip = buf->b_wininfo;
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 return wip;
2932}
2933
2934/*
2935 * Reset the local window options to the values last used in this window.
2936 * If the buffer wasn't used in this window before, use the values from
2937 * the most recently used window. If the values were never set, use the
2938 * global values for the window.
2939 */
2940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002941get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
2943 wininfo_T *wip;
2944
2945 clear_winopt(&curwin->w_onebuf_opt);
2946#ifdef FEAT_FOLDING
2947 clearFolding(curwin);
2948#endif
2949
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002950 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 if (wip != NULL && wip->wi_optset)
2952 {
2953 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2954#ifdef FEAT_FOLDING
2955 curwin->w_fold_manual = wip->wi_fold_manual;
2956 curwin->w_foldinvalid = TRUE;
2957 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2958#endif
2959 }
2960 else
2961 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2962
2963#ifdef FEAT_FOLDING
2964 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2965 if (p_fdls >= 0)
2966 curwin->w_p_fdl = p_fdls;
2967#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002968#ifdef FEAT_SYN_HL
2969 check_colorcolumn(curwin);
2970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
2973/*
2974 * Find the position (lnum and col) for the buffer 'buf' for the current
2975 * window.
2976 * Returns a pointer to no_position if no position is found.
2977 */
2978 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002979buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002982 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002984 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 if (wip != NULL)
2986 return &(wip->wi_fpos);
2987 else
2988 return &no_position;
2989}
2990
2991/*
2992 * Find the lnum for the buffer 'buf' for the current window.
2993 */
2994 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002995buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 return buflist_findfpos(buf)->lnum;
2998}
2999
3000#if defined(FEAT_LISTCMDS) || defined(PROTO)
3001/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003002 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003005buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 buf_T *buf;
3008 int len;
3009 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003010 int ro_char;
3011 int changed_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3014 {
3015 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003016 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3017 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3018 || (vim_strchr(eap->arg, '+')
3019 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3020 || (vim_strchr(eap->arg, 'a')
3021 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
3022 || (vim_strchr(eap->arg, 'h')
3023 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3024 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3025 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3026 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3027 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3028 || (vim_strchr(eap->arg, '#')
3029 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003032 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else
3034 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003035 if (message_filtered(NameBuff))
3036 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003038 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3039 : (bufIsChanged(buf) ? '+' : ' ');
3040#ifdef FEAT_TERMINAL
3041 if (term_job_running(buf->b_term))
3042 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003043 if (term_none_open(buf->b_term))
3044 ro_char = '?';
3045 else
3046 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003047 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3048 * closing, but it's not actually changed. */
3049 }
3050 else if (buf->b_term != NULL)
3051 ro_char = 'F';
3052 else
3053#endif
3054 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3055
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003056 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003057 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 buf->b_fnum,
3059 buf->b_p_bl ? ' ' : 'u',
3060 buf == curbuf ? '%' :
3061 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3062 buf->b_ml.ml_mfp == NULL ? ' ' :
3063 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003064 ro_char,
3065 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003066 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003067 if (len > IOSIZE - 20)
3068 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
3070 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 i = 40 - vim_strsize(IObuff);
3072 do
3073 {
3074 IObuff[len++] = ' ';
3075 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003076 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3077 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003078 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 msg_outtrans(IObuff);
3080 out_flush(); /* output one line at a time */
3081 ui_breakcheck();
3082 }
3083}
3084#endif
3085
3086/*
3087 * Get file name and line number for file 'fnum'.
3088 * Used by DoOneCmd() for translating '%' and '#'.
3089 * Used by insert_reg() and cmdline_paste() for '#' register.
3090 * Return FAIL if not found, OK for success.
3091 */
3092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003093buflist_name_nr(
3094 int fnum,
3095 char_u **fname,
3096 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
3098 buf_T *buf;
3099
3100 buf = buflist_findnr(fnum);
3101 if (buf == NULL || buf->b_fname == NULL)
3102 return FAIL;
3103
3104 *fname = buf->b_fname;
3105 *lnum = buflist_findlnum(buf);
3106
3107 return OK;
3108}
3109
3110/*
3111 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3112 * The file name with the full path is also remembered, for when :cd is used.
3113 * Returns FAIL for failure (file name already in use by other buffer)
3114 * OK otherwise.
3115 */
3116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003117setfname(
3118 buf_T *buf,
3119 char_u *ffname,
3120 char_u *sfname,
3121 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
Bram Moolenaar81695252004-12-29 20:58:21 +00003123 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003125 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#endif
3127
3128 if (ffname == NULL || *ffname == NUL)
3129 {
3130 /* Removing the name. */
3131 vim_free(buf->b_ffname);
3132 vim_free(buf->b_sfname);
3133 buf->b_ffname = NULL;
3134 buf->b_sfname = NULL;
3135#ifdef UNIX
3136 st.st_dev = (dev_T)-1;
3137#endif
3138 }
3139 else
3140 {
3141 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3142 if (ffname == NULL) /* out of memory */
3143 return FAIL;
3144
3145 /*
3146 * if the file name is already used in another buffer:
3147 * - if the buffer is loaded, fail
3148 * - if the buffer is not loaded, delete it from the list
3149 */
3150#ifdef UNIX
3151 if (mch_stat((char *)ffname, &st) < 0)
3152 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003153#endif
3154 if (!(buf->b_flags & BF_DUMMY))
3155#ifdef UNIX
3156 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003158 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#endif
3160 if (obuf != NULL && obuf != buf)
3161 {
3162 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3163 {
3164 if (message)
3165 EMSG(_("E95: Buffer with this name already exists"));
3166 vim_free(ffname);
3167 return FAIL;
3168 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003169 /* delete from the list */
3170 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172 sfname = vim_strsave(sfname);
3173 if (ffname == NULL || sfname == NULL)
3174 {
3175 vim_free(sfname);
3176 vim_free(ffname);
3177 return FAIL;
3178 }
3179#ifdef USE_FNAME_CASE
3180# ifdef USE_LONG_FNAME
3181 if (USE_LONG_FNAME)
3182# endif
3183 fname_case(sfname, 0); /* set correct case for short file name */
3184#endif
3185 vim_free(buf->b_ffname);
3186 vim_free(buf->b_sfname);
3187 buf->b_ffname = ffname;
3188 buf->b_sfname = sfname;
3189 }
3190 buf->b_fname = buf->b_sfname;
3191#ifdef UNIX
3192 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003193 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 else
3195 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003196 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 buf->b_dev = st.st_dev;
3198 buf->b_ino = st.st_ino;
3199 }
3200#endif
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 buf_name_changed(buf);
3205 return OK;
3206}
3207
3208/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003209 * Crude way of changing the name of a buffer. Use with care!
3210 * The name should be relative to the current directory.
3211 */
3212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003214{
3215 buf_T *buf;
3216
3217 buf = buflist_findnr(fnum);
3218 if (buf != NULL)
3219 {
3220 vim_free(buf->b_sfname);
3221 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003222 buf->b_ffname = vim_strsave(name);
3223 buf->b_sfname = NULL;
3224 /* Allocate ffname and expand into full path. Also resolves .lnk
3225 * files on Win32. */
3226 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003227 buf->b_fname = buf->b_sfname;
3228 }
3229}
3230
3231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 * Take care of what needs to be done when the name of buffer "buf" has
3233 * changed.
3234 */
3235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003236buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 /*
3239 * If the file name changed, also change the name of the swapfile
3240 */
3241 if (buf->b_ml.ml_mfp != NULL)
3242 ml_setname(buf);
3243
3244 if (curwin->w_buffer == buf)
3245 check_arg_idx(curwin); /* check file name for arg list */
3246#ifdef FEAT_TITLE
3247 maketitle(); /* set window title */
3248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 fmarks_check_names(buf); /* check named file marks */
3251 ml_timestamp(buf); /* reset timestamp */
3252}
3253
3254/*
3255 * set alternate file name for current window
3256 *
3257 * Used by do_one_cmd(), do_write() and do_ecmd().
3258 * Return the buffer.
3259 */
3260 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003261setaltfname(
3262 char_u *ffname,
3263 char_u *sfname,
3264 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 buf_T *buf;
3267
3268 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3269 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003270 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 curwin->w_alt_fnum = buf->b_fnum;
3272 return buf;
3273}
3274
3275/*
3276 * Get alternate file name for current window.
3277 * Return NULL if there isn't any, and give error message if requested.
3278 */
3279 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003280getaltfname(
3281 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 char_u *fname;
3284 linenr_T dummy;
3285
3286 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3287 {
3288 if (errmsg)
3289 EMSG(_(e_noalt));
3290 return NULL;
3291 }
3292 return fname;
3293}
3294
3295/*
3296 * Add a file name to the buflist and return its number.
3297 * Uses same flags as buflist_new(), except BLN_DUMMY.
3298 *
3299 * used by qf_init(), main() and doarglist()
3300 */
3301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304 buf_T *buf;
3305
3306 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3307 if (buf != NULL)
3308 return buf->b_fnum;
3309 return 0;
3310}
3311
3312#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3313/*
3314 * Adjust slashes in file names. Called after 'shellslash' was set.
3315 */
3316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 buf_T *bp;
3320
Bram Moolenaar29323592016-07-24 22:04:11 +02003321 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
3323 if (bp->b_ffname != NULL)
3324 slash_adjust(bp->b_ffname);
3325 if (bp->b_sfname != NULL)
3326 slash_adjust(bp->b_sfname);
3327 }
3328}
3329#endif
3330
3331/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003332 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 * Also save the local window option values.
3334 */
3335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003338 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339}
3340
3341/*
3342 * Return TRUE if 'ffname' is not the same file as current file.
3343 * Fname must have a full path (expanded by mch_FullName()).
3344 */
3345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003346otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 return otherfile_buf(curbuf, ffname
3349#ifdef UNIX
3350 , NULL
3351#endif
3352 );
3353}
3354
3355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356otherfile_buf(
3357 buf_T *buf,
3358 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003360 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003362 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 /* no name is different */
3365 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3366 return TRUE;
3367 if (fnamecmp(ffname, buf->b_ffname) == 0)
3368 return FALSE;
3369#ifdef UNIX
3370 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003371 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
Bram Moolenaar8767f522016-07-01 17:17:39 +02003373 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (stp == NULL)
3375 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003376 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 st.st_dev = (dev_T)-1;
3378 stp = &st;
3379 }
3380 /* Use dev/ino to check if the files are the same, even when the names
3381 * are different (possible with links). Still need to compare the
3382 * name above, for when the file doesn't exist yet.
3383 * Problem: The dev/ino changes when a file is deleted (and created
3384 * again) and remains the same when renamed/moved. We don't want to
3385 * mch_stat() each buffer each time, that would be too slow. Get the
3386 * dev/ino again when they appear to match, but not when they appear
3387 * to be different: Could skip a buffer when it's actually the same
3388 * file. */
3389 if (buf_same_ino(buf, stp))
3390 {
3391 buf_setino(buf);
3392 if (buf_same_ino(buf, stp))
3393 return FALSE;
3394 }
3395 }
3396#endif
3397 return TRUE;
3398}
3399
3400#if defined(UNIX) || defined(PROTO)
3401/*
3402 * Set inode and device number for a buffer.
3403 * Must always be called when b_fname is changed!.
3404 */
3405 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003406buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003408 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
3410 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3411 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003412 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 buf->b_dev = st.st_dev;
3414 buf->b_ino = st.st_ino;
3415 }
3416 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003417 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418}
3419
3420/*
3421 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3422 */
3423 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003424buf_same_ino(
3425 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003426 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003428 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 && stp->st_dev == buf->b_dev
3430 && stp->st_ino == buf->b_ino);
3431}
3432#endif
3433
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434/*
3435 * Print info about the current buffer.
3436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003438fileinfo(
3439 int fullname, /* when non-zero print full path */
3440 int shorthelp,
3441 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 char_u *name;
3444 int n;
3445 char_u *p;
3446 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003447 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 buffer = alloc(IOSIZE);
3450 if (buffer == NULL)
3451 return;
3452
3453 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3454 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003455 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 p = buffer + STRLEN(buffer);
3457 }
3458 else
3459 p = buffer;
3460
3461 *p++ = '"';
3462 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003463 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 else
3465 {
3466 if (!fullname && curbuf->b_fname != NULL)
3467 name = curbuf->b_fname;
3468 else
3469 name = curbuf->b_ffname;
3470 home_replace(shorthelp ? curbuf : NULL, name, p,
3471 (int)(IOSIZE - (p - buffer)), TRUE);
3472 }
3473
Bram Moolenaara800b422010-06-27 01:15:55 +02003474 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 curbufIsChanged() ? (shortmess(SHM_MOD)
3476 ? " [+]" : _(" [Modified]")) : " ",
3477 (curbuf->b_flags & BF_NOTEDITED)
3478#ifdef FEAT_QUICKFIX
3479 && !bt_dontwrite(curbuf)
3480#endif
3481 ? _("[Not edited]") : "",
3482 (curbuf->b_flags & BF_NEW)
3483#ifdef FEAT_QUICKFIX
3484 && !bt_dontwrite(curbuf)
3485#endif
3486 ? _("[New file]") : "",
3487 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003488 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 : _("[readonly]")) : "",
3490 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3491 || curbuf->b_p_ro) ?
3492 " " : "");
3493 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3494 * causes an overflow, thus for large numbers divide instead. */
3495 if (curwin->w_cursor.lnum > 1000000L)
3496 n = (int)(((long)curwin->w_cursor.lnum) /
3497 ((long)curbuf->b_ml.ml_line_count / 100L));
3498 else
3499 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3500 (long)curbuf->b_ml.ml_line_count);
3501 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3502 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003503 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505#ifdef FEAT_CMDL_INFO
3506 else if (p_ru)
3507 {
3508 /* Current line and column are already on the screen -- webb */
3509 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003510 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003512 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 (long)curbuf->b_ml.ml_line_count, n);
3514 }
3515#endif
3516 else
3517 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003518 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 _("line %ld of %ld --%d%%-- col "),
3520 (long)curwin->w_cursor.lnum,
3521 (long)curbuf->b_ml.ml_line_count,
3522 n);
3523 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003524 len = STRLEN(buffer);
3525 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3527 }
3528
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003529 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 if (dont_truncate)
3532 {
3533 /* Temporarily set msg_scroll to avoid the message being truncated.
3534 * First call msg_start() to get the message in the right place. */
3535 msg_start();
3536 n = msg_scroll;
3537 msg_scroll = TRUE;
3538 msg(buffer);
3539 msg_scroll = n;
3540 }
3541 else
3542 {
3543 p = msg_trunc_attr(buffer, FALSE, 0);
3544 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* Need to repeat the message after redrawing when:
3546 * - When restart_edit is set (otherwise there will be a delay
3547 * before redrawing).
3548 * - When the screen was scrolled but there is no wait-return
3549 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003550 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552
3553 vim_free(buffer);
3554}
3555
3556 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003557col_print(
3558 char_u *buf,
3559 size_t buflen,
3560 int col,
3561 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
3563 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003564 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003566 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567}
3568
3569#if defined(FEAT_TITLE) || defined(PROTO)
3570/*
3571 * put file name in title bar of window and in icon title
3572 */
3573
3574static char_u *lasttitle = NULL;
3575static char_u *lasticon = NULL;
3576
3577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003578maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
3580 char_u *p;
3581 char_u *t_str = NULL;
3582 char_u *i_name;
3583 char_u *i_str = NULL;
3584 int maxlen = 0;
3585 int len;
3586 int mustset;
3587 char_u buf[IOSIZE];
3588 int off;
3589
3590 if (!redrawing())
3591 {
3592 /* Postpone updating the title when 'lazyredraw' is set. */
3593 need_maketitle = TRUE;
3594 return;
3595 }
3596
3597 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003598 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 return;
3600
3601 if (p_title)
3602 {
3603 if (p_titlelen > 0)
3604 {
3605 maxlen = p_titlelen * Columns / 100;
3606 if (maxlen < 10)
3607 maxlen = 10;
3608 }
3609
3610 t_str = buf;
3611 if (*p_titlestring != NUL)
3612 {
3613#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003614 if (stl_syntax & STL_IN_TITLE)
3615 {
3616 int use_sandbox = FALSE;
3617 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003618
3619# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003620 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003621# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003622 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003624 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003625 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003626 if (called_emsg)
3627 set_string_option_direct((char_u *)"titlestring", -1,
3628 (char_u *)"", OPT_FREE, SID_ERROR);
3629 called_emsg |= save_called_emsg;
3630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 else
3632#endif
3633 t_str = p_titlestring;
3634 }
3635 else
3636 {
3637 /* format: "fname + (path) (1 of 2) - VIM" */
3638
Bram Moolenaar2c666692012-09-05 13:30:40 +02003639#define SPACE_FOR_FNAME (IOSIZE - 100)
3640#define SPACE_FOR_DIR (IOSIZE - 20)
3641#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003643 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003644#ifdef FEAT_TERMINAL
3645 else if (curbuf->b_term != NULL)
3646 {
3647 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3648 SPACE_FOR_FNAME);
3649 }
3650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 else
3652 {
3653 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003654 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657
Bram Moolenaar21554412017-07-24 21:44:43 +02003658#ifdef FEAT_TERMINAL
3659 if (curbuf->b_term == NULL)
3660#endif
3661 switch (bufIsChanged(curbuf)
3662 + (curbuf->b_p_ro * 2)
3663 + (!curbuf->b_p_ma * 4))
3664 {
3665 case 1: STRCAT(buf, " +"); break;
3666 case 2: STRCAT(buf, " ="); break;
3667 case 3: STRCAT(buf, " =+"); break;
3668 case 4:
3669 case 6: STRCAT(buf, " -"); break;
3670 case 5:
3671 case 7: STRCAT(buf, " -+"); break;
3672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar21554412017-07-24 21:44:43 +02003674 if (curbuf->b_fname != NULL
3675#ifdef FEAT_TERMINAL
3676 && curbuf->b_term == NULL
3677#endif
3678 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
3680 /* Get path of file, replace home dir with ~ */
3681 off = (int)STRLEN(buf);
3682 buf[off++] = ' ';
3683 buf[off++] = '(';
3684 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003685 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686#ifdef BACKSLASH_IN_FILENAME
3687 /* avoid "c:/name" to be reduced to "c" */
3688 if (isalpha(buf[off]) && buf[off + 1] == ':')
3689 off += 2;
3690#endif
3691 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003692 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003694 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003695 /* must be a help buffer */
3696 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003697 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003701
Bram Moolenaar2c666692012-09-05 13:30:40 +02003702 /* Translate unprintable chars and concatenate. Keep some
3703 * room for the server name. When there is no room (very long
3704 * file name) use (...). */
3705 if (off < SPACE_FOR_DIR)
3706 {
3707 p = transstr(buf + off);
3708 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3709 vim_free(p);
3710 }
3711 else
3712 {
3713 vim_strncpy(buf + off, (char_u *)"...",
3714 (size_t)(SPACE_FOR_ARGNR - off));
3715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 STRCAT(buf, ")");
3717 }
3718
Bram Moolenaar2c666692012-09-05 13:30:40 +02003719 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721#if defined(FEAT_CLIENTSERVER)
3722 if (serverName != NULL)
3723 {
3724 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003725 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727 else
3728#endif
3729 STRCAT(buf, " - VIM");
3730
3731 if (maxlen > 0)
3732 {
3733 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003734 if (vim_strsize(buf) > maxlen)
3735 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 }
3738 }
3739 mustset = ti_change(t_str, &lasttitle);
3740
3741 if (p_icon)
3742 {
3743 i_str = buf;
3744 if (*p_iconstring != NUL)
3745 {
3746#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003747 if (stl_syntax & STL_IN_ICON)
3748 {
3749 int use_sandbox = FALSE;
3750 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003751
3752# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003753 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003754# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003755 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003757 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003758 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003759 if (called_emsg)
3760 set_string_option_direct((char_u *)"iconstring", -1,
3761 (char_u *)"", OPT_FREE, SID_ERROR);
3762 called_emsg |= save_called_emsg;
3763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 else
3765#endif
3766 i_str = p_iconstring;
3767 }
3768 else
3769 {
3770 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003771 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 else /* use file name only in icon */
3773 i_name = gettail(curbuf->b_ffname);
3774 *i_str = NUL;
3775 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003776 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 if (len > 100)
3778 {
3779 len -= 100;
3780#ifdef FEAT_MBYTE
3781 if (has_mbyte)
3782 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3783#endif
3784 i_name += len;
3785 }
3786 STRCPY(i_str, i_name);
3787 trans_characters(i_str, IOSIZE);
3788 }
3789 }
3790
3791 mustset |= ti_change(i_str, &lasticon);
3792
3793 if (mustset)
3794 resettitle();
3795}
3796
3797/*
3798 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3799 * from "str" if it does.
3800 * Return TRUE when "*last" changed.
3801 */
3802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003803ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
3805 if ((str == NULL) != (*last == NULL)
3806 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3807 {
3808 vim_free(*last);
3809 if (str == NULL)
3810 *last = NULL;
3811 else
3812 *last = vim_strsave(str);
3813 return TRUE;
3814 }
3815 return FALSE;
3816}
3817
3818/*
3819 * Put current window title back (used after calling a shell)
3820 */
3821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003822resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
3824 mch_settitle(lasttitle, lasticon);
3825}
Bram Moolenaarea408852005-06-25 22:49:46 +00003826
3827# if defined(EXITFREE) || defined(PROTO)
3828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003830{
3831 vim_free(lasttitle);
3832 vim_free(lasticon);
3833}
3834# endif
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#endif /* FEAT_TITLE */
3837
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003838#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003840 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 * Return length of string in screen cells.
3842 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003843 * Normally works for window "wp", except when working for 'tabline' then it
3844 * is "curwin".
3845 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 * Items are drawn interspersed with the text that surrounds it
3847 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3848 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3849 *
3850 * If maxwidth is not zero, the string will be filled at any middle marker
3851 * or truncated if too long, fillchar is used for all whitespace.
3852 */
3853 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003854build_stl_str_hl(
3855 win_T *wp,
3856 char_u *out, /* buffer to write into != NameBuff */
3857 size_t outlen, /* length of out[] */
3858 char_u *fmt,
3859 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3860 int fillchar,
3861 int maxwidth,
3862 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3863 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
3865 char_u *p;
3866 char_u *s;
3867 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003868 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003870 win_T *save_curwin;
3871 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872#endif
3873 int empty_line;
3874 colnr_T virtcol;
3875 long l;
3876 long n;
3877 int prevchar_isflag;
3878 int prevchar_isitem;
3879 int itemisflag;
3880 int fillable;
3881 char_u *str;
3882 long num;
3883 int width;
3884 int itemcnt;
3885 int curitem;
3886 int groupitem[STL_MAX_ITEM];
3887 int groupdepth;
3888 struct stl_item
3889 {
3890 char_u *start;
3891 int minwid;
3892 int maxwid;
3893 enum
3894 {
3895 Normal,
3896 Empty,
3897 Group,
3898 Middle,
3899 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003900 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 Trunc
3902 } type;
3903 } item[STL_MAX_ITEM];
3904 int minwid;
3905 int maxwid;
3906 int zeropad;
3907 char_u base;
3908 char_u opt;
3909#define TMPLEN 70
3910 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003911 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003912 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003913 int save_must_redraw = must_redraw;
3914 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003915
3916#ifdef FEAT_EVAL
3917 /*
3918 * When the format starts with "%!" then evaluate it as an expression and
3919 * use the result as the actual format string.
3920 */
3921 if (fmt[0] == '%' && fmt[1] == '!')
3922 {
3923 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3924 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003925 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003926 }
3927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 if (fillchar == 0)
3930 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_MBYTE
3932 /* Can't handle a multi-byte fill character yet. */
3933 else if (mb_char2len(fillchar) > 1)
3934 fillchar = '-';
3935#endif
3936
Bram Moolenaar567199b2013-04-24 16:52:36 +02003937 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3938 * p will become invalid when getting another buffer line. */
3939 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3940 empty_line = (*p == NUL);
3941
3942 /* Get the byte value now, in case we need it below. This is more
3943 * efficient than making a copy of the line. */
3944 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3945 byteval = 0;
3946 else
3947#ifdef FEAT_MBYTE
3948 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3949#else
3950 byteval = p[wp->w_cursor.col];
3951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953 groupdepth = 0;
3954 p = out;
3955 curitem = 0;
3956 prevchar_isflag = TRUE;
3957 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003958 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003960 if (curitem == STL_MAX_ITEM)
3961 {
3962 /* There are too many items. Add the error code to the statusline
3963 * to give the user a hint about what went wrong. */
3964 if (p + 6 < out + outlen)
3965 {
3966 mch_memmove(p, " E541", (size_t)5);
3967 p += 5;
3968 }
3969 break;
3970 }
3971
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (*s != NUL && *s != '%')
3973 prevchar_isflag = prevchar_isitem = FALSE;
3974
3975 /*
3976 * Handle up to the next '%' or the end.
3977 */
3978 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3979 *p++ = *s++;
3980 if (*s == NUL || p + 1 >= out + outlen)
3981 break;
3982
3983 /*
3984 * Handle one '%' item.
3985 */
3986 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003987 if (*s == NUL) /* ignore trailing % */
3988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (*s == '%')
3990 {
3991 if (p + 1 >= out + outlen)
3992 break;
3993 *p++ = *s++;
3994 prevchar_isflag = prevchar_isitem = FALSE;
3995 continue;
3996 }
3997 if (*s == STL_MIDDLEMARK)
3998 {
3999 s++;
4000 if (groupdepth > 0)
4001 continue;
4002 item[curitem].type = Middle;
4003 item[curitem++].start = p;
4004 continue;
4005 }
4006 if (*s == STL_TRUNCMARK)
4007 {
4008 s++;
4009 item[curitem].type = Trunc;
4010 item[curitem++].start = p;
4011 continue;
4012 }
4013 if (*s == ')')
4014 {
4015 s++;
4016 if (groupdepth < 1)
4017 continue;
4018 groupdepth--;
4019
4020 t = item[groupitem[groupdepth]].start;
4021 *p = NUL;
4022 l = vim_strsize(t);
4023 if (curitem > groupitem[groupdepth] + 1
4024 && item[groupitem[groupdepth]].minwid == 0)
4025 {
4026 /* remove group if all items are empty */
4027 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01004028 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 break;
4030 if (n == curitem)
4031 {
4032 p = t;
4033 l = 0;
4034 }
4035 }
4036 if (l > item[groupitem[groupdepth]].maxwid)
4037 {
4038 /* truncate, remove n bytes of text at the start */
4039#ifdef FEAT_MBYTE
4040 if (has_mbyte)
4041 {
4042 /* Find the first character that should be included. */
4043 n = 0;
4044 while (l >= item[groupitem[groupdepth]].maxwid)
4045 {
4046 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004047 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 }
4050 else
4051#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004052 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004055 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 p = p - n + 1;
4057#ifdef FEAT_MBYTE
4058 /* Fill up space left over by half a double-wide char. */
4059 while (++l < item[groupitem[groupdepth]].minwid)
4060 *p++ = fillchar;
4061#endif
4062
4063 /* correct the start of the items for the truncation */
4064 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4065 {
4066 item[l].start -= n;
4067 if (item[l].start < t)
4068 item[l].start = t;
4069 }
4070 }
4071 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4072 {
4073 /* fill */
4074 n = item[groupitem[groupdepth]].minwid;
4075 if (n < 0)
4076 {
4077 /* fill by appending characters */
4078 n = 0 - n;
4079 while (l++ < n && p + 1 < out + outlen)
4080 *p++ = fillchar;
4081 }
4082 else
4083 {
4084 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004085 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 l = n - l;
4087 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004088 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 p += l;
4090 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4091 item[n].start += l;
4092 for ( ; l > 0; l--)
4093 *t++ = fillchar;
4094 }
4095 }
4096 continue;
4097 }
4098 minwid = 0;
4099 maxwid = 9999;
4100 zeropad = FALSE;
4101 l = 1;
4102 if (*s == '0')
4103 {
4104 s++;
4105 zeropad = TRUE;
4106 }
4107 if (*s == '-')
4108 {
4109 s++;
4110 l = -1;
4111 }
4112 if (VIM_ISDIGIT(*s))
4113 {
4114 minwid = (int)getdigits(&s);
4115 if (minwid < 0) /* overflow */
4116 minwid = 0;
4117 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004118 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 {
4120 item[curitem].type = Highlight;
4121 item[curitem].start = p;
4122 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4123 s++;
4124 curitem++;
4125 continue;
4126 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004127 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4128 {
4129 if (*s == STL_TABCLOSENR)
4130 {
4131 if (minwid == 0)
4132 {
4133 /* %X ends the close label, go back to the previously
4134 * define tab label nr. */
4135 for (n = curitem - 1; n >= 0; --n)
4136 if (item[n].type == TabPage && item[n].minwid >= 0)
4137 {
4138 minwid = item[n].minwid;
4139 break;
4140 }
4141 }
4142 else
4143 /* close nrs are stored as negative values */
4144 minwid = - minwid;
4145 }
4146 item[curitem].type = TabPage;
4147 item[curitem].start = p;
4148 item[curitem].minwid = minwid;
4149 s++;
4150 curitem++;
4151 continue;
4152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 if (*s == '.')
4154 {
4155 s++;
4156 if (VIM_ISDIGIT(*s))
4157 {
4158 maxwid = (int)getdigits(&s);
4159 if (maxwid <= 0) /* overflow */
4160 maxwid = 50;
4161 }
4162 }
4163 minwid = (minwid > 50 ? 50 : minwid) * l;
4164 if (*s == '(')
4165 {
4166 groupitem[groupdepth++] = curitem;
4167 item[curitem].type = Group;
4168 item[curitem].start = p;
4169 item[curitem].minwid = minwid;
4170 item[curitem].maxwid = maxwid;
4171 s++;
4172 curitem++;
4173 continue;
4174 }
4175 if (vim_strchr(STL_ALL, *s) == NULL)
4176 {
4177 s++;
4178 continue;
4179 }
4180 opt = *s++;
4181
4182 /* OK - now for the real work */
4183 base = 'D';
4184 itemisflag = FALSE;
4185 fillable = TRUE;
4186 num = -1;
4187 str = NULL;
4188 switch (opt)
4189 {
4190 case STL_FILEPATH:
4191 case STL_FULLPATH:
4192 case STL_FILENAME:
4193 fillable = FALSE; /* don't change ' ' to fillchar */
4194 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004195 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 else
4197 {
4198 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004199 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4201 }
4202 trans_characters(NameBuff, MAXPATHL);
4203 if (opt != STL_FILENAME)
4204 str = NameBuff;
4205 else
4206 str = gettail(NameBuff);
4207 break;
4208
4209 case STL_VIM_EXPR: /* '{' */
4210 itemisflag = TRUE;
4211 t = p;
4212 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4213 *p++ = *s++;
4214 if (*s != '}') /* missing '}' or out of space */
4215 break;
4216 s++;
4217 *p = 0;
4218 p = t;
4219
4220#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004221 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4223
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004224 save_curbuf = curbuf;
4225 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 curwin = wp;
4227 curbuf = wp->w_buffer;
4228
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004229 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004231 curwin = save_curwin;
4232 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004233 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 if (str != NULL && *str != 0)
4236 {
4237 if (*skipdigits(str) == NUL)
4238 {
4239 num = atoi((char *)str);
4240 vim_free(str);
4241 str = NULL;
4242 itemisflag = FALSE;
4243 }
4244 }
4245#endif
4246 break;
4247
4248 case STL_LINE:
4249 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4250 ? 0L : (long)(wp->w_cursor.lnum);
4251 break;
4252
4253 case STL_NUMLINES:
4254 num = wp->w_buffer->b_ml.ml_line_count;
4255 break;
4256
4257 case STL_COLUMN:
4258 num = !(State & INSERT) && empty_line
4259 ? 0 : (int)wp->w_cursor.col + 1;
4260 break;
4261
4262 case STL_VIRTCOL:
4263 case STL_VIRTCOL_ALT:
4264 /* In list mode virtcol needs to be recomputed */
4265 virtcol = wp->w_virtcol;
4266 if (wp->w_p_list && lcs_tab1 == NUL)
4267 {
4268 wp->w_p_list = FALSE;
4269 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4270 wp->w_p_list = TRUE;
4271 }
4272 ++virtcol;
4273 /* Don't display %V if it's the same as %c. */
4274 if (opt == STL_VIRTCOL_ALT
4275 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4276 ? 0 : (int)wp->w_cursor.col + 1)))
4277 break;
4278 num = (long)virtcol;
4279 break;
4280
4281 case STL_PERCENTAGE:
4282 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4283 (long)wp->w_buffer->b_ml.ml_line_count);
4284 break;
4285
4286 case STL_ALTPERCENT:
4287 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004288 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 break;
4290
4291 case STL_ARGLISTSTAT:
4292 fillable = FALSE;
4293 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004294 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 str = tmp;
4296 break;
4297
4298 case STL_KEYMAP:
4299 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004300 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 str = tmp;
4302 break;
4303 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004304#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004305 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306#else
4307 num = 0;
4308#endif
4309 break;
4310
4311 case STL_BUFNO:
4312 num = wp->w_buffer->b_fnum;
4313 break;
4314
4315 case STL_OFFSET_X:
4316 base = 'X';
4317 case STL_OFFSET:
4318#ifdef FEAT_BYTEOFF
4319 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4320 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4321 0L : l + 1 + (!(State & INSERT) && empty_line ?
4322 0 : (int)wp->w_cursor.col);
4323#endif
4324 break;
4325
4326 case STL_BYTEVAL_X:
4327 base = 'X';
4328 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004329 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (num == NL)
4331 num = 0;
4332 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4333 num = NL;
4334 break;
4335
4336 case STL_ROFLAG:
4337 case STL_ROFLAG_ALT:
4338 itemisflag = TRUE;
4339 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004340 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 break;
4342
4343 case STL_HELPFLAG:
4344 case STL_HELPFLAG_ALT:
4345 itemisflag = TRUE;
4346 if (wp->w_buffer->b_help)
4347 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004348 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 break;
4350
4351#ifdef FEAT_AUTOCMD
4352 case STL_FILETYPE:
4353 if (*wp->w_buffer->b_p_ft != NUL
4354 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4355 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004356 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4357 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 str = tmp;
4359 }
4360 break;
4361
4362 case STL_FILETYPE_ALT:
4363 itemisflag = TRUE;
4364 if (*wp->w_buffer->b_p_ft != NUL
4365 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4366 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004367 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4368 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 for (t = tmp; *t != 0; t++)
4370 *t = TOUPPER_LOC(*t);
4371 str = tmp;
4372 }
4373 break;
4374#endif
4375
Bram Moolenaar4033c552017-09-16 20:54:51 +02004376#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 case STL_PREVIEWFLAG:
4378 case STL_PREVIEWFLAG_ALT:
4379 itemisflag = TRUE;
4380 if (wp->w_p_pvw)
4381 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4382 : _("[Preview]"));
4383 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004384
4385 case STL_QUICKFIX:
4386 if (bt_quickfix(wp->w_buffer))
4387 str = (char_u *)(wp->w_llist_ref
4388 ? _(msg_loclist)
4389 : _(msg_qflist));
4390 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#endif
4392
4393 case STL_MODIFIED:
4394 case STL_MODIFIED_ALT:
4395 itemisflag = TRUE;
4396 switch ((opt == STL_MODIFIED_ALT)
4397 + bufIsChanged(wp->w_buffer) * 2
4398 + (!wp->w_buffer->b_p_ma) * 4)
4399 {
4400 case 2: str = (char_u *)"[+]"; break;
4401 case 3: str = (char_u *)",+"; break;
4402 case 4: str = (char_u *)"[-]"; break;
4403 case 5: str = (char_u *)",-"; break;
4404 case 6: str = (char_u *)"[+-]"; break;
4405 case 7: str = (char_u *)",+-"; break;
4406 }
4407 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004408
4409 case STL_HIGHLIGHT:
4410 t = s;
4411 while (*s != '#' && *s != NUL)
4412 ++s;
4413 if (*s == '#')
4414 {
4415 item[curitem].type = Highlight;
4416 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004417 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004418 curitem++;
4419 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004420 if (*s != NUL)
4421 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004422 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424
4425 item[curitem].start = p;
4426 item[curitem].type = Normal;
4427 if (str != NULL && *str)
4428 {
4429 t = str;
4430 if (itemisflag)
4431 {
4432 if ((t[0] && t[1])
4433 && ((!prevchar_isitem && *t == ',')
4434 || (prevchar_isflag && *t == ' ')))
4435 t++;
4436 prevchar_isflag = TRUE;
4437 }
4438 l = vim_strsize(t);
4439 if (l > 0)
4440 prevchar_isitem = TRUE;
4441 if (l > maxwid)
4442 {
4443 while (l >= maxwid)
4444#ifdef FEAT_MBYTE
4445 if (has_mbyte)
4446 {
4447 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004448 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 else
4451#endif
4452 l -= byte2cells(*t++);
4453 if (p + 1 >= out + outlen)
4454 break;
4455 *p++ = '<';
4456 }
4457 if (minwid > 0)
4458 {
4459 for (; l < minwid && p + 1 < out + outlen; l++)
4460 {
4461 /* Don't put a "-" in front of a digit. */
4462 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4463 *p++ = ' ';
4464 else
4465 *p++ = fillchar;
4466 }
4467 minwid = 0;
4468 }
4469 else
4470 minwid *= -1;
4471 while (*t && p + 1 < out + outlen)
4472 {
4473 *p++ = *t++;
4474 /* Change a space by fillchar, unless fillchar is '-' and a
4475 * digit follows. */
4476 if (fillable && p[-1] == ' '
4477 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4478 p[-1] = fillchar;
4479 }
4480 for (; l < minwid && p + 1 < out + outlen; l++)
4481 *p++ = fillchar;
4482 }
4483 else if (num >= 0)
4484 {
4485 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4486 char_u nstr[20];
4487
4488 if (p + 20 >= out + outlen)
4489 break; /* not sufficient space */
4490 prevchar_isitem = TRUE;
4491 t = nstr;
4492 if (opt == STL_VIRTCOL_ALT)
4493 {
4494 *t++ = '-';
4495 minwid--;
4496 }
4497 *t++ = '%';
4498 if (zeropad)
4499 *t++ = '0';
4500 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004501 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 *t = 0;
4503
4504 for (n = num, l = 1; n >= nbase; n /= nbase)
4505 l++;
4506 if (opt == STL_VIRTCOL_ALT)
4507 l++;
4508 if (l > maxwid)
4509 {
4510 l += 2;
4511 n = l - maxwid;
4512 while (l-- > maxwid)
4513 num /= nbase;
4514 *t++ = '>';
4515 *t++ = '%';
4516 *t = t[-3];
4517 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004518 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4519 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004522 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4523 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 p += STRLEN(p);
4525 }
4526 else
4527 item[curitem].type = Empty;
4528
4529 if (opt == STL_VIM_EXPR)
4530 vim_free(str);
4531
4532 if (num >= 0 || (!itemisflag && str && *str))
4533 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4534 curitem++;
4535 }
4536 *p = NUL;
4537 itemcnt = curitem;
4538
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004539#ifdef FEAT_EVAL
4540 if (usefmt != fmt)
4541 vim_free(usefmt);
4542#endif
4543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 width = vim_strsize(out);
4545 if (maxwidth > 0 && width > maxwidth)
4546 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004547 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 l = 0;
4549 if (itemcnt == 0)
4550 s = out;
4551 else
4552 {
4553 for ( ; l < itemcnt; l++)
4554 if (item[l].type == Trunc)
4555 {
4556 /* Truncate at %< item. */
4557 s = item[l].start;
4558 break;
4559 }
4560 if (l == itemcnt)
4561 {
4562 /* No %< item, truncate first item. */
4563 s = item[0].start;
4564 l = 0;
4565 }
4566 }
4567
4568 if (width - vim_strsize(s) >= maxwidth)
4569 {
4570 /* Truncation mark is beyond max length */
4571#ifdef FEAT_MBYTE
4572 if (has_mbyte)
4573 {
4574 s = out;
4575 width = 0;
4576 for (;;)
4577 {
4578 width += ptr2cells(s);
4579 if (width >= maxwidth)
4580 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004581 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 /* Fill up for half a double-wide character. */
4584 while (++width < maxwidth)
4585 *s++ = fillchar;
4586 }
4587 else
4588#endif
4589 s = out + maxwidth - 1;
4590 for (l = 0; l < itemcnt; l++)
4591 if (item[l].start > s)
4592 break;
4593 itemcnt = l;
4594 *s++ = '>';
4595 *s = 0;
4596 }
4597 else
4598 {
4599#ifdef FEAT_MBYTE
4600 if (has_mbyte)
4601 {
4602 n = 0;
4603 while (width >= maxwidth)
4604 {
4605 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004606 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 }
4609 else
4610#endif
4611 n = width - maxwidth + 1;
4612 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004613 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 *s = '<';
4615
4616 /* Fill up for half a double-wide character. */
4617 while (++width < maxwidth)
4618 {
4619 s = s + STRLEN(s);
4620 *s++ = fillchar;
4621 *s = NUL;
4622 }
4623
4624 --n; /* count the '<' */
4625 for (; l < itemcnt; l++)
4626 {
4627 if (item[l].start - n >= s)
4628 item[l].start -= n;
4629 else
4630 item[l].start = s;
4631 }
4632 }
4633 width = maxwidth;
4634 }
4635 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4636 {
4637 /* Apply STL_MIDDLE if any */
4638 for (l = 0; l < itemcnt; l++)
4639 if (item[l].type == Middle)
4640 break;
4641 if (l < itemcnt)
4642 {
4643 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004644 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 for (s = item[l].start; s < p; s++)
4646 *s = fillchar;
4647 for (l++; l < itemcnt; l++)
4648 item[l].start += maxwidth - width;
4649 width = maxwidth;
4650 }
4651 }
4652
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004653 /* Store the info about highlighting. */
4654 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004656 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 for (l = 0; l < itemcnt; l++)
4658 {
4659 if (item[l].type == Highlight)
4660 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004661 sp->start = item[l].start;
4662 sp->userhl = item[l].minwid;
4663 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004666 sp->start = NULL;
4667 sp->userhl = 0;
4668 }
4669
4670 /* Store the info about tab pages labels. */
4671 if (tabtab != NULL)
4672 {
4673 sp = tabtab;
4674 for (l = 0; l < itemcnt; l++)
4675 {
4676 if (item[l].type == TabPage)
4677 {
4678 sp->start = item[l].start;
4679 sp->userhl = item[l].minwid;
4680 sp++;
4681 }
4682 }
4683 sp->start = NULL;
4684 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004687 /* When inside update_screen we do not want redrawing a stausline, ruler,
4688 * title, etc. to trigger another redraw, it may cause an endless loop. */
4689 if (updating_screen)
4690 {
4691 must_redraw = save_must_redraw;
4692 curwin->w_redr_type = save_redr_type;
4693 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004694
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 return width;
4696}
4697#endif /* FEAT_STL_OPT */
4698
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004699#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4700 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004702 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4703 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 */
4705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004706get_rel_pos(
4707 win_T *wp,
4708 char_u *buf,
4709 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
4711 long above; /* number of lines above window */
4712 long below; /* number of lines below window */
4713
Bram Moolenaar0027c212015-01-07 13:31:52 +01004714 if (buflen < 3) /* need at least 3 chars for writing */
4715 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 above = wp->w_topline - 1;
4717#ifdef FEAT_DIFF
4718 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004719 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4720 above = 0; /* All buffer lines are displayed and there is an
4721 * indication of filler lines, that can be considered
4722 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723#endif
4724 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4725 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004726 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4727 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004729 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004731 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 ? (int)(above / ((above + below) / 100L))
4733 : (int)(above * 100L / (above + below)));
4734}
4735#endif
4736
4737/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004738 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 * Return TRUE if it was appended.
4740 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004741 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004742append_arg_number(
4743 win_T *wp,
4744 char_u *buf,
4745 int buflen,
4746 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 char_u *p;
4749
4750 if (ARGCOUNT <= 1) /* nothing to do */
4751 return FALSE;
4752
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004753 p = buf + STRLEN(buf); /* go to the end of the buffer */
4754 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 return FALSE;
4756 *p++ = ' ';
4757 *p++ = '(';
4758 if (add_file)
4759 {
4760 STRCPY(p, "file ");
4761 p += 5;
4762 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004763 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4764 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4766 return TRUE;
4767}
4768
4769/*
4770 * If fname is not a full path, make it a full path.
4771 * Returns pointer to allocated memory (NULL for failure).
4772 */
4773 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004774fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 /*
4777 * Force expanding the path always for Unix, because symbolic links may
4778 * mess up the full path name, even though it starts with a '/'.
4779 * Also expand when there is ".." in the file name, try to remove it,
4780 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004781 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 * For MS-Windows also expand names like "longna~1" to "longname".
4783 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004784#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 return FullName_save(fname, TRUE);
4786#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004787 if (!vim_isAbsName(fname)
4788 || strstr((char *)fname, "..") != NULL
4789 || strstr((char *)fname, "//") != NULL
4790# ifdef BACKSLASH_IN_FILENAME
4791 || strstr((char *)fname, "\\\\") != NULL
4792# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004793# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 )
4797 return FullName_save(fname, FALSE);
4798
4799 fname = vim_strsave(fname);
4800
Bram Moolenaar9b942202007-10-03 12:31:33 +00004801# ifdef USE_FNAME_CASE
4802# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 if (fname != NULL)
4807 fname_case(fname, 0); /* set correct case for file name */
4808 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
4811 return fname;
4812#endif
4813}
4814
4815/*
4816 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4817 * "ffname" becomes a pointer to allocated memory (or NULL).
4818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004820fname_expand(
4821 buf_T *buf UNUSED,
4822 char_u **ffname,
4823 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824{
4825 if (*ffname == NULL) /* if no file name given, nothing to do */
4826 return;
4827 if (*sfname == NULL) /* if no short file name given, use ffname */
4828 *sfname = *ffname;
4829 *ffname = fix_fname(*ffname); /* expand to full path */
4830
4831#ifdef FEAT_SHORTCUT
4832 if (!buf->b_p_bin)
4833 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004834 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
4836 /* If the file name is a shortcut file, use the file it links to. */
4837 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004838 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
4840 vim_free(*ffname);
4841 *ffname = rfname;
4842 *sfname = rfname;
4843 }
4844 }
4845#endif
4846}
4847
4848/*
4849 * Get the file name for an argument list entry.
4850 */
4851 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004852alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 buf_T *bp;
4855
4856 /* Use the name from the associated buffer if it exists. */
4857 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004858 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 return aep->ae_fname;
4860 return bp->b_fname;
4861}
4862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863/*
4864 * do_arg_all(): Open up to 'count' windows, one for each argument.
4865 */
4866 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004867do_arg_all(
4868 int count,
4869 int forceit, /* hide buffers in current windows */
4870 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
4872 int i;
4873 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004874 char_u *opened; /* Array of weight for which args are open:
4875 * 0: not opened
4876 * 1: opened in other tab
4877 * 2: opened in curtab
4878 * 3: opened in curtab and curwin
4879 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004880 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 int use_firstwin = FALSE; /* use first window for arglist */
4882 int split_ret = OK;
4883 int p_ea_save;
4884 alist_T *alist; /* argument list to be used */
4885 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004886 tabpage_T *tpnext;
4887 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004888 win_T *old_curwin, *last_curwin;
4889 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004890 win_T *new_curwin = NULL;
4891 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 if (ARGCOUNT <= 0)
4894 {
4895 /* Don't give an error message. We don't want it when the ":all"
4896 * command is in the .vimrc. */
4897 return;
4898 }
4899 setpcmark();
4900
4901 opened_len = ARGCOUNT;
4902 opened = alloc_clear((unsigned)opened_len);
4903 if (opened == NULL)
4904 return;
4905
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004906 /* Autocommands may do anything to the argument list. Make sure it's not
4907 * freed while we are working here by "locking" it. We still have to
4908 * watch out for its size to be changed. */
4909 alist = curwin->w_alist;
4910 ++alist->al_refcount;
4911
4912 old_curwin = curwin;
4913 old_curtab = curtab;
4914
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004915# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919 /*
4920 * Try closing all windows that are not in the argument list.
4921 * Also close windows that are not full width;
4922 * When 'hidden' or "forceit" set the buffer becomes hidden.
4923 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004924 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004926 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004927 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004928 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004930 tpnext = curtab->tp_next;
4931 for (wp = firstwin; wp != NULL; wp = wpnext)
4932 {
4933 wpnext = wp->w_next;
4934 buf = wp->w_buffer;
4935 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004936 || (!keep_tabs && (buf->b_nwindows > 1
4937 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004938 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004939 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004941 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004942 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004944 if (i < alist->al_ga.ga_len
4945 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4946 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4947 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004949 int weight = 1;
4950
4951 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004952 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004953 ++weight;
4954 if (old_curwin == wp)
4955 ++weight;
4956 }
4957
4958 if (weight > (int)opened[i])
4959 {
4960 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004961 if (i == 0)
4962 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004963 if (new_curwin != NULL)
4964 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004965 new_curwin = wp;
4966 new_curtab = curtab;
4967 }
4968 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004969 else if (keep_tabs)
4970 i = opened_len;
4971
4972 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004973 {
4974 /* Use the current argument list for all windows
4975 * containing a file from it. */
4976 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004977 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004978 ++wp->w_alist->al_refcount;
4979 }
4980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004984 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004986 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004988 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004989 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004991 /* If the buffer was changed, and we would like to hide it,
4992 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004993 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004994 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004996#ifdef FEAT_AUTOCMD
4997 bufref_T bufref;
4998
4999 set_bufref(&bufref, buf);
5000#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005001 (void)autowrite(buf, FALSE);
5002#ifdef FEAT_AUTOCMD
5003 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005004 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005005 {
5006 wpnext = firstwin; /* start all over... */
5007 continue;
5008 }
5009#endif
5010 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005011 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005012 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005013 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005014 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005015 else
5016 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005017 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaar4033c552017-09-16 20:54:51 +02005018#ifdef FEAT_AUTOCMD
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005019 /* check if autocommands removed the next window */
5020 if (!win_valid(wpnext))
5021 wpnext = firstwin; /* start all over... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005027
5028 /* Without the ":tab" modifier only do the current tab page. */
5029 if (had_tab == 0 || tpnext == NULL)
5030 break;
5031
5032# ifdef FEAT_AUTOCMD
5033 /* check if autocommands removed the next tab page */
5034 if (!valid_tabpage(tpnext))
5035 tpnext = first_tabpage; /* start all over...*/
5036# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005037 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039
5040 /*
5041 * Open a window for files in the argument list that don't have one.
5042 * ARGCOUNT may change while doing this, because of autocommands.
5043 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005044 if (count > opened_len || count <= 0)
5045 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046
5047#ifdef FEAT_AUTOCMD
5048 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5049 ++autocmd_no_enter;
5050 ++autocmd_no_leave;
5051#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005052 last_curwin = curwin;
5053 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005055 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5056 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005057 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005058 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5059 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005060
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005061 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5064 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005065 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 /* Move the already present window to below the current window */
5068 if (curwin->w_arg_idx != i)
5069 {
5070 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5071 {
5072 if (wpnext->w_arg_idx == i)
5073 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005074 if (keep_tabs)
5075 {
5076 new_curwin = wpnext;
5077 new_curtab = curtab;
5078 }
5079 else
5080 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 break;
5082 }
5083 }
5084 }
5085 }
5086 else if (split_ret == OK)
5087 {
5088 if (!use_firstwin) /* split current window */
5089 {
5090 p_ea_save = p_ea;
5091 p_ea = TRUE; /* use space from all windows */
5092 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5093 p_ea = p_ea_save;
5094 if (split_ret == FAIL)
5095 continue;
5096 }
5097#ifdef FEAT_AUTOCMD
5098 else /* first window: do autocmd for leaving this buffer */
5099 --autocmd_no_leave;
5100#endif
5101
5102 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005103 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 */
5105 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005106 if (i == 0)
5107 {
5108 new_curwin = curwin;
5109 new_curtab = curtab;
5110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5112 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005113 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005115 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116#ifdef FEAT_AUTOCMD
5117 if (use_firstwin)
5118 ++autocmd_no_leave;
5119#endif
5120 use_firstwin = FALSE;
5121 }
5122 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005123
5124 /* When ":tab" was used open a new tab for a new window repeatedly. */
5125 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5126 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128
5129 /* Remove the "lock" on the argument list. */
5130 alist_unlink(alist);
5131
5132#ifdef FEAT_AUTOCMD
5133 --autocmd_no_enter;
5134#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005135 /* restore last referenced tabpage's curwin */
5136 if (last_curtab != new_curtab)
5137 {
5138 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005139 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005140 if (win_valid(last_curwin))
5141 win_enter(last_curwin, FALSE);
5142 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005143 /* to window with first arg */
5144 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005145 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005146 if (win_valid(new_curwin))
5147 win_enter(new_curwin, FALSE);
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149#ifdef FEAT_AUTOCMD
5150 --autocmd_no_leave;
5151#endif
5152 vim_free(opened);
5153}
5154
5155# if defined(FEAT_LISTCMDS) || defined(PROTO)
5156/*
5157 * Open a window for a number of buffers.
5158 */
5159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005160ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162 buf_T *buf;
5163 win_T *wp, *wpnext;
5164 int split_ret = OK;
5165 int p_ea_save;
5166 int open_wins = 0;
5167 int r;
5168 int count; /* Maximum number of windows to open. */
5169 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005170 int had_tab = cmdmod.tab;
5171 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173 if (eap->addr_count == 0) /* make as many windows as possible */
5174 count = 9999;
5175 else
5176 count = eap->line2; /* make as many windows as specified */
5177 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5178 all = FALSE;
5179 else
5180 all = TRUE;
5181
5182 setpcmark();
5183
5184#ifdef FEAT_GUI
5185 need_mouse_correct = TRUE;
5186#endif
5187
5188 /*
5189 * Close superfluous windows (two windows for the same buffer).
5190 * Also close windows that are not full-width.
5191 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005192 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005193 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005194 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005196 tpnext = curtab->tp_next;
5197 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005199 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005200 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005201 || ((cmdmod.split & WSP_VERT)
5202 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005203 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005204 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005205 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaar362ce482012-06-06 19:02:45 +02005206#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005207 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005208#endif
5209 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005210 {
5211 win_close(wp, FALSE);
5212#ifdef FEAT_AUTOCMD
5213 wpnext = firstwin; /* just in case an autocommand does
5214 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005215 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005216 open_wins = 0;
5217#endif
5218 }
5219 else
5220 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005222
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005223 /* Without the ":tab" modifier only do the current tab page. */
5224 if (had_tab == 0 || tpnext == NULL)
5225 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005226 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
5228
5229 /*
5230 * Go through the buffer list. When a buffer doesn't have a window yet,
5231 * open one. Otherwise move the window to the right position.
5232 * Watch out for autocommands that delete buffers or windows!
5233 */
5234#ifdef FEAT_AUTOCMD
5235 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5236 ++autocmd_no_enter;
5237#endif
5238 win_enter(lastwin, FALSE);
5239#ifdef FEAT_AUTOCMD
5240 ++autocmd_no_leave;
5241#endif
5242 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5243 {
5244 /* Check if this buffer needs a window */
5245 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5246 continue;
5247
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005248 if (had_tab != 0)
5249 {
5250 /* With the ":tab" modifier don't move the window. */
5251 if (buf->b_nwindows > 0)
5252 wp = lastwin; /* buffer has a window, skip it */
5253 else
5254 wp = NULL;
5255 }
5256 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005257 {
5258 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005259 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005260 if (wp->w_buffer == buf)
5261 break;
5262 /* If the buffer already has a window, move it */
5263 if (wp != NULL)
5264 win_move_after(wp, curwin);
5265 }
5266
5267 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005269#ifdef FEAT_AUTOCMD
5270 bufref_T bufref;
5271
5272 set_bufref(&bufref, buf);
5273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 /* Split the window and put the buffer in it */
5275 p_ea_save = p_ea;
5276 p_ea = TRUE; /* use space from all windows */
5277 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5278 ++open_wins;
5279 p_ea = p_ea_save;
5280 if (split_ret == FAIL)
5281 continue;
5282
5283 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005284#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 swap_exists_action = SEA_DIALOG;
5286#endif
5287 set_curbuf(buf, DOBUF_GOTO);
5288#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005289 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005291 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005292#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 break;
5296 }
5297#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005298#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (swap_exists_action == SEA_QUIT)
5300 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005301# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5302 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005303
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005304 /* Reset the error/interrupt/exception state here so that
5305 * aborting() returns FALSE when closing a window. */
5306 enter_cleanup(&cs);
5307# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005308
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005309 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 win_close(curwin, TRUE);
5311 --open_wins;
5312 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005313 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005314
5315# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5316 /* Restore the error/interrupt/exception state if not
5317 * discarded by a new aborting error, interrupt, or uncaught
5318 * exception. */
5319 leave_cleanup(&cs);
5320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
5322 else
5323 handle_swap_exists(NULL);
5324#endif
5325 }
5326
5327 ui_breakcheck();
5328 if (got_int)
5329 {
5330 (void)vgetc(); /* only break the file loading, not the rest */
5331 break;
5332 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005333#ifdef FEAT_EVAL
5334 /* Autocommands deleted the buffer or aborted script processing!!! */
5335 if (aborting())
5336 break;
5337#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005338 /* When ":tab" was used open a new tab for a new window repeatedly. */
5339 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5340 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342#ifdef FEAT_AUTOCMD
5343 --autocmd_no_enter;
5344#endif
5345 win_enter(firstwin, FALSE); /* back to first window */
5346#ifdef FEAT_AUTOCMD
5347 --autocmd_no_leave;
5348#endif
5349
5350 /*
5351 * Close superfluous windows.
5352 */
5353 for (wp = lastwin; open_wins > count; )
5354 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005355 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 || autowrite(wp->w_buffer, FALSE) == OK);
5357#ifdef FEAT_AUTOCMD
5358 if (!win_valid(wp))
5359 {
5360 /* BufWrite Autocommands made the window invalid, start over */
5361 wp = lastwin;
5362 }
5363 else
5364#endif
5365 if (r)
5366 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005367 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 --open_wins;
5369 wp = lastwin;
5370 }
5371 else
5372 {
5373 wp = wp->w_prev;
5374 if (wp == NULL)
5375 break;
5376 }
5377 }
5378}
5379# endif /* FEAT_LISTCMDS */
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005382static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005383
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384/*
5385 * do_modelines() - process mode lines for the current file
5386 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005387 * "flags" can be:
5388 * OPT_WINONLY only set options local to window
5389 * OPT_NOWIN don't set options local to window
5390 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 * Returns immediately if the "ml" option isn't set.
5392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005394do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005396 linenr_T lnum;
5397 int nmlines;
5398 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
5400 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5401 return;
5402
5403 /* Disallow recursive entry here. Can happen when executing a modeline
5404 * triggers an autocommand, which reloads modelines with a ":do". */
5405 if (entered)
5406 return;
5407
5408 ++entered;
5409 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5410 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005411 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 nmlines = 0;
5413
5414 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5415 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005416 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 nmlines = 0;
5418 --entered;
5419}
5420
5421#include "version.h" /* for version number */
5422
5423/*
5424 * chk_modeline() - check a single line for a mode string
5425 * Return FAIL if an error encountered.
5426 */
5427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005428chk_modeline(
5429 linenr_T lnum,
5430 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 char_u *s;
5433 char_u *e;
5434 char_u *linecopy; /* local copy of any modeline found */
5435 int prev;
5436 int vers;
5437 int end;
5438 int retval = OK;
5439 char_u *save_sourcing_name;
5440 linenr_T save_sourcing_lnum;
5441#ifdef FEAT_EVAL
5442 scid_T save_SID;
5443#endif
5444
5445 prev = -1;
5446 for (s = ml_get(lnum); *s != NUL; ++s)
5447 {
5448 if (prev == -1 || vim_isspace(prev))
5449 {
5450 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5451 || STRNCMP(s, "vi:", (size_t)3) == 0)
5452 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005453 /* Accept both "vim" and "Vim". */
5454 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
5456 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5457 e = s + 4;
5458 else
5459 e = s + 3;
5460 vers = getdigits(&e);
5461 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005462 && (s[0] != 'V'
5463 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 && (s[3] == ':'
5465 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5466 || (VIM_VERSION_100 < vers && s[3] == '<')
5467 || (VIM_VERSION_100 > vers && s[3] == '>')
5468 || (VIM_VERSION_100 == vers && s[3] == '=')))
5469 break;
5470 }
5471 }
5472 prev = *s;
5473 }
5474
5475 if (*s)
5476 {
5477 do /* skip over "ex:", "vi:" or "vim:" */
5478 ++s;
5479 while (s[-1] != ':');
5480
5481 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5482 if (linecopy == NULL)
5483 return FAIL;
5484
5485 save_sourcing_lnum = sourcing_lnum;
5486 save_sourcing_name = sourcing_name;
5487 sourcing_lnum = lnum; /* prepare for emsg() */
5488 sourcing_name = (char_u *)"modelines";
5489
5490 end = FALSE;
5491 while (end == FALSE)
5492 {
5493 s = skipwhite(s);
5494 if (*s == NUL)
5495 break;
5496
5497 /*
5498 * Find end of set command: ':' or end of line.
5499 * Skip over "\:", replacing it with ":".
5500 */
5501 for (e = s; *e != ':' && *e != NUL; ++e)
5502 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005503 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if (*e == NUL)
5505 end = TRUE;
5506
5507 /*
5508 * If there is a "set" command, require a terminating ':' and
5509 * ignore the stuff after the ':'.
5510 * "vi:set opt opt opt: foo" -- foo not interpreted
5511 * "vi:opt opt opt: foo" -- foo interpreted
5512 * Accept "se" for compatibility with Elvis.
5513 */
5514 if (STRNCMP(s, "set ", (size_t)4) == 0
5515 || STRNCMP(s, "se ", (size_t)3) == 0)
5516 {
5517 if (*e != ':') /* no terminating ':'? */
5518 break;
5519 end = TRUE;
5520 s = vim_strchr(s, ' ') + 1;
5521 }
5522 *e = NUL; /* truncate the set command */
5523
5524 if (*s != NUL) /* skip over an empty "::" */
5525 {
5526#ifdef FEAT_EVAL
5527 save_SID = current_SID;
5528 current_SID = SID_MODELINE;
5529#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005530 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531#ifdef FEAT_EVAL
5532 current_SID = save_SID;
5533#endif
5534 if (retval == FAIL) /* stop if error found */
5535 break;
5536 }
5537 s = e + 1; /* advance to next part */
5538 }
5539
5540 sourcing_lnum = save_sourcing_lnum;
5541 sourcing_name = save_sourcing_name;
5542
5543 vim_free(linecopy);
5544 }
5545 return retval;
5546}
5547
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005548#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005550read_viminfo_bufferlist(
5551 vir_T *virp,
5552 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 char_u *tab;
5555 linenr_T lnum;
5556 colnr_T col;
5557 buf_T *buf;
5558 char_u *sfname;
5559 char_u *xline;
5560
5561 /* Handle long line and escaped characters. */
5562 xline = viminfo_readstring(virp, 1, FALSE);
5563
5564 /* don't read in if there are files on the command-line or if writing: */
5565 if (xline != NULL && !writing && ARGCOUNT == 0
5566 && find_viminfo_parameter('%') != NULL)
5567 {
5568 /* Format is: <fname> Tab <lnum> Tab <col>.
5569 * Watch out for a Tab in the file name, work from the end. */
5570 lnum = 0;
5571 col = 0;
5572 tab = vim_strrchr(xline, '\t');
5573 if (tab != NULL)
5574 {
5575 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005576 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 tab = vim_strrchr(xline, '\t');
5578 if (tab != NULL)
5579 {
5580 *tab++ = '\0';
5581 lnum = atol((char *)tab);
5582 }
5583 }
5584
5585 /* Expand "~/" in the file name at "line + 1" to a full path.
5586 * Then try shortening it by comparing with the current directory */
5587 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005588 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
5590 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5591 if (buf != NULL) /* just in case... */
5592 {
5593 buf->b_last_cursor.lnum = lnum;
5594 buf->b_last_cursor.col = col;
5595 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5596 }
5597 }
5598 vim_free(xline);
5599
5600 return viminfo_readline(virp);
5601}
5602
5603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005604write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
5606 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005608 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005610 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 if (find_viminfo_parameter('%') == NULL)
5613 return;
5614
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005615 /* Without a number -1 is returned: do all buffers. */
5616 max_buffers = get_viminfo_parameter('%');
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005619#define LINE_BUF_LEN (MAXPATHL + 40)
5620 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 if (line == NULL)
5622 return;
5623
Bram Moolenaarf740b292006-02-16 22:11:02 +00005624 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005627 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005628 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 {
5630 if (buf->b_fname == NULL
5631 || !buf->b_p_bl
5632#ifdef FEAT_QUICKFIX
5633 || bt_quickfix(buf)
5634#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005635#ifdef FEAT_TERMINAL
5636 || bt_terminal(buf)
5637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 || removable(buf->b_ffname))
5639 continue;
5640
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005641 if (max_buffers-- == 0)
5642 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 putc('%', fp);
5644 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005645 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 (long)buf->b_last_cursor.lnum,
5647 buf->b_last_cursor.col);
5648 viminfo_writestring(fp, line);
5649 }
5650 vim_free(line);
5651}
5652#endif
5653
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005654/*
5655 * Return TRUE if "buf" is the quickfix buffer.
5656 */
5657 int
5658bt_quickfix(buf_T *buf)
5659{
5660 return buf != NULL && buf->b_p_bt[0] == 'q';
5661}
5662
5663/*
5664 * Return TRUE if "buf" is a terminal buffer.
5665 */
5666 int
5667bt_terminal(buf_T *buf)
5668{
5669 return buf != NULL && buf->b_p_bt[0] == 't';
5670}
5671
5672/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005673 * Return TRUE if "buf" is a help buffer.
5674 */
5675 int
5676bt_help(buf_T *buf)
5677{
5678 return buf != NULL && buf->b_help;
5679}
5680
5681/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005682 * Return TRUE if "buf" is a "nofile", "acwrite" or "terminal" buffer.
5683 * This means the buffer name is not a file name.
5684 */
5685 int
5686bt_nofile(buf_T *buf)
5687{
5688 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5689 || buf->b_p_bt[0] == 'a'
5690 || buf->b_p_bt[0] == 't');
5691}
5692
5693/*
5694 * Return TRUE if "buf" is a "nowrite", "nofile" or "terminal" buffer.
5695 */
5696 int
5697bt_dontwrite(buf_T *buf)
5698{
5699 return buf != NULL && (buf->b_p_bt[0] == 'n' || buf->b_p_bt[0] == 't');
5700}
5701
5702 int
5703bt_dontwrite_msg(buf_T *buf)
5704{
5705 if (bt_dontwrite(buf))
5706 {
5707 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5708 return TRUE;
5709 }
5710 return FALSE;
5711}
5712
5713/*
5714 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5715 * and 'bufhidden'.
5716 */
5717 int
5718buf_hide(buf_T *buf)
5719{
5720 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5721 switch (buf->b_p_bh[0])
5722 {
5723 case 'u': /* "unload" */
5724 case 'w': /* "wipe" */
5725 case 'd': return FALSE; /* "delete" */
5726 case 'h': return TRUE; /* "hide" */
5727 }
5728 return (p_hid || cmdmod.hide);
5729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
5731/*
5732 * Return special buffer name.
5733 * Returns NULL when the buffer has a normal file name.
5734 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005735 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005736buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005738#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005740 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005741 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005742 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005743
5744 /*
5745 * For location list window, w_llist_ref points to the location list.
5746 * For quickfix window, w_llist_ref is NULL.
5747 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005748 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005749 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005750 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005751 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005756 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 if (bt_nofile(buf))
5758 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005759#ifdef FEAT_TERMINAL
5760 if (buf->b_term != NULL)
5761 return term_get_status_text(buf->b_term);
5762#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005763 if (buf->b_fname != NULL)
5764 return buf->b_fname;
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005765 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005767
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005769 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 return NULL;
5771}
5772
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005773#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005774 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5775 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005776# define SWITCH_TO_WIN
5777
5778/*
5779 * Find a window that contains "buf" and switch to it.
5780 * If there is no such window, use the current window and change "curbuf".
5781 * Caller must initialize save_curbuf to NULL.
5782 * restore_win_for_buf() MUST be called later!
5783 */
5784 void
5785switch_to_win_for_buf(
5786 buf_T *buf,
5787 win_T **save_curwinp,
5788 tabpage_T **save_curtabp,
5789 bufref_T *save_curbuf)
5790{
5791 win_T *wp;
5792 tabpage_T *tp;
5793
5794 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5795 switch_buffer(save_curbuf, buf);
5796 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5797 {
5798 restore_win(*save_curwinp, *save_curtabp, TRUE);
5799 switch_buffer(save_curbuf, buf);
5800 }
5801}
5802
5803 void
5804restore_win_for_buf(
5805 win_T *save_curwin,
5806 tabpage_T *save_curtab,
5807 bufref_T *save_curbuf)
5808{
5809 if (save_curbuf->br_buf == NULL)
5810 restore_win(save_curwin, save_curtab, TRUE);
5811 else
5812 restore_buffer(save_curbuf);
5813}
5814#endif
5815
Bram Moolenaar4033c552017-09-16 20:54:51 +02005816#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005817/*
5818 * Find a window for buffer "buf".
5819 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5820 * If not found FAIL is returned.
5821 */
5822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005823find_win_for_buf(
5824 buf_T *buf,
5825 win_T **wp,
5826 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005827{
5828 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5829 if ((*wp)->w_buffer == buf)
5830 goto win_found;
5831 return FAIL;
5832win_found:
5833 return OK;
5834}
5835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
5837#if defined(FEAT_SIGNS) || defined(PROTO)
5838/*
5839 * Insert the sign into the signlist.
5840 */
5841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005842insert_sign(
5843 buf_T *buf, /* buffer to store sign in */
5844 signlist_T *prev, /* previous sign entry */
5845 signlist_T *next, /* next sign entry */
5846 int id, /* sign ID */
5847 linenr_T lnum, /* line number which gets the mark */
5848 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 signlist_T *newsign;
5851
5852 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5853 if (newsign != NULL)
5854 {
5855 newsign->id = id;
5856 newsign->lnum = lnum;
5857 newsign->typenr = typenr;
5858 newsign->next = next;
5859#ifdef FEAT_NETBEANS_INTG
5860 newsign->prev = prev;
5861 if (next != NULL)
5862 next->prev = newsign;
5863#endif
5864
5865 if (prev == NULL)
5866 {
5867 /* When adding first sign need to redraw the windows to create the
5868 * column for signs. */
5869 if (buf->b_signlist == NULL)
5870 {
5871 redraw_buf_later(buf, NOT_VALID);
5872 changed_cline_bef_curs();
5873 }
5874
5875 /* first sign in signlist */
5876 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005877#ifdef FEAT_NETBEANS_INTG
5878 if (netbeans_active())
5879 buf->b_has_sign_column = TRUE;
5880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 }
5882 else
5883 prev->next = newsign;
5884 }
5885}
5886
5887/*
5888 * Add the sign into the signlist. Find the right spot to do it though.
5889 */
5890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005891buf_addsign(
5892 buf_T *buf, /* buffer to store sign in */
5893 int id, /* sign ID */
5894 linenr_T lnum, /* line number which gets the mark */
5895 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 signlist_T *sign; /* a sign in the signlist */
5898 signlist_T *prev; /* the previous sign */
5899
5900 prev = NULL;
5901 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5902 {
5903 if (lnum == sign->lnum && id == sign->id)
5904 {
5905 sign->typenr = typenr;
5906 return;
5907 }
5908 else if (
5909#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5910 id < 0 &&
5911#endif
5912 lnum < sign->lnum)
5913 {
5914#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5915 /* XXX - GRP: Is this because of sign slide problem? Or is it
5916 * really needed? Or is it because we allow multiple signs per
5917 * line? If so, should I add that feature to FEAT_SIGNS?
5918 */
5919 while (prev != NULL && prev->lnum == lnum)
5920 prev = prev->prev;
5921 if (prev == NULL)
5922 sign = buf->b_signlist;
5923 else
5924 sign = prev->next;
5925#endif
5926 insert_sign(buf, prev, sign, id, lnum, typenr);
5927 return;
5928 }
5929 prev = sign;
5930 }
5931#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5932 /* XXX - GRP: See previous comment */
5933 while (prev != NULL && prev->lnum == lnum)
5934 prev = prev->prev;
5935 if (prev == NULL)
5936 sign = buf->b_signlist;
5937 else
5938 sign = prev->next;
5939#endif
5940 insert_sign(buf, prev, sign, id, lnum, typenr);
5941
5942 return;
5943}
5944
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005945/*
5946 * For an existing, placed sign "markId" change the type to "typenr".
5947 * Returns the line number of the sign, or zero if the sign is not found.
5948 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005949 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005950buf_change_sign_type(
5951 buf_T *buf, /* buffer to store sign in */
5952 int markId, /* sign ID */
5953 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
5955 signlist_T *sign; /* a sign in the signlist */
5956
5957 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5958 {
5959 if (sign->id == markId)
5960 {
5961 sign->typenr = typenr;
5962 return sign->lnum;
5963 }
5964 }
5965
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005966 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967}
5968
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005970buf_getsigntype(
5971 buf_T *buf,
5972 linenr_T lnum,
5973 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 signlist_T *sign; /* a sign in a b_signlist */
5976
5977 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5978 if (sign->lnum == lnum
5979 && (type == SIGN_ANY
5980# ifdef FEAT_SIGN_ICONS
5981 || (type == SIGN_ICON
5982 && sign_get_image(sign->typenr) != NULL)
5983# endif
5984 || (type == SIGN_TEXT
5985 && sign_get_text(sign->typenr) != NULL)
5986 || (type == SIGN_LINEHL
5987 && sign_get_attr(sign->typenr, TRUE) != 0)))
5988 return sign->typenr;
5989 return 0;
5990}
5991
5992
5993 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005994buf_delsign(
5995 buf_T *buf, /* buffer sign is stored in */
5996 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
5998 signlist_T **lastp; /* pointer to pointer to current sign */
5999 signlist_T *sign; /* a sign in a b_signlist */
6000 signlist_T *next; /* the next sign in a b_signlist */
6001 linenr_T lnum; /* line number whose sign was deleted */
6002
6003 lastp = &buf->b_signlist;
6004 lnum = 0;
6005 for (sign = buf->b_signlist; sign != NULL; sign = next)
6006 {
6007 next = sign->next;
6008 if (sign->id == id)
6009 {
6010 *lastp = next;
6011#ifdef FEAT_NETBEANS_INTG
6012 if (next != NULL)
6013 next->prev = sign->prev;
6014#endif
6015 lnum = sign->lnum;
6016 vim_free(sign);
6017 break;
6018 }
6019 else
6020 lastp = &sign->next;
6021 }
6022
6023 /* When deleted the last sign need to redraw the windows to remove the
6024 * sign column. */
6025 if (buf->b_signlist == NULL)
6026 {
6027 redraw_buf_later(buf, NOT_VALID);
6028 changed_cline_bef_curs();
6029 }
6030
6031 return lnum;
6032}
6033
6034
6035/*
6036 * Find the line number of the sign with the requested id. If the sign does
6037 * not exist, return 0 as the line number. This will still let the correct file
6038 * get loaded.
6039 */
6040 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006041buf_findsign(
6042 buf_T *buf, /* buffer to store sign in */
6043 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 signlist_T *sign; /* a sign in the signlist */
6046
6047 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6048 if (sign->id == id)
6049 return sign->lnum;
6050
6051 return 0;
6052}
6053
6054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006055buf_findsign_id(
6056 buf_T *buf, /* buffer whose sign we are searching for */
6057 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 signlist_T *sign; /* a sign in the signlist */
6060
6061 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6062 if (sign->lnum == lnum)
6063 return sign->id;
6064
6065 return 0;
6066}
6067
6068
6069# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
6070/* see if a given type of sign exists on a specific line */
6071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006072buf_findsigntype_id(
6073 buf_T *buf, /* buffer whose sign we are searching for */
6074 linenr_T lnum, /* line number of sign */
6075 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 signlist_T *sign; /* a sign in the signlist */
6078
6079 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6080 if (sign->lnum == lnum && sign->typenr == typenr)
6081 return sign->id;
6082
6083 return 0;
6084}
6085
6086
6087# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6088/* return the number of icons on the given line */
6089 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006090buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 signlist_T *sign; /* a sign in the signlist */
6093 int count = 0;
6094
6095 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6096 if (sign->lnum == lnum)
6097 if (sign_get_image(sign->typenr) != NULL)
6098 count++;
6099
6100 return count;
6101}
6102# endif /* FEAT_SIGN_ICONS */
6103# endif /* FEAT_NETBEANS_INTG */
6104
6105
6106/*
6107 * Delete signs in buffer "buf".
6108 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006110buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
6112 signlist_T *next;
6113
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006114 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006115 * sign column. Not when curwin is NULL (this means we're exiting). */
6116 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006117 {
6118 redraw_buf_later(buf, NOT_VALID);
6119 changed_cline_bef_curs();
6120 }
6121
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 while (buf->b_signlist != NULL)
6123 {
6124 next = buf->b_signlist->next;
6125 vim_free(buf->b_signlist);
6126 buf->b_signlist = next;
6127 }
6128}
6129
6130/*
6131 * Delete all signs in all buffers.
6132 */
6133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006134buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
6136 buf_T *buf; /* buffer we are checking for signs */
6137
Bram Moolenaar29323592016-07-24 22:04:11 +02006138 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141}
6142
6143/*
6144 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6145 */
6146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006147sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 buf_T *buf;
6150 signlist_T *p;
6151 char lbuf[BUFSIZ];
6152
6153 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6154 msg_putchar('\n');
6155 if (rbuf == NULL)
6156 buf = firstbuf;
6157 else
6158 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006159 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {
6161 if (buf->b_signlist != NULL)
6162 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006163 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006164 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 msg_putchar('\n');
6166 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006167 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006169 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6171 MSG_PUTS(lbuf);
6172 msg_putchar('\n');
6173 }
6174 if (rbuf != NULL)
6175 break;
6176 buf = buf->b_next;
6177 }
6178}
6179
6180/*
6181 * Adjust a placed sign for inserted/deleted lines.
6182 */
6183 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006184sign_mark_adjust(
6185 linenr_T line1,
6186 linenr_T line2,
6187 long amount,
6188 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189{
6190 signlist_T *sign; /* a sign in a b_signlist */
6191
6192 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6193 {
6194 if (sign->lnum >= line1 && sign->lnum <= line2)
6195 {
6196 if (amount == MAXLNUM)
6197 sign->lnum = line1;
6198 else
6199 sign->lnum += amount;
6200 }
6201 else if (sign->lnum > line2)
6202 sign->lnum += amount_after;
6203 }
6204}
6205#endif /* FEAT_SIGNS */
6206
6207/*
6208 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6209 */
6210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006211set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 if (on != curbuf->b_p_bl)
6214 {
6215 curbuf->b_p_bl = on;
6216#ifdef FEAT_AUTOCMD
6217 if (on)
6218 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6219 else
6220 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6221#endif
6222 }
6223}
6224
6225/*
6226 * Read the file for "buf" again and check if the contents changed.
6227 * Return TRUE if it changed or this could not be checked.
6228 */
6229 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006230buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 buf_T *newbuf;
6233 int differ = TRUE;
6234 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 exarg_T ea;
6237
6238 /* Allocate a buffer without putting it in the buffer list. */
6239 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6240 if (newbuf == NULL)
6241 return TRUE;
6242
6243 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6244 if (prep_exarg(&ea, buf) == FAIL)
6245 {
6246 wipe_buffer(newbuf, FALSE);
6247 return TRUE;
6248 }
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 /* set curwin/curbuf to buf and save a few things */
6251 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
Bram Moolenaar4770d092006-01-12 23:22:24 +00006253 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 && readfile(buf->b_ffname, buf->b_fname,
6255 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6256 &ea, READ_NEW | READ_DUMMY) == OK)
6257 {
6258 /* compare the two files line by line */
6259 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6260 {
6261 differ = FALSE;
6262 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6263 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6264 {
6265 differ = TRUE;
6266 break;
6267 }
6268 }
6269 }
6270 vim_free(ea.cmd);
6271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 /* restore curwin/curbuf and a few other things */
6273 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
6275 if (curbuf != newbuf) /* safety check */
6276 wipe_buffer(newbuf, FALSE);
6277
6278 return differ;
6279}
6280
6281/*
6282 * Wipe out a buffer and decrement the last buffer number if it was used for
6283 * this buffer. Call this to wipe out a temp buffer that does not contain any
6284 * marks.
6285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006287wipe_buffer(
6288 buf_T *buf,
6289 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 if (buf->b_fnum == top_file_num - 1)
6292 --top_file_num;
6293
6294#ifdef FEAT_AUTOCMD
6295 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006296 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006298 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299#ifdef FEAT_AUTOCMD
6300 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006301 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302#endif
6303}