blob: 058e4d785ba5dee86a95a902bd095d49aaf701c6 [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 Moolenaar8767f522016-07-01 17:17:39 +020038static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
40static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
48static void free_buffer(buf_T *);
49static void free_buffer_stuff(buf_T *buf, int free_options);
50static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar7fd73202010-07-25 16:58:46 +020062#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
63static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066#ifdef FEAT_AUTOCMD
67static char *e_auabort = N_("E855: Autocommands caused command to abort");
68#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram 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
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001622#ifdef FEAT_VIMINFO
1623 curbuf->b_last_used = vim_time();
1624#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 redraw_later(NOT_VALID);
1627}
1628
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001629#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1630/*
1631 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001632 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001633 */
1634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001635do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001636{
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001637 if (starting == 0
1638 && curbuf->b_ffname != NULL
1639 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001640 shorten_fnames(TRUE);
1641}
1642#endif
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644/*
1645 * functions for dealing with the buffer list
1646 */
1647
1648/*
1649 * Add a file name to the buffer list. Return a pointer to the buffer.
1650 * If the same file name already exists return a pointer to that buffer.
1651 * If it does not exist, or if fname == NULL, a new entry is created.
1652 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1653 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1654 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001655 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 * This is the ONLY way to create a new buffer.
1657 */
1658static int top_file_num = 1; /* highest file number */
1659
1660 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001661buflist_new(
1662 char_u *ffname, /* full path of fname or relative */
1663 char_u *sfname, /* short fname or NULL */
1664 linenr_T lnum, /* preferred cursor line */
1665 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 buf_T *buf;
1668#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001669 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670#endif
1671
1672 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1673
1674 /*
1675 * If file name already exists in the list, update the entry.
1676 */
1677#ifdef UNIX
1678 /* On Unix we can use inode numbers when the file exists. Works better
1679 * for hard links. */
1680 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1681 st.st_dev = (dev_T)-1;
1682#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001683 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#ifdef UNIX
1685 buflist_findname_stat(ffname, &st)
1686#else
1687 buflist_findname(ffname)
1688#endif
1689 ) != NULL)
1690 {
1691 vim_free(ffname);
1692 if (lnum != 0)
1693 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1694 /* copy the options now, if 'cpo' doesn't have 's' and not done
1695 * already */
1696 buf_copy_options(buf, 0);
1697 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1698 {
1699 buf->b_p_bl = TRUE;
1700#ifdef FEAT_AUTOCMD
1701 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001704 if (!buf_valid(buf))
1705 return NULL;
1706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707#endif
1708 }
1709 return buf;
1710 }
1711
1712 /*
1713 * If the current buffer has no name and no contents, use the current
1714 * buffer. Otherwise: Need to allocate a new buffer structure.
1715 *
1716 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001717 * (A spell file buffer is allocated in spell.c, but that's not a normal
1718 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 */
1720 buf = NULL;
1721 if ((flags & BLN_CURBUF)
1722 && curbuf != NULL
1723 && curbuf->b_ffname == NULL
1724 && curbuf->b_nwindows <= 1
1725 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1726 {
1727 buf = curbuf;
1728#ifdef FEAT_AUTOCMD
1729 /* It's like this buffer is deleted. Watch out for autocommands that
1730 * change curbuf! If that happens, allocate a new buffer anyway. */
1731 if (curbuf->b_p_bl)
1732 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1733 if (buf == curbuf)
1734 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1735# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001736 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 return NULL;
1738# endif
1739#endif
1740#ifdef FEAT_QUICKFIX
1741# ifdef FEAT_AUTOCMD
1742 if (buf == curbuf)
1743# endif
1744 {
1745 /* Make sure 'bufhidden' and 'buftype' are empty */
1746 clear_string_option(&buf->b_p_bh);
1747 clear_string_option(&buf->b_p_bt);
1748 }
1749#endif
1750 }
1751 if (buf != curbuf || curbuf == NULL)
1752 {
1753 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1754 if (buf == NULL)
1755 {
1756 vim_free(ffname);
1757 return NULL;
1758 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001759#ifdef FEAT_EVAL
1760 /* init b: variables */
1761 buf->b_vars = dict_alloc();
1762 if (buf->b_vars == NULL)
1763 {
1764 vim_free(ffname);
1765 vim_free(buf);
1766 return NULL;
1767 }
1768 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 }
1771
1772 if (ffname != NULL)
1773 {
1774 buf->b_ffname = ffname;
1775 buf->b_sfname = vim_strsave(sfname);
1776 }
1777
1778 clear_wininfo(buf);
1779 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1780
1781 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1782 || buf->b_wininfo == NULL)
1783 {
1784 vim_free(buf->b_ffname);
1785 buf->b_ffname = NULL;
1786 vim_free(buf->b_sfname);
1787 buf->b_sfname = NULL;
1788 if (buf != curbuf)
1789 free_buffer(buf);
1790 return NULL;
1791 }
1792
1793 if (buf == curbuf)
1794 {
1795 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001796 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (buf != curbuf) /* autocommands deleted the buffer! */
1798 return NULL;
1799#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001800 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 return NULL;
1802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001804
1805 /* Init the options. */
1806 buf->b_p_initialized = FALSE;
1807 buf_copy_options(buf, BCO_ENTER);
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#ifdef FEAT_KEYMAP
1810 /* need to reload lmaps and set b:keymap_name */
1811 curbuf->b_kmap_state |= KEYMAP_INIT;
1812#endif
1813 }
1814 else
1815 {
1816 /*
1817 * put new buffer at the end of the buffer list
1818 */
1819 buf->b_next = NULL;
1820 if (firstbuf == NULL) /* buffer list is empty */
1821 {
1822 buf->b_prev = NULL;
1823 firstbuf = buf;
1824 }
1825 else /* append new buffer at end of list */
1826 {
1827 lastbuf->b_next = buf;
1828 buf->b_prev = lastbuf;
1829 }
1830 lastbuf = buf;
1831
1832 buf->b_fnum = top_file_num++;
1833 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1834 {
1835 EMSG(_("W14: Warning: List of file names overflow"));
1836 if (emsg_silent == 0)
1837 {
1838 out_flush();
1839 ui_delay(3000L, TRUE); /* make sure it is noticed */
1840 }
1841 top_file_num = 1;
1842 }
1843
1844 /*
1845 * Always copy the options from the current buffer.
1846 */
1847 buf_copy_options(buf, BCO_ALWAYS);
1848 }
1849
1850 buf->b_wininfo->wi_fpos.lnum = lnum;
1851 buf->b_wininfo->wi_win = curwin;
1852
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001853#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001854 hash_init(&buf->b_s.b_keywtab);
1855 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856#endif
1857
1858 buf->b_fname = buf->b_sfname;
1859#ifdef UNIX
1860 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001861 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 else
1863 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001864 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 buf->b_dev = st.st_dev;
1866 buf->b_ino = st.st_ino;
1867 }
1868#endif
1869 buf->b_u_synced = TRUE;
1870 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001871 if (flags & BLN_DUMMY)
1872 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 buf_clear_file(buf);
1874 clrallmarks(buf); /* clear marks */
1875 fmarks_check_names(buf); /* check file marks for this file */
1876 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1877#ifdef FEAT_AUTOCMD
1878 if (!(flags & BLN_DUMMY))
1879 {
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001880 /* Tricky: these autocommands may change the buffer list. They could
1881 * also split the window with re-using the one empty buffer. This may
1882 * result in unexpectedly losing the empty buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001884 if (!buf_valid(buf))
1885 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001889 if (!buf_valid(buf))
1890 return NULL;
1891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001893 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 return NULL;
1895# endif
1896 }
1897#endif
1898
1899 return buf;
1900}
1901
1902/*
1903 * Free the memory for the options of a buffer.
1904 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1905 * 'fileencoding'.
1906 */
1907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001908free_buf_options(
1909 buf_T *buf,
1910 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
1912 if (free_p_ff)
1913 {
1914#ifdef FEAT_MBYTE
1915 clear_string_option(&buf->b_p_fenc);
1916#endif
1917 clear_string_option(&buf->b_p_ff);
1918#ifdef FEAT_QUICKFIX
1919 clear_string_option(&buf->b_p_bh);
1920 clear_string_option(&buf->b_p_bt);
1921#endif
1922 }
1923#ifdef FEAT_FIND_ID
1924 clear_string_option(&buf->b_p_def);
1925 clear_string_option(&buf->b_p_inc);
1926# ifdef FEAT_EVAL
1927 clear_string_option(&buf->b_p_inex);
1928# endif
1929#endif
1930#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1931 clear_string_option(&buf->b_p_inde);
1932 clear_string_option(&buf->b_p_indk);
1933#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001934#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1935 clear_string_option(&buf->b_p_bexpr);
1936#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001937#if defined(FEAT_CRYPT)
1938 clear_string_option(&buf->b_p_cm);
1939#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001940#if defined(FEAT_EVAL)
1941 clear_string_option(&buf->b_p_fex);
1942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943#ifdef FEAT_CRYPT
1944 clear_string_option(&buf->b_p_key);
1945#endif
1946 clear_string_option(&buf->b_p_kp);
1947 clear_string_option(&buf->b_p_mps);
1948 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001949 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 clear_string_option(&buf->b_p_isk);
1951#ifdef FEAT_KEYMAP
1952 clear_string_option(&buf->b_p_keymap);
1953 ga_clear(&buf->b_kmap_ga);
1954#endif
1955#ifdef FEAT_COMMENTS
1956 clear_string_option(&buf->b_p_com);
1957#endif
1958#ifdef FEAT_FOLDING
1959 clear_string_option(&buf->b_p_cms);
1960#endif
1961 clear_string_option(&buf->b_p_nf);
1962#ifdef FEAT_SYN_HL
1963 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001964 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001965#endif
1966#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001967 clear_string_option(&buf->b_s.b_p_spc);
1968 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001969 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001970 buf->b_s.b_cap_prog = NULL;
1971 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972#endif
1973#ifdef FEAT_SEARCHPATH
1974 clear_string_option(&buf->b_p_sua);
1975#endif
1976#ifdef FEAT_AUTOCMD
1977 clear_string_option(&buf->b_p_ft);
1978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#ifdef FEAT_CINDENT
1980 clear_string_option(&buf->b_p_cink);
1981 clear_string_option(&buf->b_p_cino);
1982#endif
1983#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1984 clear_string_option(&buf->b_p_cinw);
1985#endif
1986#ifdef FEAT_INS_EXPAND
1987 clear_string_option(&buf->b_p_cpt);
1988#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001989#ifdef FEAT_COMPL_FUNC
1990 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001991 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993#ifdef FEAT_QUICKFIX
1994 clear_string_option(&buf->b_p_gp);
1995 clear_string_option(&buf->b_p_mp);
1996 clear_string_option(&buf->b_p_efm);
1997#endif
1998 clear_string_option(&buf->b_p_ep);
1999 clear_string_option(&buf->b_p_path);
2000 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002001 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002#ifdef FEAT_INS_EXPAND
2003 clear_string_option(&buf->b_p_dict);
2004 clear_string_option(&buf->b_p_tsr);
2005#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002006#ifdef FEAT_TEXTOBJ
2007 clear_string_option(&buf->b_p_qe);
2008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002010 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002011#ifdef FEAT_LISP
2012 clear_string_option(&buf->b_p_lw);
2013#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002014 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015}
2016
2017/*
2018 * get alternate file n
2019 * set linenr to lnum or altfpos.lnum if lnum == 0
2020 * also set cursor column to altfpos.col if 'startofline' is not set.
2021 * if (options & GETF_SETMARK) call setpcmark()
2022 * if (options & GETF_ALT) we are jumping to an alternate file.
2023 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2024 *
2025 * return FAIL for failure, OK for success
2026 */
2027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002028buflist_getfile(
2029 int n,
2030 linenr_T lnum,
2031 int options,
2032 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
2034 buf_T *buf;
2035#ifdef FEAT_WINDOWS
2036 win_T *wp = NULL;
2037#endif
2038 pos_T *fpos;
2039 colnr_T col;
2040
2041 buf = buflist_findnr(n);
2042 if (buf == NULL)
2043 {
2044 if ((options & GETF_ALT) && n == 0)
2045 EMSG(_(e_noalt));
2046 else
2047 EMSGN(_("E92: Buffer %ld not found"), n);
2048 return FAIL;
2049 }
2050
2051 /* if alternate file is the current buffer, nothing to do */
2052 if (buf == curbuf)
2053 return OK;
2054
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002055 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002056 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002057 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002059 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002060#ifdef FEAT_AUTOCMD
2061 if (curbuf_locked())
2062 return FAIL;
2063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 /* altfpos may be changed by getfile(), get it now */
2066 if (lnum == 0)
2067 {
2068 fpos = buflist_findfpos(buf);
2069 lnum = fpos->lnum;
2070 col = fpos->col;
2071 }
2072 else
2073 col = 0;
2074
2075#ifdef FEAT_WINDOWS
2076 if (options & GETF_SWITCH)
2077 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002078 /* If 'switchbuf' contains "useopen": jump to first window containing
2079 * "buf" if one exists */
2080 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002082
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002083 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002084 * page containing "buf" if one exists */
2085 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002086 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002087
2088 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2089 * current buffer isn't empty: open new tab or window */
2090 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2091 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002093 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002094 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002095 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2096 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002098 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 }
2100 }
2101#endif
2102
2103 ++RedrawingDisabled;
2104 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2105 lnum, forceit) <= 0)
2106 {
2107 --RedrawingDisabled;
2108
2109 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2110 if (!p_sol && col != 0)
2111 {
2112 curwin->w_cursor.col = col;
2113 check_cursor_col();
2114#ifdef FEAT_VIRTUALEDIT
2115 curwin->w_cursor.coladd = 0;
2116#endif
2117 curwin->w_set_curswant = TRUE;
2118 }
2119 return OK;
2120 }
2121 --RedrawingDisabled;
2122 return FAIL;
2123}
2124
2125/*
2126 * go to the last know line number for the current buffer
2127 */
2128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002129buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
2131 pos_T *fpos;
2132
2133 fpos = buflist_findfpos(curbuf);
2134
2135 curwin->w_cursor.lnum = fpos->lnum;
2136 check_cursor_lnum();
2137
2138 if (p_sol)
2139 curwin->w_cursor.col = 0;
2140 else
2141 {
2142 curwin->w_cursor.col = fpos->col;
2143 check_cursor_col();
2144#ifdef FEAT_VIRTUALEDIT
2145 curwin->w_cursor.coladd = 0;
2146#endif
2147 curwin->w_set_curswant = TRUE;
2148 }
2149}
2150
Bram Moolenaar81695252004-12-29 20:58:21 +00002151#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2152/*
2153 * Find file in buffer list by name (it has to be for the current window).
2154 * Returns NULL if not found.
2155 */
2156 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002157buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002158{
2159 char_u *ffname;
2160 buf_T *buf = NULL;
2161
2162 /* First make the name into a full path name */
2163 ffname = FullName_save(fname,
2164#ifdef UNIX
2165 TRUE /* force expansion, get rid of symbolic links */
2166#else
2167 FALSE
2168#endif
2169 );
2170 if (ffname != NULL)
2171 {
2172 buf = buflist_findname(ffname);
2173 vim_free(ffname);
2174 }
2175 return buf;
2176}
2177#endif
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179/*
2180 * Find file in buffer list by name (it has to be for the current window).
2181 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002182 * Skips dummy buffers.
2183 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 */
2185 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
2188#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002189 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 if (mch_stat((char *)ffname, &st) < 0)
2192 st.st_dev = (dev_T)-1;
2193 return buflist_findname_stat(ffname, &st);
2194}
2195
2196/*
2197 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2198 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002199 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 */
2201 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202buflist_findname_stat(
2203 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002204 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
2206#endif
2207 buf_T *buf;
2208
2209 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002210 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef UNIX
2212 , stp
2213#endif
2214 ))
2215 return buf;
2216 return NULL;
2217}
2218
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002219#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2220 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221/*
2222 * Find file in buffer list by a regexp pattern.
2223 * Return fnum of the found buffer.
2224 * Return < 0 for error.
2225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227buflist_findpat(
2228 char_u *pattern,
2229 char_u *pattern_end, /* pointer to first char after pattern */
2230 int unlisted, /* find unlisted buffers */
2231 int diffmode UNUSED, /* find diff-mode buffers only */
2232 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 int match = -1;
2236 int find_listed;
2237 char_u *pat;
2238 char_u *patend;
2239 int attempt;
2240 char_u *p;
2241 int toggledollar;
2242
2243 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2244 {
2245 if (*pattern == '%')
2246 match = curbuf->b_fnum;
2247 else
2248 match = curwin->w_alt_fnum;
2249#ifdef FEAT_DIFF
2250 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2251 match = -1;
2252#endif
2253 }
2254
2255 /*
2256 * Try four ways of matching a listed buffer:
2257 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002258 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 * attempt == 2: with '$' at end (only match at end)
2260 * attempt == 3: with '^' at start and '$' at end (only full match)
2261 * Repeat this for finding an unlisted buffer if there was no matching
2262 * listed buffer.
2263 */
2264 else
2265 {
2266 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2267 if (pat == NULL)
2268 return -1;
2269 patend = pat + STRLEN(pat) - 1;
2270 toggledollar = (patend > pat && *patend == '$');
2271
2272 /* First try finding a listed buffer. If not found and "unlisted"
2273 * is TRUE, try finding an unlisted buffer. */
2274 find_listed = TRUE;
2275 for (;;)
2276 {
2277 for (attempt = 0; attempt <= 3; ++attempt)
2278 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002279 regmatch_T regmatch;
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 /* may add '^' and '$' */
2282 if (toggledollar)
2283 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2284 p = pat;
2285 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2286 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002287 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2288 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 vim_free(pat);
2291 return -1;
2292 }
2293
2294 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2295 if (buf->b_p_bl == find_listed
2296#ifdef FEAT_DIFF
2297 && (!diffmode || diff_mode_buf(buf))
2298#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002299 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002301 if (curtab_only)
2302 {
2303 /* Ignore the match if the buffer is not open in
2304 * the current tab. */
2305#ifdef FEAT_WINDOWS
2306 win_T *wp;
2307
2308 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2309 if (wp->w_buffer == buf)
2310 break;
2311 if (wp == NULL)
2312 continue;
2313#else
2314 if (curwin->w_buffer != buf)
2315 continue;
2316#endif
2317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 if (match >= 0) /* already found a match */
2319 {
2320 match = -2;
2321 break;
2322 }
2323 match = buf->b_fnum; /* remember first match */
2324 }
2325
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002326 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (match >= 0) /* found one match */
2328 break;
2329 }
2330
2331 /* Only search for unlisted buffers if there was no match with
2332 * a listed buffer. */
2333 if (!unlisted || !find_listed || match != -1)
2334 break;
2335 find_listed = FALSE;
2336 }
2337
2338 vim_free(pat);
2339 }
2340
2341 if (match == -2)
2342 EMSG2(_("E93: More than one match for %s"), pattern);
2343 else if (match < 0)
2344 EMSG2(_("E94: No matching buffer for %s"), pattern);
2345 return match;
2346}
2347#endif
2348
2349#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2350
2351/*
2352 * Find all buffer names that match.
2353 * For command line expansion of ":buf" and ":sbuf".
2354 * Return OK if matches found, FAIL otherwise.
2355 */
2356 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002357ExpandBufnames(
2358 char_u *pat,
2359 int *num_file,
2360 char_u ***file,
2361 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
2363 int count = 0;
2364 buf_T *buf;
2365 int round;
2366 char_u *p;
2367 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002368 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 *num_file = 0; /* return values in case of FAIL */
2371 *file = NULL;
2372
Bram Moolenaar05159a02005-02-26 23:04:13 +00002373 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2374 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002376 patc = alloc((unsigned)STRLEN(pat) + 11);
2377 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002379 STRCPY(patc, "\\(^\\|[\\/]\\)");
2380 STRCPY(patc + 11, pat + 1);
2381 }
2382 else
2383 patc = pat;
2384
2385 /*
2386 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002387 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002388 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002389 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002390 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002391 regmatch_T regmatch;
2392
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002393 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002394 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002395 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2396 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002397 {
2398 if (patc != pat)
2399 vim_free(patc);
2400 return FAIL;
2401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 /*
2404 * round == 1: Count the matches.
2405 * round == 2: Build the array to keep the matches.
2406 */
2407 for (round = 1; round <= 2; ++round)
2408 {
2409 count = 0;
2410 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2411 {
2412 if (!buf->b_p_bl) /* skip unlisted buffers */
2413 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002414 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (p != NULL)
2416 {
2417 if (round == 1)
2418 ++count;
2419 else
2420 {
2421 if (options & WILD_HOME_REPLACE)
2422 p = home_replace_save(buf, p);
2423 else
2424 p = vim_strsave(p);
2425 (*file)[count++] = p;
2426 }
2427 }
2428 }
2429 if (count == 0) /* no match found, break here */
2430 break;
2431 if (round == 1)
2432 {
2433 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2434 if (*file == NULL)
2435 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002436 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002437 if (patc != pat)
2438 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 return FAIL;
2440 }
2441 }
2442 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002443 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (count) /* match(es) found, break here */
2445 break;
2446 }
2447
Bram Moolenaar05159a02005-02-26 23:04:13 +00002448 if (patc != pat)
2449 vim_free(patc);
2450
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 *num_file = count;
2452 return (count == 0 ? FAIL : OK);
2453}
2454
2455#endif /* FEAT_CMDL_COMPL */
2456
2457#ifdef HAVE_BUFLIST_MATCH
2458/*
2459 * Check for a match on the file name for buffer "buf" with regprog "prog".
2460 */
2461 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002462buflist_match(
2463 regmatch_T *rmp,
2464 buf_T *buf,
2465 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 char_u *match;
2468
2469 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002470 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002472 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 return match;
2475}
2476
2477/*
2478 * Try matching the regexp in "prog" with file name "name".
2479 * Return "name" when there is a match, NULL when not.
2480 */
2481 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002482fname_match(
2483 regmatch_T *rmp,
2484 char_u *name,
2485 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 char_u *match = NULL;
2488 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
2490 if (name != NULL)
2491 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002492 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002493 rmp->rm_ic = p_fic || ignore_case;
2494 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 match = name;
2496 else
2497 {
2498 /* Replace $(HOME) with '~' and try matching again. */
2499 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002500 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 match = name;
2502 vim_free(p);
2503 }
2504 }
2505
2506 return match;
2507}
2508#endif
2509
2510/*
2511 * find file in buffer list by number
2512 */
2513 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002514buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 buf_T *buf;
2517
2518 if (nr == 0)
2519 nr = curwin->w_alt_fnum;
2520 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2521 if (buf->b_fnum == nr)
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002522 return buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 return NULL;
2524}
2525
2526/*
2527 * Get name of file 'n' in the buffer list.
2528 * When the file has no name an empty string is returned.
2529 * home_replace() is used to shorten the file name (used for marks).
2530 * Returns a pointer to allocated memory, of NULL when failed.
2531 */
2532 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002533buflist_nr2name(
2534 int n,
2535 int fullname,
2536 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538 buf_T *buf;
2539
2540 buf = buflist_findnr(n);
2541 if (buf == NULL)
2542 return NULL;
2543 return home_replace_save(helptail ? buf : NULL,
2544 fullname ? buf->b_ffname : buf->b_fname);
2545}
2546
2547/*
2548 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2549 * When "copy_options" is TRUE save the local window option values.
2550 * When "lnum" is 0 only do the options.
2551 */
2552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002553buflist_setfpos(
2554 buf_T *buf,
2555 win_T *win,
2556 linenr_T lnum,
2557 colnr_T col,
2558 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 wininfo_T *wip;
2561
2562 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2563 if (wip->wi_win == win)
2564 break;
2565 if (wip == NULL)
2566 {
2567 /* allocate a new entry */
2568 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2569 if (wip == NULL)
2570 return;
2571 wip->wi_win = win;
2572 if (lnum == 0) /* set lnum even when it's 0 */
2573 lnum = 1;
2574 }
2575 else
2576 {
2577 /* remove the entry from the list */
2578 if (wip->wi_prev)
2579 wip->wi_prev->wi_next = wip->wi_next;
2580 else
2581 buf->b_wininfo = wip->wi_next;
2582 if (wip->wi_next)
2583 wip->wi_next->wi_prev = wip->wi_prev;
2584 if (copy_options && wip->wi_optset)
2585 {
2586 clear_winopt(&wip->wi_opt);
2587#ifdef FEAT_FOLDING
2588 deleteFoldRecurse(&wip->wi_folds);
2589#endif
2590 }
2591 }
2592 if (lnum != 0)
2593 {
2594 wip->wi_fpos.lnum = lnum;
2595 wip->wi_fpos.col = col;
2596 }
2597 if (copy_options)
2598 {
2599 /* Save the window-specific option values. */
2600 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2601#ifdef FEAT_FOLDING
2602 wip->wi_fold_manual = win->w_fold_manual;
2603 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2604#endif
2605 wip->wi_optset = TRUE;
2606 }
2607
2608 /* insert the entry in front of the list */
2609 wip->wi_next = buf->b_wininfo;
2610 buf->b_wininfo = wip;
2611 wip->wi_prev = NULL;
2612 if (wip->wi_next)
2613 wip->wi_next->wi_prev = wip;
2614
2615 return;
2616}
2617
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002618#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002619static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002620
2621/*
2622 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2623 * page. That's because a diff is local to a tab page.
2624 */
2625 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002626wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002627{
2628 win_T *wp;
2629
2630 if (wip->wi_opt.wo_diff)
2631 {
2632 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2633 /* return FALSE when it's a window in the current tab page, thus
2634 * the buffer was in diff mode here */
2635 if (wip->wi_win == wp)
2636 return FALSE;
2637 return TRUE;
2638 }
2639 return FALSE;
2640}
2641#endif
2642
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643/*
2644 * Find info for the current window in buffer "buf".
2645 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002646 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2647 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 * Returns NULL when there isn't any info.
2649 */
2650 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002651find_wininfo(
2652 buf_T *buf,
2653 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654{
2655 wininfo_T *wip;
2656
2657 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002658 if (wip->wi_win == curwin
2659#ifdef FEAT_DIFF
2660 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2661#endif
2662 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002664
2665 /* If no wininfo for curwin, use the first in the list (that doesn't have
2666 * 'diff' set and is in another tab page). */
2667 if (wip == NULL)
2668 {
2669#ifdef FEAT_DIFF
2670 if (skip_diff_buffer)
2671 {
2672 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2673 if (!wininfo_other_tab_diff(wip))
2674 break;
2675 }
2676 else
2677#endif
2678 wip = buf->b_wininfo;
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 return wip;
2681}
2682
2683/*
2684 * Reset the local window options to the values last used in this window.
2685 * If the buffer wasn't used in this window before, use the values from
2686 * the most recently used window. If the values were never set, use the
2687 * global values for the window.
2688 */
2689 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002690get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 wininfo_T *wip;
2693
2694 clear_winopt(&curwin->w_onebuf_opt);
2695#ifdef FEAT_FOLDING
2696 clearFolding(curwin);
2697#endif
2698
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002699 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (wip != NULL && wip->wi_optset)
2701 {
2702 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2703#ifdef FEAT_FOLDING
2704 curwin->w_fold_manual = wip->wi_fold_manual;
2705 curwin->w_foldinvalid = TRUE;
2706 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2707#endif
2708 }
2709 else
2710 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2711
2712#ifdef FEAT_FOLDING
2713 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2714 if (p_fdls >= 0)
2715 curwin->w_p_fdl = p_fdls;
2716#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002717#ifdef FEAT_SYN_HL
2718 check_colorcolumn(curwin);
2719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720}
2721
2722/*
2723 * Find the position (lnum and col) for the buffer 'buf' for the current
2724 * window.
2725 * Returns a pointer to no_position if no position is found.
2726 */
2727 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002728buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729{
2730 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002731 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002733 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (wip != NULL)
2735 return &(wip->wi_fpos);
2736 else
2737 return &no_position;
2738}
2739
2740/*
2741 * Find the lnum for the buffer 'buf' for the current window.
2742 */
2743 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002744buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 return buflist_findfpos(buf)->lnum;
2747}
2748
2749#if defined(FEAT_LISTCMDS) || defined(PROTO)
2750/*
2751 * List all know file names (for :files and :buffers command).
2752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002754buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 buf_T *buf;
2757 int len;
2758 int i;
2759
2760 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2761 {
2762 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002763 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2764 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2765 || (vim_strchr(eap->arg, '+')
2766 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2767 || (vim_strchr(eap->arg, 'a')
2768 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2769 || (vim_strchr(eap->arg, 'h')
2770 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2771 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2772 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2773 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2774 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2775 || (vim_strchr(eap->arg, '#')
2776 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 continue;
2778 msg_putchar('\n');
2779 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002780 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 else
2782 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2783
Bram Moolenaar50cde822005-06-05 21:54:54 +00002784 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 buf->b_fnum,
2786 buf->b_p_bl ? ' ' : 'u',
2787 buf == curbuf ? '%' :
2788 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2789 buf->b_ml.ml_mfp == NULL ? ' ' :
2790 (buf->b_nwindows == 0 ? 'h' : 'a'),
2791 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2792 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002793 : (bufIsChanged(buf) ? '+' : ' '),
2794 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002795 if (len > IOSIZE - 20)
2796 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 i = 40 - vim_strsize(IObuff);
2800 do
2801 {
2802 IObuff[len++] = ' ';
2803 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002804 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2805 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002806 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 msg_outtrans(IObuff);
2808 out_flush(); /* output one line at a time */
2809 ui_breakcheck();
2810 }
2811}
2812#endif
2813
2814/*
2815 * Get file name and line number for file 'fnum'.
2816 * Used by DoOneCmd() for translating '%' and '#'.
2817 * Used by insert_reg() and cmdline_paste() for '#' register.
2818 * Return FAIL if not found, OK for success.
2819 */
2820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002821buflist_name_nr(
2822 int fnum,
2823 char_u **fname,
2824 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
2826 buf_T *buf;
2827
2828 buf = buflist_findnr(fnum);
2829 if (buf == NULL || buf->b_fname == NULL)
2830 return FAIL;
2831
2832 *fname = buf->b_fname;
2833 *lnum = buflist_findlnum(buf);
2834
2835 return OK;
2836}
2837
2838/*
2839 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2840 * The file name with the full path is also remembered, for when :cd is used.
2841 * Returns FAIL for failure (file name already in use by other buffer)
2842 * OK otherwise.
2843 */
2844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845setfname(
2846 buf_T *buf,
2847 char_u *ffname,
2848 char_u *sfname,
2849 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
Bram Moolenaar81695252004-12-29 20:58:21 +00002851 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002853 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854#endif
2855
2856 if (ffname == NULL || *ffname == NUL)
2857 {
2858 /* Removing the name. */
2859 vim_free(buf->b_ffname);
2860 vim_free(buf->b_sfname);
2861 buf->b_ffname = NULL;
2862 buf->b_sfname = NULL;
2863#ifdef UNIX
2864 st.st_dev = (dev_T)-1;
2865#endif
2866 }
2867 else
2868 {
2869 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2870 if (ffname == NULL) /* out of memory */
2871 return FAIL;
2872
2873 /*
2874 * if the file name is already used in another buffer:
2875 * - if the buffer is loaded, fail
2876 * - if the buffer is not loaded, delete it from the list
2877 */
2878#ifdef UNIX
2879 if (mch_stat((char *)ffname, &st) < 0)
2880 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002881#endif
2882 if (!(buf->b_flags & BF_DUMMY))
2883#ifdef UNIX
2884 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002886 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887#endif
2888 if (obuf != NULL && obuf != buf)
2889 {
2890 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2891 {
2892 if (message)
2893 EMSG(_("E95: Buffer with this name already exists"));
2894 vim_free(ffname);
2895 return FAIL;
2896 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002897 /* delete from the list */
2898 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900 sfname = vim_strsave(sfname);
2901 if (ffname == NULL || sfname == NULL)
2902 {
2903 vim_free(sfname);
2904 vim_free(ffname);
2905 return FAIL;
2906 }
2907#ifdef USE_FNAME_CASE
2908# ifdef USE_LONG_FNAME
2909 if (USE_LONG_FNAME)
2910# endif
2911 fname_case(sfname, 0); /* set correct case for short file name */
2912#endif
2913 vim_free(buf->b_ffname);
2914 vim_free(buf->b_sfname);
2915 buf->b_ffname = ffname;
2916 buf->b_sfname = sfname;
2917 }
2918 buf->b_fname = buf->b_sfname;
2919#ifdef UNIX
2920 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002921 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 else
2923 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002924 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 buf->b_dev = st.st_dev;
2926 buf->b_ino = st.st_ino;
2927 }
2928#endif
2929
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 buf_name_changed(buf);
2933 return OK;
2934}
2935
2936/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002937 * Crude way of changing the name of a buffer. Use with care!
2938 * The name should be relative to the current directory.
2939 */
2940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002941buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002942{
2943 buf_T *buf;
2944
2945 buf = buflist_findnr(fnum);
2946 if (buf != NULL)
2947 {
2948 vim_free(buf->b_sfname);
2949 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002950 buf->b_ffname = vim_strsave(name);
2951 buf->b_sfname = NULL;
2952 /* Allocate ffname and expand into full path. Also resolves .lnk
2953 * files on Win32. */
2954 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002955 buf->b_fname = buf->b_sfname;
2956 }
2957}
2958
2959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 * Take care of what needs to be done when the name of buffer "buf" has
2961 * changed.
2962 */
2963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002964buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965{
2966 /*
2967 * If the file name changed, also change the name of the swapfile
2968 */
2969 if (buf->b_ml.ml_mfp != NULL)
2970 ml_setname(buf);
2971
2972 if (curwin->w_buffer == buf)
2973 check_arg_idx(curwin); /* check file name for arg list */
2974#ifdef FEAT_TITLE
2975 maketitle(); /* set window title */
2976#endif
2977#ifdef FEAT_WINDOWS
2978 status_redraw_all(); /* status lines need to be redrawn */
2979#endif
2980 fmarks_check_names(buf); /* check named file marks */
2981 ml_timestamp(buf); /* reset timestamp */
2982}
2983
2984/*
2985 * set alternate file name for current window
2986 *
2987 * Used by do_one_cmd(), do_write() and do_ecmd().
2988 * Return the buffer.
2989 */
2990 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002991setaltfname(
2992 char_u *ffname,
2993 char_u *sfname,
2994 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 buf_T *buf;
2997
2998 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2999 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003000 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 curwin->w_alt_fnum = buf->b_fnum;
3002 return buf;
3003}
3004
3005/*
3006 * Get alternate file name for current window.
3007 * Return NULL if there isn't any, and give error message if requested.
3008 */
3009 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003010getaltfname(
3011 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 char_u *fname;
3014 linenr_T dummy;
3015
3016 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3017 {
3018 if (errmsg)
3019 EMSG(_(e_noalt));
3020 return NULL;
3021 }
3022 return fname;
3023}
3024
3025/*
3026 * Add a file name to the buflist and return its number.
3027 * Uses same flags as buflist_new(), except BLN_DUMMY.
3028 *
3029 * used by qf_init(), main() and doarglist()
3030 */
3031 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003032buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 buf_T *buf;
3035
3036 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3037 if (buf != NULL)
3038 return buf->b_fnum;
3039 return 0;
3040}
3041
3042#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3043/*
3044 * Adjust slashes in file names. Called after 'shellslash' was set.
3045 */
3046 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003047buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 buf_T *bp;
3050
3051 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3052 {
3053 if (bp->b_ffname != NULL)
3054 slash_adjust(bp->b_ffname);
3055 if (bp->b_sfname != NULL)
3056 slash_adjust(bp->b_sfname);
3057 }
3058}
3059#endif
3060
3061/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003062 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 * Also save the local window option values.
3064 */
3065 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003066buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003068 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069}
3070
3071/*
3072 * Return TRUE if 'ffname' is not the same file as current file.
3073 * Fname must have a full path (expanded by mch_FullName()).
3074 */
3075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003076otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 return otherfile_buf(curbuf, ffname
3079#ifdef UNIX
3080 , NULL
3081#endif
3082 );
3083}
3084
3085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086otherfile_buf(
3087 buf_T *buf,
3088 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003090 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003092 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 /* no name is different */
3095 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3096 return TRUE;
3097 if (fnamecmp(ffname, buf->b_ffname) == 0)
3098 return FALSE;
3099#ifdef UNIX
3100 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003101 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
Bram Moolenaar8767f522016-07-01 17:17:39 +02003103 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (stp == NULL)
3105 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003106 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 st.st_dev = (dev_T)-1;
3108 stp = &st;
3109 }
3110 /* Use dev/ino to check if the files are the same, even when the names
3111 * are different (possible with links). Still need to compare the
3112 * name above, for when the file doesn't exist yet.
3113 * Problem: The dev/ino changes when a file is deleted (and created
3114 * again) and remains the same when renamed/moved. We don't want to
3115 * mch_stat() each buffer each time, that would be too slow. Get the
3116 * dev/ino again when they appear to match, but not when they appear
3117 * to be different: Could skip a buffer when it's actually the same
3118 * file. */
3119 if (buf_same_ino(buf, stp))
3120 {
3121 buf_setino(buf);
3122 if (buf_same_ino(buf, stp))
3123 return FALSE;
3124 }
3125 }
3126#endif
3127 return TRUE;
3128}
3129
3130#if defined(UNIX) || defined(PROTO)
3131/*
3132 * Set inode and device number for a buffer.
3133 * Must always be called when b_fname is changed!.
3134 */
3135 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003136buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003138 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3141 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003142 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 buf->b_dev = st.st_dev;
3144 buf->b_ino = st.st_ino;
3145 }
3146 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003147 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148}
3149
3150/*
3151 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3152 */
3153 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003154buf_same_ino(
3155 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003156 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003158 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 && stp->st_dev == buf->b_dev
3160 && stp->st_ino == buf->b_ino);
3161}
3162#endif
3163
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003164/*
3165 * Print info about the current buffer.
3166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003168fileinfo(
3169 int fullname, /* when non-zero print full path */
3170 int shorthelp,
3171 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 char_u *name;
3174 int n;
3175 char_u *p;
3176 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003177 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 buffer = alloc(IOSIZE);
3180 if (buffer == NULL)
3181 return;
3182
3183 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3184 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003185 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 p = buffer + STRLEN(buffer);
3187 }
3188 else
3189 p = buffer;
3190
3191 *p++ = '"';
3192 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003193 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 else
3195 {
3196 if (!fullname && curbuf->b_fname != NULL)
3197 name = curbuf->b_fname;
3198 else
3199 name = curbuf->b_ffname;
3200 home_replace(shorthelp ? curbuf : NULL, name, p,
3201 (int)(IOSIZE - (p - buffer)), TRUE);
3202 }
3203
Bram Moolenaara800b422010-06-27 01:15:55 +02003204 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 curbufIsChanged() ? (shortmess(SHM_MOD)
3206 ? " [+]" : _(" [Modified]")) : " ",
3207 (curbuf->b_flags & BF_NOTEDITED)
3208#ifdef FEAT_QUICKFIX
3209 && !bt_dontwrite(curbuf)
3210#endif
3211 ? _("[Not edited]") : "",
3212 (curbuf->b_flags & BF_NEW)
3213#ifdef FEAT_QUICKFIX
3214 && !bt_dontwrite(curbuf)
3215#endif
3216 ? _("[New file]") : "",
3217 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003218 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 : _("[readonly]")) : "",
3220 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3221 || curbuf->b_p_ro) ?
3222 " " : "");
3223 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3224 * causes an overflow, thus for large numbers divide instead. */
3225 if (curwin->w_cursor.lnum > 1000000L)
3226 n = (int)(((long)curwin->w_cursor.lnum) /
3227 ((long)curbuf->b_ml.ml_line_count / 100L));
3228 else
3229 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3230 (long)curbuf->b_ml.ml_line_count);
3231 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3232 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003233 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 }
3235#ifdef FEAT_CMDL_INFO
3236 else if (p_ru)
3237 {
3238 /* Current line and column are already on the screen -- webb */
3239 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003240 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003242 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 (long)curbuf->b_ml.ml_line_count, n);
3244 }
3245#endif
3246 else
3247 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003248 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 _("line %ld of %ld --%d%%-- col "),
3250 (long)curwin->w_cursor.lnum,
3251 (long)curbuf->b_ml.ml_line_count,
3252 n);
3253 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003254 len = STRLEN(buffer);
3255 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3257 }
3258
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003259 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261 if (dont_truncate)
3262 {
3263 /* Temporarily set msg_scroll to avoid the message being truncated.
3264 * First call msg_start() to get the message in the right place. */
3265 msg_start();
3266 n = msg_scroll;
3267 msg_scroll = TRUE;
3268 msg(buffer);
3269 msg_scroll = n;
3270 }
3271 else
3272 {
3273 p = msg_trunc_attr(buffer, FALSE, 0);
3274 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 /* Need to repeat the message after redrawing when:
3276 * - When restart_edit is set (otherwise there will be a delay
3277 * before redrawing).
3278 * - When the screen was scrolled but there is no wait-return
3279 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003280 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282
3283 vim_free(buffer);
3284}
3285
3286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003287col_print(
3288 char_u *buf,
3289 size_t buflen,
3290 int col,
3291 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003294 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003296 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297}
3298
3299#if defined(FEAT_TITLE) || defined(PROTO)
3300/*
3301 * put file name in title bar of window and in icon title
3302 */
3303
3304static char_u *lasttitle = NULL;
3305static char_u *lasticon = NULL;
3306
3307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003308maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 char_u *p;
3311 char_u *t_str = NULL;
3312 char_u *i_name;
3313 char_u *i_str = NULL;
3314 int maxlen = 0;
3315 int len;
3316 int mustset;
3317 char_u buf[IOSIZE];
3318 int off;
3319
3320 if (!redrawing())
3321 {
3322 /* Postpone updating the title when 'lazyredraw' is set. */
3323 need_maketitle = TRUE;
3324 return;
3325 }
3326
3327 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003328 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 return;
3330
3331 if (p_title)
3332 {
3333 if (p_titlelen > 0)
3334 {
3335 maxlen = p_titlelen * Columns / 100;
3336 if (maxlen < 10)
3337 maxlen = 10;
3338 }
3339
3340 t_str = buf;
3341 if (*p_titlestring != NUL)
3342 {
3343#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003344 if (stl_syntax & STL_IN_TITLE)
3345 {
3346 int use_sandbox = FALSE;
3347 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003348
3349# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003350 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003351# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003352 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003354 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003355 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003356 if (called_emsg)
3357 set_string_option_direct((char_u *)"titlestring", -1,
3358 (char_u *)"", OPT_FREE, SID_ERROR);
3359 called_emsg |= save_called_emsg;
3360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 else
3362#endif
3363 t_str = p_titlestring;
3364 }
3365 else
3366 {
3367 /* format: "fname + (path) (1 of 2) - VIM" */
3368
Bram Moolenaar2c666692012-09-05 13:30:40 +02003369#define SPACE_FOR_FNAME (IOSIZE - 100)
3370#define SPACE_FOR_DIR (IOSIZE - 20)
3371#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003373 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else
3375 {
3376 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003377 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380
3381 switch (bufIsChanged(curbuf)
3382 + (curbuf->b_p_ro * 2)
3383 + (!curbuf->b_p_ma * 4))
3384 {
3385 case 1: STRCAT(buf, " +"); break;
3386 case 2: STRCAT(buf, " ="); break;
3387 case 3: STRCAT(buf, " =+"); break;
3388 case 4:
3389 case 6: STRCAT(buf, " -"); break;
3390 case 5:
3391 case 7: STRCAT(buf, " -+"); break;
3392 }
3393
3394 if (curbuf->b_fname != NULL)
3395 {
3396 /* Get path of file, replace home dir with ~ */
3397 off = (int)STRLEN(buf);
3398 buf[off++] = ' ';
3399 buf[off++] = '(';
3400 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003401 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#ifdef BACKSLASH_IN_FILENAME
3403 /* avoid "c:/name" to be reduced to "c" */
3404 if (isalpha(buf[off]) && buf[off + 1] == ':')
3405 off += 2;
3406#endif
3407 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003408 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003411 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003412 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003415
Bram Moolenaar2c666692012-09-05 13:30:40 +02003416 /* Translate unprintable chars and concatenate. Keep some
3417 * room for the server name. When there is no room (very long
3418 * file name) use (...). */
3419 if (off < SPACE_FOR_DIR)
3420 {
3421 p = transstr(buf + off);
3422 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3423 vim_free(p);
3424 }
3425 else
3426 {
3427 vim_strncpy(buf + off, (char_u *)"...",
3428 (size_t)(SPACE_FOR_ARGNR - off));
3429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 STRCAT(buf, ")");
3431 }
3432
Bram Moolenaar2c666692012-09-05 13:30:40 +02003433 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435#if defined(FEAT_CLIENTSERVER)
3436 if (serverName != NULL)
3437 {
3438 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003439 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441 else
3442#endif
3443 STRCAT(buf, " - VIM");
3444
3445 if (maxlen > 0)
3446 {
3447 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003448 if (vim_strsize(buf) > maxlen)
3449 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
3451 }
3452 }
3453 mustset = ti_change(t_str, &lasttitle);
3454
3455 if (p_icon)
3456 {
3457 i_str = buf;
3458 if (*p_iconstring != NUL)
3459 {
3460#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003461 if (stl_syntax & STL_IN_ICON)
3462 {
3463 int use_sandbox = FALSE;
3464 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003465
3466# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003467 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003468# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003469 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003471 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003472 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003473 if (called_emsg)
3474 set_string_option_direct((char_u *)"iconstring", -1,
3475 (char_u *)"", OPT_FREE, SID_ERROR);
3476 called_emsg |= save_called_emsg;
3477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 else
3479#endif
3480 i_str = p_iconstring;
3481 }
3482 else
3483 {
3484 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003485 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 else /* use file name only in icon */
3487 i_name = gettail(curbuf->b_ffname);
3488 *i_str = NUL;
3489 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003490 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (len > 100)
3492 {
3493 len -= 100;
3494#ifdef FEAT_MBYTE
3495 if (has_mbyte)
3496 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3497#endif
3498 i_name += len;
3499 }
3500 STRCPY(i_str, i_name);
3501 trans_characters(i_str, IOSIZE);
3502 }
3503 }
3504
3505 mustset |= ti_change(i_str, &lasticon);
3506
3507 if (mustset)
3508 resettitle();
3509}
3510
3511/*
3512 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3513 * from "str" if it does.
3514 * Return TRUE when "*last" changed.
3515 */
3516 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003517ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
3519 if ((str == NULL) != (*last == NULL)
3520 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3521 {
3522 vim_free(*last);
3523 if (str == NULL)
3524 *last = NULL;
3525 else
3526 *last = vim_strsave(str);
3527 return TRUE;
3528 }
3529 return FALSE;
3530}
3531
3532/*
3533 * Put current window title back (used after calling a shell)
3534 */
3535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003536resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
3538 mch_settitle(lasttitle, lasticon);
3539}
Bram Moolenaarea408852005-06-25 22:49:46 +00003540
3541# if defined(EXITFREE) || defined(PROTO)
3542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003543free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003544{
3545 vim_free(lasttitle);
3546 vim_free(lasticon);
3547}
3548# endif
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#endif /* FEAT_TITLE */
3551
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003552#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003554 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 * Return length of string in screen cells.
3556 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003557 * Normally works for window "wp", except when working for 'tabline' then it
3558 * is "curwin".
3559 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 * Items are drawn interspersed with the text that surrounds it
3561 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3562 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3563 *
3564 * If maxwidth is not zero, the string will be filled at any middle marker
3565 * or truncated if too long, fillchar is used for all whitespace.
3566 */
3567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568build_stl_str_hl(
3569 win_T *wp,
3570 char_u *out, /* buffer to write into != NameBuff */
3571 size_t outlen, /* length of out[] */
3572 char_u *fmt,
3573 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3574 int fillchar,
3575 int maxwidth,
3576 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3577 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 char_u *p;
3580 char_u *s;
3581 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003582 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583#ifdef FEAT_EVAL
3584 win_T *o_curwin;
3585 buf_T *o_curbuf;
3586#endif
3587 int empty_line;
3588 colnr_T virtcol;
3589 long l;
3590 long n;
3591 int prevchar_isflag;
3592 int prevchar_isitem;
3593 int itemisflag;
3594 int fillable;
3595 char_u *str;
3596 long num;
3597 int width;
3598 int itemcnt;
3599 int curitem;
3600 int groupitem[STL_MAX_ITEM];
3601 int groupdepth;
3602 struct stl_item
3603 {
3604 char_u *start;
3605 int minwid;
3606 int maxwid;
3607 enum
3608 {
3609 Normal,
3610 Empty,
3611 Group,
3612 Middle,
3613 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003614 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 Trunc
3616 } type;
3617 } item[STL_MAX_ITEM];
3618 int minwid;
3619 int maxwid;
3620 int zeropad;
3621 char_u base;
3622 char_u opt;
3623#define TMPLEN 70
3624 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003625 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003626 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003627
3628#ifdef FEAT_EVAL
3629 /*
3630 * When the format starts with "%!" then evaluate it as an expression and
3631 * use the result as the actual format string.
3632 */
3633 if (fmt[0] == '%' && fmt[1] == '!')
3634 {
3635 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3636 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003637 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003638 }
3639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
3641 if (fillchar == 0)
3642 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003643#ifdef FEAT_MBYTE
3644 /* Can't handle a multi-byte fill character yet. */
3645 else if (mb_char2len(fillchar) > 1)
3646 fillchar = '-';
3647#endif
3648
Bram Moolenaar567199b2013-04-24 16:52:36 +02003649 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3650 * p will become invalid when getting another buffer line. */
3651 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3652 empty_line = (*p == NUL);
3653
3654 /* Get the byte value now, in case we need it below. This is more
3655 * efficient than making a copy of the line. */
3656 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3657 byteval = 0;
3658 else
3659#ifdef FEAT_MBYTE
3660 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3661#else
3662 byteval = p[wp->w_cursor.col];
3663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 groupdepth = 0;
3666 p = out;
3667 curitem = 0;
3668 prevchar_isflag = TRUE;
3669 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003670 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003672 if (curitem == STL_MAX_ITEM)
3673 {
3674 /* There are too many items. Add the error code to the statusline
3675 * to give the user a hint about what went wrong. */
3676 if (p + 6 < out + outlen)
3677 {
3678 mch_memmove(p, " E541", (size_t)5);
3679 p += 5;
3680 }
3681 break;
3682 }
3683
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 if (*s != NUL && *s != '%')
3685 prevchar_isflag = prevchar_isitem = FALSE;
3686
3687 /*
3688 * Handle up to the next '%' or the end.
3689 */
3690 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3691 *p++ = *s++;
3692 if (*s == NUL || p + 1 >= out + outlen)
3693 break;
3694
3695 /*
3696 * Handle one '%' item.
3697 */
3698 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003699 if (*s == NUL) /* ignore trailing % */
3700 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 if (*s == '%')
3702 {
3703 if (p + 1 >= out + outlen)
3704 break;
3705 *p++ = *s++;
3706 prevchar_isflag = prevchar_isitem = FALSE;
3707 continue;
3708 }
3709 if (*s == STL_MIDDLEMARK)
3710 {
3711 s++;
3712 if (groupdepth > 0)
3713 continue;
3714 item[curitem].type = Middle;
3715 item[curitem++].start = p;
3716 continue;
3717 }
3718 if (*s == STL_TRUNCMARK)
3719 {
3720 s++;
3721 item[curitem].type = Trunc;
3722 item[curitem++].start = p;
3723 continue;
3724 }
3725 if (*s == ')')
3726 {
3727 s++;
3728 if (groupdepth < 1)
3729 continue;
3730 groupdepth--;
3731
3732 t = item[groupitem[groupdepth]].start;
3733 *p = NUL;
3734 l = vim_strsize(t);
3735 if (curitem > groupitem[groupdepth] + 1
3736 && item[groupitem[groupdepth]].minwid == 0)
3737 {
3738 /* remove group if all items are empty */
3739 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003740 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 break;
3742 if (n == curitem)
3743 {
3744 p = t;
3745 l = 0;
3746 }
3747 }
3748 if (l > item[groupitem[groupdepth]].maxwid)
3749 {
3750 /* truncate, remove n bytes of text at the start */
3751#ifdef FEAT_MBYTE
3752 if (has_mbyte)
3753 {
3754 /* Find the first character that should be included. */
3755 n = 0;
3756 while (l >= item[groupitem[groupdepth]].maxwid)
3757 {
3758 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003759 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761 }
3762 else
3763#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003764 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765
3766 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003767 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 p = p - n + 1;
3769#ifdef FEAT_MBYTE
3770 /* Fill up space left over by half a double-wide char. */
3771 while (++l < item[groupitem[groupdepth]].minwid)
3772 *p++ = fillchar;
3773#endif
3774
3775 /* correct the start of the items for the truncation */
3776 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3777 {
3778 item[l].start -= n;
3779 if (item[l].start < t)
3780 item[l].start = t;
3781 }
3782 }
3783 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3784 {
3785 /* fill */
3786 n = item[groupitem[groupdepth]].minwid;
3787 if (n < 0)
3788 {
3789 /* fill by appending characters */
3790 n = 0 - n;
3791 while (l++ < n && p + 1 < out + outlen)
3792 *p++ = fillchar;
3793 }
3794 else
3795 {
3796 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003797 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 l = n - l;
3799 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003800 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 p += l;
3802 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3803 item[n].start += l;
3804 for ( ; l > 0; l--)
3805 *t++ = fillchar;
3806 }
3807 }
3808 continue;
3809 }
3810 minwid = 0;
3811 maxwid = 9999;
3812 zeropad = FALSE;
3813 l = 1;
3814 if (*s == '0')
3815 {
3816 s++;
3817 zeropad = TRUE;
3818 }
3819 if (*s == '-')
3820 {
3821 s++;
3822 l = -1;
3823 }
3824 if (VIM_ISDIGIT(*s))
3825 {
3826 minwid = (int)getdigits(&s);
3827 if (minwid < 0) /* overflow */
3828 minwid = 0;
3829 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003830 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
3832 item[curitem].type = Highlight;
3833 item[curitem].start = p;
3834 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3835 s++;
3836 curitem++;
3837 continue;
3838 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003839 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3840 {
3841 if (*s == STL_TABCLOSENR)
3842 {
3843 if (minwid == 0)
3844 {
3845 /* %X ends the close label, go back to the previously
3846 * define tab label nr. */
3847 for (n = curitem - 1; n >= 0; --n)
3848 if (item[n].type == TabPage && item[n].minwid >= 0)
3849 {
3850 minwid = item[n].minwid;
3851 break;
3852 }
3853 }
3854 else
3855 /* close nrs are stored as negative values */
3856 minwid = - minwid;
3857 }
3858 item[curitem].type = TabPage;
3859 item[curitem].start = p;
3860 item[curitem].minwid = minwid;
3861 s++;
3862 curitem++;
3863 continue;
3864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 if (*s == '.')
3866 {
3867 s++;
3868 if (VIM_ISDIGIT(*s))
3869 {
3870 maxwid = (int)getdigits(&s);
3871 if (maxwid <= 0) /* overflow */
3872 maxwid = 50;
3873 }
3874 }
3875 minwid = (minwid > 50 ? 50 : minwid) * l;
3876 if (*s == '(')
3877 {
3878 groupitem[groupdepth++] = curitem;
3879 item[curitem].type = Group;
3880 item[curitem].start = p;
3881 item[curitem].minwid = minwid;
3882 item[curitem].maxwid = maxwid;
3883 s++;
3884 curitem++;
3885 continue;
3886 }
3887 if (vim_strchr(STL_ALL, *s) == NULL)
3888 {
3889 s++;
3890 continue;
3891 }
3892 opt = *s++;
3893
3894 /* OK - now for the real work */
3895 base = 'D';
3896 itemisflag = FALSE;
3897 fillable = TRUE;
3898 num = -1;
3899 str = NULL;
3900 switch (opt)
3901 {
3902 case STL_FILEPATH:
3903 case STL_FULLPATH:
3904 case STL_FILENAME:
3905 fillable = FALSE; /* don't change ' ' to fillchar */
3906 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003907 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 else
3909 {
3910 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003911 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3913 }
3914 trans_characters(NameBuff, MAXPATHL);
3915 if (opt != STL_FILENAME)
3916 str = NameBuff;
3917 else
3918 str = gettail(NameBuff);
3919 break;
3920
3921 case STL_VIM_EXPR: /* '{' */
3922 itemisflag = TRUE;
3923 t = p;
3924 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3925 *p++ = *s++;
3926 if (*s != '}') /* missing '}' or out of space */
3927 break;
3928 s++;
3929 *p = 0;
3930 p = t;
3931
3932#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003933 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3935
3936 o_curbuf = curbuf;
3937 o_curwin = curwin;
3938 curwin = wp;
3939 curbuf = wp->w_buffer;
3940
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003941 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
3943 curwin = o_curwin;
3944 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003945 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
3947 if (str != NULL && *str != 0)
3948 {
3949 if (*skipdigits(str) == NUL)
3950 {
3951 num = atoi((char *)str);
3952 vim_free(str);
3953 str = NULL;
3954 itemisflag = FALSE;
3955 }
3956 }
3957#endif
3958 break;
3959
3960 case STL_LINE:
3961 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3962 ? 0L : (long)(wp->w_cursor.lnum);
3963 break;
3964
3965 case STL_NUMLINES:
3966 num = wp->w_buffer->b_ml.ml_line_count;
3967 break;
3968
3969 case STL_COLUMN:
3970 num = !(State & INSERT) && empty_line
3971 ? 0 : (int)wp->w_cursor.col + 1;
3972 break;
3973
3974 case STL_VIRTCOL:
3975 case STL_VIRTCOL_ALT:
3976 /* In list mode virtcol needs to be recomputed */
3977 virtcol = wp->w_virtcol;
3978 if (wp->w_p_list && lcs_tab1 == NUL)
3979 {
3980 wp->w_p_list = FALSE;
3981 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3982 wp->w_p_list = TRUE;
3983 }
3984 ++virtcol;
3985 /* Don't display %V if it's the same as %c. */
3986 if (opt == STL_VIRTCOL_ALT
3987 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3988 ? 0 : (int)wp->w_cursor.col + 1)))
3989 break;
3990 num = (long)virtcol;
3991 break;
3992
3993 case STL_PERCENTAGE:
3994 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3995 (long)wp->w_buffer->b_ml.ml_line_count);
3996 break;
3997
3998 case STL_ALTPERCENT:
3999 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004000 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 break;
4002
4003 case STL_ARGLISTSTAT:
4004 fillable = FALSE;
4005 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004006 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 str = tmp;
4008 break;
4009
4010 case STL_KEYMAP:
4011 fillable = FALSE;
4012 if (get_keymap_str(wp, tmp, TMPLEN))
4013 str = tmp;
4014 break;
4015 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004016#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004017 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018#else
4019 num = 0;
4020#endif
4021 break;
4022
4023 case STL_BUFNO:
4024 num = wp->w_buffer->b_fnum;
4025 break;
4026
4027 case STL_OFFSET_X:
4028 base = 'X';
4029 case STL_OFFSET:
4030#ifdef FEAT_BYTEOFF
4031 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4032 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4033 0L : l + 1 + (!(State & INSERT) && empty_line ?
4034 0 : (int)wp->w_cursor.col);
4035#endif
4036 break;
4037
4038 case STL_BYTEVAL_X:
4039 base = 'X';
4040 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004041 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (num == NL)
4043 num = 0;
4044 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4045 num = NL;
4046 break;
4047
4048 case STL_ROFLAG:
4049 case STL_ROFLAG_ALT:
4050 itemisflag = TRUE;
4051 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004052 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 break;
4054
4055 case STL_HELPFLAG:
4056 case STL_HELPFLAG_ALT:
4057 itemisflag = TRUE;
4058 if (wp->w_buffer->b_help)
4059 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004060 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 break;
4062
4063#ifdef FEAT_AUTOCMD
4064 case STL_FILETYPE:
4065 if (*wp->w_buffer->b_p_ft != NUL
4066 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4067 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004068 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4069 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 str = tmp;
4071 }
4072 break;
4073
4074 case STL_FILETYPE_ALT:
4075 itemisflag = TRUE;
4076 if (*wp->w_buffer->b_p_ft != NUL
4077 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4078 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004079 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4080 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 for (t = tmp; *t != 0; t++)
4082 *t = TOUPPER_LOC(*t);
4083 str = tmp;
4084 }
4085 break;
4086#endif
4087
4088#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4089 case STL_PREVIEWFLAG:
4090 case STL_PREVIEWFLAG_ALT:
4091 itemisflag = TRUE;
4092 if (wp->w_p_pvw)
4093 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4094 : _("[Preview]"));
4095 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004096
4097 case STL_QUICKFIX:
4098 if (bt_quickfix(wp->w_buffer))
4099 str = (char_u *)(wp->w_llist_ref
4100 ? _(msg_loclist)
4101 : _(msg_qflist));
4102 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103#endif
4104
4105 case STL_MODIFIED:
4106 case STL_MODIFIED_ALT:
4107 itemisflag = TRUE;
4108 switch ((opt == STL_MODIFIED_ALT)
4109 + bufIsChanged(wp->w_buffer) * 2
4110 + (!wp->w_buffer->b_p_ma) * 4)
4111 {
4112 case 2: str = (char_u *)"[+]"; break;
4113 case 3: str = (char_u *)",+"; break;
4114 case 4: str = (char_u *)"[-]"; break;
4115 case 5: str = (char_u *)",-"; break;
4116 case 6: str = (char_u *)"[+-]"; break;
4117 case 7: str = (char_u *)",+-"; break;
4118 }
4119 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004120
4121 case STL_HIGHLIGHT:
4122 t = s;
4123 while (*s != '#' && *s != NUL)
4124 ++s;
4125 if (*s == '#')
4126 {
4127 item[curitem].type = Highlight;
4128 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004129 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004130 curitem++;
4131 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004132 if (*s != NUL)
4133 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004134 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136
4137 item[curitem].start = p;
4138 item[curitem].type = Normal;
4139 if (str != NULL && *str)
4140 {
4141 t = str;
4142 if (itemisflag)
4143 {
4144 if ((t[0] && t[1])
4145 && ((!prevchar_isitem && *t == ',')
4146 || (prevchar_isflag && *t == ' ')))
4147 t++;
4148 prevchar_isflag = TRUE;
4149 }
4150 l = vim_strsize(t);
4151 if (l > 0)
4152 prevchar_isitem = TRUE;
4153 if (l > maxwid)
4154 {
4155 while (l >= maxwid)
4156#ifdef FEAT_MBYTE
4157 if (has_mbyte)
4158 {
4159 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004160 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162 else
4163#endif
4164 l -= byte2cells(*t++);
4165 if (p + 1 >= out + outlen)
4166 break;
4167 *p++ = '<';
4168 }
4169 if (minwid > 0)
4170 {
4171 for (; l < minwid && p + 1 < out + outlen; l++)
4172 {
4173 /* Don't put a "-" in front of a digit. */
4174 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4175 *p++ = ' ';
4176 else
4177 *p++ = fillchar;
4178 }
4179 minwid = 0;
4180 }
4181 else
4182 minwid *= -1;
4183 while (*t && p + 1 < out + outlen)
4184 {
4185 *p++ = *t++;
4186 /* Change a space by fillchar, unless fillchar is '-' and a
4187 * digit follows. */
4188 if (fillable && p[-1] == ' '
4189 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4190 p[-1] = fillchar;
4191 }
4192 for (; l < minwid && p + 1 < out + outlen; l++)
4193 *p++ = fillchar;
4194 }
4195 else if (num >= 0)
4196 {
4197 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4198 char_u nstr[20];
4199
4200 if (p + 20 >= out + outlen)
4201 break; /* not sufficient space */
4202 prevchar_isitem = TRUE;
4203 t = nstr;
4204 if (opt == STL_VIRTCOL_ALT)
4205 {
4206 *t++ = '-';
4207 minwid--;
4208 }
4209 *t++ = '%';
4210 if (zeropad)
4211 *t++ = '0';
4212 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004213 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 *t = 0;
4215
4216 for (n = num, l = 1; n >= nbase; n /= nbase)
4217 l++;
4218 if (opt == STL_VIRTCOL_ALT)
4219 l++;
4220 if (l > maxwid)
4221 {
4222 l += 2;
4223 n = l - maxwid;
4224 while (l-- > maxwid)
4225 num /= nbase;
4226 *t++ = '>';
4227 *t++ = '%';
4228 *t = t[-3];
4229 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004230 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4231 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004234 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4235 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 p += STRLEN(p);
4237 }
4238 else
4239 item[curitem].type = Empty;
4240
4241 if (opt == STL_VIM_EXPR)
4242 vim_free(str);
4243
4244 if (num >= 0 || (!itemisflag && str && *str))
4245 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4246 curitem++;
4247 }
4248 *p = NUL;
4249 itemcnt = curitem;
4250
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004251#ifdef FEAT_EVAL
4252 if (usefmt != fmt)
4253 vim_free(usefmt);
4254#endif
4255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 width = vim_strsize(out);
4257 if (maxwidth > 0 && width > maxwidth)
4258 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004259 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 l = 0;
4261 if (itemcnt == 0)
4262 s = out;
4263 else
4264 {
4265 for ( ; l < itemcnt; l++)
4266 if (item[l].type == Trunc)
4267 {
4268 /* Truncate at %< item. */
4269 s = item[l].start;
4270 break;
4271 }
4272 if (l == itemcnt)
4273 {
4274 /* No %< item, truncate first item. */
4275 s = item[0].start;
4276 l = 0;
4277 }
4278 }
4279
4280 if (width - vim_strsize(s) >= maxwidth)
4281 {
4282 /* Truncation mark is beyond max length */
4283#ifdef FEAT_MBYTE
4284 if (has_mbyte)
4285 {
4286 s = out;
4287 width = 0;
4288 for (;;)
4289 {
4290 width += ptr2cells(s);
4291 if (width >= maxwidth)
4292 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004293 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 /* Fill up for half a double-wide character. */
4296 while (++width < maxwidth)
4297 *s++ = fillchar;
4298 }
4299 else
4300#endif
4301 s = out + maxwidth - 1;
4302 for (l = 0; l < itemcnt; l++)
4303 if (item[l].start > s)
4304 break;
4305 itemcnt = l;
4306 *s++ = '>';
4307 *s = 0;
4308 }
4309 else
4310 {
4311#ifdef FEAT_MBYTE
4312 if (has_mbyte)
4313 {
4314 n = 0;
4315 while (width >= maxwidth)
4316 {
4317 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004318 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 }
4321 else
4322#endif
4323 n = width - maxwidth + 1;
4324 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004325 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 *s = '<';
4327
4328 /* Fill up for half a double-wide character. */
4329 while (++width < maxwidth)
4330 {
4331 s = s + STRLEN(s);
4332 *s++ = fillchar;
4333 *s = NUL;
4334 }
4335
4336 --n; /* count the '<' */
4337 for (; l < itemcnt; l++)
4338 {
4339 if (item[l].start - n >= s)
4340 item[l].start -= n;
4341 else
4342 item[l].start = s;
4343 }
4344 }
4345 width = maxwidth;
4346 }
4347 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4348 {
4349 /* Apply STL_MIDDLE if any */
4350 for (l = 0; l < itemcnt; l++)
4351 if (item[l].type == Middle)
4352 break;
4353 if (l < itemcnt)
4354 {
4355 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004356 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 for (s = item[l].start; s < p; s++)
4358 *s = fillchar;
4359 for (l++; l < itemcnt; l++)
4360 item[l].start += maxwidth - width;
4361 width = maxwidth;
4362 }
4363 }
4364
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004365 /* Store the info about highlighting. */
4366 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004368 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 for (l = 0; l < itemcnt; l++)
4370 {
4371 if (item[l].type == Highlight)
4372 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004373 sp->start = item[l].start;
4374 sp->userhl = item[l].minwid;
4375 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004378 sp->start = NULL;
4379 sp->userhl = 0;
4380 }
4381
4382 /* Store the info about tab pages labels. */
4383 if (tabtab != NULL)
4384 {
4385 sp = tabtab;
4386 for (l = 0; l < itemcnt; l++)
4387 {
4388 if (item[l].type == TabPage)
4389 {
4390 sp->start = item[l].start;
4391 sp->userhl = item[l].minwid;
4392 sp++;
4393 }
4394 }
4395 sp->start = NULL;
4396 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398
4399 return width;
4400}
4401#endif /* FEAT_STL_OPT */
4402
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004403#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4404 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004406 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4407 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 */
4409 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004410get_rel_pos(
4411 win_T *wp,
4412 char_u *buf,
4413 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414{
4415 long above; /* number of lines above window */
4416 long below; /* number of lines below window */
4417
Bram Moolenaar0027c212015-01-07 13:31:52 +01004418 if (buflen < 3) /* need at least 3 chars for writing */
4419 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 above = wp->w_topline - 1;
4421#ifdef FEAT_DIFF
4422 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004423 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4424 above = 0; /* All buffer lines are displayed and there is an
4425 * indication of filler lines, that can be considered
4426 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427#endif
4428 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4429 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004430 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4431 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004433 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004435 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 ? (int)(above / ((above + below) / 100L))
4437 : (int)(above * 100L / (above + below)));
4438}
4439#endif
4440
4441/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004442 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 * Return TRUE if it was appended.
4444 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004445 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004446append_arg_number(
4447 win_T *wp,
4448 char_u *buf,
4449 int buflen,
4450 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 char_u *p;
4453
4454 if (ARGCOUNT <= 1) /* nothing to do */
4455 return FALSE;
4456
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004457 p = buf + STRLEN(buf); /* go to the end of the buffer */
4458 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return FALSE;
4460 *p++ = ' ';
4461 *p++ = '(';
4462 if (add_file)
4463 {
4464 STRCPY(p, "file ");
4465 p += 5;
4466 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004467 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4468 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4470 return TRUE;
4471}
4472
4473/*
4474 * If fname is not a full path, make it a full path.
4475 * Returns pointer to allocated memory (NULL for failure).
4476 */
4477 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004478fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479{
4480 /*
4481 * Force expanding the path always for Unix, because symbolic links may
4482 * mess up the full path name, even though it starts with a '/'.
4483 * Also expand when there is ".." in the file name, try to remove it,
4484 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004485 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 * For MS-Windows also expand names like "longna~1" to "longname".
4487 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004488#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 return FullName_save(fname, TRUE);
4490#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004491 if (!vim_isAbsName(fname)
4492 || strstr((char *)fname, "..") != NULL
4493 || strstr((char *)fname, "//") != NULL
4494# ifdef BACKSLASH_IN_FILENAME
4495 || strstr((char *)fname, "\\\\") != NULL
4496# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004497# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 )
4501 return FullName_save(fname, FALSE);
4502
4503 fname = vim_strsave(fname);
4504
Bram Moolenaar9b942202007-10-03 12:31:33 +00004505# ifdef USE_FNAME_CASE
4506# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
4510 if (fname != NULL)
4511 fname_case(fname, 0); /* set correct case for file name */
4512 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004513# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
4515 return fname;
4516#endif
4517}
4518
4519/*
4520 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4521 * "ffname" becomes a pointer to allocated memory (or NULL).
4522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004524fname_expand(
4525 buf_T *buf UNUSED,
4526 char_u **ffname,
4527 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528{
4529 if (*ffname == NULL) /* if no file name given, nothing to do */
4530 return;
4531 if (*sfname == NULL) /* if no short file name given, use ffname */
4532 *sfname = *ffname;
4533 *ffname = fix_fname(*ffname); /* expand to full path */
4534
4535#ifdef FEAT_SHORTCUT
4536 if (!buf->b_p_bin)
4537 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004538 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540 /* If the file name is a shortcut file, use the file it links to. */
4541 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004542 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
4544 vim_free(*ffname);
4545 *ffname = rfname;
4546 *sfname = rfname;
4547 }
4548 }
4549#endif
4550}
4551
4552/*
4553 * Get the file name for an argument list entry.
4554 */
4555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004556alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
4558 buf_T *bp;
4559
4560 /* Use the name from the associated buffer if it exists. */
4561 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004562 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 return aep->ae_fname;
4564 return bp->b_fname;
4565}
4566
4567#if defined(FEAT_WINDOWS) || defined(PROTO)
4568/*
4569 * do_arg_all(): Open up to 'count' windows, one for each argument.
4570 */
4571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004572do_arg_all(
4573 int count,
4574 int forceit, /* hide buffers in current windows */
4575 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576{
4577 int i;
4578 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004579 char_u *opened; /* Array of weight for which args are open:
4580 * 0: not opened
4581 * 1: opened in other tab
4582 * 2: opened in curtab
4583 * 3: opened in curtab and curwin
4584 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004585 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 int use_firstwin = FALSE; /* use first window for arglist */
4587 int split_ret = OK;
4588 int p_ea_save;
4589 alist_T *alist; /* argument list to be used */
4590 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004591 tabpage_T *tpnext;
4592 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004593 win_T *old_curwin, *last_curwin;
4594 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004595 win_T *new_curwin = NULL;
4596 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 if (ARGCOUNT <= 0)
4599 {
4600 /* Don't give an error message. We don't want it when the ":all"
4601 * command is in the .vimrc. */
4602 return;
4603 }
4604 setpcmark();
4605
4606 opened_len = ARGCOUNT;
4607 opened = alloc_clear((unsigned)opened_len);
4608 if (opened == NULL)
4609 return;
4610
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004611 /* Autocommands may do anything to the argument list. Make sure it's not
4612 * freed while we are working here by "locking" it. We still have to
4613 * watch out for its size to be changed. */
4614 alist = curwin->w_alist;
4615 ++alist->al_refcount;
4616
4617 old_curwin = curwin;
4618 old_curtab = curtab;
4619
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004620# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
4624 /*
4625 * Try closing all windows that are not in the argument list.
4626 * Also close windows that are not full width;
4627 * When 'hidden' or "forceit" set the buffer becomes hidden.
4628 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004629 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004631 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004632 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004633 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004635 tpnext = curtab->tp_next;
4636 for (wp = firstwin; wp != NULL; wp = wpnext)
4637 {
4638 wpnext = wp->w_next;
4639 buf = wp->w_buffer;
4640 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004641 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004642 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004643 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004646 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004647 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004649 if (i < alist->al_ga.ga_len
4650 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4651 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4652 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004654 int weight = 1;
4655
4656 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004657 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004658 ++weight;
4659 if (old_curwin == wp)
4660 ++weight;
4661 }
4662
4663 if (weight > (int)opened[i])
4664 {
4665 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004666 if (i == 0)
4667 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004668 if (new_curwin != NULL)
4669 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004670 new_curwin = wp;
4671 new_curtab = curtab;
4672 }
4673 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004674 else if (keep_tabs)
4675 i = opened_len;
4676
4677 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004678 {
4679 /* Use the current argument list for all windows
4680 * containing a file from it. */
4681 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004682 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004683 ++wp->w_alist->al_refcount;
4684 }
4685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004689 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004691 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004693 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004694 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004696 /* If the buffer was changed, and we would like to hide it,
4697 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004698 if (!P_HID(buf) && buf->b_nwindows <= 1
4699 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004701 (void)autowrite(buf, FALSE);
4702#ifdef FEAT_AUTOCMD
4703 /* check if autocommands removed the window */
4704 if (!win_valid(wp) || !buf_valid(buf))
4705 {
4706 wpnext = firstwin; /* start all over... */
4707 continue;
4708 }
4709#endif
4710 }
4711#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004712 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004713 if (firstwin == lastwin
4714 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004715#endif
4716 use_firstwin = TRUE;
4717#ifdef FEAT_WINDOWS
4718 else
4719 {
4720 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4721# ifdef FEAT_AUTOCMD
4722 /* check if autocommands removed the next window */
4723 if (!win_valid(wpnext))
4724 wpnext = firstwin; /* start all over... */
4725# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
4727#endif
4728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 }
4730 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004731
4732 /* Without the ":tab" modifier only do the current tab page. */
4733 if (had_tab == 0 || tpnext == NULL)
4734 break;
4735
4736# ifdef FEAT_AUTOCMD
4737 /* check if autocommands removed the next tab page */
4738 if (!valid_tabpage(tpnext))
4739 tpnext = first_tabpage; /* start all over...*/
4740# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004741 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743
4744 /*
4745 * Open a window for files in the argument list that don't have one.
4746 * ARGCOUNT may change while doing this, because of autocommands.
4747 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004748 if (count > opened_len || count <= 0)
4749 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751#ifdef FEAT_AUTOCMD
4752 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4753 ++autocmd_no_enter;
4754 ++autocmd_no_leave;
4755#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004756 last_curwin = curwin;
4757 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004759#ifdef FEAT_WINDOWS
4760 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4761 * leaving an empty tab page when executed locally. */
4762 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4763 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4764 use_firstwin = TRUE;
4765#endif
4766
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004767 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
4769 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4770 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004771 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
4773 /* Move the already present window to below the current window */
4774 if (curwin->w_arg_idx != i)
4775 {
4776 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4777 {
4778 if (wpnext->w_arg_idx == i)
4779 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004780 if (keep_tabs)
4781 {
4782 new_curwin = wpnext;
4783 new_curtab = curtab;
4784 }
4785 else
4786 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 break;
4788 }
4789 }
4790 }
4791 }
4792 else if (split_ret == OK)
4793 {
4794 if (!use_firstwin) /* split current window */
4795 {
4796 p_ea_save = p_ea;
4797 p_ea = TRUE; /* use space from all windows */
4798 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4799 p_ea = p_ea_save;
4800 if (split_ret == FAIL)
4801 continue;
4802 }
4803#ifdef FEAT_AUTOCMD
4804 else /* first window: do autocmd for leaving this buffer */
4805 --autocmd_no_leave;
4806#endif
4807
4808 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004809 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 */
4811 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004812 if (i == 0)
4813 {
4814 new_curwin = curwin;
4815 new_curtab = curtab;
4816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4818 ECMD_ONE,
4819 ((P_HID(curwin->w_buffer)
4820 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004821 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822#ifdef FEAT_AUTOCMD
4823 if (use_firstwin)
4824 ++autocmd_no_leave;
4825#endif
4826 use_firstwin = FALSE;
4827 }
4828 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004829
4830 /* When ":tab" was used open a new tab for a new window repeatedly. */
4831 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4832 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834
4835 /* Remove the "lock" on the argument list. */
4836 alist_unlink(alist);
4837
4838#ifdef FEAT_AUTOCMD
4839 --autocmd_no_enter;
4840#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004841 /* restore last referenced tabpage's curwin */
4842 if (last_curtab != new_curtab)
4843 {
4844 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004845 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004846 if (win_valid(last_curwin))
4847 win_enter(last_curwin, FALSE);
4848 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004849 /* to window with first arg */
4850 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004851 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004852 if (win_valid(new_curwin))
4853 win_enter(new_curwin, FALSE);
4854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855#ifdef FEAT_AUTOCMD
4856 --autocmd_no_leave;
4857#endif
4858 vim_free(opened);
4859}
4860
4861# if defined(FEAT_LISTCMDS) || defined(PROTO)
4862/*
4863 * Open a window for a number of buffers.
4864 */
4865 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004866ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
4868 buf_T *buf;
4869 win_T *wp, *wpnext;
4870 int split_ret = OK;
4871 int p_ea_save;
4872 int open_wins = 0;
4873 int r;
4874 int count; /* Maximum number of windows to open. */
4875 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004876#ifdef FEAT_WINDOWS
4877 int had_tab = cmdmod.tab;
4878 tabpage_T *tpnext;
4879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 if (eap->addr_count == 0) /* make as many windows as possible */
4882 count = 9999;
4883 else
4884 count = eap->line2; /* make as many windows as specified */
4885 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4886 all = FALSE;
4887 else
4888 all = TRUE;
4889
4890 setpcmark();
4891
4892#ifdef FEAT_GUI
4893 need_mouse_correct = TRUE;
4894#endif
4895
4896 /*
4897 * Close superfluous windows (two windows for the same buffer).
4898 * Also close windows that are not full-width.
4899 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004900#ifdef FEAT_WINDOWS
4901 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004902 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004903 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004906 tpnext = curtab->tp_next;
4907 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004909 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004910 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004911#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004912 || ((cmdmod.split & WSP_VERT)
4913 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004914 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004915 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004916 || (had_tab > 0 && wp != firstwin)
4917#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004918 ) && firstwin != lastwin
4919#ifdef FEAT_AUTOCMD
4920 && !(wp->w_closing || wp->w_buffer->b_closing)
4921#endif
4922 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004923 {
4924 win_close(wp, FALSE);
4925#ifdef FEAT_AUTOCMD
4926 wpnext = firstwin; /* just in case an autocommand does
4927 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004928 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004929 open_wins = 0;
4930#endif
4931 }
4932 else
4933 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004935
4936#ifdef FEAT_WINDOWS
4937 /* Without the ":tab" modifier only do the current tab page. */
4938 if (had_tab == 0 || tpnext == NULL)
4939 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004940 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944 /*
4945 * Go through the buffer list. When a buffer doesn't have a window yet,
4946 * open one. Otherwise move the window to the right position.
4947 * Watch out for autocommands that delete buffers or windows!
4948 */
4949#ifdef FEAT_AUTOCMD
4950 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4951 ++autocmd_no_enter;
4952#endif
4953 win_enter(lastwin, FALSE);
4954#ifdef FEAT_AUTOCMD
4955 ++autocmd_no_leave;
4956#endif
4957 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4958 {
4959 /* Check if this buffer needs a window */
4960 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4961 continue;
4962
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004963#ifdef FEAT_WINDOWS
4964 if (had_tab != 0)
4965 {
4966 /* With the ":tab" modifier don't move the window. */
4967 if (buf->b_nwindows > 0)
4968 wp = lastwin; /* buffer has a window, skip it */
4969 else
4970 wp = NULL;
4971 }
4972 else
4973#endif
4974 {
4975 /* Check if this buffer already has a window */
4976 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4977 if (wp->w_buffer == buf)
4978 break;
4979 /* If the buffer already has a window, move it */
4980 if (wp != NULL)
4981 win_move_after(wp, curwin);
4982 }
4983
4984 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 /* Split the window and put the buffer in it */
4987 p_ea_save = p_ea;
4988 p_ea = TRUE; /* use space from all windows */
4989 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4990 ++open_wins;
4991 p_ea = p_ea_save;
4992 if (split_ret == FAIL)
4993 continue;
4994
4995 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004996#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 swap_exists_action = SEA_DIALOG;
4998#endif
4999 set_curbuf(buf, DOBUF_GOTO);
5000#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00005003#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 break;
5007 }
5008#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005009#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (swap_exists_action == SEA_QUIT)
5011 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005012# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5013 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005014
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005015 /* Reset the error/interrupt/exception state here so that
5016 * aborting() returns FALSE when closing a window. */
5017 enter_cleanup(&cs);
5018# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005019
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005020 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 win_close(curwin, TRUE);
5022 --open_wins;
5023 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005024 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005025
5026# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5027 /* Restore the error/interrupt/exception state if not
5028 * discarded by a new aborting error, interrupt, or uncaught
5029 * exception. */
5030 leave_cleanup(&cs);
5031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 else
5034 handle_swap_exists(NULL);
5035#endif
5036 }
5037
5038 ui_breakcheck();
5039 if (got_int)
5040 {
5041 (void)vgetc(); /* only break the file loading, not the rest */
5042 break;
5043 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005044#ifdef FEAT_EVAL
5045 /* Autocommands deleted the buffer or aborted script processing!!! */
5046 if (aborting())
5047 break;
5048#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005049#ifdef FEAT_WINDOWS
5050 /* When ":tab" was used open a new tab for a new window repeatedly. */
5051 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5052 cmdmod.tab = 9999;
5053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055#ifdef FEAT_AUTOCMD
5056 --autocmd_no_enter;
5057#endif
5058 win_enter(firstwin, FALSE); /* back to first window */
5059#ifdef FEAT_AUTOCMD
5060 --autocmd_no_leave;
5061#endif
5062
5063 /*
5064 * Close superfluous windows.
5065 */
5066 for (wp = lastwin; open_wins > count; )
5067 {
5068 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5069 || autowrite(wp->w_buffer, FALSE) == OK);
5070#ifdef FEAT_AUTOCMD
5071 if (!win_valid(wp))
5072 {
5073 /* BufWrite Autocommands made the window invalid, start over */
5074 wp = lastwin;
5075 }
5076 else
5077#endif
5078 if (r)
5079 {
5080 win_close(wp, !P_HID(wp->w_buffer));
5081 --open_wins;
5082 wp = lastwin;
5083 }
5084 else
5085 {
5086 wp = wp->w_prev;
5087 if (wp == NULL)
5088 break;
5089 }
5090 }
5091}
5092# endif /* FEAT_LISTCMDS */
5093
5094#endif /* FEAT_WINDOWS */
5095
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005096static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098/*
5099 * do_modelines() - process mode lines for the current file
5100 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005101 * "flags" can be:
5102 * OPT_WINONLY only set options local to window
5103 * OPT_NOWIN don't set options local to window
5104 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 * Returns immediately if the "ml" option isn't set.
5106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005108do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005110 linenr_T lnum;
5111 int nmlines;
5112 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5115 return;
5116
5117 /* Disallow recursive entry here. Can happen when executing a modeline
5118 * triggers an autocommand, which reloads modelines with a ":do". */
5119 if (entered)
5120 return;
5121
5122 ++entered;
5123 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5124 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005125 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 nmlines = 0;
5127
5128 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5129 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005130 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 nmlines = 0;
5132 --entered;
5133}
5134
5135#include "version.h" /* for version number */
5136
5137/*
5138 * chk_modeline() - check a single line for a mode string
5139 * Return FAIL if an error encountered.
5140 */
5141 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005142chk_modeline(
5143 linenr_T lnum,
5144 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 char_u *s;
5147 char_u *e;
5148 char_u *linecopy; /* local copy of any modeline found */
5149 int prev;
5150 int vers;
5151 int end;
5152 int retval = OK;
5153 char_u *save_sourcing_name;
5154 linenr_T save_sourcing_lnum;
5155#ifdef FEAT_EVAL
5156 scid_T save_SID;
5157#endif
5158
5159 prev = -1;
5160 for (s = ml_get(lnum); *s != NUL; ++s)
5161 {
5162 if (prev == -1 || vim_isspace(prev))
5163 {
5164 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5165 || STRNCMP(s, "vi:", (size_t)3) == 0)
5166 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005167 /* Accept both "vim" and "Vim". */
5168 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
5170 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5171 e = s + 4;
5172 else
5173 e = s + 3;
5174 vers = getdigits(&e);
5175 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005176 && (s[0] != 'V'
5177 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 && (s[3] == ':'
5179 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5180 || (VIM_VERSION_100 < vers && s[3] == '<')
5181 || (VIM_VERSION_100 > vers && s[3] == '>')
5182 || (VIM_VERSION_100 == vers && s[3] == '=')))
5183 break;
5184 }
5185 }
5186 prev = *s;
5187 }
5188
5189 if (*s)
5190 {
5191 do /* skip over "ex:", "vi:" or "vim:" */
5192 ++s;
5193 while (s[-1] != ':');
5194
5195 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5196 if (linecopy == NULL)
5197 return FAIL;
5198
5199 save_sourcing_lnum = sourcing_lnum;
5200 save_sourcing_name = sourcing_name;
5201 sourcing_lnum = lnum; /* prepare for emsg() */
5202 sourcing_name = (char_u *)"modelines";
5203
5204 end = FALSE;
5205 while (end == FALSE)
5206 {
5207 s = skipwhite(s);
5208 if (*s == NUL)
5209 break;
5210
5211 /*
5212 * Find end of set command: ':' or end of line.
5213 * Skip over "\:", replacing it with ":".
5214 */
5215 for (e = s; *e != ':' && *e != NUL; ++e)
5216 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005217 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if (*e == NUL)
5219 end = TRUE;
5220
5221 /*
5222 * If there is a "set" command, require a terminating ':' and
5223 * ignore the stuff after the ':'.
5224 * "vi:set opt opt opt: foo" -- foo not interpreted
5225 * "vi:opt opt opt: foo" -- foo interpreted
5226 * Accept "se" for compatibility with Elvis.
5227 */
5228 if (STRNCMP(s, "set ", (size_t)4) == 0
5229 || STRNCMP(s, "se ", (size_t)3) == 0)
5230 {
5231 if (*e != ':') /* no terminating ':'? */
5232 break;
5233 end = TRUE;
5234 s = vim_strchr(s, ' ') + 1;
5235 }
5236 *e = NUL; /* truncate the set command */
5237
5238 if (*s != NUL) /* skip over an empty "::" */
5239 {
5240#ifdef FEAT_EVAL
5241 save_SID = current_SID;
5242 current_SID = SID_MODELINE;
5243#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005244 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#ifdef FEAT_EVAL
5246 current_SID = save_SID;
5247#endif
5248 if (retval == FAIL) /* stop if error found */
5249 break;
5250 }
5251 s = e + 1; /* advance to next part */
5252 }
5253
5254 sourcing_lnum = save_sourcing_lnum;
5255 sourcing_name = save_sourcing_name;
5256
5257 vim_free(linecopy);
5258 }
5259 return retval;
5260}
5261
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005262#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005264read_viminfo_bufferlist(
5265 vir_T *virp,
5266 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 char_u *tab;
5269 linenr_T lnum;
5270 colnr_T col;
5271 buf_T *buf;
5272 char_u *sfname;
5273 char_u *xline;
5274
5275 /* Handle long line and escaped characters. */
5276 xline = viminfo_readstring(virp, 1, FALSE);
5277
5278 /* don't read in if there are files on the command-line or if writing: */
5279 if (xline != NULL && !writing && ARGCOUNT == 0
5280 && find_viminfo_parameter('%') != NULL)
5281 {
5282 /* Format is: <fname> Tab <lnum> Tab <col>.
5283 * Watch out for a Tab in the file name, work from the end. */
5284 lnum = 0;
5285 col = 0;
5286 tab = vim_strrchr(xline, '\t');
5287 if (tab != NULL)
5288 {
5289 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005290 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 tab = vim_strrchr(xline, '\t');
5292 if (tab != NULL)
5293 {
5294 *tab++ = '\0';
5295 lnum = atol((char *)tab);
5296 }
5297 }
5298
5299 /* Expand "~/" in the file name at "line + 1" to a full path.
5300 * Then try shortening it by comparing with the current directory */
5301 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005302 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5305 if (buf != NULL) /* just in case... */
5306 {
5307 buf->b_last_cursor.lnum = lnum;
5308 buf->b_last_cursor.col = col;
5309 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5310 }
5311 }
5312 vim_free(xline);
5313
5314 return viminfo_readline(virp);
5315}
5316
5317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005318write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 buf_T *buf;
5321#ifdef FEAT_WINDOWS
5322 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005323 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324#endif
5325 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005326 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
5328 if (find_viminfo_parameter('%') == NULL)
5329 return;
5330
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005331 /* Without a number -1 is returned: do all buffers. */
5332 max_buffers = get_viminfo_parameter('%');
5333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005335#define LINE_BUF_LEN (MAXPATHL + 40)
5336 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (line == NULL)
5338 return;
5339
5340#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005341 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 set_last_cursor(win);
5343#else
5344 set_last_cursor(curwin);
5345#endif
5346
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005347 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5349 {
5350 if (buf->b_fname == NULL
5351 || !buf->b_p_bl
5352#ifdef FEAT_QUICKFIX
5353 || bt_quickfix(buf)
5354#endif
5355 || removable(buf->b_ffname))
5356 continue;
5357
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005358 if (max_buffers-- == 0)
5359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 putc('%', fp);
5361 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005362 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 (long)buf->b_last_cursor.lnum,
5364 buf->b_last_cursor.col);
5365 viminfo_writestring(fp, line);
5366 }
5367 vim_free(line);
5368}
5369#endif
5370
5371
5372/*
5373 * Return special buffer name.
5374 * Returns NULL when the buffer has a normal file name.
5375 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005376 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005377buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5380 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005381 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005382 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005383 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005384
5385 /*
5386 * For location list window, w_llist_ref points to the location list.
5387 * For quickfix window, w_llist_ref is NULL.
5388 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005389 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005390 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005391 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005392 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#endif
5395#ifdef FEAT_QUICKFIX
5396 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5397 * contains the name as specified by the user */
5398 if (bt_nofile(buf))
5399 {
5400 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005401 return buf->b_sfname;
5402 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404#endif
5405 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005406 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 return NULL;
5408}
5409
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005410#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5411 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5412 || defined(PROTO)
5413/*
5414 * Find a window for buffer "buf".
5415 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5416 * If not found FAIL is returned.
5417 */
5418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005419find_win_for_buf(
5420 buf_T *buf,
5421 win_T **wp,
5422 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005423{
5424 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5425 if ((*wp)->w_buffer == buf)
5426 goto win_found;
5427 return FAIL;
5428win_found:
5429 return OK;
5430}
5431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432
5433#if defined(FEAT_SIGNS) || defined(PROTO)
5434/*
5435 * Insert the sign into the signlist.
5436 */
5437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005438insert_sign(
5439 buf_T *buf, /* buffer to store sign in */
5440 signlist_T *prev, /* previous sign entry */
5441 signlist_T *next, /* next sign entry */
5442 int id, /* sign ID */
5443 linenr_T lnum, /* line number which gets the mark */
5444 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
5446 signlist_T *newsign;
5447
5448 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5449 if (newsign != NULL)
5450 {
5451 newsign->id = id;
5452 newsign->lnum = lnum;
5453 newsign->typenr = typenr;
5454 newsign->next = next;
5455#ifdef FEAT_NETBEANS_INTG
5456 newsign->prev = prev;
5457 if (next != NULL)
5458 next->prev = newsign;
5459#endif
5460
5461 if (prev == NULL)
5462 {
5463 /* When adding first sign need to redraw the windows to create the
5464 * column for signs. */
5465 if (buf->b_signlist == NULL)
5466 {
5467 redraw_buf_later(buf, NOT_VALID);
5468 changed_cline_bef_curs();
5469 }
5470
5471 /* first sign in signlist */
5472 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005473#ifdef FEAT_NETBEANS_INTG
5474 if (netbeans_active())
5475 buf->b_has_sign_column = TRUE;
5476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478 else
5479 prev->next = newsign;
5480 }
5481}
5482
5483/*
5484 * Add the sign into the signlist. Find the right spot to do it though.
5485 */
5486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005487buf_addsign(
5488 buf_T *buf, /* buffer to store sign in */
5489 int id, /* sign ID */
5490 linenr_T lnum, /* line number which gets the mark */
5491 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 signlist_T *sign; /* a sign in the signlist */
5494 signlist_T *prev; /* the previous sign */
5495
5496 prev = NULL;
5497 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5498 {
5499 if (lnum == sign->lnum && id == sign->id)
5500 {
5501 sign->typenr = typenr;
5502 return;
5503 }
5504 else if (
5505#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5506 id < 0 &&
5507#endif
5508 lnum < sign->lnum)
5509 {
5510#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5511 /* XXX - GRP: Is this because of sign slide problem? Or is it
5512 * really needed? Or is it because we allow multiple signs per
5513 * line? If so, should I add that feature to FEAT_SIGNS?
5514 */
5515 while (prev != NULL && prev->lnum == lnum)
5516 prev = prev->prev;
5517 if (prev == NULL)
5518 sign = buf->b_signlist;
5519 else
5520 sign = prev->next;
5521#endif
5522 insert_sign(buf, prev, sign, id, lnum, typenr);
5523 return;
5524 }
5525 prev = sign;
5526 }
5527#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5528 /* XXX - GRP: See previous comment */
5529 while (prev != NULL && prev->lnum == lnum)
5530 prev = prev->prev;
5531 if (prev == NULL)
5532 sign = buf->b_signlist;
5533 else
5534 sign = prev->next;
5535#endif
5536 insert_sign(buf, prev, sign, id, lnum, typenr);
5537
5538 return;
5539}
5540
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005541/*
5542 * For an existing, placed sign "markId" change the type to "typenr".
5543 * Returns the line number of the sign, or zero if the sign is not found.
5544 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005545 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005546buf_change_sign_type(
5547 buf_T *buf, /* buffer to store sign in */
5548 int markId, /* sign ID */
5549 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551 signlist_T *sign; /* a sign in the signlist */
5552
5553 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5554 {
5555 if (sign->id == markId)
5556 {
5557 sign->typenr = typenr;
5558 return sign->lnum;
5559 }
5560 }
5561
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005562 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563}
5564
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005565 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005566buf_getsigntype(
5567 buf_T *buf,
5568 linenr_T lnum,
5569 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570{
5571 signlist_T *sign; /* a sign in a b_signlist */
5572
5573 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5574 if (sign->lnum == lnum
5575 && (type == SIGN_ANY
5576# ifdef FEAT_SIGN_ICONS
5577 || (type == SIGN_ICON
5578 && sign_get_image(sign->typenr) != NULL)
5579# endif
5580 || (type == SIGN_TEXT
5581 && sign_get_text(sign->typenr) != NULL)
5582 || (type == SIGN_LINEHL
5583 && sign_get_attr(sign->typenr, TRUE) != 0)))
5584 return sign->typenr;
5585 return 0;
5586}
5587
5588
5589 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005590buf_delsign(
5591 buf_T *buf, /* buffer sign is stored in */
5592 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 signlist_T **lastp; /* pointer to pointer to current sign */
5595 signlist_T *sign; /* a sign in a b_signlist */
5596 signlist_T *next; /* the next sign in a b_signlist */
5597 linenr_T lnum; /* line number whose sign was deleted */
5598
5599 lastp = &buf->b_signlist;
5600 lnum = 0;
5601 for (sign = buf->b_signlist; sign != NULL; sign = next)
5602 {
5603 next = sign->next;
5604 if (sign->id == id)
5605 {
5606 *lastp = next;
5607#ifdef FEAT_NETBEANS_INTG
5608 if (next != NULL)
5609 next->prev = sign->prev;
5610#endif
5611 lnum = sign->lnum;
5612 vim_free(sign);
5613 break;
5614 }
5615 else
5616 lastp = &sign->next;
5617 }
5618
5619 /* When deleted the last sign need to redraw the windows to remove the
5620 * sign column. */
5621 if (buf->b_signlist == NULL)
5622 {
5623 redraw_buf_later(buf, NOT_VALID);
5624 changed_cline_bef_curs();
5625 }
5626
5627 return lnum;
5628}
5629
5630
5631/*
5632 * Find the line number of the sign with the requested id. If the sign does
5633 * not exist, return 0 as the line number. This will still let the correct file
5634 * get loaded.
5635 */
5636 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005637buf_findsign(
5638 buf_T *buf, /* buffer to store sign in */
5639 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 signlist_T *sign; /* a sign in the signlist */
5642
5643 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5644 if (sign->id == id)
5645 return sign->lnum;
5646
5647 return 0;
5648}
5649
5650 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005651buf_findsign_id(
5652 buf_T *buf, /* buffer whose sign we are searching for */
5653 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654{
5655 signlist_T *sign; /* a sign in the signlist */
5656
5657 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5658 if (sign->lnum == lnum)
5659 return sign->id;
5660
5661 return 0;
5662}
5663
5664
5665# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5666/* see if a given type of sign exists on a specific line */
5667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005668buf_findsigntype_id(
5669 buf_T *buf, /* buffer whose sign we are searching for */
5670 linenr_T lnum, /* line number of sign */
5671 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 signlist_T *sign; /* a sign in the signlist */
5674
5675 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5676 if (sign->lnum == lnum && sign->typenr == typenr)
5677 return sign->id;
5678
5679 return 0;
5680}
5681
5682
5683# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5684/* return the number of icons on the given line */
5685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005686buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 signlist_T *sign; /* a sign in the signlist */
5689 int count = 0;
5690
5691 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5692 if (sign->lnum == lnum)
5693 if (sign_get_image(sign->typenr) != NULL)
5694 count++;
5695
5696 return count;
5697}
5698# endif /* FEAT_SIGN_ICONS */
5699# endif /* FEAT_NETBEANS_INTG */
5700
5701
5702/*
5703 * Delete signs in buffer "buf".
5704 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 signlist_T *next;
5709
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005710 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005711 * sign column. Not when curwin is NULL (this means we're exiting). */
5712 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005713 {
5714 redraw_buf_later(buf, NOT_VALID);
5715 changed_cline_bef_curs();
5716 }
5717
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 while (buf->b_signlist != NULL)
5719 {
5720 next = buf->b_signlist->next;
5721 vim_free(buf->b_signlist);
5722 buf->b_signlist = next;
5723 }
5724}
5725
5726/*
5727 * Delete all signs in all buffers.
5728 */
5729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005730buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 buf_T *buf; /* buffer we are checking for signs */
5733
5734 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5735 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737}
5738
5739/*
5740 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5741 */
5742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005743sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
5745 buf_T *buf;
5746 signlist_T *p;
5747 char lbuf[BUFSIZ];
5748
5749 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5750 msg_putchar('\n');
5751 if (rbuf == NULL)
5752 buf = firstbuf;
5753 else
5754 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005755 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
5757 if (buf->b_signlist != NULL)
5758 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005759 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5761 msg_putchar('\n');
5762 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005763 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005765 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5767 MSG_PUTS(lbuf);
5768 msg_putchar('\n');
5769 }
5770 if (rbuf != NULL)
5771 break;
5772 buf = buf->b_next;
5773 }
5774}
5775
5776/*
5777 * Adjust a placed sign for inserted/deleted lines.
5778 */
5779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005780sign_mark_adjust(
5781 linenr_T line1,
5782 linenr_T line2,
5783 long amount,
5784 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786 signlist_T *sign; /* a sign in a b_signlist */
5787
5788 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5789 {
5790 if (sign->lnum >= line1 && sign->lnum <= line2)
5791 {
5792 if (amount == MAXLNUM)
5793 sign->lnum = line1;
5794 else
5795 sign->lnum += amount;
5796 }
5797 else if (sign->lnum > line2)
5798 sign->lnum += amount_after;
5799 }
5800}
5801#endif /* FEAT_SIGNS */
5802
5803/*
5804 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5805 */
5806 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005807set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
5809 if (on != curbuf->b_p_bl)
5810 {
5811 curbuf->b_p_bl = on;
5812#ifdef FEAT_AUTOCMD
5813 if (on)
5814 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5815 else
5816 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5817#endif
5818 }
5819}
5820
5821/*
5822 * Read the file for "buf" again and check if the contents changed.
5823 * Return TRUE if it changed or this could not be checked.
5824 */
5825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005826buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828 buf_T *newbuf;
5829 int differ = TRUE;
5830 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 exarg_T ea;
5833
5834 /* Allocate a buffer without putting it in the buffer list. */
5835 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5836 if (newbuf == NULL)
5837 return TRUE;
5838
5839 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5840 if (prep_exarg(&ea, buf) == FAIL)
5841 {
5842 wipe_buffer(newbuf, FALSE);
5843 return TRUE;
5844 }
5845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 /* set curwin/curbuf to buf and save a few things */
5847 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
Bram Moolenaar4770d092006-01-12 23:22:24 +00005849 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 && readfile(buf->b_ffname, buf->b_fname,
5851 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5852 &ea, READ_NEW | READ_DUMMY) == OK)
5853 {
5854 /* compare the two files line by line */
5855 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5856 {
5857 differ = FALSE;
5858 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5859 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5860 {
5861 differ = TRUE;
5862 break;
5863 }
5864 }
5865 }
5866 vim_free(ea.cmd);
5867
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 /* restore curwin/curbuf and a few other things */
5869 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
5871 if (curbuf != newbuf) /* safety check */
5872 wipe_buffer(newbuf, FALSE);
5873
5874 return differ;
5875}
5876
5877/*
5878 * Wipe out a buffer and decrement the last buffer number if it was used for
5879 * this buffer. Call this to wipe out a temp buffer that does not contain any
5880 * marks.
5881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005883wipe_buffer(
5884 buf_T *buf,
5885 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 if (buf->b_fnum == top_file_num - 1)
5888 --top_file_num;
5889
5890#ifdef FEAT_AUTOCMD
5891 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005892 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005894 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#ifdef FEAT_AUTOCMD
5896 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005897 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898#endif
5899}