blob: 9ef8a5082262f08cfb678383410d20626a12bdc7 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaarf28dbce2016-01-29 22:03:47 +010038static buf_T *buflist_findname_stat(char_u *ffname, struct stat *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, struct stat *stp);
40static int buf_same_ino(buf_T *buf, struct stat *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 Moolenaar071d4272004-06-13 20:20:40 +000070/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020071 * Open current buffer, that is: open the memfile and read the file into
72 * memory.
73 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
75 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010076open_buffer(
77 int read_stdin, /* read file from stdin */
78 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
79 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080{
81 int retval = OK;
82#ifdef FEAT_AUTOCMD
83 buf_T *old_curbuf;
84#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +010085#ifdef FEAT_SYN_HL
86 long old_tw = curbuf->b_p_tw;
87#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89 /*
90 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
91 * When re-entering the same buffer, it should not change, because the
92 * user may have reset the flag by hand.
93 */
94 if (readonlymode && curbuf->b_ffname != NULL
95 && (curbuf->b_flags & BF_NEVERLOADED))
96 curbuf->b_p_ro = TRUE;
97
Bram Moolenaar4770d092006-01-12 23:22:24 +000098 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 {
100 /*
101 * There MUST be a memfile, otherwise we can't do anything
102 * If we can't create one for the current buffer, take another buffer
103 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100104 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
106 if (curbuf->b_ml.ml_mfp != NULL)
107 break;
108 /*
109 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000110 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 */
112 if (curbuf == NULL)
113 {
114 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
115 getout(2);
116 }
117 EMSG(_("E83: Cannot allocate buffer, using other one..."));
118 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100119#ifdef FEAT_SYN_HL
120 if (old_tw != curbuf->b_p_tw)
121 check_colorcolumn(curwin);
122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 return FAIL;
124 }
125
126#ifdef FEAT_AUTOCMD
127 /* The autocommands in readfile() may change the buffer, but only AFTER
128 * reading the file. */
129 old_curbuf = curbuf;
130 modified_was_set = FALSE;
131#endif
132
133 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100134 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136 if (curbuf->b_ffname != NULL
137#ifdef FEAT_NETBEANS_INTG
138 && netbeansReadFile
139#endif
140 )
141 {
142#ifdef FEAT_NETBEANS_INTG
143 int oldFire = netbeansFireChanges;
144
145 netbeansFireChanges = 0;
146#endif
147 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200148 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
149 flags | READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#ifdef FEAT_NETBEANS_INTG
151 netbeansFireChanges = oldFire;
152#endif
153 /* Help buffer is filtered. */
154 if (curbuf->b_help)
155 fix_help_buffer();
156 }
157 else if (read_stdin)
158 {
159 int save_bin = curbuf->b_p_bin;
160 linenr_T line_count;
161
162 /*
163 * First read the text in binary mode into the buffer.
164 * Then read from that same buffer and append at the end. This makes
165 * it possible to retry when 'fileformat' or 'fileencoding' was
166 * guessed wrong.
167 */
168 curbuf->b_p_bin = TRUE;
169 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200170 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
171 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 curbuf->b_p_bin = save_bin;
173 if (retval == OK)
174 {
175 line_count = curbuf->b_ml.ml_line_count;
176 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200177 (linenr_T)0, (linenr_T)MAXLNUM, eap,
178 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 if (retval == OK)
180 {
181 /* Delete the binary lines. */
182 while (--line_count >= 0)
183 ml_delete((linenr_T)1, FALSE);
184 }
185 else
186 {
187 /* Delete the converted lines. */
188 while (curbuf->b_ml.ml_line_count > line_count)
189 ml_delete(line_count, FALSE);
190 }
191 /* Put the cursor on the first line. */
192 curwin->w_cursor.lnum = 1;
193 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000194
195 /* Set or reset 'modified' before executing autocommands, so that
196 * it can be changed there. */
197 if (!readonlymode && !bufempty())
198 changed();
199 else if (retval != FAIL)
200 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef FEAT_AUTOCMD
202# ifdef FEAT_EVAL
203 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
204 curbuf, &retval);
205# else
206 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
207# endif
208#endif
209 }
210 }
211
212 /* if first time loading this buffer, init b_chartab[] */
213 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100216#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100217 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100218#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
221 /*
222 * Set/reset the Changed flag first, autocmds may change the buffer.
223 * Apply the automatic commands, before processing the modelines.
224 * So the modelines have priority over auto commands.
225 */
226 /* When reading stdin, the buffer contents always needs writing, so set
227 * the changed flag. Unless in readonly mode: "ls | gview -".
228 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000229 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#ifdef FEAT_AUTOCMD
231 || modified_was_set /* ":set modified" used in autocmd */
232# ifdef FEAT_EVAL
233 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
234# endif
235#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000236 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000238 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 unchanged(curbuf, FALSE);
240 save_file_ff(curbuf); /* keep this fileformat */
241
242 /* require "!" to overwrite the file, because it wasn't read completely */
243#ifdef FEAT_EVAL
244 if (aborting())
245#else
246 if (got_int)
247#endif
248 curbuf->b_flags |= BF_READERR;
249
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000250#ifdef FEAT_FOLDING
251 /* Need to update automatic folding. Do this before the autocommands,
252 * they may use the fold info. */
253 foldUpdateAll(curwin);
254#endif
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifdef FEAT_AUTOCMD
257 /* need to set w_topline, unless some autocommand already did that. */
258 if (!(curwin->w_valid & VALID_TOPLINE))
259 {
260 curwin->w_topline = 1;
261# ifdef FEAT_DIFF
262 curwin->w_topfill = 0;
263# endif
264 }
265# ifdef FEAT_EVAL
266 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
267# else
268 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
269# endif
270#endif
271
272 if (retval != FAIL)
273 {
274#ifdef FEAT_AUTOCMD
275 /*
276 * The autocommands may have changed the current buffer. Apply the
277 * modelines to the correct buffer, if it still exists and is loaded.
278 */
279 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
280 {
281 aco_save_T aco;
282
283 /* Go to the buffer that was opened. */
284 aucmd_prepbuf(&aco, old_curbuf);
285#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000286 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
288
289#ifdef FEAT_AUTOCMD
290# ifdef FEAT_EVAL
291 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
292 &retval);
293# else
294 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
295# endif
296
297 /* restore curwin/curbuf and a few other things */
298 aucmd_restbuf(&aco);
299 }
300#endif
301 }
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 return retval;
304}
305
306/*
307 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
308 */
309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100310buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311{
312 buf_T *bp;
313
314 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
315 if (bp == buf)
316 return TRUE;
317 return FALSE;
318}
319
320/*
321 * Close the link to a buffer.
322 * "action" is used when there is no longer a window for the buffer.
323 * It can be:
324 * 0 buffer becomes hidden
325 * DOBUF_UNLOAD buffer is unloaded
326 * DOBUF_DELETE buffer is unloaded and removed from buffer list
327 * DOBUF_WIPE buffer is unloaded and really deleted
328 * When doing all but the first one on the current buffer, the caller should
329 * get a new buffer very soon!
330 *
331 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100332 *
333 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
334 * cause there to be only one window with this buffer. e.g. when ":quit" is
335 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 */
337 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100338close_buffer(
339 win_T *win, /* if not NULL, set b_last_cursor */
340 buf_T *buf,
341 int action,
342 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
344#ifdef FEAT_AUTOCMD
345 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100346 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
348 int unload_buf = (action != 0);
349 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
350 int wipe_buf = (action == DOBUF_WIPE);
351
352#ifdef FEAT_QUICKFIX
353 /*
354 * Force unloading or deleting when 'bufhidden' says so.
355 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
356 * "hide" (otherwise we could never free or delete a buffer).
357 */
358 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
359 {
360 del_buf = TRUE;
361 unload_buf = TRUE;
362 }
363 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
364 {
365 del_buf = TRUE;
366 unload_buf = TRUE;
367 wipe_buf = TRUE;
368 }
369 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
370 unload_buf = TRUE;
371#endif
372
Bram Moolenaar3be85852014-06-12 14:01:31 +0200373 if (win != NULL
374#ifdef FEAT_WINDOWS
375 && win_valid(win) /* in case autocommands closed the window */
376#endif
377 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
379 /* Set b_last_cursor when closing the last window for the buffer.
380 * Remember the last cursor position and window options of the buffer.
381 * This used to be only for the current window, but then options like
382 * 'foldmethod' may be lost with a ":only" command. */
383 if (buf->b_nwindows == 1)
384 set_last_cursor(win);
385 buflist_setfpos(buf, win,
386 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
387 win->w_cursor.col, TRUE);
388 }
389
390#ifdef FEAT_AUTOCMD
391 /* When the buffer is no longer in a window, trigger BufWinLeave */
392 if (buf->b_nwindows == 1)
393 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200394 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
396 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200397 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100398 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200399 /* Autocommands deleted the buffer. */
400aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100401 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100403 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200404 buf->b_closing = FALSE;
405 if (abort_if_last && one_window())
406 /* Autocommands made this the only window. */
407 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
409 /* When the buffer becomes hidden, but is not unloaded, trigger
410 * BufHidden */
411 if (!unload_buf)
412 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200413 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
415 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200416 if (!buf_valid(buf))
417 /* Autocommands deleted the buffer. */
418 goto aucmd_abort;
419 buf->b_closing = FALSE;
420 if (abort_if_last && one_window())
421 /* Autocommands made this the only window. */
422 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 }
424# ifdef FEAT_EVAL
425 if (aborting()) /* autocmds may abort script processing */
426 return;
427# endif
428 }
429 nwindows = buf->b_nwindows;
430#endif
431
432 /* decrease the link count from windows (unless not in any window) */
433 if (buf->b_nwindows > 0)
434 --buf->b_nwindows;
435
436 /* Return when a window is displaying the buffer or when it's not
437 * unloaded. */
438 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440
441 /* Always remove the buffer when there is no file name. */
442 if (buf->b_ffname == NULL)
443 del_buf = TRUE;
444
445 /*
446 * Free all things allocated for this buffer.
447 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
448 */
449#ifdef FEAT_AUTOCMD
450 /* Remember if we are closing the current buffer. Restore the number of
451 * windows, so that autocommands in buf_freeall() don't get confused. */
452 is_curbuf = (buf == curbuf);
453 buf->b_nwindows = nwindows;
454#endif
455
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200456 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200457 if (
458#ifdef FEAT_WINDOWS
459 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100460#else
461 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200462#endif
463 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200464 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466#ifdef FEAT_AUTOCMD
467 /* Autocommands may have deleted the buffer. */
468 if (!buf_valid(buf))
469 return;
470# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000471 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 return;
473# endif
474
475 /* Autocommands may have opened or closed windows for this buffer.
476 * Decrement the count for the close we do here. */
477 if (buf->b_nwindows > 0)
478 --buf->b_nwindows;
479
480 /*
481 * It's possible that autocommands change curbuf to the one being deleted.
482 * This might cause the previous curbuf to be deleted unexpectedly. But
483 * in some cases it's OK to delete the curbuf, because a new one is
484 * obtained anyway. Therefore only return if curbuf changed to the
485 * deleted buffer.
486 */
487 if (buf == curbuf && !is_curbuf)
488 return;
489#endif
490
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000491 /* Change directories when the 'acd' option is set. */
492 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494 /*
495 * Remove the buffer from the list.
496 */
497 if (wipe_buf)
498 {
499#ifdef FEAT_SUN_WORKSHOP
500 if (usingSunWorkShop)
501 workshop_file_closed_lineno((char *)buf->b_ffname,
502 (int)buf->b_last_cursor.lnum);
503#endif
504 vim_free(buf->b_ffname);
505 vim_free(buf->b_sfname);
506 if (buf->b_prev == NULL)
507 firstbuf = buf->b_next;
508 else
509 buf->b_prev->b_next = buf->b_next;
510 if (buf->b_next == NULL)
511 lastbuf = buf->b_prev;
512 else
513 buf->b_next->b_prev = buf->b_prev;
514 free_buffer(buf);
515 }
516 else
517 {
518 if (del_buf)
519 {
520 /* Free all internal variables and reset option values, to make
521 * ":bdel" compatible with Vim 5.7. */
522 free_buffer_stuff(buf, TRUE);
523
524 /* Make it look like a new buffer. */
525 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
526
527 /* Init the options when loaded again. */
528 buf->b_p_initialized = FALSE;
529 }
530 buf_clear_file(buf);
531 if (del_buf)
532 buf->b_p_bl = FALSE;
533 }
534}
535
536/*
537 * Make buffer not contain a file.
538 */
539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100540buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 buf->b_ml.ml_line_count = 1;
543 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 buf->b_p_eol = TRUE;
546 buf->b_start_eol = TRUE;
547#ifdef FEAT_MBYTE
548 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000549 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550#endif
551 buf->b_ml.ml_mfp = NULL;
552 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
553#ifdef FEAT_NETBEANS_INTG
554 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555#endif
556}
557
558/*
559 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200560 * the file. flags:
561 * BFA_DEL buffer is going to be deleted
562 * BFA_WIPE buffer is going to be wiped out
563 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100566buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
568#ifdef FEAT_AUTOCMD
569 int is_curbuf = (buf == curbuf);
570
Bram Moolenaar362ce482012-06-06 19:02:45 +0200571 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
573 if (!buf_valid(buf)) /* autocommands may delete the buffer */
574 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200575 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {
577 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
578 if (!buf_valid(buf)) /* autocommands may delete the buffer */
579 return;
580 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200581 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {
583 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
584 FALSE, buf);
585 if (!buf_valid(buf)) /* autocommands may delete the buffer */
586 return;
587 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200588 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000590 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 return;
592# endif
593
594 /*
595 * It's possible that autocommands change curbuf to the one being deleted.
596 * This might cause curbuf to be deleted unexpectedly. But in some cases
597 * it's OK to delete the curbuf, because a new one is obtained anyway.
598 * Therefore only return if curbuf changed to the deleted buffer.
599 */
600 if (buf == curbuf && !is_curbuf)
601 return;
602#endif
603#ifdef FEAT_DIFF
604 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
605#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200606#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100607 /* Remove any ownsyntax, unless exiting. */
608 if (firstwin != NULL && curwin->w_buffer == buf)
609 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200610#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000611
612#ifdef FEAT_FOLDING
613 /* No folds in an empty buffer. */
614# ifdef FEAT_WINDOWS
615 {
616 win_T *win;
617 tabpage_T *tp;
618
619 FOR_ALL_TAB_WINDOWS(tp, win)
620 if (win->w_buffer == buf)
621 clearFolding(win);
622 }
623# else
624 if (curwin->w_buffer == buf)
625 clearFolding(curwin);
626# endif
627#endif
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#ifdef FEAT_TCL
630 tcl_buffer_free(buf);
631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ml_close(buf, TRUE); /* close and delete the memline/memfile */
633 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200634 if ((flags & BFA_KEEP_UNDO) == 0)
635 {
636 u_blockfree(buf); /* free the memory allocated for undo */
637 u_clearall(buf); /* reset all undo information */
638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000642 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643}
644
645/*
646 * Free a buffer structure and the things it contains related to the buffer
647 * itself (not the file, that must have been done already).
648 */
649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100650free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200653#ifdef FEAT_EVAL
654 unref_var_dict(buf->b_vars);
655#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200656#ifdef FEAT_LUA
657 lua_buffer_free(buf);
658#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000659#ifdef FEAT_MZSCHEME
660 mzscheme_buffer_free(buf);
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662#ifdef FEAT_PERL
663 perl_buf_free(buf);
664#endif
665#ifdef FEAT_PYTHON
666 python_buffer_free(buf);
667#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200668#ifdef FEAT_PYTHON3
669 python3_buffer_free(buf);
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671#ifdef FEAT_RUBY
672 ruby_buffer_free(buf);
673#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000674#ifdef FEAT_AUTOCMD
675 aubuflocal_remove(buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200676 if (autocmd_busy)
677 {
678 /* Do not free the buffer structure while autocommands are executing,
679 * it's still needed. Free it when autocmd_busy is reset. */
680 buf->b_next = au_pending_free_buf;
681 au_pending_free_buf = buf;
682 }
683 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000684#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200685 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686}
687
688/*
689 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
690 */
691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100692free_buffer_stuff(
693 buf_T *buf,
694 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
696 if (free_options)
697 {
698 clear_wininfo(buf); /* including window-local options */
699 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200700#ifdef FEAT_SPELL
701 ga_clear(&buf->b_s.b_langp);
702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
704#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200705 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
706 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#endif
708#ifdef FEAT_USR_CMDS
709 uc_clear(&buf->b_ucmds); /* clear local user commands */
710#endif
711#ifdef FEAT_SIGNS
712 buf_delete_signs(buf); /* delete any signs */
713#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000714#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200715 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#ifdef FEAT_LOCALMAP
718 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
719 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
720#endif
721#ifdef FEAT_MBYTE
722 vim_free(buf->b_start_fenc);
723 buf->b_start_fenc = NULL;
724#endif
725}
726
727/*
728 * Free the b_wininfo list for buffer "buf".
729 */
730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100731clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733 wininfo_T *wip;
734
735 while (buf->b_wininfo != NULL)
736 {
737 wip = buf->b_wininfo;
738 buf->b_wininfo = wip->wi_next;
739 if (wip->wi_optset)
740 {
741 clear_winopt(&wip->wi_opt);
742#ifdef FEAT_FOLDING
743 deleteFoldRecurse(&wip->wi_folds);
744#endif
745 }
746 vim_free(wip);
747 }
748}
749
750#if defined(FEAT_LISTCMDS) || defined(PROTO)
751/*
752 * Go to another buffer. Handles the result of the ATTENTION dialog.
753 */
754 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100755goto_buffer(
756 exarg_T *eap,
757 int start,
758 int dir,
759 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000761# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 buf_T *old_curbuf = curbuf;
763
764 swap_exists_action = SEA_DIALOG;
765# endif
766 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
767 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000768# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
770 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000771# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
772 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000773
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000774 /* Reset the error/interrupt/exception state here so that
775 * aborting() returns FALSE when closing a window. */
776 enter_cleanup(&cs);
777# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000778
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000779 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 win_close(curwin, TRUE);
781 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000782 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000783
784# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
785 /* Restore the error/interrupt/exception state if not discarded by a
786 * new aborting error, interrupt, or uncaught exception. */
787 leave_cleanup(&cs);
788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
790 else
791 handle_swap_exists(old_curbuf);
792# endif
793}
794#endif
795
Bram Moolenaare64ac772005-12-07 20:54:59 +0000796#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797/*
798 * Handle the situation of swap_exists_action being set.
799 * It is allowed for "old_curbuf" to be NULL or invalid.
800 */
801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100802handle_swap_exists(buf_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000804# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
805 cleanup_T cs;
806# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100807#ifdef FEAT_SYN_HL
808 long old_tw = curbuf->b_p_tw;
809#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (swap_exists_action == SEA_QUIT)
812 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000813# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
814 /* Reset the error/interrupt/exception state here so that
815 * aborting() returns FALSE when closing a buffer. */
816 enter_cleanup(&cs);
817# endif
818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 /* User selected Quit at ATTENTION prompt. Go back to previous
820 * buffer. If that buffer is gone or the same as the current one,
821 * open a new, empty buffer. */
822 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000823 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100824 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000826 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100830#ifdef FEAT_SYN_HL
831 if (old_tw != curbuf->b_p_tw)
832 check_colorcolumn(curwin);
833#endif
834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000836
837# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
838 /* Restore the error/interrupt/exception state if not discarded by a
839 * new aborting error, interrupt, or uncaught exception. */
840 leave_cleanup(&cs);
841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 else if (swap_exists_action == SEA_RECOVER)
844 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000845# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
846 /* Reset the error/interrupt/exception state here so that
847 * aborting() returns FALSE when closing a buffer. */
848 enter_cleanup(&cs);
849# endif
850
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 /* User selected Recover at ATTENTION prompt. */
852 msg_scroll = TRUE;
853 ml_recover();
854 MSG_PUTS("\n"); /* don't overwrite the last message */
855 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000856 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000857
858# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
859 /* Restore the error/interrupt/exception state if not discarded by a
860 * new aborting error, interrupt, or uncaught exception. */
861 leave_cleanup(&cs);
862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864 swap_exists_action = SEA_NONE;
865}
866#endif
867
868#if defined(FEAT_LISTCMDS) || defined(PROTO)
869/*
870 * do_bufdel() - delete or unload buffer(s)
871 *
872 * addr_count == 0: ":bdel" - delete current buffer
873 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
874 * buffer "end_bnr", then any other arguments.
875 * addr_count == 2: ":N,N bdel" - delete buffers in range
876 *
877 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
878 * DOBUF_DEL (":bdel")
879 *
880 * Returns error message or NULL
881 */
882 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100883do_bufdel(
884 int command,
885 char_u *arg, /* pointer to extra arguments */
886 int addr_count,
887 int start_bnr, /* first buffer number in a range */
888 int end_bnr, /* buffer nr or last buffer nr in a range */
889 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 int do_current = 0; /* delete current buffer? */
892 int deleted = 0; /* number of buffers deleted */
893 char_u *errormsg = NULL; /* return value */
894 int bnr; /* buffer number */
895 char_u *p;
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (addr_count == 0)
898 {
899 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
900 }
901 else
902 {
903 if (addr_count == 2)
904 {
905 if (*arg) /* both range and argument is not allowed */
906 return (char_u *)_(e_trailing);
907 bnr = start_bnr;
908 }
909 else /* addr_count == 1 */
910 bnr = end_bnr;
911
912 for ( ;!got_int; ui_breakcheck())
913 {
914 /*
915 * delete the current buffer last, otherwise when the
916 * current buffer is deleted, the next buffer becomes
917 * the current one and will be loaded, which may then
918 * also be deleted, etc.
919 */
920 if (bnr == curbuf->b_fnum)
921 do_current = bnr;
922 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
923 forceit) == OK)
924 ++deleted;
925
926 /*
927 * find next buffer number to delete/unload
928 */
929 if (addr_count == 2)
930 {
931 if (++bnr > end_bnr)
932 break;
933 }
934 else /* addr_count == 1 */
935 {
936 arg = skipwhite(arg);
937 if (*arg == NUL)
938 break;
939 if (!VIM_ISDIGIT(*arg))
940 {
941 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100942 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
943 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (bnr < 0) /* failed */
945 break;
946 arg = p;
947 }
948 else
949 bnr = getdigits(&arg);
950 }
951 }
952 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
953 FORWARD, do_current, forceit) == OK)
954 ++deleted;
955
956 if (deleted == 0)
957 {
958 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000959 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000961 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000963 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 errormsg = IObuff;
965 }
966 else if (deleted >= p_report)
967 {
968 if (command == DOBUF_UNLOAD)
969 {
970 if (deleted == 1)
971 MSG(_("1 buffer unloaded"));
972 else
973 smsg((char_u *)_("%d buffers unloaded"), deleted);
974 }
975 else if (command == DOBUF_DEL)
976 {
977 if (deleted == 1)
978 MSG(_("1 buffer deleted"));
979 else
980 smsg((char_u *)_("%d buffers deleted"), deleted);
981 }
982 else
983 {
984 if (deleted == 1)
985 MSG(_("1 buffer wiped out"));
986 else
987 smsg((char_u *)_("%d buffers wiped out"), deleted);
988 }
989 }
990 }
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
993 return errormsg;
994}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200995#endif /* FEAT_LISTCMDS */
996
997#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
998 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001000static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001001
1002/*
1003 * Make the current buffer empty.
1004 * Used when it is wiped out and it's the last buffer.
1005 */
1006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001007empty_curbuf(
1008 int close_others,
1009 int forceit,
1010 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001011{
1012 int retval;
1013 buf_T *buf = curbuf;
1014
1015 if (action == DOBUF_UNLOAD)
1016 {
1017 EMSG(_("E90: Cannot unload last buffer"));
1018 return FAIL;
1019 }
1020
1021 if (close_others)
1022 {
1023 /* Close any other windows on this buffer, then make it empty. */
1024#ifdef FEAT_WINDOWS
1025 close_windows(buf, TRUE);
1026#endif
1027 }
1028
1029 setpcmark();
1030 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1031 forceit ? ECMD_FORCEIT : 0, curwin);
1032
1033 /*
1034 * do_ecmd() may create a new buffer, then we have to delete
1035 * the old one. But do_ecmd() may have done that already, check
1036 * if the buffer still exists.
1037 */
1038 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1039 close_buffer(NULL, buf, action, FALSE);
1040 if (!close_others)
1041 need_fileinfo = FALSE;
1042 return retval;
1043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044/*
1045 * Implementation of the commands for the buffer list.
1046 *
1047 * action == DOBUF_GOTO go to specified buffer
1048 * action == DOBUF_SPLIT split window and go to specified buffer
1049 * action == DOBUF_UNLOAD unload specified buffer(s)
1050 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1051 * action == DOBUF_WIPE delete specified buffer(s) really
1052 *
1053 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1054 * start == DOBUF_FIRST go to "count" buffer from first buffer
1055 * start == DOBUF_LAST go to "count" buffer from last buffer
1056 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1057 *
1058 * Return FAIL or OK.
1059 */
1060 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001061do_buffer(
1062 int action,
1063 int start,
1064 int dir, /* FORWARD or BACKWARD */
1065 int count, /* buffer number or number of buffers */
1066 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
1068 buf_T *buf;
1069 buf_T *bp;
1070 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1071 || action == DOBUF_WIPE);
1072
1073 switch (start)
1074 {
1075 case DOBUF_FIRST: buf = firstbuf; break;
1076 case DOBUF_LAST: buf = lastbuf; break;
1077 default: buf = curbuf; break;
1078 }
1079 if (start == DOBUF_MOD) /* find next modified buffer */
1080 {
1081 while (count-- > 0)
1082 {
1083 do
1084 {
1085 buf = buf->b_next;
1086 if (buf == NULL)
1087 buf = firstbuf;
1088 }
1089 while (buf != curbuf && !bufIsChanged(buf));
1090 }
1091 if (!bufIsChanged(buf))
1092 {
1093 EMSG(_("E84: No modified buffer found"));
1094 return FAIL;
1095 }
1096 }
1097 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1098 {
1099 while (buf != NULL && buf->b_fnum != count)
1100 buf = buf->b_next;
1101 }
1102 else
1103 {
1104 bp = NULL;
1105 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1106 {
1107 /* remember the buffer where we start, we come back there when all
1108 * buffers are unlisted. */
1109 if (bp == NULL)
1110 bp = buf;
1111 if (dir == FORWARD)
1112 {
1113 buf = buf->b_next;
1114 if (buf == NULL)
1115 buf = firstbuf;
1116 }
1117 else
1118 {
1119 buf = buf->b_prev;
1120 if (buf == NULL)
1121 buf = lastbuf;
1122 }
1123 /* don't count unlisted buffers */
1124 if (unload || buf->b_p_bl)
1125 {
1126 --count;
1127 bp = NULL; /* use this buffer as new starting point */
1128 }
1129 if (bp == buf)
1130 {
1131 /* back where we started, didn't find anything. */
1132 EMSG(_("E85: There is no listed buffer"));
1133 return FAIL;
1134 }
1135 }
1136 }
1137
1138 if (buf == NULL) /* could not find it */
1139 {
1140 if (start == DOBUF_FIRST)
1141 {
1142 /* don't warn when deleting */
1143 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001144 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146 else if (dir == FORWARD)
1147 EMSG(_("E87: Cannot go beyond last buffer"));
1148 else
1149 EMSG(_("E88: Cannot go before first buffer"));
1150 return FAIL;
1151 }
1152
1153#ifdef FEAT_GUI
1154 need_mouse_correct = TRUE;
1155#endif
1156
1157#ifdef FEAT_LISTCMDS
1158 /*
1159 * delete buffer buf from memory and/or the list
1160 */
1161 if (unload)
1162 {
1163 int forward;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
1165 /* When unloading or deleting a buffer that's already unloaded and
1166 * unlisted: fail silently. */
1167 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1168 return FAIL;
1169
1170 if (!forceit && bufIsChanged(buf))
1171 {
1172#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1173 if ((p_confirm || cmdmod.confirm) && p_write)
1174 {
1175 dialog_changed(buf, FALSE);
1176# ifdef FEAT_AUTOCMD
1177 if (!buf_valid(buf))
1178 /* Autocommand deleted buffer, oops! It's not changed
1179 * now. */
1180 return FAIL;
1181# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001182 /* If it's still changed fail silently, the dialog already
1183 * mentioned why it fails. */
1184 if (bufIsChanged(buf))
1185 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001187 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#endif
1189 {
1190 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1191 buf->b_fnum);
1192 return FAIL;
1193 }
1194 }
1195
1196 /*
1197 * If deleting the last (listed) buffer, make it empty.
1198 * The last (listed) buffer cannot be unloaded.
1199 */
1200 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1201 if (bp->b_p_bl && bp != buf)
1202 break;
1203 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001204 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
1206#ifdef FEAT_WINDOWS
1207 /*
1208 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001209 * (unless it's the only window). Repeat this so long as we end up in
1210 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001212 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001213# ifdef FEAT_AUTOCMD
1214 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1215# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001216 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001217 {
1218 if (win_close(curwin, FALSE) == FAIL)
1219 break;
1220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#endif
1222
1223 /*
1224 * If the buffer to be deleted is not the current one, delete it here.
1225 */
1226 if (buf != curbuf)
1227 {
1228#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001229 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#endif
1231 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001232 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 return OK;
1234 }
1235
1236 /*
1237 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001238 * There should be another, otherwise it would have been handled
1239 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 * First use au_new_curbuf, if it is valid.
1241 * Then prefer the buffer we most recently visited.
1242 * Else try to find one that is loaded, after the current buffer,
1243 * then before the current buffer.
1244 * Finally use any buffer.
1245 */
1246 buf = NULL; /* selected buffer */
1247 bp = NULL; /* used when no loaded buffer found */
1248#ifdef FEAT_AUTOCMD
1249 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1250 buf = au_new_curbuf;
1251# ifdef FEAT_JUMPLIST
1252 else
1253# endif
1254#endif
1255#ifdef FEAT_JUMPLIST
1256 if (curwin->w_jumplistlen > 0)
1257 {
1258 int jumpidx;
1259
1260 jumpidx = curwin->w_jumplistidx - 1;
1261 if (jumpidx < 0)
1262 jumpidx = curwin->w_jumplistlen - 1;
1263
1264 forward = jumpidx;
1265 while (jumpidx != curwin->w_jumplistidx)
1266 {
1267 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1268 if (buf != NULL)
1269 {
1270 if (buf == curbuf || !buf->b_p_bl)
1271 buf = NULL; /* skip current and unlisted bufs */
1272 else if (buf->b_ml.ml_mfp == NULL)
1273 {
1274 /* skip unloaded buf, but may keep it for later */
1275 if (bp == NULL)
1276 bp = buf;
1277 buf = NULL;
1278 }
1279 }
1280 if (buf != NULL) /* found a valid buffer: stop searching */
1281 break;
1282 /* advance to older entry in jump list */
1283 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1284 break;
1285 if (--jumpidx < 0)
1286 jumpidx = curwin->w_jumplistlen - 1;
1287 if (jumpidx == forward) /* List exhausted for sure */
1288 break;
1289 }
1290 }
1291#endif
1292
1293 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1294 {
1295 forward = TRUE;
1296 buf = curbuf->b_next;
1297 for (;;)
1298 {
1299 if (buf == NULL)
1300 {
1301 if (!forward) /* tried both directions */
1302 break;
1303 buf = curbuf->b_prev;
1304 forward = FALSE;
1305 continue;
1306 }
1307 /* in non-help buffer, try to skip help buffers, and vv */
1308 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1309 {
1310 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1311 break;
1312 if (bp == NULL) /* remember unloaded buf for later */
1313 bp = buf;
1314 }
1315 if (forward)
1316 buf = buf->b_next;
1317 else
1318 buf = buf->b_prev;
1319 }
1320 }
1321 if (buf == NULL) /* No loaded buffer, use unloaded one */
1322 buf = bp;
1323 if (buf == NULL) /* No loaded buffer, find listed one */
1324 {
1325 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1326 if (buf->b_p_bl && buf != curbuf)
1327 break;
1328 }
1329 if (buf == NULL) /* Still no buffer, just take one */
1330 {
1331 if (curbuf->b_next != NULL)
1332 buf = curbuf->b_next;
1333 else
1334 buf = curbuf->b_prev;
1335 }
1336 }
1337
Bram Moolenaara02471e2014-01-10 16:43:14 +01001338 if (buf == NULL)
1339 {
1340 /* Autocommands must have wiped out all other buffers. Only option
1341 * now is to make the current buffer empty. */
1342 return empty_curbuf(FALSE, forceit, action);
1343 }
1344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 /*
1346 * make buf current buffer
1347 */
1348 if (action == DOBUF_SPLIT) /* split window first */
1349 {
1350# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001351 /* If 'switchbuf' contains "useopen": jump to first window containing
1352 * "buf" if one exists */
1353 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001354 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001355 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001356 * page containing "buf" if one exists */
1357 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 return OK;
1359 if (win_split(0, 0) == FAIL)
1360# endif
1361 return FAIL;
1362 }
1363#endif
1364
1365 /* go to current buffer - nothing to do */
1366 if (buf == curbuf)
1367 return OK;
1368
1369 /*
1370 * Check if the current buffer may be abandoned.
1371 */
1372 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1373 {
1374#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1375 if ((p_confirm || cmdmod.confirm) && p_write)
1376 {
1377 dialog_changed(curbuf, FALSE);
1378# ifdef FEAT_AUTOCMD
1379 if (!buf_valid(buf))
1380 /* Autocommand deleted buffer, oops! */
1381 return FAIL;
1382# endif
1383 }
1384 if (bufIsChanged(curbuf))
1385#endif
1386 {
1387 EMSG(_(e_nowrtmsg));
1388 return FAIL;
1389 }
1390 }
1391
1392 /* Go to the other buffer. */
1393 set_curbuf(buf, action);
1394
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001395#if defined(FEAT_LISTCMDS) \
1396 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001398 {
1399 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#endif
1402
1403#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1404 if (aborting()) /* autocmds may abort script processing */
1405 return FAIL;
1406#endif
1407
1408 return OK;
1409}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412/*
1413 * Set current buffer to "buf". Executes autocommands and closes current
1414 * buffer. "action" tells how to close the current buffer:
1415 * DOBUF_GOTO free or hide it
1416 * DOBUF_SPLIT nothing
1417 * DOBUF_UNLOAD unload it
1418 * DOBUF_DEL delete it
1419 * DOBUF_WIPE wipe it out
1420 */
1421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001422set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 buf_T *prevbuf;
1425 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1426 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001427#ifdef FEAT_SYN_HL
1428 long old_tw = curbuf->b_p_tw;
1429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001432 if (!cmdmod.keepalt)
1433 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001434 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 /* Don't restart Select mode after switching to another buffer. */
1437 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
1439 /* close_windows() or apply_autocmds() may change curbuf */
1440 prevbuf = curbuf;
1441
1442#ifdef FEAT_AUTOCMD
1443 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1444# ifdef FEAT_EVAL
1445 if (buf_valid(prevbuf) && !aborting())
1446# else
1447 if (buf_valid(prevbuf))
1448# endif
1449#endif
1450 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001451#ifdef FEAT_SYN_HL
1452 if (prevbuf == curwin->w_buffer)
1453 reset_synblock(curwin);
1454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#ifdef FEAT_WINDOWS
1456 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001457 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458#endif
1459#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1460 if (buf_valid(prevbuf) && !aborting())
1461#else
1462 if (buf_valid(prevbuf))
1463#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001464 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001465#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001466 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001467#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001468 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001469 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1471 unload ? action : (action == DOBUF_GOTO
1472 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001473 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001474#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001475 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001476 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001477 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001478#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001482 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001483 * it did ":bunload") or aborted the script processing!
1484 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1485 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001486# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001487 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001488# endif
1489# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001490 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001491# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001492 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001496#ifdef FEAT_SYN_HL
1497 if (old_tw != curbuf->b_p_tw)
1498 check_colorcolumn(curwin);
1499#endif
1500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501}
1502
1503/*
1504 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001505 * Old curbuf must have been abandoned already! This also means "curbuf" may
1506 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 */
1508 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
1511 /* Copy buffer and window local option values. Not for a help buffer. */
1512 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1513 if (!buf->b_help)
1514 get_winopts(buf);
1515#ifdef FEAT_FOLDING
1516 else
1517 /* Remove all folds in the window. */
1518 clearFolding(curwin);
1519 foldUpdateAll(curwin); /* update folds (later). */
1520#endif
1521
1522 /* Get the buffer in the current window. */
1523 curwin->w_buffer = buf;
1524 curbuf = buf;
1525 ++curbuf->b_nwindows;
1526
1527#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001528 if (curwin->w_p_diff)
1529 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#endif
1531
Bram Moolenaara971b822011-09-14 14:43:25 +02001532#ifdef FEAT_SYN_HL
1533 curwin->w_s = &(buf->b_s);
1534#endif
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 /* Cursor on first line by default. */
1537 curwin->w_cursor.lnum = 1;
1538 curwin->w_cursor.col = 0;
1539#ifdef FEAT_VIRTUALEDIT
1540 curwin->w_cursor.coladd = 0;
1541#endif
1542 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001543#ifdef FEAT_AUTOCMD
1544 curwin->w_topline_was_set = FALSE;
1545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001547 /* mark cursor position as being invalid */
1548 curwin->w_valid = 0;
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 /* Make sure the buffer is loaded. */
1551 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001552 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001553#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001554 /* If there is no filetype, allow for detecting one. Esp. useful for
1555 * ":ball" used in a autocommand. If there already is a filetype we
1556 * might prefer to keep it. */
1557 if (*curbuf->b_p_ft == NUL)
1558 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001559#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001560
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001561 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 else
1564 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001565 if (!msg_silent)
1566 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1568#ifdef FEAT_AUTOCMD
1569 curwin->w_topline = 1;
1570# ifdef FEAT_DIFF
1571 curwin->w_topfill = 0;
1572# endif
1573 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1574 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1575#endif
1576 }
1577
1578 /* If autocommands did not change the cursor position, restore cursor lnum
1579 * and possibly cursor col. */
1580 if (curwin->w_cursor.lnum == 1 && inindent(0))
1581 buflist_getfpos();
1582
1583 check_arg_idx(curwin); /* check for valid arg_idx */
1584#ifdef FEAT_TITLE
1585 maketitle();
1586#endif
1587#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001588 /* when autocmds didn't change it */
1589 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590#endif
1591 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1592
Bram Moolenaar009b2592004-10-24 19:18:58 +00001593#ifdef FEAT_NETBEANS_INTG
1594 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001595 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001596#endif
1597
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001598 /* Change directories when the 'acd' option is set. */
1599 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601#ifdef FEAT_KEYMAP
1602 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001603 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001605#ifdef FEAT_SPELL
1606 /* May need to set the spell language. Can only do this after the buffer
1607 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001608 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1609 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001610#endif
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 redraw_later(NOT_VALID);
1613}
1614
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001615#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1616/*
1617 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001618 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001619 */
1620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001621do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001622{
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001623 if (starting == 0
1624 && curbuf->b_ffname != NULL
1625 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001626 shorten_fnames(TRUE);
1627}
1628#endif
1629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630/*
1631 * functions for dealing with the buffer list
1632 */
1633
1634/*
1635 * Add a file name to the buffer list. Return a pointer to the buffer.
1636 * If the same file name already exists return a pointer to that buffer.
1637 * If it does not exist, or if fname == NULL, a new entry is created.
1638 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1639 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1640 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 * This is the ONLY way to create a new buffer.
1642 */
1643static int top_file_num = 1; /* highest file number */
1644
1645 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646buflist_new(
1647 char_u *ffname, /* full path of fname or relative */
1648 char_u *sfname, /* short fname or NULL */
1649 linenr_T lnum, /* preferred cursor line */
1650 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
1652 buf_T *buf;
1653#ifdef UNIX
1654 struct stat st;
1655#endif
1656
1657 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1658
1659 /*
1660 * If file name already exists in the list, update the entry.
1661 */
1662#ifdef UNIX
1663 /* On Unix we can use inode numbers when the file exists. Works better
1664 * for hard links. */
1665 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1666 st.st_dev = (dev_T)-1;
1667#endif
1668 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1669#ifdef UNIX
1670 buflist_findname_stat(ffname, &st)
1671#else
1672 buflist_findname(ffname)
1673#endif
1674 ) != NULL)
1675 {
1676 vim_free(ffname);
1677 if (lnum != 0)
1678 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1679 /* copy the options now, if 'cpo' doesn't have 's' and not done
1680 * already */
1681 buf_copy_options(buf, 0);
1682 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1683 {
1684 buf->b_p_bl = TRUE;
1685#ifdef FEAT_AUTOCMD
1686 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001689 if (!buf_valid(buf))
1690 return NULL;
1691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#endif
1693 }
1694 return buf;
1695 }
1696
1697 /*
1698 * If the current buffer has no name and no contents, use the current
1699 * buffer. Otherwise: Need to allocate a new buffer structure.
1700 *
1701 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001702 * (A spell file buffer is allocated in spell.c, but that's not a normal
1703 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 */
1705 buf = NULL;
1706 if ((flags & BLN_CURBUF)
1707 && curbuf != NULL
1708 && curbuf->b_ffname == NULL
1709 && curbuf->b_nwindows <= 1
1710 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1711 {
1712 buf = curbuf;
1713#ifdef FEAT_AUTOCMD
1714 /* It's like this buffer is deleted. Watch out for autocommands that
1715 * change curbuf! If that happens, allocate a new buffer anyway. */
1716 if (curbuf->b_p_bl)
1717 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1718 if (buf == curbuf)
1719 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1720# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001721 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 return NULL;
1723# endif
1724#endif
1725#ifdef FEAT_QUICKFIX
1726# ifdef FEAT_AUTOCMD
1727 if (buf == curbuf)
1728# endif
1729 {
1730 /* Make sure 'bufhidden' and 'buftype' are empty */
1731 clear_string_option(&buf->b_p_bh);
1732 clear_string_option(&buf->b_p_bt);
1733 }
1734#endif
1735 }
1736 if (buf != curbuf || curbuf == NULL)
1737 {
1738 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1739 if (buf == NULL)
1740 {
1741 vim_free(ffname);
1742 return NULL;
1743 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001744#ifdef FEAT_EVAL
1745 /* init b: variables */
1746 buf->b_vars = dict_alloc();
1747 if (buf->b_vars == NULL)
1748 {
1749 vim_free(ffname);
1750 vim_free(buf);
1751 return NULL;
1752 }
1753 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756
1757 if (ffname != NULL)
1758 {
1759 buf->b_ffname = ffname;
1760 buf->b_sfname = vim_strsave(sfname);
1761 }
1762
1763 clear_wininfo(buf);
1764 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1765
1766 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1767 || buf->b_wininfo == NULL)
1768 {
1769 vim_free(buf->b_ffname);
1770 buf->b_ffname = NULL;
1771 vim_free(buf->b_sfname);
1772 buf->b_sfname = NULL;
1773 if (buf != curbuf)
1774 free_buffer(buf);
1775 return NULL;
1776 }
1777
1778 if (buf == curbuf)
1779 {
1780 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001781 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (buf != curbuf) /* autocommands deleted the buffer! */
1783 return NULL;
1784#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001785 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 return NULL;
1787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001789
1790 /* Init the options. */
1791 buf->b_p_initialized = FALSE;
1792 buf_copy_options(buf, BCO_ENTER);
1793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#ifdef FEAT_KEYMAP
1795 /* need to reload lmaps and set b:keymap_name */
1796 curbuf->b_kmap_state |= KEYMAP_INIT;
1797#endif
1798 }
1799 else
1800 {
1801 /*
1802 * put new buffer at the end of the buffer list
1803 */
1804 buf->b_next = NULL;
1805 if (firstbuf == NULL) /* buffer list is empty */
1806 {
1807 buf->b_prev = NULL;
1808 firstbuf = buf;
1809 }
1810 else /* append new buffer at end of list */
1811 {
1812 lastbuf->b_next = buf;
1813 buf->b_prev = lastbuf;
1814 }
1815 lastbuf = buf;
1816
1817 buf->b_fnum = top_file_num++;
1818 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1819 {
1820 EMSG(_("W14: Warning: List of file names overflow"));
1821 if (emsg_silent == 0)
1822 {
1823 out_flush();
1824 ui_delay(3000L, TRUE); /* make sure it is noticed */
1825 }
1826 top_file_num = 1;
1827 }
1828
1829 /*
1830 * Always copy the options from the current buffer.
1831 */
1832 buf_copy_options(buf, BCO_ALWAYS);
1833 }
1834
1835 buf->b_wininfo->wi_fpos.lnum = lnum;
1836 buf->b_wininfo->wi_win = curwin;
1837
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001838#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001839 hash_init(&buf->b_s.b_keywtab);
1840 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841#endif
1842
1843 buf->b_fname = buf->b_sfname;
1844#ifdef UNIX
1845 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001846 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 else
1848 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001849 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 buf->b_dev = st.st_dev;
1851 buf->b_ino = st.st_ino;
1852 }
1853#endif
1854 buf->b_u_synced = TRUE;
1855 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001856 if (flags & BLN_DUMMY)
1857 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 buf_clear_file(buf);
1859 clrallmarks(buf); /* clear marks */
1860 fmarks_check_names(buf); /* check file marks for this file */
1861 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1862#ifdef FEAT_AUTOCMD
1863 if (!(flags & BLN_DUMMY))
1864 {
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001865 /* Tricky: these autocommands may change the buffer list. They could
1866 * also split the window with re-using the one empty buffer. This may
1867 * result in unexpectedly losing the empty buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001869 if (!buf_valid(buf))
1870 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001874 if (!buf_valid(buf))
1875 return NULL;
1876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001878 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 return NULL;
1880# endif
1881 }
1882#endif
1883
1884 return buf;
1885}
1886
1887/*
1888 * Free the memory for the options of a buffer.
1889 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1890 * 'fileencoding'.
1891 */
1892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001893free_buf_options(
1894 buf_T *buf,
1895 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 if (free_p_ff)
1898 {
1899#ifdef FEAT_MBYTE
1900 clear_string_option(&buf->b_p_fenc);
1901#endif
1902 clear_string_option(&buf->b_p_ff);
1903#ifdef FEAT_QUICKFIX
1904 clear_string_option(&buf->b_p_bh);
1905 clear_string_option(&buf->b_p_bt);
1906#endif
1907 }
1908#ifdef FEAT_FIND_ID
1909 clear_string_option(&buf->b_p_def);
1910 clear_string_option(&buf->b_p_inc);
1911# ifdef FEAT_EVAL
1912 clear_string_option(&buf->b_p_inex);
1913# endif
1914#endif
1915#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1916 clear_string_option(&buf->b_p_inde);
1917 clear_string_option(&buf->b_p_indk);
1918#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001919#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1920 clear_string_option(&buf->b_p_bexpr);
1921#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001922#if defined(FEAT_CRYPT)
1923 clear_string_option(&buf->b_p_cm);
1924#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001925#if defined(FEAT_EVAL)
1926 clear_string_option(&buf->b_p_fex);
1927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#ifdef FEAT_CRYPT
1929 clear_string_option(&buf->b_p_key);
1930#endif
1931 clear_string_option(&buf->b_p_kp);
1932 clear_string_option(&buf->b_p_mps);
1933 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001934 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 clear_string_option(&buf->b_p_isk);
1936#ifdef FEAT_KEYMAP
1937 clear_string_option(&buf->b_p_keymap);
1938 ga_clear(&buf->b_kmap_ga);
1939#endif
1940#ifdef FEAT_COMMENTS
1941 clear_string_option(&buf->b_p_com);
1942#endif
1943#ifdef FEAT_FOLDING
1944 clear_string_option(&buf->b_p_cms);
1945#endif
1946 clear_string_option(&buf->b_p_nf);
1947#ifdef FEAT_SYN_HL
1948 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001949 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001950#endif
1951#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001952 clear_string_option(&buf->b_s.b_p_spc);
1953 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001954 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001955 buf->b_s.b_cap_prog = NULL;
1956 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957#endif
1958#ifdef FEAT_SEARCHPATH
1959 clear_string_option(&buf->b_p_sua);
1960#endif
1961#ifdef FEAT_AUTOCMD
1962 clear_string_option(&buf->b_p_ft);
1963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964#ifdef FEAT_CINDENT
1965 clear_string_option(&buf->b_p_cink);
1966 clear_string_option(&buf->b_p_cino);
1967#endif
1968#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1969 clear_string_option(&buf->b_p_cinw);
1970#endif
1971#ifdef FEAT_INS_EXPAND
1972 clear_string_option(&buf->b_p_cpt);
1973#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001974#ifdef FEAT_COMPL_FUNC
1975 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001976 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#ifdef FEAT_QUICKFIX
1979 clear_string_option(&buf->b_p_gp);
1980 clear_string_option(&buf->b_p_mp);
1981 clear_string_option(&buf->b_p_efm);
1982#endif
1983 clear_string_option(&buf->b_p_ep);
1984 clear_string_option(&buf->b_p_path);
1985 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01001986 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987#ifdef FEAT_INS_EXPAND
1988 clear_string_option(&buf->b_p_dict);
1989 clear_string_option(&buf->b_p_tsr);
1990#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001991#ifdef FEAT_TEXTOBJ
1992 clear_string_option(&buf->b_p_qe);
1993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001995 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01001996#ifdef FEAT_LISP
1997 clear_string_option(&buf->b_p_lw);
1998#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02001999 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000}
2001
2002/*
2003 * get alternate file n
2004 * set linenr to lnum or altfpos.lnum if lnum == 0
2005 * also set cursor column to altfpos.col if 'startofline' is not set.
2006 * if (options & GETF_SETMARK) call setpcmark()
2007 * if (options & GETF_ALT) we are jumping to an alternate file.
2008 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2009 *
2010 * return FAIL for failure, OK for success
2011 */
2012 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002013buflist_getfile(
2014 int n,
2015 linenr_T lnum,
2016 int options,
2017 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
2019 buf_T *buf;
2020#ifdef FEAT_WINDOWS
2021 win_T *wp = NULL;
2022#endif
2023 pos_T *fpos;
2024 colnr_T col;
2025
2026 buf = buflist_findnr(n);
2027 if (buf == NULL)
2028 {
2029 if ((options & GETF_ALT) && n == 0)
2030 EMSG(_(e_noalt));
2031 else
2032 EMSGN(_("E92: Buffer %ld not found"), n);
2033 return FAIL;
2034 }
2035
2036 /* if alternate file is the current buffer, nothing to do */
2037 if (buf == curbuf)
2038 return OK;
2039
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002040 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002041 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002042 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002044 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002045#ifdef FEAT_AUTOCMD
2046 if (curbuf_locked())
2047 return FAIL;
2048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 /* altfpos may be changed by getfile(), get it now */
2051 if (lnum == 0)
2052 {
2053 fpos = buflist_findfpos(buf);
2054 lnum = fpos->lnum;
2055 col = fpos->col;
2056 }
2057 else
2058 col = 0;
2059
2060#ifdef FEAT_WINDOWS
2061 if (options & GETF_SWITCH)
2062 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002063 /* If 'switchbuf' contains "useopen": jump to first window containing
2064 * "buf" if one exists */
2065 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002067
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002068 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002069 * page containing "buf" if one exists */
2070 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002071 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002072
2073 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2074 * current buffer isn't empty: open new tab or window */
2075 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2076 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002078 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002079 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002080 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2081 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002083 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 }
2086#endif
2087
2088 ++RedrawingDisabled;
2089 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2090 lnum, forceit) <= 0)
2091 {
2092 --RedrawingDisabled;
2093
2094 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2095 if (!p_sol && col != 0)
2096 {
2097 curwin->w_cursor.col = col;
2098 check_cursor_col();
2099#ifdef FEAT_VIRTUALEDIT
2100 curwin->w_cursor.coladd = 0;
2101#endif
2102 curwin->w_set_curswant = TRUE;
2103 }
2104 return OK;
2105 }
2106 --RedrawingDisabled;
2107 return FAIL;
2108}
2109
2110/*
2111 * go to the last know line number for the current buffer
2112 */
2113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002114buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 pos_T *fpos;
2117
2118 fpos = buflist_findfpos(curbuf);
2119
2120 curwin->w_cursor.lnum = fpos->lnum;
2121 check_cursor_lnum();
2122
2123 if (p_sol)
2124 curwin->w_cursor.col = 0;
2125 else
2126 {
2127 curwin->w_cursor.col = fpos->col;
2128 check_cursor_col();
2129#ifdef FEAT_VIRTUALEDIT
2130 curwin->w_cursor.coladd = 0;
2131#endif
2132 curwin->w_set_curswant = TRUE;
2133 }
2134}
2135
Bram Moolenaar81695252004-12-29 20:58:21 +00002136#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2137/*
2138 * Find file in buffer list by name (it has to be for the current window).
2139 * Returns NULL if not found.
2140 */
2141 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002142buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002143{
2144 char_u *ffname;
2145 buf_T *buf = NULL;
2146
2147 /* First make the name into a full path name */
2148 ffname = FullName_save(fname,
2149#ifdef UNIX
2150 TRUE /* force expansion, get rid of symbolic links */
2151#else
2152 FALSE
2153#endif
2154 );
2155 if (ffname != NULL)
2156 {
2157 buf = buflist_findname(ffname);
2158 vim_free(ffname);
2159 }
2160 return buf;
2161}
2162#endif
2163
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164/*
2165 * Find file in buffer list by name (it has to be for the current window).
2166 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002167 * Skips dummy buffers.
2168 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 */
2170 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173#ifdef UNIX
2174 struct stat st;
2175
2176 if (mch_stat((char *)ffname, &st) < 0)
2177 st.st_dev = (dev_T)-1;
2178 return buflist_findname_stat(ffname, &st);
2179}
2180
2181/*
2182 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2183 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002184 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 */
2186 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002187buflist_findname_stat(
2188 char_u *ffname,
2189 struct stat *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191#endif
2192 buf_T *buf;
2193
2194 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002195 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196#ifdef UNIX
2197 , stp
2198#endif
2199 ))
2200 return buf;
2201 return NULL;
2202}
2203
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002204#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2205 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206/*
2207 * Find file in buffer list by a regexp pattern.
2208 * Return fnum of the found buffer.
2209 * Return < 0 for error.
2210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002212buflist_findpat(
2213 char_u *pattern,
2214 char_u *pattern_end, /* pointer to first char after pattern */
2215 int unlisted, /* find unlisted buffers */
2216 int diffmode UNUSED, /* find diff-mode buffers only */
2217 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218{
2219 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 int match = -1;
2221 int find_listed;
2222 char_u *pat;
2223 char_u *patend;
2224 int attempt;
2225 char_u *p;
2226 int toggledollar;
2227
2228 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2229 {
2230 if (*pattern == '%')
2231 match = curbuf->b_fnum;
2232 else
2233 match = curwin->w_alt_fnum;
2234#ifdef FEAT_DIFF
2235 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2236 match = -1;
2237#endif
2238 }
2239
2240 /*
2241 * Try four ways of matching a listed buffer:
2242 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002243 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 * attempt == 2: with '$' at end (only match at end)
2245 * attempt == 3: with '^' at start and '$' at end (only full match)
2246 * Repeat this for finding an unlisted buffer if there was no matching
2247 * listed buffer.
2248 */
2249 else
2250 {
2251 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2252 if (pat == NULL)
2253 return -1;
2254 patend = pat + STRLEN(pat) - 1;
2255 toggledollar = (patend > pat && *patend == '$');
2256
2257 /* First try finding a listed buffer. If not found and "unlisted"
2258 * is TRUE, try finding an unlisted buffer. */
2259 find_listed = TRUE;
2260 for (;;)
2261 {
2262 for (attempt = 0; attempt <= 3; ++attempt)
2263 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002264 regmatch_T regmatch;
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 /* may add '^' and '$' */
2267 if (toggledollar)
2268 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2269 p = pat;
2270 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2271 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002272 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2273 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 vim_free(pat);
2276 return -1;
2277 }
2278
2279 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2280 if (buf->b_p_bl == find_listed
2281#ifdef FEAT_DIFF
2282 && (!diffmode || diff_mode_buf(buf))
2283#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002284 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002286 if (curtab_only)
2287 {
2288 /* Ignore the match if the buffer is not open in
2289 * the current tab. */
2290#ifdef FEAT_WINDOWS
2291 win_T *wp;
2292
2293 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2294 if (wp->w_buffer == buf)
2295 break;
2296 if (wp == NULL)
2297 continue;
2298#else
2299 if (curwin->w_buffer != buf)
2300 continue;
2301#endif
2302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (match >= 0) /* already found a match */
2304 {
2305 match = -2;
2306 break;
2307 }
2308 match = buf->b_fnum; /* remember first match */
2309 }
2310
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002311 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (match >= 0) /* found one match */
2313 break;
2314 }
2315
2316 /* Only search for unlisted buffers if there was no match with
2317 * a listed buffer. */
2318 if (!unlisted || !find_listed || match != -1)
2319 break;
2320 find_listed = FALSE;
2321 }
2322
2323 vim_free(pat);
2324 }
2325
2326 if (match == -2)
2327 EMSG2(_("E93: More than one match for %s"), pattern);
2328 else if (match < 0)
2329 EMSG2(_("E94: No matching buffer for %s"), pattern);
2330 return match;
2331}
2332#endif
2333
2334#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2335
2336/*
2337 * Find all buffer names that match.
2338 * For command line expansion of ":buf" and ":sbuf".
2339 * Return OK if matches found, FAIL otherwise.
2340 */
2341 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002342ExpandBufnames(
2343 char_u *pat,
2344 int *num_file,
2345 char_u ***file,
2346 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
2348 int count = 0;
2349 buf_T *buf;
2350 int round;
2351 char_u *p;
2352 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002353 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 *num_file = 0; /* return values in case of FAIL */
2356 *file = NULL;
2357
Bram Moolenaar05159a02005-02-26 23:04:13 +00002358 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2359 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002361 patc = alloc((unsigned)STRLEN(pat) + 11);
2362 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002364 STRCPY(patc, "\\(^\\|[\\/]\\)");
2365 STRCPY(patc + 11, pat + 1);
2366 }
2367 else
2368 patc = pat;
2369
2370 /*
2371 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002372 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002373 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002374 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002375 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002376 regmatch_T regmatch;
2377
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002378 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002379 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002380 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2381 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002382 {
2383 if (patc != pat)
2384 vim_free(patc);
2385 return FAIL;
2386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 /*
2389 * round == 1: Count the matches.
2390 * round == 2: Build the array to keep the matches.
2391 */
2392 for (round = 1; round <= 2; ++round)
2393 {
2394 count = 0;
2395 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2396 {
2397 if (!buf->b_p_bl) /* skip unlisted buffers */
2398 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002399 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (p != NULL)
2401 {
2402 if (round == 1)
2403 ++count;
2404 else
2405 {
2406 if (options & WILD_HOME_REPLACE)
2407 p = home_replace_save(buf, p);
2408 else
2409 p = vim_strsave(p);
2410 (*file)[count++] = p;
2411 }
2412 }
2413 }
2414 if (count == 0) /* no match found, break here */
2415 break;
2416 if (round == 1)
2417 {
2418 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2419 if (*file == NULL)
2420 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002421 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002422 if (patc != pat)
2423 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 return FAIL;
2425 }
2426 }
2427 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002428 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (count) /* match(es) found, break here */
2430 break;
2431 }
2432
Bram Moolenaar05159a02005-02-26 23:04:13 +00002433 if (patc != pat)
2434 vim_free(patc);
2435
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 *num_file = count;
2437 return (count == 0 ? FAIL : OK);
2438}
2439
2440#endif /* FEAT_CMDL_COMPL */
2441
2442#ifdef HAVE_BUFLIST_MATCH
2443/*
2444 * Check for a match on the file name for buffer "buf" with regprog "prog".
2445 */
2446 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002447buflist_match(
2448 regmatch_T *rmp,
2449 buf_T *buf,
2450 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 char_u *match;
2453
2454 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002455 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002457 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 return match;
2460}
2461
2462/*
2463 * Try matching the regexp in "prog" with file name "name".
2464 * Return "name" when there is a match, NULL when not.
2465 */
2466 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002467fname_match(
2468 regmatch_T *rmp,
2469 char_u *name,
2470 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 char_u *match = NULL;
2473 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 if (name != NULL)
2476 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002477 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002478 rmp->rm_ic = p_fic || ignore_case;
2479 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 match = name;
2481 else
2482 {
2483 /* Replace $(HOME) with '~' and try matching again. */
2484 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002485 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 match = name;
2487 vim_free(p);
2488 }
2489 }
2490
2491 return match;
2492}
2493#endif
2494
2495/*
2496 * find file in buffer list by number
2497 */
2498 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002499buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 buf_T *buf;
2502
2503 if (nr == 0)
2504 nr = curwin->w_alt_fnum;
2505 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2506 if (buf->b_fnum == nr)
2507 return (buf);
2508 return NULL;
2509}
2510
2511/*
2512 * Get name of file 'n' in the buffer list.
2513 * When the file has no name an empty string is returned.
2514 * home_replace() is used to shorten the file name (used for marks).
2515 * Returns a pointer to allocated memory, of NULL when failed.
2516 */
2517 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002518buflist_nr2name(
2519 int n,
2520 int fullname,
2521 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523 buf_T *buf;
2524
2525 buf = buflist_findnr(n);
2526 if (buf == NULL)
2527 return NULL;
2528 return home_replace_save(helptail ? buf : NULL,
2529 fullname ? buf->b_ffname : buf->b_fname);
2530}
2531
2532/*
2533 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2534 * When "copy_options" is TRUE save the local window option values.
2535 * When "lnum" is 0 only do the options.
2536 */
2537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002538buflist_setfpos(
2539 buf_T *buf,
2540 win_T *win,
2541 linenr_T lnum,
2542 colnr_T col,
2543 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 wininfo_T *wip;
2546
2547 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2548 if (wip->wi_win == win)
2549 break;
2550 if (wip == NULL)
2551 {
2552 /* allocate a new entry */
2553 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2554 if (wip == NULL)
2555 return;
2556 wip->wi_win = win;
2557 if (lnum == 0) /* set lnum even when it's 0 */
2558 lnum = 1;
2559 }
2560 else
2561 {
2562 /* remove the entry from the list */
2563 if (wip->wi_prev)
2564 wip->wi_prev->wi_next = wip->wi_next;
2565 else
2566 buf->b_wininfo = wip->wi_next;
2567 if (wip->wi_next)
2568 wip->wi_next->wi_prev = wip->wi_prev;
2569 if (copy_options && wip->wi_optset)
2570 {
2571 clear_winopt(&wip->wi_opt);
2572#ifdef FEAT_FOLDING
2573 deleteFoldRecurse(&wip->wi_folds);
2574#endif
2575 }
2576 }
2577 if (lnum != 0)
2578 {
2579 wip->wi_fpos.lnum = lnum;
2580 wip->wi_fpos.col = col;
2581 }
2582 if (copy_options)
2583 {
2584 /* Save the window-specific option values. */
2585 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2586#ifdef FEAT_FOLDING
2587 wip->wi_fold_manual = win->w_fold_manual;
2588 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2589#endif
2590 wip->wi_optset = TRUE;
2591 }
2592
2593 /* insert the entry in front of the list */
2594 wip->wi_next = buf->b_wininfo;
2595 buf->b_wininfo = wip;
2596 wip->wi_prev = NULL;
2597 if (wip->wi_next)
2598 wip->wi_next->wi_prev = wip;
2599
2600 return;
2601}
2602
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002603#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002604static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002605
2606/*
2607 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2608 * page. That's because a diff is local to a tab page.
2609 */
2610 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002611wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002612{
2613 win_T *wp;
2614
2615 if (wip->wi_opt.wo_diff)
2616 {
2617 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2618 /* return FALSE when it's a window in the current tab page, thus
2619 * the buffer was in diff mode here */
2620 if (wip->wi_win == wp)
2621 return FALSE;
2622 return TRUE;
2623 }
2624 return FALSE;
2625}
2626#endif
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628/*
2629 * Find info for the current window in buffer "buf".
2630 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002631 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2632 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 * Returns NULL when there isn't any info.
2634 */
2635 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002636find_wininfo(
2637 buf_T *buf,
2638 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
2640 wininfo_T *wip;
2641
2642 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002643 if (wip->wi_win == curwin
2644#ifdef FEAT_DIFF
2645 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2646#endif
2647 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002649
2650 /* If no wininfo for curwin, use the first in the list (that doesn't have
2651 * 'diff' set and is in another tab page). */
2652 if (wip == NULL)
2653 {
2654#ifdef FEAT_DIFF
2655 if (skip_diff_buffer)
2656 {
2657 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2658 if (!wininfo_other_tab_diff(wip))
2659 break;
2660 }
2661 else
2662#endif
2663 wip = buf->b_wininfo;
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 return wip;
2666}
2667
2668/*
2669 * Reset the local window options to the values last used in this window.
2670 * If the buffer wasn't used in this window before, use the values from
2671 * the most recently used window. If the values were never set, use the
2672 * global values for the window.
2673 */
2674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002675get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
2677 wininfo_T *wip;
2678
2679 clear_winopt(&curwin->w_onebuf_opt);
2680#ifdef FEAT_FOLDING
2681 clearFolding(curwin);
2682#endif
2683
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002684 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (wip != NULL && wip->wi_optset)
2686 {
2687 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2688#ifdef FEAT_FOLDING
2689 curwin->w_fold_manual = wip->wi_fold_manual;
2690 curwin->w_foldinvalid = TRUE;
2691 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2692#endif
2693 }
2694 else
2695 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2696
2697#ifdef FEAT_FOLDING
2698 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2699 if (p_fdls >= 0)
2700 curwin->w_p_fdl = p_fdls;
2701#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002702#ifdef FEAT_SYN_HL
2703 check_colorcolumn(curwin);
2704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705}
2706
2707/*
2708 * Find the position (lnum and col) for the buffer 'buf' for the current
2709 * window.
2710 * Returns a pointer to no_position if no position is found.
2711 */
2712 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002713buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
2715 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002716 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002718 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (wip != NULL)
2720 return &(wip->wi_fpos);
2721 else
2722 return &no_position;
2723}
2724
2725/*
2726 * Find the lnum for the buffer 'buf' for the current window.
2727 */
2728 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002729buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
2731 return buflist_findfpos(buf)->lnum;
2732}
2733
2734#if defined(FEAT_LISTCMDS) || defined(PROTO)
2735/*
2736 * List all know file names (for :files and :buffers command).
2737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002739buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740{
2741 buf_T *buf;
2742 int len;
2743 int i;
2744
2745 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2746 {
2747 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002748 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2749 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2750 || (vim_strchr(eap->arg, '+')
2751 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2752 || (vim_strchr(eap->arg, 'a')
2753 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2754 || (vim_strchr(eap->arg, 'h')
2755 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2756 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2757 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2758 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2759 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2760 || (vim_strchr(eap->arg, '#')
2761 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 continue;
2763 msg_putchar('\n');
2764 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002765 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else
2767 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2768
Bram Moolenaar50cde822005-06-05 21:54:54 +00002769 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 buf->b_fnum,
2771 buf->b_p_bl ? ' ' : 'u',
2772 buf == curbuf ? '%' :
2773 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2774 buf->b_ml.ml_mfp == NULL ? ' ' :
2775 (buf->b_nwindows == 0 ? 'h' : 'a'),
2776 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2777 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002778 : (bufIsChanged(buf) ? '+' : ' '),
2779 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002780 if (len > IOSIZE - 20)
2781 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 i = 40 - vim_strsize(IObuff);
2785 do
2786 {
2787 IObuff[len++] = ' ';
2788 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002789 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2790 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002791 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 msg_outtrans(IObuff);
2793 out_flush(); /* output one line at a time */
2794 ui_breakcheck();
2795 }
2796}
2797#endif
2798
2799/*
2800 * Get file name and line number for file 'fnum'.
2801 * Used by DoOneCmd() for translating '%' and '#'.
2802 * Used by insert_reg() and cmdline_paste() for '#' register.
2803 * Return FAIL if not found, OK for success.
2804 */
2805 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002806buflist_name_nr(
2807 int fnum,
2808 char_u **fname,
2809 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
2811 buf_T *buf;
2812
2813 buf = buflist_findnr(fnum);
2814 if (buf == NULL || buf->b_fname == NULL)
2815 return FAIL;
2816
2817 *fname = buf->b_fname;
2818 *lnum = buflist_findlnum(buf);
2819
2820 return OK;
2821}
2822
2823/*
2824 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2825 * The file name with the full path is also remembered, for when :cd is used.
2826 * Returns FAIL for failure (file name already in use by other buffer)
2827 * OK otherwise.
2828 */
2829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002830setfname(
2831 buf_T *buf,
2832 char_u *ffname,
2833 char_u *sfname,
2834 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
Bram Moolenaar81695252004-12-29 20:58:21 +00002836 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837#ifdef UNIX
2838 struct stat st;
2839#endif
2840
2841 if (ffname == NULL || *ffname == NUL)
2842 {
2843 /* Removing the name. */
2844 vim_free(buf->b_ffname);
2845 vim_free(buf->b_sfname);
2846 buf->b_ffname = NULL;
2847 buf->b_sfname = NULL;
2848#ifdef UNIX
2849 st.st_dev = (dev_T)-1;
2850#endif
2851 }
2852 else
2853 {
2854 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2855 if (ffname == NULL) /* out of memory */
2856 return FAIL;
2857
2858 /*
2859 * if the file name is already used in another buffer:
2860 * - if the buffer is loaded, fail
2861 * - if the buffer is not loaded, delete it from the list
2862 */
2863#ifdef UNIX
2864 if (mch_stat((char *)ffname, &st) < 0)
2865 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002866#endif
2867 if (!(buf->b_flags & BF_DUMMY))
2868#ifdef UNIX
2869 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002871 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872#endif
2873 if (obuf != NULL && obuf != buf)
2874 {
2875 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2876 {
2877 if (message)
2878 EMSG(_("E95: Buffer with this name already exists"));
2879 vim_free(ffname);
2880 return FAIL;
2881 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002882 /* delete from the list */
2883 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885 sfname = vim_strsave(sfname);
2886 if (ffname == NULL || sfname == NULL)
2887 {
2888 vim_free(sfname);
2889 vim_free(ffname);
2890 return FAIL;
2891 }
2892#ifdef USE_FNAME_CASE
2893# ifdef USE_LONG_FNAME
2894 if (USE_LONG_FNAME)
2895# endif
2896 fname_case(sfname, 0); /* set correct case for short file name */
2897#endif
2898 vim_free(buf->b_ffname);
2899 vim_free(buf->b_sfname);
2900 buf->b_ffname = ffname;
2901 buf->b_sfname = sfname;
2902 }
2903 buf->b_fname = buf->b_sfname;
2904#ifdef UNIX
2905 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002906 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 else
2908 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002909 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 buf->b_dev = st.st_dev;
2911 buf->b_ino = st.st_ino;
2912 }
2913#endif
2914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
2917 buf_name_changed(buf);
2918 return OK;
2919}
2920
2921/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002922 * Crude way of changing the name of a buffer. Use with care!
2923 * The name should be relative to the current directory.
2924 */
2925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002927{
2928 buf_T *buf;
2929
2930 buf = buflist_findnr(fnum);
2931 if (buf != NULL)
2932 {
2933 vim_free(buf->b_sfname);
2934 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002935 buf->b_ffname = vim_strsave(name);
2936 buf->b_sfname = NULL;
2937 /* Allocate ffname and expand into full path. Also resolves .lnk
2938 * files on Win32. */
2939 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002940 buf->b_fname = buf->b_sfname;
2941 }
2942}
2943
2944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 * Take care of what needs to be done when the name of buffer "buf" has
2946 * changed.
2947 */
2948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002949buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 /*
2952 * If the file name changed, also change the name of the swapfile
2953 */
2954 if (buf->b_ml.ml_mfp != NULL)
2955 ml_setname(buf);
2956
2957 if (curwin->w_buffer == buf)
2958 check_arg_idx(curwin); /* check file name for arg list */
2959#ifdef FEAT_TITLE
2960 maketitle(); /* set window title */
2961#endif
2962#ifdef FEAT_WINDOWS
2963 status_redraw_all(); /* status lines need to be redrawn */
2964#endif
2965 fmarks_check_names(buf); /* check named file marks */
2966 ml_timestamp(buf); /* reset timestamp */
2967}
2968
2969/*
2970 * set alternate file name for current window
2971 *
2972 * Used by do_one_cmd(), do_write() and do_ecmd().
2973 * Return the buffer.
2974 */
2975 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002976setaltfname(
2977 char_u *ffname,
2978 char_u *sfname,
2979 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 buf_T *buf;
2982
2983 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2984 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002985 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 curwin->w_alt_fnum = buf->b_fnum;
2987 return buf;
2988}
2989
2990/*
2991 * Get alternate file name for current window.
2992 * Return NULL if there isn't any, and give error message if requested.
2993 */
2994 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002995getaltfname(
2996 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 char_u *fname;
2999 linenr_T dummy;
3000
3001 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3002 {
3003 if (errmsg)
3004 EMSG(_(e_noalt));
3005 return NULL;
3006 }
3007 return fname;
3008}
3009
3010/*
3011 * Add a file name to the buflist and return its number.
3012 * Uses same flags as buflist_new(), except BLN_DUMMY.
3013 *
3014 * used by qf_init(), main() and doarglist()
3015 */
3016 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003017buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 buf_T *buf;
3020
3021 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3022 if (buf != NULL)
3023 return buf->b_fnum;
3024 return 0;
3025}
3026
3027#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3028/*
3029 * Adjust slashes in file names. Called after 'shellslash' was set.
3030 */
3031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003032buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 buf_T *bp;
3035
3036 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3037 {
3038 if (bp->b_ffname != NULL)
3039 slash_adjust(bp->b_ffname);
3040 if (bp->b_sfname != NULL)
3041 slash_adjust(bp->b_sfname);
3042 }
3043}
3044#endif
3045
3046/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003047 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 * Also save the local window option values.
3049 */
3050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003051buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003053 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054}
3055
3056/*
3057 * Return TRUE if 'ffname' is not the same file as current file.
3058 * Fname must have a full path (expanded by mch_FullName()).
3059 */
3060 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003061otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 return otherfile_buf(curbuf, ffname
3064#ifdef UNIX
3065 , NULL
3066#endif
3067 );
3068}
3069
3070 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003071otherfile_buf(
3072 buf_T *buf,
3073 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074#ifdef UNIX
Bram Moolenaar7454a062016-01-30 15:14:10 +01003075 , struct stat *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003077 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 /* no name is different */
3080 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3081 return TRUE;
3082 if (fnamecmp(ffname, buf->b_ffname) == 0)
3083 return FALSE;
3084#ifdef UNIX
3085 {
3086 struct stat st;
3087
3088 /* If no struct stat given, get it now */
3089 if (stp == NULL)
3090 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003091 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 st.st_dev = (dev_T)-1;
3093 stp = &st;
3094 }
3095 /* Use dev/ino to check if the files are the same, even when the names
3096 * are different (possible with links). Still need to compare the
3097 * name above, for when the file doesn't exist yet.
3098 * Problem: The dev/ino changes when a file is deleted (and created
3099 * again) and remains the same when renamed/moved. We don't want to
3100 * mch_stat() each buffer each time, that would be too slow. Get the
3101 * dev/ino again when they appear to match, but not when they appear
3102 * to be different: Could skip a buffer when it's actually the same
3103 * file. */
3104 if (buf_same_ino(buf, stp))
3105 {
3106 buf_setino(buf);
3107 if (buf_same_ino(buf, stp))
3108 return FALSE;
3109 }
3110 }
3111#endif
3112 return TRUE;
3113}
3114
3115#if defined(UNIX) || defined(PROTO)
3116/*
3117 * Set inode and device number for a buffer.
3118 * Must always be called when b_fname is changed!.
3119 */
3120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003121buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 struct stat st;
3124
3125 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3126 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003127 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 buf->b_dev = st.st_dev;
3129 buf->b_ino = st.st_ino;
3130 }
3131 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003132 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134
3135/*
3136 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3137 */
3138 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003139buf_same_ino(
3140 buf_T *buf,
3141 struct stat *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003143 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 && stp->st_dev == buf->b_dev
3145 && stp->st_ino == buf->b_ino);
3146}
3147#endif
3148
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003149/*
3150 * Print info about the current buffer.
3151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003153fileinfo(
3154 int fullname, /* when non-zero print full path */
3155 int shorthelp,
3156 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 char_u *name;
3159 int n;
3160 char_u *p;
3161 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003162 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 buffer = alloc(IOSIZE);
3165 if (buffer == NULL)
3166 return;
3167
3168 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3169 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003170 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 p = buffer + STRLEN(buffer);
3172 }
3173 else
3174 p = buffer;
3175
3176 *p++ = '"';
3177 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003178 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 else
3180 {
3181 if (!fullname && curbuf->b_fname != NULL)
3182 name = curbuf->b_fname;
3183 else
3184 name = curbuf->b_ffname;
3185 home_replace(shorthelp ? curbuf : NULL, name, p,
3186 (int)(IOSIZE - (p - buffer)), TRUE);
3187 }
3188
Bram Moolenaara800b422010-06-27 01:15:55 +02003189 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 curbufIsChanged() ? (shortmess(SHM_MOD)
3191 ? " [+]" : _(" [Modified]")) : " ",
3192 (curbuf->b_flags & BF_NOTEDITED)
3193#ifdef FEAT_QUICKFIX
3194 && !bt_dontwrite(curbuf)
3195#endif
3196 ? _("[Not edited]") : "",
3197 (curbuf->b_flags & BF_NEW)
3198#ifdef FEAT_QUICKFIX
3199 && !bt_dontwrite(curbuf)
3200#endif
3201 ? _("[New file]") : "",
3202 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003203 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 : _("[readonly]")) : "",
3205 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3206 || curbuf->b_p_ro) ?
3207 " " : "");
3208 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3209 * causes an overflow, thus for large numbers divide instead. */
3210 if (curwin->w_cursor.lnum > 1000000L)
3211 n = (int)(((long)curwin->w_cursor.lnum) /
3212 ((long)curbuf->b_ml.ml_line_count / 100L));
3213 else
3214 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3215 (long)curbuf->b_ml.ml_line_count);
3216 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3217 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003218 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
3220#ifdef FEAT_CMDL_INFO
3221 else if (p_ru)
3222 {
3223 /* Current line and column are already on the screen -- webb */
3224 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003225 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003227 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 (long)curbuf->b_ml.ml_line_count, n);
3229 }
3230#endif
3231 else
3232 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003233 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 _("line %ld of %ld --%d%%-- col "),
3235 (long)curwin->w_cursor.lnum,
3236 (long)curbuf->b_ml.ml_line_count,
3237 n);
3238 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003239 len = STRLEN(buffer);
3240 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3242 }
3243
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003244 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
3246 if (dont_truncate)
3247 {
3248 /* Temporarily set msg_scroll to avoid the message being truncated.
3249 * First call msg_start() to get the message in the right place. */
3250 msg_start();
3251 n = msg_scroll;
3252 msg_scroll = TRUE;
3253 msg(buffer);
3254 msg_scroll = n;
3255 }
3256 else
3257 {
3258 p = msg_trunc_attr(buffer, FALSE, 0);
3259 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 /* Need to repeat the message after redrawing when:
3261 * - When restart_edit is set (otherwise there will be a delay
3262 * before redrawing).
3263 * - When the screen was scrolled but there is no wait-return
3264 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003265 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267
3268 vim_free(buffer);
3269}
3270
3271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003272col_print(
3273 char_u *buf,
3274 size_t buflen,
3275 int col,
3276 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003279 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003281 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282}
3283
3284#if defined(FEAT_TITLE) || defined(PROTO)
3285/*
3286 * put file name in title bar of window and in icon title
3287 */
3288
3289static char_u *lasttitle = NULL;
3290static char_u *lasticon = NULL;
3291
3292 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 char_u *p;
3296 char_u *t_str = NULL;
3297 char_u *i_name;
3298 char_u *i_str = NULL;
3299 int maxlen = 0;
3300 int len;
3301 int mustset;
3302 char_u buf[IOSIZE];
3303 int off;
3304
3305 if (!redrawing())
3306 {
3307 /* Postpone updating the title when 'lazyredraw' is set. */
3308 need_maketitle = TRUE;
3309 return;
3310 }
3311
3312 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003313 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 return;
3315
3316 if (p_title)
3317 {
3318 if (p_titlelen > 0)
3319 {
3320 maxlen = p_titlelen * Columns / 100;
3321 if (maxlen < 10)
3322 maxlen = 10;
3323 }
3324
3325 t_str = buf;
3326 if (*p_titlestring != NUL)
3327 {
3328#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003329 if (stl_syntax & STL_IN_TITLE)
3330 {
3331 int use_sandbox = FALSE;
3332 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003333
3334# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003335 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003336# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003337 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003339 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003340 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003341 if (called_emsg)
3342 set_string_option_direct((char_u *)"titlestring", -1,
3343 (char_u *)"", OPT_FREE, SID_ERROR);
3344 called_emsg |= save_called_emsg;
3345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 else
3347#endif
3348 t_str = p_titlestring;
3349 }
3350 else
3351 {
3352 /* format: "fname + (path) (1 of 2) - VIM" */
3353
Bram Moolenaar2c666692012-09-05 13:30:40 +02003354#define SPACE_FOR_FNAME (IOSIZE - 100)
3355#define SPACE_FOR_DIR (IOSIZE - 20)
3356#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003358 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
3360 {
3361 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003362 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
3365
3366 switch (bufIsChanged(curbuf)
3367 + (curbuf->b_p_ro * 2)
3368 + (!curbuf->b_p_ma * 4))
3369 {
3370 case 1: STRCAT(buf, " +"); break;
3371 case 2: STRCAT(buf, " ="); break;
3372 case 3: STRCAT(buf, " =+"); break;
3373 case 4:
3374 case 6: STRCAT(buf, " -"); break;
3375 case 5:
3376 case 7: STRCAT(buf, " -+"); break;
3377 }
3378
3379 if (curbuf->b_fname != NULL)
3380 {
3381 /* Get path of file, replace home dir with ~ */
3382 off = (int)STRLEN(buf);
3383 buf[off++] = ' ';
3384 buf[off++] = '(';
3385 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003386 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#ifdef BACKSLASH_IN_FILENAME
3388 /* avoid "c:/name" to be reduced to "c" */
3389 if (isalpha(buf[off]) && buf[off + 1] == ':')
3390 off += 2;
3391#endif
3392 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003393 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003396 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003397 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003400
Bram Moolenaar2c666692012-09-05 13:30:40 +02003401 /* Translate unprintable chars and concatenate. Keep some
3402 * room for the server name. When there is no room (very long
3403 * file name) use (...). */
3404 if (off < SPACE_FOR_DIR)
3405 {
3406 p = transstr(buf + off);
3407 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3408 vim_free(p);
3409 }
3410 else
3411 {
3412 vim_strncpy(buf + off, (char_u *)"...",
3413 (size_t)(SPACE_FOR_ARGNR - off));
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 STRCAT(buf, ")");
3416 }
3417
Bram Moolenaar2c666692012-09-05 13:30:40 +02003418 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420#if defined(FEAT_CLIENTSERVER)
3421 if (serverName != NULL)
3422 {
3423 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003424 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426 else
3427#endif
3428 STRCAT(buf, " - VIM");
3429
3430 if (maxlen > 0)
3431 {
3432 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003433 if (vim_strsize(buf) > maxlen)
3434 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436 }
3437 }
3438 mustset = ti_change(t_str, &lasttitle);
3439
3440 if (p_icon)
3441 {
3442 i_str = buf;
3443 if (*p_iconstring != NUL)
3444 {
3445#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003446 if (stl_syntax & STL_IN_ICON)
3447 {
3448 int use_sandbox = FALSE;
3449 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003450
3451# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003452 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003453# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003454 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003456 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003457 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003458 if (called_emsg)
3459 set_string_option_direct((char_u *)"iconstring", -1,
3460 (char_u *)"", OPT_FREE, SID_ERROR);
3461 called_emsg |= save_called_emsg;
3462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 else
3464#endif
3465 i_str = p_iconstring;
3466 }
3467 else
3468 {
3469 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003470 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 else /* use file name only in icon */
3472 i_name = gettail(curbuf->b_ffname);
3473 *i_str = NUL;
3474 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003475 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (len > 100)
3477 {
3478 len -= 100;
3479#ifdef FEAT_MBYTE
3480 if (has_mbyte)
3481 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3482#endif
3483 i_name += len;
3484 }
3485 STRCPY(i_str, i_name);
3486 trans_characters(i_str, IOSIZE);
3487 }
3488 }
3489
3490 mustset |= ti_change(i_str, &lasticon);
3491
3492 if (mustset)
3493 resettitle();
3494}
3495
3496/*
3497 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3498 * from "str" if it does.
3499 * Return TRUE when "*last" changed.
3500 */
3501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003502ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
3504 if ((str == NULL) != (*last == NULL)
3505 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3506 {
3507 vim_free(*last);
3508 if (str == NULL)
3509 *last = NULL;
3510 else
3511 *last = vim_strsave(str);
3512 return TRUE;
3513 }
3514 return FALSE;
3515}
3516
3517/*
3518 * Put current window title back (used after calling a shell)
3519 */
3520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003521resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
3523 mch_settitle(lasttitle, lasticon);
3524}
Bram Moolenaarea408852005-06-25 22:49:46 +00003525
3526# if defined(EXITFREE) || defined(PROTO)
3527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003528free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003529{
3530 vim_free(lasttitle);
3531 vim_free(lasticon);
3532}
3533# endif
3534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#endif /* FEAT_TITLE */
3536
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003537#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003539 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 * Return length of string in screen cells.
3541 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003542 * Normally works for window "wp", except when working for 'tabline' then it
3543 * is "curwin".
3544 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 * Items are drawn interspersed with the text that surrounds it
3546 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3547 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3548 *
3549 * If maxwidth is not zero, the string will be filled at any middle marker
3550 * or truncated if too long, fillchar is used for all whitespace.
3551 */
3552 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553build_stl_str_hl(
3554 win_T *wp,
3555 char_u *out, /* buffer to write into != NameBuff */
3556 size_t outlen, /* length of out[] */
3557 char_u *fmt,
3558 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3559 int fillchar,
3560 int maxwidth,
3561 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3562 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
3564 char_u *p;
3565 char_u *s;
3566 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003567 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568#ifdef FEAT_EVAL
3569 win_T *o_curwin;
3570 buf_T *o_curbuf;
3571#endif
3572 int empty_line;
3573 colnr_T virtcol;
3574 long l;
3575 long n;
3576 int prevchar_isflag;
3577 int prevchar_isitem;
3578 int itemisflag;
3579 int fillable;
3580 char_u *str;
3581 long num;
3582 int width;
3583 int itemcnt;
3584 int curitem;
3585 int groupitem[STL_MAX_ITEM];
3586 int groupdepth;
3587 struct stl_item
3588 {
3589 char_u *start;
3590 int minwid;
3591 int maxwid;
3592 enum
3593 {
3594 Normal,
3595 Empty,
3596 Group,
3597 Middle,
3598 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003599 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 Trunc
3601 } type;
3602 } item[STL_MAX_ITEM];
3603 int minwid;
3604 int maxwid;
3605 int zeropad;
3606 char_u base;
3607 char_u opt;
3608#define TMPLEN 70
3609 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003610 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003611 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003612
3613#ifdef FEAT_EVAL
3614 /*
3615 * When the format starts with "%!" then evaluate it as an expression and
3616 * use the result as the actual format string.
3617 */
3618 if (fmt[0] == '%' && fmt[1] == '!')
3619 {
3620 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3621 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003622 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003623 }
3624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
3626 if (fillchar == 0)
3627 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003628#ifdef FEAT_MBYTE
3629 /* Can't handle a multi-byte fill character yet. */
3630 else if (mb_char2len(fillchar) > 1)
3631 fillchar = '-';
3632#endif
3633
Bram Moolenaar567199b2013-04-24 16:52:36 +02003634 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3635 * p will become invalid when getting another buffer line. */
3636 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3637 empty_line = (*p == NUL);
3638
3639 /* Get the byte value now, in case we need it below. This is more
3640 * efficient than making a copy of the line. */
3641 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3642 byteval = 0;
3643 else
3644#ifdef FEAT_MBYTE
3645 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3646#else
3647 byteval = p[wp->w_cursor.col];
3648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 groupdepth = 0;
3651 p = out;
3652 curitem = 0;
3653 prevchar_isflag = TRUE;
3654 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003655 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003657 if (curitem == STL_MAX_ITEM)
3658 {
3659 /* There are too many items. Add the error code to the statusline
3660 * to give the user a hint about what went wrong. */
3661 if (p + 6 < out + outlen)
3662 {
3663 mch_memmove(p, " E541", (size_t)5);
3664 p += 5;
3665 }
3666 break;
3667 }
3668
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (*s != NUL && *s != '%')
3670 prevchar_isflag = prevchar_isitem = FALSE;
3671
3672 /*
3673 * Handle up to the next '%' or the end.
3674 */
3675 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3676 *p++ = *s++;
3677 if (*s == NUL || p + 1 >= out + outlen)
3678 break;
3679
3680 /*
3681 * Handle one '%' item.
3682 */
3683 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003684 if (*s == NUL) /* ignore trailing % */
3685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 if (*s == '%')
3687 {
3688 if (p + 1 >= out + outlen)
3689 break;
3690 *p++ = *s++;
3691 prevchar_isflag = prevchar_isitem = FALSE;
3692 continue;
3693 }
3694 if (*s == STL_MIDDLEMARK)
3695 {
3696 s++;
3697 if (groupdepth > 0)
3698 continue;
3699 item[curitem].type = Middle;
3700 item[curitem++].start = p;
3701 continue;
3702 }
3703 if (*s == STL_TRUNCMARK)
3704 {
3705 s++;
3706 item[curitem].type = Trunc;
3707 item[curitem++].start = p;
3708 continue;
3709 }
3710 if (*s == ')')
3711 {
3712 s++;
3713 if (groupdepth < 1)
3714 continue;
3715 groupdepth--;
3716
3717 t = item[groupitem[groupdepth]].start;
3718 *p = NUL;
3719 l = vim_strsize(t);
3720 if (curitem > groupitem[groupdepth] + 1
3721 && item[groupitem[groupdepth]].minwid == 0)
3722 {
3723 /* remove group if all items are empty */
3724 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003725 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 break;
3727 if (n == curitem)
3728 {
3729 p = t;
3730 l = 0;
3731 }
3732 }
3733 if (l > item[groupitem[groupdepth]].maxwid)
3734 {
3735 /* truncate, remove n bytes of text at the start */
3736#ifdef FEAT_MBYTE
3737 if (has_mbyte)
3738 {
3739 /* Find the first character that should be included. */
3740 n = 0;
3741 while (l >= item[groupitem[groupdepth]].maxwid)
3742 {
3743 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003744 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746 }
3747 else
3748#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003749 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
3751 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003752 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 p = p - n + 1;
3754#ifdef FEAT_MBYTE
3755 /* Fill up space left over by half a double-wide char. */
3756 while (++l < item[groupitem[groupdepth]].minwid)
3757 *p++ = fillchar;
3758#endif
3759
3760 /* correct the start of the items for the truncation */
3761 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3762 {
3763 item[l].start -= n;
3764 if (item[l].start < t)
3765 item[l].start = t;
3766 }
3767 }
3768 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3769 {
3770 /* fill */
3771 n = item[groupitem[groupdepth]].minwid;
3772 if (n < 0)
3773 {
3774 /* fill by appending characters */
3775 n = 0 - n;
3776 while (l++ < n && p + 1 < out + outlen)
3777 *p++ = fillchar;
3778 }
3779 else
3780 {
3781 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003782 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 l = n - l;
3784 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003785 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 p += l;
3787 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3788 item[n].start += l;
3789 for ( ; l > 0; l--)
3790 *t++ = fillchar;
3791 }
3792 }
3793 continue;
3794 }
3795 minwid = 0;
3796 maxwid = 9999;
3797 zeropad = FALSE;
3798 l = 1;
3799 if (*s == '0')
3800 {
3801 s++;
3802 zeropad = TRUE;
3803 }
3804 if (*s == '-')
3805 {
3806 s++;
3807 l = -1;
3808 }
3809 if (VIM_ISDIGIT(*s))
3810 {
3811 minwid = (int)getdigits(&s);
3812 if (minwid < 0) /* overflow */
3813 minwid = 0;
3814 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003815 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 {
3817 item[curitem].type = Highlight;
3818 item[curitem].start = p;
3819 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3820 s++;
3821 curitem++;
3822 continue;
3823 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003824 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3825 {
3826 if (*s == STL_TABCLOSENR)
3827 {
3828 if (minwid == 0)
3829 {
3830 /* %X ends the close label, go back to the previously
3831 * define tab label nr. */
3832 for (n = curitem - 1; n >= 0; --n)
3833 if (item[n].type == TabPage && item[n].minwid >= 0)
3834 {
3835 minwid = item[n].minwid;
3836 break;
3837 }
3838 }
3839 else
3840 /* close nrs are stored as negative values */
3841 minwid = - minwid;
3842 }
3843 item[curitem].type = TabPage;
3844 item[curitem].start = p;
3845 item[curitem].minwid = minwid;
3846 s++;
3847 curitem++;
3848 continue;
3849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (*s == '.')
3851 {
3852 s++;
3853 if (VIM_ISDIGIT(*s))
3854 {
3855 maxwid = (int)getdigits(&s);
3856 if (maxwid <= 0) /* overflow */
3857 maxwid = 50;
3858 }
3859 }
3860 minwid = (minwid > 50 ? 50 : minwid) * l;
3861 if (*s == '(')
3862 {
3863 groupitem[groupdepth++] = curitem;
3864 item[curitem].type = Group;
3865 item[curitem].start = p;
3866 item[curitem].minwid = minwid;
3867 item[curitem].maxwid = maxwid;
3868 s++;
3869 curitem++;
3870 continue;
3871 }
3872 if (vim_strchr(STL_ALL, *s) == NULL)
3873 {
3874 s++;
3875 continue;
3876 }
3877 opt = *s++;
3878
3879 /* OK - now for the real work */
3880 base = 'D';
3881 itemisflag = FALSE;
3882 fillable = TRUE;
3883 num = -1;
3884 str = NULL;
3885 switch (opt)
3886 {
3887 case STL_FILEPATH:
3888 case STL_FULLPATH:
3889 case STL_FILENAME:
3890 fillable = FALSE; /* don't change ' ' to fillchar */
3891 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003892 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 else
3894 {
3895 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003896 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3898 }
3899 trans_characters(NameBuff, MAXPATHL);
3900 if (opt != STL_FILENAME)
3901 str = NameBuff;
3902 else
3903 str = gettail(NameBuff);
3904 break;
3905
3906 case STL_VIM_EXPR: /* '{' */
3907 itemisflag = TRUE;
3908 t = p;
3909 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3910 *p++ = *s++;
3911 if (*s != '}') /* missing '}' or out of space */
3912 break;
3913 s++;
3914 *p = 0;
3915 p = t;
3916
3917#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003918 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3920
3921 o_curbuf = curbuf;
3922 o_curwin = curwin;
3923 curwin = wp;
3924 curbuf = wp->w_buffer;
3925
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003926 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 curwin = o_curwin;
3929 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003930 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932 if (str != NULL && *str != 0)
3933 {
3934 if (*skipdigits(str) == NUL)
3935 {
3936 num = atoi((char *)str);
3937 vim_free(str);
3938 str = NULL;
3939 itemisflag = FALSE;
3940 }
3941 }
3942#endif
3943 break;
3944
3945 case STL_LINE:
3946 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3947 ? 0L : (long)(wp->w_cursor.lnum);
3948 break;
3949
3950 case STL_NUMLINES:
3951 num = wp->w_buffer->b_ml.ml_line_count;
3952 break;
3953
3954 case STL_COLUMN:
3955 num = !(State & INSERT) && empty_line
3956 ? 0 : (int)wp->w_cursor.col + 1;
3957 break;
3958
3959 case STL_VIRTCOL:
3960 case STL_VIRTCOL_ALT:
3961 /* In list mode virtcol needs to be recomputed */
3962 virtcol = wp->w_virtcol;
3963 if (wp->w_p_list && lcs_tab1 == NUL)
3964 {
3965 wp->w_p_list = FALSE;
3966 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3967 wp->w_p_list = TRUE;
3968 }
3969 ++virtcol;
3970 /* Don't display %V if it's the same as %c. */
3971 if (opt == STL_VIRTCOL_ALT
3972 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3973 ? 0 : (int)wp->w_cursor.col + 1)))
3974 break;
3975 num = (long)virtcol;
3976 break;
3977
3978 case STL_PERCENTAGE:
3979 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3980 (long)wp->w_buffer->b_ml.ml_line_count);
3981 break;
3982
3983 case STL_ALTPERCENT:
3984 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003985 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 break;
3987
3988 case STL_ARGLISTSTAT:
3989 fillable = FALSE;
3990 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003991 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 str = tmp;
3993 break;
3994
3995 case STL_KEYMAP:
3996 fillable = FALSE;
3997 if (get_keymap_str(wp, tmp, TMPLEN))
3998 str = tmp;
3999 break;
4000 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004001#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004002 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003#else
4004 num = 0;
4005#endif
4006 break;
4007
4008 case STL_BUFNO:
4009 num = wp->w_buffer->b_fnum;
4010 break;
4011
4012 case STL_OFFSET_X:
4013 base = 'X';
4014 case STL_OFFSET:
4015#ifdef FEAT_BYTEOFF
4016 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4017 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4018 0L : l + 1 + (!(State & INSERT) && empty_line ?
4019 0 : (int)wp->w_cursor.col);
4020#endif
4021 break;
4022
4023 case STL_BYTEVAL_X:
4024 base = 'X';
4025 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004026 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 if (num == NL)
4028 num = 0;
4029 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4030 num = NL;
4031 break;
4032
4033 case STL_ROFLAG:
4034 case STL_ROFLAG_ALT:
4035 itemisflag = TRUE;
4036 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004037 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 break;
4039
4040 case STL_HELPFLAG:
4041 case STL_HELPFLAG_ALT:
4042 itemisflag = TRUE;
4043 if (wp->w_buffer->b_help)
4044 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004045 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 break;
4047
4048#ifdef FEAT_AUTOCMD
4049 case STL_FILETYPE:
4050 if (*wp->w_buffer->b_p_ft != NUL
4051 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4052 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004053 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4054 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 str = tmp;
4056 }
4057 break;
4058
4059 case STL_FILETYPE_ALT:
4060 itemisflag = TRUE;
4061 if (*wp->w_buffer->b_p_ft != NUL
4062 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4063 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004064 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4065 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 for (t = tmp; *t != 0; t++)
4067 *t = TOUPPER_LOC(*t);
4068 str = tmp;
4069 }
4070 break;
4071#endif
4072
4073#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4074 case STL_PREVIEWFLAG:
4075 case STL_PREVIEWFLAG_ALT:
4076 itemisflag = TRUE;
4077 if (wp->w_p_pvw)
4078 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4079 : _("[Preview]"));
4080 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004081
4082 case STL_QUICKFIX:
4083 if (bt_quickfix(wp->w_buffer))
4084 str = (char_u *)(wp->w_llist_ref
4085 ? _(msg_loclist)
4086 : _(msg_qflist));
4087 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088#endif
4089
4090 case STL_MODIFIED:
4091 case STL_MODIFIED_ALT:
4092 itemisflag = TRUE;
4093 switch ((opt == STL_MODIFIED_ALT)
4094 + bufIsChanged(wp->w_buffer) * 2
4095 + (!wp->w_buffer->b_p_ma) * 4)
4096 {
4097 case 2: str = (char_u *)"[+]"; break;
4098 case 3: str = (char_u *)",+"; break;
4099 case 4: str = (char_u *)"[-]"; break;
4100 case 5: str = (char_u *)",-"; break;
4101 case 6: str = (char_u *)"[+-]"; break;
4102 case 7: str = (char_u *)",+-"; break;
4103 }
4104 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004105
4106 case STL_HIGHLIGHT:
4107 t = s;
4108 while (*s != '#' && *s != NUL)
4109 ++s;
4110 if (*s == '#')
4111 {
4112 item[curitem].type = Highlight;
4113 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004114 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004115 curitem++;
4116 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004117 if (*s != NUL)
4118 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004119 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121
4122 item[curitem].start = p;
4123 item[curitem].type = Normal;
4124 if (str != NULL && *str)
4125 {
4126 t = str;
4127 if (itemisflag)
4128 {
4129 if ((t[0] && t[1])
4130 && ((!prevchar_isitem && *t == ',')
4131 || (prevchar_isflag && *t == ' ')))
4132 t++;
4133 prevchar_isflag = TRUE;
4134 }
4135 l = vim_strsize(t);
4136 if (l > 0)
4137 prevchar_isitem = TRUE;
4138 if (l > maxwid)
4139 {
4140 while (l >= maxwid)
4141#ifdef FEAT_MBYTE
4142 if (has_mbyte)
4143 {
4144 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004145 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
4147 else
4148#endif
4149 l -= byte2cells(*t++);
4150 if (p + 1 >= out + outlen)
4151 break;
4152 *p++ = '<';
4153 }
4154 if (minwid > 0)
4155 {
4156 for (; l < minwid && p + 1 < out + outlen; l++)
4157 {
4158 /* Don't put a "-" in front of a digit. */
4159 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4160 *p++ = ' ';
4161 else
4162 *p++ = fillchar;
4163 }
4164 minwid = 0;
4165 }
4166 else
4167 minwid *= -1;
4168 while (*t && p + 1 < out + outlen)
4169 {
4170 *p++ = *t++;
4171 /* Change a space by fillchar, unless fillchar is '-' and a
4172 * digit follows. */
4173 if (fillable && p[-1] == ' '
4174 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4175 p[-1] = fillchar;
4176 }
4177 for (; l < minwid && p + 1 < out + outlen; l++)
4178 *p++ = fillchar;
4179 }
4180 else if (num >= 0)
4181 {
4182 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4183 char_u nstr[20];
4184
4185 if (p + 20 >= out + outlen)
4186 break; /* not sufficient space */
4187 prevchar_isitem = TRUE;
4188 t = nstr;
4189 if (opt == STL_VIRTCOL_ALT)
4190 {
4191 *t++ = '-';
4192 minwid--;
4193 }
4194 *t++ = '%';
4195 if (zeropad)
4196 *t++ = '0';
4197 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004198 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 *t = 0;
4200
4201 for (n = num, l = 1; n >= nbase; n /= nbase)
4202 l++;
4203 if (opt == STL_VIRTCOL_ALT)
4204 l++;
4205 if (l > maxwid)
4206 {
4207 l += 2;
4208 n = l - maxwid;
4209 while (l-- > maxwid)
4210 num /= nbase;
4211 *t++ = '>';
4212 *t++ = '%';
4213 *t = t[-3];
4214 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004215 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4216 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004219 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4220 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 p += STRLEN(p);
4222 }
4223 else
4224 item[curitem].type = Empty;
4225
4226 if (opt == STL_VIM_EXPR)
4227 vim_free(str);
4228
4229 if (num >= 0 || (!itemisflag && str && *str))
4230 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4231 curitem++;
4232 }
4233 *p = NUL;
4234 itemcnt = curitem;
4235
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004236#ifdef FEAT_EVAL
4237 if (usefmt != fmt)
4238 vim_free(usefmt);
4239#endif
4240
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 width = vim_strsize(out);
4242 if (maxwidth > 0 && width > maxwidth)
4243 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004244 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 l = 0;
4246 if (itemcnt == 0)
4247 s = out;
4248 else
4249 {
4250 for ( ; l < itemcnt; l++)
4251 if (item[l].type == Trunc)
4252 {
4253 /* Truncate at %< item. */
4254 s = item[l].start;
4255 break;
4256 }
4257 if (l == itemcnt)
4258 {
4259 /* No %< item, truncate first item. */
4260 s = item[0].start;
4261 l = 0;
4262 }
4263 }
4264
4265 if (width - vim_strsize(s) >= maxwidth)
4266 {
4267 /* Truncation mark is beyond max length */
4268#ifdef FEAT_MBYTE
4269 if (has_mbyte)
4270 {
4271 s = out;
4272 width = 0;
4273 for (;;)
4274 {
4275 width += ptr2cells(s);
4276 if (width >= maxwidth)
4277 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004278 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 /* Fill up for half a double-wide character. */
4281 while (++width < maxwidth)
4282 *s++ = fillchar;
4283 }
4284 else
4285#endif
4286 s = out + maxwidth - 1;
4287 for (l = 0; l < itemcnt; l++)
4288 if (item[l].start > s)
4289 break;
4290 itemcnt = l;
4291 *s++ = '>';
4292 *s = 0;
4293 }
4294 else
4295 {
4296#ifdef FEAT_MBYTE
4297 if (has_mbyte)
4298 {
4299 n = 0;
4300 while (width >= maxwidth)
4301 {
4302 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004303 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 }
4306 else
4307#endif
4308 n = width - maxwidth + 1;
4309 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004310 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 *s = '<';
4312
4313 /* Fill up for half a double-wide character. */
4314 while (++width < maxwidth)
4315 {
4316 s = s + STRLEN(s);
4317 *s++ = fillchar;
4318 *s = NUL;
4319 }
4320
4321 --n; /* count the '<' */
4322 for (; l < itemcnt; l++)
4323 {
4324 if (item[l].start - n >= s)
4325 item[l].start -= n;
4326 else
4327 item[l].start = s;
4328 }
4329 }
4330 width = maxwidth;
4331 }
4332 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4333 {
4334 /* Apply STL_MIDDLE if any */
4335 for (l = 0; l < itemcnt; l++)
4336 if (item[l].type == Middle)
4337 break;
4338 if (l < itemcnt)
4339 {
4340 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004341 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 for (s = item[l].start; s < p; s++)
4343 *s = fillchar;
4344 for (l++; l < itemcnt; l++)
4345 item[l].start += maxwidth - width;
4346 width = maxwidth;
4347 }
4348 }
4349
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004350 /* Store the info about highlighting. */
4351 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004353 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 for (l = 0; l < itemcnt; l++)
4355 {
4356 if (item[l].type == Highlight)
4357 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004358 sp->start = item[l].start;
4359 sp->userhl = item[l].minwid;
4360 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004363 sp->start = NULL;
4364 sp->userhl = 0;
4365 }
4366
4367 /* Store the info about tab pages labels. */
4368 if (tabtab != NULL)
4369 {
4370 sp = tabtab;
4371 for (l = 0; l < itemcnt; l++)
4372 {
4373 if (item[l].type == TabPage)
4374 {
4375 sp->start = item[l].start;
4376 sp->userhl = item[l].minwid;
4377 sp++;
4378 }
4379 }
4380 sp->start = NULL;
4381 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383
4384 return width;
4385}
4386#endif /* FEAT_STL_OPT */
4387
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004388#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4389 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004391 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4392 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 */
4394 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004395get_rel_pos(
4396 win_T *wp,
4397 char_u *buf,
4398 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
4400 long above; /* number of lines above window */
4401 long below; /* number of lines below window */
4402
Bram Moolenaar0027c212015-01-07 13:31:52 +01004403 if (buflen < 3) /* need at least 3 chars for writing */
4404 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 above = wp->w_topline - 1;
4406#ifdef FEAT_DIFF
4407 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004408 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4409 above = 0; /* All buffer lines are displayed and there is an
4410 * indication of filler lines, that can be considered
4411 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412#endif
4413 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4414 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004415 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4416 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004418 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004420 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 ? (int)(above / ((above + below) / 100L))
4422 : (int)(above * 100L / (above + below)));
4423}
4424#endif
4425
4426/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004427 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 * Return TRUE if it was appended.
4429 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004430 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004431append_arg_number(
4432 win_T *wp,
4433 char_u *buf,
4434 int buflen,
4435 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436{
4437 char_u *p;
4438
4439 if (ARGCOUNT <= 1) /* nothing to do */
4440 return FALSE;
4441
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004442 p = buf + STRLEN(buf); /* go to the end of the buffer */
4443 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 return FALSE;
4445 *p++ = ' ';
4446 *p++ = '(';
4447 if (add_file)
4448 {
4449 STRCPY(p, "file ");
4450 p += 5;
4451 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004452 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4453 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4455 return TRUE;
4456}
4457
4458/*
4459 * If fname is not a full path, make it a full path.
4460 * Returns pointer to allocated memory (NULL for failure).
4461 */
4462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004463fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
4465 /*
4466 * Force expanding the path always for Unix, because symbolic links may
4467 * mess up the full path name, even though it starts with a '/'.
4468 * Also expand when there is ".." in the file name, try to remove it,
4469 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004470 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 * For MS-Windows also expand names like "longna~1" to "longname".
4472 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004473#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 return FullName_save(fname, TRUE);
4475#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004476 if (!vim_isAbsName(fname)
4477 || strstr((char *)fname, "..") != NULL
4478 || strstr((char *)fname, "//") != NULL
4479# ifdef BACKSLASH_IN_FILENAME
4480 || strstr((char *)fname, "\\\\") != NULL
4481# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004482# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 )
4486 return FullName_save(fname, FALSE);
4487
4488 fname = vim_strsave(fname);
4489
Bram Moolenaar9b942202007-10-03 12:31:33 +00004490# ifdef USE_FNAME_CASE
4491# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 {
4495 if (fname != NULL)
4496 fname_case(fname, 0); /* set correct case for file name */
4497 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
4500 return fname;
4501#endif
4502}
4503
4504/*
4505 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4506 * "ffname" becomes a pointer to allocated memory (or NULL).
4507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004509fname_expand(
4510 buf_T *buf UNUSED,
4511 char_u **ffname,
4512 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513{
4514 if (*ffname == NULL) /* if no file name given, nothing to do */
4515 return;
4516 if (*sfname == NULL) /* if no short file name given, use ffname */
4517 *sfname = *ffname;
4518 *ffname = fix_fname(*ffname); /* expand to full path */
4519
4520#ifdef FEAT_SHORTCUT
4521 if (!buf->b_p_bin)
4522 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004523 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
4525 /* If the file name is a shortcut file, use the file it links to. */
4526 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004527 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 vim_free(*ffname);
4530 *ffname = rfname;
4531 *sfname = rfname;
4532 }
4533 }
4534#endif
4535}
4536
4537/*
4538 * Get the file name for an argument list entry.
4539 */
4540 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004541alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542{
4543 buf_T *bp;
4544
4545 /* Use the name from the associated buffer if it exists. */
4546 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004547 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 return aep->ae_fname;
4549 return bp->b_fname;
4550}
4551
4552#if defined(FEAT_WINDOWS) || defined(PROTO)
4553/*
4554 * do_arg_all(): Open up to 'count' windows, one for each argument.
4555 */
4556 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004557do_arg_all(
4558 int count,
4559 int forceit, /* hide buffers in current windows */
4560 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
4562 int i;
4563 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004564 char_u *opened; /* Array of weight for which args are open:
4565 * 0: not opened
4566 * 1: opened in other tab
4567 * 2: opened in curtab
4568 * 3: opened in curtab and curwin
4569 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004570 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 int use_firstwin = FALSE; /* use first window for arglist */
4572 int split_ret = OK;
4573 int p_ea_save;
4574 alist_T *alist; /* argument list to be used */
4575 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004576 tabpage_T *tpnext;
4577 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004578 win_T *old_curwin, *last_curwin;
4579 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004580 win_T *new_curwin = NULL;
4581 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
4583 if (ARGCOUNT <= 0)
4584 {
4585 /* Don't give an error message. We don't want it when the ":all"
4586 * command is in the .vimrc. */
4587 return;
4588 }
4589 setpcmark();
4590
4591 opened_len = ARGCOUNT;
4592 opened = alloc_clear((unsigned)opened_len);
4593 if (opened == NULL)
4594 return;
4595
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004596 /* Autocommands may do anything to the argument list. Make sure it's not
4597 * freed while we are working here by "locking" it. We still have to
4598 * watch out for its size to be changed. */
4599 alist = curwin->w_alist;
4600 ++alist->al_refcount;
4601
4602 old_curwin = curwin;
4603 old_curtab = curtab;
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605#ifdef FEAT_GUI
4606 need_mouse_correct = TRUE;
4607#endif
4608
4609 /*
4610 * Try closing all windows that are not in the argument list.
4611 * Also close windows that are not full width;
4612 * When 'hidden' or "forceit" set the buffer becomes hidden.
4613 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004614 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004616 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004617 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004618 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004620 tpnext = curtab->tp_next;
4621 for (wp = firstwin; wp != NULL; wp = wpnext)
4622 {
4623 wpnext = wp->w_next;
4624 buf = wp->w_buffer;
4625 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004626 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004628 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004630 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004631 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004632 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004634 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004635 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004637 if (i < alist->al_ga.ga_len
4638 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4639 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4640 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004642 int weight = 1;
4643
4644 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004645 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004646 ++weight;
4647 if (old_curwin == wp)
4648 ++weight;
4649 }
4650
4651 if (weight > (int)opened[i])
4652 {
4653 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004654 if (i == 0)
4655 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004656 if (new_curwin != NULL)
4657 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004658 new_curwin = wp;
4659 new_curtab = curtab;
4660 }
4661 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004662 else if (keep_tabs)
4663 i = opened_len;
4664
4665 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004666 {
4667 /* Use the current argument list for all windows
4668 * containing a file from it. */
4669 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004670 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004671 ++wp->w_alist->al_refcount;
4672 }
4673 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 }
4676 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004677 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004679 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004681 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004682 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004684 /* If the buffer was changed, and we would like to hide it,
4685 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004686 if (!P_HID(buf) && buf->b_nwindows <= 1
4687 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004689 (void)autowrite(buf, FALSE);
4690#ifdef FEAT_AUTOCMD
4691 /* check if autocommands removed the window */
4692 if (!win_valid(wp) || !buf_valid(buf))
4693 {
4694 wpnext = firstwin; /* start all over... */
4695 continue;
4696 }
4697#endif
4698 }
4699#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004700 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004701 if (firstwin == lastwin
4702 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004703#endif
4704 use_firstwin = TRUE;
4705#ifdef FEAT_WINDOWS
4706 else
4707 {
4708 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4709# ifdef FEAT_AUTOCMD
4710 /* check if autocommands removed the next window */
4711 if (!win_valid(wpnext))
4712 wpnext = firstwin; /* start all over... */
4713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
4715#endif
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 }
4718 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004719
4720 /* Without the ":tab" modifier only do the current tab page. */
4721 if (had_tab == 0 || tpnext == NULL)
4722 break;
4723
4724# ifdef FEAT_AUTOCMD
4725 /* check if autocommands removed the next tab page */
4726 if (!valid_tabpage(tpnext))
4727 tpnext = first_tabpage; /* start all over...*/
4728# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004729 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731
4732 /*
4733 * Open a window for files in the argument list that don't have one.
4734 * ARGCOUNT may change while doing this, because of autocommands.
4735 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004736 if (count > opened_len || count <= 0)
4737 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738
4739#ifdef FEAT_AUTOCMD
4740 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4741 ++autocmd_no_enter;
4742 ++autocmd_no_leave;
4743#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004744 last_curwin = curwin;
4745 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004747#ifdef FEAT_WINDOWS
4748 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4749 * leaving an empty tab page when executed locally. */
4750 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4751 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4752 use_firstwin = TRUE;
4753#endif
4754
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004755 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4758 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004759 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
4761 /* Move the already present window to below the current window */
4762 if (curwin->w_arg_idx != i)
4763 {
4764 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4765 {
4766 if (wpnext->w_arg_idx == i)
4767 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004768 if (keep_tabs)
4769 {
4770 new_curwin = wpnext;
4771 new_curtab = curtab;
4772 }
4773 else
4774 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 break;
4776 }
4777 }
4778 }
4779 }
4780 else if (split_ret == OK)
4781 {
4782 if (!use_firstwin) /* split current window */
4783 {
4784 p_ea_save = p_ea;
4785 p_ea = TRUE; /* use space from all windows */
4786 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4787 p_ea = p_ea_save;
4788 if (split_ret == FAIL)
4789 continue;
4790 }
4791#ifdef FEAT_AUTOCMD
4792 else /* first window: do autocmd for leaving this buffer */
4793 --autocmd_no_leave;
4794#endif
4795
4796 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004797 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 */
4799 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004800 if (i == 0)
4801 {
4802 new_curwin = curwin;
4803 new_curtab = curtab;
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4806 ECMD_ONE,
4807 ((P_HID(curwin->w_buffer)
4808 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004809 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810#ifdef FEAT_AUTOCMD
4811 if (use_firstwin)
4812 ++autocmd_no_leave;
4813#endif
4814 use_firstwin = FALSE;
4815 }
4816 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004817
4818 /* When ":tab" was used open a new tab for a new window repeatedly. */
4819 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4820 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822
4823 /* Remove the "lock" on the argument list. */
4824 alist_unlink(alist);
4825
4826#ifdef FEAT_AUTOCMD
4827 --autocmd_no_enter;
4828#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004829 /* restore last referenced tabpage's curwin */
4830 if (last_curtab != new_curtab)
4831 {
4832 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004833 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004834 if (win_valid(last_curwin))
4835 win_enter(last_curwin, FALSE);
4836 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004837 /* to window with first arg */
4838 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004839 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004840 if (win_valid(new_curwin))
4841 win_enter(new_curwin, FALSE);
4842
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843#ifdef FEAT_AUTOCMD
4844 --autocmd_no_leave;
4845#endif
4846 vim_free(opened);
4847}
4848
4849# if defined(FEAT_LISTCMDS) || defined(PROTO)
4850/*
4851 * Open a window for a number of buffers.
4852 */
4853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004854ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 buf_T *buf;
4857 win_T *wp, *wpnext;
4858 int split_ret = OK;
4859 int p_ea_save;
4860 int open_wins = 0;
4861 int r;
4862 int count; /* Maximum number of windows to open. */
4863 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004864#ifdef FEAT_WINDOWS
4865 int had_tab = cmdmod.tab;
4866 tabpage_T *tpnext;
4867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 if (eap->addr_count == 0) /* make as many windows as possible */
4870 count = 9999;
4871 else
4872 count = eap->line2; /* make as many windows as specified */
4873 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4874 all = FALSE;
4875 else
4876 all = TRUE;
4877
4878 setpcmark();
4879
4880#ifdef FEAT_GUI
4881 need_mouse_correct = TRUE;
4882#endif
4883
4884 /*
4885 * Close superfluous windows (two windows for the same buffer).
4886 * Also close windows that are not full-width.
4887 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004888#ifdef FEAT_WINDOWS
4889 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004890 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004891 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004894 tpnext = curtab->tp_next;
4895 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004897 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004898 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004899#ifdef FEAT_VERTSPLIT
4900 || ((cmdmod.split & WSP_VERT)
4901 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004902 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004903 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004905#ifdef FEAT_WINDOWS
4906 || (had_tab > 0 && wp != firstwin)
4907#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004908 ) && firstwin != lastwin
4909#ifdef FEAT_AUTOCMD
4910 && !(wp->w_closing || wp->w_buffer->b_closing)
4911#endif
4912 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004913 {
4914 win_close(wp, FALSE);
4915#ifdef FEAT_AUTOCMD
4916 wpnext = firstwin; /* just in case an autocommand does
4917 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004918 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004919 open_wins = 0;
4920#endif
4921 }
4922 else
4923 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004925
4926#ifdef FEAT_WINDOWS
4927 /* Without the ":tab" modifier only do the current tab page. */
4928 if (had_tab == 0 || tpnext == NULL)
4929 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004930 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934 /*
4935 * Go through the buffer list. When a buffer doesn't have a window yet,
4936 * open one. Otherwise move the window to the right position.
4937 * Watch out for autocommands that delete buffers or windows!
4938 */
4939#ifdef FEAT_AUTOCMD
4940 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4941 ++autocmd_no_enter;
4942#endif
4943 win_enter(lastwin, FALSE);
4944#ifdef FEAT_AUTOCMD
4945 ++autocmd_no_leave;
4946#endif
4947 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4948 {
4949 /* Check if this buffer needs a window */
4950 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4951 continue;
4952
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004953#ifdef FEAT_WINDOWS
4954 if (had_tab != 0)
4955 {
4956 /* With the ":tab" modifier don't move the window. */
4957 if (buf->b_nwindows > 0)
4958 wp = lastwin; /* buffer has a window, skip it */
4959 else
4960 wp = NULL;
4961 }
4962 else
4963#endif
4964 {
4965 /* Check if this buffer already has a window */
4966 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4967 if (wp->w_buffer == buf)
4968 break;
4969 /* If the buffer already has a window, move it */
4970 if (wp != NULL)
4971 win_move_after(wp, curwin);
4972 }
4973
4974 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
4976 /* Split the window and put the buffer in it */
4977 p_ea_save = p_ea;
4978 p_ea = TRUE; /* use space from all windows */
4979 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4980 ++open_wins;
4981 p_ea = p_ea_save;
4982 if (split_ret == FAIL)
4983 continue;
4984
4985 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004986#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 swap_exists_action = SEA_DIALOG;
4988#endif
4989 set_curbuf(buf, DOBUF_GOTO);
4990#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004993#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 break;
4997 }
4998#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004999#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 if (swap_exists_action == SEA_QUIT)
5001 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005002# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5003 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005004
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005005 /* Reset the error/interrupt/exception state here so that
5006 * aborting() returns FALSE when closing a window. */
5007 enter_cleanup(&cs);
5008# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005009
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005010 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 win_close(curwin, TRUE);
5012 --open_wins;
5013 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005014 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005015
5016# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5017 /* Restore the error/interrupt/exception state if not
5018 * discarded by a new aborting error, interrupt, or uncaught
5019 * exception. */
5020 leave_cleanup(&cs);
5021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 else
5024 handle_swap_exists(NULL);
5025#endif
5026 }
5027
5028 ui_breakcheck();
5029 if (got_int)
5030 {
5031 (void)vgetc(); /* only break the file loading, not the rest */
5032 break;
5033 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005034#ifdef FEAT_EVAL
5035 /* Autocommands deleted the buffer or aborted script processing!!! */
5036 if (aborting())
5037 break;
5038#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005039#ifdef FEAT_WINDOWS
5040 /* When ":tab" was used open a new tab for a new window repeatedly. */
5041 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5042 cmdmod.tab = 9999;
5043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045#ifdef FEAT_AUTOCMD
5046 --autocmd_no_enter;
5047#endif
5048 win_enter(firstwin, FALSE); /* back to first window */
5049#ifdef FEAT_AUTOCMD
5050 --autocmd_no_leave;
5051#endif
5052
5053 /*
5054 * Close superfluous windows.
5055 */
5056 for (wp = lastwin; open_wins > count; )
5057 {
5058 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5059 || autowrite(wp->w_buffer, FALSE) == OK);
5060#ifdef FEAT_AUTOCMD
5061 if (!win_valid(wp))
5062 {
5063 /* BufWrite Autocommands made the window invalid, start over */
5064 wp = lastwin;
5065 }
5066 else
5067#endif
5068 if (r)
5069 {
5070 win_close(wp, !P_HID(wp->w_buffer));
5071 --open_wins;
5072 wp = lastwin;
5073 }
5074 else
5075 {
5076 wp = wp->w_prev;
5077 if (wp == NULL)
5078 break;
5079 }
5080 }
5081}
5082# endif /* FEAT_LISTCMDS */
5083
5084#endif /* FEAT_WINDOWS */
5085
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005086static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088/*
5089 * do_modelines() - process mode lines for the current file
5090 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005091 * "flags" can be:
5092 * OPT_WINONLY only set options local to window
5093 * OPT_NOWIN don't set options local to window
5094 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 * Returns immediately if the "ml" option isn't set.
5096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005098do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005100 linenr_T lnum;
5101 int nmlines;
5102 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103
5104 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5105 return;
5106
5107 /* Disallow recursive entry here. Can happen when executing a modeline
5108 * triggers an autocommand, which reloads modelines with a ":do". */
5109 if (entered)
5110 return;
5111
5112 ++entered;
5113 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5114 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005115 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 nmlines = 0;
5117
5118 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5119 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005120 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 nmlines = 0;
5122 --entered;
5123}
5124
5125#include "version.h" /* for version number */
5126
5127/*
5128 * chk_modeline() - check a single line for a mode string
5129 * Return FAIL if an error encountered.
5130 */
5131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005132chk_modeline(
5133 linenr_T lnum,
5134 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135{
5136 char_u *s;
5137 char_u *e;
5138 char_u *linecopy; /* local copy of any modeline found */
5139 int prev;
5140 int vers;
5141 int end;
5142 int retval = OK;
5143 char_u *save_sourcing_name;
5144 linenr_T save_sourcing_lnum;
5145#ifdef FEAT_EVAL
5146 scid_T save_SID;
5147#endif
5148
5149 prev = -1;
5150 for (s = ml_get(lnum); *s != NUL; ++s)
5151 {
5152 if (prev == -1 || vim_isspace(prev))
5153 {
5154 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5155 || STRNCMP(s, "vi:", (size_t)3) == 0)
5156 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005157 /* Accept both "vim" and "Vim". */
5158 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
5160 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5161 e = s + 4;
5162 else
5163 e = s + 3;
5164 vers = getdigits(&e);
5165 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005166 && (s[0] != 'V'
5167 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 && (s[3] == ':'
5169 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5170 || (VIM_VERSION_100 < vers && s[3] == '<')
5171 || (VIM_VERSION_100 > vers && s[3] == '>')
5172 || (VIM_VERSION_100 == vers && s[3] == '=')))
5173 break;
5174 }
5175 }
5176 prev = *s;
5177 }
5178
5179 if (*s)
5180 {
5181 do /* skip over "ex:", "vi:" or "vim:" */
5182 ++s;
5183 while (s[-1] != ':');
5184
5185 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5186 if (linecopy == NULL)
5187 return FAIL;
5188
5189 save_sourcing_lnum = sourcing_lnum;
5190 save_sourcing_name = sourcing_name;
5191 sourcing_lnum = lnum; /* prepare for emsg() */
5192 sourcing_name = (char_u *)"modelines";
5193
5194 end = FALSE;
5195 while (end == FALSE)
5196 {
5197 s = skipwhite(s);
5198 if (*s == NUL)
5199 break;
5200
5201 /*
5202 * Find end of set command: ':' or end of line.
5203 * Skip over "\:", replacing it with ":".
5204 */
5205 for (e = s; *e != ':' && *e != NUL; ++e)
5206 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005207 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (*e == NUL)
5209 end = TRUE;
5210
5211 /*
5212 * If there is a "set" command, require a terminating ':' and
5213 * ignore the stuff after the ':'.
5214 * "vi:set opt opt opt: foo" -- foo not interpreted
5215 * "vi:opt opt opt: foo" -- foo interpreted
5216 * Accept "se" for compatibility with Elvis.
5217 */
5218 if (STRNCMP(s, "set ", (size_t)4) == 0
5219 || STRNCMP(s, "se ", (size_t)3) == 0)
5220 {
5221 if (*e != ':') /* no terminating ':'? */
5222 break;
5223 end = TRUE;
5224 s = vim_strchr(s, ' ') + 1;
5225 }
5226 *e = NUL; /* truncate the set command */
5227
5228 if (*s != NUL) /* skip over an empty "::" */
5229 {
5230#ifdef FEAT_EVAL
5231 save_SID = current_SID;
5232 current_SID = SID_MODELINE;
5233#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005234 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#ifdef FEAT_EVAL
5236 current_SID = save_SID;
5237#endif
5238 if (retval == FAIL) /* stop if error found */
5239 break;
5240 }
5241 s = e + 1; /* advance to next part */
5242 }
5243
5244 sourcing_lnum = save_sourcing_lnum;
5245 sourcing_name = save_sourcing_name;
5246
5247 vim_free(linecopy);
5248 }
5249 return retval;
5250}
5251
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005252#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005254read_viminfo_bufferlist(
5255 vir_T *virp,
5256 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257{
5258 char_u *tab;
5259 linenr_T lnum;
5260 colnr_T col;
5261 buf_T *buf;
5262 char_u *sfname;
5263 char_u *xline;
5264
5265 /* Handle long line and escaped characters. */
5266 xline = viminfo_readstring(virp, 1, FALSE);
5267
5268 /* don't read in if there are files on the command-line or if writing: */
5269 if (xline != NULL && !writing && ARGCOUNT == 0
5270 && find_viminfo_parameter('%') != NULL)
5271 {
5272 /* Format is: <fname> Tab <lnum> Tab <col>.
5273 * Watch out for a Tab in the file name, work from the end. */
5274 lnum = 0;
5275 col = 0;
5276 tab = vim_strrchr(xline, '\t');
5277 if (tab != NULL)
5278 {
5279 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005280 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 tab = vim_strrchr(xline, '\t');
5282 if (tab != NULL)
5283 {
5284 *tab++ = '\0';
5285 lnum = atol((char *)tab);
5286 }
5287 }
5288
5289 /* Expand "~/" in the file name at "line + 1" to a full path.
5290 * Then try shortening it by comparing with the current directory */
5291 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005292 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5295 if (buf != NULL) /* just in case... */
5296 {
5297 buf->b_last_cursor.lnum = lnum;
5298 buf->b_last_cursor.col = col;
5299 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5300 }
5301 }
5302 vim_free(xline);
5303
5304 return viminfo_readline(virp);
5305}
5306
5307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005308write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
5310 buf_T *buf;
5311#ifdef FEAT_WINDOWS
5312 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005313 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314#endif
5315 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005316 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317
5318 if (find_viminfo_parameter('%') == NULL)
5319 return;
5320
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005321 /* Without a number -1 is returned: do all buffers. */
5322 max_buffers = get_viminfo_parameter('%');
5323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005325#define LINE_BUF_LEN (MAXPATHL + 40)
5326 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 if (line == NULL)
5328 return;
5329
5330#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005331 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 set_last_cursor(win);
5333#else
5334 set_last_cursor(curwin);
5335#endif
5336
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005337 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5339 {
5340 if (buf->b_fname == NULL
5341 || !buf->b_p_bl
5342#ifdef FEAT_QUICKFIX
5343 || bt_quickfix(buf)
5344#endif
5345 || removable(buf->b_ffname))
5346 continue;
5347
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005348 if (max_buffers-- == 0)
5349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 putc('%', fp);
5351 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005352 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 (long)buf->b_last_cursor.lnum,
5354 buf->b_last_cursor.col);
5355 viminfo_writestring(fp, line);
5356 }
5357 vim_free(line);
5358}
5359#endif
5360
5361
5362/*
5363 * Return special buffer name.
5364 * Returns NULL when the buffer has a normal file name.
5365 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005366 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005367buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
5369#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5370 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005371 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005372 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005373 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005374
5375 /*
5376 * For location list window, w_llist_ref points to the location list.
5377 * For quickfix window, w_llist_ref is NULL.
5378 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005379 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005380 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005381 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005382 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384#endif
5385#ifdef FEAT_QUICKFIX
5386 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5387 * contains the name as specified by the user */
5388 if (bt_nofile(buf))
5389 {
5390 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005391 return buf->b_sfname;
5392 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394#endif
5395 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005396 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 return NULL;
5398}
5399
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005400#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5401 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5402 || defined(PROTO)
5403/*
5404 * Find a window for buffer "buf".
5405 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5406 * If not found FAIL is returned.
5407 */
5408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005409find_win_for_buf(
5410 buf_T *buf,
5411 win_T **wp,
5412 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005413{
5414 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5415 if ((*wp)->w_buffer == buf)
5416 goto win_found;
5417 return FAIL;
5418win_found:
5419 return OK;
5420}
5421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423#if defined(FEAT_SIGNS) || defined(PROTO)
5424/*
5425 * Insert the sign into the signlist.
5426 */
5427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005428insert_sign(
5429 buf_T *buf, /* buffer to store sign in */
5430 signlist_T *prev, /* previous sign entry */
5431 signlist_T *next, /* next sign entry */
5432 int id, /* sign ID */
5433 linenr_T lnum, /* line number which gets the mark */
5434 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 signlist_T *newsign;
5437
5438 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5439 if (newsign != NULL)
5440 {
5441 newsign->id = id;
5442 newsign->lnum = lnum;
5443 newsign->typenr = typenr;
5444 newsign->next = next;
5445#ifdef FEAT_NETBEANS_INTG
5446 newsign->prev = prev;
5447 if (next != NULL)
5448 next->prev = newsign;
5449#endif
5450
5451 if (prev == NULL)
5452 {
5453 /* When adding first sign need to redraw the windows to create the
5454 * column for signs. */
5455 if (buf->b_signlist == NULL)
5456 {
5457 redraw_buf_later(buf, NOT_VALID);
5458 changed_cline_bef_curs();
5459 }
5460
5461 /* first sign in signlist */
5462 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005463#ifdef FEAT_NETBEANS_INTG
5464 if (netbeans_active())
5465 buf->b_has_sign_column = TRUE;
5466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 else
5469 prev->next = newsign;
5470 }
5471}
5472
5473/*
5474 * Add the sign into the signlist. Find the right spot to do it though.
5475 */
5476 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005477buf_addsign(
5478 buf_T *buf, /* buffer to store sign in */
5479 int id, /* sign ID */
5480 linenr_T lnum, /* line number which gets the mark */
5481 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483 signlist_T *sign; /* a sign in the signlist */
5484 signlist_T *prev; /* the previous sign */
5485
5486 prev = NULL;
5487 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5488 {
5489 if (lnum == sign->lnum && id == sign->id)
5490 {
5491 sign->typenr = typenr;
5492 return;
5493 }
5494 else if (
5495#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5496 id < 0 &&
5497#endif
5498 lnum < sign->lnum)
5499 {
5500#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5501 /* XXX - GRP: Is this because of sign slide problem? Or is it
5502 * really needed? Or is it because we allow multiple signs per
5503 * line? If so, should I add that feature to FEAT_SIGNS?
5504 */
5505 while (prev != NULL && prev->lnum == lnum)
5506 prev = prev->prev;
5507 if (prev == NULL)
5508 sign = buf->b_signlist;
5509 else
5510 sign = prev->next;
5511#endif
5512 insert_sign(buf, prev, sign, id, lnum, typenr);
5513 return;
5514 }
5515 prev = sign;
5516 }
5517#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5518 /* XXX - GRP: See previous comment */
5519 while (prev != NULL && prev->lnum == lnum)
5520 prev = prev->prev;
5521 if (prev == NULL)
5522 sign = buf->b_signlist;
5523 else
5524 sign = prev->next;
5525#endif
5526 insert_sign(buf, prev, sign, id, lnum, typenr);
5527
5528 return;
5529}
5530
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005531/*
5532 * For an existing, placed sign "markId" change the type to "typenr".
5533 * Returns the line number of the sign, or zero if the sign is not found.
5534 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005535 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005536buf_change_sign_type(
5537 buf_T *buf, /* buffer to store sign in */
5538 int markId, /* sign ID */
5539 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 signlist_T *sign; /* a sign in the signlist */
5542
5543 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5544 {
5545 if (sign->id == markId)
5546 {
5547 sign->typenr = typenr;
5548 return sign->lnum;
5549 }
5550 }
5551
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005552 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553}
5554
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005555 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005556buf_getsigntype(
5557 buf_T *buf,
5558 linenr_T lnum,
5559 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 signlist_T *sign; /* a sign in a b_signlist */
5562
5563 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5564 if (sign->lnum == lnum
5565 && (type == SIGN_ANY
5566# ifdef FEAT_SIGN_ICONS
5567 || (type == SIGN_ICON
5568 && sign_get_image(sign->typenr) != NULL)
5569# endif
5570 || (type == SIGN_TEXT
5571 && sign_get_text(sign->typenr) != NULL)
5572 || (type == SIGN_LINEHL
5573 && sign_get_attr(sign->typenr, TRUE) != 0)))
5574 return sign->typenr;
5575 return 0;
5576}
5577
5578
5579 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005580buf_delsign(
5581 buf_T *buf, /* buffer sign is stored in */
5582 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 signlist_T **lastp; /* pointer to pointer to current sign */
5585 signlist_T *sign; /* a sign in a b_signlist */
5586 signlist_T *next; /* the next sign in a b_signlist */
5587 linenr_T lnum; /* line number whose sign was deleted */
5588
5589 lastp = &buf->b_signlist;
5590 lnum = 0;
5591 for (sign = buf->b_signlist; sign != NULL; sign = next)
5592 {
5593 next = sign->next;
5594 if (sign->id == id)
5595 {
5596 *lastp = next;
5597#ifdef FEAT_NETBEANS_INTG
5598 if (next != NULL)
5599 next->prev = sign->prev;
5600#endif
5601 lnum = sign->lnum;
5602 vim_free(sign);
5603 break;
5604 }
5605 else
5606 lastp = &sign->next;
5607 }
5608
5609 /* When deleted the last sign need to redraw the windows to remove the
5610 * sign column. */
5611 if (buf->b_signlist == NULL)
5612 {
5613 redraw_buf_later(buf, NOT_VALID);
5614 changed_cline_bef_curs();
5615 }
5616
5617 return lnum;
5618}
5619
5620
5621/*
5622 * Find the line number of the sign with the requested id. If the sign does
5623 * not exist, return 0 as the line number. This will still let the correct file
5624 * get loaded.
5625 */
5626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005627buf_findsign(
5628 buf_T *buf, /* buffer to store sign in */
5629 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631 signlist_T *sign; /* a sign in the signlist */
5632
5633 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5634 if (sign->id == id)
5635 return sign->lnum;
5636
5637 return 0;
5638}
5639
5640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005641buf_findsign_id(
5642 buf_T *buf, /* buffer whose sign we are searching for */
5643 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
5645 signlist_T *sign; /* a sign in the signlist */
5646
5647 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5648 if (sign->lnum == lnum)
5649 return sign->id;
5650
5651 return 0;
5652}
5653
5654
5655# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5656/* see if a given type of sign exists on a specific line */
5657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005658buf_findsigntype_id(
5659 buf_T *buf, /* buffer whose sign we are searching for */
5660 linenr_T lnum, /* line number of sign */
5661 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 signlist_T *sign; /* a sign in the signlist */
5664
5665 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5666 if (sign->lnum == lnum && sign->typenr == typenr)
5667 return sign->id;
5668
5669 return 0;
5670}
5671
5672
5673# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5674/* return the number of icons on the given line */
5675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005676buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 signlist_T *sign; /* a sign in the signlist */
5679 int count = 0;
5680
5681 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5682 if (sign->lnum == lnum)
5683 if (sign_get_image(sign->typenr) != NULL)
5684 count++;
5685
5686 return count;
5687}
5688# endif /* FEAT_SIGN_ICONS */
5689# endif /* FEAT_NETBEANS_INTG */
5690
5691
5692/*
5693 * Delete signs in buffer "buf".
5694 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005696buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698 signlist_T *next;
5699
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005700 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005701 * sign column. Not when curwin is NULL (this means we're exiting). */
5702 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005703 {
5704 redraw_buf_later(buf, NOT_VALID);
5705 changed_cline_bef_curs();
5706 }
5707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 while (buf->b_signlist != NULL)
5709 {
5710 next = buf->b_signlist->next;
5711 vim_free(buf->b_signlist);
5712 buf->b_signlist = next;
5713 }
5714}
5715
5716/*
5717 * Delete all signs in all buffers.
5718 */
5719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005720buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
5722 buf_T *buf; /* buffer we are checking for signs */
5723
5724 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5725 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727}
5728
5729/*
5730 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5731 */
5732 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005733sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735 buf_T *buf;
5736 signlist_T *p;
5737 char lbuf[BUFSIZ];
5738
5739 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5740 msg_putchar('\n');
5741 if (rbuf == NULL)
5742 buf = firstbuf;
5743 else
5744 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005745 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
5747 if (buf->b_signlist != NULL)
5748 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005749 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5751 msg_putchar('\n');
5752 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005753 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005755 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5757 MSG_PUTS(lbuf);
5758 msg_putchar('\n');
5759 }
5760 if (rbuf != NULL)
5761 break;
5762 buf = buf->b_next;
5763 }
5764}
5765
5766/*
5767 * Adjust a placed sign for inserted/deleted lines.
5768 */
5769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005770sign_mark_adjust(
5771 linenr_T line1,
5772 linenr_T line2,
5773 long amount,
5774 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 signlist_T *sign; /* a sign in a b_signlist */
5777
5778 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5779 {
5780 if (sign->lnum >= line1 && sign->lnum <= line2)
5781 {
5782 if (amount == MAXLNUM)
5783 sign->lnum = line1;
5784 else
5785 sign->lnum += amount;
5786 }
5787 else if (sign->lnum > line2)
5788 sign->lnum += amount_after;
5789 }
5790}
5791#endif /* FEAT_SIGNS */
5792
5793/*
5794 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5795 */
5796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005797set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 if (on != curbuf->b_p_bl)
5800 {
5801 curbuf->b_p_bl = on;
5802#ifdef FEAT_AUTOCMD
5803 if (on)
5804 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5805 else
5806 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5807#endif
5808 }
5809}
5810
5811/*
5812 * Read the file for "buf" again and check if the contents changed.
5813 * Return TRUE if it changed or this could not be checked.
5814 */
5815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005816buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818 buf_T *newbuf;
5819 int differ = TRUE;
5820 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 exarg_T ea;
5823
5824 /* Allocate a buffer without putting it in the buffer list. */
5825 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5826 if (newbuf == NULL)
5827 return TRUE;
5828
5829 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5830 if (prep_exarg(&ea, buf) == FAIL)
5831 {
5832 wipe_buffer(newbuf, FALSE);
5833 return TRUE;
5834 }
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 /* set curwin/curbuf to buf and save a few things */
5837 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaar4770d092006-01-12 23:22:24 +00005839 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 && readfile(buf->b_ffname, buf->b_fname,
5841 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5842 &ea, READ_NEW | READ_DUMMY) == OK)
5843 {
5844 /* compare the two files line by line */
5845 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5846 {
5847 differ = FALSE;
5848 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5849 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5850 {
5851 differ = TRUE;
5852 break;
5853 }
5854 }
5855 }
5856 vim_free(ea.cmd);
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 /* restore curwin/curbuf and a few other things */
5859 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
5861 if (curbuf != newbuf) /* safety check */
5862 wipe_buffer(newbuf, FALSE);
5863
5864 return differ;
5865}
5866
5867/*
5868 * Wipe out a buffer and decrement the last buffer number if it was used for
5869 * this buffer. Call this to wipe out a temp buffer that does not contain any
5870 * marks.
5871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005873wipe_buffer(
5874 buf_T *buf,
5875 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 if (buf->b_fnum == top_file_num - 1)
5878 --top_file_num;
5879
5880#ifdef FEAT_AUTOCMD
5881 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005882 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005884 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885#ifdef FEAT_AUTOCMD
5886 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005887 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888#endif
5889}