blob: 0bbb1f467a5a45ab3ec01dbea089eff47d1d7b3c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
36static wininfo_T *find_wininfo(buf_T *buf, int skip_diff_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static buf_T *buflist_findname_stat(char_u *ffname, struct stat *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, struct stat *stp);
40static int buf_same_ino(buf_T *buf, struct stat *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
48static void free_buffer(buf_T *);
49static void free_buffer_stuff(buf_T *buf, int free_options);
50static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar7fd73202010-07-25 16:58:46 +020062#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
63static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066#ifdef FEAT_AUTOCMD
67static char *e_auabort = N_("E855: Autocommands caused command to abort");
68#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram Moolenaar071d4272004-06-13 20:20:40 +000070/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020071 * Open current buffer, that is: open the memfile and read the file into
72 * memory.
73 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
75 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010076open_buffer(
77 int read_stdin, /* read file from stdin */
78 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
79 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080{
81 int retval = OK;
82#ifdef FEAT_AUTOCMD
83 buf_T *old_curbuf;
84#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +010085#ifdef FEAT_SYN_HL
86 long old_tw = curbuf->b_p_tw;
87#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89 /*
90 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
91 * When re-entering the same buffer, it should not change, because the
92 * user may have reset the flag by hand.
93 */
94 if (readonlymode && curbuf->b_ffname != NULL
95 && (curbuf->b_flags & BF_NEVERLOADED))
96 curbuf->b_p_ro = TRUE;
97
Bram Moolenaar4770d092006-01-12 23:22:24 +000098 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 {
100 /*
101 * There MUST be a memfile, otherwise we can't do anything
102 * If we can't create one for the current buffer, take another buffer
103 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100104 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
106 if (curbuf->b_ml.ml_mfp != NULL)
107 break;
108 /*
109 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000110 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 */
112 if (curbuf == NULL)
113 {
114 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
115 getout(2);
116 }
117 EMSG(_("E83: Cannot allocate buffer, using other one..."));
118 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100119#ifdef FEAT_SYN_HL
120 if (old_tw != curbuf->b_p_tw)
121 check_colorcolumn(curwin);
122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 return FAIL;
124 }
125
126#ifdef FEAT_AUTOCMD
127 /* The autocommands in readfile() may change the buffer, but only AFTER
128 * reading the file. */
129 old_curbuf = curbuf;
130 modified_was_set = FALSE;
131#endif
132
133 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100134 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136 if (curbuf->b_ffname != NULL
137#ifdef FEAT_NETBEANS_INTG
138 && netbeansReadFile
139#endif
140 )
141 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100142 int old_msg_silent = msg_silent;
143
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_NETBEANS_INTG
145 int oldFire = netbeansFireChanges;
146
147 netbeansFireChanges = 0;
148#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100149 if (shortmess(SHM_FILEINFO))
150 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200152 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
153 flags | READ_NEW);
Bram Moolenaar426dd022016-03-15 15:09:29 +0100154 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#ifdef FEAT_NETBEANS_INTG
156 netbeansFireChanges = oldFire;
157#endif
158 /* Help buffer is filtered. */
159 if (curbuf->b_help)
160 fix_help_buffer();
161 }
162 else if (read_stdin)
163 {
164 int save_bin = curbuf->b_p_bin;
165 linenr_T line_count;
166
167 /*
168 * First read the text in binary mode into the buffer.
169 * Then read from that same buffer and append at the end. This makes
170 * it possible to retry when 'fileformat' or 'fileencoding' was
171 * guessed wrong.
172 */
173 curbuf->b_p_bin = TRUE;
174 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200175 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
176 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 curbuf->b_p_bin = save_bin;
178 if (retval == OK)
179 {
180 line_count = curbuf->b_ml.ml_line_count;
181 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200182 (linenr_T)0, (linenr_T)MAXLNUM, eap,
183 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 if (retval == OK)
185 {
186 /* Delete the binary lines. */
187 while (--line_count >= 0)
188 ml_delete((linenr_T)1, FALSE);
189 }
190 else
191 {
192 /* Delete the converted lines. */
193 while (curbuf->b_ml.ml_line_count > line_count)
194 ml_delete(line_count, FALSE);
195 }
196 /* Put the cursor on the first line. */
197 curwin->w_cursor.lnum = 1;
198 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000199
200 /* Set or reset 'modified' before executing autocommands, so that
201 * it can be changed there. */
202 if (!readonlymode && !bufempty())
203 changed();
204 else if (retval != FAIL)
205 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifdef FEAT_AUTOCMD
207# ifdef FEAT_EVAL
208 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
209 curbuf, &retval);
210# else
211 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
212# endif
213#endif
214 }
215 }
216
217 /* if first time loading this buffer, init b_chartab[] */
218 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100221#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100222 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100223#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
226 /*
227 * Set/reset the Changed flag first, autocmds may change the buffer.
228 * Apply the automatic commands, before processing the modelines.
229 * So the modelines have priority over auto commands.
230 */
231 /* When reading stdin, the buffer contents always needs writing, so set
232 * the changed flag. Unless in readonly mode: "ls | gview -".
233 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000234 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#ifdef FEAT_AUTOCMD
236 || modified_was_set /* ":set modified" used in autocmd */
237# ifdef FEAT_EVAL
238 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
239# endif
240#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000241 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000243 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 unchanged(curbuf, FALSE);
245 save_file_ff(curbuf); /* keep this fileformat */
246
247 /* require "!" to overwrite the file, because it wasn't read completely */
248#ifdef FEAT_EVAL
249 if (aborting())
250#else
251 if (got_int)
252#endif
253 curbuf->b_flags |= BF_READERR;
254
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000255#ifdef FEAT_FOLDING
256 /* Need to update automatic folding. Do this before the autocommands,
257 * they may use the fold info. */
258 foldUpdateAll(curwin);
259#endif
260
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#ifdef FEAT_AUTOCMD
262 /* need to set w_topline, unless some autocommand already did that. */
263 if (!(curwin->w_valid & VALID_TOPLINE))
264 {
265 curwin->w_topline = 1;
266# ifdef FEAT_DIFF
267 curwin->w_topfill = 0;
268# endif
269 }
270# ifdef FEAT_EVAL
271 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
272# else
273 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
274# endif
275#endif
276
277 if (retval != FAIL)
278 {
279#ifdef FEAT_AUTOCMD
280 /*
281 * The autocommands may have changed the current buffer. Apply the
282 * modelines to the correct buffer, if it still exists and is loaded.
283 */
284 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
285 {
286 aco_save_T aco;
287
288 /* Go to the buffer that was opened. */
289 aucmd_prepbuf(&aco, old_curbuf);
290#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000291 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
293
294#ifdef FEAT_AUTOCMD
295# ifdef FEAT_EVAL
296 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
297 &retval);
298# else
299 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
300# endif
301
302 /* restore curwin/curbuf and a few other things */
303 aucmd_restbuf(&aco);
304 }
305#endif
306 }
307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 return retval;
309}
310
311/*
312 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
313 */
314 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100315buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316{
317 buf_T *bp;
318
319 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
320 if (bp == buf)
321 return TRUE;
322 return FALSE;
323}
324
325/*
326 * Close the link to a buffer.
327 * "action" is used when there is no longer a window for the buffer.
328 * It can be:
329 * 0 buffer becomes hidden
330 * DOBUF_UNLOAD buffer is unloaded
331 * DOBUF_DELETE buffer is unloaded and removed from buffer list
332 * DOBUF_WIPE buffer is unloaded and really deleted
333 * When doing all but the first one on the current buffer, the caller should
334 * get a new buffer very soon!
335 *
336 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100337 *
338 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
339 * cause there to be only one window with this buffer. e.g. when ":quit" is
340 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 */
342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100343close_buffer(
344 win_T *win, /* if not NULL, set b_last_cursor */
345 buf_T *buf,
346 int action,
347 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348{
349#ifdef FEAT_AUTOCMD
350 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100351 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#endif
353 int unload_buf = (action != 0);
354 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
355 int wipe_buf = (action == DOBUF_WIPE);
356
357#ifdef FEAT_QUICKFIX
358 /*
359 * Force unloading or deleting when 'bufhidden' says so.
360 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
361 * "hide" (otherwise we could never free or delete a buffer).
362 */
363 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
364 {
365 del_buf = TRUE;
366 unload_buf = TRUE;
367 }
368 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
369 {
370 del_buf = TRUE;
371 unload_buf = TRUE;
372 wipe_buf = TRUE;
373 }
374 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
375 unload_buf = TRUE;
376#endif
377
Bram Moolenaar3be85852014-06-12 14:01:31 +0200378 if (win != NULL
379#ifdef FEAT_WINDOWS
380 && win_valid(win) /* in case autocommands closed the window */
381#endif
382 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 /* Set b_last_cursor when closing the last window for the buffer.
385 * Remember the last cursor position and window options of the buffer.
386 * This used to be only for the current window, but then options like
387 * 'foldmethod' may be lost with a ":only" command. */
388 if (buf->b_nwindows == 1)
389 set_last_cursor(win);
390 buflist_setfpos(buf, win,
391 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
392 win->w_cursor.col, TRUE);
393 }
394
395#ifdef FEAT_AUTOCMD
396 /* When the buffer is no longer in a window, trigger BufWinLeave */
397 if (buf->b_nwindows == 1)
398 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200399 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
401 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200402 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100403 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200404 /* Autocommands deleted the buffer. */
405aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100406 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100408 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200409 buf->b_closing = FALSE;
410 if (abort_if_last && one_window())
411 /* Autocommands made this the only window. */
412 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 /* When the buffer becomes hidden, but is not unloaded, trigger
415 * BufHidden */
416 if (!unload_buf)
417 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200418 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
420 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200421 if (!buf_valid(buf))
422 /* Autocommands deleted the buffer. */
423 goto aucmd_abort;
424 buf->b_closing = FALSE;
425 if (abort_if_last && one_window())
426 /* Autocommands made this the only window. */
427 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 }
429# ifdef FEAT_EVAL
430 if (aborting()) /* autocmds may abort script processing */
431 return;
432# endif
433 }
434 nwindows = buf->b_nwindows;
435#endif
436
437 /* decrease the link count from windows (unless not in any window) */
438 if (buf->b_nwindows > 0)
439 --buf->b_nwindows;
440
441 /* Return when a window is displaying the buffer or when it's not
442 * unloaded. */
443 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445
446 /* Always remove the buffer when there is no file name. */
447 if (buf->b_ffname == NULL)
448 del_buf = TRUE;
449
450 /*
451 * Free all things allocated for this buffer.
452 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
453 */
454#ifdef FEAT_AUTOCMD
455 /* Remember if we are closing the current buffer. Restore the number of
456 * windows, so that autocommands in buf_freeall() don't get confused. */
457 is_curbuf = (buf == curbuf);
458 buf->b_nwindows = nwindows;
459#endif
460
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200461 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200462 if (
463#ifdef FEAT_WINDOWS
464 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100465#else
466 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200467#endif
468 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200469 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471#ifdef FEAT_AUTOCMD
472 /* Autocommands may have deleted the buffer. */
473 if (!buf_valid(buf))
474 return;
475# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000476 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 return;
478# endif
479
480 /* Autocommands may have opened or closed windows for this buffer.
481 * Decrement the count for the close we do here. */
482 if (buf->b_nwindows > 0)
483 --buf->b_nwindows;
484
485 /*
486 * It's possible that autocommands change curbuf to the one being deleted.
487 * This might cause the previous curbuf to be deleted unexpectedly. But
488 * in some cases it's OK to delete the curbuf, because a new one is
489 * obtained anyway. Therefore only return if curbuf changed to the
490 * deleted buffer.
491 */
492 if (buf == curbuf && !is_curbuf)
493 return;
494#endif
495
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000496 /* Change directories when the 'acd' option is set. */
497 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498
499 /*
500 * Remove the buffer from the list.
501 */
502 if (wipe_buf)
503 {
504#ifdef FEAT_SUN_WORKSHOP
505 if (usingSunWorkShop)
506 workshop_file_closed_lineno((char *)buf->b_ffname,
507 (int)buf->b_last_cursor.lnum);
508#endif
509 vim_free(buf->b_ffname);
510 vim_free(buf->b_sfname);
511 if (buf->b_prev == NULL)
512 firstbuf = buf->b_next;
513 else
514 buf->b_prev->b_next = buf->b_next;
515 if (buf->b_next == NULL)
516 lastbuf = buf->b_prev;
517 else
518 buf->b_next->b_prev = buf->b_prev;
519 free_buffer(buf);
520 }
521 else
522 {
523 if (del_buf)
524 {
525 /* Free all internal variables and reset option values, to make
526 * ":bdel" compatible with Vim 5.7. */
527 free_buffer_stuff(buf, TRUE);
528
529 /* Make it look like a new buffer. */
530 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
531
532 /* Init the options when loaded again. */
533 buf->b_p_initialized = FALSE;
534 }
535 buf_clear_file(buf);
536 if (del_buf)
537 buf->b_p_bl = FALSE;
538 }
539}
540
541/*
542 * Make buffer not contain a file.
543 */
544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100545buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 buf->b_ml.ml_line_count = 1;
548 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 buf->b_p_eol = TRUE;
551 buf->b_start_eol = TRUE;
552#ifdef FEAT_MBYTE
553 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000554 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555#endif
556 buf->b_ml.ml_mfp = NULL;
557 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
558#ifdef FEAT_NETBEANS_INTG
559 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
561}
562
563/*
564 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200565 * the file. flags:
566 * BFA_DEL buffer is going to be deleted
567 * BFA_WIPE buffer is going to be wiped out
568 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100571buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572{
573#ifdef FEAT_AUTOCMD
574 int is_curbuf = (buf == curbuf);
575
Bram Moolenaar362ce482012-06-06 19:02:45 +0200576 buf->b_closing = TRUE;
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200577 if (buf->b_ml.ml_mfp != NULL)
578 {
579 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
580 if (!buf_valid(buf)) /* autocommands may delete the buffer */
581 return;
582 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200583 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {
585 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
586 if (!buf_valid(buf)) /* autocommands may delete the buffer */
587 return;
588 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200589 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {
591 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
592 FALSE, buf);
593 if (!buf_valid(buf)) /* autocommands may delete the buffer */
594 return;
595 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200596 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000598 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 return;
600# endif
601
602 /*
603 * It's possible that autocommands change curbuf to the one being deleted.
604 * This might cause curbuf to be deleted unexpectedly. But in some cases
605 * it's OK to delete the curbuf, because a new one is obtained anyway.
606 * Therefore only return if curbuf changed to the deleted buffer.
607 */
608 if (buf == curbuf && !is_curbuf)
609 return;
610#endif
611#ifdef FEAT_DIFF
612 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
613#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200614#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100615 /* Remove any ownsyntax, unless exiting. */
616 if (firstwin != NULL && curwin->w_buffer == buf)
617 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200618#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000619
620#ifdef FEAT_FOLDING
621 /* No folds in an empty buffer. */
622# ifdef FEAT_WINDOWS
623 {
624 win_T *win;
625 tabpage_T *tp;
626
627 FOR_ALL_TAB_WINDOWS(tp, win)
628 if (win->w_buffer == buf)
629 clearFolding(win);
630 }
631# else
632 if (curwin->w_buffer == buf)
633 clearFolding(curwin);
634# endif
635#endif
636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637#ifdef FEAT_TCL
638 tcl_buffer_free(buf);
639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 ml_close(buf, TRUE); /* close and delete the memline/memfile */
641 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200642 if ((flags & BFA_KEEP_UNDO) == 0)
643 {
644 u_blockfree(buf); /* free the memory allocated for undo */
645 u_clearall(buf); /* reset all undo information */
646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200648 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000650 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651}
652
653/*
654 * Free a buffer structure and the things it contains related to the buffer
655 * itself (not the file, that must have been done already).
656 */
657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100658free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200661#ifdef FEAT_EVAL
662 unref_var_dict(buf->b_vars);
663#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200664#ifdef FEAT_LUA
665 lua_buffer_free(buf);
666#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000667#ifdef FEAT_MZSCHEME
668 mzscheme_buffer_free(buf);
669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#ifdef FEAT_PERL
671 perl_buf_free(buf);
672#endif
673#ifdef FEAT_PYTHON
674 python_buffer_free(buf);
675#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200676#ifdef FEAT_PYTHON3
677 python3_buffer_free(buf);
678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#ifdef FEAT_RUBY
680 ruby_buffer_free(buf);
681#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200682#ifdef FEAT_JOB_CHANNEL
683 channel_buffer_free(buf);
684#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000685#ifdef FEAT_AUTOCMD
686 aubuflocal_remove(buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200687 if (autocmd_busy)
688 {
689 /* Do not free the buffer structure while autocommands are executing,
690 * it's still needed. Free it when autocmd_busy is reset. */
691 buf->b_next = au_pending_free_buf;
692 au_pending_free_buf = buf;
693 }
694 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000695#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200696 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
699/*
700 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
701 */
702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703free_buffer_stuff(
704 buf_T *buf,
705 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
707 if (free_options)
708 {
709 clear_wininfo(buf); /* including window-local options */
710 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200711#ifdef FEAT_SPELL
712 ga_clear(&buf->b_s.b_langp);
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 }
715#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200716 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
717 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#endif
719#ifdef FEAT_USR_CMDS
720 uc_clear(&buf->b_ucmds); /* clear local user commands */
721#endif
722#ifdef FEAT_SIGNS
723 buf_delete_signs(buf); /* delete any signs */
724#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000725#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200726 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#ifdef FEAT_LOCALMAP
729 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
730 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
731#endif
732#ifdef FEAT_MBYTE
733 vim_free(buf->b_start_fenc);
734 buf->b_start_fenc = NULL;
735#endif
736}
737
738/*
739 * Free the b_wininfo list for buffer "buf".
740 */
741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100742clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 wininfo_T *wip;
745
746 while (buf->b_wininfo != NULL)
747 {
748 wip = buf->b_wininfo;
749 buf->b_wininfo = wip->wi_next;
750 if (wip->wi_optset)
751 {
752 clear_winopt(&wip->wi_opt);
753#ifdef FEAT_FOLDING
754 deleteFoldRecurse(&wip->wi_folds);
755#endif
756 }
757 vim_free(wip);
758 }
759}
760
761#if defined(FEAT_LISTCMDS) || defined(PROTO)
762/*
763 * Go to another buffer. Handles the result of the ATTENTION dialog.
764 */
765 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100766goto_buffer(
767 exarg_T *eap,
768 int start,
769 int dir,
770 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000772# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 buf_T *old_curbuf = curbuf;
774
775 swap_exists_action = SEA_DIALOG;
776# endif
777 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
778 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000779# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
781 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000782# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
783 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000784
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000785 /* Reset the error/interrupt/exception state here so that
786 * aborting() returns FALSE when closing a window. */
787 enter_cleanup(&cs);
788# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000789
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000790 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 win_close(curwin, TRUE);
792 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000793 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000794
795# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
796 /* Restore the error/interrupt/exception state if not discarded by a
797 * new aborting error, interrupt, or uncaught exception. */
798 leave_cleanup(&cs);
799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801 else
802 handle_swap_exists(old_curbuf);
803# endif
804}
805#endif
806
Bram Moolenaare64ac772005-12-07 20:54:59 +0000807#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
809 * Handle the situation of swap_exists_action being set.
810 * It is allowed for "old_curbuf" to be NULL or invalid.
811 */
812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813handle_swap_exists(buf_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000815# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
816 cleanup_T cs;
817# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100818#ifdef FEAT_SYN_HL
819 long old_tw = curbuf->b_p_tw;
820#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (swap_exists_action == SEA_QUIT)
823 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000824# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
825 /* Reset the error/interrupt/exception state here so that
826 * aborting() returns FALSE when closing a buffer. */
827 enter_cleanup(&cs);
828# endif
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 /* User selected Quit at ATTENTION prompt. Go back to previous
831 * buffer. If that buffer is gone or the same as the current one,
832 * open a new, empty buffer. */
833 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000834 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100835 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000837 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100841#ifdef FEAT_SYN_HL
842 if (old_tw != curbuf->b_p_tw)
843 check_colorcolumn(curwin);
844#endif
845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000847
848# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
849 /* Restore the error/interrupt/exception state if not discarded by a
850 * new aborting error, interrupt, or uncaught exception. */
851 leave_cleanup(&cs);
852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 else if (swap_exists_action == SEA_RECOVER)
855 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000856# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
857 /* Reset the error/interrupt/exception state here so that
858 * aborting() returns FALSE when closing a buffer. */
859 enter_cleanup(&cs);
860# endif
861
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 /* User selected Recover at ATTENTION prompt. */
863 msg_scroll = TRUE;
864 ml_recover();
865 MSG_PUTS("\n"); /* don't overwrite the last message */
866 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000867 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000868
869# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
870 /* Restore the error/interrupt/exception state if not discarded by a
871 * new aborting error, interrupt, or uncaught exception. */
872 leave_cleanup(&cs);
873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875 swap_exists_action = SEA_NONE;
876}
877#endif
878
879#if defined(FEAT_LISTCMDS) || defined(PROTO)
880/*
881 * do_bufdel() - delete or unload buffer(s)
882 *
883 * addr_count == 0: ":bdel" - delete current buffer
884 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
885 * buffer "end_bnr", then any other arguments.
886 * addr_count == 2: ":N,N bdel" - delete buffers in range
887 *
888 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
889 * DOBUF_DEL (":bdel")
890 *
891 * Returns error message or NULL
892 */
893 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100894do_bufdel(
895 int command,
896 char_u *arg, /* pointer to extra arguments */
897 int addr_count,
898 int start_bnr, /* first buffer number in a range */
899 int end_bnr, /* buffer nr or last buffer nr in a range */
900 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901{
902 int do_current = 0; /* delete current buffer? */
903 int deleted = 0; /* number of buffers deleted */
904 char_u *errormsg = NULL; /* return value */
905 int bnr; /* buffer number */
906 char_u *p;
907
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (addr_count == 0)
909 {
910 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
911 }
912 else
913 {
914 if (addr_count == 2)
915 {
916 if (*arg) /* both range and argument is not allowed */
917 return (char_u *)_(e_trailing);
918 bnr = start_bnr;
919 }
920 else /* addr_count == 1 */
921 bnr = end_bnr;
922
923 for ( ;!got_int; ui_breakcheck())
924 {
925 /*
926 * delete the current buffer last, otherwise when the
927 * current buffer is deleted, the next buffer becomes
928 * the current one and will be loaded, which may then
929 * also be deleted, etc.
930 */
931 if (bnr == curbuf->b_fnum)
932 do_current = bnr;
933 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
934 forceit) == OK)
935 ++deleted;
936
937 /*
938 * find next buffer number to delete/unload
939 */
940 if (addr_count == 2)
941 {
942 if (++bnr > end_bnr)
943 break;
944 }
945 else /* addr_count == 1 */
946 {
947 arg = skipwhite(arg);
948 if (*arg == NUL)
949 break;
950 if (!VIM_ISDIGIT(*arg))
951 {
952 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100953 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
954 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (bnr < 0) /* failed */
956 break;
957 arg = p;
958 }
959 else
960 bnr = getdigits(&arg);
961 }
962 }
963 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
964 FORWARD, do_current, forceit) == OK)
965 ++deleted;
966
967 if (deleted == 0)
968 {
969 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000970 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000972 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000974 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 errormsg = IObuff;
976 }
977 else if (deleted >= p_report)
978 {
979 if (command == DOBUF_UNLOAD)
980 {
981 if (deleted == 1)
982 MSG(_("1 buffer unloaded"));
983 else
984 smsg((char_u *)_("%d buffers unloaded"), deleted);
985 }
986 else if (command == DOBUF_DEL)
987 {
988 if (deleted == 1)
989 MSG(_("1 buffer deleted"));
990 else
991 smsg((char_u *)_("%d buffers deleted"), deleted);
992 }
993 else
994 {
995 if (deleted == 1)
996 MSG(_("1 buffer wiped out"));
997 else
998 smsg((char_u *)_("%d buffers wiped out"), deleted);
999 }
1000 }
1001 }
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 return errormsg;
1005}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001006#endif /* FEAT_LISTCMDS */
1007
1008#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1009 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001011static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001012
1013/*
1014 * Make the current buffer empty.
1015 * Used when it is wiped out and it's the last buffer.
1016 */
1017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001018empty_curbuf(
1019 int close_others,
1020 int forceit,
1021 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001022{
1023 int retval;
1024 buf_T *buf = curbuf;
1025
1026 if (action == DOBUF_UNLOAD)
1027 {
1028 EMSG(_("E90: Cannot unload last buffer"));
1029 return FAIL;
1030 }
1031
1032 if (close_others)
1033 {
1034 /* Close any other windows on this buffer, then make it empty. */
1035#ifdef FEAT_WINDOWS
1036 close_windows(buf, TRUE);
1037#endif
1038 }
1039
1040 setpcmark();
1041 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1042 forceit ? ECMD_FORCEIT : 0, curwin);
1043
1044 /*
1045 * do_ecmd() may create a new buffer, then we have to delete
1046 * the old one. But do_ecmd() may have done that already, check
1047 * if the buffer still exists.
1048 */
1049 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1050 close_buffer(NULL, buf, action, FALSE);
1051 if (!close_others)
1052 need_fileinfo = FALSE;
1053 return retval;
1054}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055/*
1056 * Implementation of the commands for the buffer list.
1057 *
1058 * action == DOBUF_GOTO go to specified buffer
1059 * action == DOBUF_SPLIT split window and go to specified buffer
1060 * action == DOBUF_UNLOAD unload specified buffer(s)
1061 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1062 * action == DOBUF_WIPE delete specified buffer(s) really
1063 *
1064 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1065 * start == DOBUF_FIRST go to "count" buffer from first buffer
1066 * start == DOBUF_LAST go to "count" buffer from last buffer
1067 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1068 *
1069 * Return FAIL or OK.
1070 */
1071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001072do_buffer(
1073 int action,
1074 int start,
1075 int dir, /* FORWARD or BACKWARD */
1076 int count, /* buffer number or number of buffers */
1077 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
1079 buf_T *buf;
1080 buf_T *bp;
1081 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1082 || action == DOBUF_WIPE);
1083
1084 switch (start)
1085 {
1086 case DOBUF_FIRST: buf = firstbuf; break;
1087 case DOBUF_LAST: buf = lastbuf; break;
1088 default: buf = curbuf; break;
1089 }
1090 if (start == DOBUF_MOD) /* find next modified buffer */
1091 {
1092 while (count-- > 0)
1093 {
1094 do
1095 {
1096 buf = buf->b_next;
1097 if (buf == NULL)
1098 buf = firstbuf;
1099 }
1100 while (buf != curbuf && !bufIsChanged(buf));
1101 }
1102 if (!bufIsChanged(buf))
1103 {
1104 EMSG(_("E84: No modified buffer found"));
1105 return FAIL;
1106 }
1107 }
1108 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1109 {
1110 while (buf != NULL && buf->b_fnum != count)
1111 buf = buf->b_next;
1112 }
1113 else
1114 {
1115 bp = NULL;
1116 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1117 {
1118 /* remember the buffer where we start, we come back there when all
1119 * buffers are unlisted. */
1120 if (bp == NULL)
1121 bp = buf;
1122 if (dir == FORWARD)
1123 {
1124 buf = buf->b_next;
1125 if (buf == NULL)
1126 buf = firstbuf;
1127 }
1128 else
1129 {
1130 buf = buf->b_prev;
1131 if (buf == NULL)
1132 buf = lastbuf;
1133 }
1134 /* don't count unlisted buffers */
1135 if (unload || buf->b_p_bl)
1136 {
1137 --count;
1138 bp = NULL; /* use this buffer as new starting point */
1139 }
1140 if (bp == buf)
1141 {
1142 /* back where we started, didn't find anything. */
1143 EMSG(_("E85: There is no listed buffer"));
1144 return FAIL;
1145 }
1146 }
1147 }
1148
1149 if (buf == NULL) /* could not find it */
1150 {
1151 if (start == DOBUF_FIRST)
1152 {
1153 /* don't warn when deleting */
1154 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001155 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157 else if (dir == FORWARD)
1158 EMSG(_("E87: Cannot go beyond last buffer"));
1159 else
1160 EMSG(_("E88: Cannot go before first buffer"));
1161 return FAIL;
1162 }
1163
1164#ifdef FEAT_GUI
1165 need_mouse_correct = TRUE;
1166#endif
1167
1168#ifdef FEAT_LISTCMDS
1169 /*
1170 * delete buffer buf from memory and/or the list
1171 */
1172 if (unload)
1173 {
1174 int forward;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176 /* When unloading or deleting a buffer that's already unloaded and
1177 * unlisted: fail silently. */
1178 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1179 return FAIL;
1180
1181 if (!forceit && bufIsChanged(buf))
1182 {
1183#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1184 if ((p_confirm || cmdmod.confirm) && p_write)
1185 {
1186 dialog_changed(buf, FALSE);
1187# ifdef FEAT_AUTOCMD
1188 if (!buf_valid(buf))
1189 /* Autocommand deleted buffer, oops! It's not changed
1190 * now. */
1191 return FAIL;
1192# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001193 /* If it's still changed fail silently, the dialog already
1194 * mentioned why it fails. */
1195 if (bufIsChanged(buf))
1196 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#endif
1200 {
1201 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1202 buf->b_fnum);
1203 return FAIL;
1204 }
1205 }
1206
1207 /*
1208 * If deleting the last (listed) buffer, make it empty.
1209 * The last (listed) buffer cannot be unloaded.
1210 */
1211 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1212 if (bp->b_p_bl && bp != buf)
1213 break;
1214 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001215 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
1217#ifdef FEAT_WINDOWS
1218 /*
1219 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001220 * (unless it's the only window). Repeat this so long as we end up in
1221 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001223 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001224# ifdef FEAT_AUTOCMD
1225 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1226# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001227 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001228 {
1229 if (win_close(curwin, FALSE) == FAIL)
1230 break;
1231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#endif
1233
1234 /*
1235 * If the buffer to be deleted is not the current one, delete it here.
1236 */
1237 if (buf != curbuf)
1238 {
1239#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001240 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241#endif
1242 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001243 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 return OK;
1245 }
1246
1247 /*
1248 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001249 * There should be another, otherwise it would have been handled
1250 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 * First use au_new_curbuf, if it is valid.
1252 * Then prefer the buffer we most recently visited.
1253 * Else try to find one that is loaded, after the current buffer,
1254 * then before the current buffer.
1255 * Finally use any buffer.
1256 */
1257 buf = NULL; /* selected buffer */
1258 bp = NULL; /* used when no loaded buffer found */
1259#ifdef FEAT_AUTOCMD
1260 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1261 buf = au_new_curbuf;
1262# ifdef FEAT_JUMPLIST
1263 else
1264# endif
1265#endif
1266#ifdef FEAT_JUMPLIST
1267 if (curwin->w_jumplistlen > 0)
1268 {
1269 int jumpidx;
1270
1271 jumpidx = curwin->w_jumplistidx - 1;
1272 if (jumpidx < 0)
1273 jumpidx = curwin->w_jumplistlen - 1;
1274
1275 forward = jumpidx;
1276 while (jumpidx != curwin->w_jumplistidx)
1277 {
1278 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1279 if (buf != NULL)
1280 {
1281 if (buf == curbuf || !buf->b_p_bl)
1282 buf = NULL; /* skip current and unlisted bufs */
1283 else if (buf->b_ml.ml_mfp == NULL)
1284 {
1285 /* skip unloaded buf, but may keep it for later */
1286 if (bp == NULL)
1287 bp = buf;
1288 buf = NULL;
1289 }
1290 }
1291 if (buf != NULL) /* found a valid buffer: stop searching */
1292 break;
1293 /* advance to older entry in jump list */
1294 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1295 break;
1296 if (--jumpidx < 0)
1297 jumpidx = curwin->w_jumplistlen - 1;
1298 if (jumpidx == forward) /* List exhausted for sure */
1299 break;
1300 }
1301 }
1302#endif
1303
1304 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1305 {
1306 forward = TRUE;
1307 buf = curbuf->b_next;
1308 for (;;)
1309 {
1310 if (buf == NULL)
1311 {
1312 if (!forward) /* tried both directions */
1313 break;
1314 buf = curbuf->b_prev;
1315 forward = FALSE;
1316 continue;
1317 }
1318 /* in non-help buffer, try to skip help buffers, and vv */
1319 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1320 {
1321 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1322 break;
1323 if (bp == NULL) /* remember unloaded buf for later */
1324 bp = buf;
1325 }
1326 if (forward)
1327 buf = buf->b_next;
1328 else
1329 buf = buf->b_prev;
1330 }
1331 }
1332 if (buf == NULL) /* No loaded buffer, use unloaded one */
1333 buf = bp;
1334 if (buf == NULL) /* No loaded buffer, find listed one */
1335 {
1336 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1337 if (buf->b_p_bl && buf != curbuf)
1338 break;
1339 }
1340 if (buf == NULL) /* Still no buffer, just take one */
1341 {
1342 if (curbuf->b_next != NULL)
1343 buf = curbuf->b_next;
1344 else
1345 buf = curbuf->b_prev;
1346 }
1347 }
1348
Bram Moolenaara02471e2014-01-10 16:43:14 +01001349 if (buf == NULL)
1350 {
1351 /* Autocommands must have wiped out all other buffers. Only option
1352 * now is to make the current buffer empty. */
1353 return empty_curbuf(FALSE, forceit, action);
1354 }
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 /*
1357 * make buf current buffer
1358 */
1359 if (action == DOBUF_SPLIT) /* split window first */
1360 {
1361# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001362 /* If 'switchbuf' contains "useopen": jump to first window containing
1363 * "buf" if one exists */
1364 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001365 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001366 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001367 * page containing "buf" if one exists */
1368 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 return OK;
1370 if (win_split(0, 0) == FAIL)
1371# endif
1372 return FAIL;
1373 }
1374#endif
1375
1376 /* go to current buffer - nothing to do */
1377 if (buf == curbuf)
1378 return OK;
1379
1380 /*
1381 * Check if the current buffer may be abandoned.
1382 */
1383 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1384 {
1385#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1386 if ((p_confirm || cmdmod.confirm) && p_write)
1387 {
1388 dialog_changed(curbuf, FALSE);
1389# ifdef FEAT_AUTOCMD
1390 if (!buf_valid(buf))
1391 /* Autocommand deleted buffer, oops! */
1392 return FAIL;
1393# endif
1394 }
1395 if (bufIsChanged(curbuf))
1396#endif
1397 {
1398 EMSG(_(e_nowrtmsg));
1399 return FAIL;
1400 }
1401 }
1402
1403 /* Go to the other buffer. */
1404 set_curbuf(buf, action);
1405
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001406#if defined(FEAT_LISTCMDS) \
1407 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001409 {
1410 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#endif
1413
1414#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1415 if (aborting()) /* autocmds may abort script processing */
1416 return FAIL;
1417#endif
1418
1419 return OK;
1420}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
1423/*
1424 * Set current buffer to "buf". Executes autocommands and closes current
1425 * buffer. "action" tells how to close the current buffer:
1426 * DOBUF_GOTO free or hide it
1427 * DOBUF_SPLIT nothing
1428 * DOBUF_UNLOAD unload it
1429 * DOBUF_DEL delete it
1430 * DOBUF_WIPE wipe it out
1431 */
1432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001433set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 buf_T *prevbuf;
1436 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1437 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001438#ifdef FEAT_SYN_HL
1439 long old_tw = curbuf->b_p_tw;
1440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001443 if (!cmdmod.keepalt)
1444 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001445 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /* Don't restart Select mode after switching to another buffer. */
1448 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450 /* close_windows() or apply_autocmds() may change curbuf */
1451 prevbuf = curbuf;
1452
1453#ifdef FEAT_AUTOCMD
1454 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1455# ifdef FEAT_EVAL
1456 if (buf_valid(prevbuf) && !aborting())
1457# else
1458 if (buf_valid(prevbuf))
1459# endif
1460#endif
1461 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001462#ifdef FEAT_SYN_HL
1463 if (prevbuf == curwin->w_buffer)
1464 reset_synblock(curwin);
1465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#ifdef FEAT_WINDOWS
1467 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001468 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#endif
1470#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1471 if (buf_valid(prevbuf) && !aborting())
1472#else
1473 if (buf_valid(prevbuf))
1474#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001475 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001476#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001477 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001478#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001479 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001480 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1482 unload ? action : (action == DOBUF_GOTO
1483 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001484 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001485#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001486 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001487 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001488 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001489#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001493 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001494 * it did ":bunload") or aborted the script processing!
1495 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1496 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001497# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001498 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001499# endif
1500# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001501 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001502# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001503 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001507#ifdef FEAT_SYN_HL
1508 if (old_tw != curbuf->b_p_tw)
1509 check_colorcolumn(curwin);
1510#endif
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512}
1513
1514/*
1515 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001516 * Old curbuf must have been abandoned already! This also means "curbuf" may
1517 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
1519 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522 /* Copy buffer and window local option values. Not for a help buffer. */
1523 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1524 if (!buf->b_help)
1525 get_winopts(buf);
1526#ifdef FEAT_FOLDING
1527 else
1528 /* Remove all folds in the window. */
1529 clearFolding(curwin);
1530 foldUpdateAll(curwin); /* update folds (later). */
1531#endif
1532
1533 /* Get the buffer in the current window. */
1534 curwin->w_buffer = buf;
1535 curbuf = buf;
1536 ++curbuf->b_nwindows;
1537
1538#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001539 if (curwin->w_p_diff)
1540 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541#endif
1542
Bram Moolenaara971b822011-09-14 14:43:25 +02001543#ifdef FEAT_SYN_HL
1544 curwin->w_s = &(buf->b_s);
1545#endif
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 /* Cursor on first line by default. */
1548 curwin->w_cursor.lnum = 1;
1549 curwin->w_cursor.col = 0;
1550#ifdef FEAT_VIRTUALEDIT
1551 curwin->w_cursor.coladd = 0;
1552#endif
1553 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001554#ifdef FEAT_AUTOCMD
1555 curwin->w_topline_was_set = FALSE;
1556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001558 /* mark cursor position as being invalid */
1559 curwin->w_valid = 0;
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 /* Make sure the buffer is loaded. */
1562 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001563 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001564#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001565 /* If there is no filetype, allow for detecting one. Esp. useful for
1566 * ":ball" used in a autocommand. If there already is a filetype we
1567 * might prefer to keep it. */
1568 if (*curbuf->b_p_ft == NUL)
1569 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001570#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001571
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001572 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 else
1575 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001576 if (!msg_silent)
1577 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1579#ifdef FEAT_AUTOCMD
1580 curwin->w_topline = 1;
1581# ifdef FEAT_DIFF
1582 curwin->w_topfill = 0;
1583# endif
1584 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1585 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1586#endif
1587 }
1588
1589 /* If autocommands did not change the cursor position, restore cursor lnum
1590 * and possibly cursor col. */
1591 if (curwin->w_cursor.lnum == 1 && inindent(0))
1592 buflist_getfpos();
1593
1594 check_arg_idx(curwin); /* check for valid arg_idx */
1595#ifdef FEAT_TITLE
1596 maketitle();
1597#endif
1598#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001599 /* when autocmds didn't change it */
1600 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601#endif
1602 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1603
Bram Moolenaar009b2592004-10-24 19:18:58 +00001604#ifdef FEAT_NETBEANS_INTG
1605 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001606 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001607#endif
1608
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001609 /* Change directories when the 'acd' option is set. */
1610 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612#ifdef FEAT_KEYMAP
1613 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001614 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001616#ifdef FEAT_SPELL
1617 /* May need to set the spell language. Can only do this after the buffer
1618 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001619 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1620 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001621#endif
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 redraw_later(NOT_VALID);
1624}
1625
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001626#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1627/*
1628 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001629 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001630 */
1631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001632do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001633{
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001634 if (starting == 0
1635 && curbuf->b_ffname != NULL
1636 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001637 shorten_fnames(TRUE);
1638}
1639#endif
1640
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641/*
1642 * functions for dealing with the buffer list
1643 */
1644
1645/*
1646 * Add a file name to the buffer list. Return a pointer to the buffer.
1647 * If the same file name already exists return a pointer to that buffer.
1648 * If it does not exist, or if fname == NULL, a new entry is created.
1649 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1650 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1651 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001652 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 * This is the ONLY way to create a new buffer.
1654 */
1655static int top_file_num = 1; /* highest file number */
1656
1657 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001658buflist_new(
1659 char_u *ffname, /* full path of fname or relative */
1660 char_u *sfname, /* short fname or NULL */
1661 linenr_T lnum, /* preferred cursor line */
1662 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 buf_T *buf;
1665#ifdef UNIX
1666 struct stat st;
1667#endif
1668
1669 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1670
1671 /*
1672 * If file name already exists in the list, update the entry.
1673 */
1674#ifdef UNIX
1675 /* On Unix we can use inode numbers when the file exists. Works better
1676 * for hard links. */
1677 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1678 st.st_dev = (dev_T)-1;
1679#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001680 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681#ifdef UNIX
1682 buflist_findname_stat(ffname, &st)
1683#else
1684 buflist_findname(ffname)
1685#endif
1686 ) != NULL)
1687 {
1688 vim_free(ffname);
1689 if (lnum != 0)
1690 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1691 /* copy the options now, if 'cpo' doesn't have 's' and not done
1692 * already */
1693 buf_copy_options(buf, 0);
1694 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1695 {
1696 buf->b_p_bl = TRUE;
1697#ifdef FEAT_AUTOCMD
1698 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001699 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001701 if (!buf_valid(buf))
1702 return NULL;
1703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
1705 }
1706 return buf;
1707 }
1708
1709 /*
1710 * If the current buffer has no name and no contents, use the current
1711 * buffer. Otherwise: Need to allocate a new buffer structure.
1712 *
1713 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001714 * (A spell file buffer is allocated in spell.c, but that's not a normal
1715 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 */
1717 buf = NULL;
1718 if ((flags & BLN_CURBUF)
1719 && curbuf != NULL
1720 && curbuf->b_ffname == NULL
1721 && curbuf->b_nwindows <= 1
1722 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1723 {
1724 buf = curbuf;
1725#ifdef FEAT_AUTOCMD
1726 /* It's like this buffer is deleted. Watch out for autocommands that
1727 * change curbuf! If that happens, allocate a new buffer anyway. */
1728 if (curbuf->b_p_bl)
1729 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1730 if (buf == curbuf)
1731 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1732# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001733 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 return NULL;
1735# endif
1736#endif
1737#ifdef FEAT_QUICKFIX
1738# ifdef FEAT_AUTOCMD
1739 if (buf == curbuf)
1740# endif
1741 {
1742 /* Make sure 'bufhidden' and 'buftype' are empty */
1743 clear_string_option(&buf->b_p_bh);
1744 clear_string_option(&buf->b_p_bt);
1745 }
1746#endif
1747 }
1748 if (buf != curbuf || curbuf == NULL)
1749 {
1750 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1751 if (buf == NULL)
1752 {
1753 vim_free(ffname);
1754 return NULL;
1755 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001756#ifdef FEAT_EVAL
1757 /* init b: variables */
1758 buf->b_vars = dict_alloc();
1759 if (buf->b_vars == NULL)
1760 {
1761 vim_free(ffname);
1762 vim_free(buf);
1763 return NULL;
1764 }
1765 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
1768
1769 if (ffname != NULL)
1770 {
1771 buf->b_ffname = ffname;
1772 buf->b_sfname = vim_strsave(sfname);
1773 }
1774
1775 clear_wininfo(buf);
1776 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1777
1778 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1779 || buf->b_wininfo == NULL)
1780 {
1781 vim_free(buf->b_ffname);
1782 buf->b_ffname = NULL;
1783 vim_free(buf->b_sfname);
1784 buf->b_sfname = NULL;
1785 if (buf != curbuf)
1786 free_buffer(buf);
1787 return NULL;
1788 }
1789
1790 if (buf == curbuf)
1791 {
1792 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001793 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (buf != curbuf) /* autocommands deleted the buffer! */
1795 return NULL;
1796#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001797 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 return NULL;
1799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001801
1802 /* Init the options. */
1803 buf->b_p_initialized = FALSE;
1804 buf_copy_options(buf, BCO_ENTER);
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806#ifdef FEAT_KEYMAP
1807 /* need to reload lmaps and set b:keymap_name */
1808 curbuf->b_kmap_state |= KEYMAP_INIT;
1809#endif
1810 }
1811 else
1812 {
1813 /*
1814 * put new buffer at the end of the buffer list
1815 */
1816 buf->b_next = NULL;
1817 if (firstbuf == NULL) /* buffer list is empty */
1818 {
1819 buf->b_prev = NULL;
1820 firstbuf = buf;
1821 }
1822 else /* append new buffer at end of list */
1823 {
1824 lastbuf->b_next = buf;
1825 buf->b_prev = lastbuf;
1826 }
1827 lastbuf = buf;
1828
1829 buf->b_fnum = top_file_num++;
1830 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1831 {
1832 EMSG(_("W14: Warning: List of file names overflow"));
1833 if (emsg_silent == 0)
1834 {
1835 out_flush();
1836 ui_delay(3000L, TRUE); /* make sure it is noticed */
1837 }
1838 top_file_num = 1;
1839 }
1840
1841 /*
1842 * Always copy the options from the current buffer.
1843 */
1844 buf_copy_options(buf, BCO_ALWAYS);
1845 }
1846
1847 buf->b_wininfo->wi_fpos.lnum = lnum;
1848 buf->b_wininfo->wi_win = curwin;
1849
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001850#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001851 hash_init(&buf->b_s.b_keywtab);
1852 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853#endif
1854
1855 buf->b_fname = buf->b_sfname;
1856#ifdef UNIX
1857 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001858 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 else
1860 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001861 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 buf->b_dev = st.st_dev;
1863 buf->b_ino = st.st_ino;
1864 }
1865#endif
1866 buf->b_u_synced = TRUE;
1867 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001868 if (flags & BLN_DUMMY)
1869 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 buf_clear_file(buf);
1871 clrallmarks(buf); /* clear marks */
1872 fmarks_check_names(buf); /* check file marks for this file */
1873 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1874#ifdef FEAT_AUTOCMD
1875 if (!(flags & BLN_DUMMY))
1876 {
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001877 /* Tricky: these autocommands may change the buffer list. They could
1878 * also split the window with re-using the one empty buffer. This may
1879 * result in unexpectedly losing the empty buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001881 if (!buf_valid(buf))
1882 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001886 if (!buf_valid(buf))
1887 return NULL;
1888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001890 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 return NULL;
1892# endif
1893 }
1894#endif
1895
1896 return buf;
1897}
1898
1899/*
1900 * Free the memory for the options of a buffer.
1901 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1902 * 'fileencoding'.
1903 */
1904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001905free_buf_options(
1906 buf_T *buf,
1907 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908{
1909 if (free_p_ff)
1910 {
1911#ifdef FEAT_MBYTE
1912 clear_string_option(&buf->b_p_fenc);
1913#endif
1914 clear_string_option(&buf->b_p_ff);
1915#ifdef FEAT_QUICKFIX
1916 clear_string_option(&buf->b_p_bh);
1917 clear_string_option(&buf->b_p_bt);
1918#endif
1919 }
1920#ifdef FEAT_FIND_ID
1921 clear_string_option(&buf->b_p_def);
1922 clear_string_option(&buf->b_p_inc);
1923# ifdef FEAT_EVAL
1924 clear_string_option(&buf->b_p_inex);
1925# endif
1926#endif
1927#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1928 clear_string_option(&buf->b_p_inde);
1929 clear_string_option(&buf->b_p_indk);
1930#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001931#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1932 clear_string_option(&buf->b_p_bexpr);
1933#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001934#if defined(FEAT_CRYPT)
1935 clear_string_option(&buf->b_p_cm);
1936#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001937#if defined(FEAT_EVAL)
1938 clear_string_option(&buf->b_p_fex);
1939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940#ifdef FEAT_CRYPT
1941 clear_string_option(&buf->b_p_key);
1942#endif
1943 clear_string_option(&buf->b_p_kp);
1944 clear_string_option(&buf->b_p_mps);
1945 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001946 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 clear_string_option(&buf->b_p_isk);
1948#ifdef FEAT_KEYMAP
1949 clear_string_option(&buf->b_p_keymap);
1950 ga_clear(&buf->b_kmap_ga);
1951#endif
1952#ifdef FEAT_COMMENTS
1953 clear_string_option(&buf->b_p_com);
1954#endif
1955#ifdef FEAT_FOLDING
1956 clear_string_option(&buf->b_p_cms);
1957#endif
1958 clear_string_option(&buf->b_p_nf);
1959#ifdef FEAT_SYN_HL
1960 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001961 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001962#endif
1963#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001964 clear_string_option(&buf->b_s.b_p_spc);
1965 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001966 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001967 buf->b_s.b_cap_prog = NULL;
1968 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969#endif
1970#ifdef FEAT_SEARCHPATH
1971 clear_string_option(&buf->b_p_sua);
1972#endif
1973#ifdef FEAT_AUTOCMD
1974 clear_string_option(&buf->b_p_ft);
1975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976#ifdef FEAT_CINDENT
1977 clear_string_option(&buf->b_p_cink);
1978 clear_string_option(&buf->b_p_cino);
1979#endif
1980#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1981 clear_string_option(&buf->b_p_cinw);
1982#endif
1983#ifdef FEAT_INS_EXPAND
1984 clear_string_option(&buf->b_p_cpt);
1985#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001986#ifdef FEAT_COMPL_FUNC
1987 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001988 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990#ifdef FEAT_QUICKFIX
1991 clear_string_option(&buf->b_p_gp);
1992 clear_string_option(&buf->b_p_mp);
1993 clear_string_option(&buf->b_p_efm);
1994#endif
1995 clear_string_option(&buf->b_p_ep);
1996 clear_string_option(&buf->b_p_path);
1997 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01001998 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999#ifdef FEAT_INS_EXPAND
2000 clear_string_option(&buf->b_p_dict);
2001 clear_string_option(&buf->b_p_tsr);
2002#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002003#ifdef FEAT_TEXTOBJ
2004 clear_string_option(&buf->b_p_qe);
2005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002007 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002008#ifdef FEAT_LISP
2009 clear_string_option(&buf->b_p_lw);
2010#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002011 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012}
2013
2014/*
2015 * get alternate file n
2016 * set linenr to lnum or altfpos.lnum if lnum == 0
2017 * also set cursor column to altfpos.col if 'startofline' is not set.
2018 * if (options & GETF_SETMARK) call setpcmark()
2019 * if (options & GETF_ALT) we are jumping to an alternate file.
2020 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2021 *
2022 * return FAIL for failure, OK for success
2023 */
2024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002025buflist_getfile(
2026 int n,
2027 linenr_T lnum,
2028 int options,
2029 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030{
2031 buf_T *buf;
2032#ifdef FEAT_WINDOWS
2033 win_T *wp = NULL;
2034#endif
2035 pos_T *fpos;
2036 colnr_T col;
2037
2038 buf = buflist_findnr(n);
2039 if (buf == NULL)
2040 {
2041 if ((options & GETF_ALT) && n == 0)
2042 EMSG(_(e_noalt));
2043 else
2044 EMSGN(_("E92: Buffer %ld not found"), n);
2045 return FAIL;
2046 }
2047
2048 /* if alternate file is the current buffer, nothing to do */
2049 if (buf == curbuf)
2050 return OK;
2051
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002052 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002053 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002054 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002056 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002057#ifdef FEAT_AUTOCMD
2058 if (curbuf_locked())
2059 return FAIL;
2060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 /* altfpos may be changed by getfile(), get it now */
2063 if (lnum == 0)
2064 {
2065 fpos = buflist_findfpos(buf);
2066 lnum = fpos->lnum;
2067 col = fpos->col;
2068 }
2069 else
2070 col = 0;
2071
2072#ifdef FEAT_WINDOWS
2073 if (options & GETF_SWITCH)
2074 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002075 /* If 'switchbuf' contains "useopen": jump to first window containing
2076 * "buf" if one exists */
2077 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002079
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002080 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002081 * page containing "buf" if one exists */
2082 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002083 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002084
2085 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2086 * current buffer isn't empty: open new tab or window */
2087 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2088 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002090 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002091 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002092 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2093 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002095 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097 }
2098#endif
2099
2100 ++RedrawingDisabled;
2101 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2102 lnum, forceit) <= 0)
2103 {
2104 --RedrawingDisabled;
2105
2106 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2107 if (!p_sol && col != 0)
2108 {
2109 curwin->w_cursor.col = col;
2110 check_cursor_col();
2111#ifdef FEAT_VIRTUALEDIT
2112 curwin->w_cursor.coladd = 0;
2113#endif
2114 curwin->w_set_curswant = TRUE;
2115 }
2116 return OK;
2117 }
2118 --RedrawingDisabled;
2119 return FAIL;
2120}
2121
2122/*
2123 * go to the last know line number for the current buffer
2124 */
2125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002126buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 pos_T *fpos;
2129
2130 fpos = buflist_findfpos(curbuf);
2131
2132 curwin->w_cursor.lnum = fpos->lnum;
2133 check_cursor_lnum();
2134
2135 if (p_sol)
2136 curwin->w_cursor.col = 0;
2137 else
2138 {
2139 curwin->w_cursor.col = fpos->col;
2140 check_cursor_col();
2141#ifdef FEAT_VIRTUALEDIT
2142 curwin->w_cursor.coladd = 0;
2143#endif
2144 curwin->w_set_curswant = TRUE;
2145 }
2146}
2147
Bram Moolenaar81695252004-12-29 20:58:21 +00002148#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2149/*
2150 * Find file in buffer list by name (it has to be for the current window).
2151 * Returns NULL if not found.
2152 */
2153 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002154buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002155{
2156 char_u *ffname;
2157 buf_T *buf = NULL;
2158
2159 /* First make the name into a full path name */
2160 ffname = FullName_save(fname,
2161#ifdef UNIX
2162 TRUE /* force expansion, get rid of symbolic links */
2163#else
2164 FALSE
2165#endif
2166 );
2167 if (ffname != NULL)
2168 {
2169 buf = buflist_findname(ffname);
2170 vim_free(ffname);
2171 }
2172 return buf;
2173}
2174#endif
2175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176/*
2177 * Find file in buffer list by name (it has to be for the current window).
2178 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002179 * Skips dummy buffers.
2180 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 */
2182 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002183buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
2185#ifdef UNIX
2186 struct stat st;
2187
2188 if (mch_stat((char *)ffname, &st) < 0)
2189 st.st_dev = (dev_T)-1;
2190 return buflist_findname_stat(ffname, &st);
2191}
2192
2193/*
2194 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2195 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002196 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 */
2198 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199buflist_findname_stat(
2200 char_u *ffname,
2201 struct stat *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
2203#endif
2204 buf_T *buf;
2205
2206 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002207 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#ifdef UNIX
2209 , stp
2210#endif
2211 ))
2212 return buf;
2213 return NULL;
2214}
2215
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002216#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2217 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218/*
2219 * Find file in buffer list by a regexp pattern.
2220 * Return fnum of the found buffer.
2221 * Return < 0 for error.
2222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002224buflist_findpat(
2225 char_u *pattern,
2226 char_u *pattern_end, /* pointer to first char after pattern */
2227 int unlisted, /* find unlisted buffers */
2228 int diffmode UNUSED, /* find diff-mode buffers only */
2229 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 int match = -1;
2233 int find_listed;
2234 char_u *pat;
2235 char_u *patend;
2236 int attempt;
2237 char_u *p;
2238 int toggledollar;
2239
2240 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2241 {
2242 if (*pattern == '%')
2243 match = curbuf->b_fnum;
2244 else
2245 match = curwin->w_alt_fnum;
2246#ifdef FEAT_DIFF
2247 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2248 match = -1;
2249#endif
2250 }
2251
2252 /*
2253 * Try four ways of matching a listed buffer:
2254 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002255 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 * attempt == 2: with '$' at end (only match at end)
2257 * attempt == 3: with '^' at start and '$' at end (only full match)
2258 * Repeat this for finding an unlisted buffer if there was no matching
2259 * listed buffer.
2260 */
2261 else
2262 {
2263 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2264 if (pat == NULL)
2265 return -1;
2266 patend = pat + STRLEN(pat) - 1;
2267 toggledollar = (patend > pat && *patend == '$');
2268
2269 /* First try finding a listed buffer. If not found and "unlisted"
2270 * is TRUE, try finding an unlisted buffer. */
2271 find_listed = TRUE;
2272 for (;;)
2273 {
2274 for (attempt = 0; attempt <= 3; ++attempt)
2275 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002276 regmatch_T regmatch;
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 /* may add '^' and '$' */
2279 if (toggledollar)
2280 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2281 p = pat;
2282 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2283 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002284 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2285 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 vim_free(pat);
2288 return -1;
2289 }
2290
2291 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2292 if (buf->b_p_bl == find_listed
2293#ifdef FEAT_DIFF
2294 && (!diffmode || diff_mode_buf(buf))
2295#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002296 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002298 if (curtab_only)
2299 {
2300 /* Ignore the match if the buffer is not open in
2301 * the current tab. */
2302#ifdef FEAT_WINDOWS
2303 win_T *wp;
2304
2305 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2306 if (wp->w_buffer == buf)
2307 break;
2308 if (wp == NULL)
2309 continue;
2310#else
2311 if (curwin->w_buffer != buf)
2312 continue;
2313#endif
2314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (match >= 0) /* already found a match */
2316 {
2317 match = -2;
2318 break;
2319 }
2320 match = buf->b_fnum; /* remember first match */
2321 }
2322
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002323 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (match >= 0) /* found one match */
2325 break;
2326 }
2327
2328 /* Only search for unlisted buffers if there was no match with
2329 * a listed buffer. */
2330 if (!unlisted || !find_listed || match != -1)
2331 break;
2332 find_listed = FALSE;
2333 }
2334
2335 vim_free(pat);
2336 }
2337
2338 if (match == -2)
2339 EMSG2(_("E93: More than one match for %s"), pattern);
2340 else if (match < 0)
2341 EMSG2(_("E94: No matching buffer for %s"), pattern);
2342 return match;
2343}
2344#endif
2345
2346#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2347
2348/*
2349 * Find all buffer names that match.
2350 * For command line expansion of ":buf" and ":sbuf".
2351 * Return OK if matches found, FAIL otherwise.
2352 */
2353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002354ExpandBufnames(
2355 char_u *pat,
2356 int *num_file,
2357 char_u ***file,
2358 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
2360 int count = 0;
2361 buf_T *buf;
2362 int round;
2363 char_u *p;
2364 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002365 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367 *num_file = 0; /* return values in case of FAIL */
2368 *file = NULL;
2369
Bram Moolenaar05159a02005-02-26 23:04:13 +00002370 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2371 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002373 patc = alloc((unsigned)STRLEN(pat) + 11);
2374 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002376 STRCPY(patc, "\\(^\\|[\\/]\\)");
2377 STRCPY(patc + 11, pat + 1);
2378 }
2379 else
2380 patc = pat;
2381
2382 /*
2383 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002384 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002385 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002386 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002387 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002388 regmatch_T regmatch;
2389
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002390 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002391 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002392 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2393 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002394 {
2395 if (patc != pat)
2396 vim_free(patc);
2397 return FAIL;
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400 /*
2401 * round == 1: Count the matches.
2402 * round == 2: Build the array to keep the matches.
2403 */
2404 for (round = 1; round <= 2; ++round)
2405 {
2406 count = 0;
2407 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2408 {
2409 if (!buf->b_p_bl) /* skip unlisted buffers */
2410 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002411 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (p != NULL)
2413 {
2414 if (round == 1)
2415 ++count;
2416 else
2417 {
2418 if (options & WILD_HOME_REPLACE)
2419 p = home_replace_save(buf, p);
2420 else
2421 p = vim_strsave(p);
2422 (*file)[count++] = p;
2423 }
2424 }
2425 }
2426 if (count == 0) /* no match found, break here */
2427 break;
2428 if (round == 1)
2429 {
2430 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2431 if (*file == NULL)
2432 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002433 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002434 if (patc != pat)
2435 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 return FAIL;
2437 }
2438 }
2439 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002440 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (count) /* match(es) found, break here */
2442 break;
2443 }
2444
Bram Moolenaar05159a02005-02-26 23:04:13 +00002445 if (patc != pat)
2446 vim_free(patc);
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 *num_file = count;
2449 return (count == 0 ? FAIL : OK);
2450}
2451
2452#endif /* FEAT_CMDL_COMPL */
2453
2454#ifdef HAVE_BUFLIST_MATCH
2455/*
2456 * Check for a match on the file name for buffer "buf" with regprog "prog".
2457 */
2458 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002459buflist_match(
2460 regmatch_T *rmp,
2461 buf_T *buf,
2462 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 char_u *match;
2465
2466 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002467 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002469 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471 return match;
2472}
2473
2474/*
2475 * Try matching the regexp in "prog" with file name "name".
2476 * Return "name" when there is a match, NULL when not.
2477 */
2478 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002479fname_match(
2480 regmatch_T *rmp,
2481 char_u *name,
2482 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 char_u *match = NULL;
2485 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
2487 if (name != NULL)
2488 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002489 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002490 rmp->rm_ic = p_fic || ignore_case;
2491 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 match = name;
2493 else
2494 {
2495 /* Replace $(HOME) with '~' and try matching again. */
2496 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002497 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 match = name;
2499 vim_free(p);
2500 }
2501 }
2502
2503 return match;
2504}
2505#endif
2506
2507/*
2508 * find file in buffer list by number
2509 */
2510 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002511buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 buf_T *buf;
2514
2515 if (nr == 0)
2516 nr = curwin->w_alt_fnum;
2517 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2518 if (buf->b_fnum == nr)
2519 return (buf);
2520 return NULL;
2521}
2522
2523/*
2524 * Get name of file 'n' in the buffer list.
2525 * When the file has no name an empty string is returned.
2526 * home_replace() is used to shorten the file name (used for marks).
2527 * Returns a pointer to allocated memory, of NULL when failed.
2528 */
2529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002530buflist_nr2name(
2531 int n,
2532 int fullname,
2533 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 buf_T *buf;
2536
2537 buf = buflist_findnr(n);
2538 if (buf == NULL)
2539 return NULL;
2540 return home_replace_save(helptail ? buf : NULL,
2541 fullname ? buf->b_ffname : buf->b_fname);
2542}
2543
2544/*
2545 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2546 * When "copy_options" is TRUE save the local window option values.
2547 * When "lnum" is 0 only do the options.
2548 */
2549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002550buflist_setfpos(
2551 buf_T *buf,
2552 win_T *win,
2553 linenr_T lnum,
2554 colnr_T col,
2555 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
2557 wininfo_T *wip;
2558
2559 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2560 if (wip->wi_win == win)
2561 break;
2562 if (wip == NULL)
2563 {
2564 /* allocate a new entry */
2565 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2566 if (wip == NULL)
2567 return;
2568 wip->wi_win = win;
2569 if (lnum == 0) /* set lnum even when it's 0 */
2570 lnum = 1;
2571 }
2572 else
2573 {
2574 /* remove the entry from the list */
2575 if (wip->wi_prev)
2576 wip->wi_prev->wi_next = wip->wi_next;
2577 else
2578 buf->b_wininfo = wip->wi_next;
2579 if (wip->wi_next)
2580 wip->wi_next->wi_prev = wip->wi_prev;
2581 if (copy_options && wip->wi_optset)
2582 {
2583 clear_winopt(&wip->wi_opt);
2584#ifdef FEAT_FOLDING
2585 deleteFoldRecurse(&wip->wi_folds);
2586#endif
2587 }
2588 }
2589 if (lnum != 0)
2590 {
2591 wip->wi_fpos.lnum = lnum;
2592 wip->wi_fpos.col = col;
2593 }
2594 if (copy_options)
2595 {
2596 /* Save the window-specific option values. */
2597 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2598#ifdef FEAT_FOLDING
2599 wip->wi_fold_manual = win->w_fold_manual;
2600 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2601#endif
2602 wip->wi_optset = TRUE;
2603 }
2604
2605 /* insert the entry in front of the list */
2606 wip->wi_next = buf->b_wininfo;
2607 buf->b_wininfo = wip;
2608 wip->wi_prev = NULL;
2609 if (wip->wi_next)
2610 wip->wi_next->wi_prev = wip;
2611
2612 return;
2613}
2614
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002615#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002616static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002617
2618/*
2619 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2620 * page. That's because a diff is local to a tab page.
2621 */
2622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002623wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002624{
2625 win_T *wp;
2626
2627 if (wip->wi_opt.wo_diff)
2628 {
2629 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2630 /* return FALSE when it's a window in the current tab page, thus
2631 * the buffer was in diff mode here */
2632 if (wip->wi_win == wp)
2633 return FALSE;
2634 return TRUE;
2635 }
2636 return FALSE;
2637}
2638#endif
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640/*
2641 * Find info for the current window in buffer "buf".
2642 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002643 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2644 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 * Returns NULL when there isn't any info.
2646 */
2647 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002648find_wininfo(
2649 buf_T *buf,
2650 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 wininfo_T *wip;
2653
2654 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002655 if (wip->wi_win == curwin
2656#ifdef FEAT_DIFF
2657 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2658#endif
2659 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002661
2662 /* If no wininfo for curwin, use the first in the list (that doesn't have
2663 * 'diff' set and is in another tab page). */
2664 if (wip == NULL)
2665 {
2666#ifdef FEAT_DIFF
2667 if (skip_diff_buffer)
2668 {
2669 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2670 if (!wininfo_other_tab_diff(wip))
2671 break;
2672 }
2673 else
2674#endif
2675 wip = buf->b_wininfo;
2676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 return wip;
2678}
2679
2680/*
2681 * Reset the local window options to the values last used in this window.
2682 * If the buffer wasn't used in this window before, use the values from
2683 * the most recently used window. If the values were never set, use the
2684 * global values for the window.
2685 */
2686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002687get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
2689 wininfo_T *wip;
2690
2691 clear_winopt(&curwin->w_onebuf_opt);
2692#ifdef FEAT_FOLDING
2693 clearFolding(curwin);
2694#endif
2695
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002696 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (wip != NULL && wip->wi_optset)
2698 {
2699 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2700#ifdef FEAT_FOLDING
2701 curwin->w_fold_manual = wip->wi_fold_manual;
2702 curwin->w_foldinvalid = TRUE;
2703 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2704#endif
2705 }
2706 else
2707 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2708
2709#ifdef FEAT_FOLDING
2710 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2711 if (p_fdls >= 0)
2712 curwin->w_p_fdl = p_fdls;
2713#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002714#ifdef FEAT_SYN_HL
2715 check_colorcolumn(curwin);
2716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717}
2718
2719/*
2720 * Find the position (lnum and col) for the buffer 'buf' for the current
2721 * window.
2722 * Returns a pointer to no_position if no position is found.
2723 */
2724 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002725buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002728 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002730 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (wip != NULL)
2732 return &(wip->wi_fpos);
2733 else
2734 return &no_position;
2735}
2736
2737/*
2738 * Find the lnum for the buffer 'buf' for the current window.
2739 */
2740 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002741buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
2743 return buflist_findfpos(buf)->lnum;
2744}
2745
2746#if defined(FEAT_LISTCMDS) || defined(PROTO)
2747/*
2748 * List all know file names (for :files and :buffers command).
2749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002751buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 buf_T *buf;
2754 int len;
2755 int i;
2756
2757 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2758 {
2759 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002760 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2761 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2762 || (vim_strchr(eap->arg, '+')
2763 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2764 || (vim_strchr(eap->arg, 'a')
2765 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2766 || (vim_strchr(eap->arg, 'h')
2767 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2768 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2769 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2770 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2771 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2772 || (vim_strchr(eap->arg, '#')
2773 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 continue;
2775 msg_putchar('\n');
2776 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002777 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 else
2779 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2780
Bram Moolenaar50cde822005-06-05 21:54:54 +00002781 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 buf->b_fnum,
2783 buf->b_p_bl ? ' ' : 'u',
2784 buf == curbuf ? '%' :
2785 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2786 buf->b_ml.ml_mfp == NULL ? ' ' :
2787 (buf->b_nwindows == 0 ? 'h' : 'a'),
2788 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2789 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002790 : (bufIsChanged(buf) ? '+' : ' '),
2791 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002792 if (len > IOSIZE - 20)
2793 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 i = 40 - vim_strsize(IObuff);
2797 do
2798 {
2799 IObuff[len++] = ' ';
2800 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002801 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2802 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002803 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 msg_outtrans(IObuff);
2805 out_flush(); /* output one line at a time */
2806 ui_breakcheck();
2807 }
2808}
2809#endif
2810
2811/*
2812 * Get file name and line number for file 'fnum'.
2813 * Used by DoOneCmd() for translating '%' and '#'.
2814 * Used by insert_reg() and cmdline_paste() for '#' register.
2815 * Return FAIL if not found, OK for success.
2816 */
2817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002818buflist_name_nr(
2819 int fnum,
2820 char_u **fname,
2821 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822{
2823 buf_T *buf;
2824
2825 buf = buflist_findnr(fnum);
2826 if (buf == NULL || buf->b_fname == NULL)
2827 return FAIL;
2828
2829 *fname = buf->b_fname;
2830 *lnum = buflist_findlnum(buf);
2831
2832 return OK;
2833}
2834
2835/*
2836 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2837 * The file name with the full path is also remembered, for when :cd is used.
2838 * Returns FAIL for failure (file name already in use by other buffer)
2839 * OK otherwise.
2840 */
2841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002842setfname(
2843 buf_T *buf,
2844 char_u *ffname,
2845 char_u *sfname,
2846 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
Bram Moolenaar81695252004-12-29 20:58:21 +00002848 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#ifdef UNIX
2850 struct stat st;
2851#endif
2852
2853 if (ffname == NULL || *ffname == NUL)
2854 {
2855 /* Removing the name. */
2856 vim_free(buf->b_ffname);
2857 vim_free(buf->b_sfname);
2858 buf->b_ffname = NULL;
2859 buf->b_sfname = NULL;
2860#ifdef UNIX
2861 st.st_dev = (dev_T)-1;
2862#endif
2863 }
2864 else
2865 {
2866 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2867 if (ffname == NULL) /* out of memory */
2868 return FAIL;
2869
2870 /*
2871 * if the file name is already used in another buffer:
2872 * - if the buffer is loaded, fail
2873 * - if the buffer is not loaded, delete it from the list
2874 */
2875#ifdef UNIX
2876 if (mch_stat((char *)ffname, &st) < 0)
2877 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002878#endif
2879 if (!(buf->b_flags & BF_DUMMY))
2880#ifdef UNIX
2881 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002883 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884#endif
2885 if (obuf != NULL && obuf != buf)
2886 {
2887 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2888 {
2889 if (message)
2890 EMSG(_("E95: Buffer with this name already exists"));
2891 vim_free(ffname);
2892 return FAIL;
2893 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002894 /* delete from the list */
2895 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897 sfname = vim_strsave(sfname);
2898 if (ffname == NULL || sfname == NULL)
2899 {
2900 vim_free(sfname);
2901 vim_free(ffname);
2902 return FAIL;
2903 }
2904#ifdef USE_FNAME_CASE
2905# ifdef USE_LONG_FNAME
2906 if (USE_LONG_FNAME)
2907# endif
2908 fname_case(sfname, 0); /* set correct case for short file name */
2909#endif
2910 vim_free(buf->b_ffname);
2911 vim_free(buf->b_sfname);
2912 buf->b_ffname = ffname;
2913 buf->b_sfname = sfname;
2914 }
2915 buf->b_fname = buf->b_sfname;
2916#ifdef UNIX
2917 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002918 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 else
2920 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002921 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 buf->b_dev = st.st_dev;
2923 buf->b_ino = st.st_ino;
2924 }
2925#endif
2926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
2929 buf_name_changed(buf);
2930 return OK;
2931}
2932
2933/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002934 * Crude way of changing the name of a buffer. Use with care!
2935 * The name should be relative to the current directory.
2936 */
2937 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002939{
2940 buf_T *buf;
2941
2942 buf = buflist_findnr(fnum);
2943 if (buf != NULL)
2944 {
2945 vim_free(buf->b_sfname);
2946 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002947 buf->b_ffname = vim_strsave(name);
2948 buf->b_sfname = NULL;
2949 /* Allocate ffname and expand into full path. Also resolves .lnk
2950 * files on Win32. */
2951 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002952 buf->b_fname = buf->b_sfname;
2953 }
2954}
2955
2956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 * Take care of what needs to be done when the name of buffer "buf" has
2958 * changed.
2959 */
2960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002961buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 /*
2964 * If the file name changed, also change the name of the swapfile
2965 */
2966 if (buf->b_ml.ml_mfp != NULL)
2967 ml_setname(buf);
2968
2969 if (curwin->w_buffer == buf)
2970 check_arg_idx(curwin); /* check file name for arg list */
2971#ifdef FEAT_TITLE
2972 maketitle(); /* set window title */
2973#endif
2974#ifdef FEAT_WINDOWS
2975 status_redraw_all(); /* status lines need to be redrawn */
2976#endif
2977 fmarks_check_names(buf); /* check named file marks */
2978 ml_timestamp(buf); /* reset timestamp */
2979}
2980
2981/*
2982 * set alternate file name for current window
2983 *
2984 * Used by do_one_cmd(), do_write() and do_ecmd().
2985 * Return the buffer.
2986 */
2987 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002988setaltfname(
2989 char_u *ffname,
2990 char_u *sfname,
2991 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 buf_T *buf;
2994
2995 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2996 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002997 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 curwin->w_alt_fnum = buf->b_fnum;
2999 return buf;
3000}
3001
3002/*
3003 * Get alternate file name for current window.
3004 * Return NULL if there isn't any, and give error message if requested.
3005 */
3006 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003007getaltfname(
3008 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 char_u *fname;
3011 linenr_T dummy;
3012
3013 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3014 {
3015 if (errmsg)
3016 EMSG(_(e_noalt));
3017 return NULL;
3018 }
3019 return fname;
3020}
3021
3022/*
3023 * Add a file name to the buflist and return its number.
3024 * Uses same flags as buflist_new(), except BLN_DUMMY.
3025 *
3026 * used by qf_init(), main() and doarglist()
3027 */
3028 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003029buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 buf_T *buf;
3032
3033 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3034 if (buf != NULL)
3035 return buf->b_fnum;
3036 return 0;
3037}
3038
3039#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3040/*
3041 * Adjust slashes in file names. Called after 'shellslash' was set.
3042 */
3043 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003044buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 buf_T *bp;
3047
3048 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3049 {
3050 if (bp->b_ffname != NULL)
3051 slash_adjust(bp->b_ffname);
3052 if (bp->b_sfname != NULL)
3053 slash_adjust(bp->b_sfname);
3054 }
3055}
3056#endif
3057
3058/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003059 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 * Also save the local window option values.
3061 */
3062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003063buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003065 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066}
3067
3068/*
3069 * Return TRUE if 'ffname' is not the same file as current file.
3070 * Fname must have a full path (expanded by mch_FullName()).
3071 */
3072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003073otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
3075 return otherfile_buf(curbuf, ffname
3076#ifdef UNIX
3077 , NULL
3078#endif
3079 );
3080}
3081
3082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003083otherfile_buf(
3084 buf_T *buf,
3085 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#ifdef UNIX
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087 , struct stat *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003089 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 /* no name is different */
3092 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3093 return TRUE;
3094 if (fnamecmp(ffname, buf->b_ffname) == 0)
3095 return FALSE;
3096#ifdef UNIX
3097 {
3098 struct stat st;
3099
3100 /* If no struct stat given, get it now */
3101 if (stp == NULL)
3102 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003103 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 st.st_dev = (dev_T)-1;
3105 stp = &st;
3106 }
3107 /* Use dev/ino to check if the files are the same, even when the names
3108 * are different (possible with links). Still need to compare the
3109 * name above, for when the file doesn't exist yet.
3110 * Problem: The dev/ino changes when a file is deleted (and created
3111 * again) and remains the same when renamed/moved. We don't want to
3112 * mch_stat() each buffer each time, that would be too slow. Get the
3113 * dev/ino again when they appear to match, but not when they appear
3114 * to be different: Could skip a buffer when it's actually the same
3115 * file. */
3116 if (buf_same_ino(buf, stp))
3117 {
3118 buf_setino(buf);
3119 if (buf_same_ino(buf, stp))
3120 return FALSE;
3121 }
3122 }
3123#endif
3124 return TRUE;
3125}
3126
3127#if defined(UNIX) || defined(PROTO)
3128/*
3129 * Set inode and device number for a buffer.
3130 * Must always be called when b_fname is changed!.
3131 */
3132 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003133buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
3135 struct stat st;
3136
3137 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3138 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003139 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 buf->b_dev = st.st_dev;
3141 buf->b_ino = st.st_ino;
3142 }
3143 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003144 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145}
3146
3147/*
3148 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3149 */
3150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003151buf_same_ino(
3152 buf_T *buf,
3153 struct stat *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003155 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 && stp->st_dev == buf->b_dev
3157 && stp->st_ino == buf->b_ino);
3158}
3159#endif
3160
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003161/*
3162 * Print info about the current buffer.
3163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003165fileinfo(
3166 int fullname, /* when non-zero print full path */
3167 int shorthelp,
3168 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
3170 char_u *name;
3171 int n;
3172 char_u *p;
3173 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003174 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 buffer = alloc(IOSIZE);
3177 if (buffer == NULL)
3178 return;
3179
3180 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3181 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003182 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 p = buffer + STRLEN(buffer);
3184 }
3185 else
3186 p = buffer;
3187
3188 *p++ = '"';
3189 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003190 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 else
3192 {
3193 if (!fullname && curbuf->b_fname != NULL)
3194 name = curbuf->b_fname;
3195 else
3196 name = curbuf->b_ffname;
3197 home_replace(shorthelp ? curbuf : NULL, name, p,
3198 (int)(IOSIZE - (p - buffer)), TRUE);
3199 }
3200
Bram Moolenaara800b422010-06-27 01:15:55 +02003201 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 curbufIsChanged() ? (shortmess(SHM_MOD)
3203 ? " [+]" : _(" [Modified]")) : " ",
3204 (curbuf->b_flags & BF_NOTEDITED)
3205#ifdef FEAT_QUICKFIX
3206 && !bt_dontwrite(curbuf)
3207#endif
3208 ? _("[Not edited]") : "",
3209 (curbuf->b_flags & BF_NEW)
3210#ifdef FEAT_QUICKFIX
3211 && !bt_dontwrite(curbuf)
3212#endif
3213 ? _("[New file]") : "",
3214 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003215 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 : _("[readonly]")) : "",
3217 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3218 || curbuf->b_p_ro) ?
3219 " " : "");
3220 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3221 * causes an overflow, thus for large numbers divide instead. */
3222 if (curwin->w_cursor.lnum > 1000000L)
3223 n = (int)(((long)curwin->w_cursor.lnum) /
3224 ((long)curbuf->b_ml.ml_line_count / 100L));
3225 else
3226 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3227 (long)curbuf->b_ml.ml_line_count);
3228 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3229 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003230 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232#ifdef FEAT_CMDL_INFO
3233 else if (p_ru)
3234 {
3235 /* Current line and column are already on the screen -- webb */
3236 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003237 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003239 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 (long)curbuf->b_ml.ml_line_count, n);
3241 }
3242#endif
3243 else
3244 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003245 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 _("line %ld of %ld --%d%%-- col "),
3247 (long)curwin->w_cursor.lnum,
3248 (long)curbuf->b_ml.ml_line_count,
3249 n);
3250 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003251 len = STRLEN(buffer);
3252 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3254 }
3255
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003256 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258 if (dont_truncate)
3259 {
3260 /* Temporarily set msg_scroll to avoid the message being truncated.
3261 * First call msg_start() to get the message in the right place. */
3262 msg_start();
3263 n = msg_scroll;
3264 msg_scroll = TRUE;
3265 msg(buffer);
3266 msg_scroll = n;
3267 }
3268 else
3269 {
3270 p = msg_trunc_attr(buffer, FALSE, 0);
3271 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 /* Need to repeat the message after redrawing when:
3273 * - When restart_edit is set (otherwise there will be a delay
3274 * before redrawing).
3275 * - When the screen was scrolled but there is no wait-return
3276 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003277 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279
3280 vim_free(buffer);
3281}
3282
3283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003284col_print(
3285 char_u *buf,
3286 size_t buflen,
3287 int col,
3288 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003291 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003293 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294}
3295
3296#if defined(FEAT_TITLE) || defined(PROTO)
3297/*
3298 * put file name in title bar of window and in icon title
3299 */
3300
3301static char_u *lasttitle = NULL;
3302static char_u *lasticon = NULL;
3303
3304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 char_u *p;
3308 char_u *t_str = NULL;
3309 char_u *i_name;
3310 char_u *i_str = NULL;
3311 int maxlen = 0;
3312 int len;
3313 int mustset;
3314 char_u buf[IOSIZE];
3315 int off;
3316
3317 if (!redrawing())
3318 {
3319 /* Postpone updating the title when 'lazyredraw' is set. */
3320 need_maketitle = TRUE;
3321 return;
3322 }
3323
3324 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003325 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 return;
3327
3328 if (p_title)
3329 {
3330 if (p_titlelen > 0)
3331 {
3332 maxlen = p_titlelen * Columns / 100;
3333 if (maxlen < 10)
3334 maxlen = 10;
3335 }
3336
3337 t_str = buf;
3338 if (*p_titlestring != NUL)
3339 {
3340#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003341 if (stl_syntax & STL_IN_TITLE)
3342 {
3343 int use_sandbox = FALSE;
3344 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003345
3346# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003347 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003348# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003349 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003351 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003352 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003353 if (called_emsg)
3354 set_string_option_direct((char_u *)"titlestring", -1,
3355 (char_u *)"", OPT_FREE, SID_ERROR);
3356 called_emsg |= save_called_emsg;
3357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 else
3359#endif
3360 t_str = p_titlestring;
3361 }
3362 else
3363 {
3364 /* format: "fname + (path) (1 of 2) - VIM" */
3365
Bram Moolenaar2c666692012-09-05 13:30:40 +02003366#define SPACE_FOR_FNAME (IOSIZE - 100)
3367#define SPACE_FOR_DIR (IOSIZE - 20)
3368#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003370 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else
3372 {
3373 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003374 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377
3378 switch (bufIsChanged(curbuf)
3379 + (curbuf->b_p_ro * 2)
3380 + (!curbuf->b_p_ma * 4))
3381 {
3382 case 1: STRCAT(buf, " +"); break;
3383 case 2: STRCAT(buf, " ="); break;
3384 case 3: STRCAT(buf, " =+"); break;
3385 case 4:
3386 case 6: STRCAT(buf, " -"); break;
3387 case 5:
3388 case 7: STRCAT(buf, " -+"); break;
3389 }
3390
3391 if (curbuf->b_fname != NULL)
3392 {
3393 /* Get path of file, replace home dir with ~ */
3394 off = (int)STRLEN(buf);
3395 buf[off++] = ' ';
3396 buf[off++] = '(';
3397 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003398 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#ifdef BACKSLASH_IN_FILENAME
3400 /* avoid "c:/name" to be reduced to "c" */
3401 if (isalpha(buf[off]) && buf[off + 1] == ':')
3402 off += 2;
3403#endif
3404 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003405 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003408 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003409 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003412
Bram Moolenaar2c666692012-09-05 13:30:40 +02003413 /* Translate unprintable chars and concatenate. Keep some
3414 * room for the server name. When there is no room (very long
3415 * file name) use (...). */
3416 if (off < SPACE_FOR_DIR)
3417 {
3418 p = transstr(buf + off);
3419 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3420 vim_free(p);
3421 }
3422 else
3423 {
3424 vim_strncpy(buf + off, (char_u *)"...",
3425 (size_t)(SPACE_FOR_ARGNR - off));
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 STRCAT(buf, ")");
3428 }
3429
Bram Moolenaar2c666692012-09-05 13:30:40 +02003430 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432#if defined(FEAT_CLIENTSERVER)
3433 if (serverName != NULL)
3434 {
3435 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003436 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 else
3439#endif
3440 STRCAT(buf, " - VIM");
3441
3442 if (maxlen > 0)
3443 {
3444 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003445 if (vim_strsize(buf) > maxlen)
3446 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 }
3449 }
3450 mustset = ti_change(t_str, &lasttitle);
3451
3452 if (p_icon)
3453 {
3454 i_str = buf;
3455 if (*p_iconstring != NUL)
3456 {
3457#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003458 if (stl_syntax & STL_IN_ICON)
3459 {
3460 int use_sandbox = FALSE;
3461 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003462
3463# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003464 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003465# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003466 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003468 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003469 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003470 if (called_emsg)
3471 set_string_option_direct((char_u *)"iconstring", -1,
3472 (char_u *)"", OPT_FREE, SID_ERROR);
3473 called_emsg |= save_called_emsg;
3474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 else
3476#endif
3477 i_str = p_iconstring;
3478 }
3479 else
3480 {
3481 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003482 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 else /* use file name only in icon */
3484 i_name = gettail(curbuf->b_ffname);
3485 *i_str = NUL;
3486 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003487 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (len > 100)
3489 {
3490 len -= 100;
3491#ifdef FEAT_MBYTE
3492 if (has_mbyte)
3493 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3494#endif
3495 i_name += len;
3496 }
3497 STRCPY(i_str, i_name);
3498 trans_characters(i_str, IOSIZE);
3499 }
3500 }
3501
3502 mustset |= ti_change(i_str, &lasticon);
3503
3504 if (mustset)
3505 resettitle();
3506}
3507
3508/*
3509 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3510 * from "str" if it does.
3511 * Return TRUE when "*last" changed.
3512 */
3513 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003514ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 if ((str == NULL) != (*last == NULL)
3517 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3518 {
3519 vim_free(*last);
3520 if (str == NULL)
3521 *last = NULL;
3522 else
3523 *last = vim_strsave(str);
3524 return TRUE;
3525 }
3526 return FALSE;
3527}
3528
3529/*
3530 * Put current window title back (used after calling a shell)
3531 */
3532 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003533resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534{
3535 mch_settitle(lasttitle, lasticon);
3536}
Bram Moolenaarea408852005-06-25 22:49:46 +00003537
3538# if defined(EXITFREE) || defined(PROTO)
3539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003540free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003541{
3542 vim_free(lasttitle);
3543 vim_free(lasticon);
3544}
3545# endif
3546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547#endif /* FEAT_TITLE */
3548
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003549#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003551 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 * Return length of string in screen cells.
3553 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003554 * Normally works for window "wp", except when working for 'tabline' then it
3555 * is "curwin".
3556 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 * Items are drawn interspersed with the text that surrounds it
3558 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3559 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3560 *
3561 * If maxwidth is not zero, the string will be filled at any middle marker
3562 * or truncated if too long, fillchar is used for all whitespace.
3563 */
3564 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003565build_stl_str_hl(
3566 win_T *wp,
3567 char_u *out, /* buffer to write into != NameBuff */
3568 size_t outlen, /* length of out[] */
3569 char_u *fmt,
3570 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3571 int fillchar,
3572 int maxwidth,
3573 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3574 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576 char_u *p;
3577 char_u *s;
3578 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003579 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580#ifdef FEAT_EVAL
3581 win_T *o_curwin;
3582 buf_T *o_curbuf;
3583#endif
3584 int empty_line;
3585 colnr_T virtcol;
3586 long l;
3587 long n;
3588 int prevchar_isflag;
3589 int prevchar_isitem;
3590 int itemisflag;
3591 int fillable;
3592 char_u *str;
3593 long num;
3594 int width;
3595 int itemcnt;
3596 int curitem;
3597 int groupitem[STL_MAX_ITEM];
3598 int groupdepth;
3599 struct stl_item
3600 {
3601 char_u *start;
3602 int minwid;
3603 int maxwid;
3604 enum
3605 {
3606 Normal,
3607 Empty,
3608 Group,
3609 Middle,
3610 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003611 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 Trunc
3613 } type;
3614 } item[STL_MAX_ITEM];
3615 int minwid;
3616 int maxwid;
3617 int zeropad;
3618 char_u base;
3619 char_u opt;
3620#define TMPLEN 70
3621 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003622 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003623 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003624
3625#ifdef FEAT_EVAL
3626 /*
3627 * When the format starts with "%!" then evaluate it as an expression and
3628 * use the result as the actual format string.
3629 */
3630 if (fmt[0] == '%' && fmt[1] == '!')
3631 {
3632 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3633 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003634 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003635 }
3636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 if (fillchar == 0)
3639 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003640#ifdef FEAT_MBYTE
3641 /* Can't handle a multi-byte fill character yet. */
3642 else if (mb_char2len(fillchar) > 1)
3643 fillchar = '-';
3644#endif
3645
Bram Moolenaar567199b2013-04-24 16:52:36 +02003646 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3647 * p will become invalid when getting another buffer line. */
3648 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3649 empty_line = (*p == NUL);
3650
3651 /* Get the byte value now, in case we need it below. This is more
3652 * efficient than making a copy of the line. */
3653 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3654 byteval = 0;
3655 else
3656#ifdef FEAT_MBYTE
3657 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3658#else
3659 byteval = p[wp->w_cursor.col];
3660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662 groupdepth = 0;
3663 p = out;
3664 curitem = 0;
3665 prevchar_isflag = TRUE;
3666 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003667 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003669 if (curitem == STL_MAX_ITEM)
3670 {
3671 /* There are too many items. Add the error code to the statusline
3672 * to give the user a hint about what went wrong. */
3673 if (p + 6 < out + outlen)
3674 {
3675 mch_memmove(p, " E541", (size_t)5);
3676 p += 5;
3677 }
3678 break;
3679 }
3680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (*s != NUL && *s != '%')
3682 prevchar_isflag = prevchar_isitem = FALSE;
3683
3684 /*
3685 * Handle up to the next '%' or the end.
3686 */
3687 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3688 *p++ = *s++;
3689 if (*s == NUL || p + 1 >= out + outlen)
3690 break;
3691
3692 /*
3693 * Handle one '%' item.
3694 */
3695 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003696 if (*s == NUL) /* ignore trailing % */
3697 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 if (*s == '%')
3699 {
3700 if (p + 1 >= out + outlen)
3701 break;
3702 *p++ = *s++;
3703 prevchar_isflag = prevchar_isitem = FALSE;
3704 continue;
3705 }
3706 if (*s == STL_MIDDLEMARK)
3707 {
3708 s++;
3709 if (groupdepth > 0)
3710 continue;
3711 item[curitem].type = Middle;
3712 item[curitem++].start = p;
3713 continue;
3714 }
3715 if (*s == STL_TRUNCMARK)
3716 {
3717 s++;
3718 item[curitem].type = Trunc;
3719 item[curitem++].start = p;
3720 continue;
3721 }
3722 if (*s == ')')
3723 {
3724 s++;
3725 if (groupdepth < 1)
3726 continue;
3727 groupdepth--;
3728
3729 t = item[groupitem[groupdepth]].start;
3730 *p = NUL;
3731 l = vim_strsize(t);
3732 if (curitem > groupitem[groupdepth] + 1
3733 && item[groupitem[groupdepth]].minwid == 0)
3734 {
3735 /* remove group if all items are empty */
3736 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003737 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 break;
3739 if (n == curitem)
3740 {
3741 p = t;
3742 l = 0;
3743 }
3744 }
3745 if (l > item[groupitem[groupdepth]].maxwid)
3746 {
3747 /* truncate, remove n bytes of text at the start */
3748#ifdef FEAT_MBYTE
3749 if (has_mbyte)
3750 {
3751 /* Find the first character that should be included. */
3752 n = 0;
3753 while (l >= item[groupitem[groupdepth]].maxwid)
3754 {
3755 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003756 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758 }
3759 else
3760#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003761 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003764 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 p = p - n + 1;
3766#ifdef FEAT_MBYTE
3767 /* Fill up space left over by half a double-wide char. */
3768 while (++l < item[groupitem[groupdepth]].minwid)
3769 *p++ = fillchar;
3770#endif
3771
3772 /* correct the start of the items for the truncation */
3773 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3774 {
3775 item[l].start -= n;
3776 if (item[l].start < t)
3777 item[l].start = t;
3778 }
3779 }
3780 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3781 {
3782 /* fill */
3783 n = item[groupitem[groupdepth]].minwid;
3784 if (n < 0)
3785 {
3786 /* fill by appending characters */
3787 n = 0 - n;
3788 while (l++ < n && p + 1 < out + outlen)
3789 *p++ = fillchar;
3790 }
3791 else
3792 {
3793 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003794 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 l = n - l;
3796 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003797 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 p += l;
3799 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3800 item[n].start += l;
3801 for ( ; l > 0; l--)
3802 *t++ = fillchar;
3803 }
3804 }
3805 continue;
3806 }
3807 minwid = 0;
3808 maxwid = 9999;
3809 zeropad = FALSE;
3810 l = 1;
3811 if (*s == '0')
3812 {
3813 s++;
3814 zeropad = TRUE;
3815 }
3816 if (*s == '-')
3817 {
3818 s++;
3819 l = -1;
3820 }
3821 if (VIM_ISDIGIT(*s))
3822 {
3823 minwid = (int)getdigits(&s);
3824 if (minwid < 0) /* overflow */
3825 minwid = 0;
3826 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003827 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 {
3829 item[curitem].type = Highlight;
3830 item[curitem].start = p;
3831 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3832 s++;
3833 curitem++;
3834 continue;
3835 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003836 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3837 {
3838 if (*s == STL_TABCLOSENR)
3839 {
3840 if (minwid == 0)
3841 {
3842 /* %X ends the close label, go back to the previously
3843 * define tab label nr. */
3844 for (n = curitem - 1; n >= 0; --n)
3845 if (item[n].type == TabPage && item[n].minwid >= 0)
3846 {
3847 minwid = item[n].minwid;
3848 break;
3849 }
3850 }
3851 else
3852 /* close nrs are stored as negative values */
3853 minwid = - minwid;
3854 }
3855 item[curitem].type = TabPage;
3856 item[curitem].start = p;
3857 item[curitem].minwid = minwid;
3858 s++;
3859 curitem++;
3860 continue;
3861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (*s == '.')
3863 {
3864 s++;
3865 if (VIM_ISDIGIT(*s))
3866 {
3867 maxwid = (int)getdigits(&s);
3868 if (maxwid <= 0) /* overflow */
3869 maxwid = 50;
3870 }
3871 }
3872 minwid = (minwid > 50 ? 50 : minwid) * l;
3873 if (*s == '(')
3874 {
3875 groupitem[groupdepth++] = curitem;
3876 item[curitem].type = Group;
3877 item[curitem].start = p;
3878 item[curitem].minwid = minwid;
3879 item[curitem].maxwid = maxwid;
3880 s++;
3881 curitem++;
3882 continue;
3883 }
3884 if (vim_strchr(STL_ALL, *s) == NULL)
3885 {
3886 s++;
3887 continue;
3888 }
3889 opt = *s++;
3890
3891 /* OK - now for the real work */
3892 base = 'D';
3893 itemisflag = FALSE;
3894 fillable = TRUE;
3895 num = -1;
3896 str = NULL;
3897 switch (opt)
3898 {
3899 case STL_FILEPATH:
3900 case STL_FULLPATH:
3901 case STL_FILENAME:
3902 fillable = FALSE; /* don't change ' ' to fillchar */
3903 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003904 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 else
3906 {
3907 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003908 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3910 }
3911 trans_characters(NameBuff, MAXPATHL);
3912 if (opt != STL_FILENAME)
3913 str = NameBuff;
3914 else
3915 str = gettail(NameBuff);
3916 break;
3917
3918 case STL_VIM_EXPR: /* '{' */
3919 itemisflag = TRUE;
3920 t = p;
3921 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3922 *p++ = *s++;
3923 if (*s != '}') /* missing '}' or out of space */
3924 break;
3925 s++;
3926 *p = 0;
3927 p = t;
3928
3929#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003930 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3932
3933 o_curbuf = curbuf;
3934 o_curwin = curwin;
3935 curwin = wp;
3936 curbuf = wp->w_buffer;
3937
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003938 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
3940 curwin = o_curwin;
3941 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003942 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 if (str != NULL && *str != 0)
3945 {
3946 if (*skipdigits(str) == NUL)
3947 {
3948 num = atoi((char *)str);
3949 vim_free(str);
3950 str = NULL;
3951 itemisflag = FALSE;
3952 }
3953 }
3954#endif
3955 break;
3956
3957 case STL_LINE:
3958 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3959 ? 0L : (long)(wp->w_cursor.lnum);
3960 break;
3961
3962 case STL_NUMLINES:
3963 num = wp->w_buffer->b_ml.ml_line_count;
3964 break;
3965
3966 case STL_COLUMN:
3967 num = !(State & INSERT) && empty_line
3968 ? 0 : (int)wp->w_cursor.col + 1;
3969 break;
3970
3971 case STL_VIRTCOL:
3972 case STL_VIRTCOL_ALT:
3973 /* In list mode virtcol needs to be recomputed */
3974 virtcol = wp->w_virtcol;
3975 if (wp->w_p_list && lcs_tab1 == NUL)
3976 {
3977 wp->w_p_list = FALSE;
3978 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3979 wp->w_p_list = TRUE;
3980 }
3981 ++virtcol;
3982 /* Don't display %V if it's the same as %c. */
3983 if (opt == STL_VIRTCOL_ALT
3984 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3985 ? 0 : (int)wp->w_cursor.col + 1)))
3986 break;
3987 num = (long)virtcol;
3988 break;
3989
3990 case STL_PERCENTAGE:
3991 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3992 (long)wp->w_buffer->b_ml.ml_line_count);
3993 break;
3994
3995 case STL_ALTPERCENT:
3996 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003997 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 break;
3999
4000 case STL_ARGLISTSTAT:
4001 fillable = FALSE;
4002 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004003 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 str = tmp;
4005 break;
4006
4007 case STL_KEYMAP:
4008 fillable = FALSE;
4009 if (get_keymap_str(wp, tmp, TMPLEN))
4010 str = tmp;
4011 break;
4012 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004013#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004014 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#else
4016 num = 0;
4017#endif
4018 break;
4019
4020 case STL_BUFNO:
4021 num = wp->w_buffer->b_fnum;
4022 break;
4023
4024 case STL_OFFSET_X:
4025 base = 'X';
4026 case STL_OFFSET:
4027#ifdef FEAT_BYTEOFF
4028 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4029 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4030 0L : l + 1 + (!(State & INSERT) && empty_line ?
4031 0 : (int)wp->w_cursor.col);
4032#endif
4033 break;
4034
4035 case STL_BYTEVAL_X:
4036 base = 'X';
4037 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004038 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (num == NL)
4040 num = 0;
4041 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4042 num = NL;
4043 break;
4044
4045 case STL_ROFLAG:
4046 case STL_ROFLAG_ALT:
4047 itemisflag = TRUE;
4048 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004049 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 break;
4051
4052 case STL_HELPFLAG:
4053 case STL_HELPFLAG_ALT:
4054 itemisflag = TRUE;
4055 if (wp->w_buffer->b_help)
4056 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004057 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 break;
4059
4060#ifdef FEAT_AUTOCMD
4061 case STL_FILETYPE:
4062 if (*wp->w_buffer->b_p_ft != NUL
4063 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4064 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004065 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4066 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 str = tmp;
4068 }
4069 break;
4070
4071 case STL_FILETYPE_ALT:
4072 itemisflag = TRUE;
4073 if (*wp->w_buffer->b_p_ft != NUL
4074 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4075 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004076 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4077 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 for (t = tmp; *t != 0; t++)
4079 *t = TOUPPER_LOC(*t);
4080 str = tmp;
4081 }
4082 break;
4083#endif
4084
4085#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4086 case STL_PREVIEWFLAG:
4087 case STL_PREVIEWFLAG_ALT:
4088 itemisflag = TRUE;
4089 if (wp->w_p_pvw)
4090 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4091 : _("[Preview]"));
4092 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004093
4094 case STL_QUICKFIX:
4095 if (bt_quickfix(wp->w_buffer))
4096 str = (char_u *)(wp->w_llist_ref
4097 ? _(msg_loclist)
4098 : _(msg_qflist));
4099 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100#endif
4101
4102 case STL_MODIFIED:
4103 case STL_MODIFIED_ALT:
4104 itemisflag = TRUE;
4105 switch ((opt == STL_MODIFIED_ALT)
4106 + bufIsChanged(wp->w_buffer) * 2
4107 + (!wp->w_buffer->b_p_ma) * 4)
4108 {
4109 case 2: str = (char_u *)"[+]"; break;
4110 case 3: str = (char_u *)",+"; break;
4111 case 4: str = (char_u *)"[-]"; break;
4112 case 5: str = (char_u *)",-"; break;
4113 case 6: str = (char_u *)"[+-]"; break;
4114 case 7: str = (char_u *)",+-"; break;
4115 }
4116 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004117
4118 case STL_HIGHLIGHT:
4119 t = s;
4120 while (*s != '#' && *s != NUL)
4121 ++s;
4122 if (*s == '#')
4123 {
4124 item[curitem].type = Highlight;
4125 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004126 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004127 curitem++;
4128 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004129 if (*s != NUL)
4130 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004131 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133
4134 item[curitem].start = p;
4135 item[curitem].type = Normal;
4136 if (str != NULL && *str)
4137 {
4138 t = str;
4139 if (itemisflag)
4140 {
4141 if ((t[0] && t[1])
4142 && ((!prevchar_isitem && *t == ',')
4143 || (prevchar_isflag && *t == ' ')))
4144 t++;
4145 prevchar_isflag = TRUE;
4146 }
4147 l = vim_strsize(t);
4148 if (l > 0)
4149 prevchar_isitem = TRUE;
4150 if (l > maxwid)
4151 {
4152 while (l >= maxwid)
4153#ifdef FEAT_MBYTE
4154 if (has_mbyte)
4155 {
4156 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004157 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159 else
4160#endif
4161 l -= byte2cells(*t++);
4162 if (p + 1 >= out + outlen)
4163 break;
4164 *p++ = '<';
4165 }
4166 if (minwid > 0)
4167 {
4168 for (; l < minwid && p + 1 < out + outlen; l++)
4169 {
4170 /* Don't put a "-" in front of a digit. */
4171 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4172 *p++ = ' ';
4173 else
4174 *p++ = fillchar;
4175 }
4176 minwid = 0;
4177 }
4178 else
4179 minwid *= -1;
4180 while (*t && p + 1 < out + outlen)
4181 {
4182 *p++ = *t++;
4183 /* Change a space by fillchar, unless fillchar is '-' and a
4184 * digit follows. */
4185 if (fillable && p[-1] == ' '
4186 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4187 p[-1] = fillchar;
4188 }
4189 for (; l < minwid && p + 1 < out + outlen; l++)
4190 *p++ = fillchar;
4191 }
4192 else if (num >= 0)
4193 {
4194 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4195 char_u nstr[20];
4196
4197 if (p + 20 >= out + outlen)
4198 break; /* not sufficient space */
4199 prevchar_isitem = TRUE;
4200 t = nstr;
4201 if (opt == STL_VIRTCOL_ALT)
4202 {
4203 *t++ = '-';
4204 minwid--;
4205 }
4206 *t++ = '%';
4207 if (zeropad)
4208 *t++ = '0';
4209 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004210 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 *t = 0;
4212
4213 for (n = num, l = 1; n >= nbase; n /= nbase)
4214 l++;
4215 if (opt == STL_VIRTCOL_ALT)
4216 l++;
4217 if (l > maxwid)
4218 {
4219 l += 2;
4220 n = l - maxwid;
4221 while (l-- > maxwid)
4222 num /= nbase;
4223 *t++ = '>';
4224 *t++ = '%';
4225 *t = t[-3];
4226 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004227 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4228 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004231 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4232 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 p += STRLEN(p);
4234 }
4235 else
4236 item[curitem].type = Empty;
4237
4238 if (opt == STL_VIM_EXPR)
4239 vim_free(str);
4240
4241 if (num >= 0 || (!itemisflag && str && *str))
4242 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4243 curitem++;
4244 }
4245 *p = NUL;
4246 itemcnt = curitem;
4247
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004248#ifdef FEAT_EVAL
4249 if (usefmt != fmt)
4250 vim_free(usefmt);
4251#endif
4252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 width = vim_strsize(out);
4254 if (maxwidth > 0 && width > maxwidth)
4255 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004256 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 l = 0;
4258 if (itemcnt == 0)
4259 s = out;
4260 else
4261 {
4262 for ( ; l < itemcnt; l++)
4263 if (item[l].type == Trunc)
4264 {
4265 /* Truncate at %< item. */
4266 s = item[l].start;
4267 break;
4268 }
4269 if (l == itemcnt)
4270 {
4271 /* No %< item, truncate first item. */
4272 s = item[0].start;
4273 l = 0;
4274 }
4275 }
4276
4277 if (width - vim_strsize(s) >= maxwidth)
4278 {
4279 /* Truncation mark is beyond max length */
4280#ifdef FEAT_MBYTE
4281 if (has_mbyte)
4282 {
4283 s = out;
4284 width = 0;
4285 for (;;)
4286 {
4287 width += ptr2cells(s);
4288 if (width >= maxwidth)
4289 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004290 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 /* Fill up for half a double-wide character. */
4293 while (++width < maxwidth)
4294 *s++ = fillchar;
4295 }
4296 else
4297#endif
4298 s = out + maxwidth - 1;
4299 for (l = 0; l < itemcnt; l++)
4300 if (item[l].start > s)
4301 break;
4302 itemcnt = l;
4303 *s++ = '>';
4304 *s = 0;
4305 }
4306 else
4307 {
4308#ifdef FEAT_MBYTE
4309 if (has_mbyte)
4310 {
4311 n = 0;
4312 while (width >= maxwidth)
4313 {
4314 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004315 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 }
4318 else
4319#endif
4320 n = width - maxwidth + 1;
4321 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004322 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 *s = '<';
4324
4325 /* Fill up for half a double-wide character. */
4326 while (++width < maxwidth)
4327 {
4328 s = s + STRLEN(s);
4329 *s++ = fillchar;
4330 *s = NUL;
4331 }
4332
4333 --n; /* count the '<' */
4334 for (; l < itemcnt; l++)
4335 {
4336 if (item[l].start - n >= s)
4337 item[l].start -= n;
4338 else
4339 item[l].start = s;
4340 }
4341 }
4342 width = maxwidth;
4343 }
4344 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4345 {
4346 /* Apply STL_MIDDLE if any */
4347 for (l = 0; l < itemcnt; l++)
4348 if (item[l].type == Middle)
4349 break;
4350 if (l < itemcnt)
4351 {
4352 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004353 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 for (s = item[l].start; s < p; s++)
4355 *s = fillchar;
4356 for (l++; l < itemcnt; l++)
4357 item[l].start += maxwidth - width;
4358 width = maxwidth;
4359 }
4360 }
4361
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004362 /* Store the info about highlighting. */
4363 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004365 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 for (l = 0; l < itemcnt; l++)
4367 {
4368 if (item[l].type == Highlight)
4369 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004370 sp->start = item[l].start;
4371 sp->userhl = item[l].minwid;
4372 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004375 sp->start = NULL;
4376 sp->userhl = 0;
4377 }
4378
4379 /* Store the info about tab pages labels. */
4380 if (tabtab != NULL)
4381 {
4382 sp = tabtab;
4383 for (l = 0; l < itemcnt; l++)
4384 {
4385 if (item[l].type == TabPage)
4386 {
4387 sp->start = item[l].start;
4388 sp->userhl = item[l].minwid;
4389 sp++;
4390 }
4391 }
4392 sp->start = NULL;
4393 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395
4396 return width;
4397}
4398#endif /* FEAT_STL_OPT */
4399
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004400#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4401 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004403 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4404 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 */
4406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004407get_rel_pos(
4408 win_T *wp,
4409 char_u *buf,
4410 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
4412 long above; /* number of lines above window */
4413 long below; /* number of lines below window */
4414
Bram Moolenaar0027c212015-01-07 13:31:52 +01004415 if (buflen < 3) /* need at least 3 chars for writing */
4416 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 above = wp->w_topline - 1;
4418#ifdef FEAT_DIFF
4419 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004420 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4421 above = 0; /* All buffer lines are displayed and there is an
4422 * indication of filler lines, that can be considered
4423 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424#endif
4425 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4426 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004427 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4428 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004430 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004432 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 ? (int)(above / ((above + below) / 100L))
4434 : (int)(above * 100L / (above + below)));
4435}
4436#endif
4437
4438/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004439 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 * Return TRUE if it was appended.
4441 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004442 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004443append_arg_number(
4444 win_T *wp,
4445 char_u *buf,
4446 int buflen,
4447 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
4449 char_u *p;
4450
4451 if (ARGCOUNT <= 1) /* nothing to do */
4452 return FALSE;
4453
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004454 p = buf + STRLEN(buf); /* go to the end of the buffer */
4455 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 return FALSE;
4457 *p++ = ' ';
4458 *p++ = '(';
4459 if (add_file)
4460 {
4461 STRCPY(p, "file ");
4462 p += 5;
4463 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004464 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4465 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4467 return TRUE;
4468}
4469
4470/*
4471 * If fname is not a full path, make it a full path.
4472 * Returns pointer to allocated memory (NULL for failure).
4473 */
4474 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004475fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
4477 /*
4478 * Force expanding the path always for Unix, because symbolic links may
4479 * mess up the full path name, even though it starts with a '/'.
4480 * Also expand when there is ".." in the file name, try to remove it,
4481 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004482 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 * For MS-Windows also expand names like "longna~1" to "longname".
4484 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004485#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 return FullName_save(fname, TRUE);
4487#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004488 if (!vim_isAbsName(fname)
4489 || strstr((char *)fname, "..") != NULL
4490 || strstr((char *)fname, "//") != NULL
4491# ifdef BACKSLASH_IN_FILENAME
4492 || strstr((char *)fname, "\\\\") != NULL
4493# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004494# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 )
4498 return FullName_save(fname, FALSE);
4499
4500 fname = vim_strsave(fname);
4501
Bram Moolenaar9b942202007-10-03 12:31:33 +00004502# ifdef USE_FNAME_CASE
4503# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
4507 if (fname != NULL)
4508 fname_case(fname, 0); /* set correct case for file name */
4509 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
4512 return fname;
4513#endif
4514}
4515
4516/*
4517 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4518 * "ffname" becomes a pointer to allocated memory (or NULL).
4519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004521fname_expand(
4522 buf_T *buf UNUSED,
4523 char_u **ffname,
4524 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 if (*ffname == NULL) /* if no file name given, nothing to do */
4527 return;
4528 if (*sfname == NULL) /* if no short file name given, use ffname */
4529 *sfname = *ffname;
4530 *ffname = fix_fname(*ffname); /* expand to full path */
4531
4532#ifdef FEAT_SHORTCUT
4533 if (!buf->b_p_bin)
4534 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004535 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537 /* If the file name is a shortcut file, use the file it links to. */
4538 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004539 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 {
4541 vim_free(*ffname);
4542 *ffname = rfname;
4543 *sfname = rfname;
4544 }
4545 }
4546#endif
4547}
4548
4549/*
4550 * Get the file name for an argument list entry.
4551 */
4552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004553alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
4555 buf_T *bp;
4556
4557 /* Use the name from the associated buffer if it exists. */
4558 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004559 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 return aep->ae_fname;
4561 return bp->b_fname;
4562}
4563
4564#if defined(FEAT_WINDOWS) || defined(PROTO)
4565/*
4566 * do_arg_all(): Open up to 'count' windows, one for each argument.
4567 */
4568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004569do_arg_all(
4570 int count,
4571 int forceit, /* hide buffers in current windows */
4572 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573{
4574 int i;
4575 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004576 char_u *opened; /* Array of weight for which args are open:
4577 * 0: not opened
4578 * 1: opened in other tab
4579 * 2: opened in curtab
4580 * 3: opened in curtab and curwin
4581 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004582 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 int use_firstwin = FALSE; /* use first window for arglist */
4584 int split_ret = OK;
4585 int p_ea_save;
4586 alist_T *alist; /* argument list to be used */
4587 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004588 tabpage_T *tpnext;
4589 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004590 win_T *old_curwin, *last_curwin;
4591 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004592 win_T *new_curwin = NULL;
4593 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
4595 if (ARGCOUNT <= 0)
4596 {
4597 /* Don't give an error message. We don't want it when the ":all"
4598 * command is in the .vimrc. */
4599 return;
4600 }
4601 setpcmark();
4602
4603 opened_len = ARGCOUNT;
4604 opened = alloc_clear((unsigned)opened_len);
4605 if (opened == NULL)
4606 return;
4607
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004608 /* Autocommands may do anything to the argument list. Make sure it's not
4609 * freed while we are working here by "locking" it. We still have to
4610 * watch out for its size to be changed. */
4611 alist = curwin->w_alist;
4612 ++alist->al_refcount;
4613
4614 old_curwin = curwin;
4615 old_curtab = curtab;
4616
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004617# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 /*
4622 * Try closing all windows that are not in the argument list.
4623 * Also close windows that are not full width;
4624 * When 'hidden' or "forceit" set the buffer becomes hidden.
4625 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004626 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004628 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004629 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004630 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004632 tpnext = curtab->tp_next;
4633 for (wp = firstwin; wp != NULL; wp = wpnext)
4634 {
4635 wpnext = wp->w_next;
4636 buf = wp->w_buffer;
4637 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004638 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004639 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004640 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004643 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004644 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004646 if (i < alist->al_ga.ga_len
4647 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4648 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4649 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004651 int weight = 1;
4652
4653 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004654 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004655 ++weight;
4656 if (old_curwin == wp)
4657 ++weight;
4658 }
4659
4660 if (weight > (int)opened[i])
4661 {
4662 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004663 if (i == 0)
4664 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004665 if (new_curwin != NULL)
4666 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004667 new_curwin = wp;
4668 new_curtab = curtab;
4669 }
4670 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004671 else if (keep_tabs)
4672 i = opened_len;
4673
4674 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004675 {
4676 /* Use the current argument list for all windows
4677 * containing a file from it. */
4678 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004679 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004680 ++wp->w_alist->al_refcount;
4681 }
4682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
4685 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004686 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004688 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004690 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004691 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004693 /* If the buffer was changed, and we would like to hide it,
4694 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004695 if (!P_HID(buf) && buf->b_nwindows <= 1
4696 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004698 (void)autowrite(buf, FALSE);
4699#ifdef FEAT_AUTOCMD
4700 /* check if autocommands removed the window */
4701 if (!win_valid(wp) || !buf_valid(buf))
4702 {
4703 wpnext = firstwin; /* start all over... */
4704 continue;
4705 }
4706#endif
4707 }
4708#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004709 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004710 if (firstwin == lastwin
4711 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004712#endif
4713 use_firstwin = TRUE;
4714#ifdef FEAT_WINDOWS
4715 else
4716 {
4717 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4718# ifdef FEAT_AUTOCMD
4719 /* check if autocommands removed the next window */
4720 if (!win_valid(wpnext))
4721 wpnext = firstwin; /* start all over... */
4722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
4724#endif
4725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
4727 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004728
4729 /* Without the ":tab" modifier only do the current tab page. */
4730 if (had_tab == 0 || tpnext == NULL)
4731 break;
4732
4733# ifdef FEAT_AUTOCMD
4734 /* check if autocommands removed the next tab page */
4735 if (!valid_tabpage(tpnext))
4736 tpnext = first_tabpage; /* start all over...*/
4737# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004738 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
4740
4741 /*
4742 * Open a window for files in the argument list that don't have one.
4743 * ARGCOUNT may change while doing this, because of autocommands.
4744 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004745 if (count > opened_len || count <= 0)
4746 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748#ifdef FEAT_AUTOCMD
4749 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4750 ++autocmd_no_enter;
4751 ++autocmd_no_leave;
4752#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004753 last_curwin = curwin;
4754 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004756#ifdef FEAT_WINDOWS
4757 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4758 * leaving an empty tab page when executed locally. */
4759 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4760 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4761 use_firstwin = TRUE;
4762#endif
4763
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004764 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4767 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004768 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 /* Move the already present window to below the current window */
4771 if (curwin->w_arg_idx != i)
4772 {
4773 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4774 {
4775 if (wpnext->w_arg_idx == i)
4776 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004777 if (keep_tabs)
4778 {
4779 new_curwin = wpnext;
4780 new_curtab = curtab;
4781 }
4782 else
4783 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 break;
4785 }
4786 }
4787 }
4788 }
4789 else if (split_ret == OK)
4790 {
4791 if (!use_firstwin) /* split current window */
4792 {
4793 p_ea_save = p_ea;
4794 p_ea = TRUE; /* use space from all windows */
4795 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4796 p_ea = p_ea_save;
4797 if (split_ret == FAIL)
4798 continue;
4799 }
4800#ifdef FEAT_AUTOCMD
4801 else /* first window: do autocmd for leaving this buffer */
4802 --autocmd_no_leave;
4803#endif
4804
4805 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004806 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 */
4808 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004809 if (i == 0)
4810 {
4811 new_curwin = curwin;
4812 new_curtab = curtab;
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4815 ECMD_ONE,
4816 ((P_HID(curwin->w_buffer)
4817 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004818 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819#ifdef FEAT_AUTOCMD
4820 if (use_firstwin)
4821 ++autocmd_no_leave;
4822#endif
4823 use_firstwin = FALSE;
4824 }
4825 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004826
4827 /* When ":tab" was used open a new tab for a new window repeatedly. */
4828 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4829 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831
4832 /* Remove the "lock" on the argument list. */
4833 alist_unlink(alist);
4834
4835#ifdef FEAT_AUTOCMD
4836 --autocmd_no_enter;
4837#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004838 /* restore last referenced tabpage's curwin */
4839 if (last_curtab != new_curtab)
4840 {
4841 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004842 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004843 if (win_valid(last_curwin))
4844 win_enter(last_curwin, FALSE);
4845 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004846 /* to window with first arg */
4847 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004848 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004849 if (win_valid(new_curwin))
4850 win_enter(new_curwin, FALSE);
4851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#ifdef FEAT_AUTOCMD
4853 --autocmd_no_leave;
4854#endif
4855 vim_free(opened);
4856}
4857
4858# if defined(FEAT_LISTCMDS) || defined(PROTO)
4859/*
4860 * Open a window for a number of buffers.
4861 */
4862 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004863ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 buf_T *buf;
4866 win_T *wp, *wpnext;
4867 int split_ret = OK;
4868 int p_ea_save;
4869 int open_wins = 0;
4870 int r;
4871 int count; /* Maximum number of windows to open. */
4872 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004873#ifdef FEAT_WINDOWS
4874 int had_tab = cmdmod.tab;
4875 tabpage_T *tpnext;
4876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878 if (eap->addr_count == 0) /* make as many windows as possible */
4879 count = 9999;
4880 else
4881 count = eap->line2; /* make as many windows as specified */
4882 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4883 all = FALSE;
4884 else
4885 all = TRUE;
4886
4887 setpcmark();
4888
4889#ifdef FEAT_GUI
4890 need_mouse_correct = TRUE;
4891#endif
4892
4893 /*
4894 * Close superfluous windows (two windows for the same buffer).
4895 * Also close windows that are not full-width.
4896 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004897#ifdef FEAT_WINDOWS
4898 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004899 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004900 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004903 tpnext = curtab->tp_next;
4904 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004906 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004907 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004908#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004909 || ((cmdmod.split & WSP_VERT)
4910 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004911 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004912 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004913 || (had_tab > 0 && wp != firstwin)
4914#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004915 ) && firstwin != lastwin
4916#ifdef FEAT_AUTOCMD
4917 && !(wp->w_closing || wp->w_buffer->b_closing)
4918#endif
4919 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004920 {
4921 win_close(wp, FALSE);
4922#ifdef FEAT_AUTOCMD
4923 wpnext = firstwin; /* just in case an autocommand does
4924 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004925 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004926 open_wins = 0;
4927#endif
4928 }
4929 else
4930 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004932
4933#ifdef FEAT_WINDOWS
4934 /* Without the ":tab" modifier only do the current tab page. */
4935 if (had_tab == 0 || tpnext == NULL)
4936 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004937 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 /*
4942 * Go through the buffer list. When a buffer doesn't have a window yet,
4943 * open one. Otherwise move the window to the right position.
4944 * Watch out for autocommands that delete buffers or windows!
4945 */
4946#ifdef FEAT_AUTOCMD
4947 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4948 ++autocmd_no_enter;
4949#endif
4950 win_enter(lastwin, FALSE);
4951#ifdef FEAT_AUTOCMD
4952 ++autocmd_no_leave;
4953#endif
4954 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4955 {
4956 /* Check if this buffer needs a window */
4957 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4958 continue;
4959
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004960#ifdef FEAT_WINDOWS
4961 if (had_tab != 0)
4962 {
4963 /* With the ":tab" modifier don't move the window. */
4964 if (buf->b_nwindows > 0)
4965 wp = lastwin; /* buffer has a window, skip it */
4966 else
4967 wp = NULL;
4968 }
4969 else
4970#endif
4971 {
4972 /* Check if this buffer already has a window */
4973 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4974 if (wp->w_buffer == buf)
4975 break;
4976 /* If the buffer already has a window, move it */
4977 if (wp != NULL)
4978 win_move_after(wp, curwin);
4979 }
4980
4981 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 /* Split the window and put the buffer in it */
4984 p_ea_save = p_ea;
4985 p_ea = TRUE; /* use space from all windows */
4986 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4987 ++open_wins;
4988 p_ea = p_ea_save;
4989 if (split_ret == FAIL)
4990 continue;
4991
4992 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004993#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 swap_exists_action = SEA_DIALOG;
4995#endif
4996 set_curbuf(buf, DOBUF_GOTO);
4997#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00005000#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 break;
5004 }
5005#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005006#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 if (swap_exists_action == SEA_QUIT)
5008 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005009# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5010 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005011
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005012 /* Reset the error/interrupt/exception state here so that
5013 * aborting() returns FALSE when closing a window. */
5014 enter_cleanup(&cs);
5015# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005016
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005017 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 win_close(curwin, TRUE);
5019 --open_wins;
5020 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005021 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005022
5023# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5024 /* Restore the error/interrupt/exception state if not
5025 * discarded by a new aborting error, interrupt, or uncaught
5026 * exception. */
5027 leave_cleanup(&cs);
5028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030 else
5031 handle_swap_exists(NULL);
5032#endif
5033 }
5034
5035 ui_breakcheck();
5036 if (got_int)
5037 {
5038 (void)vgetc(); /* only break the file loading, not the rest */
5039 break;
5040 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005041#ifdef FEAT_EVAL
5042 /* Autocommands deleted the buffer or aborted script processing!!! */
5043 if (aborting())
5044 break;
5045#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005046#ifdef FEAT_WINDOWS
5047 /* When ":tab" was used open a new tab for a new window repeatedly. */
5048 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5049 cmdmod.tab = 9999;
5050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052#ifdef FEAT_AUTOCMD
5053 --autocmd_no_enter;
5054#endif
5055 win_enter(firstwin, FALSE); /* back to first window */
5056#ifdef FEAT_AUTOCMD
5057 --autocmd_no_leave;
5058#endif
5059
5060 /*
5061 * Close superfluous windows.
5062 */
5063 for (wp = lastwin; open_wins > count; )
5064 {
5065 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5066 || autowrite(wp->w_buffer, FALSE) == OK);
5067#ifdef FEAT_AUTOCMD
5068 if (!win_valid(wp))
5069 {
5070 /* BufWrite Autocommands made the window invalid, start over */
5071 wp = lastwin;
5072 }
5073 else
5074#endif
5075 if (r)
5076 {
5077 win_close(wp, !P_HID(wp->w_buffer));
5078 --open_wins;
5079 wp = lastwin;
5080 }
5081 else
5082 {
5083 wp = wp->w_prev;
5084 if (wp == NULL)
5085 break;
5086 }
5087 }
5088}
5089# endif /* FEAT_LISTCMDS */
5090
5091#endif /* FEAT_WINDOWS */
5092
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005093static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005094
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095/*
5096 * do_modelines() - process mode lines for the current file
5097 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005098 * "flags" can be:
5099 * OPT_WINONLY only set options local to window
5100 * OPT_NOWIN don't set options local to window
5101 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 * Returns immediately if the "ml" option isn't set.
5103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005105do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005107 linenr_T lnum;
5108 int nmlines;
5109 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
5111 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5112 return;
5113
5114 /* Disallow recursive entry here. Can happen when executing a modeline
5115 * triggers an autocommand, which reloads modelines with a ":do". */
5116 if (entered)
5117 return;
5118
5119 ++entered;
5120 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5121 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005122 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 nmlines = 0;
5124
5125 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5126 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005127 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 nmlines = 0;
5129 --entered;
5130}
5131
5132#include "version.h" /* for version number */
5133
5134/*
5135 * chk_modeline() - check a single line for a mode string
5136 * Return FAIL if an error encountered.
5137 */
5138 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005139chk_modeline(
5140 linenr_T lnum,
5141 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142{
5143 char_u *s;
5144 char_u *e;
5145 char_u *linecopy; /* local copy of any modeline found */
5146 int prev;
5147 int vers;
5148 int end;
5149 int retval = OK;
5150 char_u *save_sourcing_name;
5151 linenr_T save_sourcing_lnum;
5152#ifdef FEAT_EVAL
5153 scid_T save_SID;
5154#endif
5155
5156 prev = -1;
5157 for (s = ml_get(lnum); *s != NUL; ++s)
5158 {
5159 if (prev == -1 || vim_isspace(prev))
5160 {
5161 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5162 || STRNCMP(s, "vi:", (size_t)3) == 0)
5163 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005164 /* Accept both "vim" and "Vim". */
5165 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
5167 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5168 e = s + 4;
5169 else
5170 e = s + 3;
5171 vers = getdigits(&e);
5172 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005173 && (s[0] != 'V'
5174 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 && (s[3] == ':'
5176 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5177 || (VIM_VERSION_100 < vers && s[3] == '<')
5178 || (VIM_VERSION_100 > vers && s[3] == '>')
5179 || (VIM_VERSION_100 == vers && s[3] == '=')))
5180 break;
5181 }
5182 }
5183 prev = *s;
5184 }
5185
5186 if (*s)
5187 {
5188 do /* skip over "ex:", "vi:" or "vim:" */
5189 ++s;
5190 while (s[-1] != ':');
5191
5192 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5193 if (linecopy == NULL)
5194 return FAIL;
5195
5196 save_sourcing_lnum = sourcing_lnum;
5197 save_sourcing_name = sourcing_name;
5198 sourcing_lnum = lnum; /* prepare for emsg() */
5199 sourcing_name = (char_u *)"modelines";
5200
5201 end = FALSE;
5202 while (end == FALSE)
5203 {
5204 s = skipwhite(s);
5205 if (*s == NUL)
5206 break;
5207
5208 /*
5209 * Find end of set command: ':' or end of line.
5210 * Skip over "\:", replacing it with ":".
5211 */
5212 for (e = s; *e != ':' && *e != NUL; ++e)
5213 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005214 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (*e == NUL)
5216 end = TRUE;
5217
5218 /*
5219 * If there is a "set" command, require a terminating ':' and
5220 * ignore the stuff after the ':'.
5221 * "vi:set opt opt opt: foo" -- foo not interpreted
5222 * "vi:opt opt opt: foo" -- foo interpreted
5223 * Accept "se" for compatibility with Elvis.
5224 */
5225 if (STRNCMP(s, "set ", (size_t)4) == 0
5226 || STRNCMP(s, "se ", (size_t)3) == 0)
5227 {
5228 if (*e != ':') /* no terminating ':'? */
5229 break;
5230 end = TRUE;
5231 s = vim_strchr(s, ' ') + 1;
5232 }
5233 *e = NUL; /* truncate the set command */
5234
5235 if (*s != NUL) /* skip over an empty "::" */
5236 {
5237#ifdef FEAT_EVAL
5238 save_SID = current_SID;
5239 current_SID = SID_MODELINE;
5240#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005241 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#ifdef FEAT_EVAL
5243 current_SID = save_SID;
5244#endif
5245 if (retval == FAIL) /* stop if error found */
5246 break;
5247 }
5248 s = e + 1; /* advance to next part */
5249 }
5250
5251 sourcing_lnum = save_sourcing_lnum;
5252 sourcing_name = save_sourcing_name;
5253
5254 vim_free(linecopy);
5255 }
5256 return retval;
5257}
5258
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005259#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005261read_viminfo_bufferlist(
5262 vir_T *virp,
5263 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
5265 char_u *tab;
5266 linenr_T lnum;
5267 colnr_T col;
5268 buf_T *buf;
5269 char_u *sfname;
5270 char_u *xline;
5271
5272 /* Handle long line and escaped characters. */
5273 xline = viminfo_readstring(virp, 1, FALSE);
5274
5275 /* don't read in if there are files on the command-line or if writing: */
5276 if (xline != NULL && !writing && ARGCOUNT == 0
5277 && find_viminfo_parameter('%') != NULL)
5278 {
5279 /* Format is: <fname> Tab <lnum> Tab <col>.
5280 * Watch out for a Tab in the file name, work from the end. */
5281 lnum = 0;
5282 col = 0;
5283 tab = vim_strrchr(xline, '\t');
5284 if (tab != NULL)
5285 {
5286 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005287 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 tab = vim_strrchr(xline, '\t');
5289 if (tab != NULL)
5290 {
5291 *tab++ = '\0';
5292 lnum = atol((char *)tab);
5293 }
5294 }
5295
5296 /* Expand "~/" in the file name at "line + 1" to a full path.
5297 * Then try shortening it by comparing with the current directory */
5298 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005299 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5302 if (buf != NULL) /* just in case... */
5303 {
5304 buf->b_last_cursor.lnum = lnum;
5305 buf->b_last_cursor.col = col;
5306 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5307 }
5308 }
5309 vim_free(xline);
5310
5311 return viminfo_readline(virp);
5312}
5313
5314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005315write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 buf_T *buf;
5318#ifdef FEAT_WINDOWS
5319 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005320 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#endif
5322 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005323 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
5325 if (find_viminfo_parameter('%') == NULL)
5326 return;
5327
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005328 /* Without a number -1 is returned: do all buffers. */
5329 max_buffers = get_viminfo_parameter('%');
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005332#define LINE_BUF_LEN (MAXPATHL + 40)
5333 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (line == NULL)
5335 return;
5336
5337#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005338 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 set_last_cursor(win);
5340#else
5341 set_last_cursor(curwin);
5342#endif
5343
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005344 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5346 {
5347 if (buf->b_fname == NULL
5348 || !buf->b_p_bl
5349#ifdef FEAT_QUICKFIX
5350 || bt_quickfix(buf)
5351#endif
5352 || removable(buf->b_ffname))
5353 continue;
5354
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005355 if (max_buffers-- == 0)
5356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 putc('%', fp);
5358 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005359 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 (long)buf->b_last_cursor.lnum,
5361 buf->b_last_cursor.col);
5362 viminfo_writestring(fp, line);
5363 }
5364 vim_free(line);
5365}
5366#endif
5367
5368
5369/*
5370 * Return special buffer name.
5371 * Returns NULL when the buffer has a normal file name.
5372 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005373 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005374buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
5376#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5377 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005378 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005379 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005380 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005381
5382 /*
5383 * For location list window, w_llist_ref points to the location list.
5384 * For quickfix window, w_llist_ref is NULL.
5385 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005386 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005387 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005388 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005389 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391#endif
5392#ifdef FEAT_QUICKFIX
5393 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5394 * contains the name as specified by the user */
5395 if (bt_nofile(buf))
5396 {
5397 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005398 return buf->b_sfname;
5399 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
5401#endif
5402 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005403 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 return NULL;
5405}
5406
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005407#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5408 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5409 || defined(PROTO)
5410/*
5411 * Find a window for buffer "buf".
5412 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5413 * If not found FAIL is returned.
5414 */
5415 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005416find_win_for_buf(
5417 buf_T *buf,
5418 win_T **wp,
5419 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005420{
5421 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5422 if ((*wp)->w_buffer == buf)
5423 goto win_found;
5424 return FAIL;
5425win_found:
5426 return OK;
5427}
5428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429
5430#if defined(FEAT_SIGNS) || defined(PROTO)
5431/*
5432 * Insert the sign into the signlist.
5433 */
5434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005435insert_sign(
5436 buf_T *buf, /* buffer to store sign in */
5437 signlist_T *prev, /* previous sign entry */
5438 signlist_T *next, /* next sign entry */
5439 int id, /* sign ID */
5440 linenr_T lnum, /* line number which gets the mark */
5441 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 signlist_T *newsign;
5444
5445 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5446 if (newsign != NULL)
5447 {
5448 newsign->id = id;
5449 newsign->lnum = lnum;
5450 newsign->typenr = typenr;
5451 newsign->next = next;
5452#ifdef FEAT_NETBEANS_INTG
5453 newsign->prev = prev;
5454 if (next != NULL)
5455 next->prev = newsign;
5456#endif
5457
5458 if (prev == NULL)
5459 {
5460 /* When adding first sign need to redraw the windows to create the
5461 * column for signs. */
5462 if (buf->b_signlist == NULL)
5463 {
5464 redraw_buf_later(buf, NOT_VALID);
5465 changed_cline_bef_curs();
5466 }
5467
5468 /* first sign in signlist */
5469 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005470#ifdef FEAT_NETBEANS_INTG
5471 if (netbeans_active())
5472 buf->b_has_sign_column = TRUE;
5473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
5475 else
5476 prev->next = newsign;
5477 }
5478}
5479
5480/*
5481 * Add the sign into the signlist. Find the right spot to do it though.
5482 */
5483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005484buf_addsign(
5485 buf_T *buf, /* buffer to store sign in */
5486 int id, /* sign ID */
5487 linenr_T lnum, /* line number which gets the mark */
5488 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 signlist_T *sign; /* a sign in the signlist */
5491 signlist_T *prev; /* the previous sign */
5492
5493 prev = NULL;
5494 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5495 {
5496 if (lnum == sign->lnum && id == sign->id)
5497 {
5498 sign->typenr = typenr;
5499 return;
5500 }
5501 else if (
5502#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5503 id < 0 &&
5504#endif
5505 lnum < sign->lnum)
5506 {
5507#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5508 /* XXX - GRP: Is this because of sign slide problem? Or is it
5509 * really needed? Or is it because we allow multiple signs per
5510 * line? If so, should I add that feature to FEAT_SIGNS?
5511 */
5512 while (prev != NULL && prev->lnum == lnum)
5513 prev = prev->prev;
5514 if (prev == NULL)
5515 sign = buf->b_signlist;
5516 else
5517 sign = prev->next;
5518#endif
5519 insert_sign(buf, prev, sign, id, lnum, typenr);
5520 return;
5521 }
5522 prev = sign;
5523 }
5524#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5525 /* XXX - GRP: See previous comment */
5526 while (prev != NULL && prev->lnum == lnum)
5527 prev = prev->prev;
5528 if (prev == NULL)
5529 sign = buf->b_signlist;
5530 else
5531 sign = prev->next;
5532#endif
5533 insert_sign(buf, prev, sign, id, lnum, typenr);
5534
5535 return;
5536}
5537
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005538/*
5539 * For an existing, placed sign "markId" change the type to "typenr".
5540 * Returns the line number of the sign, or zero if the sign is not found.
5541 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005542 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005543buf_change_sign_type(
5544 buf_T *buf, /* buffer to store sign in */
5545 int markId, /* sign ID */
5546 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 signlist_T *sign; /* a sign in the signlist */
5549
5550 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5551 {
5552 if (sign->id == markId)
5553 {
5554 sign->typenr = typenr;
5555 return sign->lnum;
5556 }
5557 }
5558
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005559 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560}
5561
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005563buf_getsigntype(
5564 buf_T *buf,
5565 linenr_T lnum,
5566 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568 signlist_T *sign; /* a sign in a b_signlist */
5569
5570 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5571 if (sign->lnum == lnum
5572 && (type == SIGN_ANY
5573# ifdef FEAT_SIGN_ICONS
5574 || (type == SIGN_ICON
5575 && sign_get_image(sign->typenr) != NULL)
5576# endif
5577 || (type == SIGN_TEXT
5578 && sign_get_text(sign->typenr) != NULL)
5579 || (type == SIGN_LINEHL
5580 && sign_get_attr(sign->typenr, TRUE) != 0)))
5581 return sign->typenr;
5582 return 0;
5583}
5584
5585
5586 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005587buf_delsign(
5588 buf_T *buf, /* buffer sign is stored in */
5589 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 signlist_T **lastp; /* pointer to pointer to current sign */
5592 signlist_T *sign; /* a sign in a b_signlist */
5593 signlist_T *next; /* the next sign in a b_signlist */
5594 linenr_T lnum; /* line number whose sign was deleted */
5595
5596 lastp = &buf->b_signlist;
5597 lnum = 0;
5598 for (sign = buf->b_signlist; sign != NULL; sign = next)
5599 {
5600 next = sign->next;
5601 if (sign->id == id)
5602 {
5603 *lastp = next;
5604#ifdef FEAT_NETBEANS_INTG
5605 if (next != NULL)
5606 next->prev = sign->prev;
5607#endif
5608 lnum = sign->lnum;
5609 vim_free(sign);
5610 break;
5611 }
5612 else
5613 lastp = &sign->next;
5614 }
5615
5616 /* When deleted the last sign need to redraw the windows to remove the
5617 * sign column. */
5618 if (buf->b_signlist == NULL)
5619 {
5620 redraw_buf_later(buf, NOT_VALID);
5621 changed_cline_bef_curs();
5622 }
5623
5624 return lnum;
5625}
5626
5627
5628/*
5629 * Find the line number of the sign with the requested id. If the sign does
5630 * not exist, return 0 as the line number. This will still let the correct file
5631 * get loaded.
5632 */
5633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005634buf_findsign(
5635 buf_T *buf, /* buffer to store sign in */
5636 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 signlist_T *sign; /* a sign in the signlist */
5639
5640 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5641 if (sign->id == id)
5642 return sign->lnum;
5643
5644 return 0;
5645}
5646
5647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005648buf_findsign_id(
5649 buf_T *buf, /* buffer whose sign we are searching for */
5650 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
5652 signlist_T *sign; /* a sign in the signlist */
5653
5654 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5655 if (sign->lnum == lnum)
5656 return sign->id;
5657
5658 return 0;
5659}
5660
5661
5662# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5663/* see if a given type of sign exists on a specific line */
5664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005665buf_findsigntype_id(
5666 buf_T *buf, /* buffer whose sign we are searching for */
5667 linenr_T lnum, /* line number of sign */
5668 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670 signlist_T *sign; /* a sign in the signlist */
5671
5672 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5673 if (sign->lnum == lnum && sign->typenr == typenr)
5674 return sign->id;
5675
5676 return 0;
5677}
5678
5679
5680# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5681/* return the number of icons on the given line */
5682 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005683buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
5685 signlist_T *sign; /* a sign in the signlist */
5686 int count = 0;
5687
5688 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5689 if (sign->lnum == lnum)
5690 if (sign_get_image(sign->typenr) != NULL)
5691 count++;
5692
5693 return count;
5694}
5695# endif /* FEAT_SIGN_ICONS */
5696# endif /* FEAT_NETBEANS_INTG */
5697
5698
5699/*
5700 * Delete signs in buffer "buf".
5701 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005703buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
5705 signlist_T *next;
5706
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005707 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005708 * sign column. Not when curwin is NULL (this means we're exiting). */
5709 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005710 {
5711 redraw_buf_later(buf, NOT_VALID);
5712 changed_cline_bef_curs();
5713 }
5714
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 while (buf->b_signlist != NULL)
5716 {
5717 next = buf->b_signlist->next;
5718 vim_free(buf->b_signlist);
5719 buf->b_signlist = next;
5720 }
5721}
5722
5723/*
5724 * Delete all signs in all buffers.
5725 */
5726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005727buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729 buf_T *buf; /* buffer we are checking for signs */
5730
5731 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5732 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734}
5735
5736/*
5737 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5738 */
5739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005740sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 buf_T *buf;
5743 signlist_T *p;
5744 char lbuf[BUFSIZ];
5745
5746 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5747 msg_putchar('\n');
5748 if (rbuf == NULL)
5749 buf = firstbuf;
5750 else
5751 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005752 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 if (buf->b_signlist != NULL)
5755 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005756 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5758 msg_putchar('\n');
5759 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005760 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005762 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5764 MSG_PUTS(lbuf);
5765 msg_putchar('\n');
5766 }
5767 if (rbuf != NULL)
5768 break;
5769 buf = buf->b_next;
5770 }
5771}
5772
5773/*
5774 * Adjust a placed sign for inserted/deleted lines.
5775 */
5776 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005777sign_mark_adjust(
5778 linenr_T line1,
5779 linenr_T line2,
5780 long amount,
5781 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
5783 signlist_T *sign; /* a sign in a b_signlist */
5784
5785 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5786 {
5787 if (sign->lnum >= line1 && sign->lnum <= line2)
5788 {
5789 if (amount == MAXLNUM)
5790 sign->lnum = line1;
5791 else
5792 sign->lnum += amount;
5793 }
5794 else if (sign->lnum > line2)
5795 sign->lnum += amount_after;
5796 }
5797}
5798#endif /* FEAT_SIGNS */
5799
5800/*
5801 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5802 */
5803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005804set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 if (on != curbuf->b_p_bl)
5807 {
5808 curbuf->b_p_bl = on;
5809#ifdef FEAT_AUTOCMD
5810 if (on)
5811 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5812 else
5813 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5814#endif
5815 }
5816}
5817
5818/*
5819 * Read the file for "buf" again and check if the contents changed.
5820 * Return TRUE if it changed or this could not be checked.
5821 */
5822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005823buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 buf_T *newbuf;
5826 int differ = TRUE;
5827 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 exarg_T ea;
5830
5831 /* Allocate a buffer without putting it in the buffer list. */
5832 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5833 if (newbuf == NULL)
5834 return TRUE;
5835
5836 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5837 if (prep_exarg(&ea, buf) == FAIL)
5838 {
5839 wipe_buffer(newbuf, FALSE);
5840 return TRUE;
5841 }
5842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 /* set curwin/curbuf to buf and save a few things */
5844 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
Bram Moolenaar4770d092006-01-12 23:22:24 +00005846 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 && readfile(buf->b_ffname, buf->b_fname,
5848 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5849 &ea, READ_NEW | READ_DUMMY) == OK)
5850 {
5851 /* compare the two files line by line */
5852 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5853 {
5854 differ = FALSE;
5855 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5856 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5857 {
5858 differ = TRUE;
5859 break;
5860 }
5861 }
5862 }
5863 vim_free(ea.cmd);
5864
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 /* restore curwin/curbuf and a few other things */
5866 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868 if (curbuf != newbuf) /* safety check */
5869 wipe_buffer(newbuf, FALSE);
5870
5871 return differ;
5872}
5873
5874/*
5875 * Wipe out a buffer and decrement the last buffer number if it was used for
5876 * this buffer. Call this to wipe out a temp buffer that does not contain any
5877 * marks.
5878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005880wipe_buffer(
5881 buf_T *buf,
5882 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 if (buf->b_fnum == top_file_num - 1)
5885 --top_file_num;
5886
5887#ifdef FEAT_AUTOCMD
5888 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005889 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005891 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#ifdef FEAT_AUTOCMD
5893 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005894 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#endif
5896}