blob: b0e3a22b507419c7da4b581a5ba11709a0416c8b [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 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100142 int old_msg_silent = msg_silent;
143
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_NETBEANS_INTG
145 int oldFire = netbeansFireChanges;
146
147 netbeansFireChanges = 0;
148#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100149 if (shortmess(SHM_FILEINFO))
150 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200152 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
153 flags | READ_NEW);
Bram Moolenaar426dd022016-03-15 15:09:29 +0100154 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#ifdef FEAT_NETBEANS_INTG
156 netbeansFireChanges = oldFire;
157#endif
158 /* Help buffer is filtered. */
159 if (curbuf->b_help)
160 fix_help_buffer();
161 }
162 else if (read_stdin)
163 {
164 int save_bin = curbuf->b_p_bin;
165 linenr_T line_count;
166
167 /*
168 * First read the text in binary mode into the buffer.
169 * Then read from that same buffer and append at the end. This makes
170 * it possible to retry when 'fileformat' or 'fileencoding' was
171 * guessed wrong.
172 */
173 curbuf->b_p_bin = TRUE;
174 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200175 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
176 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 curbuf->b_p_bin = save_bin;
178 if (retval == OK)
179 {
180 line_count = curbuf->b_ml.ml_line_count;
181 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200182 (linenr_T)0, (linenr_T)MAXLNUM, eap,
183 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 if (retval == OK)
185 {
186 /* Delete the binary lines. */
187 while (--line_count >= 0)
188 ml_delete((linenr_T)1, FALSE);
189 }
190 else
191 {
192 /* Delete the converted lines. */
193 while (curbuf->b_ml.ml_line_count > line_count)
194 ml_delete(line_count, FALSE);
195 }
196 /* Put the cursor on the first line. */
197 curwin->w_cursor.lnum = 1;
198 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000199
200 /* Set or reset 'modified' before executing autocommands, so that
201 * it can be changed there. */
202 if (!readonlymode && !bufempty())
203 changed();
204 else if (retval != FAIL)
205 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifdef FEAT_AUTOCMD
207# ifdef FEAT_EVAL
208 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
209 curbuf, &retval);
210# else
211 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
212# endif
213#endif
214 }
215 }
216
217 /* if first time loading this buffer, init b_chartab[] */
218 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100221#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100222 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100223#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
226 /*
227 * Set/reset the Changed flag first, autocmds may change the buffer.
228 * Apply the automatic commands, before processing the modelines.
229 * So the modelines have priority over auto commands.
230 */
231 /* When reading stdin, the buffer contents always needs writing, so set
232 * the changed flag. Unless in readonly mode: "ls | gview -".
233 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000234 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#ifdef FEAT_AUTOCMD
236 || modified_was_set /* ":set modified" used in autocmd */
237# ifdef FEAT_EVAL
238 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
239# endif
240#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000241 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000243 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 unchanged(curbuf, FALSE);
245 save_file_ff(curbuf); /* keep this fileformat */
246
247 /* require "!" to overwrite the file, because it wasn't read completely */
248#ifdef FEAT_EVAL
249 if (aborting())
250#else
251 if (got_int)
252#endif
253 curbuf->b_flags |= BF_READERR;
254
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000255#ifdef FEAT_FOLDING
256 /* Need to update automatic folding. Do this before the autocommands,
257 * they may use the fold info. */
258 foldUpdateAll(curwin);
259#endif
260
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#ifdef FEAT_AUTOCMD
262 /* need to set w_topline, unless some autocommand already did that. */
263 if (!(curwin->w_valid & VALID_TOPLINE))
264 {
265 curwin->w_topline = 1;
266# ifdef FEAT_DIFF
267 curwin->w_topfill = 0;
268# endif
269 }
270# ifdef FEAT_EVAL
271 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
272# else
273 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
274# endif
275#endif
276
277 if (retval != FAIL)
278 {
279#ifdef FEAT_AUTOCMD
280 /*
281 * The autocommands may have changed the current buffer. Apply the
282 * modelines to the correct buffer, if it still exists and is loaded.
283 */
284 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
285 {
286 aco_save_T aco;
287
288 /* Go to the buffer that was opened. */
289 aucmd_prepbuf(&aco, old_curbuf);
290#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000291 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
293
294#ifdef FEAT_AUTOCMD
295# ifdef FEAT_EVAL
296 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
297 &retval);
298# else
299 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
300# endif
301
302 /* restore curwin/curbuf and a few other things */
303 aucmd_restbuf(&aco);
304 }
305#endif
306 }
307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 return retval;
309}
310
311/*
312 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
313 */
314 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100315buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316{
317 buf_T *bp;
318
319 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
320 if (bp == buf)
321 return TRUE;
322 return FALSE;
323}
324
325/*
326 * Close the link to a buffer.
327 * "action" is used when there is no longer a window for the buffer.
328 * It can be:
329 * 0 buffer becomes hidden
330 * DOBUF_UNLOAD buffer is unloaded
331 * DOBUF_DELETE buffer is unloaded and removed from buffer list
332 * DOBUF_WIPE buffer is unloaded and really deleted
333 * When doing all but the first one on the current buffer, the caller should
334 * get a new buffer very soon!
335 *
336 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100337 *
338 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
339 * cause there to be only one window with this buffer. e.g. when ":quit" is
340 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 */
342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100343close_buffer(
344 win_T *win, /* if not NULL, set b_last_cursor */
345 buf_T *buf,
346 int action,
347 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348{
349#ifdef FEAT_AUTOCMD
350 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100351 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#endif
353 int unload_buf = (action != 0);
354 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
355 int wipe_buf = (action == DOBUF_WIPE);
356
357#ifdef FEAT_QUICKFIX
358 /*
359 * Force unloading or deleting when 'bufhidden' says so.
360 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
361 * "hide" (otherwise we could never free or delete a buffer).
362 */
363 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
364 {
365 del_buf = TRUE;
366 unload_buf = TRUE;
367 }
368 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
369 {
370 del_buf = TRUE;
371 unload_buf = TRUE;
372 wipe_buf = TRUE;
373 }
374 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
375 unload_buf = TRUE;
376#endif
377
Bram Moolenaar3be85852014-06-12 14:01:31 +0200378 if (win != NULL
379#ifdef FEAT_WINDOWS
380 && win_valid(win) /* in case autocommands closed the window */
381#endif
382 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 /* Set b_last_cursor when closing the last window for the buffer.
385 * Remember the last cursor position and window options of the buffer.
386 * This used to be only for the current window, but then options like
387 * 'foldmethod' may be lost with a ":only" command. */
388 if (buf->b_nwindows == 1)
389 set_last_cursor(win);
390 buflist_setfpos(buf, win,
391 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
392 win->w_cursor.col, TRUE);
393 }
394
395#ifdef FEAT_AUTOCMD
396 /* When the buffer is no longer in a window, trigger BufWinLeave */
397 if (buf->b_nwindows == 1)
398 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200399 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
401 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200402 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100403 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200404 /* Autocommands deleted the buffer. */
405aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100406 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100408 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200409 buf->b_closing = FALSE;
410 if (abort_if_last && one_window())
411 /* Autocommands made this the only window. */
412 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 /* When the buffer becomes hidden, but is not unloaded, trigger
415 * BufHidden */
416 if (!unload_buf)
417 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200418 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
420 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200421 if (!buf_valid(buf))
422 /* Autocommands deleted the buffer. */
423 goto aucmd_abort;
424 buf->b_closing = FALSE;
425 if (abort_if_last && one_window())
426 /* Autocommands made this the only window. */
427 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 }
429# ifdef FEAT_EVAL
430 if (aborting()) /* autocmds may abort script processing */
431 return;
432# endif
433 }
434 nwindows = buf->b_nwindows;
435#endif
436
437 /* decrease the link count from windows (unless not in any window) */
438 if (buf->b_nwindows > 0)
439 --buf->b_nwindows;
440
441 /* Return when a window is displaying the buffer or when it's not
442 * unloaded. */
443 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445
446 /* Always remove the buffer when there is no file name. */
447 if (buf->b_ffname == NULL)
448 del_buf = TRUE;
449
450 /*
451 * Free all things allocated for this buffer.
452 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
453 */
454#ifdef FEAT_AUTOCMD
455 /* Remember if we are closing the current buffer. Restore the number of
456 * windows, so that autocommands in buf_freeall() don't get confused. */
457 is_curbuf = (buf == curbuf);
458 buf->b_nwindows = nwindows;
459#endif
460
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200461 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200462 if (
463#ifdef FEAT_WINDOWS
464 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100465#else
466 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200467#endif
468 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200469 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471#ifdef FEAT_AUTOCMD
472 /* Autocommands may have deleted the buffer. */
473 if (!buf_valid(buf))
474 return;
475# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000476 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 return;
478# endif
479
480 /* Autocommands may have opened or closed windows for this buffer.
481 * Decrement the count for the close we do here. */
482 if (buf->b_nwindows > 0)
483 --buf->b_nwindows;
484
485 /*
486 * It's possible that autocommands change curbuf to the one being deleted.
487 * This might cause the previous curbuf to be deleted unexpectedly. But
488 * in some cases it's OK to delete the curbuf, because a new one is
489 * obtained anyway. Therefore only return if curbuf changed to the
490 * deleted buffer.
491 */
492 if (buf == curbuf && !is_curbuf)
493 return;
494#endif
495
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000496 /* Change directories when the 'acd' option is set. */
497 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498
499 /*
500 * Remove the buffer from the list.
501 */
502 if (wipe_buf)
503 {
504#ifdef FEAT_SUN_WORKSHOP
505 if (usingSunWorkShop)
506 workshop_file_closed_lineno((char *)buf->b_ffname,
507 (int)buf->b_last_cursor.lnum);
508#endif
509 vim_free(buf->b_ffname);
510 vim_free(buf->b_sfname);
511 if (buf->b_prev == NULL)
512 firstbuf = buf->b_next;
513 else
514 buf->b_prev->b_next = buf->b_next;
515 if (buf->b_next == NULL)
516 lastbuf = buf->b_prev;
517 else
518 buf->b_next->b_prev = buf->b_prev;
519 free_buffer(buf);
520 }
521 else
522 {
523 if (del_buf)
524 {
525 /* Free all internal variables and reset option values, to make
526 * ":bdel" compatible with Vim 5.7. */
527 free_buffer_stuff(buf, TRUE);
528
529 /* Make it look like a new buffer. */
530 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
531
532 /* Init the options when loaded again. */
533 buf->b_p_initialized = FALSE;
534 }
535 buf_clear_file(buf);
536 if (del_buf)
537 buf->b_p_bl = FALSE;
538 }
539}
540
541/*
542 * Make buffer not contain a file.
543 */
544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 buf->b_ml.ml_line_count = 1;
548 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 buf->b_p_eol = TRUE;
551 buf->b_start_eol = TRUE;
552#ifdef FEAT_MBYTE
553 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000554 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555#endif
556 buf->b_ml.ml_mfp = NULL;
557 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
558#ifdef FEAT_NETBEANS_INTG
559 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
561}
562
563/*
564 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200565 * the file. flags:
566 * BFA_DEL buffer is going to be deleted
567 * BFA_WIPE buffer is going to be wiped out
568 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100571buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572{
573#ifdef FEAT_AUTOCMD
574 int is_curbuf = (buf == curbuf);
575
Bram Moolenaar362ce482012-06-06 19:02:45 +0200576 buf->b_closing = TRUE;
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200577 if (buf->b_ml.ml_mfp != NULL)
578 {
579 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
580 if (!buf_valid(buf)) /* autocommands may delete the buffer */
581 return;
582 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200583 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {
585 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
586 if (!buf_valid(buf)) /* autocommands may delete the buffer */
587 return;
588 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200589 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {
591 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
592 FALSE, buf);
593 if (!buf_valid(buf)) /* autocommands may delete the buffer */
594 return;
595 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200596 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000598 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 return;
600# endif
601
602 /*
603 * It's possible that autocommands change curbuf to the one being deleted.
604 * This might cause curbuf to be deleted unexpectedly. But in some cases
605 * it's OK to delete the curbuf, because a new one is obtained anyway.
606 * Therefore only return if curbuf changed to the deleted buffer.
607 */
608 if (buf == curbuf && !is_curbuf)
609 return;
610#endif
611#ifdef FEAT_DIFF
612 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
613#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200614#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100615 /* Remove any ownsyntax, unless exiting. */
616 if (firstwin != NULL && curwin->w_buffer == buf)
617 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200618#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000619
620#ifdef FEAT_FOLDING
621 /* No folds in an empty buffer. */
622# ifdef FEAT_WINDOWS
623 {
624 win_T *win;
625 tabpage_T *tp;
626
627 FOR_ALL_TAB_WINDOWS(tp, win)
628 if (win->w_buffer == buf)
629 clearFolding(win);
630 }
631# else
632 if (curwin->w_buffer == buf)
633 clearFolding(curwin);
634# endif
635#endif
636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637#ifdef FEAT_TCL
638 tcl_buffer_free(buf);
639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 ml_close(buf, TRUE); /* close and delete the memline/memfile */
641 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200642 if ((flags & BFA_KEEP_UNDO) == 0)
643 {
644 u_blockfree(buf); /* free the memory allocated for undo */
645 u_clearall(buf); /* reset all undo information */
646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200648 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000650 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651}
652
653/*
654 * Free a buffer structure and the things it contains related to the buffer
655 * itself (not the file, that must have been done already).
656 */
657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100658free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200661#ifdef FEAT_EVAL
662 unref_var_dict(buf->b_vars);
663#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200664#ifdef FEAT_LUA
665 lua_buffer_free(buf);
666#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000667#ifdef FEAT_MZSCHEME
668 mzscheme_buffer_free(buf);
669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#ifdef FEAT_PERL
671 perl_buf_free(buf);
672#endif
673#ifdef FEAT_PYTHON
674 python_buffer_free(buf);
675#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200676#ifdef FEAT_PYTHON3
677 python3_buffer_free(buf);
678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#ifdef FEAT_RUBY
680 ruby_buffer_free(buf);
681#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200682#ifdef FEAT_JOB_CHANNEL
683 channel_buffer_free(buf);
684#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000685#ifdef FEAT_AUTOCMD
686 aubuflocal_remove(buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200687 if (autocmd_busy)
688 {
689 /* Do not free the buffer structure while autocommands are executing,
690 * it's still needed. Free it when autocmd_busy is reset. */
691 buf->b_next = au_pending_free_buf;
692 au_pending_free_buf = buf;
693 }
694 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000695#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200696 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
699/*
700 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
701 */
702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703free_buffer_stuff(
704 buf_T *buf,
705 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
707 if (free_options)
708 {
709 clear_wininfo(buf); /* including window-local options */
710 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200711#ifdef FEAT_SPELL
712 ga_clear(&buf->b_s.b_langp);
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 }
715#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200716 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
717 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#endif
719#ifdef FEAT_USR_CMDS
720 uc_clear(&buf->b_ucmds); /* clear local user commands */
721#endif
722#ifdef FEAT_SIGNS
723 buf_delete_signs(buf); /* delete any signs */
724#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000725#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200726 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#ifdef FEAT_LOCALMAP
729 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
730 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
731#endif
732#ifdef FEAT_MBYTE
733 vim_free(buf->b_start_fenc);
734 buf->b_start_fenc = NULL;
735#endif
736}
737
738/*
739 * Free the b_wininfo list for buffer "buf".
740 */
741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100742clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 wininfo_T *wip;
745
746 while (buf->b_wininfo != NULL)
747 {
748 wip = buf->b_wininfo;
749 buf->b_wininfo = wip->wi_next;
750 if (wip->wi_optset)
751 {
752 clear_winopt(&wip->wi_opt);
753#ifdef FEAT_FOLDING
754 deleteFoldRecurse(&wip->wi_folds);
755#endif
756 }
757 vim_free(wip);
758 }
759}
760
761#if defined(FEAT_LISTCMDS) || defined(PROTO)
762/*
763 * Go to another buffer. Handles the result of the ATTENTION dialog.
764 */
765 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100766goto_buffer(
767 exarg_T *eap,
768 int start,
769 int dir,
770 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000772# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 buf_T *old_curbuf = curbuf;
774
775 swap_exists_action = SEA_DIALOG;
776# endif
777 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
778 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000779# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
781 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000782# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
783 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000784
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000785 /* Reset the error/interrupt/exception state here so that
786 * aborting() returns FALSE when closing a window. */
787 enter_cleanup(&cs);
788# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000789
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000790 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 win_close(curwin, TRUE);
792 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000793 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000794
795# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
796 /* Restore the error/interrupt/exception state if not discarded by a
797 * new aborting error, interrupt, or uncaught exception. */
798 leave_cleanup(&cs);
799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801 else
802 handle_swap_exists(old_curbuf);
803# endif
804}
805#endif
806
Bram Moolenaare64ac772005-12-07 20:54:59 +0000807#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
809 * Handle the situation of swap_exists_action being set.
810 * It is allowed for "old_curbuf" to be NULL or invalid.
811 */
812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813handle_swap_exists(buf_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000815# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
816 cleanup_T cs;
817# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100818#ifdef FEAT_SYN_HL
819 long old_tw = curbuf->b_p_tw;
820#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (swap_exists_action == SEA_QUIT)
823 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000824# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
825 /* Reset the error/interrupt/exception state here so that
826 * aborting() returns FALSE when closing a buffer. */
827 enter_cleanup(&cs);
828# endif
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 /* User selected Quit at ATTENTION prompt. Go back to previous
831 * buffer. If that buffer is gone or the same as the current one,
832 * open a new, empty buffer. */
833 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000834 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100835 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000837 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100841#ifdef FEAT_SYN_HL
842 if (old_tw != curbuf->b_p_tw)
843 check_colorcolumn(curwin);
844#endif
845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000847
848# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
849 /* Restore the error/interrupt/exception state if not discarded by a
850 * new aborting error, interrupt, or uncaught exception. */
851 leave_cleanup(&cs);
852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 else if (swap_exists_action == SEA_RECOVER)
855 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000856# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
857 /* Reset the error/interrupt/exception state here so that
858 * aborting() returns FALSE when closing a buffer. */
859 enter_cleanup(&cs);
860# endif
861
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 /* User selected Recover at ATTENTION prompt. */
863 msg_scroll = TRUE;
864 ml_recover();
865 MSG_PUTS("\n"); /* don't overwrite the last message */
866 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000867 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000868
869# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
870 /* Restore the error/interrupt/exception state if not discarded by a
871 * new aborting error, interrupt, or uncaught exception. */
872 leave_cleanup(&cs);
873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875 swap_exists_action = SEA_NONE;
876}
877#endif
878
879#if defined(FEAT_LISTCMDS) || defined(PROTO)
880/*
881 * do_bufdel() - delete or unload buffer(s)
882 *
883 * addr_count == 0: ":bdel" - delete current buffer
884 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
885 * buffer "end_bnr", then any other arguments.
886 * addr_count == 2: ":N,N bdel" - delete buffers in range
887 *
888 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
889 * DOBUF_DEL (":bdel")
890 *
891 * Returns error message or NULL
892 */
893 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100894do_bufdel(
895 int command,
896 char_u *arg, /* pointer to extra arguments */
897 int addr_count,
898 int start_bnr, /* first buffer number in a range */
899 int end_bnr, /* buffer nr or last buffer nr in a range */
900 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901{
902 int do_current = 0; /* delete current buffer? */
903 int deleted = 0; /* number of buffers deleted */
904 char_u *errormsg = NULL; /* return value */
905 int bnr; /* buffer number */
906 char_u *p;
907
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (addr_count == 0)
909 {
910 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
911 }
912 else
913 {
914 if (addr_count == 2)
915 {
916 if (*arg) /* both range and argument is not allowed */
917 return (char_u *)_(e_trailing);
918 bnr = start_bnr;
919 }
920 else /* addr_count == 1 */
921 bnr = end_bnr;
922
923 for ( ;!got_int; ui_breakcheck())
924 {
925 /*
926 * delete the current buffer last, otherwise when the
927 * current buffer is deleted, the next buffer becomes
928 * the current one and will be loaded, which may then
929 * also be deleted, etc.
930 */
931 if (bnr == curbuf->b_fnum)
932 do_current = bnr;
933 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
934 forceit) == OK)
935 ++deleted;
936
937 /*
938 * find next buffer number to delete/unload
939 */
940 if (addr_count == 2)
941 {
942 if (++bnr > end_bnr)
943 break;
944 }
945 else /* addr_count == 1 */
946 {
947 arg = skipwhite(arg);
948 if (*arg == NUL)
949 break;
950 if (!VIM_ISDIGIT(*arg))
951 {
952 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100953 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
954 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (bnr < 0) /* failed */
956 break;
957 arg = p;
958 }
959 else
960 bnr = getdigits(&arg);
961 }
962 }
963 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
964 FORWARD, do_current, forceit) == OK)
965 ++deleted;
966
967 if (deleted == 0)
968 {
969 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000970 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000972 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000974 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 errormsg = IObuff;
976 }
977 else if (deleted >= p_report)
978 {
979 if (command == DOBUF_UNLOAD)
980 {
981 if (deleted == 1)
982 MSG(_("1 buffer unloaded"));
983 else
984 smsg((char_u *)_("%d buffers unloaded"), deleted);
985 }
986 else if (command == DOBUF_DEL)
987 {
988 if (deleted == 1)
989 MSG(_("1 buffer deleted"));
990 else
991 smsg((char_u *)_("%d buffers deleted"), deleted);
992 }
993 else
994 {
995 if (deleted == 1)
996 MSG(_("1 buffer wiped out"));
997 else
998 smsg((char_u *)_("%d buffers wiped out"), deleted);
999 }
1000 }
1001 }
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 return errormsg;
1005}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001006#endif /* FEAT_LISTCMDS */
1007
1008#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1009 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001011static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001012
1013/*
1014 * Make the current buffer empty.
1015 * Used when it is wiped out and it's the last buffer.
1016 */
1017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001018empty_curbuf(
1019 int close_others,
1020 int forceit,
1021 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001022{
1023 int retval;
1024 buf_T *buf = curbuf;
1025
1026 if (action == DOBUF_UNLOAD)
1027 {
1028 EMSG(_("E90: Cannot unload last buffer"));
1029 return FAIL;
1030 }
1031
1032 if (close_others)
1033 {
1034 /* Close any other windows on this buffer, then make it empty. */
1035#ifdef FEAT_WINDOWS
1036 close_windows(buf, TRUE);
1037#endif
1038 }
1039
1040 setpcmark();
1041 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1042 forceit ? ECMD_FORCEIT : 0, curwin);
1043
1044 /*
1045 * do_ecmd() may create a new buffer, then we have to delete
1046 * the old one. But do_ecmd() may have done that already, check
1047 * if the buffer still exists.
1048 */
1049 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1050 close_buffer(NULL, buf, action, FALSE);
1051 if (!close_others)
1052 need_fileinfo = FALSE;
1053 return retval;
1054}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055/*
1056 * Implementation of the commands for the buffer list.
1057 *
1058 * action == DOBUF_GOTO go to specified buffer
1059 * action == DOBUF_SPLIT split window and go to specified buffer
1060 * action == DOBUF_UNLOAD unload specified buffer(s)
1061 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1062 * action == DOBUF_WIPE delete specified buffer(s) really
1063 *
1064 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1065 * start == DOBUF_FIRST go to "count" buffer from first buffer
1066 * start == DOBUF_LAST go to "count" buffer from last buffer
1067 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1068 *
1069 * Return FAIL or OK.
1070 */
1071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001072do_buffer(
1073 int action,
1074 int start,
1075 int dir, /* FORWARD or BACKWARD */
1076 int count, /* buffer number or number of buffers */
1077 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
1079 buf_T *buf;
1080 buf_T *bp;
1081 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1082 || action == DOBUF_WIPE);
1083
1084 switch (start)
1085 {
1086 case DOBUF_FIRST: buf = firstbuf; break;
1087 case DOBUF_LAST: buf = lastbuf; break;
1088 default: buf = curbuf; break;
1089 }
1090 if (start == DOBUF_MOD) /* find next modified buffer */
1091 {
1092 while (count-- > 0)
1093 {
1094 do
1095 {
1096 buf = buf->b_next;
1097 if (buf == NULL)
1098 buf = firstbuf;
1099 }
1100 while (buf != curbuf && !bufIsChanged(buf));
1101 }
1102 if (!bufIsChanged(buf))
1103 {
1104 EMSG(_("E84: No modified buffer found"));
1105 return FAIL;
1106 }
1107 }
1108 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1109 {
1110 while (buf != NULL && buf->b_fnum != count)
1111 buf = buf->b_next;
1112 }
1113 else
1114 {
1115 bp = NULL;
1116 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1117 {
1118 /* remember the buffer where we start, we come back there when all
1119 * buffers are unlisted. */
1120 if (bp == NULL)
1121 bp = buf;
1122 if (dir == FORWARD)
1123 {
1124 buf = buf->b_next;
1125 if (buf == NULL)
1126 buf = firstbuf;
1127 }
1128 else
1129 {
1130 buf = buf->b_prev;
1131 if (buf == NULL)
1132 buf = lastbuf;
1133 }
1134 /* don't count unlisted buffers */
1135 if (unload || buf->b_p_bl)
1136 {
1137 --count;
1138 bp = NULL; /* use this buffer as new starting point */
1139 }
1140 if (bp == buf)
1141 {
1142 /* back where we started, didn't find anything. */
1143 EMSG(_("E85: There is no listed buffer"));
1144 return FAIL;
1145 }
1146 }
1147 }
1148
1149 if (buf == NULL) /* could not find it */
1150 {
1151 if (start == DOBUF_FIRST)
1152 {
1153 /* don't warn when deleting */
1154 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001155 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157 else if (dir == FORWARD)
1158 EMSG(_("E87: Cannot go beyond last buffer"));
1159 else
1160 EMSG(_("E88: Cannot go before first buffer"));
1161 return FAIL;
1162 }
1163
1164#ifdef FEAT_GUI
1165 need_mouse_correct = TRUE;
1166#endif
1167
1168#ifdef FEAT_LISTCMDS
1169 /*
1170 * delete buffer buf from memory and/or the list
1171 */
1172 if (unload)
1173 {
1174 int forward;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176 /* When unloading or deleting a buffer that's already unloaded and
1177 * unlisted: fail silently. */
1178 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1179 return FAIL;
1180
1181 if (!forceit && bufIsChanged(buf))
1182 {
1183#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1184 if ((p_confirm || cmdmod.confirm) && p_write)
1185 {
1186 dialog_changed(buf, FALSE);
1187# ifdef FEAT_AUTOCMD
1188 if (!buf_valid(buf))
1189 /* Autocommand deleted buffer, oops! It's not changed
1190 * now. */
1191 return FAIL;
1192# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001193 /* If it's still changed fail silently, the dialog already
1194 * mentioned why it fails. */
1195 if (bufIsChanged(buf))
1196 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#endif
1200 {
1201 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1202 buf->b_fnum);
1203 return FAIL;
1204 }
1205 }
1206
1207 /*
1208 * If deleting the last (listed) buffer, make it empty.
1209 * The last (listed) buffer cannot be unloaded.
1210 */
1211 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1212 if (bp->b_p_bl && bp != buf)
1213 break;
1214 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001215 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
1217#ifdef FEAT_WINDOWS
1218 /*
1219 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001220 * (unless it's the only window). Repeat this so long as we end up in
1221 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001223 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001224# ifdef FEAT_AUTOCMD
1225 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1226# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001227 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001228 {
1229 if (win_close(curwin, FALSE) == FAIL)
1230 break;
1231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#endif
1233
1234 /*
1235 * If the buffer to be deleted is not the current one, delete it here.
1236 */
1237 if (buf != curbuf)
1238 {
1239#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001240 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241#endif
1242 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001243 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 return OK;
1245 }
1246
1247 /*
1248 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001249 * There should be another, otherwise it would have been handled
1250 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 * First use au_new_curbuf, if it is valid.
1252 * Then prefer the buffer we most recently visited.
1253 * Else try to find one that is loaded, after the current buffer,
1254 * then before the current buffer.
1255 * Finally use any buffer.
1256 */
1257 buf = NULL; /* selected buffer */
1258 bp = NULL; /* used when no loaded buffer found */
1259#ifdef FEAT_AUTOCMD
1260 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1261 buf = au_new_curbuf;
1262# ifdef FEAT_JUMPLIST
1263 else
1264# endif
1265#endif
1266#ifdef FEAT_JUMPLIST
1267 if (curwin->w_jumplistlen > 0)
1268 {
1269 int jumpidx;
1270
1271 jumpidx = curwin->w_jumplistidx - 1;
1272 if (jumpidx < 0)
1273 jumpidx = curwin->w_jumplistlen - 1;
1274
1275 forward = jumpidx;
1276 while (jumpidx != curwin->w_jumplistidx)
1277 {
1278 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1279 if (buf != NULL)
1280 {
1281 if (buf == curbuf || !buf->b_p_bl)
1282 buf = NULL; /* skip current and unlisted bufs */
1283 else if (buf->b_ml.ml_mfp == NULL)
1284 {
1285 /* skip unloaded buf, but may keep it for later */
1286 if (bp == NULL)
1287 bp = buf;
1288 buf = NULL;
1289 }
1290 }
1291 if (buf != NULL) /* found a valid buffer: stop searching */
1292 break;
1293 /* advance to older entry in jump list */
1294 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1295 break;
1296 if (--jumpidx < 0)
1297 jumpidx = curwin->w_jumplistlen - 1;
1298 if (jumpidx == forward) /* List exhausted for sure */
1299 break;
1300 }
1301 }
1302#endif
1303
1304 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1305 {
1306 forward = TRUE;
1307 buf = curbuf->b_next;
1308 for (;;)
1309 {
1310 if (buf == NULL)
1311 {
1312 if (!forward) /* tried both directions */
1313 break;
1314 buf = curbuf->b_prev;
1315 forward = FALSE;
1316 continue;
1317 }
1318 /* in non-help buffer, try to skip help buffers, and vv */
1319 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1320 {
1321 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1322 break;
1323 if (bp == NULL) /* remember unloaded buf for later */
1324 bp = buf;
1325 }
1326 if (forward)
1327 buf = buf->b_next;
1328 else
1329 buf = buf->b_prev;
1330 }
1331 }
1332 if (buf == NULL) /* No loaded buffer, use unloaded one */
1333 buf = bp;
1334 if (buf == NULL) /* No loaded buffer, find listed one */
1335 {
1336 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1337 if (buf->b_p_bl && buf != curbuf)
1338 break;
1339 }
1340 if (buf == NULL) /* Still no buffer, just take one */
1341 {
1342 if (curbuf->b_next != NULL)
1343 buf = curbuf->b_next;
1344 else
1345 buf = curbuf->b_prev;
1346 }
1347 }
1348
Bram Moolenaara02471e2014-01-10 16:43:14 +01001349 if (buf == NULL)
1350 {
1351 /* Autocommands must have wiped out all other buffers. Only option
1352 * now is to make the current buffer empty. */
1353 return empty_curbuf(FALSE, forceit, action);
1354 }
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 /*
1357 * make buf current buffer
1358 */
1359 if (action == DOBUF_SPLIT) /* split window first */
1360 {
1361# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001362 /* If 'switchbuf' contains "useopen": jump to first window containing
1363 * "buf" if one exists */
1364 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001365 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001366 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001367 * page containing "buf" if one exists */
1368 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 return OK;
1370 if (win_split(0, 0) == FAIL)
1371# endif
1372 return FAIL;
1373 }
1374#endif
1375
1376 /* go to current buffer - nothing to do */
1377 if (buf == curbuf)
1378 return OK;
1379
1380 /*
1381 * Check if the current buffer may be abandoned.
1382 */
1383 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1384 {
1385#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1386 if ((p_confirm || cmdmod.confirm) && p_write)
1387 {
1388 dialog_changed(curbuf, FALSE);
1389# ifdef FEAT_AUTOCMD
1390 if (!buf_valid(buf))
1391 /* Autocommand deleted buffer, oops! */
1392 return FAIL;
1393# endif
1394 }
1395 if (bufIsChanged(curbuf))
1396#endif
1397 {
1398 EMSG(_(e_nowrtmsg));
1399 return FAIL;
1400 }
1401 }
1402
1403 /* Go to the other buffer. */
1404 set_curbuf(buf, action);
1405
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001406#if defined(FEAT_LISTCMDS) \
1407 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001409 {
1410 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#endif
1413
1414#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1415 if (aborting()) /* autocmds may abort script processing */
1416 return FAIL;
1417#endif
1418
1419 return OK;
1420}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
1423/*
1424 * Set current buffer to "buf". Executes autocommands and closes current
1425 * buffer. "action" tells how to close the current buffer:
1426 * DOBUF_GOTO free or hide it
1427 * DOBUF_SPLIT nothing
1428 * DOBUF_UNLOAD unload it
1429 * DOBUF_DEL delete it
1430 * DOBUF_WIPE wipe it out
1431 */
1432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001433set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 buf_T *prevbuf;
1436 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1437 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001438#ifdef FEAT_SYN_HL
1439 long old_tw = curbuf->b_p_tw;
1440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001443 if (!cmdmod.keepalt)
1444 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001445 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /* Don't restart Select mode after switching to another buffer. */
1448 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450 /* close_windows() or apply_autocmds() may change curbuf */
1451 prevbuf = curbuf;
1452
1453#ifdef FEAT_AUTOCMD
1454 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1455# ifdef FEAT_EVAL
1456 if (buf_valid(prevbuf) && !aborting())
1457# else
1458 if (buf_valid(prevbuf))
1459# endif
1460#endif
1461 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001462#ifdef FEAT_SYN_HL
1463 if (prevbuf == curwin->w_buffer)
1464 reset_synblock(curwin);
1465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#ifdef FEAT_WINDOWS
1467 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001468 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#endif
1470#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1471 if (buf_valid(prevbuf) && !aborting())
1472#else
1473 if (buf_valid(prevbuf))
1474#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001475 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001476#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001477 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001478#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001479 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001480 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1482 unload ? action : (action == DOBUF_GOTO
1483 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001484 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001485#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001486 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001487 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001488 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001489#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001493 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001494 * it did ":bunload") or aborted the script processing!
1495 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1496 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001497# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001498 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001499# endif
1500# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001501 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001502# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001503 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001507#ifdef FEAT_SYN_HL
1508 if (old_tw != curbuf->b_p_tw)
1509 check_colorcolumn(curwin);
1510#endif
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512}
1513
1514/*
1515 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001516 * Old curbuf must have been abandoned already! This also means "curbuf" may
1517 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
1519 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522 /* Copy buffer and window local option values. Not for a help buffer. */
1523 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1524 if (!buf->b_help)
1525 get_winopts(buf);
1526#ifdef FEAT_FOLDING
1527 else
1528 /* Remove all folds in the window. */
1529 clearFolding(curwin);
1530 foldUpdateAll(curwin); /* update folds (later). */
1531#endif
1532
1533 /* Get the buffer in the current window. */
1534 curwin->w_buffer = buf;
1535 curbuf = buf;
1536 ++curbuf->b_nwindows;
1537
1538#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001539 if (curwin->w_p_diff)
1540 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541#endif
1542
Bram Moolenaara971b822011-09-14 14:43:25 +02001543#ifdef FEAT_SYN_HL
1544 curwin->w_s = &(buf->b_s);
1545#endif
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 /* Cursor on first line by default. */
1548 curwin->w_cursor.lnum = 1;
1549 curwin->w_cursor.col = 0;
1550#ifdef FEAT_VIRTUALEDIT
1551 curwin->w_cursor.coladd = 0;
1552#endif
1553 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001554#ifdef FEAT_AUTOCMD
1555 curwin->w_topline_was_set = FALSE;
1556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001558 /* mark cursor position as being invalid */
1559 curwin->w_valid = 0;
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 /* Make sure the buffer is loaded. */
1562 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001563 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001564#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001565 /* If there is no filetype, allow for detecting one. Esp. useful for
1566 * ":ball" used in a autocommand. If there already is a filetype we
1567 * might prefer to keep it. */
1568 if (*curbuf->b_p_ft == NUL)
1569 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001570#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001571
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001572 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 else
1575 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001576 if (!msg_silent)
1577 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1579#ifdef FEAT_AUTOCMD
1580 curwin->w_topline = 1;
1581# ifdef FEAT_DIFF
1582 curwin->w_topfill = 0;
1583# endif
1584 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1585 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1586#endif
1587 }
1588
1589 /* If autocommands did not change the cursor position, restore cursor lnum
1590 * and possibly cursor col. */
1591 if (curwin->w_cursor.lnum == 1 && inindent(0))
1592 buflist_getfpos();
1593
1594 check_arg_idx(curwin); /* check for valid arg_idx */
1595#ifdef FEAT_TITLE
1596 maketitle();
1597#endif
1598#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001599 /* when autocmds didn't change it */
1600 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601#endif
1602 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1603
Bram Moolenaar009b2592004-10-24 19:18:58 +00001604#ifdef FEAT_NETBEANS_INTG
1605 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001606 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001607#endif
1608
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001609 /* Change directories when the 'acd' option is set. */
1610 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612#ifdef FEAT_KEYMAP
1613 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001614 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001616#ifdef FEAT_SPELL
1617 /* May need to set the spell language. Can only do this after the buffer
1618 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001619 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1620 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001621#endif
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 redraw_later(NOT_VALID);
1624}
1625
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001626#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1627/*
1628 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001629 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001630 */
1631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001632do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001633{
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001634 if (starting == 0
1635 && curbuf->b_ffname != NULL
1636 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001637 shorten_fnames(TRUE);
1638}
1639#endif
1640
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641/*
1642 * functions for dealing with the buffer list
1643 */
1644
1645/*
1646 * Add a file name to the buffer list. Return a pointer to the buffer.
1647 * If the same file name already exists return a pointer to that buffer.
1648 * If it does not exist, or if fname == NULL, a new entry is created.
1649 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1650 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1651 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 * This is the ONLY way to create a new buffer.
1653 */
1654static int top_file_num = 1; /* highest file number */
1655
1656 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001657buflist_new(
1658 char_u *ffname, /* full path of fname or relative */
1659 char_u *sfname, /* short fname or NULL */
1660 linenr_T lnum, /* preferred cursor line */
1661 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 buf_T *buf;
1664#ifdef UNIX
1665 struct stat st;
1666#endif
1667
1668 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1669
1670 /*
1671 * If file name already exists in the list, update the entry.
1672 */
1673#ifdef UNIX
1674 /* On Unix we can use inode numbers when the file exists. Works better
1675 * for hard links. */
1676 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1677 st.st_dev = (dev_T)-1;
1678#endif
1679 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1680#ifdef UNIX
1681 buflist_findname_stat(ffname, &st)
1682#else
1683 buflist_findname(ffname)
1684#endif
1685 ) != NULL)
1686 {
1687 vim_free(ffname);
1688 if (lnum != 0)
1689 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1690 /* copy the options now, if 'cpo' doesn't have 's' and not done
1691 * already */
1692 buf_copy_options(buf, 0);
1693 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1694 {
1695 buf->b_p_bl = TRUE;
1696#ifdef FEAT_AUTOCMD
1697 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001700 if (!buf_valid(buf))
1701 return NULL;
1702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
1704 }
1705 return buf;
1706 }
1707
1708 /*
1709 * If the current buffer has no name and no contents, use the current
1710 * buffer. Otherwise: Need to allocate a new buffer structure.
1711 *
1712 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001713 * (A spell file buffer is allocated in spell.c, but that's not a normal
1714 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 */
1716 buf = NULL;
1717 if ((flags & BLN_CURBUF)
1718 && curbuf != NULL
1719 && curbuf->b_ffname == NULL
1720 && curbuf->b_nwindows <= 1
1721 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1722 {
1723 buf = curbuf;
1724#ifdef FEAT_AUTOCMD
1725 /* It's like this buffer is deleted. Watch out for autocommands that
1726 * change curbuf! If that happens, allocate a new buffer anyway. */
1727 if (curbuf->b_p_bl)
1728 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1729 if (buf == curbuf)
1730 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1731# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001732 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 return NULL;
1734# endif
1735#endif
1736#ifdef FEAT_QUICKFIX
1737# ifdef FEAT_AUTOCMD
1738 if (buf == curbuf)
1739# endif
1740 {
1741 /* Make sure 'bufhidden' and 'buftype' are empty */
1742 clear_string_option(&buf->b_p_bh);
1743 clear_string_option(&buf->b_p_bt);
1744 }
1745#endif
1746 }
1747 if (buf != curbuf || curbuf == NULL)
1748 {
1749 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1750 if (buf == NULL)
1751 {
1752 vim_free(ffname);
1753 return NULL;
1754 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001755#ifdef FEAT_EVAL
1756 /* init b: variables */
1757 buf->b_vars = dict_alloc();
1758 if (buf->b_vars == NULL)
1759 {
1760 vim_free(ffname);
1761 vim_free(buf);
1762 return NULL;
1763 }
1764 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767
1768 if (ffname != NULL)
1769 {
1770 buf->b_ffname = ffname;
1771 buf->b_sfname = vim_strsave(sfname);
1772 }
1773
1774 clear_wininfo(buf);
1775 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1776
1777 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1778 || buf->b_wininfo == NULL)
1779 {
1780 vim_free(buf->b_ffname);
1781 buf->b_ffname = NULL;
1782 vim_free(buf->b_sfname);
1783 buf->b_sfname = NULL;
1784 if (buf != curbuf)
1785 free_buffer(buf);
1786 return NULL;
1787 }
1788
1789 if (buf == curbuf)
1790 {
1791 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001792 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 if (buf != curbuf) /* autocommands deleted the buffer! */
1794 return NULL;
1795#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001796 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 return NULL;
1798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001800
1801 /* Init the options. */
1802 buf->b_p_initialized = FALSE;
1803 buf_copy_options(buf, BCO_ENTER);
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805#ifdef FEAT_KEYMAP
1806 /* need to reload lmaps and set b:keymap_name */
1807 curbuf->b_kmap_state |= KEYMAP_INIT;
1808#endif
1809 }
1810 else
1811 {
1812 /*
1813 * put new buffer at the end of the buffer list
1814 */
1815 buf->b_next = NULL;
1816 if (firstbuf == NULL) /* buffer list is empty */
1817 {
1818 buf->b_prev = NULL;
1819 firstbuf = buf;
1820 }
1821 else /* append new buffer at end of list */
1822 {
1823 lastbuf->b_next = buf;
1824 buf->b_prev = lastbuf;
1825 }
1826 lastbuf = buf;
1827
1828 buf->b_fnum = top_file_num++;
1829 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1830 {
1831 EMSG(_("W14: Warning: List of file names overflow"));
1832 if (emsg_silent == 0)
1833 {
1834 out_flush();
1835 ui_delay(3000L, TRUE); /* make sure it is noticed */
1836 }
1837 top_file_num = 1;
1838 }
1839
1840 /*
1841 * Always copy the options from the current buffer.
1842 */
1843 buf_copy_options(buf, BCO_ALWAYS);
1844 }
1845
1846 buf->b_wininfo->wi_fpos.lnum = lnum;
1847 buf->b_wininfo->wi_win = curwin;
1848
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001849#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001850 hash_init(&buf->b_s.b_keywtab);
1851 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852#endif
1853
1854 buf->b_fname = buf->b_sfname;
1855#ifdef UNIX
1856 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001857 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 else
1859 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001860 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 buf->b_dev = st.st_dev;
1862 buf->b_ino = st.st_ino;
1863 }
1864#endif
1865 buf->b_u_synced = TRUE;
1866 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001867 if (flags & BLN_DUMMY)
1868 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 buf_clear_file(buf);
1870 clrallmarks(buf); /* clear marks */
1871 fmarks_check_names(buf); /* check file marks for this file */
1872 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1873#ifdef FEAT_AUTOCMD
1874 if (!(flags & BLN_DUMMY))
1875 {
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001876 /* Tricky: these autocommands may change the buffer list. They could
1877 * also split the window with re-using the one empty buffer. This may
1878 * result in unexpectedly losing the empty buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001880 if (!buf_valid(buf))
1881 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001885 if (!buf_valid(buf))
1886 return NULL;
1887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001889 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 return NULL;
1891# endif
1892 }
1893#endif
1894
1895 return buf;
1896}
1897
1898/*
1899 * Free the memory for the options of a buffer.
1900 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1901 * 'fileencoding'.
1902 */
1903 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001904free_buf_options(
1905 buf_T *buf,
1906 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907{
1908 if (free_p_ff)
1909 {
1910#ifdef FEAT_MBYTE
1911 clear_string_option(&buf->b_p_fenc);
1912#endif
1913 clear_string_option(&buf->b_p_ff);
1914#ifdef FEAT_QUICKFIX
1915 clear_string_option(&buf->b_p_bh);
1916 clear_string_option(&buf->b_p_bt);
1917#endif
1918 }
1919#ifdef FEAT_FIND_ID
1920 clear_string_option(&buf->b_p_def);
1921 clear_string_option(&buf->b_p_inc);
1922# ifdef FEAT_EVAL
1923 clear_string_option(&buf->b_p_inex);
1924# endif
1925#endif
1926#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1927 clear_string_option(&buf->b_p_inde);
1928 clear_string_option(&buf->b_p_indk);
1929#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001930#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1931 clear_string_option(&buf->b_p_bexpr);
1932#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001933#if defined(FEAT_CRYPT)
1934 clear_string_option(&buf->b_p_cm);
1935#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001936#if defined(FEAT_EVAL)
1937 clear_string_option(&buf->b_p_fex);
1938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939#ifdef FEAT_CRYPT
1940 clear_string_option(&buf->b_p_key);
1941#endif
1942 clear_string_option(&buf->b_p_kp);
1943 clear_string_option(&buf->b_p_mps);
1944 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001945 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 clear_string_option(&buf->b_p_isk);
1947#ifdef FEAT_KEYMAP
1948 clear_string_option(&buf->b_p_keymap);
1949 ga_clear(&buf->b_kmap_ga);
1950#endif
1951#ifdef FEAT_COMMENTS
1952 clear_string_option(&buf->b_p_com);
1953#endif
1954#ifdef FEAT_FOLDING
1955 clear_string_option(&buf->b_p_cms);
1956#endif
1957 clear_string_option(&buf->b_p_nf);
1958#ifdef FEAT_SYN_HL
1959 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001960 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001961#endif
1962#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001963 clear_string_option(&buf->b_s.b_p_spc);
1964 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001965 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001966 buf->b_s.b_cap_prog = NULL;
1967 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968#endif
1969#ifdef FEAT_SEARCHPATH
1970 clear_string_option(&buf->b_p_sua);
1971#endif
1972#ifdef FEAT_AUTOCMD
1973 clear_string_option(&buf->b_p_ft);
1974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975#ifdef FEAT_CINDENT
1976 clear_string_option(&buf->b_p_cink);
1977 clear_string_option(&buf->b_p_cino);
1978#endif
1979#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1980 clear_string_option(&buf->b_p_cinw);
1981#endif
1982#ifdef FEAT_INS_EXPAND
1983 clear_string_option(&buf->b_p_cpt);
1984#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001985#ifdef FEAT_COMPL_FUNC
1986 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001987 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989#ifdef FEAT_QUICKFIX
1990 clear_string_option(&buf->b_p_gp);
1991 clear_string_option(&buf->b_p_mp);
1992 clear_string_option(&buf->b_p_efm);
1993#endif
1994 clear_string_option(&buf->b_p_ep);
1995 clear_string_option(&buf->b_p_path);
1996 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01001997 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998#ifdef FEAT_INS_EXPAND
1999 clear_string_option(&buf->b_p_dict);
2000 clear_string_option(&buf->b_p_tsr);
2001#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002002#ifdef FEAT_TEXTOBJ
2003 clear_string_option(&buf->b_p_qe);
2004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002006 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002007#ifdef FEAT_LISP
2008 clear_string_option(&buf->b_p_lw);
2009#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002010 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011}
2012
2013/*
2014 * get alternate file n
2015 * set linenr to lnum or altfpos.lnum if lnum == 0
2016 * also set cursor column to altfpos.col if 'startofline' is not set.
2017 * if (options & GETF_SETMARK) call setpcmark()
2018 * if (options & GETF_ALT) we are jumping to an alternate file.
2019 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2020 *
2021 * return FAIL for failure, OK for success
2022 */
2023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002024buflist_getfile(
2025 int n,
2026 linenr_T lnum,
2027 int options,
2028 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 buf_T *buf;
2031#ifdef FEAT_WINDOWS
2032 win_T *wp = NULL;
2033#endif
2034 pos_T *fpos;
2035 colnr_T col;
2036
2037 buf = buflist_findnr(n);
2038 if (buf == NULL)
2039 {
2040 if ((options & GETF_ALT) && n == 0)
2041 EMSG(_(e_noalt));
2042 else
2043 EMSGN(_("E92: Buffer %ld not found"), n);
2044 return FAIL;
2045 }
2046
2047 /* if alternate file is the current buffer, nothing to do */
2048 if (buf == curbuf)
2049 return OK;
2050
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002051 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002052 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002053 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002055 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002056#ifdef FEAT_AUTOCMD
2057 if (curbuf_locked())
2058 return FAIL;
2059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
2061 /* altfpos may be changed by getfile(), get it now */
2062 if (lnum == 0)
2063 {
2064 fpos = buflist_findfpos(buf);
2065 lnum = fpos->lnum;
2066 col = fpos->col;
2067 }
2068 else
2069 col = 0;
2070
2071#ifdef FEAT_WINDOWS
2072 if (options & GETF_SWITCH)
2073 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002074 /* If 'switchbuf' contains "useopen": jump to first window containing
2075 * "buf" if one exists */
2076 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002078
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002079 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002080 * page containing "buf" if one exists */
2081 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002082 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002083
2084 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2085 * current buffer isn't empty: open new tab or window */
2086 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2087 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002089 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002090 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002091 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2092 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002094 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096 }
2097#endif
2098
2099 ++RedrawingDisabled;
2100 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2101 lnum, forceit) <= 0)
2102 {
2103 --RedrawingDisabled;
2104
2105 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2106 if (!p_sol && col != 0)
2107 {
2108 curwin->w_cursor.col = col;
2109 check_cursor_col();
2110#ifdef FEAT_VIRTUALEDIT
2111 curwin->w_cursor.coladd = 0;
2112#endif
2113 curwin->w_set_curswant = TRUE;
2114 }
2115 return OK;
2116 }
2117 --RedrawingDisabled;
2118 return FAIL;
2119}
2120
2121/*
2122 * go to the last know line number for the current buffer
2123 */
2124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002125buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 pos_T *fpos;
2128
2129 fpos = buflist_findfpos(curbuf);
2130
2131 curwin->w_cursor.lnum = fpos->lnum;
2132 check_cursor_lnum();
2133
2134 if (p_sol)
2135 curwin->w_cursor.col = 0;
2136 else
2137 {
2138 curwin->w_cursor.col = fpos->col;
2139 check_cursor_col();
2140#ifdef FEAT_VIRTUALEDIT
2141 curwin->w_cursor.coladd = 0;
2142#endif
2143 curwin->w_set_curswant = TRUE;
2144 }
2145}
2146
Bram Moolenaar81695252004-12-29 20:58:21 +00002147#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2148/*
2149 * Find file in buffer list by name (it has to be for the current window).
2150 * Returns NULL if not found.
2151 */
2152 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002153buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002154{
2155 char_u *ffname;
2156 buf_T *buf = NULL;
2157
2158 /* First make the name into a full path name */
2159 ffname = FullName_save(fname,
2160#ifdef UNIX
2161 TRUE /* force expansion, get rid of symbolic links */
2162#else
2163 FALSE
2164#endif
2165 );
2166 if (ffname != NULL)
2167 {
2168 buf = buflist_findname(ffname);
2169 vim_free(ffname);
2170 }
2171 return buf;
2172}
2173#endif
2174
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175/*
2176 * Find file in buffer list by name (it has to be for the current window).
2177 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002178 * Skips dummy buffers.
2179 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 */
2181 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002182buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183{
2184#ifdef UNIX
2185 struct stat st;
2186
2187 if (mch_stat((char *)ffname, &st) < 0)
2188 st.st_dev = (dev_T)-1;
2189 return buflist_findname_stat(ffname, &st);
2190}
2191
2192/*
2193 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2194 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002195 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 */
2197 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002198buflist_findname_stat(
2199 char_u *ffname,
2200 struct stat *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201{
2202#endif
2203 buf_T *buf;
2204
2205 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002206 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207#ifdef UNIX
2208 , stp
2209#endif
2210 ))
2211 return buf;
2212 return NULL;
2213}
2214
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002215#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2216 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217/*
2218 * Find file in buffer list by a regexp pattern.
2219 * Return fnum of the found buffer.
2220 * Return < 0 for error.
2221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223buflist_findpat(
2224 char_u *pattern,
2225 char_u *pattern_end, /* pointer to first char after pattern */
2226 int unlisted, /* find unlisted buffers */
2227 int diffmode UNUSED, /* find diff-mode buffers only */
2228 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
2230 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 int match = -1;
2232 int find_listed;
2233 char_u *pat;
2234 char_u *patend;
2235 int attempt;
2236 char_u *p;
2237 int toggledollar;
2238
2239 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2240 {
2241 if (*pattern == '%')
2242 match = curbuf->b_fnum;
2243 else
2244 match = curwin->w_alt_fnum;
2245#ifdef FEAT_DIFF
2246 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2247 match = -1;
2248#endif
2249 }
2250
2251 /*
2252 * Try four ways of matching a listed buffer:
2253 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002254 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 * attempt == 2: with '$' at end (only match at end)
2256 * attempt == 3: with '^' at start and '$' at end (only full match)
2257 * Repeat this for finding an unlisted buffer if there was no matching
2258 * listed buffer.
2259 */
2260 else
2261 {
2262 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2263 if (pat == NULL)
2264 return -1;
2265 patend = pat + STRLEN(pat) - 1;
2266 toggledollar = (patend > pat && *patend == '$');
2267
2268 /* First try finding a listed buffer. If not found and "unlisted"
2269 * is TRUE, try finding an unlisted buffer. */
2270 find_listed = TRUE;
2271 for (;;)
2272 {
2273 for (attempt = 0; attempt <= 3; ++attempt)
2274 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002275 regmatch_T regmatch;
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 /* may add '^' and '$' */
2278 if (toggledollar)
2279 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2280 p = pat;
2281 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2282 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002283 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2284 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 vim_free(pat);
2287 return -1;
2288 }
2289
2290 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2291 if (buf->b_p_bl == find_listed
2292#ifdef FEAT_DIFF
2293 && (!diffmode || diff_mode_buf(buf))
2294#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002295 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002297 if (curtab_only)
2298 {
2299 /* Ignore the match if the buffer is not open in
2300 * the current tab. */
2301#ifdef FEAT_WINDOWS
2302 win_T *wp;
2303
2304 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2305 if (wp->w_buffer == buf)
2306 break;
2307 if (wp == NULL)
2308 continue;
2309#else
2310 if (curwin->w_buffer != buf)
2311 continue;
2312#endif
2313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (match >= 0) /* already found a match */
2315 {
2316 match = -2;
2317 break;
2318 }
2319 match = buf->b_fnum; /* remember first match */
2320 }
2321
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002322 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (match >= 0) /* found one match */
2324 break;
2325 }
2326
2327 /* Only search for unlisted buffers if there was no match with
2328 * a listed buffer. */
2329 if (!unlisted || !find_listed || match != -1)
2330 break;
2331 find_listed = FALSE;
2332 }
2333
2334 vim_free(pat);
2335 }
2336
2337 if (match == -2)
2338 EMSG2(_("E93: More than one match for %s"), pattern);
2339 else if (match < 0)
2340 EMSG2(_("E94: No matching buffer for %s"), pattern);
2341 return match;
2342}
2343#endif
2344
2345#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2346
2347/*
2348 * Find all buffer names that match.
2349 * For command line expansion of ":buf" and ":sbuf".
2350 * Return OK if matches found, FAIL otherwise.
2351 */
2352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002353ExpandBufnames(
2354 char_u *pat,
2355 int *num_file,
2356 char_u ***file,
2357 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359 int count = 0;
2360 buf_T *buf;
2361 int round;
2362 char_u *p;
2363 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002364 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366 *num_file = 0; /* return values in case of FAIL */
2367 *file = NULL;
2368
Bram Moolenaar05159a02005-02-26 23:04:13 +00002369 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2370 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002372 patc = alloc((unsigned)STRLEN(pat) + 11);
2373 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002375 STRCPY(patc, "\\(^\\|[\\/]\\)");
2376 STRCPY(patc + 11, pat + 1);
2377 }
2378 else
2379 patc = pat;
2380
2381 /*
2382 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002383 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002384 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002385 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002386 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002387 regmatch_T regmatch;
2388
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002389 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002390 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002391 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2392 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002393 {
2394 if (patc != pat)
2395 vim_free(patc);
2396 return FAIL;
2397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 /*
2400 * round == 1: Count the matches.
2401 * round == 2: Build the array to keep the matches.
2402 */
2403 for (round = 1; round <= 2; ++round)
2404 {
2405 count = 0;
2406 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2407 {
2408 if (!buf->b_p_bl) /* skip unlisted buffers */
2409 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002410 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (p != NULL)
2412 {
2413 if (round == 1)
2414 ++count;
2415 else
2416 {
2417 if (options & WILD_HOME_REPLACE)
2418 p = home_replace_save(buf, p);
2419 else
2420 p = vim_strsave(p);
2421 (*file)[count++] = p;
2422 }
2423 }
2424 }
2425 if (count == 0) /* no match found, break here */
2426 break;
2427 if (round == 1)
2428 {
2429 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2430 if (*file == NULL)
2431 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002432 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002433 if (patc != pat)
2434 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return FAIL;
2436 }
2437 }
2438 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002439 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (count) /* match(es) found, break here */
2441 break;
2442 }
2443
Bram Moolenaar05159a02005-02-26 23:04:13 +00002444 if (patc != pat)
2445 vim_free(patc);
2446
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 *num_file = count;
2448 return (count == 0 ? FAIL : OK);
2449}
2450
2451#endif /* FEAT_CMDL_COMPL */
2452
2453#ifdef HAVE_BUFLIST_MATCH
2454/*
2455 * Check for a match on the file name for buffer "buf" with regprog "prog".
2456 */
2457 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002458buflist_match(
2459 regmatch_T *rmp,
2460 buf_T *buf,
2461 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 char_u *match;
2464
2465 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002466 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002468 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470 return match;
2471}
2472
2473/*
2474 * Try matching the regexp in "prog" with file name "name".
2475 * Return "name" when there is a match, NULL when not.
2476 */
2477 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002478fname_match(
2479 regmatch_T *rmp,
2480 char_u *name,
2481 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 char_u *match = NULL;
2484 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486 if (name != NULL)
2487 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002488 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002489 rmp->rm_ic = p_fic || ignore_case;
2490 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 match = name;
2492 else
2493 {
2494 /* Replace $(HOME) with '~' and try matching again. */
2495 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002496 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 match = name;
2498 vim_free(p);
2499 }
2500 }
2501
2502 return match;
2503}
2504#endif
2505
2506/*
2507 * find file in buffer list by number
2508 */
2509 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002510buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 buf_T *buf;
2513
2514 if (nr == 0)
2515 nr = curwin->w_alt_fnum;
2516 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2517 if (buf->b_fnum == nr)
2518 return (buf);
2519 return NULL;
2520}
2521
2522/*
2523 * Get name of file 'n' in the buffer list.
2524 * When the file has no name an empty string is returned.
2525 * home_replace() is used to shorten the file name (used for marks).
2526 * Returns a pointer to allocated memory, of NULL when failed.
2527 */
2528 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002529buflist_nr2name(
2530 int n,
2531 int fullname,
2532 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
2534 buf_T *buf;
2535
2536 buf = buflist_findnr(n);
2537 if (buf == NULL)
2538 return NULL;
2539 return home_replace_save(helptail ? buf : NULL,
2540 fullname ? buf->b_ffname : buf->b_fname);
2541}
2542
2543/*
2544 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2545 * When "copy_options" is TRUE save the local window option values.
2546 * When "lnum" is 0 only do the options.
2547 */
2548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002549buflist_setfpos(
2550 buf_T *buf,
2551 win_T *win,
2552 linenr_T lnum,
2553 colnr_T col,
2554 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 wininfo_T *wip;
2557
2558 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2559 if (wip->wi_win == win)
2560 break;
2561 if (wip == NULL)
2562 {
2563 /* allocate a new entry */
2564 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2565 if (wip == NULL)
2566 return;
2567 wip->wi_win = win;
2568 if (lnum == 0) /* set lnum even when it's 0 */
2569 lnum = 1;
2570 }
2571 else
2572 {
2573 /* remove the entry from the list */
2574 if (wip->wi_prev)
2575 wip->wi_prev->wi_next = wip->wi_next;
2576 else
2577 buf->b_wininfo = wip->wi_next;
2578 if (wip->wi_next)
2579 wip->wi_next->wi_prev = wip->wi_prev;
2580 if (copy_options && wip->wi_optset)
2581 {
2582 clear_winopt(&wip->wi_opt);
2583#ifdef FEAT_FOLDING
2584 deleteFoldRecurse(&wip->wi_folds);
2585#endif
2586 }
2587 }
2588 if (lnum != 0)
2589 {
2590 wip->wi_fpos.lnum = lnum;
2591 wip->wi_fpos.col = col;
2592 }
2593 if (copy_options)
2594 {
2595 /* Save the window-specific option values. */
2596 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2597#ifdef FEAT_FOLDING
2598 wip->wi_fold_manual = win->w_fold_manual;
2599 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2600#endif
2601 wip->wi_optset = TRUE;
2602 }
2603
2604 /* insert the entry in front of the list */
2605 wip->wi_next = buf->b_wininfo;
2606 buf->b_wininfo = wip;
2607 wip->wi_prev = NULL;
2608 if (wip->wi_next)
2609 wip->wi_next->wi_prev = wip;
2610
2611 return;
2612}
2613
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002614#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002615static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002616
2617/*
2618 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2619 * page. That's because a diff is local to a tab page.
2620 */
2621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002622wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002623{
2624 win_T *wp;
2625
2626 if (wip->wi_opt.wo_diff)
2627 {
2628 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2629 /* return FALSE when it's a window in the current tab page, thus
2630 * the buffer was in diff mode here */
2631 if (wip->wi_win == wp)
2632 return FALSE;
2633 return TRUE;
2634 }
2635 return FALSE;
2636}
2637#endif
2638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639/*
2640 * Find info for the current window in buffer "buf".
2641 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002642 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2643 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * Returns NULL when there isn't any info.
2645 */
2646 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002647find_wininfo(
2648 buf_T *buf,
2649 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 wininfo_T *wip;
2652
2653 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002654 if (wip->wi_win == curwin
2655#ifdef FEAT_DIFF
2656 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2657#endif
2658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002660
2661 /* If no wininfo for curwin, use the first in the list (that doesn't have
2662 * 'diff' set and is in another tab page). */
2663 if (wip == NULL)
2664 {
2665#ifdef FEAT_DIFF
2666 if (skip_diff_buffer)
2667 {
2668 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2669 if (!wininfo_other_tab_diff(wip))
2670 break;
2671 }
2672 else
2673#endif
2674 wip = buf->b_wininfo;
2675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 return wip;
2677}
2678
2679/*
2680 * Reset the local window options to the values last used in this window.
2681 * If the buffer wasn't used in this window before, use the values from
2682 * the most recently used window. If the values were never set, use the
2683 * global values for the window.
2684 */
2685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002686get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
2688 wininfo_T *wip;
2689
2690 clear_winopt(&curwin->w_onebuf_opt);
2691#ifdef FEAT_FOLDING
2692 clearFolding(curwin);
2693#endif
2694
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002695 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (wip != NULL && wip->wi_optset)
2697 {
2698 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2699#ifdef FEAT_FOLDING
2700 curwin->w_fold_manual = wip->wi_fold_manual;
2701 curwin->w_foldinvalid = TRUE;
2702 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2703#endif
2704 }
2705 else
2706 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2707
2708#ifdef FEAT_FOLDING
2709 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2710 if (p_fdls >= 0)
2711 curwin->w_p_fdl = p_fdls;
2712#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002713#ifdef FEAT_SYN_HL
2714 check_colorcolumn(curwin);
2715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716}
2717
2718/*
2719 * Find the position (lnum and col) for the buffer 'buf' for the current
2720 * window.
2721 * Returns a pointer to no_position if no position is found.
2722 */
2723 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002724buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
2726 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002727 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002729 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (wip != NULL)
2731 return &(wip->wi_fpos);
2732 else
2733 return &no_position;
2734}
2735
2736/*
2737 * Find the lnum for the buffer 'buf' for the current window.
2738 */
2739 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002740buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 return buflist_findfpos(buf)->lnum;
2743}
2744
2745#if defined(FEAT_LISTCMDS) || defined(PROTO)
2746/*
2747 * List all know file names (for :files and :buffers command).
2748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002750buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 buf_T *buf;
2753 int len;
2754 int i;
2755
2756 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2757 {
2758 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002759 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2760 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2761 || (vim_strchr(eap->arg, '+')
2762 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2763 || (vim_strchr(eap->arg, 'a')
2764 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2765 || (vim_strchr(eap->arg, 'h')
2766 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2767 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2768 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2769 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2770 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2771 || (vim_strchr(eap->arg, '#')
2772 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 continue;
2774 msg_putchar('\n');
2775 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002776 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 else
2778 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2779
Bram Moolenaar50cde822005-06-05 21:54:54 +00002780 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 buf->b_fnum,
2782 buf->b_p_bl ? ' ' : 'u',
2783 buf == curbuf ? '%' :
2784 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2785 buf->b_ml.ml_mfp == NULL ? ' ' :
2786 (buf->b_nwindows == 0 ? 'h' : 'a'),
2787 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2788 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002789 : (bufIsChanged(buf) ? '+' : ' '),
2790 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002791 if (len > IOSIZE - 20)
2792 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 i = 40 - vim_strsize(IObuff);
2796 do
2797 {
2798 IObuff[len++] = ' ';
2799 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002800 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2801 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002802 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 msg_outtrans(IObuff);
2804 out_flush(); /* output one line at a time */
2805 ui_breakcheck();
2806 }
2807}
2808#endif
2809
2810/*
2811 * Get file name and line number for file 'fnum'.
2812 * Used by DoOneCmd() for translating '%' and '#'.
2813 * Used by insert_reg() and cmdline_paste() for '#' register.
2814 * Return FAIL if not found, OK for success.
2815 */
2816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002817buflist_name_nr(
2818 int fnum,
2819 char_u **fname,
2820 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821{
2822 buf_T *buf;
2823
2824 buf = buflist_findnr(fnum);
2825 if (buf == NULL || buf->b_fname == NULL)
2826 return FAIL;
2827
2828 *fname = buf->b_fname;
2829 *lnum = buflist_findlnum(buf);
2830
2831 return OK;
2832}
2833
2834/*
2835 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2836 * The file name with the full path is also remembered, for when :cd is used.
2837 * Returns FAIL for failure (file name already in use by other buffer)
2838 * OK otherwise.
2839 */
2840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841setfname(
2842 buf_T *buf,
2843 char_u *ffname,
2844 char_u *sfname,
2845 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
Bram Moolenaar81695252004-12-29 20:58:21 +00002847 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848#ifdef UNIX
2849 struct stat st;
2850#endif
2851
2852 if (ffname == NULL || *ffname == NUL)
2853 {
2854 /* Removing the name. */
2855 vim_free(buf->b_ffname);
2856 vim_free(buf->b_sfname);
2857 buf->b_ffname = NULL;
2858 buf->b_sfname = NULL;
2859#ifdef UNIX
2860 st.st_dev = (dev_T)-1;
2861#endif
2862 }
2863 else
2864 {
2865 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2866 if (ffname == NULL) /* out of memory */
2867 return FAIL;
2868
2869 /*
2870 * if the file name is already used in another buffer:
2871 * - if the buffer is loaded, fail
2872 * - if the buffer is not loaded, delete it from the list
2873 */
2874#ifdef UNIX
2875 if (mch_stat((char *)ffname, &st) < 0)
2876 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002877#endif
2878 if (!(buf->b_flags & BF_DUMMY))
2879#ifdef UNIX
2880 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002882 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883#endif
2884 if (obuf != NULL && obuf != buf)
2885 {
2886 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2887 {
2888 if (message)
2889 EMSG(_("E95: Buffer with this name already exists"));
2890 vim_free(ffname);
2891 return FAIL;
2892 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002893 /* delete from the list */
2894 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
2896 sfname = vim_strsave(sfname);
2897 if (ffname == NULL || sfname == NULL)
2898 {
2899 vim_free(sfname);
2900 vim_free(ffname);
2901 return FAIL;
2902 }
2903#ifdef USE_FNAME_CASE
2904# ifdef USE_LONG_FNAME
2905 if (USE_LONG_FNAME)
2906# endif
2907 fname_case(sfname, 0); /* set correct case for short file name */
2908#endif
2909 vim_free(buf->b_ffname);
2910 vim_free(buf->b_sfname);
2911 buf->b_ffname = ffname;
2912 buf->b_sfname = sfname;
2913 }
2914 buf->b_fname = buf->b_sfname;
2915#ifdef UNIX
2916 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002917 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 else
2919 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002920 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 buf->b_dev = st.st_dev;
2922 buf->b_ino = st.st_ino;
2923 }
2924#endif
2925
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
2928 buf_name_changed(buf);
2929 return OK;
2930}
2931
2932/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002933 * Crude way of changing the name of a buffer. Use with care!
2934 * The name should be relative to the current directory.
2935 */
2936 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002937buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002938{
2939 buf_T *buf;
2940
2941 buf = buflist_findnr(fnum);
2942 if (buf != NULL)
2943 {
2944 vim_free(buf->b_sfname);
2945 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002946 buf->b_ffname = vim_strsave(name);
2947 buf->b_sfname = NULL;
2948 /* Allocate ffname and expand into full path. Also resolves .lnk
2949 * files on Win32. */
2950 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002951 buf->b_fname = buf->b_sfname;
2952 }
2953}
2954
2955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 * Take care of what needs to be done when the name of buffer "buf" has
2957 * changed.
2958 */
2959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002960buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
2962 /*
2963 * If the file name changed, also change the name of the swapfile
2964 */
2965 if (buf->b_ml.ml_mfp != NULL)
2966 ml_setname(buf);
2967
2968 if (curwin->w_buffer == buf)
2969 check_arg_idx(curwin); /* check file name for arg list */
2970#ifdef FEAT_TITLE
2971 maketitle(); /* set window title */
2972#endif
2973#ifdef FEAT_WINDOWS
2974 status_redraw_all(); /* status lines need to be redrawn */
2975#endif
2976 fmarks_check_names(buf); /* check named file marks */
2977 ml_timestamp(buf); /* reset timestamp */
2978}
2979
2980/*
2981 * set alternate file name for current window
2982 *
2983 * Used by do_one_cmd(), do_write() and do_ecmd().
2984 * Return the buffer.
2985 */
2986 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002987setaltfname(
2988 char_u *ffname,
2989 char_u *sfname,
2990 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
2992 buf_T *buf;
2993
2994 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2995 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002996 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 curwin->w_alt_fnum = buf->b_fnum;
2998 return buf;
2999}
3000
3001/*
3002 * Get alternate file name for current window.
3003 * Return NULL if there isn't any, and give error message if requested.
3004 */
3005 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003006getaltfname(
3007 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 char_u *fname;
3010 linenr_T dummy;
3011
3012 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3013 {
3014 if (errmsg)
3015 EMSG(_(e_noalt));
3016 return NULL;
3017 }
3018 return fname;
3019}
3020
3021/*
3022 * Add a file name to the buflist and return its number.
3023 * Uses same flags as buflist_new(), except BLN_DUMMY.
3024 *
3025 * used by qf_init(), main() and doarglist()
3026 */
3027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003028buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 buf_T *buf;
3031
3032 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3033 if (buf != NULL)
3034 return buf->b_fnum;
3035 return 0;
3036}
3037
3038#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3039/*
3040 * Adjust slashes in file names. Called after 'shellslash' was set.
3041 */
3042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003043buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 buf_T *bp;
3046
3047 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3048 {
3049 if (bp->b_ffname != NULL)
3050 slash_adjust(bp->b_ffname);
3051 if (bp->b_sfname != NULL)
3052 slash_adjust(bp->b_sfname);
3053 }
3054}
3055#endif
3056
3057/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003058 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 * Also save the local window option values.
3060 */
3061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003062buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003064 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065}
3066
3067/*
3068 * Return TRUE if 'ffname' is not the same file as current file.
3069 * Fname must have a full path (expanded by mch_FullName()).
3070 */
3071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003072otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 return otherfile_buf(curbuf, ffname
3075#ifdef UNIX
3076 , NULL
3077#endif
3078 );
3079}
3080
3081 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003082otherfile_buf(
3083 buf_T *buf,
3084 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085#ifdef UNIX
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086 , struct stat *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003088 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 /* no name is different */
3091 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3092 return TRUE;
3093 if (fnamecmp(ffname, buf->b_ffname) == 0)
3094 return FALSE;
3095#ifdef UNIX
3096 {
3097 struct stat st;
3098
3099 /* If no struct stat given, get it now */
3100 if (stp == NULL)
3101 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003102 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 st.st_dev = (dev_T)-1;
3104 stp = &st;
3105 }
3106 /* Use dev/ino to check if the files are the same, even when the names
3107 * are different (possible with links). Still need to compare the
3108 * name above, for when the file doesn't exist yet.
3109 * Problem: The dev/ino changes when a file is deleted (and created
3110 * again) and remains the same when renamed/moved. We don't want to
3111 * mch_stat() each buffer each time, that would be too slow. Get the
3112 * dev/ino again when they appear to match, but not when they appear
3113 * to be different: Could skip a buffer when it's actually the same
3114 * file. */
3115 if (buf_same_ino(buf, stp))
3116 {
3117 buf_setino(buf);
3118 if (buf_same_ino(buf, stp))
3119 return FALSE;
3120 }
3121 }
3122#endif
3123 return TRUE;
3124}
3125
3126#if defined(UNIX) || defined(PROTO)
3127/*
3128 * Set inode and device number for a buffer.
3129 * Must always be called when b_fname is changed!.
3130 */
3131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003132buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 struct stat st;
3135
3136 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3137 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003138 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 buf->b_dev = st.st_dev;
3140 buf->b_ino = st.st_ino;
3141 }
3142 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003143 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144}
3145
3146/*
3147 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3148 */
3149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003150buf_same_ino(
3151 buf_T *buf,
3152 struct stat *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003154 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 && stp->st_dev == buf->b_dev
3156 && stp->st_ino == buf->b_ino);
3157}
3158#endif
3159
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003160/*
3161 * Print info about the current buffer.
3162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003164fileinfo(
3165 int fullname, /* when non-zero print full path */
3166 int shorthelp,
3167 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 char_u *name;
3170 int n;
3171 char_u *p;
3172 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003173 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
3175 buffer = alloc(IOSIZE);
3176 if (buffer == NULL)
3177 return;
3178
3179 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3180 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003181 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 p = buffer + STRLEN(buffer);
3183 }
3184 else
3185 p = buffer;
3186
3187 *p++ = '"';
3188 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003189 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else
3191 {
3192 if (!fullname && curbuf->b_fname != NULL)
3193 name = curbuf->b_fname;
3194 else
3195 name = curbuf->b_ffname;
3196 home_replace(shorthelp ? curbuf : NULL, name, p,
3197 (int)(IOSIZE - (p - buffer)), TRUE);
3198 }
3199
Bram Moolenaara800b422010-06-27 01:15:55 +02003200 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 curbufIsChanged() ? (shortmess(SHM_MOD)
3202 ? " [+]" : _(" [Modified]")) : " ",
3203 (curbuf->b_flags & BF_NOTEDITED)
3204#ifdef FEAT_QUICKFIX
3205 && !bt_dontwrite(curbuf)
3206#endif
3207 ? _("[Not edited]") : "",
3208 (curbuf->b_flags & BF_NEW)
3209#ifdef FEAT_QUICKFIX
3210 && !bt_dontwrite(curbuf)
3211#endif
3212 ? _("[New file]") : "",
3213 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003214 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 : _("[readonly]")) : "",
3216 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3217 || curbuf->b_p_ro) ?
3218 " " : "");
3219 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3220 * causes an overflow, thus for large numbers divide instead. */
3221 if (curwin->w_cursor.lnum > 1000000L)
3222 n = (int)(((long)curwin->w_cursor.lnum) /
3223 ((long)curbuf->b_ml.ml_line_count / 100L));
3224 else
3225 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3226 (long)curbuf->b_ml.ml_line_count);
3227 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3228 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003229 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
3231#ifdef FEAT_CMDL_INFO
3232 else if (p_ru)
3233 {
3234 /* Current line and column are already on the screen -- webb */
3235 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003236 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003238 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 (long)curbuf->b_ml.ml_line_count, n);
3240 }
3241#endif
3242 else
3243 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003244 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 _("line %ld of %ld --%d%%-- col "),
3246 (long)curwin->w_cursor.lnum,
3247 (long)curbuf->b_ml.ml_line_count,
3248 n);
3249 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003250 len = STRLEN(buffer);
3251 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3253 }
3254
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003255 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
3257 if (dont_truncate)
3258 {
3259 /* Temporarily set msg_scroll to avoid the message being truncated.
3260 * First call msg_start() to get the message in the right place. */
3261 msg_start();
3262 n = msg_scroll;
3263 msg_scroll = TRUE;
3264 msg(buffer);
3265 msg_scroll = n;
3266 }
3267 else
3268 {
3269 p = msg_trunc_attr(buffer, FALSE, 0);
3270 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 /* Need to repeat the message after redrawing when:
3272 * - When restart_edit is set (otherwise there will be a delay
3273 * before redrawing).
3274 * - When the screen was scrolled but there is no wait-return
3275 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003276 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
3278
3279 vim_free(buffer);
3280}
3281
3282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003283col_print(
3284 char_u *buf,
3285 size_t buflen,
3286 int col,
3287 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
3289 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003290 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003292 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293}
3294
3295#if defined(FEAT_TITLE) || defined(PROTO)
3296/*
3297 * put file name in title bar of window and in icon title
3298 */
3299
3300static char_u *lasttitle = NULL;
3301static char_u *lasticon = NULL;
3302
3303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003304maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 char_u *p;
3307 char_u *t_str = NULL;
3308 char_u *i_name;
3309 char_u *i_str = NULL;
3310 int maxlen = 0;
3311 int len;
3312 int mustset;
3313 char_u buf[IOSIZE];
3314 int off;
3315
3316 if (!redrawing())
3317 {
3318 /* Postpone updating the title when 'lazyredraw' is set. */
3319 need_maketitle = TRUE;
3320 return;
3321 }
3322
3323 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003324 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 return;
3326
3327 if (p_title)
3328 {
3329 if (p_titlelen > 0)
3330 {
3331 maxlen = p_titlelen * Columns / 100;
3332 if (maxlen < 10)
3333 maxlen = 10;
3334 }
3335
3336 t_str = buf;
3337 if (*p_titlestring != NUL)
3338 {
3339#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003340 if (stl_syntax & STL_IN_TITLE)
3341 {
3342 int use_sandbox = FALSE;
3343 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003344
3345# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003346 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003347# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003348 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003350 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003351 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003352 if (called_emsg)
3353 set_string_option_direct((char_u *)"titlestring", -1,
3354 (char_u *)"", OPT_FREE, SID_ERROR);
3355 called_emsg |= save_called_emsg;
3356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
3358#endif
3359 t_str = p_titlestring;
3360 }
3361 else
3362 {
3363 /* format: "fname + (path) (1 of 2) - VIM" */
3364
Bram Moolenaar2c666692012-09-05 13:30:40 +02003365#define SPACE_FOR_FNAME (IOSIZE - 100)
3366#define SPACE_FOR_DIR (IOSIZE - 20)
3367#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003369 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else
3371 {
3372 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003373 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376
3377 switch (bufIsChanged(curbuf)
3378 + (curbuf->b_p_ro * 2)
3379 + (!curbuf->b_p_ma * 4))
3380 {
3381 case 1: STRCAT(buf, " +"); break;
3382 case 2: STRCAT(buf, " ="); break;
3383 case 3: STRCAT(buf, " =+"); break;
3384 case 4:
3385 case 6: STRCAT(buf, " -"); break;
3386 case 5:
3387 case 7: STRCAT(buf, " -+"); break;
3388 }
3389
3390 if (curbuf->b_fname != NULL)
3391 {
3392 /* Get path of file, replace home dir with ~ */
3393 off = (int)STRLEN(buf);
3394 buf[off++] = ' ';
3395 buf[off++] = '(';
3396 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003397 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398#ifdef BACKSLASH_IN_FILENAME
3399 /* avoid "c:/name" to be reduced to "c" */
3400 if (isalpha(buf[off]) && buf[off + 1] == ':')
3401 off += 2;
3402#endif
3403 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003404 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003407 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003408 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003411
Bram Moolenaar2c666692012-09-05 13:30:40 +02003412 /* Translate unprintable chars and concatenate. Keep some
3413 * room for the server name. When there is no room (very long
3414 * file name) use (...). */
3415 if (off < SPACE_FOR_DIR)
3416 {
3417 p = transstr(buf + off);
3418 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3419 vim_free(p);
3420 }
3421 else
3422 {
3423 vim_strncpy(buf + off, (char_u *)"...",
3424 (size_t)(SPACE_FOR_ARGNR - off));
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 STRCAT(buf, ")");
3427 }
3428
Bram Moolenaar2c666692012-09-05 13:30:40 +02003429 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431#if defined(FEAT_CLIENTSERVER)
3432 if (serverName != NULL)
3433 {
3434 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003435 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 else
3438#endif
3439 STRCAT(buf, " - VIM");
3440
3441 if (maxlen > 0)
3442 {
3443 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003444 if (vim_strsize(buf) > maxlen)
3445 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447 }
3448 }
3449 mustset = ti_change(t_str, &lasttitle);
3450
3451 if (p_icon)
3452 {
3453 i_str = buf;
3454 if (*p_iconstring != NUL)
3455 {
3456#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003457 if (stl_syntax & STL_IN_ICON)
3458 {
3459 int use_sandbox = FALSE;
3460 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003461
3462# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003463 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003464# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003465 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003467 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003468 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003469 if (called_emsg)
3470 set_string_option_direct((char_u *)"iconstring", -1,
3471 (char_u *)"", OPT_FREE, SID_ERROR);
3472 called_emsg |= save_called_emsg;
3473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 else
3475#endif
3476 i_str = p_iconstring;
3477 }
3478 else
3479 {
3480 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003481 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else /* use file name only in icon */
3483 i_name = gettail(curbuf->b_ffname);
3484 *i_str = NUL;
3485 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (len > 100)
3488 {
3489 len -= 100;
3490#ifdef FEAT_MBYTE
3491 if (has_mbyte)
3492 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3493#endif
3494 i_name += len;
3495 }
3496 STRCPY(i_str, i_name);
3497 trans_characters(i_str, IOSIZE);
3498 }
3499 }
3500
3501 mustset |= ti_change(i_str, &lasticon);
3502
3503 if (mustset)
3504 resettitle();
3505}
3506
3507/*
3508 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3509 * from "str" if it does.
3510 * Return TRUE when "*last" changed.
3511 */
3512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003513ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 if ((str == NULL) != (*last == NULL)
3516 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3517 {
3518 vim_free(*last);
3519 if (str == NULL)
3520 *last = NULL;
3521 else
3522 *last = vim_strsave(str);
3523 return TRUE;
3524 }
3525 return FALSE;
3526}
3527
3528/*
3529 * Put current window title back (used after calling a shell)
3530 */
3531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003532resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533{
3534 mch_settitle(lasttitle, lasticon);
3535}
Bram Moolenaarea408852005-06-25 22:49:46 +00003536
3537# if defined(EXITFREE) || defined(PROTO)
3538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003539free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003540{
3541 vim_free(lasttitle);
3542 vim_free(lasticon);
3543}
3544# endif
3545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546#endif /* FEAT_TITLE */
3547
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003548#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003550 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 * Return length of string in screen cells.
3552 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003553 * Normally works for window "wp", except when working for 'tabline' then it
3554 * is "curwin".
3555 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 * Items are drawn interspersed with the text that surrounds it
3557 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3558 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3559 *
3560 * If maxwidth is not zero, the string will be filled at any middle marker
3561 * or truncated if too long, fillchar is used for all whitespace.
3562 */
3563 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003564build_stl_str_hl(
3565 win_T *wp,
3566 char_u *out, /* buffer to write into != NameBuff */
3567 size_t outlen, /* length of out[] */
3568 char_u *fmt,
3569 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3570 int fillchar,
3571 int maxwidth,
3572 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3573 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
3575 char_u *p;
3576 char_u *s;
3577 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003578 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579#ifdef FEAT_EVAL
3580 win_T *o_curwin;
3581 buf_T *o_curbuf;
3582#endif
3583 int empty_line;
3584 colnr_T virtcol;
3585 long l;
3586 long n;
3587 int prevchar_isflag;
3588 int prevchar_isitem;
3589 int itemisflag;
3590 int fillable;
3591 char_u *str;
3592 long num;
3593 int width;
3594 int itemcnt;
3595 int curitem;
3596 int groupitem[STL_MAX_ITEM];
3597 int groupdepth;
3598 struct stl_item
3599 {
3600 char_u *start;
3601 int minwid;
3602 int maxwid;
3603 enum
3604 {
3605 Normal,
3606 Empty,
3607 Group,
3608 Middle,
3609 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003610 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 Trunc
3612 } type;
3613 } item[STL_MAX_ITEM];
3614 int minwid;
3615 int maxwid;
3616 int zeropad;
3617 char_u base;
3618 char_u opt;
3619#define TMPLEN 70
3620 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003621 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003622 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003623
3624#ifdef FEAT_EVAL
3625 /*
3626 * When the format starts with "%!" then evaluate it as an expression and
3627 * use the result as the actual format string.
3628 */
3629 if (fmt[0] == '%' && fmt[1] == '!')
3630 {
3631 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3632 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003633 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003634 }
3635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 if (fillchar == 0)
3638 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003639#ifdef FEAT_MBYTE
3640 /* Can't handle a multi-byte fill character yet. */
3641 else if (mb_char2len(fillchar) > 1)
3642 fillchar = '-';
3643#endif
3644
Bram Moolenaar567199b2013-04-24 16:52:36 +02003645 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3646 * p will become invalid when getting another buffer line. */
3647 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3648 empty_line = (*p == NUL);
3649
3650 /* Get the byte value now, in case we need it below. This is more
3651 * efficient than making a copy of the line. */
3652 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3653 byteval = 0;
3654 else
3655#ifdef FEAT_MBYTE
3656 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3657#else
3658 byteval = p[wp->w_cursor.col];
3659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 groupdepth = 0;
3662 p = out;
3663 curitem = 0;
3664 prevchar_isflag = TRUE;
3665 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003666 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003668 if (curitem == STL_MAX_ITEM)
3669 {
3670 /* There are too many items. Add the error code to the statusline
3671 * to give the user a hint about what went wrong. */
3672 if (p + 6 < out + outlen)
3673 {
3674 mch_memmove(p, " E541", (size_t)5);
3675 p += 5;
3676 }
3677 break;
3678 }
3679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (*s != NUL && *s != '%')
3681 prevchar_isflag = prevchar_isitem = FALSE;
3682
3683 /*
3684 * Handle up to the next '%' or the end.
3685 */
3686 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3687 *p++ = *s++;
3688 if (*s == NUL || p + 1 >= out + outlen)
3689 break;
3690
3691 /*
3692 * Handle one '%' item.
3693 */
3694 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003695 if (*s == NUL) /* ignore trailing % */
3696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (*s == '%')
3698 {
3699 if (p + 1 >= out + outlen)
3700 break;
3701 *p++ = *s++;
3702 prevchar_isflag = prevchar_isitem = FALSE;
3703 continue;
3704 }
3705 if (*s == STL_MIDDLEMARK)
3706 {
3707 s++;
3708 if (groupdepth > 0)
3709 continue;
3710 item[curitem].type = Middle;
3711 item[curitem++].start = p;
3712 continue;
3713 }
3714 if (*s == STL_TRUNCMARK)
3715 {
3716 s++;
3717 item[curitem].type = Trunc;
3718 item[curitem++].start = p;
3719 continue;
3720 }
3721 if (*s == ')')
3722 {
3723 s++;
3724 if (groupdepth < 1)
3725 continue;
3726 groupdepth--;
3727
3728 t = item[groupitem[groupdepth]].start;
3729 *p = NUL;
3730 l = vim_strsize(t);
3731 if (curitem > groupitem[groupdepth] + 1
3732 && item[groupitem[groupdepth]].minwid == 0)
3733 {
3734 /* remove group if all items are empty */
3735 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003736 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 break;
3738 if (n == curitem)
3739 {
3740 p = t;
3741 l = 0;
3742 }
3743 }
3744 if (l > item[groupitem[groupdepth]].maxwid)
3745 {
3746 /* truncate, remove n bytes of text at the start */
3747#ifdef FEAT_MBYTE
3748 if (has_mbyte)
3749 {
3750 /* Find the first character that should be included. */
3751 n = 0;
3752 while (l >= item[groupitem[groupdepth]].maxwid)
3753 {
3754 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003755 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 }
3758 else
3759#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003760 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
3762 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003763 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 p = p - n + 1;
3765#ifdef FEAT_MBYTE
3766 /* Fill up space left over by half a double-wide char. */
3767 while (++l < item[groupitem[groupdepth]].minwid)
3768 *p++ = fillchar;
3769#endif
3770
3771 /* correct the start of the items for the truncation */
3772 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3773 {
3774 item[l].start -= n;
3775 if (item[l].start < t)
3776 item[l].start = t;
3777 }
3778 }
3779 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3780 {
3781 /* fill */
3782 n = item[groupitem[groupdepth]].minwid;
3783 if (n < 0)
3784 {
3785 /* fill by appending characters */
3786 n = 0 - n;
3787 while (l++ < n && p + 1 < out + outlen)
3788 *p++ = fillchar;
3789 }
3790 else
3791 {
3792 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003793 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 l = n - l;
3795 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003796 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 p += l;
3798 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3799 item[n].start += l;
3800 for ( ; l > 0; l--)
3801 *t++ = fillchar;
3802 }
3803 }
3804 continue;
3805 }
3806 minwid = 0;
3807 maxwid = 9999;
3808 zeropad = FALSE;
3809 l = 1;
3810 if (*s == '0')
3811 {
3812 s++;
3813 zeropad = TRUE;
3814 }
3815 if (*s == '-')
3816 {
3817 s++;
3818 l = -1;
3819 }
3820 if (VIM_ISDIGIT(*s))
3821 {
3822 minwid = (int)getdigits(&s);
3823 if (minwid < 0) /* overflow */
3824 minwid = 0;
3825 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003826 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
3828 item[curitem].type = Highlight;
3829 item[curitem].start = p;
3830 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3831 s++;
3832 curitem++;
3833 continue;
3834 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003835 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3836 {
3837 if (*s == STL_TABCLOSENR)
3838 {
3839 if (minwid == 0)
3840 {
3841 /* %X ends the close label, go back to the previously
3842 * define tab label nr. */
3843 for (n = curitem - 1; n >= 0; --n)
3844 if (item[n].type == TabPage && item[n].minwid >= 0)
3845 {
3846 minwid = item[n].minwid;
3847 break;
3848 }
3849 }
3850 else
3851 /* close nrs are stored as negative values */
3852 minwid = - minwid;
3853 }
3854 item[curitem].type = TabPage;
3855 item[curitem].start = p;
3856 item[curitem].minwid = minwid;
3857 s++;
3858 curitem++;
3859 continue;
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (*s == '.')
3862 {
3863 s++;
3864 if (VIM_ISDIGIT(*s))
3865 {
3866 maxwid = (int)getdigits(&s);
3867 if (maxwid <= 0) /* overflow */
3868 maxwid = 50;
3869 }
3870 }
3871 minwid = (minwid > 50 ? 50 : minwid) * l;
3872 if (*s == '(')
3873 {
3874 groupitem[groupdepth++] = curitem;
3875 item[curitem].type = Group;
3876 item[curitem].start = p;
3877 item[curitem].minwid = minwid;
3878 item[curitem].maxwid = maxwid;
3879 s++;
3880 curitem++;
3881 continue;
3882 }
3883 if (vim_strchr(STL_ALL, *s) == NULL)
3884 {
3885 s++;
3886 continue;
3887 }
3888 opt = *s++;
3889
3890 /* OK - now for the real work */
3891 base = 'D';
3892 itemisflag = FALSE;
3893 fillable = TRUE;
3894 num = -1;
3895 str = NULL;
3896 switch (opt)
3897 {
3898 case STL_FILEPATH:
3899 case STL_FULLPATH:
3900 case STL_FILENAME:
3901 fillable = FALSE; /* don't change ' ' to fillchar */
3902 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003903 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 else
3905 {
3906 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003907 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3909 }
3910 trans_characters(NameBuff, MAXPATHL);
3911 if (opt != STL_FILENAME)
3912 str = NameBuff;
3913 else
3914 str = gettail(NameBuff);
3915 break;
3916
3917 case STL_VIM_EXPR: /* '{' */
3918 itemisflag = TRUE;
3919 t = p;
3920 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3921 *p++ = *s++;
3922 if (*s != '}') /* missing '}' or out of space */
3923 break;
3924 s++;
3925 *p = 0;
3926 p = t;
3927
3928#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003929 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3931
3932 o_curbuf = curbuf;
3933 o_curwin = curwin;
3934 curwin = wp;
3935 curbuf = wp->w_buffer;
3936
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003937 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939 curwin = o_curwin;
3940 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003941 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
3943 if (str != NULL && *str != 0)
3944 {
3945 if (*skipdigits(str) == NUL)
3946 {
3947 num = atoi((char *)str);
3948 vim_free(str);
3949 str = NULL;
3950 itemisflag = FALSE;
3951 }
3952 }
3953#endif
3954 break;
3955
3956 case STL_LINE:
3957 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3958 ? 0L : (long)(wp->w_cursor.lnum);
3959 break;
3960
3961 case STL_NUMLINES:
3962 num = wp->w_buffer->b_ml.ml_line_count;
3963 break;
3964
3965 case STL_COLUMN:
3966 num = !(State & INSERT) && empty_line
3967 ? 0 : (int)wp->w_cursor.col + 1;
3968 break;
3969
3970 case STL_VIRTCOL:
3971 case STL_VIRTCOL_ALT:
3972 /* In list mode virtcol needs to be recomputed */
3973 virtcol = wp->w_virtcol;
3974 if (wp->w_p_list && lcs_tab1 == NUL)
3975 {
3976 wp->w_p_list = FALSE;
3977 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3978 wp->w_p_list = TRUE;
3979 }
3980 ++virtcol;
3981 /* Don't display %V if it's the same as %c. */
3982 if (opt == STL_VIRTCOL_ALT
3983 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3984 ? 0 : (int)wp->w_cursor.col + 1)))
3985 break;
3986 num = (long)virtcol;
3987 break;
3988
3989 case STL_PERCENTAGE:
3990 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3991 (long)wp->w_buffer->b_ml.ml_line_count);
3992 break;
3993
3994 case STL_ALTPERCENT:
3995 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003996 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 break;
3998
3999 case STL_ARGLISTSTAT:
4000 fillable = FALSE;
4001 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004002 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 str = tmp;
4004 break;
4005
4006 case STL_KEYMAP:
4007 fillable = FALSE;
4008 if (get_keymap_str(wp, tmp, TMPLEN))
4009 str = tmp;
4010 break;
4011 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004012#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004013 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#else
4015 num = 0;
4016#endif
4017 break;
4018
4019 case STL_BUFNO:
4020 num = wp->w_buffer->b_fnum;
4021 break;
4022
4023 case STL_OFFSET_X:
4024 base = 'X';
4025 case STL_OFFSET:
4026#ifdef FEAT_BYTEOFF
4027 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4028 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4029 0L : l + 1 + (!(State & INSERT) && empty_line ?
4030 0 : (int)wp->w_cursor.col);
4031#endif
4032 break;
4033
4034 case STL_BYTEVAL_X:
4035 base = 'X';
4036 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004037 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (num == NL)
4039 num = 0;
4040 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4041 num = NL;
4042 break;
4043
4044 case STL_ROFLAG:
4045 case STL_ROFLAG_ALT:
4046 itemisflag = TRUE;
4047 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004048 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 break;
4050
4051 case STL_HELPFLAG:
4052 case STL_HELPFLAG_ALT:
4053 itemisflag = TRUE;
4054 if (wp->w_buffer->b_help)
4055 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004056 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 break;
4058
4059#ifdef FEAT_AUTOCMD
4060 case STL_FILETYPE:
4061 if (*wp->w_buffer->b_p_ft != NUL
4062 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4063 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004064 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4065 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 str = tmp;
4067 }
4068 break;
4069
4070 case STL_FILETYPE_ALT:
4071 itemisflag = TRUE;
4072 if (*wp->w_buffer->b_p_ft != NUL
4073 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4074 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004075 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4076 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 for (t = tmp; *t != 0; t++)
4078 *t = TOUPPER_LOC(*t);
4079 str = tmp;
4080 }
4081 break;
4082#endif
4083
4084#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4085 case STL_PREVIEWFLAG:
4086 case STL_PREVIEWFLAG_ALT:
4087 itemisflag = TRUE;
4088 if (wp->w_p_pvw)
4089 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4090 : _("[Preview]"));
4091 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004092
4093 case STL_QUICKFIX:
4094 if (bt_quickfix(wp->w_buffer))
4095 str = (char_u *)(wp->w_llist_ref
4096 ? _(msg_loclist)
4097 : _(msg_qflist));
4098 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099#endif
4100
4101 case STL_MODIFIED:
4102 case STL_MODIFIED_ALT:
4103 itemisflag = TRUE;
4104 switch ((opt == STL_MODIFIED_ALT)
4105 + bufIsChanged(wp->w_buffer) * 2
4106 + (!wp->w_buffer->b_p_ma) * 4)
4107 {
4108 case 2: str = (char_u *)"[+]"; break;
4109 case 3: str = (char_u *)",+"; break;
4110 case 4: str = (char_u *)"[-]"; break;
4111 case 5: str = (char_u *)",-"; break;
4112 case 6: str = (char_u *)"[+-]"; break;
4113 case 7: str = (char_u *)",+-"; break;
4114 }
4115 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004116
4117 case STL_HIGHLIGHT:
4118 t = s;
4119 while (*s != '#' && *s != NUL)
4120 ++s;
4121 if (*s == '#')
4122 {
4123 item[curitem].type = Highlight;
4124 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004125 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004126 curitem++;
4127 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004128 if (*s != NUL)
4129 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004130 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132
4133 item[curitem].start = p;
4134 item[curitem].type = Normal;
4135 if (str != NULL && *str)
4136 {
4137 t = str;
4138 if (itemisflag)
4139 {
4140 if ((t[0] && t[1])
4141 && ((!prevchar_isitem && *t == ',')
4142 || (prevchar_isflag && *t == ' ')))
4143 t++;
4144 prevchar_isflag = TRUE;
4145 }
4146 l = vim_strsize(t);
4147 if (l > 0)
4148 prevchar_isitem = TRUE;
4149 if (l > maxwid)
4150 {
4151 while (l >= maxwid)
4152#ifdef FEAT_MBYTE
4153 if (has_mbyte)
4154 {
4155 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004156 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
4158 else
4159#endif
4160 l -= byte2cells(*t++);
4161 if (p + 1 >= out + outlen)
4162 break;
4163 *p++ = '<';
4164 }
4165 if (minwid > 0)
4166 {
4167 for (; l < minwid && p + 1 < out + outlen; l++)
4168 {
4169 /* Don't put a "-" in front of a digit. */
4170 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4171 *p++ = ' ';
4172 else
4173 *p++ = fillchar;
4174 }
4175 minwid = 0;
4176 }
4177 else
4178 minwid *= -1;
4179 while (*t && p + 1 < out + outlen)
4180 {
4181 *p++ = *t++;
4182 /* Change a space by fillchar, unless fillchar is '-' and a
4183 * digit follows. */
4184 if (fillable && p[-1] == ' '
4185 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4186 p[-1] = fillchar;
4187 }
4188 for (; l < minwid && p + 1 < out + outlen; l++)
4189 *p++ = fillchar;
4190 }
4191 else if (num >= 0)
4192 {
4193 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4194 char_u nstr[20];
4195
4196 if (p + 20 >= out + outlen)
4197 break; /* not sufficient space */
4198 prevchar_isitem = TRUE;
4199 t = nstr;
4200 if (opt == STL_VIRTCOL_ALT)
4201 {
4202 *t++ = '-';
4203 minwid--;
4204 }
4205 *t++ = '%';
4206 if (zeropad)
4207 *t++ = '0';
4208 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004209 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 *t = 0;
4211
4212 for (n = num, l = 1; n >= nbase; n /= nbase)
4213 l++;
4214 if (opt == STL_VIRTCOL_ALT)
4215 l++;
4216 if (l > maxwid)
4217 {
4218 l += 2;
4219 n = l - maxwid;
4220 while (l-- > maxwid)
4221 num /= nbase;
4222 *t++ = '>';
4223 *t++ = '%';
4224 *t = t[-3];
4225 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004226 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4227 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004230 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4231 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 p += STRLEN(p);
4233 }
4234 else
4235 item[curitem].type = Empty;
4236
4237 if (opt == STL_VIM_EXPR)
4238 vim_free(str);
4239
4240 if (num >= 0 || (!itemisflag && str && *str))
4241 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4242 curitem++;
4243 }
4244 *p = NUL;
4245 itemcnt = curitem;
4246
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004247#ifdef FEAT_EVAL
4248 if (usefmt != fmt)
4249 vim_free(usefmt);
4250#endif
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 width = vim_strsize(out);
4253 if (maxwidth > 0 && width > maxwidth)
4254 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004255 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 l = 0;
4257 if (itemcnt == 0)
4258 s = out;
4259 else
4260 {
4261 for ( ; l < itemcnt; l++)
4262 if (item[l].type == Trunc)
4263 {
4264 /* Truncate at %< item. */
4265 s = item[l].start;
4266 break;
4267 }
4268 if (l == itemcnt)
4269 {
4270 /* No %< item, truncate first item. */
4271 s = item[0].start;
4272 l = 0;
4273 }
4274 }
4275
4276 if (width - vim_strsize(s) >= maxwidth)
4277 {
4278 /* Truncation mark is beyond max length */
4279#ifdef FEAT_MBYTE
4280 if (has_mbyte)
4281 {
4282 s = out;
4283 width = 0;
4284 for (;;)
4285 {
4286 width += ptr2cells(s);
4287 if (width >= maxwidth)
4288 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004289 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 /* Fill up for half a double-wide character. */
4292 while (++width < maxwidth)
4293 *s++ = fillchar;
4294 }
4295 else
4296#endif
4297 s = out + maxwidth - 1;
4298 for (l = 0; l < itemcnt; l++)
4299 if (item[l].start > s)
4300 break;
4301 itemcnt = l;
4302 *s++ = '>';
4303 *s = 0;
4304 }
4305 else
4306 {
4307#ifdef FEAT_MBYTE
4308 if (has_mbyte)
4309 {
4310 n = 0;
4311 while (width >= maxwidth)
4312 {
4313 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004314 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316 }
4317 else
4318#endif
4319 n = width - maxwidth + 1;
4320 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004321 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 *s = '<';
4323
4324 /* Fill up for half a double-wide character. */
4325 while (++width < maxwidth)
4326 {
4327 s = s + STRLEN(s);
4328 *s++ = fillchar;
4329 *s = NUL;
4330 }
4331
4332 --n; /* count the '<' */
4333 for (; l < itemcnt; l++)
4334 {
4335 if (item[l].start - n >= s)
4336 item[l].start -= n;
4337 else
4338 item[l].start = s;
4339 }
4340 }
4341 width = maxwidth;
4342 }
4343 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4344 {
4345 /* Apply STL_MIDDLE if any */
4346 for (l = 0; l < itemcnt; l++)
4347 if (item[l].type == Middle)
4348 break;
4349 if (l < itemcnt)
4350 {
4351 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004352 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 for (s = item[l].start; s < p; s++)
4354 *s = fillchar;
4355 for (l++; l < itemcnt; l++)
4356 item[l].start += maxwidth - width;
4357 width = maxwidth;
4358 }
4359 }
4360
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004361 /* Store the info about highlighting. */
4362 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004364 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 for (l = 0; l < itemcnt; l++)
4366 {
4367 if (item[l].type == Highlight)
4368 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004369 sp->start = item[l].start;
4370 sp->userhl = item[l].minwid;
4371 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004374 sp->start = NULL;
4375 sp->userhl = 0;
4376 }
4377
4378 /* Store the info about tab pages labels. */
4379 if (tabtab != NULL)
4380 {
4381 sp = tabtab;
4382 for (l = 0; l < itemcnt; l++)
4383 {
4384 if (item[l].type == TabPage)
4385 {
4386 sp->start = item[l].start;
4387 sp->userhl = item[l].minwid;
4388 sp++;
4389 }
4390 }
4391 sp->start = NULL;
4392 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
4394
4395 return width;
4396}
4397#endif /* FEAT_STL_OPT */
4398
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004399#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4400 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004402 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4403 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 */
4405 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004406get_rel_pos(
4407 win_T *wp,
4408 char_u *buf,
4409 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 long above; /* number of lines above window */
4412 long below; /* number of lines below window */
4413
Bram Moolenaar0027c212015-01-07 13:31:52 +01004414 if (buflen < 3) /* need at least 3 chars for writing */
4415 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 above = wp->w_topline - 1;
4417#ifdef FEAT_DIFF
4418 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004419 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4420 above = 0; /* All buffer lines are displayed and there is an
4421 * indication of filler lines, that can be considered
4422 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423#endif
4424 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4425 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004426 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4427 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004429 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004431 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 ? (int)(above / ((above + below) / 100L))
4433 : (int)(above * 100L / (above + below)));
4434}
4435#endif
4436
4437/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004438 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 * Return TRUE if it was appended.
4440 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004441 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004442append_arg_number(
4443 win_T *wp,
4444 char_u *buf,
4445 int buflen,
4446 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447{
4448 char_u *p;
4449
4450 if (ARGCOUNT <= 1) /* nothing to do */
4451 return FALSE;
4452
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004453 p = buf + STRLEN(buf); /* go to the end of the buffer */
4454 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 return FALSE;
4456 *p++ = ' ';
4457 *p++ = '(';
4458 if (add_file)
4459 {
4460 STRCPY(p, "file ");
4461 p += 5;
4462 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004463 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4464 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4466 return TRUE;
4467}
4468
4469/*
4470 * If fname is not a full path, make it a full path.
4471 * Returns pointer to allocated memory (NULL for failure).
4472 */
4473 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004474fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475{
4476 /*
4477 * Force expanding the path always for Unix, because symbolic links may
4478 * mess up the full path name, even though it starts with a '/'.
4479 * Also expand when there is ".." in the file name, try to remove it,
4480 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004481 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 * For MS-Windows also expand names like "longna~1" to "longname".
4483 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004484#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 return FullName_save(fname, TRUE);
4486#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004487 if (!vim_isAbsName(fname)
4488 || strstr((char *)fname, "..") != NULL
4489 || strstr((char *)fname, "//") != NULL
4490# ifdef BACKSLASH_IN_FILENAME
4491 || strstr((char *)fname, "\\\\") != NULL
4492# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004493# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 )
4497 return FullName_save(fname, FALSE);
4498
4499 fname = vim_strsave(fname);
4500
Bram Moolenaar9b942202007-10-03 12:31:33 +00004501# ifdef USE_FNAME_CASE
4502# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
4506 if (fname != NULL)
4507 fname_case(fname, 0); /* set correct case for file name */
4508 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511 return fname;
4512#endif
4513}
4514
4515/*
4516 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4517 * "ffname" becomes a pointer to allocated memory (or NULL).
4518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004520fname_expand(
4521 buf_T *buf UNUSED,
4522 char_u **ffname,
4523 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525 if (*ffname == NULL) /* if no file name given, nothing to do */
4526 return;
4527 if (*sfname == NULL) /* if no short file name given, use ffname */
4528 *sfname = *ffname;
4529 *ffname = fix_fname(*ffname); /* expand to full path */
4530
4531#ifdef FEAT_SHORTCUT
4532 if (!buf->b_p_bin)
4533 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004534 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 /* If the file name is a shortcut file, use the file it links to. */
4537 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004538 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
4540 vim_free(*ffname);
4541 *ffname = rfname;
4542 *sfname = rfname;
4543 }
4544 }
4545#endif
4546}
4547
4548/*
4549 * Get the file name for an argument list entry.
4550 */
4551 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004552alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553{
4554 buf_T *bp;
4555
4556 /* Use the name from the associated buffer if it exists. */
4557 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004558 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 return aep->ae_fname;
4560 return bp->b_fname;
4561}
4562
4563#if defined(FEAT_WINDOWS) || defined(PROTO)
4564/*
4565 * do_arg_all(): Open up to 'count' windows, one for each argument.
4566 */
4567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004568do_arg_all(
4569 int count,
4570 int forceit, /* hide buffers in current windows */
4571 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572{
4573 int i;
4574 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004575 char_u *opened; /* Array of weight for which args are open:
4576 * 0: not opened
4577 * 1: opened in other tab
4578 * 2: opened in curtab
4579 * 3: opened in curtab and curwin
4580 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004581 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 int use_firstwin = FALSE; /* use first window for arglist */
4583 int split_ret = OK;
4584 int p_ea_save;
4585 alist_T *alist; /* argument list to be used */
4586 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004587 tabpage_T *tpnext;
4588 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004589 win_T *old_curwin, *last_curwin;
4590 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004591 win_T *new_curwin = NULL;
4592 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 if (ARGCOUNT <= 0)
4595 {
4596 /* Don't give an error message. We don't want it when the ":all"
4597 * command is in the .vimrc. */
4598 return;
4599 }
4600 setpcmark();
4601
4602 opened_len = ARGCOUNT;
4603 opened = alloc_clear((unsigned)opened_len);
4604 if (opened == NULL)
4605 return;
4606
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004607 /* Autocommands may do anything to the argument list. Make sure it's not
4608 * freed while we are working here by "locking" it. We still have to
4609 * watch out for its size to be changed. */
4610 alist = curwin->w_alist;
4611 ++alist->al_refcount;
4612
4613 old_curwin = curwin;
4614 old_curtab = curtab;
4615
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004616# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
4620 /*
4621 * Try closing all windows that are not in the argument list.
4622 * Also close windows that are not full width;
4623 * When 'hidden' or "forceit" set the buffer becomes hidden.
4624 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004625 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004627 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004628 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004629 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004631 tpnext = curtab->tp_next;
4632 for (wp = firstwin; wp != NULL; wp = wpnext)
4633 {
4634 wpnext = wp->w_next;
4635 buf = wp->w_buffer;
4636 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004637 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004638 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004639 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004642 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004643 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004645 if (i < alist->al_ga.ga_len
4646 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4647 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4648 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004650 int weight = 1;
4651
4652 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004653 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004654 ++weight;
4655 if (old_curwin == wp)
4656 ++weight;
4657 }
4658
4659 if (weight > (int)opened[i])
4660 {
4661 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004662 if (i == 0)
4663 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004664 if (new_curwin != NULL)
4665 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004666 new_curwin = wp;
4667 new_curtab = curtab;
4668 }
4669 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004670 else if (keep_tabs)
4671 i = opened_len;
4672
4673 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004674 {
4675 /* Use the current argument list for all windows
4676 * containing a file from it. */
4677 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004678 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004679 ++wp->w_alist->al_refcount;
4680 }
4681 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
4684 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004685 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004687 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004689 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004690 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004692 /* If the buffer was changed, and we would like to hide it,
4693 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004694 if (!P_HID(buf) && buf->b_nwindows <= 1
4695 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004697 (void)autowrite(buf, FALSE);
4698#ifdef FEAT_AUTOCMD
4699 /* check if autocommands removed the window */
4700 if (!win_valid(wp) || !buf_valid(buf))
4701 {
4702 wpnext = firstwin; /* start all over... */
4703 continue;
4704 }
4705#endif
4706 }
4707#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004708 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004709 if (firstwin == lastwin
4710 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004711#endif
4712 use_firstwin = TRUE;
4713#ifdef FEAT_WINDOWS
4714 else
4715 {
4716 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4717# ifdef FEAT_AUTOCMD
4718 /* check if autocommands removed the next window */
4719 if (!win_valid(wpnext))
4720 wpnext = firstwin; /* start all over... */
4721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
4723#endif
4724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
4726 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004727
4728 /* Without the ":tab" modifier only do the current tab page. */
4729 if (had_tab == 0 || tpnext == NULL)
4730 break;
4731
4732# ifdef FEAT_AUTOCMD
4733 /* check if autocommands removed the next tab page */
4734 if (!valid_tabpage(tpnext))
4735 tpnext = first_tabpage; /* start all over...*/
4736# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004737 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 }
4739
4740 /*
4741 * Open a window for files in the argument list that don't have one.
4742 * ARGCOUNT may change while doing this, because of autocommands.
4743 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004744 if (count > opened_len || count <= 0)
4745 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
4747#ifdef FEAT_AUTOCMD
4748 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4749 ++autocmd_no_enter;
4750 ++autocmd_no_leave;
4751#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004752 last_curwin = curwin;
4753 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004755#ifdef FEAT_WINDOWS
4756 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4757 * leaving an empty tab page when executed locally. */
4758 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4759 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4760 use_firstwin = TRUE;
4761#endif
4762
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004763 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
4765 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4766 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004767 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
4769 /* Move the already present window to below the current window */
4770 if (curwin->w_arg_idx != i)
4771 {
4772 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4773 {
4774 if (wpnext->w_arg_idx == i)
4775 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004776 if (keep_tabs)
4777 {
4778 new_curwin = wpnext;
4779 new_curtab = curtab;
4780 }
4781 else
4782 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 break;
4784 }
4785 }
4786 }
4787 }
4788 else if (split_ret == OK)
4789 {
4790 if (!use_firstwin) /* split current window */
4791 {
4792 p_ea_save = p_ea;
4793 p_ea = TRUE; /* use space from all windows */
4794 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4795 p_ea = p_ea_save;
4796 if (split_ret == FAIL)
4797 continue;
4798 }
4799#ifdef FEAT_AUTOCMD
4800 else /* first window: do autocmd for leaving this buffer */
4801 --autocmd_no_leave;
4802#endif
4803
4804 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004805 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 */
4807 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004808 if (i == 0)
4809 {
4810 new_curwin = curwin;
4811 new_curtab = curtab;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4814 ECMD_ONE,
4815 ((P_HID(curwin->w_buffer)
4816 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004817 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818#ifdef FEAT_AUTOCMD
4819 if (use_firstwin)
4820 ++autocmd_no_leave;
4821#endif
4822 use_firstwin = FALSE;
4823 }
4824 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004825
4826 /* When ":tab" was used open a new tab for a new window repeatedly. */
4827 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4828 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830
4831 /* Remove the "lock" on the argument list. */
4832 alist_unlink(alist);
4833
4834#ifdef FEAT_AUTOCMD
4835 --autocmd_no_enter;
4836#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004837 /* restore last referenced tabpage's curwin */
4838 if (last_curtab != new_curtab)
4839 {
4840 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004841 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004842 if (win_valid(last_curwin))
4843 win_enter(last_curwin, FALSE);
4844 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004845 /* to window with first arg */
4846 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004847 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004848 if (win_valid(new_curwin))
4849 win_enter(new_curwin, FALSE);
4850
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851#ifdef FEAT_AUTOCMD
4852 --autocmd_no_leave;
4853#endif
4854 vim_free(opened);
4855}
4856
4857# if defined(FEAT_LISTCMDS) || defined(PROTO)
4858/*
4859 * Open a window for a number of buffers.
4860 */
4861 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004862ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864 buf_T *buf;
4865 win_T *wp, *wpnext;
4866 int split_ret = OK;
4867 int p_ea_save;
4868 int open_wins = 0;
4869 int r;
4870 int count; /* Maximum number of windows to open. */
4871 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004872#ifdef FEAT_WINDOWS
4873 int had_tab = cmdmod.tab;
4874 tabpage_T *tpnext;
4875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 if (eap->addr_count == 0) /* make as many windows as possible */
4878 count = 9999;
4879 else
4880 count = eap->line2; /* make as many windows as specified */
4881 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4882 all = FALSE;
4883 else
4884 all = TRUE;
4885
4886 setpcmark();
4887
4888#ifdef FEAT_GUI
4889 need_mouse_correct = TRUE;
4890#endif
4891
4892 /*
4893 * Close superfluous windows (two windows for the same buffer).
4894 * Also close windows that are not full-width.
4895 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004896#ifdef FEAT_WINDOWS
4897 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004898 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004899 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004902 tpnext = curtab->tp_next;
4903 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004905 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004906 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004907#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004908 || ((cmdmod.split & WSP_VERT)
4909 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004910 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004911 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004912 || (had_tab > 0 && wp != firstwin)
4913#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004914 ) && firstwin != lastwin
4915#ifdef FEAT_AUTOCMD
4916 && !(wp->w_closing || wp->w_buffer->b_closing)
4917#endif
4918 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004919 {
4920 win_close(wp, FALSE);
4921#ifdef FEAT_AUTOCMD
4922 wpnext = firstwin; /* just in case an autocommand does
4923 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004924 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004925 open_wins = 0;
4926#endif
4927 }
4928 else
4929 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004931
4932#ifdef FEAT_WINDOWS
4933 /* Without the ":tab" modifier only do the current tab page. */
4934 if (had_tab == 0 || tpnext == NULL)
4935 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004936 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 /*
4941 * Go through the buffer list. When a buffer doesn't have a window yet,
4942 * open one. Otherwise move the window to the right position.
4943 * Watch out for autocommands that delete buffers or windows!
4944 */
4945#ifdef FEAT_AUTOCMD
4946 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4947 ++autocmd_no_enter;
4948#endif
4949 win_enter(lastwin, FALSE);
4950#ifdef FEAT_AUTOCMD
4951 ++autocmd_no_leave;
4952#endif
4953 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4954 {
4955 /* Check if this buffer needs a window */
4956 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4957 continue;
4958
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004959#ifdef FEAT_WINDOWS
4960 if (had_tab != 0)
4961 {
4962 /* With the ":tab" modifier don't move the window. */
4963 if (buf->b_nwindows > 0)
4964 wp = lastwin; /* buffer has a window, skip it */
4965 else
4966 wp = NULL;
4967 }
4968 else
4969#endif
4970 {
4971 /* Check if this buffer already has a window */
4972 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4973 if (wp->w_buffer == buf)
4974 break;
4975 /* If the buffer already has a window, move it */
4976 if (wp != NULL)
4977 win_move_after(wp, curwin);
4978 }
4979
4980 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 /* Split the window and put the buffer in it */
4983 p_ea_save = p_ea;
4984 p_ea = TRUE; /* use space from all windows */
4985 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4986 ++open_wins;
4987 p_ea = p_ea_save;
4988 if (split_ret == FAIL)
4989 continue;
4990
4991 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004992#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 swap_exists_action = SEA_DIALOG;
4994#endif
4995 set_curbuf(buf, DOBUF_GOTO);
4996#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004999#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 break;
5003 }
5004#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005005#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 if (swap_exists_action == SEA_QUIT)
5007 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005008# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5009 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005010
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005011 /* Reset the error/interrupt/exception state here so that
5012 * aborting() returns FALSE when closing a window. */
5013 enter_cleanup(&cs);
5014# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005015
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005016 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 win_close(curwin, TRUE);
5018 --open_wins;
5019 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005020 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005021
5022# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5023 /* Restore the error/interrupt/exception state if not
5024 * discarded by a new aborting error, interrupt, or uncaught
5025 * exception. */
5026 leave_cleanup(&cs);
5027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029 else
5030 handle_swap_exists(NULL);
5031#endif
5032 }
5033
5034 ui_breakcheck();
5035 if (got_int)
5036 {
5037 (void)vgetc(); /* only break the file loading, not the rest */
5038 break;
5039 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005040#ifdef FEAT_EVAL
5041 /* Autocommands deleted the buffer or aborted script processing!!! */
5042 if (aborting())
5043 break;
5044#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005045#ifdef FEAT_WINDOWS
5046 /* When ":tab" was used open a new tab for a new window repeatedly. */
5047 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5048 cmdmod.tab = 9999;
5049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051#ifdef FEAT_AUTOCMD
5052 --autocmd_no_enter;
5053#endif
5054 win_enter(firstwin, FALSE); /* back to first window */
5055#ifdef FEAT_AUTOCMD
5056 --autocmd_no_leave;
5057#endif
5058
5059 /*
5060 * Close superfluous windows.
5061 */
5062 for (wp = lastwin; open_wins > count; )
5063 {
5064 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5065 || autowrite(wp->w_buffer, FALSE) == OK);
5066#ifdef FEAT_AUTOCMD
5067 if (!win_valid(wp))
5068 {
5069 /* BufWrite Autocommands made the window invalid, start over */
5070 wp = lastwin;
5071 }
5072 else
5073#endif
5074 if (r)
5075 {
5076 win_close(wp, !P_HID(wp->w_buffer));
5077 --open_wins;
5078 wp = lastwin;
5079 }
5080 else
5081 {
5082 wp = wp->w_prev;
5083 if (wp == NULL)
5084 break;
5085 }
5086 }
5087}
5088# endif /* FEAT_LISTCMDS */
5089
5090#endif /* FEAT_WINDOWS */
5091
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005092static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094/*
5095 * do_modelines() - process mode lines for the current file
5096 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005097 * "flags" can be:
5098 * OPT_WINONLY only set options local to window
5099 * OPT_NOWIN don't set options local to window
5100 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 * Returns immediately if the "ml" option isn't set.
5102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005106 linenr_T lnum;
5107 int nmlines;
5108 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
5110 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5111 return;
5112
5113 /* Disallow recursive entry here. Can happen when executing a modeline
5114 * triggers an autocommand, which reloads modelines with a ":do". */
5115 if (entered)
5116 return;
5117
5118 ++entered;
5119 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5120 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005121 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 nmlines = 0;
5123
5124 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5125 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005126 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 nmlines = 0;
5128 --entered;
5129}
5130
5131#include "version.h" /* for version number */
5132
5133/*
5134 * chk_modeline() - check a single line for a mode string
5135 * Return FAIL if an error encountered.
5136 */
5137 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005138chk_modeline(
5139 linenr_T lnum,
5140 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 char_u *s;
5143 char_u *e;
5144 char_u *linecopy; /* local copy of any modeline found */
5145 int prev;
5146 int vers;
5147 int end;
5148 int retval = OK;
5149 char_u *save_sourcing_name;
5150 linenr_T save_sourcing_lnum;
5151#ifdef FEAT_EVAL
5152 scid_T save_SID;
5153#endif
5154
5155 prev = -1;
5156 for (s = ml_get(lnum); *s != NUL; ++s)
5157 {
5158 if (prev == -1 || vim_isspace(prev))
5159 {
5160 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5161 || STRNCMP(s, "vi:", (size_t)3) == 0)
5162 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005163 /* Accept both "vim" and "Vim". */
5164 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5167 e = s + 4;
5168 else
5169 e = s + 3;
5170 vers = getdigits(&e);
5171 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005172 && (s[0] != 'V'
5173 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 && (s[3] == ':'
5175 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5176 || (VIM_VERSION_100 < vers && s[3] == '<')
5177 || (VIM_VERSION_100 > vers && s[3] == '>')
5178 || (VIM_VERSION_100 == vers && s[3] == '=')))
5179 break;
5180 }
5181 }
5182 prev = *s;
5183 }
5184
5185 if (*s)
5186 {
5187 do /* skip over "ex:", "vi:" or "vim:" */
5188 ++s;
5189 while (s[-1] != ':');
5190
5191 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5192 if (linecopy == NULL)
5193 return FAIL;
5194
5195 save_sourcing_lnum = sourcing_lnum;
5196 save_sourcing_name = sourcing_name;
5197 sourcing_lnum = lnum; /* prepare for emsg() */
5198 sourcing_name = (char_u *)"modelines";
5199
5200 end = FALSE;
5201 while (end == FALSE)
5202 {
5203 s = skipwhite(s);
5204 if (*s == NUL)
5205 break;
5206
5207 /*
5208 * Find end of set command: ':' or end of line.
5209 * Skip over "\:", replacing it with ":".
5210 */
5211 for (e = s; *e != ':' && *e != NUL; ++e)
5212 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005213 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (*e == NUL)
5215 end = TRUE;
5216
5217 /*
5218 * If there is a "set" command, require a terminating ':' and
5219 * ignore the stuff after the ':'.
5220 * "vi:set opt opt opt: foo" -- foo not interpreted
5221 * "vi:opt opt opt: foo" -- foo interpreted
5222 * Accept "se" for compatibility with Elvis.
5223 */
5224 if (STRNCMP(s, "set ", (size_t)4) == 0
5225 || STRNCMP(s, "se ", (size_t)3) == 0)
5226 {
5227 if (*e != ':') /* no terminating ':'? */
5228 break;
5229 end = TRUE;
5230 s = vim_strchr(s, ' ') + 1;
5231 }
5232 *e = NUL; /* truncate the set command */
5233
5234 if (*s != NUL) /* skip over an empty "::" */
5235 {
5236#ifdef FEAT_EVAL
5237 save_SID = current_SID;
5238 current_SID = SID_MODELINE;
5239#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005240 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#ifdef FEAT_EVAL
5242 current_SID = save_SID;
5243#endif
5244 if (retval == FAIL) /* stop if error found */
5245 break;
5246 }
5247 s = e + 1; /* advance to next part */
5248 }
5249
5250 sourcing_lnum = save_sourcing_lnum;
5251 sourcing_name = save_sourcing_name;
5252
5253 vim_free(linecopy);
5254 }
5255 return retval;
5256}
5257
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005258#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005260read_viminfo_bufferlist(
5261 vir_T *virp,
5262 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 char_u *tab;
5265 linenr_T lnum;
5266 colnr_T col;
5267 buf_T *buf;
5268 char_u *sfname;
5269 char_u *xline;
5270
5271 /* Handle long line and escaped characters. */
5272 xline = viminfo_readstring(virp, 1, FALSE);
5273
5274 /* don't read in if there are files on the command-line or if writing: */
5275 if (xline != NULL && !writing && ARGCOUNT == 0
5276 && find_viminfo_parameter('%') != NULL)
5277 {
5278 /* Format is: <fname> Tab <lnum> Tab <col>.
5279 * Watch out for a Tab in the file name, work from the end. */
5280 lnum = 0;
5281 col = 0;
5282 tab = vim_strrchr(xline, '\t');
5283 if (tab != NULL)
5284 {
5285 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005286 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 tab = vim_strrchr(xline, '\t');
5288 if (tab != NULL)
5289 {
5290 *tab++ = '\0';
5291 lnum = atol((char *)tab);
5292 }
5293 }
5294
5295 /* Expand "~/" in the file name at "line + 1" to a full path.
5296 * Then try shortening it by comparing with the current directory */
5297 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005298 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
5300 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5301 if (buf != NULL) /* just in case... */
5302 {
5303 buf->b_last_cursor.lnum = lnum;
5304 buf->b_last_cursor.col = col;
5305 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5306 }
5307 }
5308 vim_free(xline);
5309
5310 return viminfo_readline(virp);
5311}
5312
5313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005314write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
5316 buf_T *buf;
5317#ifdef FEAT_WINDOWS
5318 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005319 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#endif
5321 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005322 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
5324 if (find_viminfo_parameter('%') == NULL)
5325 return;
5326
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005327 /* Without a number -1 is returned: do all buffers. */
5328 max_buffers = get_viminfo_parameter('%');
5329
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005331#define LINE_BUF_LEN (MAXPATHL + 40)
5332 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (line == NULL)
5334 return;
5335
5336#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005337 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 set_last_cursor(win);
5339#else
5340 set_last_cursor(curwin);
5341#endif
5342
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005343 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5345 {
5346 if (buf->b_fname == NULL
5347 || !buf->b_p_bl
5348#ifdef FEAT_QUICKFIX
5349 || bt_quickfix(buf)
5350#endif
5351 || removable(buf->b_ffname))
5352 continue;
5353
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005354 if (max_buffers-- == 0)
5355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 putc('%', fp);
5357 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005358 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 (long)buf->b_last_cursor.lnum,
5360 buf->b_last_cursor.col);
5361 viminfo_writestring(fp, line);
5362 }
5363 vim_free(line);
5364}
5365#endif
5366
5367
5368/*
5369 * Return special buffer name.
5370 * Returns NULL when the buffer has a normal file name.
5371 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005372 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005373buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
5375#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5376 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005377 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005378 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005379 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005380
5381 /*
5382 * For location list window, w_llist_ref points to the location list.
5383 * For quickfix window, w_llist_ref is NULL.
5384 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005385 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005386 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005387 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005388 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#endif
5391#ifdef FEAT_QUICKFIX
5392 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5393 * contains the name as specified by the user */
5394 if (bt_nofile(buf))
5395 {
5396 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005397 return buf->b_sfname;
5398 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400#endif
5401 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005402 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 return NULL;
5404}
5405
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005406#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5407 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5408 || defined(PROTO)
5409/*
5410 * Find a window for buffer "buf".
5411 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5412 * If not found FAIL is returned.
5413 */
5414 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005415find_win_for_buf(
5416 buf_T *buf,
5417 win_T **wp,
5418 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005419{
5420 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5421 if ((*wp)->w_buffer == buf)
5422 goto win_found;
5423 return FAIL;
5424win_found:
5425 return OK;
5426}
5427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
5429#if defined(FEAT_SIGNS) || defined(PROTO)
5430/*
5431 * Insert the sign into the signlist.
5432 */
5433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005434insert_sign(
5435 buf_T *buf, /* buffer to store sign in */
5436 signlist_T *prev, /* previous sign entry */
5437 signlist_T *next, /* next sign entry */
5438 int id, /* sign ID */
5439 linenr_T lnum, /* line number which gets the mark */
5440 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 signlist_T *newsign;
5443
5444 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5445 if (newsign != NULL)
5446 {
5447 newsign->id = id;
5448 newsign->lnum = lnum;
5449 newsign->typenr = typenr;
5450 newsign->next = next;
5451#ifdef FEAT_NETBEANS_INTG
5452 newsign->prev = prev;
5453 if (next != NULL)
5454 next->prev = newsign;
5455#endif
5456
5457 if (prev == NULL)
5458 {
5459 /* When adding first sign need to redraw the windows to create the
5460 * column for signs. */
5461 if (buf->b_signlist == NULL)
5462 {
5463 redraw_buf_later(buf, NOT_VALID);
5464 changed_cline_bef_curs();
5465 }
5466
5467 /* first sign in signlist */
5468 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005469#ifdef FEAT_NETBEANS_INTG
5470 if (netbeans_active())
5471 buf->b_has_sign_column = TRUE;
5472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474 else
5475 prev->next = newsign;
5476 }
5477}
5478
5479/*
5480 * Add the sign into the signlist. Find the right spot to do it though.
5481 */
5482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005483buf_addsign(
5484 buf_T *buf, /* buffer to store sign in */
5485 int id, /* sign ID */
5486 linenr_T lnum, /* line number which gets the mark */
5487 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 signlist_T *sign; /* a sign in the signlist */
5490 signlist_T *prev; /* the previous sign */
5491
5492 prev = NULL;
5493 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5494 {
5495 if (lnum == sign->lnum && id == sign->id)
5496 {
5497 sign->typenr = typenr;
5498 return;
5499 }
5500 else if (
5501#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5502 id < 0 &&
5503#endif
5504 lnum < sign->lnum)
5505 {
5506#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5507 /* XXX - GRP: Is this because of sign slide problem? Or is it
5508 * really needed? Or is it because we allow multiple signs per
5509 * line? If so, should I add that feature to FEAT_SIGNS?
5510 */
5511 while (prev != NULL && prev->lnum == lnum)
5512 prev = prev->prev;
5513 if (prev == NULL)
5514 sign = buf->b_signlist;
5515 else
5516 sign = prev->next;
5517#endif
5518 insert_sign(buf, prev, sign, id, lnum, typenr);
5519 return;
5520 }
5521 prev = sign;
5522 }
5523#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5524 /* XXX - GRP: See previous comment */
5525 while (prev != NULL && prev->lnum == lnum)
5526 prev = prev->prev;
5527 if (prev == NULL)
5528 sign = buf->b_signlist;
5529 else
5530 sign = prev->next;
5531#endif
5532 insert_sign(buf, prev, sign, id, lnum, typenr);
5533
5534 return;
5535}
5536
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005537/*
5538 * For an existing, placed sign "markId" change the type to "typenr".
5539 * Returns the line number of the sign, or zero if the sign is not found.
5540 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005541 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005542buf_change_sign_type(
5543 buf_T *buf, /* buffer to store sign in */
5544 int markId, /* sign ID */
5545 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 signlist_T *sign; /* a sign in the signlist */
5548
5549 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5550 {
5551 if (sign->id == markId)
5552 {
5553 sign->typenr = typenr;
5554 return sign->lnum;
5555 }
5556 }
5557
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005558 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559}
5560
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005561 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005562buf_getsigntype(
5563 buf_T *buf,
5564 linenr_T lnum,
5565 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 signlist_T *sign; /* a sign in a b_signlist */
5568
5569 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5570 if (sign->lnum == lnum
5571 && (type == SIGN_ANY
5572# ifdef FEAT_SIGN_ICONS
5573 || (type == SIGN_ICON
5574 && sign_get_image(sign->typenr) != NULL)
5575# endif
5576 || (type == SIGN_TEXT
5577 && sign_get_text(sign->typenr) != NULL)
5578 || (type == SIGN_LINEHL
5579 && sign_get_attr(sign->typenr, TRUE) != 0)))
5580 return sign->typenr;
5581 return 0;
5582}
5583
5584
5585 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005586buf_delsign(
5587 buf_T *buf, /* buffer sign is stored in */
5588 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
5590 signlist_T **lastp; /* pointer to pointer to current sign */
5591 signlist_T *sign; /* a sign in a b_signlist */
5592 signlist_T *next; /* the next sign in a b_signlist */
5593 linenr_T lnum; /* line number whose sign was deleted */
5594
5595 lastp = &buf->b_signlist;
5596 lnum = 0;
5597 for (sign = buf->b_signlist; sign != NULL; sign = next)
5598 {
5599 next = sign->next;
5600 if (sign->id == id)
5601 {
5602 *lastp = next;
5603#ifdef FEAT_NETBEANS_INTG
5604 if (next != NULL)
5605 next->prev = sign->prev;
5606#endif
5607 lnum = sign->lnum;
5608 vim_free(sign);
5609 break;
5610 }
5611 else
5612 lastp = &sign->next;
5613 }
5614
5615 /* When deleted the last sign need to redraw the windows to remove the
5616 * sign column. */
5617 if (buf->b_signlist == NULL)
5618 {
5619 redraw_buf_later(buf, NOT_VALID);
5620 changed_cline_bef_curs();
5621 }
5622
5623 return lnum;
5624}
5625
5626
5627/*
5628 * Find the line number of the sign with the requested id. If the sign does
5629 * not exist, return 0 as the line number. This will still let the correct file
5630 * get loaded.
5631 */
5632 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005633buf_findsign(
5634 buf_T *buf, /* buffer to store sign in */
5635 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 signlist_T *sign; /* a sign in the signlist */
5638
5639 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5640 if (sign->id == id)
5641 return sign->lnum;
5642
5643 return 0;
5644}
5645
5646 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005647buf_findsign_id(
5648 buf_T *buf, /* buffer whose sign we are searching for */
5649 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
5651 signlist_T *sign; /* a sign in the signlist */
5652
5653 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5654 if (sign->lnum == lnum)
5655 return sign->id;
5656
5657 return 0;
5658}
5659
5660
5661# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5662/* see if a given type of sign exists on a specific line */
5663 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005664buf_findsigntype_id(
5665 buf_T *buf, /* buffer whose sign we are searching for */
5666 linenr_T lnum, /* line number of sign */
5667 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
5669 signlist_T *sign; /* a sign in the signlist */
5670
5671 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5672 if (sign->lnum == lnum && sign->typenr == typenr)
5673 return sign->id;
5674
5675 return 0;
5676}
5677
5678
5679# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5680/* return the number of icons on the given line */
5681 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005682buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 signlist_T *sign; /* a sign in the signlist */
5685 int count = 0;
5686
5687 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5688 if (sign->lnum == lnum)
5689 if (sign_get_image(sign->typenr) != NULL)
5690 count++;
5691
5692 return count;
5693}
5694# endif /* FEAT_SIGN_ICONS */
5695# endif /* FEAT_NETBEANS_INTG */
5696
5697
5698/*
5699 * Delete signs in buffer "buf".
5700 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005702buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
5704 signlist_T *next;
5705
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005706 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005707 * sign column. Not when curwin is NULL (this means we're exiting). */
5708 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005709 {
5710 redraw_buf_later(buf, NOT_VALID);
5711 changed_cline_bef_curs();
5712 }
5713
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 while (buf->b_signlist != NULL)
5715 {
5716 next = buf->b_signlist->next;
5717 vim_free(buf->b_signlist);
5718 buf->b_signlist = next;
5719 }
5720}
5721
5722/*
5723 * Delete all signs in all buffers.
5724 */
5725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005726buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 buf_T *buf; /* buffer we are checking for signs */
5729
5730 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5731 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733}
5734
5735/*
5736 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5737 */
5738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005739sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 buf_T *buf;
5742 signlist_T *p;
5743 char lbuf[BUFSIZ];
5744
5745 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5746 msg_putchar('\n');
5747 if (rbuf == NULL)
5748 buf = firstbuf;
5749 else
5750 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005751 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 if (buf->b_signlist != NULL)
5754 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005755 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5757 msg_putchar('\n');
5758 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005759 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005761 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5763 MSG_PUTS(lbuf);
5764 msg_putchar('\n');
5765 }
5766 if (rbuf != NULL)
5767 break;
5768 buf = buf->b_next;
5769 }
5770}
5771
5772/*
5773 * Adjust a placed sign for inserted/deleted lines.
5774 */
5775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005776sign_mark_adjust(
5777 linenr_T line1,
5778 linenr_T line2,
5779 long amount,
5780 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 signlist_T *sign; /* a sign in a b_signlist */
5783
5784 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5785 {
5786 if (sign->lnum >= line1 && sign->lnum <= line2)
5787 {
5788 if (amount == MAXLNUM)
5789 sign->lnum = line1;
5790 else
5791 sign->lnum += amount;
5792 }
5793 else if (sign->lnum > line2)
5794 sign->lnum += amount_after;
5795 }
5796}
5797#endif /* FEAT_SIGNS */
5798
5799/*
5800 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5801 */
5802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005803set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 if (on != curbuf->b_p_bl)
5806 {
5807 curbuf->b_p_bl = on;
5808#ifdef FEAT_AUTOCMD
5809 if (on)
5810 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5811 else
5812 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5813#endif
5814 }
5815}
5816
5817/*
5818 * Read the file for "buf" again and check if the contents changed.
5819 * Return TRUE if it changed or this could not be checked.
5820 */
5821 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005822buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 buf_T *newbuf;
5825 int differ = TRUE;
5826 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 exarg_T ea;
5829
5830 /* Allocate a buffer without putting it in the buffer list. */
5831 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5832 if (newbuf == NULL)
5833 return TRUE;
5834
5835 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5836 if (prep_exarg(&ea, buf) == FAIL)
5837 {
5838 wipe_buffer(newbuf, FALSE);
5839 return TRUE;
5840 }
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 /* set curwin/curbuf to buf and save a few things */
5843 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
Bram Moolenaar4770d092006-01-12 23:22:24 +00005845 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 && readfile(buf->b_ffname, buf->b_fname,
5847 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5848 &ea, READ_NEW | READ_DUMMY) == OK)
5849 {
5850 /* compare the two files line by line */
5851 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5852 {
5853 differ = FALSE;
5854 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5855 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5856 {
5857 differ = TRUE;
5858 break;
5859 }
5860 }
5861 }
5862 vim_free(ea.cmd);
5863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 /* restore curwin/curbuf and a few other things */
5865 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867 if (curbuf != newbuf) /* safety check */
5868 wipe_buffer(newbuf, FALSE);
5869
5870 return differ;
5871}
5872
5873/*
5874 * Wipe out a buffer and decrement the last buffer number if it was used for
5875 * this buffer. Call this to wipe out a temp buffer that does not contain any
5876 * marks.
5877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005879wipe_buffer(
5880 buf_T *buf,
5881 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
5883 if (buf->b_fnum == top_file_num - 1)
5884 --top_file_num;
5885
5886#ifdef FEAT_AUTOCMD
5887 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005888 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005890 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891#ifdef FEAT_AUTOCMD
5892 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005893 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894#endif
5895}