blob: dbd4d3ae96b51f0e93edbb6209ef272ccc8f2466 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
36static wininfo_T *find_wininfo(buf_T *buf, int skip_diff_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020038static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
40static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
48static void free_buffer(buf_T *);
49static void free_buffer_stuff(buf_T *buf, int free_options);
50static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar7fd73202010-07-25 16:58:46 +020062#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
63static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066#ifdef FEAT_AUTOCMD
67static char *e_auabort = N_("E855: Autocommands caused command to abort");
68#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020070/* Number of times free_buffer() was called. */
71static int buf_free_count = 0;
72
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020073/* Read data from buffer for retrying. */
74 static int
75read_buffer(
76 int read_stdin, /* read file from stdin, otherwise fifo */
77 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
78 int flags) /* extra flags for readfile() */
79{
80 int retval = OK;
81 linenr_T line_count;
82
83 /*
84 * Read from the buffer which the text is already filled in and append at
85 * the end. This makes it possible to retry when 'fileformat' or
86 * 'fileencoding' was guessed wrong.
87 */
88 line_count = curbuf->b_ml.ml_line_count;
89 retval = readfile(
90 read_stdin ? NULL : curbuf->b_ffname,
91 read_stdin ? NULL : curbuf->b_fname,
92 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
93 flags | READ_BUFFER);
94 if (retval == OK)
95 {
96 /* Delete the binary lines. */
97 while (--line_count >= 0)
98 ml_delete((linenr_T)1, FALSE);
99 }
100 else
101 {
102 /* Delete the converted lines. */
103 while (curbuf->b_ml.ml_line_count > line_count)
104 ml_delete(line_count, FALSE);
105 }
106 /* Put the cursor on the first line. */
107 curwin->w_cursor.lnum = 1;
108 curwin->w_cursor.col = 0;
109
110 if (read_stdin)
111 {
112 /* Set or reset 'modified' before executing autocommands, so that
113 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100114 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 else if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117 unchanged(curbuf, FALSE);
118
119#ifdef FEAT_AUTOCMD
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100120 if (retval == OK)
121 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200122# ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100123 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200124 curbuf, &retval);
125# else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100126 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200127# endif
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100128 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200129#endif
130 }
131 return retval;
132}
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200135 * Open current buffer, that is: open the memfile and read the file into
136 * memory.
137 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 */
139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100140open_buffer(
141 int read_stdin, /* read file from stdin */
142 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
143 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145 int retval = OK;
146#ifdef FEAT_AUTOCMD
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200147 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100149#ifdef FEAT_SYN_HL
150 long old_tw = curbuf->b_p_tw;
151#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200152 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154 /*
155 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
156 * When re-entering the same buffer, it should not change, because the
157 * user may have reset the flag by hand.
158 */
159 if (readonlymode && curbuf->b_ffname != NULL
160 && (curbuf->b_flags & BF_NEVERLOADED))
161 curbuf->b_p_ro = TRUE;
162
Bram Moolenaar4770d092006-01-12 23:22:24 +0000163 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 {
165 /*
166 * There MUST be a memfile, otherwise we can't do anything
167 * If we can't create one for the current buffer, take another buffer
168 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100169 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200170 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (curbuf->b_ml.ml_mfp != NULL)
172 break;
173 /*
174 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000175 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 */
177 if (curbuf == NULL)
178 {
179 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
180 getout(2);
181 }
182 EMSG(_("E83: Cannot allocate buffer, using other one..."));
183 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100184#ifdef FEAT_SYN_HL
185 if (old_tw != curbuf->b_p_tw)
186 check_colorcolumn(curwin);
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 return FAIL;
189 }
190
191#ifdef FEAT_AUTOCMD
192 /* The autocommands in readfile() may change the buffer, but only AFTER
193 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200194 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 modified_was_set = FALSE;
196#endif
197
198 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100199 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
201 if (curbuf->b_ffname != NULL
202#ifdef FEAT_NETBEANS_INTG
203 && netbeansReadFile
204#endif
205 )
206 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100207 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200208#ifdef UNIX
209 int save_bin = curbuf->b_p_bin;
210 int perm;
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifdef FEAT_NETBEANS_INTG
213 int oldFire = netbeansFireChanges;
214
215 netbeansFireChanges = 0;
216#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200217#ifdef UNIX
218 perm = mch_getperm(curbuf->b_ffname);
219 if (perm >= 0 && (0
220# ifdef S_ISFIFO
221 || S_ISFIFO(perm)
222# endif
223# ifdef S_ISSOCK
224 || S_ISSOCK(perm)
225# endif
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200226# ifdef OPEN_CHR_FILES
227 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
228# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200229 ))
230 read_fifo = TRUE;
231 if (read_fifo)
232 curbuf->b_p_bin = TRUE;
233#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100234 if (shortmess(SHM_FILEINFO))
235 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200237 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200238 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
239#ifdef UNIX
240 if (read_fifo)
241 {
242 curbuf->b_p_bin = save_bin;
243 if (retval == OK)
244 retval = read_buffer(FALSE, eap, flags);
245 }
246#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100247 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_NETBEANS_INTG
249 netbeansFireChanges = oldFire;
250#endif
251 /* Help buffer is filtered. */
252 if (curbuf->b_help)
253 fix_help_buffer();
254 }
255 else if (read_stdin)
256 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200257 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259 /*
260 * First read the text in binary mode into the buffer.
261 * Then read from that same buffer and append at the end. This makes
262 * it possible to retry when 'fileformat' or 'fileencoding' was
263 * guessed wrong.
264 */
265 curbuf->b_p_bin = TRUE;
266 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200267 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
268 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 curbuf->b_p_bin = save_bin;
270 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200271 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273
274 /* if first time loading this buffer, init b_chartab[] */
275 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100278#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100279 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100280#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
283 /*
284 * Set/reset the Changed flag first, autocmds may change the buffer.
285 * Apply the automatic commands, before processing the modelines.
286 * So the modelines have priority over auto commands.
287 */
288 /* When reading stdin, the buffer contents always needs writing, so set
289 * the changed flag. Unless in readonly mode: "ls | gview -".
290 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000291 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#ifdef FEAT_AUTOCMD
293 || modified_was_set /* ":set modified" used in autocmd */
294# ifdef FEAT_EVAL
295 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
296# endif
297#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000298 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100300 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 unchanged(curbuf, FALSE);
302 save_file_ff(curbuf); /* keep this fileformat */
303
304 /* require "!" to overwrite the file, because it wasn't read completely */
305#ifdef FEAT_EVAL
306 if (aborting())
307#else
308 if (got_int)
309#endif
310 curbuf->b_flags |= BF_READERR;
311
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000312#ifdef FEAT_FOLDING
313 /* Need to update automatic folding. Do this before the autocommands,
314 * they may use the fold info. */
315 foldUpdateAll(curwin);
316#endif
317
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#ifdef FEAT_AUTOCMD
319 /* need to set w_topline, unless some autocommand already did that. */
320 if (!(curwin->w_valid & VALID_TOPLINE))
321 {
322 curwin->w_topline = 1;
323# ifdef FEAT_DIFF
324 curwin->w_topfill = 0;
325# endif
326 }
327# ifdef FEAT_EVAL
328 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
329# else
330 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
331# endif
332#endif
333
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100334 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
336#ifdef FEAT_AUTOCMD
337 /*
338 * The autocommands may have changed the current buffer. Apply the
339 * modelines to the correct buffer, if it still exists and is loaded.
340 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200341 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
343 aco_save_T aco;
344
345 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200346 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000348 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
350
351#ifdef FEAT_AUTOCMD
352# ifdef FEAT_EVAL
353 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
354 &retval);
355# else
356 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
357# endif
358
359 /* restore curwin/curbuf and a few other things */
360 aucmd_restbuf(&aco);
361 }
362#endif
363 }
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 return retval;
366}
367
368/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200369 * Store "buf" in "bufref" and set the free count.
370 */
371 void
372set_bufref(bufref_T *bufref, buf_T *buf)
373{
374 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200375 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200376 bufref->br_buf_free_count = buf_free_count;
377}
378
379/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200380 * Return TRUE if "bufref->br_buf" points to the same buffer as when
381 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200382 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200383 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
384 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200385 */
386 int
387bufref_valid(bufref_T *bufref)
388{
389 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200390 ? TRUE : buf_valid(bufref->br_buf)
391 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200392}
393
394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200396 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100399buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 buf_T *bp;
402
Bram Moolenaar82404332016-07-10 17:00:38 +0200403 /* Assume that we more often have a recent buffer, start with the last
404 * one. */
405 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (bp == buf)
407 return TRUE;
408 return FALSE;
409}
410
411/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200412 * A hash table used to quickly lookup a buffer by its number.
413 */
414static hashtab_T buf_hashtab;
415
416 static void
417buf_hashtab_add(buf_T *buf)
418{
419 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
420 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
421 EMSG(_("E931: Buffer cannot be registered"));
422}
423
424 static void
425buf_hashtab_remove(buf_T *buf)
426{
427 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
428
429 if (!HASHITEM_EMPTY(hi))
430 hash_remove(&buf_hashtab, hi);
431}
432
433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 * Close the link to a buffer.
435 * "action" is used when there is no longer a window for the buffer.
436 * It can be:
437 * 0 buffer becomes hidden
438 * DOBUF_UNLOAD buffer is unloaded
439 * DOBUF_DELETE buffer is unloaded and removed from buffer list
440 * DOBUF_WIPE buffer is unloaded and really deleted
441 * When doing all but the first one on the current buffer, the caller should
442 * get a new buffer very soon!
443 *
444 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100445 *
446 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
447 * cause there to be only one window with this buffer. e.g. when ":quit" is
448 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 */
450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100451close_buffer(
452 win_T *win, /* if not NULL, set b_last_cursor */
453 buf_T *buf,
454 int action,
455 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_AUTOCMD
458 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100459 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200460 bufref_T bufref;
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200461# ifdef FEAT_WINDOWS
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100462 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200463 win_T *the_curwin = curwin;
464 tabpage_T *the_curtab = curtab;
465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467 int unload_buf = (action != 0);
468 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
469 int wipe_buf = (action == DOBUF_WIPE);
470
471#ifdef FEAT_QUICKFIX
472 /*
473 * Force unloading or deleting when 'bufhidden' says so.
474 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
475 * "hide" (otherwise we could never free or delete a buffer).
476 */
477 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
478 {
479 del_buf = TRUE;
480 unload_buf = TRUE;
481 }
482 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
483 {
484 del_buf = TRUE;
485 unload_buf = TRUE;
486 wipe_buf = TRUE;
487 }
488 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
489 unload_buf = TRUE;
490#endif
491
Bram Moolenaar30180b82016-09-04 19:57:56 +0200492#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200493 /* Disallow deleting the buffer when it is locked (already being closed or
494 * halfway a command that relies on it). Unloading is allowed. */
495 if (buf->b_locked > 0 && (del_buf || wipe_buf))
496 {
497 EMSG(_("E937: Attempt to delete a buffer that is in use"));
498 return;
499 }
Bram Moolenaar30180b82016-09-04 19:57:56 +0200500#endif
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200501
Bram Moolenaar3be85852014-06-12 14:01:31 +0200502 if (win != NULL
503#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200504 && win_valid_any_tab(win) /* in case autocommands closed the window */
Bram Moolenaar3be85852014-06-12 14:01:31 +0200505#endif
506 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
508 /* Set b_last_cursor when closing the last window for the buffer.
509 * Remember the last cursor position and window options of the buffer.
510 * This used to be only for the current window, but then options like
511 * 'foldmethod' may be lost with a ":only" command. */
512 if (buf->b_nwindows == 1)
513 set_last_cursor(win);
514 buflist_setfpos(buf, win,
515 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
516 win->w_cursor.col, TRUE);
517 }
518
519#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200520 set_bufref(&bufref, buf);
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 /* When the buffer is no longer in a window, trigger BufWinLeave */
523 if (buf->b_nwindows == 1)
524 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200525 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200526 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
527 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200528 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100529 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200530 /* Autocommands deleted the buffer. */
531aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100532 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100534 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200535 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200536 if (abort_if_last && one_window())
537 /* Autocommands made this the only window. */
538 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540 /* When the buffer becomes hidden, but is not unloaded, trigger
541 * BufHidden */
542 if (!unload_buf)
543 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200544 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200545 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
546 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200547 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200548 /* Autocommands deleted the buffer. */
549 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200550 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200551 if (abort_if_last && one_window())
552 /* Autocommands made this the only window. */
553 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555# ifdef FEAT_EVAL
556 if (aborting()) /* autocmds may abort script processing */
557 return;
558# endif
559 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200560
561# ifdef FEAT_WINDOWS
562 /* If the buffer was in curwin and the window has changed, go back to that
563 * window, if it still exists. This avoids that ":edit x" triggering a
564 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
565 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
566 {
567 block_autocmds();
568 goto_tabpage_win(the_curtab, the_curwin);
569 unblock_autocmds();
570 }
571# endif
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 nwindows = buf->b_nwindows;
574#endif
575
576 /* decrease the link count from windows (unless not in any window) */
577 if (buf->b_nwindows > 0)
578 --buf->b_nwindows;
579
580 /* Return when a window is displaying the buffer or when it's not
581 * unloaded. */
582 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
585 /* Always remove the buffer when there is no file name. */
586 if (buf->b_ffname == NULL)
587 del_buf = TRUE;
588
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200589 /* When closing the current buffer stop Visual mode before freeing
590 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200591 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200592#if defined(EXITFREE)
593 && !entered_free_all_mem
594#endif
595 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200596 end_visual_mode();
597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 /*
599 * Free all things allocated for this buffer.
600 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
601 */
602#ifdef FEAT_AUTOCMD
603 /* Remember if we are closing the current buffer. Restore the number of
604 * windows, so that autocommands in buf_freeall() don't get confused. */
605 is_curbuf = (buf == curbuf);
606 buf->b_nwindows = nwindows;
607#endif
608
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200609 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611#ifdef FEAT_AUTOCMD
612 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200613 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 return;
615# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000616 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 return;
618# endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 /*
621 * It's possible that autocommands change curbuf to the one being deleted.
622 * This might cause the previous curbuf to be deleted unexpectedly. But
623 * in some cases it's OK to delete the curbuf, because a new one is
624 * obtained anyway. Therefore only return if curbuf changed to the
625 * deleted buffer.
626 */
627 if (buf == curbuf && !is_curbuf)
628 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200629
630 if (
631#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200632 win_valid_any_tab(win) &&
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200633#else
634 win != NULL &&
635#endif
636 win->w_buffer == buf)
637 win->w_buffer = NULL; /* make sure we don't use the buffer now */
638
639 /* Autocommands may have opened or closed windows for this buffer.
640 * Decrement the count for the close we do here. */
641 if (buf->b_nwindows > 0)
642 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#endif
644
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000645 /* Change directories when the 'acd' option is set. */
646 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648 /*
649 * Remove the buffer from the list.
650 */
651 if (wipe_buf)
652 {
653#ifdef FEAT_SUN_WORKSHOP
654 if (usingSunWorkShop)
655 workshop_file_closed_lineno((char *)buf->b_ffname,
656 (int)buf->b_last_cursor.lnum);
657#endif
658 vim_free(buf->b_ffname);
659 vim_free(buf->b_sfname);
660 if (buf->b_prev == NULL)
661 firstbuf = buf->b_next;
662 else
663 buf->b_prev->b_next = buf->b_next;
664 if (buf->b_next == NULL)
665 lastbuf = buf->b_prev;
666 else
667 buf->b_next->b_prev = buf->b_prev;
668 free_buffer(buf);
669 }
670 else
671 {
672 if (del_buf)
673 {
674 /* Free all internal variables and reset option values, to make
675 * ":bdel" compatible with Vim 5.7. */
676 free_buffer_stuff(buf, TRUE);
677
678 /* Make it look like a new buffer. */
679 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
680
681 /* Init the options when loaded again. */
682 buf->b_p_initialized = FALSE;
683 }
684 buf_clear_file(buf);
685 if (del_buf)
686 buf->b_p_bl = FALSE;
687 }
688}
689
690/*
691 * Make buffer not contain a file.
692 */
693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100694buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
696 buf->b_ml.ml_line_count = 1;
697 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 buf->b_p_eol = TRUE;
700 buf->b_start_eol = TRUE;
701#ifdef FEAT_MBYTE
702 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000703 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
705 buf->b_ml.ml_mfp = NULL;
706 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
707#ifdef FEAT_NETBEANS_INTG
708 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#endif
710}
711
712/*
713 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200714 * the file. Careful: get here with "curwin" NULL when exiting.
715 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200716 * BFA_DEL buffer is going to be deleted
717 * BFA_WIPE buffer is going to be wiped out
718 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100721buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723#ifdef FEAT_AUTOCMD
724 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200725 bufref_T bufref;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200726# ifdef FEAT_WINDOWS
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200727 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200728 win_T *the_curwin = curwin;
729 tabpage_T *the_curtab = curtab;
730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaar5a497892016-09-03 16:29:04 +0200732 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200733 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200734 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200735 if (buf->b_ml.ml_mfp != NULL)
736 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200737 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
738 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200739 && !bufref_valid(&bufref))
740 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200741 return;
742 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200743 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200745 if (apply_autocmds(EVENT_BUFDELETE, 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 Moolenaar071d4272004-06-13 20:20:40 +0000749 return;
750 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200751 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200753 if (apply_autocmds(EVENT_BUFWIPEOUT, 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 Moolenaare0ab94e2016-09-04 19:50:54 +0200759 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200760
761# ifdef FEAT_WINDOWS
762 /* If the buffer was in curwin and the window has changed, go back to that
763 * window, if it still exists. This avoids that ":edit x" triggering a
764 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
765 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
766 {
767 block_autocmds();
768 goto_tabpage_win(the_curtab, the_curwin);
769 unblock_autocmds();
770 }
771# endif
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000774 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 return;
776# endif
777
778 /*
779 * It's possible that autocommands change curbuf to the one being deleted.
780 * This might cause curbuf to be deleted unexpectedly. But in some cases
781 * it's OK to delete the curbuf, because a new one is obtained anyway.
782 * Therefore only return if curbuf changed to the deleted buffer.
783 */
784 if (buf == curbuf && !is_curbuf)
785 return;
786#endif
787#ifdef FEAT_DIFF
788 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
789#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200790#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100791 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200792 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100793 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200794#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000795
796#ifdef FEAT_FOLDING
797 /* No folds in an empty buffer. */
798# ifdef FEAT_WINDOWS
799 {
800 win_T *win;
801 tabpage_T *tp;
802
803 FOR_ALL_TAB_WINDOWS(tp, win)
804 if (win->w_buffer == buf)
805 clearFolding(win);
806 }
807# else
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200808 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000809 clearFolding(curwin);
810# endif
811#endif
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_TCL
814 tcl_buffer_free(buf);
815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 ml_close(buf, TRUE); /* close and delete the memline/memfile */
817 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200818 if ((flags & BFA_KEEP_UNDO) == 0)
819 {
820 u_blockfree(buf); /* free the memory allocated for undo */
821 u_clearall(buf); /* reset all undo information */
822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200824 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000826 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827}
828
829/*
830 * Free a buffer structure and the things it contains related to the buffer
831 * itself (not the file, that must have been done already).
832 */
833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100834free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200836 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200838#ifdef FEAT_EVAL
839 unref_var_dict(buf->b_vars);
840#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200841#ifdef FEAT_LUA
842 lua_buffer_free(buf);
843#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000844#ifdef FEAT_MZSCHEME
845 mzscheme_buffer_free(buf);
846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifdef FEAT_PERL
848 perl_buf_free(buf);
849#endif
850#ifdef FEAT_PYTHON
851 python_buffer_free(buf);
852#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200853#ifdef FEAT_PYTHON3
854 python3_buffer_free(buf);
855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#ifdef FEAT_RUBY
857 ruby_buffer_free(buf);
858#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200859#ifdef FEAT_JOB_CHANNEL
860 channel_buffer_free(buf);
861#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200862#ifdef FEAT_TERMINAL
863 free_terminal(buf->b_term);
864#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200865
866 buf_hashtab_remove(buf);
867
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200868#ifdef FEAT_AUTOCMD
869 aubuflocal_remove(buf);
870
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200871 if (autocmd_busy)
872 {
873 /* Do not free the buffer structure while autocommands are executing,
874 * it's still needed. Free it when autocmd_busy is reset. */
875 buf->b_next = au_pending_free_buf;
876 au_pending_free_buf = buf;
877 }
878 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000879#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200880 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881}
882
883/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100884 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100885 */
886 static void
887init_changedtick(buf_T *buf)
888{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100889 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100890
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100891 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
892 di->di_tv.v_type = VAR_NUMBER;
893 di->di_tv.v_lock = VAR_FIXED;
894 di->di_tv.vval.v_number = 0;
895
Bram Moolenaar92769c32017-02-25 15:41:37 +0100896#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100897 STRCPY(buf->b_ct_di.di_key, "changedtick");
898 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100899#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100900}
901
902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
904 */
905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100906free_buffer_stuff(
907 buf_T *buf,
908 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
910 if (free_options)
911 {
912 clear_wininfo(buf); /* including window-local options */
913 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200914#ifdef FEAT_SPELL
915 ga_clear(&buf->b_s.b_langp);
916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100919 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100920 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100921
922 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
923 hash_init(&buf->b_vars->dv_hashtab);
924 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100925 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927#endif
928#ifdef FEAT_USR_CMDS
929 uc_clear(&buf->b_ucmds); /* clear local user commands */
930#endif
931#ifdef FEAT_SIGNS
932 buf_delete_signs(buf); /* delete any signs */
933#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000934#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200935 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937#ifdef FEAT_LOCALMAP
938 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
939 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
940#endif
941#ifdef FEAT_MBYTE
942 vim_free(buf->b_start_fenc);
943 buf->b_start_fenc = NULL;
944#endif
945}
946
947/*
948 * Free the b_wininfo list for buffer "buf".
949 */
950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100951clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
953 wininfo_T *wip;
954
955 while (buf->b_wininfo != NULL)
956 {
957 wip = buf->b_wininfo;
958 buf->b_wininfo = wip->wi_next;
959 if (wip->wi_optset)
960 {
961 clear_winopt(&wip->wi_opt);
962#ifdef FEAT_FOLDING
963 deleteFoldRecurse(&wip->wi_folds);
964#endif
965 }
966 vim_free(wip);
967 }
968}
969
970#if defined(FEAT_LISTCMDS) || defined(PROTO)
971/*
972 * Go to another buffer. Handles the result of the ATTENTION dialog.
973 */
974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100975goto_buffer(
976 exarg_T *eap,
977 int start,
978 int dir,
979 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000981# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200982 bufref_T old_curbuf;
983
984 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 swap_exists_action = SEA_DIALOG;
987# endif
988 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
989 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000990# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
992 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000993# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
994 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000995
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000996 /* Reset the error/interrupt/exception state here so that
997 * aborting() returns FALSE when closing a window. */
998 enter_cleanup(&cs);
999# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001000
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001001 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 win_close(curwin, TRUE);
1003 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001004 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001005
1006# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1007 /* Restore the error/interrupt/exception state if not discarded by a
1008 * new aborting error, interrupt, or uncaught exception. */
1009 leave_cleanup(&cs);
1010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001013 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014# endif
1015}
1016#endif
1017
Bram Moolenaare64ac772005-12-07 20:54:59 +00001018#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019/*
1020 * Handle the situation of swap_exists_action being set.
1021 * It is allowed for "old_curbuf" to be NULL or invalid.
1022 */
1023 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001024handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001026# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1027 cleanup_T cs;
1028# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001029#ifdef FEAT_SYN_HL
1030 long old_tw = curbuf->b_p_tw;
1031#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001032 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (swap_exists_action == SEA_QUIT)
1035 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001036# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1037 /* Reset the error/interrupt/exception state here so that
1038 * aborting() returns FALSE when closing a buffer. */
1039 enter_cleanup(&cs);
1040# endif
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 /* User selected Quit at ATTENTION prompt. Go back to previous
1043 * buffer. If that buffer is gone or the same as the current one,
1044 * open a new, empty buffer. */
1045 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001046 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001047 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001048 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1049 || old_curbuf->br_buf == curbuf)
1050 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1051 else
1052 buf = old_curbuf->br_buf;
1053 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001054 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001055 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001056#ifdef FEAT_SYN_HL
1057 if (old_tw != curbuf->b_p_tw)
1058 check_colorcolumn(curwin);
1059#endif
1060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001062
1063# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1064 /* Restore the error/interrupt/exception state if not discarded by a
1065 * new aborting error, interrupt, or uncaught exception. */
1066 leave_cleanup(&cs);
1067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 else if (swap_exists_action == SEA_RECOVER)
1070 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001071# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1072 /* Reset the error/interrupt/exception state here so that
1073 * aborting() returns FALSE when closing a buffer. */
1074 enter_cleanup(&cs);
1075# endif
1076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 /* User selected Recover at ATTENTION prompt. */
1078 msg_scroll = TRUE;
1079 ml_recover();
1080 MSG_PUTS("\n"); /* don't overwrite the last message */
1081 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001082 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001083
1084# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1085 /* Restore the error/interrupt/exception state if not discarded by a
1086 * new aborting error, interrupt, or uncaught exception. */
1087 leave_cleanup(&cs);
1088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 swap_exists_action = SEA_NONE;
1091}
1092#endif
1093
1094#if defined(FEAT_LISTCMDS) || defined(PROTO)
1095/*
1096 * do_bufdel() - delete or unload buffer(s)
1097 *
1098 * addr_count == 0: ":bdel" - delete current buffer
1099 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1100 * buffer "end_bnr", then any other arguments.
1101 * addr_count == 2: ":N,N bdel" - delete buffers in range
1102 *
1103 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1104 * DOBUF_DEL (":bdel")
1105 *
1106 * Returns error message or NULL
1107 */
1108 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001109do_bufdel(
1110 int command,
1111 char_u *arg, /* pointer to extra arguments */
1112 int addr_count,
1113 int start_bnr, /* first buffer number in a range */
1114 int end_bnr, /* buffer nr or last buffer nr in a range */
1115 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 int do_current = 0; /* delete current buffer? */
1118 int deleted = 0; /* number of buffers deleted */
1119 char_u *errormsg = NULL; /* return value */
1120 int bnr; /* buffer number */
1121 char_u *p;
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 if (addr_count == 0)
1124 {
1125 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1126 }
1127 else
1128 {
1129 if (addr_count == 2)
1130 {
1131 if (*arg) /* both range and argument is not allowed */
1132 return (char_u *)_(e_trailing);
1133 bnr = start_bnr;
1134 }
1135 else /* addr_count == 1 */
1136 bnr = end_bnr;
1137
1138 for ( ;!got_int; ui_breakcheck())
1139 {
1140 /*
1141 * delete the current buffer last, otherwise when the
1142 * current buffer is deleted, the next buffer becomes
1143 * the current one and will be loaded, which may then
1144 * also be deleted, etc.
1145 */
1146 if (bnr == curbuf->b_fnum)
1147 do_current = bnr;
1148 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1149 forceit) == OK)
1150 ++deleted;
1151
1152 /*
1153 * find next buffer number to delete/unload
1154 */
1155 if (addr_count == 2)
1156 {
1157 if (++bnr > end_bnr)
1158 break;
1159 }
1160 else /* addr_count == 1 */
1161 {
1162 arg = skipwhite(arg);
1163 if (*arg == NUL)
1164 break;
1165 if (!VIM_ISDIGIT(*arg))
1166 {
1167 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001168 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1169 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (bnr < 0) /* failed */
1171 break;
1172 arg = p;
1173 }
1174 else
1175 bnr = getdigits(&arg);
1176 }
1177 }
1178 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1179 FORWARD, do_current, forceit) == OK)
1180 ++deleted;
1181
1182 if (deleted == 0)
1183 {
1184 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001185 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001187 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001189 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 errormsg = IObuff;
1191 }
1192 else if (deleted >= p_report)
1193 {
1194 if (command == DOBUF_UNLOAD)
1195 {
1196 if (deleted == 1)
1197 MSG(_("1 buffer unloaded"));
1198 else
1199 smsg((char_u *)_("%d buffers unloaded"), deleted);
1200 }
1201 else if (command == DOBUF_DEL)
1202 {
1203 if (deleted == 1)
1204 MSG(_("1 buffer deleted"));
1205 else
1206 smsg((char_u *)_("%d buffers deleted"), deleted);
1207 }
1208 else
1209 {
1210 if (deleted == 1)
1211 MSG(_("1 buffer wiped out"));
1212 else
1213 smsg((char_u *)_("%d buffers wiped out"), deleted);
1214 }
1215 }
1216 }
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
1219 return errormsg;
1220}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001221#endif /* FEAT_LISTCMDS */
1222
1223#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1224 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001226static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001227
1228/*
1229 * Make the current buffer empty.
1230 * Used when it is wiped out and it's the last buffer.
1231 */
1232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001233empty_curbuf(
1234 int close_others,
1235 int forceit,
1236 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001237{
1238 int retval;
1239 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001240 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001241
1242 if (action == DOBUF_UNLOAD)
1243 {
1244 EMSG(_("E90: Cannot unload last buffer"));
1245 return FAIL;
1246 }
1247
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001248 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001249#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001250 if (close_others)
1251 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001252 close_windows(buf, TRUE);
1253#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001254
1255 setpcmark();
1256 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1257 forceit ? ECMD_FORCEIT : 0, curwin);
1258
1259 /*
1260 * do_ecmd() may create a new buffer, then we have to delete
1261 * the old one. But do_ecmd() may have done that already, check
1262 * if the buffer still exists.
1263 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001264 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001265 close_buffer(NULL, buf, action, FALSE);
1266 if (!close_others)
1267 need_fileinfo = FALSE;
1268 return retval;
1269}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270/*
1271 * Implementation of the commands for the buffer list.
1272 *
1273 * action == DOBUF_GOTO go to specified buffer
1274 * action == DOBUF_SPLIT split window and go to specified buffer
1275 * action == DOBUF_UNLOAD unload specified buffer(s)
1276 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1277 * action == DOBUF_WIPE delete specified buffer(s) really
1278 *
1279 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1280 * start == DOBUF_FIRST go to "count" buffer from first buffer
1281 * start == DOBUF_LAST go to "count" buffer from last buffer
1282 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1283 *
1284 * Return FAIL or OK.
1285 */
1286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287do_buffer(
1288 int action,
1289 int start,
1290 int dir, /* FORWARD or BACKWARD */
1291 int count, /* buffer number or number of buffers */
1292 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 buf_T *buf;
1295 buf_T *bp;
1296 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1297 || action == DOBUF_WIPE);
1298
1299 switch (start)
1300 {
1301 case DOBUF_FIRST: buf = firstbuf; break;
1302 case DOBUF_LAST: buf = lastbuf; break;
1303 default: buf = curbuf; break;
1304 }
1305 if (start == DOBUF_MOD) /* find next modified buffer */
1306 {
1307 while (count-- > 0)
1308 {
1309 do
1310 {
1311 buf = buf->b_next;
1312 if (buf == NULL)
1313 buf = firstbuf;
1314 }
1315 while (buf != curbuf && !bufIsChanged(buf));
1316 }
1317 if (!bufIsChanged(buf))
1318 {
1319 EMSG(_("E84: No modified buffer found"));
1320 return FAIL;
1321 }
1322 }
1323 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1324 {
1325 while (buf != NULL && buf->b_fnum != count)
1326 buf = buf->b_next;
1327 }
1328 else
1329 {
1330 bp = NULL;
1331 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1332 {
1333 /* remember the buffer where we start, we come back there when all
1334 * buffers are unlisted. */
1335 if (bp == NULL)
1336 bp = buf;
1337 if (dir == FORWARD)
1338 {
1339 buf = buf->b_next;
1340 if (buf == NULL)
1341 buf = firstbuf;
1342 }
1343 else
1344 {
1345 buf = buf->b_prev;
1346 if (buf == NULL)
1347 buf = lastbuf;
1348 }
1349 /* don't count unlisted buffers */
1350 if (unload || buf->b_p_bl)
1351 {
1352 --count;
1353 bp = NULL; /* use this buffer as new starting point */
1354 }
1355 if (bp == buf)
1356 {
1357 /* back where we started, didn't find anything. */
1358 EMSG(_("E85: There is no listed buffer"));
1359 return FAIL;
1360 }
1361 }
1362 }
1363
1364 if (buf == NULL) /* could not find it */
1365 {
1366 if (start == DOBUF_FIRST)
1367 {
1368 /* don't warn when deleting */
1369 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001370 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372 else if (dir == FORWARD)
1373 EMSG(_("E87: Cannot go beyond last buffer"));
1374 else
1375 EMSG(_("E88: Cannot go before first buffer"));
1376 return FAIL;
1377 }
1378
1379#ifdef FEAT_GUI
1380 need_mouse_correct = TRUE;
1381#endif
1382
1383#ifdef FEAT_LISTCMDS
1384 /*
1385 * delete buffer buf from memory and/or the list
1386 */
1387 if (unload)
1388 {
1389 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001390# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1391 bufref_T bufref;
1392
1393 set_bufref(&bufref, buf);
1394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /* When unloading or deleting a buffer that's already unloaded and
1397 * unlisted: fail silently. */
1398 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1399 return FAIL;
1400
1401 if (!forceit && bufIsChanged(buf))
1402 {
1403#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1404 if ((p_confirm || cmdmod.confirm) && p_write)
1405 {
1406 dialog_changed(buf, FALSE);
1407# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001408 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /* Autocommand deleted buffer, oops! It's not changed
1410 * now. */
1411 return FAIL;
1412# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001413 /* If it's still changed fail silently, the dialog already
1414 * mentioned why it fails. */
1415 if (bufIsChanged(buf))
1416 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001418 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419#endif
1420 {
1421 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1422 buf->b_fnum);
1423 return FAIL;
1424 }
1425 }
1426
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001427 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001428 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001429 end_visual_mode();
1430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 /*
1432 * If deleting the last (listed) buffer, make it empty.
1433 * The last (listed) buffer cannot be unloaded.
1434 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001435 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (bp->b_p_bl && bp != buf)
1437 break;
1438 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001439 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441#ifdef FEAT_WINDOWS
1442 /*
1443 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444 * (unless it's the only window). Repeat this so long as we end up in
1445 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001447 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001448# ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001449 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02001450# endif
Bram Moolenaar459ca562016-11-10 18:16:33 +01001451 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001452 {
1453 if (win_close(curwin, FALSE) == FAIL)
1454 break;
1455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456#endif
1457
1458 /*
1459 * If the buffer to be deleted is not the current one, delete it here.
1460 */
1461 if (buf != curbuf)
1462 {
1463#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001464 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001465 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001467 if (buf->b_nwindows <= 0)
1468 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 return OK;
1470 }
1471
1472 /*
1473 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001474 * There should be another, otherwise it would have been handled
1475 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001476 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 * Then prefer the buffer we most recently visited.
1478 * Else try to find one that is loaded, after the current buffer,
1479 * then before the current buffer.
1480 * Finally use any buffer.
1481 */
1482 buf = NULL; /* selected buffer */
1483 bp = NULL; /* used when no loaded buffer found */
1484#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001485 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1486 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487# ifdef FEAT_JUMPLIST
1488 else
1489# endif
1490#endif
1491#ifdef FEAT_JUMPLIST
1492 if (curwin->w_jumplistlen > 0)
1493 {
1494 int jumpidx;
1495
1496 jumpidx = curwin->w_jumplistidx - 1;
1497 if (jumpidx < 0)
1498 jumpidx = curwin->w_jumplistlen - 1;
1499
1500 forward = jumpidx;
1501 while (jumpidx != curwin->w_jumplistidx)
1502 {
1503 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1504 if (buf != NULL)
1505 {
1506 if (buf == curbuf || !buf->b_p_bl)
1507 buf = NULL; /* skip current and unlisted bufs */
1508 else if (buf->b_ml.ml_mfp == NULL)
1509 {
1510 /* skip unloaded buf, but may keep it for later */
1511 if (bp == NULL)
1512 bp = buf;
1513 buf = NULL;
1514 }
1515 }
1516 if (buf != NULL) /* found a valid buffer: stop searching */
1517 break;
1518 /* advance to older entry in jump list */
1519 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1520 break;
1521 if (--jumpidx < 0)
1522 jumpidx = curwin->w_jumplistlen - 1;
1523 if (jumpidx == forward) /* List exhausted for sure */
1524 break;
1525 }
1526 }
1527#endif
1528
1529 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1530 {
1531 forward = TRUE;
1532 buf = curbuf->b_next;
1533 for (;;)
1534 {
1535 if (buf == NULL)
1536 {
1537 if (!forward) /* tried both directions */
1538 break;
1539 buf = curbuf->b_prev;
1540 forward = FALSE;
1541 continue;
1542 }
1543 /* in non-help buffer, try to skip help buffers, and vv */
1544 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1545 {
1546 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1547 break;
1548 if (bp == NULL) /* remember unloaded buf for later */
1549 bp = buf;
1550 }
1551 if (forward)
1552 buf = buf->b_next;
1553 else
1554 buf = buf->b_prev;
1555 }
1556 }
1557 if (buf == NULL) /* No loaded buffer, use unloaded one */
1558 buf = bp;
1559 if (buf == NULL) /* No loaded buffer, find listed one */
1560 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001561 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (buf->b_p_bl && buf != curbuf)
1563 break;
1564 }
1565 if (buf == NULL) /* Still no buffer, just take one */
1566 {
1567 if (curbuf->b_next != NULL)
1568 buf = curbuf->b_next;
1569 else
1570 buf = curbuf->b_prev;
1571 }
1572 }
1573
Bram Moolenaara02471e2014-01-10 16:43:14 +01001574 if (buf == NULL)
1575 {
1576 /* Autocommands must have wiped out all other buffers. Only option
1577 * now is to make the current buffer empty. */
1578 return empty_curbuf(FALSE, forceit, action);
1579 }
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 /*
1582 * make buf current buffer
1583 */
1584 if (action == DOBUF_SPLIT) /* split window first */
1585 {
1586# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001587 /* If 'switchbuf' contains "useopen": jump to first window containing
1588 * "buf" if one exists */
1589 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001590 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001591 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001592 * page containing "buf" if one exists */
1593 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 return OK;
1595 if (win_split(0, 0) == FAIL)
1596# endif
1597 return FAIL;
1598 }
1599#endif
1600
1601 /* go to current buffer - nothing to do */
1602 if (buf == curbuf)
1603 return OK;
1604
1605 /*
1606 * Check if the current buffer may be abandoned.
1607 */
1608 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1609 {
1610#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1611 if ((p_confirm || cmdmod.confirm) && p_write)
1612 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001613# ifdef FEAT_AUTOCMD
1614 bufref_T bufref;
1615
1616 set_bufref(&bufref, buf);
1617# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 dialog_changed(curbuf, FALSE);
1619# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001620 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /* Autocommand deleted buffer, oops! */
1622 return FAIL;
1623# endif
1624 }
1625 if (bufIsChanged(curbuf))
1626#endif
1627 {
1628 EMSG(_(e_nowrtmsg));
1629 return FAIL;
1630 }
1631 }
1632
1633 /* Go to the other buffer. */
1634 set_curbuf(buf, action);
1635
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001636#if defined(FEAT_LISTCMDS) \
1637 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001639 {
1640 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643
1644#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1645 if (aborting()) /* autocmds may abort script processing */
1646 return FAIL;
1647#endif
1648
1649 return OK;
1650}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
1653/*
1654 * Set current buffer to "buf". Executes autocommands and closes current
1655 * buffer. "action" tells how to close the current buffer:
1656 * DOBUF_GOTO free or hide it
1657 * DOBUF_SPLIT nothing
1658 * DOBUF_UNLOAD unload it
1659 * DOBUF_DEL delete it
1660 * DOBUF_WIPE wipe it out
1661 */
1662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001663set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
1665 buf_T *prevbuf;
1666 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1667 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001668#ifdef FEAT_SYN_HL
1669 long old_tw = curbuf->b_p_tw;
1670#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001671 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
1673 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001674 if (!cmdmod.keepalt)
1675 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001676 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 /* Don't restart Select mode after switching to another buffer. */
1679 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 /* close_windows() or apply_autocmds() may change curbuf */
1682 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001683 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
1685#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001686 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687# ifdef FEAT_EVAL
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001688 || (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689# else
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001690 || bufref_valid(&bufref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691# endif
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001692 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#endif
1694 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001695#ifdef FEAT_SYN_HL
1696 if (prevbuf == curwin->w_buffer)
1697 reset_synblock(curwin);
1698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699#ifdef FEAT_WINDOWS
1700 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001701 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#endif
1703#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001704 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001706 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001708 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001709#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001710 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001711#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001712 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001713 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1715 unload ? action : (action == DOBUF_GOTO
1716 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001717 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001718#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001719 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001720 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001721 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001722#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
1725#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001726 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001727 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001728 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1729 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001730# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001731 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001732# endif
1733# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001734 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001735# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001736 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001740#ifdef FEAT_SYN_HL
1741 if (old_tw != curbuf->b_p_tw)
1742 check_colorcolumn(curwin);
1743#endif
1744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
1747/*
1748 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001749 * Old curbuf must have been abandoned already! This also means "curbuf" may
1750 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 */
1752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 /* Copy buffer and window local option values. Not for a help buffer. */
1756 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1757 if (!buf->b_help)
1758 get_winopts(buf);
1759#ifdef FEAT_FOLDING
1760 else
1761 /* Remove all folds in the window. */
1762 clearFolding(curwin);
1763 foldUpdateAll(curwin); /* update folds (later). */
1764#endif
1765
1766 /* Get the buffer in the current window. */
1767 curwin->w_buffer = buf;
1768 curbuf = buf;
1769 ++curbuf->b_nwindows;
1770
1771#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001772 if (curwin->w_p_diff)
1773 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
1775
Bram Moolenaara971b822011-09-14 14:43:25 +02001776#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001777 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001778#endif
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 /* Cursor on first line by default. */
1781 curwin->w_cursor.lnum = 1;
1782 curwin->w_cursor.col = 0;
1783#ifdef FEAT_VIRTUALEDIT
1784 curwin->w_cursor.coladd = 0;
1785#endif
1786 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001787#ifdef FEAT_AUTOCMD
1788 curwin->w_topline_was_set = FALSE;
1789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001791 /* mark cursor position as being invalid */
1792 curwin->w_valid = 0;
1793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 /* Make sure the buffer is loaded. */
1795 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001796 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001797#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001798 /* If there is no filetype, allow for detecting one. Esp. useful for
1799 * ":ball" used in a autocommand. If there already is a filetype we
1800 * might prefer to keep it. */
1801 if (*curbuf->b_p_ft == NUL)
1802 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001803#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001804
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001805 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 else
1808 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001809 if (!msg_silent)
1810 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1812#ifdef FEAT_AUTOCMD
1813 curwin->w_topline = 1;
1814# ifdef FEAT_DIFF
1815 curwin->w_topfill = 0;
1816# endif
1817 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1818 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1819#endif
1820 }
1821
1822 /* If autocommands did not change the cursor position, restore cursor lnum
1823 * and possibly cursor col. */
1824 if (curwin->w_cursor.lnum == 1 && inindent(0))
1825 buflist_getfpos();
1826
1827 check_arg_idx(curwin); /* check for valid arg_idx */
1828#ifdef FEAT_TITLE
1829 maketitle();
1830#endif
1831#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001832 /* when autocmds didn't change it */
1833 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#endif
1835 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1836
Bram Moolenaar009b2592004-10-24 19:18:58 +00001837#ifdef FEAT_NETBEANS_INTG
1838 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001839 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001840#endif
1841
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001842 /* Change directories when the 'acd' option is set. */
1843 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
1845#ifdef FEAT_KEYMAP
1846 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001847 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001849#ifdef FEAT_SPELL
1850 /* May need to set the spell language. Can only do this after the buffer
1851 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001852 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1853 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001854#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001855#ifdef FEAT_VIMINFO
1856 curbuf->b_last_used = vim_time();
1857#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 redraw_later(NOT_VALID);
1860}
1861
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001862#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1863/*
1864 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001865 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001866 */
1867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001868do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001869{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001870 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001871 && curbuf->b_ffname != NULL
1872 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001873 shorten_fnames(TRUE);
1874}
1875#endif
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877/*
1878 * functions for dealing with the buffer list
1879 */
1880
Bram Moolenaar480778b2016-07-14 22:09:39 +02001881static int top_file_num = 1; /* highest file number */
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883/*
1884 * Add a file name to the buffer list. Return a pointer to the buffer.
1885 * If the same file name already exists return a pointer to that buffer.
1886 * If it does not exist, or if fname == NULL, a new entry is created.
1887 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1888 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1889 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001890 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001891 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1892 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 * This is the ONLY way to create a new buffer.
1894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001896buflist_new(
1897 char_u *ffname, /* full path of fname or relative */
1898 char_u *sfname, /* short fname or NULL */
1899 linenr_T lnum, /* preferred cursor line */
1900 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902 buf_T *buf;
1903#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001904 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905#endif
1906
Bram Moolenaar480778b2016-07-14 22:09:39 +02001907 if (top_file_num == 1)
1908 hash_init(&buf_hashtab);
1909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1911
1912 /*
1913 * If file name already exists in the list, update the entry.
1914 */
1915#ifdef UNIX
1916 /* On Unix we can use inode numbers when the file exists. Works better
1917 * for hard links. */
1918 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1919 st.st_dev = (dev_T)-1;
1920#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001921 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922#ifdef UNIX
1923 buflist_findname_stat(ffname, &st)
1924#else
1925 buflist_findname(ffname)
1926#endif
1927 ) != NULL)
1928 {
1929 vim_free(ffname);
1930 if (lnum != 0)
1931 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001932
1933 if ((flags & BLN_NOOPT) == 0)
1934 /* copy the options now, if 'cpo' doesn't have 's' and not done
1935 * already */
1936 buf_copy_options(buf, 0);
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1939 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001940#ifdef FEAT_AUTOCMD
1941 bufref_T bufref;
1942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 buf->b_p_bl = TRUE;
1944#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001945 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001947 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001948 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001949 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001950 return NULL;
1951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952#endif
1953 }
1954 return buf;
1955 }
1956
1957 /*
1958 * If the current buffer has no name and no contents, use the current
1959 * buffer. Otherwise: Need to allocate a new buffer structure.
1960 *
1961 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001962 * (A spell file buffer is allocated in spell.c, but that's not a normal
1963 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
1965 buf = NULL;
1966 if ((flags & BLN_CURBUF)
1967 && curbuf != NULL
1968 && curbuf->b_ffname == NULL
1969 && curbuf->b_nwindows <= 1
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001970 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 buf = curbuf;
1973#ifdef FEAT_AUTOCMD
1974 /* It's like this buffer is deleted. Watch out for autocommands that
1975 * change curbuf! If that happens, allocate a new buffer anyway. */
1976 if (curbuf->b_p_bl)
1977 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1978 if (buf == curbuf)
1979 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1980# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001981 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 return NULL;
1983# endif
1984#endif
1985#ifdef FEAT_QUICKFIX
1986# ifdef FEAT_AUTOCMD
1987 if (buf == curbuf)
1988# endif
1989 {
1990 /* Make sure 'bufhidden' and 'buftype' are empty */
1991 clear_string_option(&buf->b_p_bh);
1992 clear_string_option(&buf->b_p_bt);
1993 }
1994#endif
1995 }
1996 if (buf != curbuf || curbuf == NULL)
1997 {
1998 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1999 if (buf == NULL)
2000 {
2001 vim_free(ffname);
2002 return NULL;
2003 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002004#ifdef FEAT_EVAL
2005 /* init b: variables */
2006 buf->b_vars = dict_alloc();
2007 if (buf->b_vars == NULL)
2008 {
2009 vim_free(ffname);
2010 vim_free(buf);
2011 return NULL;
2012 }
2013 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2014#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002015 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
2017
2018 if (ffname != NULL)
2019 {
2020 buf->b_ffname = ffname;
2021 buf->b_sfname = vim_strsave(sfname);
2022 }
2023
2024 clear_wininfo(buf);
2025 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2026
2027 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2028 || buf->b_wininfo == NULL)
2029 {
2030 vim_free(buf->b_ffname);
2031 buf->b_ffname = NULL;
2032 vim_free(buf->b_sfname);
2033 buf->b_sfname = NULL;
2034 if (buf != curbuf)
2035 free_buffer(buf);
2036 return NULL;
2037 }
2038
2039 if (buf == curbuf)
2040 {
2041 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002042 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (buf != curbuf) /* autocommands deleted the buffer! */
2044 return NULL;
2045#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002046 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 return NULL;
2048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002050
2051 /* Init the options. */
2052 buf->b_p_initialized = FALSE;
2053 buf_copy_options(buf, BCO_ENTER);
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055#ifdef FEAT_KEYMAP
2056 /* need to reload lmaps and set b:keymap_name */
2057 curbuf->b_kmap_state |= KEYMAP_INIT;
2058#endif
2059 }
2060 else
2061 {
2062 /*
2063 * put new buffer at the end of the buffer list
2064 */
2065 buf->b_next = NULL;
2066 if (firstbuf == NULL) /* buffer list is empty */
2067 {
2068 buf->b_prev = NULL;
2069 firstbuf = buf;
2070 }
2071 else /* append new buffer at end of list */
2072 {
2073 lastbuf->b_next = buf;
2074 buf->b_prev = lastbuf;
2075 }
2076 lastbuf = buf;
2077
2078 buf->b_fnum = top_file_num++;
2079 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2080 {
2081 EMSG(_("W14: Warning: List of file names overflow"));
2082 if (emsg_silent == 0)
2083 {
2084 out_flush();
2085 ui_delay(3000L, TRUE); /* make sure it is noticed */
2086 }
2087 top_file_num = 1;
2088 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002089 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 /*
2092 * Always copy the options from the current buffer.
2093 */
2094 buf_copy_options(buf, BCO_ALWAYS);
2095 }
2096
2097 buf->b_wininfo->wi_fpos.lnum = lnum;
2098 buf->b_wininfo->wi_win = curwin;
2099
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002100#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002101 hash_init(&buf->b_s.b_keywtab);
2102 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103#endif
2104
2105 buf->b_fname = buf->b_sfname;
2106#ifdef UNIX
2107 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002108 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 else
2110 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002111 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 buf->b_dev = st.st_dev;
2113 buf->b_ino = st.st_ino;
2114 }
2115#endif
2116 buf->b_u_synced = TRUE;
2117 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002118 if (flags & BLN_DUMMY)
2119 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 buf_clear_file(buf);
2121 clrallmarks(buf); /* clear marks */
2122 fmarks_check_names(buf); /* check file marks for this file */
2123 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2124#ifdef FEAT_AUTOCMD
2125 if (!(flags & BLN_DUMMY))
2126 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002127 bufref_T bufref;
2128
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002129 /* Tricky: these autocommands may change the buffer list. They could
2130 * also split the window with re-using the one empty buffer. This may
2131 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002132 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002133 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002134 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002135 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002137 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002138 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002139 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002140 return NULL;
2141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002143 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 return NULL;
2145# endif
2146 }
2147#endif
2148
2149 return buf;
2150}
2151
2152/*
2153 * Free the memory for the options of a buffer.
2154 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2155 * 'fileencoding'.
2156 */
2157 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002158free_buf_options(
2159 buf_T *buf,
2160 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162 if (free_p_ff)
2163 {
2164#ifdef FEAT_MBYTE
2165 clear_string_option(&buf->b_p_fenc);
2166#endif
2167 clear_string_option(&buf->b_p_ff);
2168#ifdef FEAT_QUICKFIX
2169 clear_string_option(&buf->b_p_bh);
2170 clear_string_option(&buf->b_p_bt);
2171#endif
2172 }
2173#ifdef FEAT_FIND_ID
2174 clear_string_option(&buf->b_p_def);
2175 clear_string_option(&buf->b_p_inc);
2176# ifdef FEAT_EVAL
2177 clear_string_option(&buf->b_p_inex);
2178# endif
2179#endif
2180#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2181 clear_string_option(&buf->b_p_inde);
2182 clear_string_option(&buf->b_p_indk);
2183#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002184#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2185 clear_string_option(&buf->b_p_bexpr);
2186#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002187#if defined(FEAT_CRYPT)
2188 clear_string_option(&buf->b_p_cm);
2189#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002190 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002191#if defined(FEAT_EVAL)
2192 clear_string_option(&buf->b_p_fex);
2193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#ifdef FEAT_CRYPT
2195 clear_string_option(&buf->b_p_key);
2196#endif
2197 clear_string_option(&buf->b_p_kp);
2198 clear_string_option(&buf->b_p_mps);
2199 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002200 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 clear_string_option(&buf->b_p_isk);
2202#ifdef FEAT_KEYMAP
2203 clear_string_option(&buf->b_p_keymap);
2204 ga_clear(&buf->b_kmap_ga);
2205#endif
2206#ifdef FEAT_COMMENTS
2207 clear_string_option(&buf->b_p_com);
2208#endif
2209#ifdef FEAT_FOLDING
2210 clear_string_option(&buf->b_p_cms);
2211#endif
2212 clear_string_option(&buf->b_p_nf);
2213#ifdef FEAT_SYN_HL
2214 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002215 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002216#endif
2217#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002218 clear_string_option(&buf->b_s.b_p_spc);
2219 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002220 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002221 buf->b_s.b_cap_prog = NULL;
2222 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223#endif
2224#ifdef FEAT_SEARCHPATH
2225 clear_string_option(&buf->b_p_sua);
2226#endif
2227#ifdef FEAT_AUTOCMD
2228 clear_string_option(&buf->b_p_ft);
2229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230#ifdef FEAT_CINDENT
2231 clear_string_option(&buf->b_p_cink);
2232 clear_string_option(&buf->b_p_cino);
2233#endif
2234#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2235 clear_string_option(&buf->b_p_cinw);
2236#endif
2237#ifdef FEAT_INS_EXPAND
2238 clear_string_option(&buf->b_p_cpt);
2239#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002240#ifdef FEAT_COMPL_FUNC
2241 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002242 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244#ifdef FEAT_QUICKFIX
2245 clear_string_option(&buf->b_p_gp);
2246 clear_string_option(&buf->b_p_mp);
2247 clear_string_option(&buf->b_p_efm);
2248#endif
2249 clear_string_option(&buf->b_p_ep);
2250 clear_string_option(&buf->b_p_path);
2251 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002252 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#ifdef FEAT_INS_EXPAND
2254 clear_string_option(&buf->b_p_dict);
2255 clear_string_option(&buf->b_p_tsr);
2256#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002257#ifdef FEAT_TEXTOBJ
2258 clear_string_option(&buf->b_p_qe);
2259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002261 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002262#ifdef FEAT_LISP
2263 clear_string_option(&buf->b_p_lw);
2264#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002265 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002266#ifdef FEAT_MBYTE
2267 clear_string_option(&buf->b_p_menc);
2268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269}
2270
2271/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002272 * Get alternate file "n".
2273 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2274 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 * if (options & GETF_SETMARK) call setpcmark()
2276 * if (options & GETF_ALT) we are jumping to an alternate file.
2277 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2278 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002279 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 */
2281 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002282buflist_getfile(
2283 int n,
2284 linenr_T lnum,
2285 int options,
2286 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 buf_T *buf;
2289#ifdef FEAT_WINDOWS
2290 win_T *wp = NULL;
2291#endif
2292 pos_T *fpos;
2293 colnr_T col;
2294
2295 buf = buflist_findnr(n);
2296 if (buf == NULL)
2297 {
2298 if ((options & GETF_ALT) && n == 0)
2299 EMSG(_(e_noalt));
2300 else
2301 EMSGN(_("E92: Buffer %ld not found"), n);
2302 return FAIL;
2303 }
2304
2305 /* if alternate file is the current buffer, nothing to do */
2306 if (buf == curbuf)
2307 return OK;
2308
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002309 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002310 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002311 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002313 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002314#ifdef FEAT_AUTOCMD
2315 if (curbuf_locked())
2316 return FAIL;
2317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
2319 /* altfpos may be changed by getfile(), get it now */
2320 if (lnum == 0)
2321 {
2322 fpos = buflist_findfpos(buf);
2323 lnum = fpos->lnum;
2324 col = fpos->col;
2325 }
2326 else
2327 col = 0;
2328
2329#ifdef FEAT_WINDOWS
2330 if (options & GETF_SWITCH)
2331 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002332 /* If 'switchbuf' contains "useopen": jump to first window containing
2333 * "buf" if one exists */
2334 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002336
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002337 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002338 * page containing "buf" if one exists */
2339 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002340 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002341
2342 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2343 * current buffer isn't empty: open new tab or window */
2344 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002345 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002347 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002348 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002349 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2350 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002352 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354 }
2355#endif
2356
2357 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002358 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2359 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361 --RedrawingDisabled;
2362
2363 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2364 if (!p_sol && col != 0)
2365 {
2366 curwin->w_cursor.col = col;
2367 check_cursor_col();
2368#ifdef FEAT_VIRTUALEDIT
2369 curwin->w_cursor.coladd = 0;
2370#endif
2371 curwin->w_set_curswant = TRUE;
2372 }
2373 return OK;
2374 }
2375 --RedrawingDisabled;
2376 return FAIL;
2377}
2378
2379/*
2380 * go to the last know line number for the current buffer
2381 */
2382 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002383buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 pos_T *fpos;
2386
2387 fpos = buflist_findfpos(curbuf);
2388
2389 curwin->w_cursor.lnum = fpos->lnum;
2390 check_cursor_lnum();
2391
2392 if (p_sol)
2393 curwin->w_cursor.col = 0;
2394 else
2395 {
2396 curwin->w_cursor.col = fpos->col;
2397 check_cursor_col();
2398#ifdef FEAT_VIRTUALEDIT
2399 curwin->w_cursor.coladd = 0;
2400#endif
2401 curwin->w_set_curswant = TRUE;
2402 }
2403}
2404
Bram Moolenaar81695252004-12-29 20:58:21 +00002405#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2406/*
2407 * Find file in buffer list by name (it has to be for the current window).
2408 * Returns NULL if not found.
2409 */
2410 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002411buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002412{
2413 char_u *ffname;
2414 buf_T *buf = NULL;
2415
2416 /* First make the name into a full path name */
2417 ffname = FullName_save(fname,
2418#ifdef UNIX
2419 TRUE /* force expansion, get rid of symbolic links */
2420#else
2421 FALSE
2422#endif
2423 );
2424 if (ffname != NULL)
2425 {
2426 buf = buflist_findname(ffname);
2427 vim_free(ffname);
2428 }
2429 return buf;
2430}
2431#endif
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433/*
2434 * Find file in buffer list by name (it has to be for the current window).
2435 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002436 * Skips dummy buffers.
2437 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 */
2439 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002440buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002443 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 if (mch_stat((char *)ffname, &st) < 0)
2446 st.st_dev = (dev_T)-1;
2447 return buflist_findname_stat(ffname, &st);
2448}
2449
2450/*
2451 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2452 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002453 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 */
2455 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002456buflist_findname_stat(
2457 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002458 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
2460#endif
2461 buf_T *buf;
2462
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002463 /* Start at the last buffer, expect to find a match sooner. */
2464 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002465 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466#ifdef UNIX
2467 , stp
2468#endif
2469 ))
2470 return buf;
2471 return NULL;
2472}
2473
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002474#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2475 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476/*
2477 * Find file in buffer list by a regexp pattern.
2478 * Return fnum of the found buffer.
2479 * Return < 0 for error.
2480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002482buflist_findpat(
2483 char_u *pattern,
2484 char_u *pattern_end, /* pointer to first char after pattern */
2485 int unlisted, /* find unlisted buffers */
2486 int diffmode UNUSED, /* find diff-mode buffers only */
2487 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
2489 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 int match = -1;
2491 int find_listed;
2492 char_u *pat;
2493 char_u *patend;
2494 int attempt;
2495 char_u *p;
2496 int toggledollar;
2497
2498 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2499 {
2500 if (*pattern == '%')
2501 match = curbuf->b_fnum;
2502 else
2503 match = curwin->w_alt_fnum;
2504#ifdef FEAT_DIFF
2505 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2506 match = -1;
2507#endif
2508 }
2509
2510 /*
2511 * Try four ways of matching a listed buffer:
2512 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002513 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 * attempt == 2: with '$' at end (only match at end)
2515 * attempt == 3: with '^' at start and '$' at end (only full match)
2516 * Repeat this for finding an unlisted buffer if there was no matching
2517 * listed buffer.
2518 */
2519 else
2520 {
2521 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2522 if (pat == NULL)
2523 return -1;
2524 patend = pat + STRLEN(pat) - 1;
2525 toggledollar = (patend > pat && *patend == '$');
2526
2527 /* First try finding a listed buffer. If not found and "unlisted"
2528 * is TRUE, try finding an unlisted buffer. */
2529 find_listed = TRUE;
2530 for (;;)
2531 {
2532 for (attempt = 0; attempt <= 3; ++attempt)
2533 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002534 regmatch_T regmatch;
2535
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 /* may add '^' and '$' */
2537 if (toggledollar)
2538 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2539 p = pat;
2540 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2541 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002542 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2543 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 vim_free(pat);
2546 return -1;
2547 }
2548
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002549 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 if (buf->b_p_bl == find_listed
2551#ifdef FEAT_DIFF
2552 && (!diffmode || diff_mode_buf(buf))
2553#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002554 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002556 if (curtab_only)
2557 {
2558 /* Ignore the match if the buffer is not open in
2559 * the current tab. */
2560#ifdef FEAT_WINDOWS
2561 win_T *wp;
2562
Bram Moolenaar29323592016-07-24 22:04:11 +02002563 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002564 if (wp->w_buffer == buf)
2565 break;
2566 if (wp == NULL)
2567 continue;
2568#else
2569 if (curwin->w_buffer != buf)
2570 continue;
2571#endif
2572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (match >= 0) /* already found a match */
2574 {
2575 match = -2;
2576 break;
2577 }
2578 match = buf->b_fnum; /* remember first match */
2579 }
2580
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002581 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (match >= 0) /* found one match */
2583 break;
2584 }
2585
2586 /* Only search for unlisted buffers if there was no match with
2587 * a listed buffer. */
2588 if (!unlisted || !find_listed || match != -1)
2589 break;
2590 find_listed = FALSE;
2591 }
2592
2593 vim_free(pat);
2594 }
2595
2596 if (match == -2)
2597 EMSG2(_("E93: More than one match for %s"), pattern);
2598 else if (match < 0)
2599 EMSG2(_("E94: No matching buffer for %s"), pattern);
2600 return match;
2601}
2602#endif
2603
2604#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2605
2606/*
2607 * Find all buffer names that match.
2608 * For command line expansion of ":buf" and ":sbuf".
2609 * Return OK if matches found, FAIL otherwise.
2610 */
2611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002612ExpandBufnames(
2613 char_u *pat,
2614 int *num_file,
2615 char_u ***file,
2616 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 int count = 0;
2619 buf_T *buf;
2620 int round;
2621 char_u *p;
2622 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002623 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 *num_file = 0; /* return values in case of FAIL */
2626 *file = NULL;
2627
Bram Moolenaar05159a02005-02-26 23:04:13 +00002628 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2629 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002631 patc = alloc((unsigned)STRLEN(pat) + 11);
2632 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002634 STRCPY(patc, "\\(^\\|[\\/]\\)");
2635 STRCPY(patc + 11, pat + 1);
2636 }
2637 else
2638 patc = pat;
2639
2640 /*
2641 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002642 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002643 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002644 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002645 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002646 regmatch_T regmatch;
2647
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002648 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002649 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002650 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2651 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002652 {
2653 if (patc != pat)
2654 vim_free(patc);
2655 return FAIL;
2656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 /*
2659 * round == 1: Count the matches.
2660 * round == 2: Build the array to keep the matches.
2661 */
2662 for (round = 1; round <= 2; ++round)
2663 {
2664 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002665 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 {
2667 if (!buf->b_p_bl) /* skip unlisted buffers */
2668 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002669 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 if (p != NULL)
2671 {
2672 if (round == 1)
2673 ++count;
2674 else
2675 {
2676 if (options & WILD_HOME_REPLACE)
2677 p = home_replace_save(buf, p);
2678 else
2679 p = vim_strsave(p);
2680 (*file)[count++] = p;
2681 }
2682 }
2683 }
2684 if (count == 0) /* no match found, break here */
2685 break;
2686 if (round == 1)
2687 {
2688 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2689 if (*file == NULL)
2690 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002691 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002692 if (patc != pat)
2693 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 return FAIL;
2695 }
2696 }
2697 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002698 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (count) /* match(es) found, break here */
2700 break;
2701 }
2702
Bram Moolenaar05159a02005-02-26 23:04:13 +00002703 if (patc != pat)
2704 vim_free(patc);
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 *num_file = count;
2707 return (count == 0 ? FAIL : OK);
2708}
2709
2710#endif /* FEAT_CMDL_COMPL */
2711
2712#ifdef HAVE_BUFLIST_MATCH
2713/*
2714 * Check for a match on the file name for buffer "buf" with regprog "prog".
2715 */
2716 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002717buflist_match(
2718 regmatch_T *rmp,
2719 buf_T *buf,
2720 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 char_u *match;
2723
2724 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002725 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002727 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 return match;
2730}
2731
2732/*
2733 * Try matching the regexp in "prog" with file name "name".
2734 * Return "name" when there is a match, NULL when not.
2735 */
2736 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002737fname_match(
2738 regmatch_T *rmp,
2739 char_u *name,
2740 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 char_u *match = NULL;
2743 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 if (name != NULL)
2746 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002747 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002748 rmp->rm_ic = p_fic || ignore_case;
2749 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 match = name;
2751 else
2752 {
2753 /* Replace $(HOME) with '~' and try matching again. */
2754 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002755 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 match = name;
2757 vim_free(p);
2758 }
2759 }
2760
2761 return match;
2762}
2763#endif
2764
2765/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002766 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 */
2768 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002769buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002771 char_u key[VIM_SIZEOF_INT * 2 + 1];
2772 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774 if (nr == 0)
2775 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002776 sprintf((char *)key, "%x", nr);
2777 hi = hash_find(&buf_hashtab, key);
2778
2779 if (!HASHITEM_EMPTY(hi))
2780 return (buf_T *)(hi->hi_key
2781 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 return NULL;
2783}
2784
2785/*
2786 * Get name of file 'n' in the buffer list.
2787 * When the file has no name an empty string is returned.
2788 * home_replace() is used to shorten the file name (used for marks).
2789 * Returns a pointer to allocated memory, of NULL when failed.
2790 */
2791 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002792buflist_nr2name(
2793 int n,
2794 int fullname,
2795 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796{
2797 buf_T *buf;
2798
2799 buf = buflist_findnr(n);
2800 if (buf == NULL)
2801 return NULL;
2802 return home_replace_save(helptail ? buf : NULL,
2803 fullname ? buf->b_ffname : buf->b_fname);
2804}
2805
2806/*
2807 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2808 * When "copy_options" is TRUE save the local window option values.
2809 * When "lnum" is 0 only do the options.
2810 */
2811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002812buflist_setfpos(
2813 buf_T *buf,
2814 win_T *win,
2815 linenr_T lnum,
2816 colnr_T col,
2817 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819 wininfo_T *wip;
2820
2821 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2822 if (wip->wi_win == win)
2823 break;
2824 if (wip == NULL)
2825 {
2826 /* allocate a new entry */
2827 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2828 if (wip == NULL)
2829 return;
2830 wip->wi_win = win;
2831 if (lnum == 0) /* set lnum even when it's 0 */
2832 lnum = 1;
2833 }
2834 else
2835 {
2836 /* remove the entry from the list */
2837 if (wip->wi_prev)
2838 wip->wi_prev->wi_next = wip->wi_next;
2839 else
2840 buf->b_wininfo = wip->wi_next;
2841 if (wip->wi_next)
2842 wip->wi_next->wi_prev = wip->wi_prev;
2843 if (copy_options && wip->wi_optset)
2844 {
2845 clear_winopt(&wip->wi_opt);
2846#ifdef FEAT_FOLDING
2847 deleteFoldRecurse(&wip->wi_folds);
2848#endif
2849 }
2850 }
2851 if (lnum != 0)
2852 {
2853 wip->wi_fpos.lnum = lnum;
2854 wip->wi_fpos.col = col;
2855 }
2856 if (copy_options)
2857 {
2858 /* Save the window-specific option values. */
2859 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2860#ifdef FEAT_FOLDING
2861 wip->wi_fold_manual = win->w_fold_manual;
2862 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2863#endif
2864 wip->wi_optset = TRUE;
2865 }
2866
2867 /* insert the entry in front of the list */
2868 wip->wi_next = buf->b_wininfo;
2869 buf->b_wininfo = wip;
2870 wip->wi_prev = NULL;
2871 if (wip->wi_next)
2872 wip->wi_next->wi_prev = wip;
2873
2874 return;
2875}
2876
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002877#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002878static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002879
2880/*
2881 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2882 * page. That's because a diff is local to a tab page.
2883 */
2884 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002885wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002886{
2887 win_T *wp;
2888
2889 if (wip->wi_opt.wo_diff)
2890 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002891 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002892 /* return FALSE when it's a window in the current tab page, thus
2893 * the buffer was in diff mode here */
2894 if (wip->wi_win == wp)
2895 return FALSE;
2896 return TRUE;
2897 }
2898 return FALSE;
2899}
2900#endif
2901
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902/*
2903 * Find info for the current window in buffer "buf".
2904 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002905 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2906 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 * Returns NULL when there isn't any info.
2908 */
2909 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002910find_wininfo(
2911 buf_T *buf,
2912 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 wininfo_T *wip;
2915
2916 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002917 if (wip->wi_win == curwin
2918#ifdef FEAT_DIFF
2919 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2920#endif
2921 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002923
2924 /* If no wininfo for curwin, use the first in the list (that doesn't have
2925 * 'diff' set and is in another tab page). */
2926 if (wip == NULL)
2927 {
2928#ifdef FEAT_DIFF
2929 if (skip_diff_buffer)
2930 {
2931 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2932 if (!wininfo_other_tab_diff(wip))
2933 break;
2934 }
2935 else
2936#endif
2937 wip = buf->b_wininfo;
2938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 return wip;
2940}
2941
2942/*
2943 * Reset the local window options to the values last used in this window.
2944 * If the buffer wasn't used in this window before, use the values from
2945 * the most recently used window. If the values were never set, use the
2946 * global values for the window.
2947 */
2948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002949get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 wininfo_T *wip;
2952
2953 clear_winopt(&curwin->w_onebuf_opt);
2954#ifdef FEAT_FOLDING
2955 clearFolding(curwin);
2956#endif
2957
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002958 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 if (wip != NULL && wip->wi_optset)
2960 {
2961 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2962#ifdef FEAT_FOLDING
2963 curwin->w_fold_manual = wip->wi_fold_manual;
2964 curwin->w_foldinvalid = TRUE;
2965 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2966#endif
2967 }
2968 else
2969 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2970
2971#ifdef FEAT_FOLDING
2972 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2973 if (p_fdls >= 0)
2974 curwin->w_p_fdl = p_fdls;
2975#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002976#ifdef FEAT_SYN_HL
2977 check_colorcolumn(curwin);
2978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979}
2980
2981/*
2982 * Find the position (lnum and col) for the buffer 'buf' for the current
2983 * window.
2984 * Returns a pointer to no_position if no position is found.
2985 */
2986 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002987buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002990 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002992 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 if (wip != NULL)
2994 return &(wip->wi_fpos);
2995 else
2996 return &no_position;
2997}
2998
2999/*
3000 * Find the lnum for the buffer 'buf' for the current window.
3001 */
3002 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003003buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 return buflist_findfpos(buf)->lnum;
3006}
3007
3008#if defined(FEAT_LISTCMDS) || defined(PROTO)
3009/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003010 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003013buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 buf_T *buf;
3016 int len;
3017 int i;
3018
3019 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3020 {
3021 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003022 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3023 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3024 || (vim_strchr(eap->arg, '+')
3025 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3026 || (vim_strchr(eap->arg, 'a')
3027 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
3028 || (vim_strchr(eap->arg, 'h')
3029 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3030 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3031 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3032 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3033 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3034 || (vim_strchr(eap->arg, '#')
3035 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003038 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 else
3040 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003041 if (message_filtered(NameBuff))
3042 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003044 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003045 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 buf->b_fnum,
3047 buf->b_p_bl ? ' ' : 'u',
3048 buf == curbuf ? '%' :
3049 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3050 buf->b_ml.ml_mfp == NULL ? ' ' :
3051 (buf->b_nwindows == 0 ? 'h' : 'a'),
3052 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
3053 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00003054 : (bufIsChanged(buf) ? '+' : ' '),
3055 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003056 if (len > IOSIZE - 20)
3057 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
3059 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 i = 40 - vim_strsize(IObuff);
3061 do
3062 {
3063 IObuff[len++] = ' ';
3064 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003065 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3066 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003067 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 msg_outtrans(IObuff);
3069 out_flush(); /* output one line at a time */
3070 ui_breakcheck();
3071 }
3072}
3073#endif
3074
3075/*
3076 * Get file name and line number for file 'fnum'.
3077 * Used by DoOneCmd() for translating '%' and '#'.
3078 * Used by insert_reg() and cmdline_paste() for '#' register.
3079 * Return FAIL if not found, OK for success.
3080 */
3081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003082buflist_name_nr(
3083 int fnum,
3084 char_u **fname,
3085 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 buf_T *buf;
3088
3089 buf = buflist_findnr(fnum);
3090 if (buf == NULL || buf->b_fname == NULL)
3091 return FAIL;
3092
3093 *fname = buf->b_fname;
3094 *lnum = buflist_findlnum(buf);
3095
3096 return OK;
3097}
3098
3099/*
3100 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3101 * The file name with the full path is also remembered, for when :cd is used.
3102 * Returns FAIL for failure (file name already in use by other buffer)
3103 * OK otherwise.
3104 */
3105 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003106setfname(
3107 buf_T *buf,
3108 char_u *ffname,
3109 char_u *sfname,
3110 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
Bram Moolenaar81695252004-12-29 20:58:21 +00003112 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003114 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#endif
3116
3117 if (ffname == NULL || *ffname == NUL)
3118 {
3119 /* Removing the name. */
3120 vim_free(buf->b_ffname);
3121 vim_free(buf->b_sfname);
3122 buf->b_ffname = NULL;
3123 buf->b_sfname = NULL;
3124#ifdef UNIX
3125 st.st_dev = (dev_T)-1;
3126#endif
3127 }
3128 else
3129 {
3130 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3131 if (ffname == NULL) /* out of memory */
3132 return FAIL;
3133
3134 /*
3135 * if the file name is already used in another buffer:
3136 * - if the buffer is loaded, fail
3137 * - if the buffer is not loaded, delete it from the list
3138 */
3139#ifdef UNIX
3140 if (mch_stat((char *)ffname, &st) < 0)
3141 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003142#endif
3143 if (!(buf->b_flags & BF_DUMMY))
3144#ifdef UNIX
3145 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003147 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#endif
3149 if (obuf != NULL && obuf != buf)
3150 {
3151 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3152 {
3153 if (message)
3154 EMSG(_("E95: Buffer with this name already exists"));
3155 vim_free(ffname);
3156 return FAIL;
3157 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003158 /* delete from the list */
3159 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161 sfname = vim_strsave(sfname);
3162 if (ffname == NULL || sfname == NULL)
3163 {
3164 vim_free(sfname);
3165 vim_free(ffname);
3166 return FAIL;
3167 }
3168#ifdef USE_FNAME_CASE
3169# ifdef USE_LONG_FNAME
3170 if (USE_LONG_FNAME)
3171# endif
3172 fname_case(sfname, 0); /* set correct case for short file name */
3173#endif
3174 vim_free(buf->b_ffname);
3175 vim_free(buf->b_sfname);
3176 buf->b_ffname = ffname;
3177 buf->b_sfname = sfname;
3178 }
3179 buf->b_fname = buf->b_sfname;
3180#ifdef UNIX
3181 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003182 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 else
3184 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003185 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 buf->b_dev = st.st_dev;
3187 buf->b_ino = st.st_ino;
3188 }
3189#endif
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
3193 buf_name_changed(buf);
3194 return OK;
3195}
3196
3197/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003198 * Crude way of changing the name of a buffer. Use with care!
3199 * The name should be relative to the current directory.
3200 */
3201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003203{
3204 buf_T *buf;
3205
3206 buf = buflist_findnr(fnum);
3207 if (buf != NULL)
3208 {
3209 vim_free(buf->b_sfname);
3210 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003211 buf->b_ffname = vim_strsave(name);
3212 buf->b_sfname = NULL;
3213 /* Allocate ffname and expand into full path. Also resolves .lnk
3214 * files on Win32. */
3215 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003216 buf->b_fname = buf->b_sfname;
3217 }
3218}
3219
3220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 * Take care of what needs to be done when the name of buffer "buf" has
3222 * changed.
3223 */
3224 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 /*
3228 * If the file name changed, also change the name of the swapfile
3229 */
3230 if (buf->b_ml.ml_mfp != NULL)
3231 ml_setname(buf);
3232
3233 if (curwin->w_buffer == buf)
3234 check_arg_idx(curwin); /* check file name for arg list */
3235#ifdef FEAT_TITLE
3236 maketitle(); /* set window title */
3237#endif
3238#ifdef FEAT_WINDOWS
3239 status_redraw_all(); /* status lines need to be redrawn */
3240#endif
3241 fmarks_check_names(buf); /* check named file marks */
3242 ml_timestamp(buf); /* reset timestamp */
3243}
3244
3245/*
3246 * set alternate file name for current window
3247 *
3248 * Used by do_one_cmd(), do_write() and do_ecmd().
3249 * Return the buffer.
3250 */
3251 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252setaltfname(
3253 char_u *ffname,
3254 char_u *sfname,
3255 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 buf_T *buf;
3258
3259 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3260 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003261 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 curwin->w_alt_fnum = buf->b_fnum;
3263 return buf;
3264}
3265
3266/*
3267 * Get alternate file name for current window.
3268 * Return NULL if there isn't any, and give error message if requested.
3269 */
3270 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271getaltfname(
3272 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 char_u *fname;
3275 linenr_T dummy;
3276
3277 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3278 {
3279 if (errmsg)
3280 EMSG(_(e_noalt));
3281 return NULL;
3282 }
3283 return fname;
3284}
3285
3286/*
3287 * Add a file name to the buflist and return its number.
3288 * Uses same flags as buflist_new(), except BLN_DUMMY.
3289 *
3290 * used by qf_init(), main() and doarglist()
3291 */
3292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 buf_T *buf;
3296
3297 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3298 if (buf != NULL)
3299 return buf->b_fnum;
3300 return 0;
3301}
3302
3303#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3304/*
3305 * Adjust slashes in file names. Called after 'shellslash' was set.
3306 */
3307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003308buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 buf_T *bp;
3311
Bram Moolenaar29323592016-07-24 22:04:11 +02003312 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314 if (bp->b_ffname != NULL)
3315 slash_adjust(bp->b_ffname);
3316 if (bp->b_sfname != NULL)
3317 slash_adjust(bp->b_sfname);
3318 }
3319}
3320#endif
3321
3322/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003323 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 * Also save the local window option values.
3325 */
3326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003329 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330}
3331
3332/*
3333 * Return TRUE if 'ffname' is not the same file as current file.
3334 * Fname must have a full path (expanded by mch_FullName()).
3335 */
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 return otherfile_buf(curbuf, ffname
3340#ifdef UNIX
3341 , NULL
3342#endif
3343 );
3344}
3345
3346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003347otherfile_buf(
3348 buf_T *buf,
3349 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003351 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003353 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 /* no name is different */
3356 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3357 return TRUE;
3358 if (fnamecmp(ffname, buf->b_ffname) == 0)
3359 return FALSE;
3360#ifdef UNIX
3361 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003362 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaar8767f522016-07-01 17:17:39 +02003364 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (stp == NULL)
3366 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003367 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 st.st_dev = (dev_T)-1;
3369 stp = &st;
3370 }
3371 /* Use dev/ino to check if the files are the same, even when the names
3372 * are different (possible with links). Still need to compare the
3373 * name above, for when the file doesn't exist yet.
3374 * Problem: The dev/ino changes when a file is deleted (and created
3375 * again) and remains the same when renamed/moved. We don't want to
3376 * mch_stat() each buffer each time, that would be too slow. Get the
3377 * dev/ino again when they appear to match, but not when they appear
3378 * to be different: Could skip a buffer when it's actually the same
3379 * file. */
3380 if (buf_same_ino(buf, stp))
3381 {
3382 buf_setino(buf);
3383 if (buf_same_ino(buf, stp))
3384 return FALSE;
3385 }
3386 }
3387#endif
3388 return TRUE;
3389}
3390
3391#if defined(UNIX) || defined(PROTO)
3392/*
3393 * Set inode and device number for a buffer.
3394 * Must always be called when b_fname is changed!.
3395 */
3396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003397buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003399 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
3401 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3402 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003403 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 buf->b_dev = st.st_dev;
3405 buf->b_ino = st.st_ino;
3406 }
3407 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003408 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409}
3410
3411/*
3412 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3413 */
3414 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003415buf_same_ino(
3416 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003417 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003419 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 && stp->st_dev == buf->b_dev
3421 && stp->st_ino == buf->b_ino);
3422}
3423#endif
3424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003425/*
3426 * Print info about the current buffer.
3427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003429fileinfo(
3430 int fullname, /* when non-zero print full path */
3431 int shorthelp,
3432 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 char_u *name;
3435 int n;
3436 char_u *p;
3437 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003438 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 buffer = alloc(IOSIZE);
3441 if (buffer == NULL)
3442 return;
3443
3444 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3445 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003446 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 p = buffer + STRLEN(buffer);
3448 }
3449 else
3450 p = buffer;
3451
3452 *p++ = '"';
3453 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003454 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else
3456 {
3457 if (!fullname && curbuf->b_fname != NULL)
3458 name = curbuf->b_fname;
3459 else
3460 name = curbuf->b_ffname;
3461 home_replace(shorthelp ? curbuf : NULL, name, p,
3462 (int)(IOSIZE - (p - buffer)), TRUE);
3463 }
3464
Bram Moolenaara800b422010-06-27 01:15:55 +02003465 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 curbufIsChanged() ? (shortmess(SHM_MOD)
3467 ? " [+]" : _(" [Modified]")) : " ",
3468 (curbuf->b_flags & BF_NOTEDITED)
3469#ifdef FEAT_QUICKFIX
3470 && !bt_dontwrite(curbuf)
3471#endif
3472 ? _("[Not edited]") : "",
3473 (curbuf->b_flags & BF_NEW)
3474#ifdef FEAT_QUICKFIX
3475 && !bt_dontwrite(curbuf)
3476#endif
3477 ? _("[New file]") : "",
3478 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003479 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 : _("[readonly]")) : "",
3481 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3482 || curbuf->b_p_ro) ?
3483 " " : "");
3484 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3485 * causes an overflow, thus for large numbers divide instead. */
3486 if (curwin->w_cursor.lnum > 1000000L)
3487 n = (int)(((long)curwin->w_cursor.lnum) /
3488 ((long)curbuf->b_ml.ml_line_count / 100L));
3489 else
3490 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3491 (long)curbuf->b_ml.ml_line_count);
3492 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3493 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003494 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496#ifdef FEAT_CMDL_INFO
3497 else if (p_ru)
3498 {
3499 /* Current line and column are already on the screen -- webb */
3500 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003501 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003503 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 (long)curbuf->b_ml.ml_line_count, n);
3505 }
3506#endif
3507 else
3508 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003509 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 _("line %ld of %ld --%d%%-- col "),
3511 (long)curwin->w_cursor.lnum,
3512 (long)curbuf->b_ml.ml_line_count,
3513 n);
3514 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003515 len = STRLEN(buffer);
3516 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3518 }
3519
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003520 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
3522 if (dont_truncate)
3523 {
3524 /* Temporarily set msg_scroll to avoid the message being truncated.
3525 * First call msg_start() to get the message in the right place. */
3526 msg_start();
3527 n = msg_scroll;
3528 msg_scroll = TRUE;
3529 msg(buffer);
3530 msg_scroll = n;
3531 }
3532 else
3533 {
3534 p = msg_trunc_attr(buffer, FALSE, 0);
3535 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /* Need to repeat the message after redrawing when:
3537 * - When restart_edit is set (otherwise there will be a delay
3538 * before redrawing).
3539 * - When the screen was scrolled but there is no wait-return
3540 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003541 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543
3544 vim_free(buffer);
3545}
3546
3547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548col_print(
3549 char_u *buf,
3550 size_t buflen,
3551 int col,
3552 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
3554 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003555 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003557 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558}
3559
3560#if defined(FEAT_TITLE) || defined(PROTO)
3561/*
3562 * put file name in title bar of window and in icon title
3563 */
3564
3565static char_u *lasttitle = NULL;
3566static char_u *lasticon = NULL;
3567
3568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003569maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571 char_u *p;
3572 char_u *t_str = NULL;
3573 char_u *i_name;
3574 char_u *i_str = NULL;
3575 int maxlen = 0;
3576 int len;
3577 int mustset;
3578 char_u buf[IOSIZE];
3579 int off;
3580
3581 if (!redrawing())
3582 {
3583 /* Postpone updating the title when 'lazyredraw' is set. */
3584 need_maketitle = TRUE;
3585 return;
3586 }
3587
3588 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003589 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 return;
3591
3592 if (p_title)
3593 {
3594 if (p_titlelen > 0)
3595 {
3596 maxlen = p_titlelen * Columns / 100;
3597 if (maxlen < 10)
3598 maxlen = 10;
3599 }
3600
3601 t_str = buf;
3602 if (*p_titlestring != NUL)
3603 {
3604#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003605 if (stl_syntax & STL_IN_TITLE)
3606 {
3607 int use_sandbox = FALSE;
3608 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003609
3610# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003611 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003612# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003613 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003615 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003616 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003617 if (called_emsg)
3618 set_string_option_direct((char_u *)"titlestring", -1,
3619 (char_u *)"", OPT_FREE, SID_ERROR);
3620 called_emsg |= save_called_emsg;
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 else
3623#endif
3624 t_str = p_titlestring;
3625 }
3626 else
3627 {
3628 /* format: "fname + (path) (1 of 2) - VIM" */
3629
Bram Moolenaar2c666692012-09-05 13:30:40 +02003630#define SPACE_FOR_FNAME (IOSIZE - 100)
3631#define SPACE_FOR_DIR (IOSIZE - 20)
3632#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003634 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 else
3636 {
3637 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003638 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
3641
3642 switch (bufIsChanged(curbuf)
3643 + (curbuf->b_p_ro * 2)
3644 + (!curbuf->b_p_ma * 4))
3645 {
3646 case 1: STRCAT(buf, " +"); break;
3647 case 2: STRCAT(buf, " ="); break;
3648 case 3: STRCAT(buf, " =+"); break;
3649 case 4:
3650 case 6: STRCAT(buf, " -"); break;
3651 case 5:
3652 case 7: STRCAT(buf, " -+"); break;
3653 }
3654
3655 if (curbuf->b_fname != NULL)
3656 {
3657 /* Get path of file, replace home dir with ~ */
3658 off = (int)STRLEN(buf);
3659 buf[off++] = ' ';
3660 buf[off++] = '(';
3661 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003662 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663#ifdef BACKSLASH_IN_FILENAME
3664 /* avoid "c:/name" to be reduced to "c" */
3665 if (isalpha(buf[off]) && buf[off + 1] == ':')
3666 off += 2;
3667#endif
3668 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003669 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003672 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003673 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003676
Bram Moolenaar2c666692012-09-05 13:30:40 +02003677 /* Translate unprintable chars and concatenate. Keep some
3678 * room for the server name. When there is no room (very long
3679 * file name) use (...). */
3680 if (off < SPACE_FOR_DIR)
3681 {
3682 p = transstr(buf + off);
3683 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3684 vim_free(p);
3685 }
3686 else
3687 {
3688 vim_strncpy(buf + off, (char_u *)"...",
3689 (size_t)(SPACE_FOR_ARGNR - off));
3690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 STRCAT(buf, ")");
3692 }
3693
Bram Moolenaar2c666692012-09-05 13:30:40 +02003694 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
3696#if defined(FEAT_CLIENTSERVER)
3697 if (serverName != NULL)
3698 {
3699 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003700 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702 else
3703#endif
3704 STRCAT(buf, " - VIM");
3705
3706 if (maxlen > 0)
3707 {
3708 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003709 if (vim_strsize(buf) > maxlen)
3710 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712 }
3713 }
3714 mustset = ti_change(t_str, &lasttitle);
3715
3716 if (p_icon)
3717 {
3718 i_str = buf;
3719 if (*p_iconstring != NUL)
3720 {
3721#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003722 if (stl_syntax & STL_IN_ICON)
3723 {
3724 int use_sandbox = FALSE;
3725 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003726
3727# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003728 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003729# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003730 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003732 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003733 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003734 if (called_emsg)
3735 set_string_option_direct((char_u *)"iconstring", -1,
3736 (char_u *)"", OPT_FREE, SID_ERROR);
3737 called_emsg |= save_called_emsg;
3738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 else
3740#endif
3741 i_str = p_iconstring;
3742 }
3743 else
3744 {
3745 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003746 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 else /* use file name only in icon */
3748 i_name = gettail(curbuf->b_ffname);
3749 *i_str = NUL;
3750 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003751 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 if (len > 100)
3753 {
3754 len -= 100;
3755#ifdef FEAT_MBYTE
3756 if (has_mbyte)
3757 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3758#endif
3759 i_name += len;
3760 }
3761 STRCPY(i_str, i_name);
3762 trans_characters(i_str, IOSIZE);
3763 }
3764 }
3765
3766 mustset |= ti_change(i_str, &lasticon);
3767
3768 if (mustset)
3769 resettitle();
3770}
3771
3772/*
3773 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3774 * from "str" if it does.
3775 * Return TRUE when "*last" changed.
3776 */
3777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 if ((str == NULL) != (*last == NULL)
3781 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3782 {
3783 vim_free(*last);
3784 if (str == NULL)
3785 *last = NULL;
3786 else
3787 *last = vim_strsave(str);
3788 return TRUE;
3789 }
3790 return FALSE;
3791}
3792
3793/*
3794 * Put current window title back (used after calling a shell)
3795 */
3796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003797resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798{
3799 mch_settitle(lasttitle, lasticon);
3800}
Bram Moolenaarea408852005-06-25 22:49:46 +00003801
3802# if defined(EXITFREE) || defined(PROTO)
3803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003804free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003805{
3806 vim_free(lasttitle);
3807 vim_free(lasticon);
3808}
3809# endif
3810
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811#endif /* FEAT_TITLE */
3812
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003813#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003815 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 * Return length of string in screen cells.
3817 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003818 * Normally works for window "wp", except when working for 'tabline' then it
3819 * is "curwin".
3820 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 * Items are drawn interspersed with the text that surrounds it
3822 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3823 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3824 *
3825 * If maxwidth is not zero, the string will be filled at any middle marker
3826 * or truncated if too long, fillchar is used for all whitespace.
3827 */
3828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829build_stl_str_hl(
3830 win_T *wp,
3831 char_u *out, /* buffer to write into != NameBuff */
3832 size_t outlen, /* length of out[] */
3833 char_u *fmt,
3834 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3835 int fillchar,
3836 int maxwidth,
3837 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3838 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
3840 char_u *p;
3841 char_u *s;
3842 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003843 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844#ifdef FEAT_EVAL
3845 win_T *o_curwin;
3846 buf_T *o_curbuf;
3847#endif
3848 int empty_line;
3849 colnr_T virtcol;
3850 long l;
3851 long n;
3852 int prevchar_isflag;
3853 int prevchar_isitem;
3854 int itemisflag;
3855 int fillable;
3856 char_u *str;
3857 long num;
3858 int width;
3859 int itemcnt;
3860 int curitem;
3861 int groupitem[STL_MAX_ITEM];
3862 int groupdepth;
3863 struct stl_item
3864 {
3865 char_u *start;
3866 int minwid;
3867 int maxwid;
3868 enum
3869 {
3870 Normal,
3871 Empty,
3872 Group,
3873 Middle,
3874 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003875 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 Trunc
3877 } type;
3878 } item[STL_MAX_ITEM];
3879 int minwid;
3880 int maxwid;
3881 int zeropad;
3882 char_u base;
3883 char_u opt;
3884#define TMPLEN 70
3885 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003886 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003887 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003888
3889#ifdef FEAT_EVAL
3890 /*
3891 * When the format starts with "%!" then evaluate it as an expression and
3892 * use the result as the actual format string.
3893 */
3894 if (fmt[0] == '%' && fmt[1] == '!')
3895 {
3896 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3897 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003898 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003899 }
3900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
3902 if (fillchar == 0)
3903 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003904#ifdef FEAT_MBYTE
3905 /* Can't handle a multi-byte fill character yet. */
3906 else if (mb_char2len(fillchar) > 1)
3907 fillchar = '-';
3908#endif
3909
Bram Moolenaar567199b2013-04-24 16:52:36 +02003910 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3911 * p will become invalid when getting another buffer line. */
3912 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3913 empty_line = (*p == NUL);
3914
3915 /* Get the byte value now, in case we need it below. This is more
3916 * efficient than making a copy of the line. */
3917 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3918 byteval = 0;
3919 else
3920#ifdef FEAT_MBYTE
3921 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3922#else
3923 byteval = p[wp->w_cursor.col];
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926 groupdepth = 0;
3927 p = out;
3928 curitem = 0;
3929 prevchar_isflag = TRUE;
3930 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003931 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003933 if (curitem == STL_MAX_ITEM)
3934 {
3935 /* There are too many items. Add the error code to the statusline
3936 * to give the user a hint about what went wrong. */
3937 if (p + 6 < out + outlen)
3938 {
3939 mch_memmove(p, " E541", (size_t)5);
3940 p += 5;
3941 }
3942 break;
3943 }
3944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (*s != NUL && *s != '%')
3946 prevchar_isflag = prevchar_isitem = FALSE;
3947
3948 /*
3949 * Handle up to the next '%' or the end.
3950 */
3951 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3952 *p++ = *s++;
3953 if (*s == NUL || p + 1 >= out + outlen)
3954 break;
3955
3956 /*
3957 * Handle one '%' item.
3958 */
3959 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003960 if (*s == NUL) /* ignore trailing % */
3961 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 if (*s == '%')
3963 {
3964 if (p + 1 >= out + outlen)
3965 break;
3966 *p++ = *s++;
3967 prevchar_isflag = prevchar_isitem = FALSE;
3968 continue;
3969 }
3970 if (*s == STL_MIDDLEMARK)
3971 {
3972 s++;
3973 if (groupdepth > 0)
3974 continue;
3975 item[curitem].type = Middle;
3976 item[curitem++].start = p;
3977 continue;
3978 }
3979 if (*s == STL_TRUNCMARK)
3980 {
3981 s++;
3982 item[curitem].type = Trunc;
3983 item[curitem++].start = p;
3984 continue;
3985 }
3986 if (*s == ')')
3987 {
3988 s++;
3989 if (groupdepth < 1)
3990 continue;
3991 groupdepth--;
3992
3993 t = item[groupitem[groupdepth]].start;
3994 *p = NUL;
3995 l = vim_strsize(t);
3996 if (curitem > groupitem[groupdepth] + 1
3997 && item[groupitem[groupdepth]].minwid == 0)
3998 {
3999 /* remove group if all items are empty */
4000 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01004001 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 break;
4003 if (n == curitem)
4004 {
4005 p = t;
4006 l = 0;
4007 }
4008 }
4009 if (l > item[groupitem[groupdepth]].maxwid)
4010 {
4011 /* truncate, remove n bytes of text at the start */
4012#ifdef FEAT_MBYTE
4013 if (has_mbyte)
4014 {
4015 /* Find the first character that should be included. */
4016 n = 0;
4017 while (l >= item[groupitem[groupdepth]].maxwid)
4018 {
4019 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004020 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022 }
4023 else
4024#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004025 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004028 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 p = p - n + 1;
4030#ifdef FEAT_MBYTE
4031 /* Fill up space left over by half a double-wide char. */
4032 while (++l < item[groupitem[groupdepth]].minwid)
4033 *p++ = fillchar;
4034#endif
4035
4036 /* correct the start of the items for the truncation */
4037 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4038 {
4039 item[l].start -= n;
4040 if (item[l].start < t)
4041 item[l].start = t;
4042 }
4043 }
4044 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4045 {
4046 /* fill */
4047 n = item[groupitem[groupdepth]].minwid;
4048 if (n < 0)
4049 {
4050 /* fill by appending characters */
4051 n = 0 - n;
4052 while (l++ < n && p + 1 < out + outlen)
4053 *p++ = fillchar;
4054 }
4055 else
4056 {
4057 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004058 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 l = n - l;
4060 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004061 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 p += l;
4063 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4064 item[n].start += l;
4065 for ( ; l > 0; l--)
4066 *t++ = fillchar;
4067 }
4068 }
4069 continue;
4070 }
4071 minwid = 0;
4072 maxwid = 9999;
4073 zeropad = FALSE;
4074 l = 1;
4075 if (*s == '0')
4076 {
4077 s++;
4078 zeropad = TRUE;
4079 }
4080 if (*s == '-')
4081 {
4082 s++;
4083 l = -1;
4084 }
4085 if (VIM_ISDIGIT(*s))
4086 {
4087 minwid = (int)getdigits(&s);
4088 if (minwid < 0) /* overflow */
4089 minwid = 0;
4090 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004091 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
4093 item[curitem].type = Highlight;
4094 item[curitem].start = p;
4095 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4096 s++;
4097 curitem++;
4098 continue;
4099 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004100 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4101 {
4102 if (*s == STL_TABCLOSENR)
4103 {
4104 if (minwid == 0)
4105 {
4106 /* %X ends the close label, go back to the previously
4107 * define tab label nr. */
4108 for (n = curitem - 1; n >= 0; --n)
4109 if (item[n].type == TabPage && item[n].minwid >= 0)
4110 {
4111 minwid = item[n].minwid;
4112 break;
4113 }
4114 }
4115 else
4116 /* close nrs are stored as negative values */
4117 minwid = - minwid;
4118 }
4119 item[curitem].type = TabPage;
4120 item[curitem].start = p;
4121 item[curitem].minwid = minwid;
4122 s++;
4123 curitem++;
4124 continue;
4125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (*s == '.')
4127 {
4128 s++;
4129 if (VIM_ISDIGIT(*s))
4130 {
4131 maxwid = (int)getdigits(&s);
4132 if (maxwid <= 0) /* overflow */
4133 maxwid = 50;
4134 }
4135 }
4136 minwid = (minwid > 50 ? 50 : minwid) * l;
4137 if (*s == '(')
4138 {
4139 groupitem[groupdepth++] = curitem;
4140 item[curitem].type = Group;
4141 item[curitem].start = p;
4142 item[curitem].minwid = minwid;
4143 item[curitem].maxwid = maxwid;
4144 s++;
4145 curitem++;
4146 continue;
4147 }
4148 if (vim_strchr(STL_ALL, *s) == NULL)
4149 {
4150 s++;
4151 continue;
4152 }
4153 opt = *s++;
4154
4155 /* OK - now for the real work */
4156 base = 'D';
4157 itemisflag = FALSE;
4158 fillable = TRUE;
4159 num = -1;
4160 str = NULL;
4161 switch (opt)
4162 {
4163 case STL_FILEPATH:
4164 case STL_FULLPATH:
4165 case STL_FILENAME:
4166 fillable = FALSE; /* don't change ' ' to fillchar */
4167 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004168 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 else
4170 {
4171 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004172 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4174 }
4175 trans_characters(NameBuff, MAXPATHL);
4176 if (opt != STL_FILENAME)
4177 str = NameBuff;
4178 else
4179 str = gettail(NameBuff);
4180 break;
4181
4182 case STL_VIM_EXPR: /* '{' */
4183 itemisflag = TRUE;
4184 t = p;
4185 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4186 *p++ = *s++;
4187 if (*s != '}') /* missing '}' or out of space */
4188 break;
4189 s++;
4190 *p = 0;
4191 p = t;
4192
4193#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004194 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4196
4197 o_curbuf = curbuf;
4198 o_curwin = curwin;
4199 curwin = wp;
4200 curbuf = wp->w_buffer;
4201
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004202 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204 curwin = o_curwin;
4205 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004206 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208 if (str != NULL && *str != 0)
4209 {
4210 if (*skipdigits(str) == NUL)
4211 {
4212 num = atoi((char *)str);
4213 vim_free(str);
4214 str = NULL;
4215 itemisflag = FALSE;
4216 }
4217 }
4218#endif
4219 break;
4220
4221 case STL_LINE:
4222 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4223 ? 0L : (long)(wp->w_cursor.lnum);
4224 break;
4225
4226 case STL_NUMLINES:
4227 num = wp->w_buffer->b_ml.ml_line_count;
4228 break;
4229
4230 case STL_COLUMN:
4231 num = !(State & INSERT) && empty_line
4232 ? 0 : (int)wp->w_cursor.col + 1;
4233 break;
4234
4235 case STL_VIRTCOL:
4236 case STL_VIRTCOL_ALT:
4237 /* In list mode virtcol needs to be recomputed */
4238 virtcol = wp->w_virtcol;
4239 if (wp->w_p_list && lcs_tab1 == NUL)
4240 {
4241 wp->w_p_list = FALSE;
4242 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4243 wp->w_p_list = TRUE;
4244 }
4245 ++virtcol;
4246 /* Don't display %V if it's the same as %c. */
4247 if (opt == STL_VIRTCOL_ALT
4248 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4249 ? 0 : (int)wp->w_cursor.col + 1)))
4250 break;
4251 num = (long)virtcol;
4252 break;
4253
4254 case STL_PERCENTAGE:
4255 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4256 (long)wp->w_buffer->b_ml.ml_line_count);
4257 break;
4258
4259 case STL_ALTPERCENT:
4260 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004261 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 break;
4263
4264 case STL_ARGLISTSTAT:
4265 fillable = FALSE;
4266 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004267 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 str = tmp;
4269 break;
4270
4271 case STL_KEYMAP:
4272 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004273 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 str = tmp;
4275 break;
4276 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004277#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004278 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279#else
4280 num = 0;
4281#endif
4282 break;
4283
4284 case STL_BUFNO:
4285 num = wp->w_buffer->b_fnum;
4286 break;
4287
4288 case STL_OFFSET_X:
4289 base = 'X';
4290 case STL_OFFSET:
4291#ifdef FEAT_BYTEOFF
4292 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4293 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4294 0L : l + 1 + (!(State & INSERT) && empty_line ?
4295 0 : (int)wp->w_cursor.col);
4296#endif
4297 break;
4298
4299 case STL_BYTEVAL_X:
4300 base = 'X';
4301 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004302 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (num == NL)
4304 num = 0;
4305 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4306 num = NL;
4307 break;
4308
4309 case STL_ROFLAG:
4310 case STL_ROFLAG_ALT:
4311 itemisflag = TRUE;
4312 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004313 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 break;
4315
4316 case STL_HELPFLAG:
4317 case STL_HELPFLAG_ALT:
4318 itemisflag = TRUE;
4319 if (wp->w_buffer->b_help)
4320 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004321 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 break;
4323
4324#ifdef FEAT_AUTOCMD
4325 case STL_FILETYPE:
4326 if (*wp->w_buffer->b_p_ft != NUL
4327 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4328 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004329 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4330 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 str = tmp;
4332 }
4333 break;
4334
4335 case STL_FILETYPE_ALT:
4336 itemisflag = TRUE;
4337 if (*wp->w_buffer->b_p_ft != NUL
4338 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4339 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004340 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4341 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 for (t = tmp; *t != 0; t++)
4343 *t = TOUPPER_LOC(*t);
4344 str = tmp;
4345 }
4346 break;
4347#endif
4348
4349#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4350 case STL_PREVIEWFLAG:
4351 case STL_PREVIEWFLAG_ALT:
4352 itemisflag = TRUE;
4353 if (wp->w_p_pvw)
4354 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4355 : _("[Preview]"));
4356 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004357
4358 case STL_QUICKFIX:
4359 if (bt_quickfix(wp->w_buffer))
4360 str = (char_u *)(wp->w_llist_ref
4361 ? _(msg_loclist)
4362 : _(msg_qflist));
4363 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364#endif
4365
4366 case STL_MODIFIED:
4367 case STL_MODIFIED_ALT:
4368 itemisflag = TRUE;
4369 switch ((opt == STL_MODIFIED_ALT)
4370 + bufIsChanged(wp->w_buffer) * 2
4371 + (!wp->w_buffer->b_p_ma) * 4)
4372 {
4373 case 2: str = (char_u *)"[+]"; break;
4374 case 3: str = (char_u *)",+"; break;
4375 case 4: str = (char_u *)"[-]"; break;
4376 case 5: str = (char_u *)",-"; break;
4377 case 6: str = (char_u *)"[+-]"; break;
4378 case 7: str = (char_u *)",+-"; break;
4379 }
4380 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004381
4382 case STL_HIGHLIGHT:
4383 t = s;
4384 while (*s != '#' && *s != NUL)
4385 ++s;
4386 if (*s == '#')
4387 {
4388 item[curitem].type = Highlight;
4389 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004390 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004391 curitem++;
4392 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004393 if (*s != NUL)
4394 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004395 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397
4398 item[curitem].start = p;
4399 item[curitem].type = Normal;
4400 if (str != NULL && *str)
4401 {
4402 t = str;
4403 if (itemisflag)
4404 {
4405 if ((t[0] && t[1])
4406 && ((!prevchar_isitem && *t == ',')
4407 || (prevchar_isflag && *t == ' ')))
4408 t++;
4409 prevchar_isflag = TRUE;
4410 }
4411 l = vim_strsize(t);
4412 if (l > 0)
4413 prevchar_isitem = TRUE;
4414 if (l > maxwid)
4415 {
4416 while (l >= maxwid)
4417#ifdef FEAT_MBYTE
4418 if (has_mbyte)
4419 {
4420 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004421 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
4423 else
4424#endif
4425 l -= byte2cells(*t++);
4426 if (p + 1 >= out + outlen)
4427 break;
4428 *p++ = '<';
4429 }
4430 if (minwid > 0)
4431 {
4432 for (; l < minwid && p + 1 < out + outlen; l++)
4433 {
4434 /* Don't put a "-" in front of a digit. */
4435 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4436 *p++ = ' ';
4437 else
4438 *p++ = fillchar;
4439 }
4440 minwid = 0;
4441 }
4442 else
4443 minwid *= -1;
4444 while (*t && p + 1 < out + outlen)
4445 {
4446 *p++ = *t++;
4447 /* Change a space by fillchar, unless fillchar is '-' and a
4448 * digit follows. */
4449 if (fillable && p[-1] == ' '
4450 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4451 p[-1] = fillchar;
4452 }
4453 for (; l < minwid && p + 1 < out + outlen; l++)
4454 *p++ = fillchar;
4455 }
4456 else if (num >= 0)
4457 {
4458 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4459 char_u nstr[20];
4460
4461 if (p + 20 >= out + outlen)
4462 break; /* not sufficient space */
4463 prevchar_isitem = TRUE;
4464 t = nstr;
4465 if (opt == STL_VIRTCOL_ALT)
4466 {
4467 *t++ = '-';
4468 minwid--;
4469 }
4470 *t++ = '%';
4471 if (zeropad)
4472 *t++ = '0';
4473 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004474 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 *t = 0;
4476
4477 for (n = num, l = 1; n >= nbase; n /= nbase)
4478 l++;
4479 if (opt == STL_VIRTCOL_ALT)
4480 l++;
4481 if (l > maxwid)
4482 {
4483 l += 2;
4484 n = l - maxwid;
4485 while (l-- > maxwid)
4486 num /= nbase;
4487 *t++ = '>';
4488 *t++ = '%';
4489 *t = t[-3];
4490 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004491 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4492 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004495 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4496 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 p += STRLEN(p);
4498 }
4499 else
4500 item[curitem].type = Empty;
4501
4502 if (opt == STL_VIM_EXPR)
4503 vim_free(str);
4504
4505 if (num >= 0 || (!itemisflag && str && *str))
4506 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4507 curitem++;
4508 }
4509 *p = NUL;
4510 itemcnt = curitem;
4511
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004512#ifdef FEAT_EVAL
4513 if (usefmt != fmt)
4514 vim_free(usefmt);
4515#endif
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 width = vim_strsize(out);
4518 if (maxwidth > 0 && width > maxwidth)
4519 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004520 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 l = 0;
4522 if (itemcnt == 0)
4523 s = out;
4524 else
4525 {
4526 for ( ; l < itemcnt; l++)
4527 if (item[l].type == Trunc)
4528 {
4529 /* Truncate at %< item. */
4530 s = item[l].start;
4531 break;
4532 }
4533 if (l == itemcnt)
4534 {
4535 /* No %< item, truncate first item. */
4536 s = item[0].start;
4537 l = 0;
4538 }
4539 }
4540
4541 if (width - vim_strsize(s) >= maxwidth)
4542 {
4543 /* Truncation mark is beyond max length */
4544#ifdef FEAT_MBYTE
4545 if (has_mbyte)
4546 {
4547 s = out;
4548 width = 0;
4549 for (;;)
4550 {
4551 width += ptr2cells(s);
4552 if (width >= maxwidth)
4553 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004554 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556 /* Fill up for half a double-wide character. */
4557 while (++width < maxwidth)
4558 *s++ = fillchar;
4559 }
4560 else
4561#endif
4562 s = out + maxwidth - 1;
4563 for (l = 0; l < itemcnt; l++)
4564 if (item[l].start > s)
4565 break;
4566 itemcnt = l;
4567 *s++ = '>';
4568 *s = 0;
4569 }
4570 else
4571 {
4572#ifdef FEAT_MBYTE
4573 if (has_mbyte)
4574 {
4575 n = 0;
4576 while (width >= maxwidth)
4577 {
4578 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004579 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 }
4582 else
4583#endif
4584 n = width - maxwidth + 1;
4585 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004586 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 *s = '<';
4588
4589 /* Fill up for half a double-wide character. */
4590 while (++width < maxwidth)
4591 {
4592 s = s + STRLEN(s);
4593 *s++ = fillchar;
4594 *s = NUL;
4595 }
4596
4597 --n; /* count the '<' */
4598 for (; l < itemcnt; l++)
4599 {
4600 if (item[l].start - n >= s)
4601 item[l].start -= n;
4602 else
4603 item[l].start = s;
4604 }
4605 }
4606 width = maxwidth;
4607 }
4608 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4609 {
4610 /* Apply STL_MIDDLE if any */
4611 for (l = 0; l < itemcnt; l++)
4612 if (item[l].type == Middle)
4613 break;
4614 if (l < itemcnt)
4615 {
4616 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004617 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 for (s = item[l].start; s < p; s++)
4619 *s = fillchar;
4620 for (l++; l < itemcnt; l++)
4621 item[l].start += maxwidth - width;
4622 width = maxwidth;
4623 }
4624 }
4625
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004626 /* Store the info about highlighting. */
4627 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004629 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 for (l = 0; l < itemcnt; l++)
4631 {
4632 if (item[l].type == Highlight)
4633 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004634 sp->start = item[l].start;
4635 sp->userhl = item[l].minwid;
4636 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004639 sp->start = NULL;
4640 sp->userhl = 0;
4641 }
4642
4643 /* Store the info about tab pages labels. */
4644 if (tabtab != NULL)
4645 {
4646 sp = tabtab;
4647 for (l = 0; l < itemcnt; l++)
4648 {
4649 if (item[l].type == TabPage)
4650 {
4651 sp->start = item[l].start;
4652 sp->userhl = item[l].minwid;
4653 sp++;
4654 }
4655 }
4656 sp->start = NULL;
4657 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
4659
4660 return width;
4661}
4662#endif /* FEAT_STL_OPT */
4663
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004664#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4665 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004667 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4668 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 */
4670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004671get_rel_pos(
4672 win_T *wp,
4673 char_u *buf,
4674 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
4676 long above; /* number of lines above window */
4677 long below; /* number of lines below window */
4678
Bram Moolenaar0027c212015-01-07 13:31:52 +01004679 if (buflen < 3) /* need at least 3 chars for writing */
4680 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 above = wp->w_topline - 1;
4682#ifdef FEAT_DIFF
4683 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004684 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4685 above = 0; /* All buffer lines are displayed and there is an
4686 * indication of filler lines, that can be considered
4687 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688#endif
4689 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4690 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004691 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4692 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004694 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004696 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 ? (int)(above / ((above + below) / 100L))
4698 : (int)(above * 100L / (above + below)));
4699}
4700#endif
4701
4702/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004703 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 * Return TRUE if it was appended.
4705 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004706 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004707append_arg_number(
4708 win_T *wp,
4709 char_u *buf,
4710 int buflen,
4711 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
4713 char_u *p;
4714
4715 if (ARGCOUNT <= 1) /* nothing to do */
4716 return FALSE;
4717
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004718 p = buf + STRLEN(buf); /* go to the end of the buffer */
4719 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 return FALSE;
4721 *p++ = ' ';
4722 *p++ = '(';
4723 if (add_file)
4724 {
4725 STRCPY(p, "file ");
4726 p += 5;
4727 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004728 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4729 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4731 return TRUE;
4732}
4733
4734/*
4735 * If fname is not a full path, make it a full path.
4736 * Returns pointer to allocated memory (NULL for failure).
4737 */
4738 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004739fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 /*
4742 * Force expanding the path always for Unix, because symbolic links may
4743 * mess up the full path name, even though it starts with a '/'.
4744 * Also expand when there is ".." in the file name, try to remove it,
4745 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004746 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 * For MS-Windows also expand names like "longna~1" to "longname".
4748 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004749#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 return FullName_save(fname, TRUE);
4751#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004752 if (!vim_isAbsName(fname)
4753 || strstr((char *)fname, "..") != NULL
4754 || strstr((char *)fname, "//") != NULL
4755# ifdef BACKSLASH_IN_FILENAME
4756 || strstr((char *)fname, "\\\\") != NULL
4757# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004758# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004760# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 )
4762 return FullName_save(fname, FALSE);
4763
4764 fname = vim_strsave(fname);
4765
Bram Moolenaar9b942202007-10-03 12:31:33 +00004766# ifdef USE_FNAME_CASE
4767# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 if (fname != NULL)
4772 fname_case(fname, 0); /* set correct case for file name */
4773 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
4776 return fname;
4777#endif
4778}
4779
4780/*
4781 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4782 * "ffname" becomes a pointer to allocated memory (or NULL).
4783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004785fname_expand(
4786 buf_T *buf UNUSED,
4787 char_u **ffname,
4788 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 if (*ffname == NULL) /* if no file name given, nothing to do */
4791 return;
4792 if (*sfname == NULL) /* if no short file name given, use ffname */
4793 *sfname = *ffname;
4794 *ffname = fix_fname(*ffname); /* expand to full path */
4795
4796#ifdef FEAT_SHORTCUT
4797 if (!buf->b_p_bin)
4798 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004799 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
4801 /* If the file name is a shortcut file, use the file it links to. */
4802 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004803 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
4805 vim_free(*ffname);
4806 *ffname = rfname;
4807 *sfname = rfname;
4808 }
4809 }
4810#endif
4811}
4812
4813/*
4814 * Get the file name for an argument list entry.
4815 */
4816 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004817alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
4819 buf_T *bp;
4820
4821 /* Use the name from the associated buffer if it exists. */
4822 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004823 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return aep->ae_fname;
4825 return bp->b_fname;
4826}
4827
4828#if defined(FEAT_WINDOWS) || defined(PROTO)
4829/*
4830 * do_arg_all(): Open up to 'count' windows, one for each argument.
4831 */
4832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004833do_arg_all(
4834 int count,
4835 int forceit, /* hide buffers in current windows */
4836 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
4838 int i;
4839 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004840 char_u *opened; /* Array of weight for which args are open:
4841 * 0: not opened
4842 * 1: opened in other tab
4843 * 2: opened in curtab
4844 * 3: opened in curtab and curwin
4845 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004846 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 int use_firstwin = FALSE; /* use first window for arglist */
4848 int split_ret = OK;
4849 int p_ea_save;
4850 alist_T *alist; /* argument list to be used */
4851 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004852 tabpage_T *tpnext;
4853 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004854 win_T *old_curwin, *last_curwin;
4855 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004856 win_T *new_curwin = NULL;
4857 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 if (ARGCOUNT <= 0)
4860 {
4861 /* Don't give an error message. We don't want it when the ":all"
4862 * command is in the .vimrc. */
4863 return;
4864 }
4865 setpcmark();
4866
4867 opened_len = ARGCOUNT;
4868 opened = alloc_clear((unsigned)opened_len);
4869 if (opened == NULL)
4870 return;
4871
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004872 /* Autocommands may do anything to the argument list. Make sure it's not
4873 * freed while we are working here by "locking" it. We still have to
4874 * watch out for its size to be changed. */
4875 alist = curwin->w_alist;
4876 ++alist->al_refcount;
4877
4878 old_curwin = curwin;
4879 old_curtab = curtab;
4880
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004881# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885 /*
4886 * Try closing all windows that are not in the argument list.
4887 * Also close windows that are not full width;
4888 * When 'hidden' or "forceit" set the buffer becomes hidden.
4889 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004890 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004892 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004893 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004894 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004896 tpnext = curtab->tp_next;
4897 for (wp = firstwin; wp != NULL; wp = wpnext)
4898 {
4899 wpnext = wp->w_next;
4900 buf = wp->w_buffer;
4901 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004902 || (!keep_tabs && (buf->b_nwindows > 1
4903 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004904 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004905 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004907 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004908 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004910 if (i < alist->al_ga.ga_len
4911 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4912 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4913 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004915 int weight = 1;
4916
4917 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004918 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004919 ++weight;
4920 if (old_curwin == wp)
4921 ++weight;
4922 }
4923
4924 if (weight > (int)opened[i])
4925 {
4926 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004927 if (i == 0)
4928 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004929 if (new_curwin != NULL)
4930 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004931 new_curwin = wp;
4932 new_curtab = curtab;
4933 }
4934 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004935 else if (keep_tabs)
4936 i = opened_len;
4937
4938 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004939 {
4940 /* Use the current argument list for all windows
4941 * containing a file from it. */
4942 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004943 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004944 ++wp->w_alist->al_refcount;
4945 }
4946 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004950 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004952 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004954 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004955 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004957 /* If the buffer was changed, and we would like to hide it,
4958 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004959 if (!P_HID(buf) && buf->b_nwindows <= 1
4960 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004962#ifdef FEAT_AUTOCMD
4963 bufref_T bufref;
4964
4965 set_bufref(&bufref, buf);
4966#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004967 (void)autowrite(buf, FALSE);
4968#ifdef FEAT_AUTOCMD
4969 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004970 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004971 {
4972 wpnext = firstwin; /* start all over... */
4973 continue;
4974 }
4975#endif
4976 }
4977#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004978 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004979 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004980 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004981#endif
4982 use_firstwin = TRUE;
4983#ifdef FEAT_WINDOWS
4984 else
4985 {
4986 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4987# ifdef FEAT_AUTOCMD
4988 /* check if autocommands removed the next window */
4989 if (!win_valid(wpnext))
4990 wpnext = firstwin; /* start all over... */
4991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993#endif
4994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004997
4998 /* Without the ":tab" modifier only do the current tab page. */
4999 if (had_tab == 0 || tpnext == NULL)
5000 break;
5001
5002# ifdef FEAT_AUTOCMD
5003 /* check if autocommands removed the next tab page */
5004 if (!valid_tabpage(tpnext))
5005 tpnext = first_tabpage; /* start all over...*/
5006# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005007 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009
5010 /*
5011 * Open a window for files in the argument list that don't have one.
5012 * ARGCOUNT may change while doing this, because of autocommands.
5013 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005014 if (count > opened_len || count <= 0)
5015 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
5017#ifdef FEAT_AUTOCMD
5018 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5019 ++autocmd_no_enter;
5020 ++autocmd_no_leave;
5021#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005022 last_curwin = curwin;
5023 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005025#ifdef FEAT_WINDOWS
5026 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5027 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005028 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005029 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5030 use_firstwin = TRUE;
5031#endif
5032
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005033 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5036 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005037 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
5039 /* Move the already present window to below the current window */
5040 if (curwin->w_arg_idx != i)
5041 {
5042 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5043 {
5044 if (wpnext->w_arg_idx == i)
5045 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005046 if (keep_tabs)
5047 {
5048 new_curwin = wpnext;
5049 new_curtab = curtab;
5050 }
5051 else
5052 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054 }
5055 }
5056 }
5057 }
5058 else if (split_ret == OK)
5059 {
5060 if (!use_firstwin) /* split current window */
5061 {
5062 p_ea_save = p_ea;
5063 p_ea = TRUE; /* use space from all windows */
5064 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5065 p_ea = p_ea_save;
5066 if (split_ret == FAIL)
5067 continue;
5068 }
5069#ifdef FEAT_AUTOCMD
5070 else /* first window: do autocmd for leaving this buffer */
5071 --autocmd_no_leave;
5072#endif
5073
5074 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005075 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 */
5077 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005078 if (i == 0)
5079 {
5080 new_curwin = curwin;
5081 new_curtab = curtab;
5082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5084 ECMD_ONE,
5085 ((P_HID(curwin->w_buffer)
5086 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005087 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088#ifdef FEAT_AUTOCMD
5089 if (use_firstwin)
5090 ++autocmd_no_leave;
5091#endif
5092 use_firstwin = FALSE;
5093 }
5094 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005095
5096 /* When ":tab" was used open a new tab for a new window repeatedly. */
5097 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5098 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100
5101 /* Remove the "lock" on the argument list. */
5102 alist_unlink(alist);
5103
5104#ifdef FEAT_AUTOCMD
5105 --autocmd_no_enter;
5106#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005107 /* restore last referenced tabpage's curwin */
5108 if (last_curtab != new_curtab)
5109 {
5110 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005111 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005112 if (win_valid(last_curwin))
5113 win_enter(last_curwin, FALSE);
5114 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005115 /* to window with first arg */
5116 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005117 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005118 if (win_valid(new_curwin))
5119 win_enter(new_curwin, FALSE);
5120
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121#ifdef FEAT_AUTOCMD
5122 --autocmd_no_leave;
5123#endif
5124 vim_free(opened);
5125}
5126
5127# if defined(FEAT_LISTCMDS) || defined(PROTO)
5128/*
5129 * Open a window for a number of buffers.
5130 */
5131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005132ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
5134 buf_T *buf;
5135 win_T *wp, *wpnext;
5136 int split_ret = OK;
5137 int p_ea_save;
5138 int open_wins = 0;
5139 int r;
5140 int count; /* Maximum number of windows to open. */
5141 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005142#ifdef FEAT_WINDOWS
5143 int had_tab = cmdmod.tab;
5144 tabpage_T *tpnext;
5145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
5147 if (eap->addr_count == 0) /* make as many windows as possible */
5148 count = 9999;
5149 else
5150 count = eap->line2; /* make as many windows as specified */
5151 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5152 all = FALSE;
5153 else
5154 all = TRUE;
5155
5156 setpcmark();
5157
5158#ifdef FEAT_GUI
5159 need_mouse_correct = TRUE;
5160#endif
5161
5162 /*
5163 * Close superfluous windows (two windows for the same buffer).
5164 * Also close windows that are not full-width.
5165 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005166#ifdef FEAT_WINDOWS
5167 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005168 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005169 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005172 tpnext = curtab->tp_next;
5173 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005175 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005176 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005177#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005178 || ((cmdmod.split & WSP_VERT)
5179 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005180 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005181 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005182 || (had_tab > 0 && wp != firstwin)
5183#endif
Bram Moolenaar459ca562016-11-10 18:16:33 +01005184 ) && !ONE_WINDOW
Bram Moolenaar362ce482012-06-06 19:02:45 +02005185#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005186 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005187#endif
5188 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005189 {
5190 win_close(wp, FALSE);
5191#ifdef FEAT_AUTOCMD
5192 wpnext = firstwin; /* just in case an autocommand does
5193 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005194 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005195 open_wins = 0;
5196#endif
5197 }
5198 else
5199 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005201
5202#ifdef FEAT_WINDOWS
5203 /* Without the ":tab" modifier only do the current tab page. */
5204 if (had_tab == 0 || tpnext == NULL)
5205 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005206 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
5210 /*
5211 * Go through the buffer list. When a buffer doesn't have a window yet,
5212 * open one. Otherwise move the window to the right position.
5213 * Watch out for autocommands that delete buffers or windows!
5214 */
5215#ifdef FEAT_AUTOCMD
5216 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5217 ++autocmd_no_enter;
5218#endif
5219 win_enter(lastwin, FALSE);
5220#ifdef FEAT_AUTOCMD
5221 ++autocmd_no_leave;
5222#endif
5223 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5224 {
5225 /* Check if this buffer needs a window */
5226 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5227 continue;
5228
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005229#ifdef FEAT_WINDOWS
5230 if (had_tab != 0)
5231 {
5232 /* With the ":tab" modifier don't move the window. */
5233 if (buf->b_nwindows > 0)
5234 wp = lastwin; /* buffer has a window, skip it */
5235 else
5236 wp = NULL;
5237 }
5238 else
5239#endif
5240 {
5241 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005242 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005243 if (wp->w_buffer == buf)
5244 break;
5245 /* If the buffer already has a window, move it */
5246 if (wp != NULL)
5247 win_move_after(wp, curwin);
5248 }
5249
5250 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005252#ifdef FEAT_AUTOCMD
5253 bufref_T bufref;
5254
5255 set_bufref(&bufref, buf);
5256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 /* Split the window and put the buffer in it */
5258 p_ea_save = p_ea;
5259 p_ea = TRUE; /* use space from all windows */
5260 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5261 ++open_wins;
5262 p_ea = p_ea_save;
5263 if (split_ret == FAIL)
5264 continue;
5265
5266 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005267#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 swap_exists_action = SEA_DIALOG;
5269#endif
5270 set_curbuf(buf, DOBUF_GOTO);
5271#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005272 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005274 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005275#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 break;
5279 }
5280#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005281#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (swap_exists_action == SEA_QUIT)
5283 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005284# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5285 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005286
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005287 /* Reset the error/interrupt/exception state here so that
5288 * aborting() returns FALSE when closing a window. */
5289 enter_cleanup(&cs);
5290# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005291
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005292 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 win_close(curwin, TRUE);
5294 --open_wins;
5295 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005296 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005297
5298# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5299 /* Restore the error/interrupt/exception state if not
5300 * discarded by a new aborting error, interrupt, or uncaught
5301 * exception. */
5302 leave_cleanup(&cs);
5303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 else
5306 handle_swap_exists(NULL);
5307#endif
5308 }
5309
5310 ui_breakcheck();
5311 if (got_int)
5312 {
5313 (void)vgetc(); /* only break the file loading, not the rest */
5314 break;
5315 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005316#ifdef FEAT_EVAL
5317 /* Autocommands deleted the buffer or aborted script processing!!! */
5318 if (aborting())
5319 break;
5320#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005321#ifdef FEAT_WINDOWS
5322 /* When ":tab" was used open a new tab for a new window repeatedly. */
5323 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5324 cmdmod.tab = 9999;
5325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327#ifdef FEAT_AUTOCMD
5328 --autocmd_no_enter;
5329#endif
5330 win_enter(firstwin, FALSE); /* back to first window */
5331#ifdef FEAT_AUTOCMD
5332 --autocmd_no_leave;
5333#endif
5334
5335 /*
5336 * Close superfluous windows.
5337 */
5338 for (wp = lastwin; open_wins > count; )
5339 {
5340 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5341 || autowrite(wp->w_buffer, FALSE) == OK);
5342#ifdef FEAT_AUTOCMD
5343 if (!win_valid(wp))
5344 {
5345 /* BufWrite Autocommands made the window invalid, start over */
5346 wp = lastwin;
5347 }
5348 else
5349#endif
5350 if (r)
5351 {
5352 win_close(wp, !P_HID(wp->w_buffer));
5353 --open_wins;
5354 wp = lastwin;
5355 }
5356 else
5357 {
5358 wp = wp->w_prev;
5359 if (wp == NULL)
5360 break;
5361 }
5362 }
5363}
5364# endif /* FEAT_LISTCMDS */
5365
5366#endif /* FEAT_WINDOWS */
5367
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005368static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370/*
5371 * do_modelines() - process mode lines for the current file
5372 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005373 * "flags" can be:
5374 * OPT_WINONLY only set options local to window
5375 * OPT_NOWIN don't set options local to window
5376 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 * Returns immediately if the "ml" option isn't set.
5378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005380do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005382 linenr_T lnum;
5383 int nmlines;
5384 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
5386 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5387 return;
5388
5389 /* Disallow recursive entry here. Can happen when executing a modeline
5390 * triggers an autocommand, which reloads modelines with a ":do". */
5391 if (entered)
5392 return;
5393
5394 ++entered;
5395 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5396 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005397 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 nmlines = 0;
5399
5400 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5401 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005402 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 nmlines = 0;
5404 --entered;
5405}
5406
5407#include "version.h" /* for version number */
5408
5409/*
5410 * chk_modeline() - check a single line for a mode string
5411 * Return FAIL if an error encountered.
5412 */
5413 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005414chk_modeline(
5415 linenr_T lnum,
5416 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
5418 char_u *s;
5419 char_u *e;
5420 char_u *linecopy; /* local copy of any modeline found */
5421 int prev;
5422 int vers;
5423 int end;
5424 int retval = OK;
5425 char_u *save_sourcing_name;
5426 linenr_T save_sourcing_lnum;
5427#ifdef FEAT_EVAL
5428 scid_T save_SID;
5429#endif
5430
5431 prev = -1;
5432 for (s = ml_get(lnum); *s != NUL; ++s)
5433 {
5434 if (prev == -1 || vim_isspace(prev))
5435 {
5436 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5437 || STRNCMP(s, "vi:", (size_t)3) == 0)
5438 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005439 /* Accept both "vim" and "Vim". */
5440 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
5442 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5443 e = s + 4;
5444 else
5445 e = s + 3;
5446 vers = getdigits(&e);
5447 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005448 && (s[0] != 'V'
5449 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 && (s[3] == ':'
5451 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5452 || (VIM_VERSION_100 < vers && s[3] == '<')
5453 || (VIM_VERSION_100 > vers && s[3] == '>')
5454 || (VIM_VERSION_100 == vers && s[3] == '=')))
5455 break;
5456 }
5457 }
5458 prev = *s;
5459 }
5460
5461 if (*s)
5462 {
5463 do /* skip over "ex:", "vi:" or "vim:" */
5464 ++s;
5465 while (s[-1] != ':');
5466
5467 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5468 if (linecopy == NULL)
5469 return FAIL;
5470
5471 save_sourcing_lnum = sourcing_lnum;
5472 save_sourcing_name = sourcing_name;
5473 sourcing_lnum = lnum; /* prepare for emsg() */
5474 sourcing_name = (char_u *)"modelines";
5475
5476 end = FALSE;
5477 while (end == FALSE)
5478 {
5479 s = skipwhite(s);
5480 if (*s == NUL)
5481 break;
5482
5483 /*
5484 * Find end of set command: ':' or end of line.
5485 * Skip over "\:", replacing it with ":".
5486 */
5487 for (e = s; *e != ':' && *e != NUL; ++e)
5488 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005489 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 if (*e == NUL)
5491 end = TRUE;
5492
5493 /*
5494 * If there is a "set" command, require a terminating ':' and
5495 * ignore the stuff after the ':'.
5496 * "vi:set opt opt opt: foo" -- foo not interpreted
5497 * "vi:opt opt opt: foo" -- foo interpreted
5498 * Accept "se" for compatibility with Elvis.
5499 */
5500 if (STRNCMP(s, "set ", (size_t)4) == 0
5501 || STRNCMP(s, "se ", (size_t)3) == 0)
5502 {
5503 if (*e != ':') /* no terminating ':'? */
5504 break;
5505 end = TRUE;
5506 s = vim_strchr(s, ' ') + 1;
5507 }
5508 *e = NUL; /* truncate the set command */
5509
5510 if (*s != NUL) /* skip over an empty "::" */
5511 {
5512#ifdef FEAT_EVAL
5513 save_SID = current_SID;
5514 current_SID = SID_MODELINE;
5515#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005516 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517#ifdef FEAT_EVAL
5518 current_SID = save_SID;
5519#endif
5520 if (retval == FAIL) /* stop if error found */
5521 break;
5522 }
5523 s = e + 1; /* advance to next part */
5524 }
5525
5526 sourcing_lnum = save_sourcing_lnum;
5527 sourcing_name = save_sourcing_name;
5528
5529 vim_free(linecopy);
5530 }
5531 return retval;
5532}
5533
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005534#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005536read_viminfo_bufferlist(
5537 vir_T *virp,
5538 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 char_u *tab;
5541 linenr_T lnum;
5542 colnr_T col;
5543 buf_T *buf;
5544 char_u *sfname;
5545 char_u *xline;
5546
5547 /* Handle long line and escaped characters. */
5548 xline = viminfo_readstring(virp, 1, FALSE);
5549
5550 /* don't read in if there are files on the command-line or if writing: */
5551 if (xline != NULL && !writing && ARGCOUNT == 0
5552 && find_viminfo_parameter('%') != NULL)
5553 {
5554 /* Format is: <fname> Tab <lnum> Tab <col>.
5555 * Watch out for a Tab in the file name, work from the end. */
5556 lnum = 0;
5557 col = 0;
5558 tab = vim_strrchr(xline, '\t');
5559 if (tab != NULL)
5560 {
5561 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005562 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 tab = vim_strrchr(xline, '\t');
5564 if (tab != NULL)
5565 {
5566 *tab++ = '\0';
5567 lnum = atol((char *)tab);
5568 }
5569 }
5570
5571 /* Expand "~/" in the file name at "line + 1" to a full path.
5572 * Then try shortening it by comparing with the current directory */
5573 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005574 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
5576 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5577 if (buf != NULL) /* just in case... */
5578 {
5579 buf->b_last_cursor.lnum = lnum;
5580 buf->b_last_cursor.col = col;
5581 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5582 }
5583 }
5584 vim_free(xline);
5585
5586 return viminfo_readline(virp);
5587}
5588
5589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005590write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 buf_T *buf;
5593#ifdef FEAT_WINDOWS
5594 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005595 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596#endif
5597 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005598 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
5600 if (find_viminfo_parameter('%') == NULL)
5601 return;
5602
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005603 /* Without a number -1 is returned: do all buffers. */
5604 max_buffers = get_viminfo_parameter('%');
5605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005607#define LINE_BUF_LEN (MAXPATHL + 40)
5608 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if (line == NULL)
5610 return;
5611
5612#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005613 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 set_last_cursor(win);
5615#else
5616 set_last_cursor(curwin);
5617#endif
5618
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005619 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005620 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 if (buf->b_fname == NULL
5623 || !buf->b_p_bl
5624#ifdef FEAT_QUICKFIX
5625 || bt_quickfix(buf)
5626#endif
5627 || removable(buf->b_ffname))
5628 continue;
5629
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005630 if (max_buffers-- == 0)
5631 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 putc('%', fp);
5633 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005634 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 (long)buf->b_last_cursor.lnum,
5636 buf->b_last_cursor.col);
5637 viminfo_writestring(fp, line);
5638 }
5639 vim_free(line);
5640}
5641#endif
5642
5643
5644/*
5645 * Return special buffer name.
5646 * Returns NULL when the buffer has a normal file name.
5647 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005648 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005649buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
5651#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5652 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005653 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005654 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005655 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005656
5657 /*
5658 * For location list window, w_llist_ref points to the location list.
5659 * For quickfix window, w_llist_ref is NULL.
5660 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005661 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005662 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005663 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005664 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666#endif
5667#ifdef FEAT_QUICKFIX
5668 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5669 * contains the name as specified by the user */
5670 if (bt_nofile(buf))
5671 {
5672 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005673 return buf->b_sfname;
5674 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676#endif
5677 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005678 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 return NULL;
5680}
5681
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005682#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5683 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5684 || defined(PROTO)
5685/*
5686 * Find a window for buffer "buf".
5687 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5688 * If not found FAIL is returned.
5689 */
5690 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005691find_win_for_buf(
5692 buf_T *buf,
5693 win_T **wp,
5694 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005695{
5696 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5697 if ((*wp)->w_buffer == buf)
5698 goto win_found;
5699 return FAIL;
5700win_found:
5701 return OK;
5702}
5703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
5705#if defined(FEAT_SIGNS) || defined(PROTO)
5706/*
5707 * Insert the sign into the signlist.
5708 */
5709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005710insert_sign(
5711 buf_T *buf, /* buffer to store sign in */
5712 signlist_T *prev, /* previous sign entry */
5713 signlist_T *next, /* next sign entry */
5714 int id, /* sign ID */
5715 linenr_T lnum, /* line number which gets the mark */
5716 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 signlist_T *newsign;
5719
5720 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5721 if (newsign != NULL)
5722 {
5723 newsign->id = id;
5724 newsign->lnum = lnum;
5725 newsign->typenr = typenr;
5726 newsign->next = next;
5727#ifdef FEAT_NETBEANS_INTG
5728 newsign->prev = prev;
5729 if (next != NULL)
5730 next->prev = newsign;
5731#endif
5732
5733 if (prev == NULL)
5734 {
5735 /* When adding first sign need to redraw the windows to create the
5736 * column for signs. */
5737 if (buf->b_signlist == NULL)
5738 {
5739 redraw_buf_later(buf, NOT_VALID);
5740 changed_cline_bef_curs();
5741 }
5742
5743 /* first sign in signlist */
5744 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005745#ifdef FEAT_NETBEANS_INTG
5746 if (netbeans_active())
5747 buf->b_has_sign_column = TRUE;
5748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 }
5750 else
5751 prev->next = newsign;
5752 }
5753}
5754
5755/*
5756 * Add the sign into the signlist. Find the right spot to do it though.
5757 */
5758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005759buf_addsign(
5760 buf_T *buf, /* buffer to store sign in */
5761 int id, /* sign ID */
5762 linenr_T lnum, /* line number which gets the mark */
5763 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 signlist_T *sign; /* a sign in the signlist */
5766 signlist_T *prev; /* the previous sign */
5767
5768 prev = NULL;
5769 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5770 {
5771 if (lnum == sign->lnum && id == sign->id)
5772 {
5773 sign->typenr = typenr;
5774 return;
5775 }
5776 else if (
5777#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5778 id < 0 &&
5779#endif
5780 lnum < sign->lnum)
5781 {
5782#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5783 /* XXX - GRP: Is this because of sign slide problem? Or is it
5784 * really needed? Or is it because we allow multiple signs per
5785 * line? If so, should I add that feature to FEAT_SIGNS?
5786 */
5787 while (prev != NULL && prev->lnum == lnum)
5788 prev = prev->prev;
5789 if (prev == NULL)
5790 sign = buf->b_signlist;
5791 else
5792 sign = prev->next;
5793#endif
5794 insert_sign(buf, prev, sign, id, lnum, typenr);
5795 return;
5796 }
5797 prev = sign;
5798 }
5799#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5800 /* XXX - GRP: See previous comment */
5801 while (prev != NULL && prev->lnum == lnum)
5802 prev = prev->prev;
5803 if (prev == NULL)
5804 sign = buf->b_signlist;
5805 else
5806 sign = prev->next;
5807#endif
5808 insert_sign(buf, prev, sign, id, lnum, typenr);
5809
5810 return;
5811}
5812
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005813/*
5814 * For an existing, placed sign "markId" change the type to "typenr".
5815 * Returns the line number of the sign, or zero if the sign is not found.
5816 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005817 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005818buf_change_sign_type(
5819 buf_T *buf, /* buffer to store sign in */
5820 int markId, /* sign ID */
5821 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822{
5823 signlist_T *sign; /* a sign in the signlist */
5824
5825 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5826 {
5827 if (sign->id == markId)
5828 {
5829 sign->typenr = typenr;
5830 return sign->lnum;
5831 }
5832 }
5833
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005834 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835}
5836
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005838buf_getsigntype(
5839 buf_T *buf,
5840 linenr_T lnum,
5841 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843 signlist_T *sign; /* a sign in a b_signlist */
5844
5845 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5846 if (sign->lnum == lnum
5847 && (type == SIGN_ANY
5848# ifdef FEAT_SIGN_ICONS
5849 || (type == SIGN_ICON
5850 && sign_get_image(sign->typenr) != NULL)
5851# endif
5852 || (type == SIGN_TEXT
5853 && sign_get_text(sign->typenr) != NULL)
5854 || (type == SIGN_LINEHL
5855 && sign_get_attr(sign->typenr, TRUE) != 0)))
5856 return sign->typenr;
5857 return 0;
5858}
5859
5860
5861 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005862buf_delsign(
5863 buf_T *buf, /* buffer sign is stored in */
5864 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 signlist_T **lastp; /* pointer to pointer to current sign */
5867 signlist_T *sign; /* a sign in a b_signlist */
5868 signlist_T *next; /* the next sign in a b_signlist */
5869 linenr_T lnum; /* line number whose sign was deleted */
5870
5871 lastp = &buf->b_signlist;
5872 lnum = 0;
5873 for (sign = buf->b_signlist; sign != NULL; sign = next)
5874 {
5875 next = sign->next;
5876 if (sign->id == id)
5877 {
5878 *lastp = next;
5879#ifdef FEAT_NETBEANS_INTG
5880 if (next != NULL)
5881 next->prev = sign->prev;
5882#endif
5883 lnum = sign->lnum;
5884 vim_free(sign);
5885 break;
5886 }
5887 else
5888 lastp = &sign->next;
5889 }
5890
5891 /* When deleted the last sign need to redraw the windows to remove the
5892 * sign column. */
5893 if (buf->b_signlist == NULL)
5894 {
5895 redraw_buf_later(buf, NOT_VALID);
5896 changed_cline_bef_curs();
5897 }
5898
5899 return lnum;
5900}
5901
5902
5903/*
5904 * Find the line number of the sign with the requested id. If the sign does
5905 * not exist, return 0 as the line number. This will still let the correct file
5906 * get loaded.
5907 */
5908 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005909buf_findsign(
5910 buf_T *buf, /* buffer to store sign in */
5911 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
5913 signlist_T *sign; /* a sign in the signlist */
5914
5915 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5916 if (sign->id == id)
5917 return sign->lnum;
5918
5919 return 0;
5920}
5921
5922 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005923buf_findsign_id(
5924 buf_T *buf, /* buffer whose sign we are searching for */
5925 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 signlist_T *sign; /* a sign in the signlist */
5928
5929 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5930 if (sign->lnum == lnum)
5931 return sign->id;
5932
5933 return 0;
5934}
5935
5936
5937# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5938/* see if a given type of sign exists on a specific line */
5939 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005940buf_findsigntype_id(
5941 buf_T *buf, /* buffer whose sign we are searching for */
5942 linenr_T lnum, /* line number of sign */
5943 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 signlist_T *sign; /* a sign in the signlist */
5946
5947 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5948 if (sign->lnum == lnum && sign->typenr == typenr)
5949 return sign->id;
5950
5951 return 0;
5952}
5953
5954
5955# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5956/* return the number of icons on the given line */
5957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005958buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 signlist_T *sign; /* a sign in the signlist */
5961 int count = 0;
5962
5963 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5964 if (sign->lnum == lnum)
5965 if (sign_get_image(sign->typenr) != NULL)
5966 count++;
5967
5968 return count;
5969}
5970# endif /* FEAT_SIGN_ICONS */
5971# endif /* FEAT_NETBEANS_INTG */
5972
5973
5974/*
5975 * Delete signs in buffer "buf".
5976 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005978buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979{
5980 signlist_T *next;
5981
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005982 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005983 * sign column. Not when curwin is NULL (this means we're exiting). */
5984 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005985 {
5986 redraw_buf_later(buf, NOT_VALID);
5987 changed_cline_bef_curs();
5988 }
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 while (buf->b_signlist != NULL)
5991 {
5992 next = buf->b_signlist->next;
5993 vim_free(buf->b_signlist);
5994 buf->b_signlist = next;
5995 }
5996}
5997
5998/*
5999 * Delete all signs in all buffers.
6000 */
6001 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006002buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 buf_T *buf; /* buffer we are checking for signs */
6005
Bram Moolenaar29323592016-07-24 22:04:11 +02006006 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009}
6010
6011/*
6012 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6013 */
6014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006015sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 buf_T *buf;
6018 signlist_T *p;
6019 char lbuf[BUFSIZ];
6020
6021 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6022 msg_putchar('\n');
6023 if (rbuf == NULL)
6024 buf = firstbuf;
6025 else
6026 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006027 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 {
6029 if (buf->b_signlist != NULL)
6030 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006031 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006032 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 msg_putchar('\n');
6034 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006035 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006037 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6039 MSG_PUTS(lbuf);
6040 msg_putchar('\n');
6041 }
6042 if (rbuf != NULL)
6043 break;
6044 buf = buf->b_next;
6045 }
6046}
6047
6048/*
6049 * Adjust a placed sign for inserted/deleted lines.
6050 */
6051 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006052sign_mark_adjust(
6053 linenr_T line1,
6054 linenr_T line2,
6055 long amount,
6056 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 signlist_T *sign; /* a sign in a b_signlist */
6059
6060 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6061 {
6062 if (sign->lnum >= line1 && sign->lnum <= line2)
6063 {
6064 if (amount == MAXLNUM)
6065 sign->lnum = line1;
6066 else
6067 sign->lnum += amount;
6068 }
6069 else if (sign->lnum > line2)
6070 sign->lnum += amount_after;
6071 }
6072}
6073#endif /* FEAT_SIGNS */
6074
6075/*
6076 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6077 */
6078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006079set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
6081 if (on != curbuf->b_p_bl)
6082 {
6083 curbuf->b_p_bl = on;
6084#ifdef FEAT_AUTOCMD
6085 if (on)
6086 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6087 else
6088 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6089#endif
6090 }
6091}
6092
6093/*
6094 * Read the file for "buf" again and check if the contents changed.
6095 * Return TRUE if it changed or this could not be checked.
6096 */
6097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006098buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 buf_T *newbuf;
6101 int differ = TRUE;
6102 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 exarg_T ea;
6105
6106 /* Allocate a buffer without putting it in the buffer list. */
6107 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6108 if (newbuf == NULL)
6109 return TRUE;
6110
6111 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6112 if (prep_exarg(&ea, buf) == FAIL)
6113 {
6114 wipe_buffer(newbuf, FALSE);
6115 return TRUE;
6116 }
6117
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 /* set curwin/curbuf to buf and save a few things */
6119 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
Bram Moolenaar4770d092006-01-12 23:22:24 +00006121 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 && readfile(buf->b_ffname, buf->b_fname,
6123 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6124 &ea, READ_NEW | READ_DUMMY) == OK)
6125 {
6126 /* compare the two files line by line */
6127 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6128 {
6129 differ = FALSE;
6130 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6131 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6132 {
6133 differ = TRUE;
6134 break;
6135 }
6136 }
6137 }
6138 vim_free(ea.cmd);
6139
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 /* restore curwin/curbuf and a few other things */
6141 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143 if (curbuf != newbuf) /* safety check */
6144 wipe_buffer(newbuf, FALSE);
6145
6146 return differ;
6147}
6148
6149/*
6150 * Wipe out a buffer and decrement the last buffer number if it was used for
6151 * this buffer. Call this to wipe out a temp buffer that does not contain any
6152 * marks.
6153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006155wipe_buffer(
6156 buf_T *buf,
6157 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 if (buf->b_fnum == top_file_num - 1)
6160 --top_file_num;
6161
6162#ifdef FEAT_AUTOCMD
6163 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006164 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006166 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167#ifdef FEAT_AUTOCMD
6168 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006169 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170#endif
6171}