blob: 89dedc58f92b9207abd4983bf53ece331ac6f0d2 [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.
1618 */
1619 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001620do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001621{
1622 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1623 shorten_fnames(TRUE);
1624}
1625#endif
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627/*
1628 * functions for dealing with the buffer list
1629 */
1630
1631/*
1632 * Add a file name to the buffer list. Return a pointer to the buffer.
1633 * If the same file name already exists return a pointer to that buffer.
1634 * If it does not exist, or if fname == NULL, a new entry is created.
1635 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1636 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1637 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 * This is the ONLY way to create a new buffer.
1639 */
1640static int top_file_num = 1; /* highest file number */
1641
1642 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001643buflist_new(
1644 char_u *ffname, /* full path of fname or relative */
1645 char_u *sfname, /* short fname or NULL */
1646 linenr_T lnum, /* preferred cursor line */
1647 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
1649 buf_T *buf;
1650#ifdef UNIX
1651 struct stat st;
1652#endif
1653
1654 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1655
1656 /*
1657 * If file name already exists in the list, update the entry.
1658 */
1659#ifdef UNIX
1660 /* On Unix we can use inode numbers when the file exists. Works better
1661 * for hard links. */
1662 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1663 st.st_dev = (dev_T)-1;
1664#endif
1665 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1666#ifdef UNIX
1667 buflist_findname_stat(ffname, &st)
1668#else
1669 buflist_findname(ffname)
1670#endif
1671 ) != NULL)
1672 {
1673 vim_free(ffname);
1674 if (lnum != 0)
1675 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1676 /* copy the options now, if 'cpo' doesn't have 's' and not done
1677 * already */
1678 buf_copy_options(buf, 0);
1679 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1680 {
1681 buf->b_p_bl = TRUE;
1682#ifdef FEAT_AUTOCMD
1683 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001686 if (!buf_valid(buf))
1687 return NULL;
1688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
1690 }
1691 return buf;
1692 }
1693
1694 /*
1695 * If the current buffer has no name and no contents, use the current
1696 * buffer. Otherwise: Need to allocate a new buffer structure.
1697 *
1698 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001699 * (A spell file buffer is allocated in spell.c, but that's not a normal
1700 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 */
1702 buf = NULL;
1703 if ((flags & BLN_CURBUF)
1704 && curbuf != NULL
1705 && curbuf->b_ffname == NULL
1706 && curbuf->b_nwindows <= 1
1707 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1708 {
1709 buf = curbuf;
1710#ifdef FEAT_AUTOCMD
1711 /* It's like this buffer is deleted. Watch out for autocommands that
1712 * change curbuf! If that happens, allocate a new buffer anyway. */
1713 if (curbuf->b_p_bl)
1714 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1715 if (buf == curbuf)
1716 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1717# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001718 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 return NULL;
1720# endif
1721#endif
1722#ifdef FEAT_QUICKFIX
1723# ifdef FEAT_AUTOCMD
1724 if (buf == curbuf)
1725# endif
1726 {
1727 /* Make sure 'bufhidden' and 'buftype' are empty */
1728 clear_string_option(&buf->b_p_bh);
1729 clear_string_option(&buf->b_p_bt);
1730 }
1731#endif
1732 }
1733 if (buf != curbuf || curbuf == NULL)
1734 {
1735 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1736 if (buf == NULL)
1737 {
1738 vim_free(ffname);
1739 return NULL;
1740 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001741#ifdef FEAT_EVAL
1742 /* init b: variables */
1743 buf->b_vars = dict_alloc();
1744 if (buf->b_vars == NULL)
1745 {
1746 vim_free(ffname);
1747 vim_free(buf);
1748 return NULL;
1749 }
1750 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 }
1753
1754 if (ffname != NULL)
1755 {
1756 buf->b_ffname = ffname;
1757 buf->b_sfname = vim_strsave(sfname);
1758 }
1759
1760 clear_wininfo(buf);
1761 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1762
1763 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1764 || buf->b_wininfo == NULL)
1765 {
1766 vim_free(buf->b_ffname);
1767 buf->b_ffname = NULL;
1768 vim_free(buf->b_sfname);
1769 buf->b_sfname = NULL;
1770 if (buf != curbuf)
1771 free_buffer(buf);
1772 return NULL;
1773 }
1774
1775 if (buf == curbuf)
1776 {
1777 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001778 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (buf != curbuf) /* autocommands deleted the buffer! */
1780 return NULL;
1781#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001782 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 return NULL;
1784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001786
1787 /* Init the options. */
1788 buf->b_p_initialized = FALSE;
1789 buf_copy_options(buf, BCO_ENTER);
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#ifdef FEAT_KEYMAP
1792 /* need to reload lmaps and set b:keymap_name */
1793 curbuf->b_kmap_state |= KEYMAP_INIT;
1794#endif
1795 }
1796 else
1797 {
1798 /*
1799 * put new buffer at the end of the buffer list
1800 */
1801 buf->b_next = NULL;
1802 if (firstbuf == NULL) /* buffer list is empty */
1803 {
1804 buf->b_prev = NULL;
1805 firstbuf = buf;
1806 }
1807 else /* append new buffer at end of list */
1808 {
1809 lastbuf->b_next = buf;
1810 buf->b_prev = lastbuf;
1811 }
1812 lastbuf = buf;
1813
1814 buf->b_fnum = top_file_num++;
1815 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1816 {
1817 EMSG(_("W14: Warning: List of file names overflow"));
1818 if (emsg_silent == 0)
1819 {
1820 out_flush();
1821 ui_delay(3000L, TRUE); /* make sure it is noticed */
1822 }
1823 top_file_num = 1;
1824 }
1825
1826 /*
1827 * Always copy the options from the current buffer.
1828 */
1829 buf_copy_options(buf, BCO_ALWAYS);
1830 }
1831
1832 buf->b_wininfo->wi_fpos.lnum = lnum;
1833 buf->b_wininfo->wi_win = curwin;
1834
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001835#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001836 hash_init(&buf->b_s.b_keywtab);
1837 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838#endif
1839
1840 buf->b_fname = buf->b_sfname;
1841#ifdef UNIX
1842 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001843 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 else
1845 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001846 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 buf->b_dev = st.st_dev;
1848 buf->b_ino = st.st_ino;
1849 }
1850#endif
1851 buf->b_u_synced = TRUE;
1852 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001853 if (flags & BLN_DUMMY)
1854 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 buf_clear_file(buf);
1856 clrallmarks(buf); /* clear marks */
1857 fmarks_check_names(buf); /* check file marks for this file */
1858 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1859#ifdef FEAT_AUTOCMD
1860 if (!(flags & BLN_DUMMY))
1861 {
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001862 /* Tricky: these autocommands may change the buffer list. They could
1863 * also split the window with re-using the one empty buffer. This may
1864 * result in unexpectedly losing the empty buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001866 if (!buf_valid(buf))
1867 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001871 if (!buf_valid(buf))
1872 return NULL;
1873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001875 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 return NULL;
1877# endif
1878 }
1879#endif
1880
1881 return buf;
1882}
1883
1884/*
1885 * Free the memory for the options of a buffer.
1886 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1887 * 'fileencoding'.
1888 */
1889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001890free_buf_options(
1891 buf_T *buf,
1892 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 if (free_p_ff)
1895 {
1896#ifdef FEAT_MBYTE
1897 clear_string_option(&buf->b_p_fenc);
1898#endif
1899 clear_string_option(&buf->b_p_ff);
1900#ifdef FEAT_QUICKFIX
1901 clear_string_option(&buf->b_p_bh);
1902 clear_string_option(&buf->b_p_bt);
1903#endif
1904 }
1905#ifdef FEAT_FIND_ID
1906 clear_string_option(&buf->b_p_def);
1907 clear_string_option(&buf->b_p_inc);
1908# ifdef FEAT_EVAL
1909 clear_string_option(&buf->b_p_inex);
1910# endif
1911#endif
1912#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1913 clear_string_option(&buf->b_p_inde);
1914 clear_string_option(&buf->b_p_indk);
1915#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001916#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1917 clear_string_option(&buf->b_p_bexpr);
1918#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001919#if defined(FEAT_CRYPT)
1920 clear_string_option(&buf->b_p_cm);
1921#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001922#if defined(FEAT_EVAL)
1923 clear_string_option(&buf->b_p_fex);
1924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef FEAT_CRYPT
1926 clear_string_option(&buf->b_p_key);
1927#endif
1928 clear_string_option(&buf->b_p_kp);
1929 clear_string_option(&buf->b_p_mps);
1930 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001931 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 clear_string_option(&buf->b_p_isk);
1933#ifdef FEAT_KEYMAP
1934 clear_string_option(&buf->b_p_keymap);
1935 ga_clear(&buf->b_kmap_ga);
1936#endif
1937#ifdef FEAT_COMMENTS
1938 clear_string_option(&buf->b_p_com);
1939#endif
1940#ifdef FEAT_FOLDING
1941 clear_string_option(&buf->b_p_cms);
1942#endif
1943 clear_string_option(&buf->b_p_nf);
1944#ifdef FEAT_SYN_HL
1945 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001946 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001947#endif
1948#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001949 clear_string_option(&buf->b_s.b_p_spc);
1950 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001951 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001952 buf->b_s.b_cap_prog = NULL;
1953 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#endif
1955#ifdef FEAT_SEARCHPATH
1956 clear_string_option(&buf->b_p_sua);
1957#endif
1958#ifdef FEAT_AUTOCMD
1959 clear_string_option(&buf->b_p_ft);
1960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#ifdef FEAT_CINDENT
1962 clear_string_option(&buf->b_p_cink);
1963 clear_string_option(&buf->b_p_cino);
1964#endif
1965#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1966 clear_string_option(&buf->b_p_cinw);
1967#endif
1968#ifdef FEAT_INS_EXPAND
1969 clear_string_option(&buf->b_p_cpt);
1970#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001971#ifdef FEAT_COMPL_FUNC
1972 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001973 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975#ifdef FEAT_QUICKFIX
1976 clear_string_option(&buf->b_p_gp);
1977 clear_string_option(&buf->b_p_mp);
1978 clear_string_option(&buf->b_p_efm);
1979#endif
1980 clear_string_option(&buf->b_p_ep);
1981 clear_string_option(&buf->b_p_path);
1982 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01001983 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#ifdef FEAT_INS_EXPAND
1985 clear_string_option(&buf->b_p_dict);
1986 clear_string_option(&buf->b_p_tsr);
1987#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001988#ifdef FEAT_TEXTOBJ
1989 clear_string_option(&buf->b_p_qe);
1990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001992 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01001993#ifdef FEAT_LISP
1994 clear_string_option(&buf->b_p_lw);
1995#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02001996 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997}
1998
1999/*
2000 * get alternate file n
2001 * set linenr to lnum or altfpos.lnum if lnum == 0
2002 * also set cursor column to altfpos.col if 'startofline' is not set.
2003 * if (options & GETF_SETMARK) call setpcmark()
2004 * if (options & GETF_ALT) we are jumping to an alternate file.
2005 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2006 *
2007 * return FAIL for failure, OK for success
2008 */
2009 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002010buflist_getfile(
2011 int n,
2012 linenr_T lnum,
2013 int options,
2014 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 buf_T *buf;
2017#ifdef FEAT_WINDOWS
2018 win_T *wp = NULL;
2019#endif
2020 pos_T *fpos;
2021 colnr_T col;
2022
2023 buf = buflist_findnr(n);
2024 if (buf == NULL)
2025 {
2026 if ((options & GETF_ALT) && n == 0)
2027 EMSG(_(e_noalt));
2028 else
2029 EMSGN(_("E92: Buffer %ld not found"), n);
2030 return FAIL;
2031 }
2032
2033 /* if alternate file is the current buffer, nothing to do */
2034 if (buf == curbuf)
2035 return OK;
2036
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002037 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002038 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002039 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002041 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002042#ifdef FEAT_AUTOCMD
2043 if (curbuf_locked())
2044 return FAIL;
2045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 /* altfpos may be changed by getfile(), get it now */
2048 if (lnum == 0)
2049 {
2050 fpos = buflist_findfpos(buf);
2051 lnum = fpos->lnum;
2052 col = fpos->col;
2053 }
2054 else
2055 col = 0;
2056
2057#ifdef FEAT_WINDOWS
2058 if (options & GETF_SWITCH)
2059 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002060 /* If 'switchbuf' contains "useopen": jump to first window containing
2061 * "buf" if one exists */
2062 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002064
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002065 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002066 * page containing "buf" if one exists */
2067 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002068 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002069
2070 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2071 * current buffer isn't empty: open new tab or window */
2072 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2073 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002075 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002076 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002077 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2078 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002080 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082 }
2083#endif
2084
2085 ++RedrawingDisabled;
2086 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2087 lnum, forceit) <= 0)
2088 {
2089 --RedrawingDisabled;
2090
2091 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2092 if (!p_sol && col != 0)
2093 {
2094 curwin->w_cursor.col = col;
2095 check_cursor_col();
2096#ifdef FEAT_VIRTUALEDIT
2097 curwin->w_cursor.coladd = 0;
2098#endif
2099 curwin->w_set_curswant = TRUE;
2100 }
2101 return OK;
2102 }
2103 --RedrawingDisabled;
2104 return FAIL;
2105}
2106
2107/*
2108 * go to the last know line number for the current buffer
2109 */
2110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002111buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 pos_T *fpos;
2114
2115 fpos = buflist_findfpos(curbuf);
2116
2117 curwin->w_cursor.lnum = fpos->lnum;
2118 check_cursor_lnum();
2119
2120 if (p_sol)
2121 curwin->w_cursor.col = 0;
2122 else
2123 {
2124 curwin->w_cursor.col = fpos->col;
2125 check_cursor_col();
2126#ifdef FEAT_VIRTUALEDIT
2127 curwin->w_cursor.coladd = 0;
2128#endif
2129 curwin->w_set_curswant = TRUE;
2130 }
2131}
2132
Bram Moolenaar81695252004-12-29 20:58:21 +00002133#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2134/*
2135 * Find file in buffer list by name (it has to be for the current window).
2136 * Returns NULL if not found.
2137 */
2138 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002139buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002140{
2141 char_u *ffname;
2142 buf_T *buf = NULL;
2143
2144 /* First make the name into a full path name */
2145 ffname = FullName_save(fname,
2146#ifdef UNIX
2147 TRUE /* force expansion, get rid of symbolic links */
2148#else
2149 FALSE
2150#endif
2151 );
2152 if (ffname != NULL)
2153 {
2154 buf = buflist_findname(ffname);
2155 vim_free(ffname);
2156 }
2157 return buf;
2158}
2159#endif
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161/*
2162 * Find file in buffer list by name (it has to be for the current window).
2163 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002164 * Skips dummy buffers.
2165 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 */
2167 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002168buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
2170#ifdef UNIX
2171 struct stat st;
2172
2173 if (mch_stat((char *)ffname, &st) < 0)
2174 st.st_dev = (dev_T)-1;
2175 return buflist_findname_stat(ffname, &st);
2176}
2177
2178/*
2179 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2180 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002181 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 */
2183 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002184buflist_findname_stat(
2185 char_u *ffname,
2186 struct stat *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
2188#endif
2189 buf_T *buf;
2190
2191 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002192 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193#ifdef UNIX
2194 , stp
2195#endif
2196 ))
2197 return buf;
2198 return NULL;
2199}
2200
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002201#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2202 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203/*
2204 * Find file in buffer list by a regexp pattern.
2205 * Return fnum of the found buffer.
2206 * Return < 0 for error.
2207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002209buflist_findpat(
2210 char_u *pattern,
2211 char_u *pattern_end, /* pointer to first char after pattern */
2212 int unlisted, /* find unlisted buffers */
2213 int diffmode UNUSED, /* find diff-mode buffers only */
2214 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 int match = -1;
2218 int find_listed;
2219 char_u *pat;
2220 char_u *patend;
2221 int attempt;
2222 char_u *p;
2223 int toggledollar;
2224
2225 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2226 {
2227 if (*pattern == '%')
2228 match = curbuf->b_fnum;
2229 else
2230 match = curwin->w_alt_fnum;
2231#ifdef FEAT_DIFF
2232 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2233 match = -1;
2234#endif
2235 }
2236
2237 /*
2238 * Try four ways of matching a listed buffer:
2239 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002240 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 * attempt == 2: with '$' at end (only match at end)
2242 * attempt == 3: with '^' at start and '$' at end (only full match)
2243 * Repeat this for finding an unlisted buffer if there was no matching
2244 * listed buffer.
2245 */
2246 else
2247 {
2248 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2249 if (pat == NULL)
2250 return -1;
2251 patend = pat + STRLEN(pat) - 1;
2252 toggledollar = (patend > pat && *patend == '$');
2253
2254 /* First try finding a listed buffer. If not found and "unlisted"
2255 * is TRUE, try finding an unlisted buffer. */
2256 find_listed = TRUE;
2257 for (;;)
2258 {
2259 for (attempt = 0; attempt <= 3; ++attempt)
2260 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002261 regmatch_T regmatch;
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 /* may add '^' and '$' */
2264 if (toggledollar)
2265 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2266 p = pat;
2267 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2268 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002269 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2270 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 vim_free(pat);
2273 return -1;
2274 }
2275
2276 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2277 if (buf->b_p_bl == find_listed
2278#ifdef FEAT_DIFF
2279 && (!diffmode || diff_mode_buf(buf))
2280#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002281 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002283 if (curtab_only)
2284 {
2285 /* Ignore the match if the buffer is not open in
2286 * the current tab. */
2287#ifdef FEAT_WINDOWS
2288 win_T *wp;
2289
2290 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2291 if (wp->w_buffer == buf)
2292 break;
2293 if (wp == NULL)
2294 continue;
2295#else
2296 if (curwin->w_buffer != buf)
2297 continue;
2298#endif
2299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (match >= 0) /* already found a match */
2301 {
2302 match = -2;
2303 break;
2304 }
2305 match = buf->b_fnum; /* remember first match */
2306 }
2307
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002308 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (match >= 0) /* found one match */
2310 break;
2311 }
2312
2313 /* Only search for unlisted buffers if there was no match with
2314 * a listed buffer. */
2315 if (!unlisted || !find_listed || match != -1)
2316 break;
2317 find_listed = FALSE;
2318 }
2319
2320 vim_free(pat);
2321 }
2322
2323 if (match == -2)
2324 EMSG2(_("E93: More than one match for %s"), pattern);
2325 else if (match < 0)
2326 EMSG2(_("E94: No matching buffer for %s"), pattern);
2327 return match;
2328}
2329#endif
2330
2331#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2332
2333/*
2334 * Find all buffer names that match.
2335 * For command line expansion of ":buf" and ":sbuf".
2336 * Return OK if matches found, FAIL otherwise.
2337 */
2338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002339ExpandBufnames(
2340 char_u *pat,
2341 int *num_file,
2342 char_u ***file,
2343 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344{
2345 int count = 0;
2346 buf_T *buf;
2347 int round;
2348 char_u *p;
2349 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002350 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352 *num_file = 0; /* return values in case of FAIL */
2353 *file = NULL;
2354
Bram Moolenaar05159a02005-02-26 23:04:13 +00002355 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2356 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002358 patc = alloc((unsigned)STRLEN(pat) + 11);
2359 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002361 STRCPY(patc, "\\(^\\|[\\/]\\)");
2362 STRCPY(patc + 11, pat + 1);
2363 }
2364 else
2365 patc = pat;
2366
2367 /*
2368 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002369 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002370 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002371 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002372 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002373 regmatch_T regmatch;
2374
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002375 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002376 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002377 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2378 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002379 {
2380 if (patc != pat)
2381 vim_free(patc);
2382 return FAIL;
2383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 /*
2386 * round == 1: Count the matches.
2387 * round == 2: Build the array to keep the matches.
2388 */
2389 for (round = 1; round <= 2; ++round)
2390 {
2391 count = 0;
2392 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2393 {
2394 if (!buf->b_p_bl) /* skip unlisted buffers */
2395 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002396 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (p != NULL)
2398 {
2399 if (round == 1)
2400 ++count;
2401 else
2402 {
2403 if (options & WILD_HOME_REPLACE)
2404 p = home_replace_save(buf, p);
2405 else
2406 p = vim_strsave(p);
2407 (*file)[count++] = p;
2408 }
2409 }
2410 }
2411 if (count == 0) /* no match found, break here */
2412 break;
2413 if (round == 1)
2414 {
2415 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2416 if (*file == NULL)
2417 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002418 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002419 if (patc != pat)
2420 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 return FAIL;
2422 }
2423 }
2424 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002425 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (count) /* match(es) found, break here */
2427 break;
2428 }
2429
Bram Moolenaar05159a02005-02-26 23:04:13 +00002430 if (patc != pat)
2431 vim_free(patc);
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 *num_file = count;
2434 return (count == 0 ? FAIL : OK);
2435}
2436
2437#endif /* FEAT_CMDL_COMPL */
2438
2439#ifdef HAVE_BUFLIST_MATCH
2440/*
2441 * Check for a match on the file name for buffer "buf" with regprog "prog".
2442 */
2443 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002444buflist_match(
2445 regmatch_T *rmp,
2446 buf_T *buf,
2447 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
2449 char_u *match;
2450
2451 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002452 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002454 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
2456 return match;
2457}
2458
2459/*
2460 * Try matching the regexp in "prog" with file name "name".
2461 * Return "name" when there is a match, NULL when not.
2462 */
2463 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002464fname_match(
2465 regmatch_T *rmp,
2466 char_u *name,
2467 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 char_u *match = NULL;
2470 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 if (name != NULL)
2473 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002474 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002475 rmp->rm_ic = p_fic || ignore_case;
2476 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 match = name;
2478 else
2479 {
2480 /* Replace $(HOME) with '~' and try matching again. */
2481 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002482 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 match = name;
2484 vim_free(p);
2485 }
2486 }
2487
2488 return match;
2489}
2490#endif
2491
2492/*
2493 * find file in buffer list by number
2494 */
2495 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002496buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 buf_T *buf;
2499
2500 if (nr == 0)
2501 nr = curwin->w_alt_fnum;
2502 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2503 if (buf->b_fnum == nr)
2504 return (buf);
2505 return NULL;
2506}
2507
2508/*
2509 * Get name of file 'n' in the buffer list.
2510 * When the file has no name an empty string is returned.
2511 * home_replace() is used to shorten the file name (used for marks).
2512 * Returns a pointer to allocated memory, of NULL when failed.
2513 */
2514 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002515buflist_nr2name(
2516 int n,
2517 int fullname,
2518 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
2520 buf_T *buf;
2521
2522 buf = buflist_findnr(n);
2523 if (buf == NULL)
2524 return NULL;
2525 return home_replace_save(helptail ? buf : NULL,
2526 fullname ? buf->b_ffname : buf->b_fname);
2527}
2528
2529/*
2530 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2531 * When "copy_options" is TRUE save the local window option values.
2532 * When "lnum" is 0 only do the options.
2533 */
2534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002535buflist_setfpos(
2536 buf_T *buf,
2537 win_T *win,
2538 linenr_T lnum,
2539 colnr_T col,
2540 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 wininfo_T *wip;
2543
2544 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2545 if (wip->wi_win == win)
2546 break;
2547 if (wip == NULL)
2548 {
2549 /* allocate a new entry */
2550 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2551 if (wip == NULL)
2552 return;
2553 wip->wi_win = win;
2554 if (lnum == 0) /* set lnum even when it's 0 */
2555 lnum = 1;
2556 }
2557 else
2558 {
2559 /* remove the entry from the list */
2560 if (wip->wi_prev)
2561 wip->wi_prev->wi_next = wip->wi_next;
2562 else
2563 buf->b_wininfo = wip->wi_next;
2564 if (wip->wi_next)
2565 wip->wi_next->wi_prev = wip->wi_prev;
2566 if (copy_options && wip->wi_optset)
2567 {
2568 clear_winopt(&wip->wi_opt);
2569#ifdef FEAT_FOLDING
2570 deleteFoldRecurse(&wip->wi_folds);
2571#endif
2572 }
2573 }
2574 if (lnum != 0)
2575 {
2576 wip->wi_fpos.lnum = lnum;
2577 wip->wi_fpos.col = col;
2578 }
2579 if (copy_options)
2580 {
2581 /* Save the window-specific option values. */
2582 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2583#ifdef FEAT_FOLDING
2584 wip->wi_fold_manual = win->w_fold_manual;
2585 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2586#endif
2587 wip->wi_optset = TRUE;
2588 }
2589
2590 /* insert the entry in front of the list */
2591 wip->wi_next = buf->b_wininfo;
2592 buf->b_wininfo = wip;
2593 wip->wi_prev = NULL;
2594 if (wip->wi_next)
2595 wip->wi_next->wi_prev = wip;
2596
2597 return;
2598}
2599
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002600#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002601static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002602
2603/*
2604 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2605 * page. That's because a diff is local to a tab page.
2606 */
2607 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002608wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002609{
2610 win_T *wp;
2611
2612 if (wip->wi_opt.wo_diff)
2613 {
2614 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2615 /* return FALSE when it's a window in the current tab page, thus
2616 * the buffer was in diff mode here */
2617 if (wip->wi_win == wp)
2618 return FALSE;
2619 return TRUE;
2620 }
2621 return FALSE;
2622}
2623#endif
2624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625/*
2626 * Find info for the current window in buffer "buf".
2627 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002628 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2629 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 * Returns NULL when there isn't any info.
2631 */
2632 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002633find_wininfo(
2634 buf_T *buf,
2635 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 wininfo_T *wip;
2638
2639 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002640 if (wip->wi_win == curwin
2641#ifdef FEAT_DIFF
2642 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2643#endif
2644 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002646
2647 /* If no wininfo for curwin, use the first in the list (that doesn't have
2648 * 'diff' set and is in another tab page). */
2649 if (wip == NULL)
2650 {
2651#ifdef FEAT_DIFF
2652 if (skip_diff_buffer)
2653 {
2654 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2655 if (!wininfo_other_tab_diff(wip))
2656 break;
2657 }
2658 else
2659#endif
2660 wip = buf->b_wininfo;
2661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 return wip;
2663}
2664
2665/*
2666 * Reset the local window options to the values last used in this window.
2667 * If the buffer wasn't used in this window before, use the values from
2668 * the most recently used window. If the values were never set, use the
2669 * global values for the window.
2670 */
2671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002672get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 wininfo_T *wip;
2675
2676 clear_winopt(&curwin->w_onebuf_opt);
2677#ifdef FEAT_FOLDING
2678 clearFolding(curwin);
2679#endif
2680
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002681 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (wip != NULL && wip->wi_optset)
2683 {
2684 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2685#ifdef FEAT_FOLDING
2686 curwin->w_fold_manual = wip->wi_fold_manual;
2687 curwin->w_foldinvalid = TRUE;
2688 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2689#endif
2690 }
2691 else
2692 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2693
2694#ifdef FEAT_FOLDING
2695 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2696 if (p_fdls >= 0)
2697 curwin->w_p_fdl = p_fdls;
2698#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002699#ifdef FEAT_SYN_HL
2700 check_colorcolumn(curwin);
2701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702}
2703
2704/*
2705 * Find the position (lnum and col) for the buffer 'buf' for the current
2706 * window.
2707 * Returns a pointer to no_position if no position is found.
2708 */
2709 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002710buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002713 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002715 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (wip != NULL)
2717 return &(wip->wi_fpos);
2718 else
2719 return &no_position;
2720}
2721
2722/*
2723 * Find the lnum for the buffer 'buf' for the current window.
2724 */
2725 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002726buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
2728 return buflist_findfpos(buf)->lnum;
2729}
2730
2731#if defined(FEAT_LISTCMDS) || defined(PROTO)
2732/*
2733 * List all know file names (for :files and :buffers command).
2734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002736buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737{
2738 buf_T *buf;
2739 int len;
2740 int i;
2741
2742 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2743 {
2744 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002745 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2746 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2747 || (vim_strchr(eap->arg, '+')
2748 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2749 || (vim_strchr(eap->arg, 'a')
2750 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2751 || (vim_strchr(eap->arg, 'h')
2752 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2753 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2754 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2755 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2756 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2757 || (vim_strchr(eap->arg, '#')
2758 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 continue;
2760 msg_putchar('\n');
2761 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002762 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else
2764 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2765
Bram Moolenaar50cde822005-06-05 21:54:54 +00002766 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 buf->b_fnum,
2768 buf->b_p_bl ? ' ' : 'u',
2769 buf == curbuf ? '%' :
2770 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2771 buf->b_ml.ml_mfp == NULL ? ' ' :
2772 (buf->b_nwindows == 0 ? 'h' : 'a'),
2773 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2774 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002775 : (bufIsChanged(buf) ? '+' : ' '),
2776 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002777 if (len > IOSIZE - 20)
2778 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
2780 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 i = 40 - vim_strsize(IObuff);
2782 do
2783 {
2784 IObuff[len++] = ' ';
2785 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002786 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2787 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002788 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 msg_outtrans(IObuff);
2790 out_flush(); /* output one line at a time */
2791 ui_breakcheck();
2792 }
2793}
2794#endif
2795
2796/*
2797 * Get file name and line number for file 'fnum'.
2798 * Used by DoOneCmd() for translating '%' and '#'.
2799 * Used by insert_reg() and cmdline_paste() for '#' register.
2800 * Return FAIL if not found, OK for success.
2801 */
2802 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002803buflist_name_nr(
2804 int fnum,
2805 char_u **fname,
2806 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
2808 buf_T *buf;
2809
2810 buf = buflist_findnr(fnum);
2811 if (buf == NULL || buf->b_fname == NULL)
2812 return FAIL;
2813
2814 *fname = buf->b_fname;
2815 *lnum = buflist_findlnum(buf);
2816
2817 return OK;
2818}
2819
2820/*
2821 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2822 * The file name with the full path is also remembered, for when :cd is used.
2823 * Returns FAIL for failure (file name already in use by other buffer)
2824 * OK otherwise.
2825 */
2826 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002827setfname(
2828 buf_T *buf,
2829 char_u *ffname,
2830 char_u *sfname,
2831 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
Bram Moolenaar81695252004-12-29 20:58:21 +00002833 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834#ifdef UNIX
2835 struct stat st;
2836#endif
2837
2838 if (ffname == NULL || *ffname == NUL)
2839 {
2840 /* Removing the name. */
2841 vim_free(buf->b_ffname);
2842 vim_free(buf->b_sfname);
2843 buf->b_ffname = NULL;
2844 buf->b_sfname = NULL;
2845#ifdef UNIX
2846 st.st_dev = (dev_T)-1;
2847#endif
2848 }
2849 else
2850 {
2851 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2852 if (ffname == NULL) /* out of memory */
2853 return FAIL;
2854
2855 /*
2856 * if the file name is already used in another buffer:
2857 * - if the buffer is loaded, fail
2858 * - if the buffer is not loaded, delete it from the list
2859 */
2860#ifdef UNIX
2861 if (mch_stat((char *)ffname, &st) < 0)
2862 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002863#endif
2864 if (!(buf->b_flags & BF_DUMMY))
2865#ifdef UNIX
2866 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002868 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869#endif
2870 if (obuf != NULL && obuf != buf)
2871 {
2872 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2873 {
2874 if (message)
2875 EMSG(_("E95: Buffer with this name already exists"));
2876 vim_free(ffname);
2877 return FAIL;
2878 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002879 /* delete from the list */
2880 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882 sfname = vim_strsave(sfname);
2883 if (ffname == NULL || sfname == NULL)
2884 {
2885 vim_free(sfname);
2886 vim_free(ffname);
2887 return FAIL;
2888 }
2889#ifdef USE_FNAME_CASE
2890# ifdef USE_LONG_FNAME
2891 if (USE_LONG_FNAME)
2892# endif
2893 fname_case(sfname, 0); /* set correct case for short file name */
2894#endif
2895 vim_free(buf->b_ffname);
2896 vim_free(buf->b_sfname);
2897 buf->b_ffname = ffname;
2898 buf->b_sfname = sfname;
2899 }
2900 buf->b_fname = buf->b_sfname;
2901#ifdef UNIX
2902 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002903 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 else
2905 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002906 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 buf->b_dev = st.st_dev;
2908 buf->b_ino = st.st_ino;
2909 }
2910#endif
2911
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 buf_name_changed(buf);
2915 return OK;
2916}
2917
2918/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002919 * Crude way of changing the name of a buffer. Use with care!
2920 * The name should be relative to the current directory.
2921 */
2922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002923buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002924{
2925 buf_T *buf;
2926
2927 buf = buflist_findnr(fnum);
2928 if (buf != NULL)
2929 {
2930 vim_free(buf->b_sfname);
2931 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002932 buf->b_ffname = vim_strsave(name);
2933 buf->b_sfname = NULL;
2934 /* Allocate ffname and expand into full path. Also resolves .lnk
2935 * files on Win32. */
2936 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002937 buf->b_fname = buf->b_sfname;
2938 }
2939}
2940
2941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 * Take care of what needs to be done when the name of buffer "buf" has
2943 * changed.
2944 */
2945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002946buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 /*
2949 * If the file name changed, also change the name of the swapfile
2950 */
2951 if (buf->b_ml.ml_mfp != NULL)
2952 ml_setname(buf);
2953
2954 if (curwin->w_buffer == buf)
2955 check_arg_idx(curwin); /* check file name for arg list */
2956#ifdef FEAT_TITLE
2957 maketitle(); /* set window title */
2958#endif
2959#ifdef FEAT_WINDOWS
2960 status_redraw_all(); /* status lines need to be redrawn */
2961#endif
2962 fmarks_check_names(buf); /* check named file marks */
2963 ml_timestamp(buf); /* reset timestamp */
2964}
2965
2966/*
2967 * set alternate file name for current window
2968 *
2969 * Used by do_one_cmd(), do_write() and do_ecmd().
2970 * Return the buffer.
2971 */
2972 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002973setaltfname(
2974 char_u *ffname,
2975 char_u *sfname,
2976 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
2978 buf_T *buf;
2979
2980 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2981 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002982 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 curwin->w_alt_fnum = buf->b_fnum;
2984 return buf;
2985}
2986
2987/*
2988 * Get alternate file name for current window.
2989 * Return NULL if there isn't any, and give error message if requested.
2990 */
2991 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002992getaltfname(
2993 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 char_u *fname;
2996 linenr_T dummy;
2997
2998 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2999 {
3000 if (errmsg)
3001 EMSG(_(e_noalt));
3002 return NULL;
3003 }
3004 return fname;
3005}
3006
3007/*
3008 * Add a file name to the buflist and return its number.
3009 * Uses same flags as buflist_new(), except BLN_DUMMY.
3010 *
3011 * used by qf_init(), main() and doarglist()
3012 */
3013 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003014buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
3016 buf_T *buf;
3017
3018 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3019 if (buf != NULL)
3020 return buf->b_fnum;
3021 return 0;
3022}
3023
3024#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3025/*
3026 * Adjust slashes in file names. Called after 'shellslash' was set.
3027 */
3028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003029buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 buf_T *bp;
3032
3033 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3034 {
3035 if (bp->b_ffname != NULL)
3036 slash_adjust(bp->b_ffname);
3037 if (bp->b_sfname != NULL)
3038 slash_adjust(bp->b_sfname);
3039 }
3040}
3041#endif
3042
3043/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003044 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 * Also save the local window option values.
3046 */
3047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003048buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003050 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051}
3052
3053/*
3054 * Return TRUE if 'ffname' is not the same file as current file.
3055 * Fname must have a full path (expanded by mch_FullName()).
3056 */
3057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003058otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 return otherfile_buf(curbuf, ffname
3061#ifdef UNIX
3062 , NULL
3063#endif
3064 );
3065}
3066
3067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003068otherfile_buf(
3069 buf_T *buf,
3070 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#ifdef UNIX
Bram Moolenaar7454a062016-01-30 15:14:10 +01003072 , struct stat *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003074 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
3076 /* no name is different */
3077 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3078 return TRUE;
3079 if (fnamecmp(ffname, buf->b_ffname) == 0)
3080 return FALSE;
3081#ifdef UNIX
3082 {
3083 struct stat st;
3084
3085 /* If no struct stat given, get it now */
3086 if (stp == NULL)
3087 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003088 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 st.st_dev = (dev_T)-1;
3090 stp = &st;
3091 }
3092 /* Use dev/ino to check if the files are the same, even when the names
3093 * are different (possible with links). Still need to compare the
3094 * name above, for when the file doesn't exist yet.
3095 * Problem: The dev/ino changes when a file is deleted (and created
3096 * again) and remains the same when renamed/moved. We don't want to
3097 * mch_stat() each buffer each time, that would be too slow. Get the
3098 * dev/ino again when they appear to match, but not when they appear
3099 * to be different: Could skip a buffer when it's actually the same
3100 * file. */
3101 if (buf_same_ino(buf, stp))
3102 {
3103 buf_setino(buf);
3104 if (buf_same_ino(buf, stp))
3105 return FALSE;
3106 }
3107 }
3108#endif
3109 return TRUE;
3110}
3111
3112#if defined(UNIX) || defined(PROTO)
3113/*
3114 * Set inode and device number for a buffer.
3115 * Must always be called when b_fname is changed!.
3116 */
3117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003118buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
3120 struct stat st;
3121
3122 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3123 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003124 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 buf->b_dev = st.st_dev;
3126 buf->b_ino = st.st_ino;
3127 }
3128 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003129 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130}
3131
3132/*
3133 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3134 */
3135 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003136buf_same_ino(
3137 buf_T *buf,
3138 struct stat *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003140 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 && stp->st_dev == buf->b_dev
3142 && stp->st_ino == buf->b_ino);
3143}
3144#endif
3145
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003146/*
3147 * Print info about the current buffer.
3148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003150fileinfo(
3151 int fullname, /* when non-zero print full path */
3152 int shorthelp,
3153 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155 char_u *name;
3156 int n;
3157 char_u *p;
3158 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003159 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161 buffer = alloc(IOSIZE);
3162 if (buffer == NULL)
3163 return;
3164
3165 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3166 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003167 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 p = buffer + STRLEN(buffer);
3169 }
3170 else
3171 p = buffer;
3172
3173 *p++ = '"';
3174 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003175 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 else
3177 {
3178 if (!fullname && curbuf->b_fname != NULL)
3179 name = curbuf->b_fname;
3180 else
3181 name = curbuf->b_ffname;
3182 home_replace(shorthelp ? curbuf : NULL, name, p,
3183 (int)(IOSIZE - (p - buffer)), TRUE);
3184 }
3185
Bram Moolenaara800b422010-06-27 01:15:55 +02003186 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 curbufIsChanged() ? (shortmess(SHM_MOD)
3188 ? " [+]" : _(" [Modified]")) : " ",
3189 (curbuf->b_flags & BF_NOTEDITED)
3190#ifdef FEAT_QUICKFIX
3191 && !bt_dontwrite(curbuf)
3192#endif
3193 ? _("[Not edited]") : "",
3194 (curbuf->b_flags & BF_NEW)
3195#ifdef FEAT_QUICKFIX
3196 && !bt_dontwrite(curbuf)
3197#endif
3198 ? _("[New file]") : "",
3199 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003200 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 : _("[readonly]")) : "",
3202 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3203 || curbuf->b_p_ro) ?
3204 " " : "");
3205 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3206 * causes an overflow, thus for large numbers divide instead. */
3207 if (curwin->w_cursor.lnum > 1000000L)
3208 n = (int)(((long)curwin->w_cursor.lnum) /
3209 ((long)curbuf->b_ml.ml_line_count / 100L));
3210 else
3211 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3212 (long)curbuf->b_ml.ml_line_count);
3213 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3214 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003215 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
3217#ifdef FEAT_CMDL_INFO
3218 else if (p_ru)
3219 {
3220 /* Current line and column are already on the screen -- webb */
3221 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003222 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003224 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 (long)curbuf->b_ml.ml_line_count, n);
3226 }
3227#endif
3228 else
3229 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003230 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 _("line %ld of %ld --%d%%-- col "),
3232 (long)curwin->w_cursor.lnum,
3233 (long)curbuf->b_ml.ml_line_count,
3234 n);
3235 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003236 len = STRLEN(buffer);
3237 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3239 }
3240
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003241 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
3243 if (dont_truncate)
3244 {
3245 /* Temporarily set msg_scroll to avoid the message being truncated.
3246 * First call msg_start() to get the message in the right place. */
3247 msg_start();
3248 n = msg_scroll;
3249 msg_scroll = TRUE;
3250 msg(buffer);
3251 msg_scroll = n;
3252 }
3253 else
3254 {
3255 p = msg_trunc_attr(buffer, FALSE, 0);
3256 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 /* Need to repeat the message after redrawing when:
3258 * - When restart_edit is set (otherwise there will be a delay
3259 * before redrawing).
3260 * - When the screen was scrolled but there is no wait-return
3261 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003262 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
3264
3265 vim_free(buffer);
3266}
3267
3268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003269col_print(
3270 char_u *buf,
3271 size_t buflen,
3272 int col,
3273 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
3275 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003276 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003278 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279}
3280
3281#if defined(FEAT_TITLE) || defined(PROTO)
3282/*
3283 * put file name in title bar of window and in icon title
3284 */
3285
3286static char_u *lasttitle = NULL;
3287static char_u *lasticon = NULL;
3288
3289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003290maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
3292 char_u *p;
3293 char_u *t_str = NULL;
3294 char_u *i_name;
3295 char_u *i_str = NULL;
3296 int maxlen = 0;
3297 int len;
3298 int mustset;
3299 char_u buf[IOSIZE];
3300 int off;
3301
3302 if (!redrawing())
3303 {
3304 /* Postpone updating the title when 'lazyredraw' is set. */
3305 need_maketitle = TRUE;
3306 return;
3307 }
3308
3309 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003310 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return;
3312
3313 if (p_title)
3314 {
3315 if (p_titlelen > 0)
3316 {
3317 maxlen = p_titlelen * Columns / 100;
3318 if (maxlen < 10)
3319 maxlen = 10;
3320 }
3321
3322 t_str = buf;
3323 if (*p_titlestring != NUL)
3324 {
3325#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003326 if (stl_syntax & STL_IN_TITLE)
3327 {
3328 int use_sandbox = FALSE;
3329 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003330
3331# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003332 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003333# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003334 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003336 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003337 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003338 if (called_emsg)
3339 set_string_option_direct((char_u *)"titlestring", -1,
3340 (char_u *)"", OPT_FREE, SID_ERROR);
3341 called_emsg |= save_called_emsg;
3342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 else
3344#endif
3345 t_str = p_titlestring;
3346 }
3347 else
3348 {
3349 /* format: "fname + (path) (1 of 2) - VIM" */
3350
Bram Moolenaar2c666692012-09-05 13:30:40 +02003351#define SPACE_FOR_FNAME (IOSIZE - 100)
3352#define SPACE_FOR_DIR (IOSIZE - 20)
3353#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003355 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 else
3357 {
3358 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003359 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
3362
3363 switch (bufIsChanged(curbuf)
3364 + (curbuf->b_p_ro * 2)
3365 + (!curbuf->b_p_ma * 4))
3366 {
3367 case 1: STRCAT(buf, " +"); break;
3368 case 2: STRCAT(buf, " ="); break;
3369 case 3: STRCAT(buf, " =+"); break;
3370 case 4:
3371 case 6: STRCAT(buf, " -"); break;
3372 case 5:
3373 case 7: STRCAT(buf, " -+"); break;
3374 }
3375
3376 if (curbuf->b_fname != NULL)
3377 {
3378 /* Get path of file, replace home dir with ~ */
3379 off = (int)STRLEN(buf);
3380 buf[off++] = ' ';
3381 buf[off++] = '(';
3382 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003383 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384#ifdef BACKSLASH_IN_FILENAME
3385 /* avoid "c:/name" to be reduced to "c" */
3386 if (isalpha(buf[off]) && buf[off + 1] == ':')
3387 off += 2;
3388#endif
3389 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003390 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003393 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003394 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003397
Bram Moolenaar2c666692012-09-05 13:30:40 +02003398 /* Translate unprintable chars and concatenate. Keep some
3399 * room for the server name. When there is no room (very long
3400 * file name) use (...). */
3401 if (off < SPACE_FOR_DIR)
3402 {
3403 p = transstr(buf + off);
3404 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3405 vim_free(p);
3406 }
3407 else
3408 {
3409 vim_strncpy(buf + off, (char_u *)"...",
3410 (size_t)(SPACE_FOR_ARGNR - off));
3411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 STRCAT(buf, ")");
3413 }
3414
Bram Moolenaar2c666692012-09-05 13:30:40 +02003415 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417#if defined(FEAT_CLIENTSERVER)
3418 if (serverName != NULL)
3419 {
3420 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003421 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 else
3424#endif
3425 STRCAT(buf, " - VIM");
3426
3427 if (maxlen > 0)
3428 {
3429 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003430 if (vim_strsize(buf) > maxlen)
3431 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433 }
3434 }
3435 mustset = ti_change(t_str, &lasttitle);
3436
3437 if (p_icon)
3438 {
3439 i_str = buf;
3440 if (*p_iconstring != NUL)
3441 {
3442#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003443 if (stl_syntax & STL_IN_ICON)
3444 {
3445 int use_sandbox = FALSE;
3446 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003447
3448# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003449 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003450# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003451 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003453 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003454 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003455 if (called_emsg)
3456 set_string_option_direct((char_u *)"iconstring", -1,
3457 (char_u *)"", OPT_FREE, SID_ERROR);
3458 called_emsg |= save_called_emsg;
3459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 else
3461#endif
3462 i_str = p_iconstring;
3463 }
3464 else
3465 {
3466 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003467 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 else /* use file name only in icon */
3469 i_name = gettail(curbuf->b_ffname);
3470 *i_str = NUL;
3471 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (len > 100)
3474 {
3475 len -= 100;
3476#ifdef FEAT_MBYTE
3477 if (has_mbyte)
3478 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3479#endif
3480 i_name += len;
3481 }
3482 STRCPY(i_str, i_name);
3483 trans_characters(i_str, IOSIZE);
3484 }
3485 }
3486
3487 mustset |= ti_change(i_str, &lasticon);
3488
3489 if (mustset)
3490 resettitle();
3491}
3492
3493/*
3494 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3495 * from "str" if it does.
3496 * Return TRUE when "*last" changed.
3497 */
3498 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003499ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 if ((str == NULL) != (*last == NULL)
3502 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3503 {
3504 vim_free(*last);
3505 if (str == NULL)
3506 *last = NULL;
3507 else
3508 *last = vim_strsave(str);
3509 return TRUE;
3510 }
3511 return FALSE;
3512}
3513
3514/*
3515 * Put current window title back (used after calling a shell)
3516 */
3517 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003518resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 mch_settitle(lasttitle, lasticon);
3521}
Bram Moolenaarea408852005-06-25 22:49:46 +00003522
3523# if defined(EXITFREE) || defined(PROTO)
3524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003525free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003526{
3527 vim_free(lasttitle);
3528 vim_free(lasticon);
3529}
3530# endif
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532#endif /* FEAT_TITLE */
3533
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003534#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003536 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 * Return length of string in screen cells.
3538 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003539 * Normally works for window "wp", except when working for 'tabline' then it
3540 * is "curwin".
3541 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 * Items are drawn interspersed with the text that surrounds it
3543 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3544 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3545 *
3546 * If maxwidth is not zero, the string will be filled at any middle marker
3547 * or truncated if too long, fillchar is used for all whitespace.
3548 */
3549 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003550build_stl_str_hl(
3551 win_T *wp,
3552 char_u *out, /* buffer to write into != NameBuff */
3553 size_t outlen, /* length of out[] */
3554 char_u *fmt,
3555 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3556 int fillchar,
3557 int maxwidth,
3558 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3559 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 char_u *p;
3562 char_u *s;
3563 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003564 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565#ifdef FEAT_EVAL
3566 win_T *o_curwin;
3567 buf_T *o_curbuf;
3568#endif
3569 int empty_line;
3570 colnr_T virtcol;
3571 long l;
3572 long n;
3573 int prevchar_isflag;
3574 int prevchar_isitem;
3575 int itemisflag;
3576 int fillable;
3577 char_u *str;
3578 long num;
3579 int width;
3580 int itemcnt;
3581 int curitem;
3582 int groupitem[STL_MAX_ITEM];
3583 int groupdepth;
3584 struct stl_item
3585 {
3586 char_u *start;
3587 int minwid;
3588 int maxwid;
3589 enum
3590 {
3591 Normal,
3592 Empty,
3593 Group,
3594 Middle,
3595 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003596 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 Trunc
3598 } type;
3599 } item[STL_MAX_ITEM];
3600 int minwid;
3601 int maxwid;
3602 int zeropad;
3603 char_u base;
3604 char_u opt;
3605#define TMPLEN 70
3606 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003607 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003608 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003609
3610#ifdef FEAT_EVAL
3611 /*
3612 * When the format starts with "%!" then evaluate it as an expression and
3613 * use the result as the actual format string.
3614 */
3615 if (fmt[0] == '%' && fmt[1] == '!')
3616 {
3617 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3618 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003619 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003620 }
3621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 if (fillchar == 0)
3624 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003625#ifdef FEAT_MBYTE
3626 /* Can't handle a multi-byte fill character yet. */
3627 else if (mb_char2len(fillchar) > 1)
3628 fillchar = '-';
3629#endif
3630
Bram Moolenaar567199b2013-04-24 16:52:36 +02003631 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3632 * p will become invalid when getting another buffer line. */
3633 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3634 empty_line = (*p == NUL);
3635
3636 /* Get the byte value now, in case we need it below. This is more
3637 * efficient than making a copy of the line. */
3638 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3639 byteval = 0;
3640 else
3641#ifdef FEAT_MBYTE
3642 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3643#else
3644 byteval = p[wp->w_cursor.col];
3645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 groupdepth = 0;
3648 p = out;
3649 curitem = 0;
3650 prevchar_isflag = TRUE;
3651 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003652 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003654 if (curitem == STL_MAX_ITEM)
3655 {
3656 /* There are too many items. Add the error code to the statusline
3657 * to give the user a hint about what went wrong. */
3658 if (p + 6 < out + outlen)
3659 {
3660 mch_memmove(p, " E541", (size_t)5);
3661 p += 5;
3662 }
3663 break;
3664 }
3665
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (*s != NUL && *s != '%')
3667 prevchar_isflag = prevchar_isitem = FALSE;
3668
3669 /*
3670 * Handle up to the next '%' or the end.
3671 */
3672 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3673 *p++ = *s++;
3674 if (*s == NUL || p + 1 >= out + outlen)
3675 break;
3676
3677 /*
3678 * Handle one '%' item.
3679 */
3680 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003681 if (*s == NUL) /* ignore trailing % */
3682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 if (*s == '%')
3684 {
3685 if (p + 1 >= out + outlen)
3686 break;
3687 *p++ = *s++;
3688 prevchar_isflag = prevchar_isitem = FALSE;
3689 continue;
3690 }
3691 if (*s == STL_MIDDLEMARK)
3692 {
3693 s++;
3694 if (groupdepth > 0)
3695 continue;
3696 item[curitem].type = Middle;
3697 item[curitem++].start = p;
3698 continue;
3699 }
3700 if (*s == STL_TRUNCMARK)
3701 {
3702 s++;
3703 item[curitem].type = Trunc;
3704 item[curitem++].start = p;
3705 continue;
3706 }
3707 if (*s == ')')
3708 {
3709 s++;
3710 if (groupdepth < 1)
3711 continue;
3712 groupdepth--;
3713
3714 t = item[groupitem[groupdepth]].start;
3715 *p = NUL;
3716 l = vim_strsize(t);
3717 if (curitem > groupitem[groupdepth] + 1
3718 && item[groupitem[groupdepth]].minwid == 0)
3719 {
3720 /* remove group if all items are empty */
3721 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3722 if (item[n].type == Normal)
3723 break;
3724 if (n == curitem)
3725 {
3726 p = t;
3727 l = 0;
3728 }
3729 }
3730 if (l > item[groupitem[groupdepth]].maxwid)
3731 {
3732 /* truncate, remove n bytes of text at the start */
3733#ifdef FEAT_MBYTE
3734 if (has_mbyte)
3735 {
3736 /* Find the first character that should be included. */
3737 n = 0;
3738 while (l >= item[groupitem[groupdepth]].maxwid)
3739 {
3740 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003741 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743 }
3744 else
3745#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003746 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
3748 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003749 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 p = p - n + 1;
3751#ifdef FEAT_MBYTE
3752 /* Fill up space left over by half a double-wide char. */
3753 while (++l < item[groupitem[groupdepth]].minwid)
3754 *p++ = fillchar;
3755#endif
3756
3757 /* correct the start of the items for the truncation */
3758 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3759 {
3760 item[l].start -= n;
3761 if (item[l].start < t)
3762 item[l].start = t;
3763 }
3764 }
3765 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3766 {
3767 /* fill */
3768 n = item[groupitem[groupdepth]].minwid;
3769 if (n < 0)
3770 {
3771 /* fill by appending characters */
3772 n = 0 - n;
3773 while (l++ < n && p + 1 < out + outlen)
3774 *p++ = fillchar;
3775 }
3776 else
3777 {
3778 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003779 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 l = n - l;
3781 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003782 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 p += l;
3784 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3785 item[n].start += l;
3786 for ( ; l > 0; l--)
3787 *t++ = fillchar;
3788 }
3789 }
3790 continue;
3791 }
3792 minwid = 0;
3793 maxwid = 9999;
3794 zeropad = FALSE;
3795 l = 1;
3796 if (*s == '0')
3797 {
3798 s++;
3799 zeropad = TRUE;
3800 }
3801 if (*s == '-')
3802 {
3803 s++;
3804 l = -1;
3805 }
3806 if (VIM_ISDIGIT(*s))
3807 {
3808 minwid = (int)getdigits(&s);
3809 if (minwid < 0) /* overflow */
3810 minwid = 0;
3811 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003812 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 {
3814 item[curitem].type = Highlight;
3815 item[curitem].start = p;
3816 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3817 s++;
3818 curitem++;
3819 continue;
3820 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003821 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3822 {
3823 if (*s == STL_TABCLOSENR)
3824 {
3825 if (minwid == 0)
3826 {
3827 /* %X ends the close label, go back to the previously
3828 * define tab label nr. */
3829 for (n = curitem - 1; n >= 0; --n)
3830 if (item[n].type == TabPage && item[n].minwid >= 0)
3831 {
3832 minwid = item[n].minwid;
3833 break;
3834 }
3835 }
3836 else
3837 /* close nrs are stored as negative values */
3838 minwid = - minwid;
3839 }
3840 item[curitem].type = TabPage;
3841 item[curitem].start = p;
3842 item[curitem].minwid = minwid;
3843 s++;
3844 curitem++;
3845 continue;
3846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (*s == '.')
3848 {
3849 s++;
3850 if (VIM_ISDIGIT(*s))
3851 {
3852 maxwid = (int)getdigits(&s);
3853 if (maxwid <= 0) /* overflow */
3854 maxwid = 50;
3855 }
3856 }
3857 minwid = (minwid > 50 ? 50 : minwid) * l;
3858 if (*s == '(')
3859 {
3860 groupitem[groupdepth++] = curitem;
3861 item[curitem].type = Group;
3862 item[curitem].start = p;
3863 item[curitem].minwid = minwid;
3864 item[curitem].maxwid = maxwid;
3865 s++;
3866 curitem++;
3867 continue;
3868 }
3869 if (vim_strchr(STL_ALL, *s) == NULL)
3870 {
3871 s++;
3872 continue;
3873 }
3874 opt = *s++;
3875
3876 /* OK - now for the real work */
3877 base = 'D';
3878 itemisflag = FALSE;
3879 fillable = TRUE;
3880 num = -1;
3881 str = NULL;
3882 switch (opt)
3883 {
3884 case STL_FILEPATH:
3885 case STL_FULLPATH:
3886 case STL_FILENAME:
3887 fillable = FALSE; /* don't change ' ' to fillchar */
3888 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003889 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 else
3891 {
3892 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003893 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3895 }
3896 trans_characters(NameBuff, MAXPATHL);
3897 if (opt != STL_FILENAME)
3898 str = NameBuff;
3899 else
3900 str = gettail(NameBuff);
3901 break;
3902
3903 case STL_VIM_EXPR: /* '{' */
3904 itemisflag = TRUE;
3905 t = p;
3906 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3907 *p++ = *s++;
3908 if (*s != '}') /* missing '}' or out of space */
3909 break;
3910 s++;
3911 *p = 0;
3912 p = t;
3913
3914#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003915 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3917
3918 o_curbuf = curbuf;
3919 o_curwin = curwin;
3920 curwin = wp;
3921 curbuf = wp->w_buffer;
3922
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003923 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 curwin = o_curwin;
3926 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003927 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 if (str != NULL && *str != 0)
3930 {
3931 if (*skipdigits(str) == NUL)
3932 {
3933 num = atoi((char *)str);
3934 vim_free(str);
3935 str = NULL;
3936 itemisflag = FALSE;
3937 }
3938 }
3939#endif
3940 break;
3941
3942 case STL_LINE:
3943 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3944 ? 0L : (long)(wp->w_cursor.lnum);
3945 break;
3946
3947 case STL_NUMLINES:
3948 num = wp->w_buffer->b_ml.ml_line_count;
3949 break;
3950
3951 case STL_COLUMN:
3952 num = !(State & INSERT) && empty_line
3953 ? 0 : (int)wp->w_cursor.col + 1;
3954 break;
3955
3956 case STL_VIRTCOL:
3957 case STL_VIRTCOL_ALT:
3958 /* In list mode virtcol needs to be recomputed */
3959 virtcol = wp->w_virtcol;
3960 if (wp->w_p_list && lcs_tab1 == NUL)
3961 {
3962 wp->w_p_list = FALSE;
3963 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3964 wp->w_p_list = TRUE;
3965 }
3966 ++virtcol;
3967 /* Don't display %V if it's the same as %c. */
3968 if (opt == STL_VIRTCOL_ALT
3969 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3970 ? 0 : (int)wp->w_cursor.col + 1)))
3971 break;
3972 num = (long)virtcol;
3973 break;
3974
3975 case STL_PERCENTAGE:
3976 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3977 (long)wp->w_buffer->b_ml.ml_line_count);
3978 break;
3979
3980 case STL_ALTPERCENT:
3981 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003982 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 break;
3984
3985 case STL_ARGLISTSTAT:
3986 fillable = FALSE;
3987 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003988 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 str = tmp;
3990 break;
3991
3992 case STL_KEYMAP:
3993 fillable = FALSE;
3994 if (get_keymap_str(wp, tmp, TMPLEN))
3995 str = tmp;
3996 break;
3997 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003998#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003999 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#else
4001 num = 0;
4002#endif
4003 break;
4004
4005 case STL_BUFNO:
4006 num = wp->w_buffer->b_fnum;
4007 break;
4008
4009 case STL_OFFSET_X:
4010 base = 'X';
4011 case STL_OFFSET:
4012#ifdef FEAT_BYTEOFF
4013 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4014 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4015 0L : l + 1 + (!(State & INSERT) && empty_line ?
4016 0 : (int)wp->w_cursor.col);
4017#endif
4018 break;
4019
4020 case STL_BYTEVAL_X:
4021 base = 'X';
4022 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004023 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (num == NL)
4025 num = 0;
4026 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4027 num = NL;
4028 break;
4029
4030 case STL_ROFLAG:
4031 case STL_ROFLAG_ALT:
4032 itemisflag = TRUE;
4033 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004034 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 break;
4036
4037 case STL_HELPFLAG:
4038 case STL_HELPFLAG_ALT:
4039 itemisflag = TRUE;
4040 if (wp->w_buffer->b_help)
4041 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004042 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 break;
4044
4045#ifdef FEAT_AUTOCMD
4046 case STL_FILETYPE:
4047 if (*wp->w_buffer->b_p_ft != NUL
4048 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4049 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004050 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4051 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 str = tmp;
4053 }
4054 break;
4055
4056 case STL_FILETYPE_ALT:
4057 itemisflag = TRUE;
4058 if (*wp->w_buffer->b_p_ft != NUL
4059 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4060 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004061 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4062 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 for (t = tmp; *t != 0; t++)
4064 *t = TOUPPER_LOC(*t);
4065 str = tmp;
4066 }
4067 break;
4068#endif
4069
4070#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4071 case STL_PREVIEWFLAG:
4072 case STL_PREVIEWFLAG_ALT:
4073 itemisflag = TRUE;
4074 if (wp->w_p_pvw)
4075 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4076 : _("[Preview]"));
4077 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004078
4079 case STL_QUICKFIX:
4080 if (bt_quickfix(wp->w_buffer))
4081 str = (char_u *)(wp->w_llist_ref
4082 ? _(msg_loclist)
4083 : _(msg_qflist));
4084 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#endif
4086
4087 case STL_MODIFIED:
4088 case STL_MODIFIED_ALT:
4089 itemisflag = TRUE;
4090 switch ((opt == STL_MODIFIED_ALT)
4091 + bufIsChanged(wp->w_buffer) * 2
4092 + (!wp->w_buffer->b_p_ma) * 4)
4093 {
4094 case 2: str = (char_u *)"[+]"; break;
4095 case 3: str = (char_u *)",+"; break;
4096 case 4: str = (char_u *)"[-]"; break;
4097 case 5: str = (char_u *)",-"; break;
4098 case 6: str = (char_u *)"[+-]"; break;
4099 case 7: str = (char_u *)",+-"; break;
4100 }
4101 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004102
4103 case STL_HIGHLIGHT:
4104 t = s;
4105 while (*s != '#' && *s != NUL)
4106 ++s;
4107 if (*s == '#')
4108 {
4109 item[curitem].type = Highlight;
4110 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004111 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004112 curitem++;
4113 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004114 if (*s != NUL)
4115 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004116 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118
4119 item[curitem].start = p;
4120 item[curitem].type = Normal;
4121 if (str != NULL && *str)
4122 {
4123 t = str;
4124 if (itemisflag)
4125 {
4126 if ((t[0] && t[1])
4127 && ((!prevchar_isitem && *t == ',')
4128 || (prevchar_isflag && *t == ' ')))
4129 t++;
4130 prevchar_isflag = TRUE;
4131 }
4132 l = vim_strsize(t);
4133 if (l > 0)
4134 prevchar_isitem = TRUE;
4135 if (l > maxwid)
4136 {
4137 while (l >= maxwid)
4138#ifdef FEAT_MBYTE
4139 if (has_mbyte)
4140 {
4141 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004142 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 else
4145#endif
4146 l -= byte2cells(*t++);
4147 if (p + 1 >= out + outlen)
4148 break;
4149 *p++ = '<';
4150 }
4151 if (minwid > 0)
4152 {
4153 for (; l < minwid && p + 1 < out + outlen; l++)
4154 {
4155 /* Don't put a "-" in front of a digit. */
4156 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4157 *p++ = ' ';
4158 else
4159 *p++ = fillchar;
4160 }
4161 minwid = 0;
4162 }
4163 else
4164 minwid *= -1;
4165 while (*t && p + 1 < out + outlen)
4166 {
4167 *p++ = *t++;
4168 /* Change a space by fillchar, unless fillchar is '-' and a
4169 * digit follows. */
4170 if (fillable && p[-1] == ' '
4171 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4172 p[-1] = fillchar;
4173 }
4174 for (; l < minwid && p + 1 < out + outlen; l++)
4175 *p++ = fillchar;
4176 }
4177 else if (num >= 0)
4178 {
4179 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4180 char_u nstr[20];
4181
4182 if (p + 20 >= out + outlen)
4183 break; /* not sufficient space */
4184 prevchar_isitem = TRUE;
4185 t = nstr;
4186 if (opt == STL_VIRTCOL_ALT)
4187 {
4188 *t++ = '-';
4189 minwid--;
4190 }
4191 *t++ = '%';
4192 if (zeropad)
4193 *t++ = '0';
4194 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004195 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 *t = 0;
4197
4198 for (n = num, l = 1; n >= nbase; n /= nbase)
4199 l++;
4200 if (opt == STL_VIRTCOL_ALT)
4201 l++;
4202 if (l > maxwid)
4203 {
4204 l += 2;
4205 n = l - maxwid;
4206 while (l-- > maxwid)
4207 num /= nbase;
4208 *t++ = '>';
4209 *t++ = '%';
4210 *t = t[-3];
4211 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004212 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4213 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004216 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4217 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 p += STRLEN(p);
4219 }
4220 else
4221 item[curitem].type = Empty;
4222
4223 if (opt == STL_VIM_EXPR)
4224 vim_free(str);
4225
4226 if (num >= 0 || (!itemisflag && str && *str))
4227 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4228 curitem++;
4229 }
4230 *p = NUL;
4231 itemcnt = curitem;
4232
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004233#ifdef FEAT_EVAL
4234 if (usefmt != fmt)
4235 vim_free(usefmt);
4236#endif
4237
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 width = vim_strsize(out);
4239 if (maxwidth > 0 && width > maxwidth)
4240 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004241 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 l = 0;
4243 if (itemcnt == 0)
4244 s = out;
4245 else
4246 {
4247 for ( ; l < itemcnt; l++)
4248 if (item[l].type == Trunc)
4249 {
4250 /* Truncate at %< item. */
4251 s = item[l].start;
4252 break;
4253 }
4254 if (l == itemcnt)
4255 {
4256 /* No %< item, truncate first item. */
4257 s = item[0].start;
4258 l = 0;
4259 }
4260 }
4261
4262 if (width - vim_strsize(s) >= maxwidth)
4263 {
4264 /* Truncation mark is beyond max length */
4265#ifdef FEAT_MBYTE
4266 if (has_mbyte)
4267 {
4268 s = out;
4269 width = 0;
4270 for (;;)
4271 {
4272 width += ptr2cells(s);
4273 if (width >= maxwidth)
4274 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004275 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 /* Fill up for half a double-wide character. */
4278 while (++width < maxwidth)
4279 *s++ = fillchar;
4280 }
4281 else
4282#endif
4283 s = out + maxwidth - 1;
4284 for (l = 0; l < itemcnt; l++)
4285 if (item[l].start > s)
4286 break;
4287 itemcnt = l;
4288 *s++ = '>';
4289 *s = 0;
4290 }
4291 else
4292 {
4293#ifdef FEAT_MBYTE
4294 if (has_mbyte)
4295 {
4296 n = 0;
4297 while (width >= maxwidth)
4298 {
4299 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004300 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
4303 else
4304#endif
4305 n = width - maxwidth + 1;
4306 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004307 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 *s = '<';
4309
4310 /* Fill up for half a double-wide character. */
4311 while (++width < maxwidth)
4312 {
4313 s = s + STRLEN(s);
4314 *s++ = fillchar;
4315 *s = NUL;
4316 }
4317
4318 --n; /* count the '<' */
4319 for (; l < itemcnt; l++)
4320 {
4321 if (item[l].start - n >= s)
4322 item[l].start -= n;
4323 else
4324 item[l].start = s;
4325 }
4326 }
4327 width = maxwidth;
4328 }
4329 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4330 {
4331 /* Apply STL_MIDDLE if any */
4332 for (l = 0; l < itemcnt; l++)
4333 if (item[l].type == Middle)
4334 break;
4335 if (l < itemcnt)
4336 {
4337 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004338 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 for (s = item[l].start; s < p; s++)
4340 *s = fillchar;
4341 for (l++; l < itemcnt; l++)
4342 item[l].start += maxwidth - width;
4343 width = maxwidth;
4344 }
4345 }
4346
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004347 /* Store the info about highlighting. */
4348 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004350 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 for (l = 0; l < itemcnt; l++)
4352 {
4353 if (item[l].type == Highlight)
4354 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004355 sp->start = item[l].start;
4356 sp->userhl = item[l].minwid;
4357 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004360 sp->start = NULL;
4361 sp->userhl = 0;
4362 }
4363
4364 /* Store the info about tab pages labels. */
4365 if (tabtab != NULL)
4366 {
4367 sp = tabtab;
4368 for (l = 0; l < itemcnt; l++)
4369 {
4370 if (item[l].type == TabPage)
4371 {
4372 sp->start = item[l].start;
4373 sp->userhl = item[l].minwid;
4374 sp++;
4375 }
4376 }
4377 sp->start = NULL;
4378 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380
4381 return width;
4382}
4383#endif /* FEAT_STL_OPT */
4384
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004385#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4386 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004388 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4389 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 */
4391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004392get_rel_pos(
4393 win_T *wp,
4394 char_u *buf,
4395 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
4397 long above; /* number of lines above window */
4398 long below; /* number of lines below window */
4399
Bram Moolenaar0027c212015-01-07 13:31:52 +01004400 if (buflen < 3) /* need at least 3 chars for writing */
4401 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 above = wp->w_topline - 1;
4403#ifdef FEAT_DIFF
4404 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004405 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4406 above = 0; /* All buffer lines are displayed and there is an
4407 * indication of filler lines, that can be considered
4408 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409#endif
4410 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4411 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004412 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4413 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004415 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004417 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 ? (int)(above / ((above + below) / 100L))
4419 : (int)(above * 100L / (above + below)));
4420}
4421#endif
4422
4423/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004424 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 * Return TRUE if it was appended.
4426 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004428append_arg_number(
4429 win_T *wp,
4430 char_u *buf,
4431 int buflen,
4432 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433{
4434 char_u *p;
4435
4436 if (ARGCOUNT <= 1) /* nothing to do */
4437 return FALSE;
4438
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004439 p = buf + STRLEN(buf); /* go to the end of the buffer */
4440 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 return FALSE;
4442 *p++ = ' ';
4443 *p++ = '(';
4444 if (add_file)
4445 {
4446 STRCPY(p, "file ");
4447 p += 5;
4448 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004449 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4450 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4452 return TRUE;
4453}
4454
4455/*
4456 * If fname is not a full path, make it a full path.
4457 * Returns pointer to allocated memory (NULL for failure).
4458 */
4459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004460fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461{
4462 /*
4463 * Force expanding the path always for Unix, because symbolic links may
4464 * mess up the full path name, even though it starts with a '/'.
4465 * Also expand when there is ".." in the file name, try to remove it,
4466 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004467 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 * For MS-Windows also expand names like "longna~1" to "longname".
4469 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004470#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 return FullName_save(fname, TRUE);
4472#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004473 if (!vim_isAbsName(fname)
4474 || strstr((char *)fname, "..") != NULL
4475 || strstr((char *)fname, "//") != NULL
4476# ifdef BACKSLASH_IN_FILENAME
4477 || strstr((char *)fname, "\\\\") != NULL
4478# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004479# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 )
4483 return FullName_save(fname, FALSE);
4484
4485 fname = vim_strsave(fname);
4486
Bram Moolenaar9b942202007-10-03 12:31:33 +00004487# ifdef USE_FNAME_CASE
4488# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
4492 if (fname != NULL)
4493 fname_case(fname, 0); /* set correct case for file name */
4494 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 return fname;
4498#endif
4499}
4500
4501/*
4502 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4503 * "ffname" becomes a pointer to allocated memory (or NULL).
4504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004506fname_expand(
4507 buf_T *buf UNUSED,
4508 char_u **ffname,
4509 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
4511 if (*ffname == NULL) /* if no file name given, nothing to do */
4512 return;
4513 if (*sfname == NULL) /* if no short file name given, use ffname */
4514 *sfname = *ffname;
4515 *ffname = fix_fname(*ffname); /* expand to full path */
4516
4517#ifdef FEAT_SHORTCUT
4518 if (!buf->b_p_bin)
4519 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004520 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 /* If the file name is a shortcut file, use the file it links to. */
4523 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004524 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
4526 vim_free(*ffname);
4527 *ffname = rfname;
4528 *sfname = rfname;
4529 }
4530 }
4531#endif
4532}
4533
4534/*
4535 * Get the file name for an argument list entry.
4536 */
4537 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004538alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539{
4540 buf_T *bp;
4541
4542 /* Use the name from the associated buffer if it exists. */
4543 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004544 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 return aep->ae_fname;
4546 return bp->b_fname;
4547}
4548
4549#if defined(FEAT_WINDOWS) || defined(PROTO)
4550/*
4551 * do_arg_all(): Open up to 'count' windows, one for each argument.
4552 */
4553 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004554do_arg_all(
4555 int count,
4556 int forceit, /* hide buffers in current windows */
4557 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558{
4559 int i;
4560 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004561 char_u *opened; /* Array of weight for which args are open:
4562 * 0: not opened
4563 * 1: opened in other tab
4564 * 2: opened in curtab
4565 * 3: opened in curtab and curwin
4566 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004567 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 int use_firstwin = FALSE; /* use first window for arglist */
4569 int split_ret = OK;
4570 int p_ea_save;
4571 alist_T *alist; /* argument list to be used */
4572 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004573 tabpage_T *tpnext;
4574 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004575 win_T *old_curwin, *last_curwin;
4576 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004577 win_T *new_curwin = NULL;
4578 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
4580 if (ARGCOUNT <= 0)
4581 {
4582 /* Don't give an error message. We don't want it when the ":all"
4583 * command is in the .vimrc. */
4584 return;
4585 }
4586 setpcmark();
4587
4588 opened_len = ARGCOUNT;
4589 opened = alloc_clear((unsigned)opened_len);
4590 if (opened == NULL)
4591 return;
4592
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004593 /* Autocommands may do anything to the argument list. Make sure it's not
4594 * freed while we are working here by "locking" it. We still have to
4595 * watch out for its size to be changed. */
4596 alist = curwin->w_alist;
4597 ++alist->al_refcount;
4598
4599 old_curwin = curwin;
4600 old_curtab = curtab;
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#ifdef FEAT_GUI
4603 need_mouse_correct = TRUE;
4604#endif
4605
4606 /*
4607 * Try closing all windows that are not in the argument list.
4608 * Also close windows that are not full width;
4609 * When 'hidden' or "forceit" set the buffer becomes hidden.
4610 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004611 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004613 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004614 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004615 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004617 tpnext = curtab->tp_next;
4618 for (wp = firstwin; wp != NULL; wp = wpnext)
4619 {
4620 wpnext = wp->w_next;
4621 buf = wp->w_buffer;
4622 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004623 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004625 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004627 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004628 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004629 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004631 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004632 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004634 if (i < alist->al_ga.ga_len
4635 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4636 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4637 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004639 int weight = 1;
4640
4641 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004642 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004643 ++weight;
4644 if (old_curwin == wp)
4645 ++weight;
4646 }
4647
4648 if (weight > (int)opened[i])
4649 {
4650 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004651 if (i == 0)
4652 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004653 if (new_curwin != NULL)
4654 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004655 new_curwin = wp;
4656 new_curtab = curtab;
4657 }
4658 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004659 else if (keep_tabs)
4660 i = opened_len;
4661
4662 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004663 {
4664 /* Use the current argument list for all windows
4665 * containing a file from it. */
4666 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004667 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004668 ++wp->w_alist->al_refcount;
4669 }
4670 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004674 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004676 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004678 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004679 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004681 /* If the buffer was changed, and we would like to hide it,
4682 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004683 if (!P_HID(buf) && buf->b_nwindows <= 1
4684 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004686 (void)autowrite(buf, FALSE);
4687#ifdef FEAT_AUTOCMD
4688 /* check if autocommands removed the window */
4689 if (!win_valid(wp) || !buf_valid(buf))
4690 {
4691 wpnext = firstwin; /* start all over... */
4692 continue;
4693 }
4694#endif
4695 }
4696#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004697 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004698 if (firstwin == lastwin
4699 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004700#endif
4701 use_firstwin = TRUE;
4702#ifdef FEAT_WINDOWS
4703 else
4704 {
4705 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4706# ifdef FEAT_AUTOCMD
4707 /* check if autocommands removed the next window */
4708 if (!win_valid(wpnext))
4709 wpnext = firstwin; /* start all over... */
4710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712#endif
4713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
4715 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004716
4717 /* Without the ":tab" modifier only do the current tab page. */
4718 if (had_tab == 0 || tpnext == NULL)
4719 break;
4720
4721# ifdef FEAT_AUTOCMD
4722 /* check if autocommands removed the next tab page */
4723 if (!valid_tabpage(tpnext))
4724 tpnext = first_tabpage; /* start all over...*/
4725# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004726 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
4728
4729 /*
4730 * Open a window for files in the argument list that don't have one.
4731 * ARGCOUNT may change while doing this, because of autocommands.
4732 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004733 if (count > opened_len || count <= 0)
4734 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
4736#ifdef FEAT_AUTOCMD
4737 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4738 ++autocmd_no_enter;
4739 ++autocmd_no_leave;
4740#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004741 last_curwin = curwin;
4742 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004744#ifdef FEAT_WINDOWS
4745 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4746 * leaving an empty tab page when executed locally. */
4747 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4748 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4749 use_firstwin = TRUE;
4750#endif
4751
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004752 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4755 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004756 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 /* Move the already present window to below the current window */
4759 if (curwin->w_arg_idx != i)
4760 {
4761 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4762 {
4763 if (wpnext->w_arg_idx == i)
4764 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004765 if (keep_tabs)
4766 {
4767 new_curwin = wpnext;
4768 new_curtab = curtab;
4769 }
4770 else
4771 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 break;
4773 }
4774 }
4775 }
4776 }
4777 else if (split_ret == OK)
4778 {
4779 if (!use_firstwin) /* split current window */
4780 {
4781 p_ea_save = p_ea;
4782 p_ea = TRUE; /* use space from all windows */
4783 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4784 p_ea = p_ea_save;
4785 if (split_ret == FAIL)
4786 continue;
4787 }
4788#ifdef FEAT_AUTOCMD
4789 else /* first window: do autocmd for leaving this buffer */
4790 --autocmd_no_leave;
4791#endif
4792
4793 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004794 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 */
4796 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004797 if (i == 0)
4798 {
4799 new_curwin = curwin;
4800 new_curtab = curtab;
4801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4803 ECMD_ONE,
4804 ((P_HID(curwin->w_buffer)
4805 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004806 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807#ifdef FEAT_AUTOCMD
4808 if (use_firstwin)
4809 ++autocmd_no_leave;
4810#endif
4811 use_firstwin = FALSE;
4812 }
4813 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004814
4815 /* When ":tab" was used open a new tab for a new window repeatedly. */
4816 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4817 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819
4820 /* Remove the "lock" on the argument list. */
4821 alist_unlink(alist);
4822
4823#ifdef FEAT_AUTOCMD
4824 --autocmd_no_enter;
4825#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004826 /* restore last referenced tabpage's curwin */
4827 if (last_curtab != new_curtab)
4828 {
4829 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004830 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004831 if (win_valid(last_curwin))
4832 win_enter(last_curwin, FALSE);
4833 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004834 /* to window with first arg */
4835 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004836 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004837 if (win_valid(new_curwin))
4838 win_enter(new_curwin, FALSE);
4839
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840#ifdef FEAT_AUTOCMD
4841 --autocmd_no_leave;
4842#endif
4843 vim_free(opened);
4844}
4845
4846# if defined(FEAT_LISTCMDS) || defined(PROTO)
4847/*
4848 * Open a window for a number of buffers.
4849 */
4850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004851ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
4853 buf_T *buf;
4854 win_T *wp, *wpnext;
4855 int split_ret = OK;
4856 int p_ea_save;
4857 int open_wins = 0;
4858 int r;
4859 int count; /* Maximum number of windows to open. */
4860 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004861#ifdef FEAT_WINDOWS
4862 int had_tab = cmdmod.tab;
4863 tabpage_T *tpnext;
4864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
4866 if (eap->addr_count == 0) /* make as many windows as possible */
4867 count = 9999;
4868 else
4869 count = eap->line2; /* make as many windows as specified */
4870 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4871 all = FALSE;
4872 else
4873 all = TRUE;
4874
4875 setpcmark();
4876
4877#ifdef FEAT_GUI
4878 need_mouse_correct = TRUE;
4879#endif
4880
4881 /*
4882 * Close superfluous windows (two windows for the same buffer).
4883 * Also close windows that are not full-width.
4884 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004885#ifdef FEAT_WINDOWS
4886 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004887 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004888 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004891 tpnext = curtab->tp_next;
4892 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004894 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004895 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004896#ifdef FEAT_VERTSPLIT
4897 || ((cmdmod.split & WSP_VERT)
4898 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004899 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004900 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004902#ifdef FEAT_WINDOWS
4903 || (had_tab > 0 && wp != firstwin)
4904#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004905 ) && firstwin != lastwin
4906#ifdef FEAT_AUTOCMD
4907 && !(wp->w_closing || wp->w_buffer->b_closing)
4908#endif
4909 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004910 {
4911 win_close(wp, FALSE);
4912#ifdef FEAT_AUTOCMD
4913 wpnext = firstwin; /* just in case an autocommand does
4914 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004915 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004916 open_wins = 0;
4917#endif
4918 }
4919 else
4920 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004922
4923#ifdef FEAT_WINDOWS
4924 /* Without the ":tab" modifier only do the current tab page. */
4925 if (had_tab == 0 || tpnext == NULL)
4926 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004927 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931 /*
4932 * Go through the buffer list. When a buffer doesn't have a window yet,
4933 * open one. Otherwise move the window to the right position.
4934 * Watch out for autocommands that delete buffers or windows!
4935 */
4936#ifdef FEAT_AUTOCMD
4937 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4938 ++autocmd_no_enter;
4939#endif
4940 win_enter(lastwin, FALSE);
4941#ifdef FEAT_AUTOCMD
4942 ++autocmd_no_leave;
4943#endif
4944 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4945 {
4946 /* Check if this buffer needs a window */
4947 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4948 continue;
4949
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004950#ifdef FEAT_WINDOWS
4951 if (had_tab != 0)
4952 {
4953 /* With the ":tab" modifier don't move the window. */
4954 if (buf->b_nwindows > 0)
4955 wp = lastwin; /* buffer has a window, skip it */
4956 else
4957 wp = NULL;
4958 }
4959 else
4960#endif
4961 {
4962 /* Check if this buffer already has a window */
4963 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4964 if (wp->w_buffer == buf)
4965 break;
4966 /* If the buffer already has a window, move it */
4967 if (wp != NULL)
4968 win_move_after(wp, curwin);
4969 }
4970
4971 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 /* Split the window and put the buffer in it */
4974 p_ea_save = p_ea;
4975 p_ea = TRUE; /* use space from all windows */
4976 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4977 ++open_wins;
4978 p_ea = p_ea_save;
4979 if (split_ret == FAIL)
4980 continue;
4981
4982 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004983#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 swap_exists_action = SEA_DIALOG;
4985#endif
4986 set_curbuf(buf, DOBUF_GOTO);
4987#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004990#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 break;
4994 }
4995#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004996#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (swap_exists_action == SEA_QUIT)
4998 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004999# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5000 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005001
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005002 /* Reset the error/interrupt/exception state here so that
5003 * aborting() returns FALSE when closing a window. */
5004 enter_cleanup(&cs);
5005# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005006
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005007 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 win_close(curwin, TRUE);
5009 --open_wins;
5010 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005011 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005012
5013# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5014 /* Restore the error/interrupt/exception state if not
5015 * discarded by a new aborting error, interrupt, or uncaught
5016 * exception. */
5017 leave_cleanup(&cs);
5018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 else
5021 handle_swap_exists(NULL);
5022#endif
5023 }
5024
5025 ui_breakcheck();
5026 if (got_int)
5027 {
5028 (void)vgetc(); /* only break the file loading, not the rest */
5029 break;
5030 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005031#ifdef FEAT_EVAL
5032 /* Autocommands deleted the buffer or aborted script processing!!! */
5033 if (aborting())
5034 break;
5035#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005036#ifdef FEAT_WINDOWS
5037 /* When ":tab" was used open a new tab for a new window repeatedly. */
5038 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5039 cmdmod.tab = 9999;
5040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042#ifdef FEAT_AUTOCMD
5043 --autocmd_no_enter;
5044#endif
5045 win_enter(firstwin, FALSE); /* back to first window */
5046#ifdef FEAT_AUTOCMD
5047 --autocmd_no_leave;
5048#endif
5049
5050 /*
5051 * Close superfluous windows.
5052 */
5053 for (wp = lastwin; open_wins > count; )
5054 {
5055 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5056 || autowrite(wp->w_buffer, FALSE) == OK);
5057#ifdef FEAT_AUTOCMD
5058 if (!win_valid(wp))
5059 {
5060 /* BufWrite Autocommands made the window invalid, start over */
5061 wp = lastwin;
5062 }
5063 else
5064#endif
5065 if (r)
5066 {
5067 win_close(wp, !P_HID(wp->w_buffer));
5068 --open_wins;
5069 wp = lastwin;
5070 }
5071 else
5072 {
5073 wp = wp->w_prev;
5074 if (wp == NULL)
5075 break;
5076 }
5077 }
5078}
5079# endif /* FEAT_LISTCMDS */
5080
5081#endif /* FEAT_WINDOWS */
5082
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005083static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085/*
5086 * do_modelines() - process mode lines for the current file
5087 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005088 * "flags" can be:
5089 * OPT_WINONLY only set options local to window
5090 * OPT_NOWIN don't set options local to window
5091 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 * Returns immediately if the "ml" option isn't set.
5093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005095do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005097 linenr_T lnum;
5098 int nmlines;
5099 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
5101 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5102 return;
5103
5104 /* Disallow recursive entry here. Can happen when executing a modeline
5105 * triggers an autocommand, which reloads modelines with a ":do". */
5106 if (entered)
5107 return;
5108
5109 ++entered;
5110 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5111 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005112 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 nmlines = 0;
5114
5115 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5116 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005117 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 nmlines = 0;
5119 --entered;
5120}
5121
5122#include "version.h" /* for version number */
5123
5124/*
5125 * chk_modeline() - check a single line for a mode string
5126 * Return FAIL if an error encountered.
5127 */
5128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005129chk_modeline(
5130 linenr_T lnum,
5131 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 char_u *s;
5134 char_u *e;
5135 char_u *linecopy; /* local copy of any modeline found */
5136 int prev;
5137 int vers;
5138 int end;
5139 int retval = OK;
5140 char_u *save_sourcing_name;
5141 linenr_T save_sourcing_lnum;
5142#ifdef FEAT_EVAL
5143 scid_T save_SID;
5144#endif
5145
5146 prev = -1;
5147 for (s = ml_get(lnum); *s != NUL; ++s)
5148 {
5149 if (prev == -1 || vim_isspace(prev))
5150 {
5151 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5152 || STRNCMP(s, "vi:", (size_t)3) == 0)
5153 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005154 /* Accept both "vim" and "Vim". */
5155 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
5157 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5158 e = s + 4;
5159 else
5160 e = s + 3;
5161 vers = getdigits(&e);
5162 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005163 && (s[0] != 'V'
5164 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 && (s[3] == ':'
5166 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5167 || (VIM_VERSION_100 < vers && s[3] == '<')
5168 || (VIM_VERSION_100 > vers && s[3] == '>')
5169 || (VIM_VERSION_100 == vers && s[3] == '=')))
5170 break;
5171 }
5172 }
5173 prev = *s;
5174 }
5175
5176 if (*s)
5177 {
5178 do /* skip over "ex:", "vi:" or "vim:" */
5179 ++s;
5180 while (s[-1] != ':');
5181
5182 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5183 if (linecopy == NULL)
5184 return FAIL;
5185
5186 save_sourcing_lnum = sourcing_lnum;
5187 save_sourcing_name = sourcing_name;
5188 sourcing_lnum = lnum; /* prepare for emsg() */
5189 sourcing_name = (char_u *)"modelines";
5190
5191 end = FALSE;
5192 while (end == FALSE)
5193 {
5194 s = skipwhite(s);
5195 if (*s == NUL)
5196 break;
5197
5198 /*
5199 * Find end of set command: ':' or end of line.
5200 * Skip over "\:", replacing it with ":".
5201 */
5202 for (e = s; *e != ':' && *e != NUL; ++e)
5203 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005204 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 if (*e == NUL)
5206 end = TRUE;
5207
5208 /*
5209 * If there is a "set" command, require a terminating ':' and
5210 * ignore the stuff after the ':'.
5211 * "vi:set opt opt opt: foo" -- foo not interpreted
5212 * "vi:opt opt opt: foo" -- foo interpreted
5213 * Accept "se" for compatibility with Elvis.
5214 */
5215 if (STRNCMP(s, "set ", (size_t)4) == 0
5216 || STRNCMP(s, "se ", (size_t)3) == 0)
5217 {
5218 if (*e != ':') /* no terminating ':'? */
5219 break;
5220 end = TRUE;
5221 s = vim_strchr(s, ' ') + 1;
5222 }
5223 *e = NUL; /* truncate the set command */
5224
5225 if (*s != NUL) /* skip over an empty "::" */
5226 {
5227#ifdef FEAT_EVAL
5228 save_SID = current_SID;
5229 current_SID = SID_MODELINE;
5230#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005231 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#ifdef FEAT_EVAL
5233 current_SID = save_SID;
5234#endif
5235 if (retval == FAIL) /* stop if error found */
5236 break;
5237 }
5238 s = e + 1; /* advance to next part */
5239 }
5240
5241 sourcing_lnum = save_sourcing_lnum;
5242 sourcing_name = save_sourcing_name;
5243
5244 vim_free(linecopy);
5245 }
5246 return retval;
5247}
5248
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005249#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005251read_viminfo_bufferlist(
5252 vir_T *virp,
5253 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
5255 char_u *tab;
5256 linenr_T lnum;
5257 colnr_T col;
5258 buf_T *buf;
5259 char_u *sfname;
5260 char_u *xline;
5261
5262 /* Handle long line and escaped characters. */
5263 xline = viminfo_readstring(virp, 1, FALSE);
5264
5265 /* don't read in if there are files on the command-line or if writing: */
5266 if (xline != NULL && !writing && ARGCOUNT == 0
5267 && find_viminfo_parameter('%') != NULL)
5268 {
5269 /* Format is: <fname> Tab <lnum> Tab <col>.
5270 * Watch out for a Tab in the file name, work from the end. */
5271 lnum = 0;
5272 col = 0;
5273 tab = vim_strrchr(xline, '\t');
5274 if (tab != NULL)
5275 {
5276 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005277 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 tab = vim_strrchr(xline, '\t');
5279 if (tab != NULL)
5280 {
5281 *tab++ = '\0';
5282 lnum = atol((char *)tab);
5283 }
5284 }
5285
5286 /* Expand "~/" in the file name at "line + 1" to a full path.
5287 * Then try shortening it by comparing with the current directory */
5288 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005289 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5292 if (buf != NULL) /* just in case... */
5293 {
5294 buf->b_last_cursor.lnum = lnum;
5295 buf->b_last_cursor.col = col;
5296 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5297 }
5298 }
5299 vim_free(xline);
5300
5301 return viminfo_readline(virp);
5302}
5303
5304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005305write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
5307 buf_T *buf;
5308#ifdef FEAT_WINDOWS
5309 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005310 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311#endif
5312 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005313 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
5315 if (find_viminfo_parameter('%') == NULL)
5316 return;
5317
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005318 /* Without a number -1 is returned: do all buffers. */
5319 max_buffers = get_viminfo_parameter('%');
5320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005322#define LINE_BUF_LEN (MAXPATHL + 40)
5323 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 if (line == NULL)
5325 return;
5326
5327#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005328 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 set_last_cursor(win);
5330#else
5331 set_last_cursor(curwin);
5332#endif
5333
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005334 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5336 {
5337 if (buf->b_fname == NULL
5338 || !buf->b_p_bl
5339#ifdef FEAT_QUICKFIX
5340 || bt_quickfix(buf)
5341#endif
5342 || removable(buf->b_ffname))
5343 continue;
5344
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005345 if (max_buffers-- == 0)
5346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 putc('%', fp);
5348 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005349 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 (long)buf->b_last_cursor.lnum,
5351 buf->b_last_cursor.col);
5352 viminfo_writestring(fp, line);
5353 }
5354 vim_free(line);
5355}
5356#endif
5357
5358
5359/*
5360 * Return special buffer name.
5361 * Returns NULL when the buffer has a normal file name.
5362 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005363 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005364buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5367 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005368 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005369 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005370 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005371
5372 /*
5373 * For location list window, w_llist_ref points to the location list.
5374 * For quickfix window, w_llist_ref is NULL.
5375 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005376 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005377 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005378 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005379 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381#endif
5382#ifdef FEAT_QUICKFIX
5383 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5384 * contains the name as specified by the user */
5385 if (bt_nofile(buf))
5386 {
5387 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005388 return buf->b_sfname;
5389 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391#endif
5392 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005393 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 return NULL;
5395}
5396
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005397#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5398 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5399 || defined(PROTO)
5400/*
5401 * Find a window for buffer "buf".
5402 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5403 * If not found FAIL is returned.
5404 */
5405 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005406find_win_for_buf(
5407 buf_T *buf,
5408 win_T **wp,
5409 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005410{
5411 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5412 if ((*wp)->w_buffer == buf)
5413 goto win_found;
5414 return FAIL;
5415win_found:
5416 return OK;
5417}
5418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420#if defined(FEAT_SIGNS) || defined(PROTO)
5421/*
5422 * Insert the sign into the signlist.
5423 */
5424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005425insert_sign(
5426 buf_T *buf, /* buffer to store sign in */
5427 signlist_T *prev, /* previous sign entry */
5428 signlist_T *next, /* next sign entry */
5429 int id, /* sign ID */
5430 linenr_T lnum, /* line number which gets the mark */
5431 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 signlist_T *newsign;
5434
5435 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5436 if (newsign != NULL)
5437 {
5438 newsign->id = id;
5439 newsign->lnum = lnum;
5440 newsign->typenr = typenr;
5441 newsign->next = next;
5442#ifdef FEAT_NETBEANS_INTG
5443 newsign->prev = prev;
5444 if (next != NULL)
5445 next->prev = newsign;
5446#endif
5447
5448 if (prev == NULL)
5449 {
5450 /* When adding first sign need to redraw the windows to create the
5451 * column for signs. */
5452 if (buf->b_signlist == NULL)
5453 {
5454 redraw_buf_later(buf, NOT_VALID);
5455 changed_cline_bef_curs();
5456 }
5457
5458 /* first sign in signlist */
5459 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005460#ifdef FEAT_NETBEANS_INTG
5461 if (netbeans_active())
5462 buf->b_has_sign_column = TRUE;
5463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465 else
5466 prev->next = newsign;
5467 }
5468}
5469
5470/*
5471 * Add the sign into the signlist. Find the right spot to do it though.
5472 */
5473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005474buf_addsign(
5475 buf_T *buf, /* buffer to store sign in */
5476 int id, /* sign ID */
5477 linenr_T lnum, /* line number which gets the mark */
5478 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 signlist_T *sign; /* a sign in the signlist */
5481 signlist_T *prev; /* the previous sign */
5482
5483 prev = NULL;
5484 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5485 {
5486 if (lnum == sign->lnum && id == sign->id)
5487 {
5488 sign->typenr = typenr;
5489 return;
5490 }
5491 else if (
5492#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5493 id < 0 &&
5494#endif
5495 lnum < sign->lnum)
5496 {
5497#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5498 /* XXX - GRP: Is this because of sign slide problem? Or is it
5499 * really needed? Or is it because we allow multiple signs per
5500 * line? If so, should I add that feature to FEAT_SIGNS?
5501 */
5502 while (prev != NULL && prev->lnum == lnum)
5503 prev = prev->prev;
5504 if (prev == NULL)
5505 sign = buf->b_signlist;
5506 else
5507 sign = prev->next;
5508#endif
5509 insert_sign(buf, prev, sign, id, lnum, typenr);
5510 return;
5511 }
5512 prev = sign;
5513 }
5514#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5515 /* XXX - GRP: See previous comment */
5516 while (prev != NULL && prev->lnum == lnum)
5517 prev = prev->prev;
5518 if (prev == NULL)
5519 sign = buf->b_signlist;
5520 else
5521 sign = prev->next;
5522#endif
5523 insert_sign(buf, prev, sign, id, lnum, typenr);
5524
5525 return;
5526}
5527
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005528/*
5529 * For an existing, placed sign "markId" change the type to "typenr".
5530 * Returns the line number of the sign, or zero if the sign is not found.
5531 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005532 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005533buf_change_sign_type(
5534 buf_T *buf, /* buffer to store sign in */
5535 int markId, /* sign ID */
5536 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 signlist_T *sign; /* a sign in the signlist */
5539
5540 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5541 {
5542 if (sign->id == markId)
5543 {
5544 sign->typenr = typenr;
5545 return sign->lnum;
5546 }
5547 }
5548
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005549 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550}
5551
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005552 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005553buf_getsigntype(
5554 buf_T *buf,
5555 linenr_T lnum,
5556 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 signlist_T *sign; /* a sign in a b_signlist */
5559
5560 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5561 if (sign->lnum == lnum
5562 && (type == SIGN_ANY
5563# ifdef FEAT_SIGN_ICONS
5564 || (type == SIGN_ICON
5565 && sign_get_image(sign->typenr) != NULL)
5566# endif
5567 || (type == SIGN_TEXT
5568 && sign_get_text(sign->typenr) != NULL)
5569 || (type == SIGN_LINEHL
5570 && sign_get_attr(sign->typenr, TRUE) != 0)))
5571 return sign->typenr;
5572 return 0;
5573}
5574
5575
5576 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005577buf_delsign(
5578 buf_T *buf, /* buffer sign is stored in */
5579 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 signlist_T **lastp; /* pointer to pointer to current sign */
5582 signlist_T *sign; /* a sign in a b_signlist */
5583 signlist_T *next; /* the next sign in a b_signlist */
5584 linenr_T lnum; /* line number whose sign was deleted */
5585
5586 lastp = &buf->b_signlist;
5587 lnum = 0;
5588 for (sign = buf->b_signlist; sign != NULL; sign = next)
5589 {
5590 next = sign->next;
5591 if (sign->id == id)
5592 {
5593 *lastp = next;
5594#ifdef FEAT_NETBEANS_INTG
5595 if (next != NULL)
5596 next->prev = sign->prev;
5597#endif
5598 lnum = sign->lnum;
5599 vim_free(sign);
5600 break;
5601 }
5602 else
5603 lastp = &sign->next;
5604 }
5605
5606 /* When deleted the last sign need to redraw the windows to remove the
5607 * sign column. */
5608 if (buf->b_signlist == NULL)
5609 {
5610 redraw_buf_later(buf, NOT_VALID);
5611 changed_cline_bef_curs();
5612 }
5613
5614 return lnum;
5615}
5616
5617
5618/*
5619 * Find the line number of the sign with the requested id. If the sign does
5620 * not exist, return 0 as the line number. This will still let the correct file
5621 * get loaded.
5622 */
5623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005624buf_findsign(
5625 buf_T *buf, /* buffer to store sign in */
5626 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 signlist_T *sign; /* a sign in the signlist */
5629
5630 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5631 if (sign->id == id)
5632 return sign->lnum;
5633
5634 return 0;
5635}
5636
5637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005638buf_findsign_id(
5639 buf_T *buf, /* buffer whose sign we are searching for */
5640 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642 signlist_T *sign; /* a sign in the signlist */
5643
5644 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5645 if (sign->lnum == lnum)
5646 return sign->id;
5647
5648 return 0;
5649}
5650
5651
5652# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5653/* see if a given type of sign exists on a specific line */
5654 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005655buf_findsigntype_id(
5656 buf_T *buf, /* buffer whose sign we are searching for */
5657 linenr_T lnum, /* line number of sign */
5658 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
5660 signlist_T *sign; /* a sign in the signlist */
5661
5662 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5663 if (sign->lnum == lnum && sign->typenr == typenr)
5664 return sign->id;
5665
5666 return 0;
5667}
5668
5669
5670# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5671/* return the number of icons on the given line */
5672 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005673buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
5675 signlist_T *sign; /* a sign in the signlist */
5676 int count = 0;
5677
5678 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5679 if (sign->lnum == lnum)
5680 if (sign_get_image(sign->typenr) != NULL)
5681 count++;
5682
5683 return count;
5684}
5685# endif /* FEAT_SIGN_ICONS */
5686# endif /* FEAT_NETBEANS_INTG */
5687
5688
5689/*
5690 * Delete signs in buffer "buf".
5691 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005692 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005693buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 signlist_T *next;
5696
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005697 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005698 * sign column. Not when curwin is NULL (this means we're exiting). */
5699 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005700 {
5701 redraw_buf_later(buf, NOT_VALID);
5702 changed_cline_bef_curs();
5703 }
5704
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 while (buf->b_signlist != NULL)
5706 {
5707 next = buf->b_signlist->next;
5708 vim_free(buf->b_signlist);
5709 buf->b_signlist = next;
5710 }
5711}
5712
5713/*
5714 * Delete all signs in all buffers.
5715 */
5716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005717buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 buf_T *buf; /* buffer we are checking for signs */
5720
5721 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5722 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
5725
5726/*
5727 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5728 */
5729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005730sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 buf_T *buf;
5733 signlist_T *p;
5734 char lbuf[BUFSIZ];
5735
5736 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5737 msg_putchar('\n');
5738 if (rbuf == NULL)
5739 buf = firstbuf;
5740 else
5741 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005742 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
5744 if (buf->b_signlist != NULL)
5745 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005746 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5748 msg_putchar('\n');
5749 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005750 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005752 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5754 MSG_PUTS(lbuf);
5755 msg_putchar('\n');
5756 }
5757 if (rbuf != NULL)
5758 break;
5759 buf = buf->b_next;
5760 }
5761}
5762
5763/*
5764 * Adjust a placed sign for inserted/deleted lines.
5765 */
5766 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005767sign_mark_adjust(
5768 linenr_T line1,
5769 linenr_T line2,
5770 long amount,
5771 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
5773 signlist_T *sign; /* a sign in a b_signlist */
5774
5775 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5776 {
5777 if (sign->lnum >= line1 && sign->lnum <= line2)
5778 {
5779 if (amount == MAXLNUM)
5780 sign->lnum = line1;
5781 else
5782 sign->lnum += amount;
5783 }
5784 else if (sign->lnum > line2)
5785 sign->lnum += amount_after;
5786 }
5787}
5788#endif /* FEAT_SIGNS */
5789
5790/*
5791 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5792 */
5793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005794set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796 if (on != curbuf->b_p_bl)
5797 {
5798 curbuf->b_p_bl = on;
5799#ifdef FEAT_AUTOCMD
5800 if (on)
5801 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5802 else
5803 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5804#endif
5805 }
5806}
5807
5808/*
5809 * Read the file for "buf" again and check if the contents changed.
5810 * Return TRUE if it changed or this could not be checked.
5811 */
5812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005813buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
5815 buf_T *newbuf;
5816 int differ = TRUE;
5817 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 exarg_T ea;
5820
5821 /* Allocate a buffer without putting it in the buffer list. */
5822 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5823 if (newbuf == NULL)
5824 return TRUE;
5825
5826 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5827 if (prep_exarg(&ea, buf) == FAIL)
5828 {
5829 wipe_buffer(newbuf, FALSE);
5830 return TRUE;
5831 }
5832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 /* set curwin/curbuf to buf and save a few things */
5834 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
Bram Moolenaar4770d092006-01-12 23:22:24 +00005836 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 && readfile(buf->b_ffname, buf->b_fname,
5838 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5839 &ea, READ_NEW | READ_DUMMY) == OK)
5840 {
5841 /* compare the two files line by line */
5842 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5843 {
5844 differ = FALSE;
5845 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5846 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5847 {
5848 differ = TRUE;
5849 break;
5850 }
5851 }
5852 }
5853 vim_free(ea.cmd);
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 /* restore curwin/curbuf and a few other things */
5856 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
5858 if (curbuf != newbuf) /* safety check */
5859 wipe_buffer(newbuf, FALSE);
5860
5861 return differ;
5862}
5863
5864/*
5865 * Wipe out a buffer and decrement the last buffer number if it was used for
5866 * this buffer. Call this to wipe out a temp buffer that does not contain any
5867 * marks.
5868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005870wipe_buffer(
5871 buf_T *buf,
5872 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873{
5874 if (buf->b_fnum == top_file_num - 1)
5875 --top_file_num;
5876
5877#ifdef FEAT_AUTOCMD
5878 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005879 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005881 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882#ifdef FEAT_AUTOCMD
5883 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005884 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885#endif
5886}