blob: 586ab3848ca04f30258c6c78c5b70ccddcc1332b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
36static wininfo_T *find_wininfo(buf_T *buf, int skip_diff_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020038static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
40static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
48static void free_buffer(buf_T *);
49static void free_buffer_stuff(buf_T *buf, int free_options);
50static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar7fd73202010-07-25 16:58:46 +020062#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
63static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066#ifdef FEAT_AUTOCMD
67static char *e_auabort = N_("E855: Autocommands caused command to abort");
68#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram Moolenaar071d4272004-06-13 20:20:40 +000070/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020071 * Open current buffer, that is: open the memfile and read the file into
72 * memory.
73 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
75 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010076open_buffer(
77 int read_stdin, /* read file from stdin */
78 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
79 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080{
81 int retval = OK;
82#ifdef FEAT_AUTOCMD
83 buf_T *old_curbuf;
84#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +010085#ifdef FEAT_SYN_HL
86 long old_tw = curbuf->b_p_tw;
87#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89 /*
90 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
91 * When re-entering the same buffer, it should not change, because the
92 * user may have reset the flag by hand.
93 */
94 if (readonlymode && curbuf->b_ffname != NULL
95 && (curbuf->b_flags & BF_NEVERLOADED))
96 curbuf->b_p_ro = TRUE;
97
Bram Moolenaar4770d092006-01-12 23:22:24 +000098 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 {
100 /*
101 * There MUST be a memfile, otherwise we can't do anything
102 * If we can't create one for the current buffer, take another buffer
103 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100104 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
106 if (curbuf->b_ml.ml_mfp != NULL)
107 break;
108 /*
109 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000110 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 */
112 if (curbuf == NULL)
113 {
114 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
115 getout(2);
116 }
117 EMSG(_("E83: Cannot allocate buffer, using other one..."));
118 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100119#ifdef FEAT_SYN_HL
120 if (old_tw != curbuf->b_p_tw)
121 check_colorcolumn(curwin);
122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 return FAIL;
124 }
125
126#ifdef FEAT_AUTOCMD
127 /* The autocommands in readfile() may change the buffer, but only AFTER
128 * reading the file. */
129 old_curbuf = curbuf;
130 modified_was_set = FALSE;
131#endif
132
133 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100134 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136 if (curbuf->b_ffname != NULL
137#ifdef FEAT_NETBEANS_INTG
138 && netbeansReadFile
139#endif
140 )
141 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100142 int old_msg_silent = msg_silent;
143
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_NETBEANS_INTG
145 int oldFire = netbeansFireChanges;
146
147 netbeansFireChanges = 0;
148#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100149 if (shortmess(SHM_FILEINFO))
150 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200152 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
153 flags | READ_NEW);
Bram Moolenaar426dd022016-03-15 15:09:29 +0100154 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#ifdef FEAT_NETBEANS_INTG
156 netbeansFireChanges = oldFire;
157#endif
158 /* Help buffer is filtered. */
159 if (curbuf->b_help)
160 fix_help_buffer();
161 }
162 else if (read_stdin)
163 {
164 int save_bin = curbuf->b_p_bin;
165 linenr_T line_count;
166
167 /*
168 * First read the text in binary mode into the buffer.
169 * Then read from that same buffer and append at the end. This makes
170 * it possible to retry when 'fileformat' or 'fileencoding' was
171 * guessed wrong.
172 */
173 curbuf->b_p_bin = TRUE;
174 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200175 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
176 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 curbuf->b_p_bin = save_bin;
178 if (retval == OK)
179 {
180 line_count = curbuf->b_ml.ml_line_count;
181 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200182 (linenr_T)0, (linenr_T)MAXLNUM, eap,
183 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 if (retval == OK)
185 {
186 /* Delete the binary lines. */
187 while (--line_count >= 0)
188 ml_delete((linenr_T)1, FALSE);
189 }
190 else
191 {
192 /* Delete the converted lines. */
193 while (curbuf->b_ml.ml_line_count > line_count)
194 ml_delete(line_count, FALSE);
195 }
196 /* Put the cursor on the first line. */
197 curwin->w_cursor.lnum = 1;
198 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000199
200 /* Set or reset 'modified' before executing autocommands, so that
201 * it can be changed there. */
202 if (!readonlymode && !bufempty())
203 changed();
204 else if (retval != FAIL)
205 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifdef FEAT_AUTOCMD
207# ifdef FEAT_EVAL
208 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
209 curbuf, &retval);
210# else
211 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
212# endif
213#endif
214 }
215 }
216
217 /* if first time loading this buffer, init b_chartab[] */
218 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100221#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100222 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100223#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
226 /*
227 * Set/reset the Changed flag first, autocmds may change the buffer.
228 * Apply the automatic commands, before processing the modelines.
229 * So the modelines have priority over auto commands.
230 */
231 /* When reading stdin, the buffer contents always needs writing, so set
232 * the changed flag. Unless in readonly mode: "ls | gview -".
233 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000234 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#ifdef FEAT_AUTOCMD
236 || modified_was_set /* ":set modified" used in autocmd */
237# ifdef FEAT_EVAL
238 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
239# endif
240#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000241 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000243 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 unchanged(curbuf, FALSE);
245 save_file_ff(curbuf); /* keep this fileformat */
246
247 /* require "!" to overwrite the file, because it wasn't read completely */
248#ifdef FEAT_EVAL
249 if (aborting())
250#else
251 if (got_int)
252#endif
253 curbuf->b_flags |= BF_READERR;
254
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000255#ifdef FEAT_FOLDING
256 /* Need to update automatic folding. Do this before the autocommands,
257 * they may use the fold info. */
258 foldUpdateAll(curwin);
259#endif
260
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#ifdef FEAT_AUTOCMD
262 /* need to set w_topline, unless some autocommand already did that. */
263 if (!(curwin->w_valid & VALID_TOPLINE))
264 {
265 curwin->w_topline = 1;
266# ifdef FEAT_DIFF
267 curwin->w_topfill = 0;
268# endif
269 }
270# ifdef FEAT_EVAL
271 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
272# else
273 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
274# endif
275#endif
276
277 if (retval != FAIL)
278 {
279#ifdef FEAT_AUTOCMD
280 /*
281 * The autocommands may have changed the current buffer. Apply the
282 * modelines to the correct buffer, if it still exists and is loaded.
283 */
284 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
285 {
286 aco_save_T aco;
287
288 /* Go to the buffer that was opened. */
289 aucmd_prepbuf(&aco, old_curbuf);
290#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000291 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
293
294#ifdef FEAT_AUTOCMD
295# ifdef FEAT_EVAL
296 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
297 &retval);
298# else
299 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
300# endif
301
302 /* restore curwin/curbuf and a few other things */
303 aucmd_restbuf(&aco);
304 }
305#endif
306 }
307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 return retval;
309}
310
311/*
312 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
313 */
314 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100315buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316{
317 buf_T *bp;
318
Bram Moolenaar82404332016-07-10 17:00:38 +0200319 /* Assume that we more often have a recent buffer, start with the last
320 * one. */
321 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 if (bp == buf)
323 return TRUE;
324 return FALSE;
325}
326
327/*
328 * Close the link to a buffer.
329 * "action" is used when there is no longer a window for the buffer.
330 * It can be:
331 * 0 buffer becomes hidden
332 * DOBUF_UNLOAD buffer is unloaded
333 * DOBUF_DELETE buffer is unloaded and removed from buffer list
334 * DOBUF_WIPE buffer is unloaded and really deleted
335 * When doing all but the first one on the current buffer, the caller should
336 * get a new buffer very soon!
337 *
338 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100339 *
340 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
341 * cause there to be only one window with this buffer. e.g. when ":quit" is
342 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 */
344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100345close_buffer(
346 win_T *win, /* if not NULL, set b_last_cursor */
347 buf_T *buf,
348 int action,
349 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350{
351#ifdef FEAT_AUTOCMD
352 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100353 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#endif
355 int unload_buf = (action != 0);
356 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
357 int wipe_buf = (action == DOBUF_WIPE);
358
359#ifdef FEAT_QUICKFIX
360 /*
361 * Force unloading or deleting when 'bufhidden' says so.
362 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
363 * "hide" (otherwise we could never free or delete a buffer).
364 */
365 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
366 {
367 del_buf = TRUE;
368 unload_buf = TRUE;
369 }
370 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
371 {
372 del_buf = TRUE;
373 unload_buf = TRUE;
374 wipe_buf = TRUE;
375 }
376 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
377 unload_buf = TRUE;
378#endif
379
Bram Moolenaar3be85852014-06-12 14:01:31 +0200380 if (win != NULL
381#ifdef FEAT_WINDOWS
382 && win_valid(win) /* in case autocommands closed the window */
383#endif
384 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {
386 /* Set b_last_cursor when closing the last window for the buffer.
387 * Remember the last cursor position and window options of the buffer.
388 * This used to be only for the current window, but then options like
389 * 'foldmethod' may be lost with a ":only" command. */
390 if (buf->b_nwindows == 1)
391 set_last_cursor(win);
392 buflist_setfpos(buf, win,
393 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
394 win->w_cursor.col, TRUE);
395 }
396
397#ifdef FEAT_AUTOCMD
398 /* When the buffer is no longer in a window, trigger BufWinLeave */
399 if (buf->b_nwindows == 1)
400 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200401 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200402 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
403 FALSE, buf)
404 && !buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100405 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200406 /* Autocommands deleted the buffer. */
407aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100408 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100410 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200411 buf->b_closing = FALSE;
412 if (abort_if_last && one_window())
413 /* Autocommands made this the only window. */
414 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
416 /* When the buffer becomes hidden, but is not unloaded, trigger
417 * BufHidden */
418 if (!unload_buf)
419 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200420 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200421 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
422 FALSE, buf)
423 && !buf_valid(buf))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200424 /* Autocommands deleted the buffer. */
425 goto aucmd_abort;
426 buf->b_closing = FALSE;
427 if (abort_if_last && one_window())
428 /* Autocommands made this the only window. */
429 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 }
431# ifdef FEAT_EVAL
432 if (aborting()) /* autocmds may abort script processing */
433 return;
434# endif
435 }
436 nwindows = buf->b_nwindows;
437#endif
438
439 /* decrease the link count from windows (unless not in any window) */
440 if (buf->b_nwindows > 0)
441 --buf->b_nwindows;
442
443 /* Return when a window is displaying the buffer or when it's not
444 * unloaded. */
445 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
448 /* Always remove the buffer when there is no file name. */
449 if (buf->b_ffname == NULL)
450 del_buf = TRUE;
451
452 /*
453 * Free all things allocated for this buffer.
454 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
455 */
456#ifdef FEAT_AUTOCMD
457 /* Remember if we are closing the current buffer. Restore the number of
458 * windows, so that autocommands in buf_freeall() don't get confused. */
459 is_curbuf = (buf == curbuf);
460 buf->b_nwindows = nwindows;
461#endif
462
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200463 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
465#ifdef FEAT_AUTOCMD
466 /* Autocommands may have deleted the buffer. */
467 if (!buf_valid(buf))
468 return;
469# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000470 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 return;
472# endif
473
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 /*
475 * It's possible that autocommands change curbuf to the one being deleted.
476 * This might cause the previous curbuf to be deleted unexpectedly. But
477 * in some cases it's OK to delete the curbuf, because a new one is
478 * obtained anyway. Therefore only return if curbuf changed to the
479 * deleted buffer.
480 */
481 if (buf == curbuf && !is_curbuf)
482 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200483
484 if (
485#ifdef FEAT_WINDOWS
486 win_valid(win) &&
487#else
488 win != NULL &&
489#endif
490 win->w_buffer == buf)
491 win->w_buffer = NULL; /* make sure we don't use the buffer now */
492
493 /* Autocommands may have opened or closed windows for this buffer.
494 * Decrement the count for the close we do here. */
495 if (buf->b_nwindows > 0)
496 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497#endif
498
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000499 /* Change directories when the 'acd' option is set. */
500 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
502 /*
503 * Remove the buffer from the list.
504 */
505 if (wipe_buf)
506 {
507#ifdef FEAT_SUN_WORKSHOP
508 if (usingSunWorkShop)
509 workshop_file_closed_lineno((char *)buf->b_ffname,
510 (int)buf->b_last_cursor.lnum);
511#endif
512 vim_free(buf->b_ffname);
513 vim_free(buf->b_sfname);
514 if (buf->b_prev == NULL)
515 firstbuf = buf->b_next;
516 else
517 buf->b_prev->b_next = buf->b_next;
518 if (buf->b_next == NULL)
519 lastbuf = buf->b_prev;
520 else
521 buf->b_next->b_prev = buf->b_prev;
522 free_buffer(buf);
523 }
524 else
525 {
526 if (del_buf)
527 {
528 /* Free all internal variables and reset option values, to make
529 * ":bdel" compatible with Vim 5.7. */
530 free_buffer_stuff(buf, TRUE);
531
532 /* Make it look like a new buffer. */
533 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
534
535 /* Init the options when loaded again. */
536 buf->b_p_initialized = FALSE;
537 }
538 buf_clear_file(buf);
539 if (del_buf)
540 buf->b_p_bl = FALSE;
541 }
542}
543
544/*
545 * Make buffer not contain a file.
546 */
547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100548buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 buf->b_ml.ml_line_count = 1;
551 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 buf->b_p_eol = TRUE;
554 buf->b_start_eol = TRUE;
555#ifdef FEAT_MBYTE
556 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000557 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559 buf->b_ml.ml_mfp = NULL;
560 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
561#ifdef FEAT_NETBEANS_INTG
562 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#endif
564}
565
566/*
567 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200568 * the file. flags:
569 * BFA_DEL buffer is going to be deleted
570 * BFA_WIPE buffer is going to be wiped out
571 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100574buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
576#ifdef FEAT_AUTOCMD
577 int is_curbuf = (buf == curbuf);
578
Bram Moolenaar362ce482012-06-06 19:02:45 +0200579 buf->b_closing = TRUE;
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200580 if (buf->b_ml.ml_mfp != NULL)
581 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200582 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
583 FALSE, buf)
584 && !buf_valid(buf)) /* autocommands may delete the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200585 return;
586 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200587 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200589 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
590 FALSE, buf)
591 && !buf_valid(buf)) /* autocommands may delete the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 return;
593 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200594 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200596 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
597 FALSE, buf)
598 && !buf_valid(buf)) /* autocommands may delete the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 return;
600 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200601 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000603 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 return;
605# endif
606
607 /*
608 * It's possible that autocommands change curbuf to the one being deleted.
609 * This might cause curbuf to be deleted unexpectedly. But in some cases
610 * it's OK to delete the curbuf, because a new one is obtained anyway.
611 * Therefore only return if curbuf changed to the deleted buffer.
612 */
613 if (buf == curbuf && !is_curbuf)
614 return;
615#endif
616#ifdef FEAT_DIFF
617 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
618#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200619#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100620 /* Remove any ownsyntax, unless exiting. */
621 if (firstwin != NULL && curwin->w_buffer == buf)
622 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200623#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000624
625#ifdef FEAT_FOLDING
626 /* No folds in an empty buffer. */
627# ifdef FEAT_WINDOWS
628 {
629 win_T *win;
630 tabpage_T *tp;
631
632 FOR_ALL_TAB_WINDOWS(tp, win)
633 if (win->w_buffer == buf)
634 clearFolding(win);
635 }
636# else
637 if (curwin->w_buffer == buf)
638 clearFolding(curwin);
639# endif
640#endif
641
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#ifdef FEAT_TCL
643 tcl_buffer_free(buf);
644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 ml_close(buf, TRUE); /* close and delete the memline/memfile */
646 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200647 if ((flags & BFA_KEEP_UNDO) == 0)
648 {
649 u_blockfree(buf); /* free the memory allocated for undo */
650 u_clearall(buf); /* reset all undo information */
651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200653 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000655 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656}
657
658/*
659 * Free a buffer structure and the things it contains related to the buffer
660 * itself (not the file, that must have been done already).
661 */
662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100663free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664{
665 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200666#ifdef FEAT_EVAL
667 unref_var_dict(buf->b_vars);
668#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200669#ifdef FEAT_LUA
670 lua_buffer_free(buf);
671#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000672#ifdef FEAT_MZSCHEME
673 mzscheme_buffer_free(buf);
674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_PERL
676 perl_buf_free(buf);
677#endif
678#ifdef FEAT_PYTHON
679 python_buffer_free(buf);
680#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200681#ifdef FEAT_PYTHON3
682 python3_buffer_free(buf);
683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684#ifdef FEAT_RUBY
685 ruby_buffer_free(buf);
686#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200687#ifdef FEAT_JOB_CHANNEL
688 channel_buffer_free(buf);
689#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000690#ifdef FEAT_AUTOCMD
691 aubuflocal_remove(buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200692 if (autocmd_busy)
693 {
694 /* Do not free the buffer structure while autocommands are executing,
695 * it's still needed. Free it when autocmd_busy is reset. */
696 buf->b_next = au_pending_free_buf;
697 au_pending_free_buf = buf;
698 }
699 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000700#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200701 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702}
703
704/*
705 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
706 */
707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100708free_buffer_stuff(
709 buf_T *buf,
710 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 if (free_options)
713 {
714 clear_wininfo(buf); /* including window-local options */
715 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200716#ifdef FEAT_SPELL
717 ga_clear(&buf->b_s.b_langp);
718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
720#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200721 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
722 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#endif
724#ifdef FEAT_USR_CMDS
725 uc_clear(&buf->b_ucmds); /* clear local user commands */
726#endif
727#ifdef FEAT_SIGNS
728 buf_delete_signs(buf); /* delete any signs */
729#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000730#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200731 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733#ifdef FEAT_LOCALMAP
734 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
735 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
736#endif
737#ifdef FEAT_MBYTE
738 vim_free(buf->b_start_fenc);
739 buf->b_start_fenc = NULL;
740#endif
741}
742
743/*
744 * Free the b_wininfo list for buffer "buf".
745 */
746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100747clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
749 wininfo_T *wip;
750
751 while (buf->b_wininfo != NULL)
752 {
753 wip = buf->b_wininfo;
754 buf->b_wininfo = wip->wi_next;
755 if (wip->wi_optset)
756 {
757 clear_winopt(&wip->wi_opt);
758#ifdef FEAT_FOLDING
759 deleteFoldRecurse(&wip->wi_folds);
760#endif
761 }
762 vim_free(wip);
763 }
764}
765
766#if defined(FEAT_LISTCMDS) || defined(PROTO)
767/*
768 * Go to another buffer. Handles the result of the ATTENTION dialog.
769 */
770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100771goto_buffer(
772 exarg_T *eap,
773 int start,
774 int dir,
775 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000777# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 buf_T *old_curbuf = curbuf;
779
780 swap_exists_action = SEA_DIALOG;
781# endif
782 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
783 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000784# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
786 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000787# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
788 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000789
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000790 /* Reset the error/interrupt/exception state here so that
791 * aborting() returns FALSE when closing a window. */
792 enter_cleanup(&cs);
793# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000794
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000795 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 win_close(curwin, TRUE);
797 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000798 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000799
800# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
801 /* Restore the error/interrupt/exception state if not discarded by a
802 * new aborting error, interrupt, or uncaught exception. */
803 leave_cleanup(&cs);
804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 else
807 handle_swap_exists(old_curbuf);
808# endif
809}
810#endif
811
Bram Moolenaare64ac772005-12-07 20:54:59 +0000812#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813/*
814 * Handle the situation of swap_exists_action being set.
815 * It is allowed for "old_curbuf" to be NULL or invalid.
816 */
817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100818handle_swap_exists(buf_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000820# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
821 cleanup_T cs;
822# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100823#ifdef FEAT_SYN_HL
824 long old_tw = curbuf->b_p_tw;
825#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 if (swap_exists_action == SEA_QUIT)
828 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000829# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
830 /* Reset the error/interrupt/exception state here so that
831 * aborting() returns FALSE when closing a buffer. */
832 enter_cleanup(&cs);
833# endif
834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 /* User selected Quit at ATTENTION prompt. Go back to previous
836 * buffer. If that buffer is gone or the same as the current one,
837 * open a new, empty buffer. */
838 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000839 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100840 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000842 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100846#ifdef FEAT_SYN_HL
847 if (old_tw != curbuf->b_p_tw)
848 check_colorcolumn(curwin);
849#endif
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000852
853# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
854 /* Restore the error/interrupt/exception state if not discarded by a
855 * new aborting error, interrupt, or uncaught exception. */
856 leave_cleanup(&cs);
857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 else if (swap_exists_action == SEA_RECOVER)
860 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000861# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
862 /* Reset the error/interrupt/exception state here so that
863 * aborting() returns FALSE when closing a buffer. */
864 enter_cleanup(&cs);
865# endif
866
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 /* User selected Recover at ATTENTION prompt. */
868 msg_scroll = TRUE;
869 ml_recover();
870 MSG_PUTS("\n"); /* don't overwrite the last message */
871 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000872 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000873
874# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
875 /* Restore the error/interrupt/exception state if not discarded by a
876 * new aborting error, interrupt, or uncaught exception. */
877 leave_cleanup(&cs);
878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
880 swap_exists_action = SEA_NONE;
881}
882#endif
883
884#if defined(FEAT_LISTCMDS) || defined(PROTO)
885/*
886 * do_bufdel() - delete or unload buffer(s)
887 *
888 * addr_count == 0: ":bdel" - delete current buffer
889 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
890 * buffer "end_bnr", then any other arguments.
891 * addr_count == 2: ":N,N bdel" - delete buffers in range
892 *
893 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
894 * DOBUF_DEL (":bdel")
895 *
896 * Returns error message or NULL
897 */
898 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100899do_bufdel(
900 int command,
901 char_u *arg, /* pointer to extra arguments */
902 int addr_count,
903 int start_bnr, /* first buffer number in a range */
904 int end_bnr, /* buffer nr or last buffer nr in a range */
905 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
907 int do_current = 0; /* delete current buffer? */
908 int deleted = 0; /* number of buffers deleted */
909 char_u *errormsg = NULL; /* return value */
910 int bnr; /* buffer number */
911 char_u *p;
912
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 if (addr_count == 0)
914 {
915 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
916 }
917 else
918 {
919 if (addr_count == 2)
920 {
921 if (*arg) /* both range and argument is not allowed */
922 return (char_u *)_(e_trailing);
923 bnr = start_bnr;
924 }
925 else /* addr_count == 1 */
926 bnr = end_bnr;
927
928 for ( ;!got_int; ui_breakcheck())
929 {
930 /*
931 * delete the current buffer last, otherwise when the
932 * current buffer is deleted, the next buffer becomes
933 * the current one and will be loaded, which may then
934 * also be deleted, etc.
935 */
936 if (bnr == curbuf->b_fnum)
937 do_current = bnr;
938 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
939 forceit) == OK)
940 ++deleted;
941
942 /*
943 * find next buffer number to delete/unload
944 */
945 if (addr_count == 2)
946 {
947 if (++bnr > end_bnr)
948 break;
949 }
950 else /* addr_count == 1 */
951 {
952 arg = skipwhite(arg);
953 if (*arg == NUL)
954 break;
955 if (!VIM_ISDIGIT(*arg))
956 {
957 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100958 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
959 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (bnr < 0) /* failed */
961 break;
962 arg = p;
963 }
964 else
965 bnr = getdigits(&arg);
966 }
967 }
968 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
969 FORWARD, do_current, forceit) == OK)
970 ++deleted;
971
972 if (deleted == 0)
973 {
974 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000975 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000977 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000979 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 errormsg = IObuff;
981 }
982 else if (deleted >= p_report)
983 {
984 if (command == DOBUF_UNLOAD)
985 {
986 if (deleted == 1)
987 MSG(_("1 buffer unloaded"));
988 else
989 smsg((char_u *)_("%d buffers unloaded"), deleted);
990 }
991 else if (command == DOBUF_DEL)
992 {
993 if (deleted == 1)
994 MSG(_("1 buffer deleted"));
995 else
996 smsg((char_u *)_("%d buffers deleted"), deleted);
997 }
998 else
999 {
1000 if (deleted == 1)
1001 MSG(_("1 buffer wiped out"));
1002 else
1003 smsg((char_u *)_("%d buffers wiped out"), deleted);
1004 }
1005 }
1006 }
1007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009 return errormsg;
1010}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001011#endif /* FEAT_LISTCMDS */
1012
1013#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1014 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001016static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001017
1018/*
1019 * Make the current buffer empty.
1020 * Used when it is wiped out and it's the last buffer.
1021 */
1022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001023empty_curbuf(
1024 int close_others,
1025 int forceit,
1026 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001027{
1028 int retval;
1029 buf_T *buf = curbuf;
1030
1031 if (action == DOBUF_UNLOAD)
1032 {
1033 EMSG(_("E90: Cannot unload last buffer"));
1034 return FAIL;
1035 }
1036
1037 if (close_others)
1038 {
1039 /* Close any other windows on this buffer, then make it empty. */
1040#ifdef FEAT_WINDOWS
1041 close_windows(buf, TRUE);
1042#endif
1043 }
1044
1045 setpcmark();
1046 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1047 forceit ? ECMD_FORCEIT : 0, curwin);
1048
1049 /*
1050 * do_ecmd() may create a new buffer, then we have to delete
1051 * the old one. But do_ecmd() may have done that already, check
1052 * if the buffer still exists.
1053 */
1054 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1055 close_buffer(NULL, buf, action, FALSE);
1056 if (!close_others)
1057 need_fileinfo = FALSE;
1058 return retval;
1059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060/*
1061 * Implementation of the commands for the buffer list.
1062 *
1063 * action == DOBUF_GOTO go to specified buffer
1064 * action == DOBUF_SPLIT split window and go to specified buffer
1065 * action == DOBUF_UNLOAD unload specified buffer(s)
1066 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1067 * action == DOBUF_WIPE delete specified buffer(s) really
1068 *
1069 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1070 * start == DOBUF_FIRST go to "count" buffer from first buffer
1071 * start == DOBUF_LAST go to "count" buffer from last buffer
1072 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1073 *
1074 * Return FAIL or OK.
1075 */
1076 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001077do_buffer(
1078 int action,
1079 int start,
1080 int dir, /* FORWARD or BACKWARD */
1081 int count, /* buffer number or number of buffers */
1082 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 buf_T *buf;
1085 buf_T *bp;
1086 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1087 || action == DOBUF_WIPE);
1088
1089 switch (start)
1090 {
1091 case DOBUF_FIRST: buf = firstbuf; break;
1092 case DOBUF_LAST: buf = lastbuf; break;
1093 default: buf = curbuf; break;
1094 }
1095 if (start == DOBUF_MOD) /* find next modified buffer */
1096 {
1097 while (count-- > 0)
1098 {
1099 do
1100 {
1101 buf = buf->b_next;
1102 if (buf == NULL)
1103 buf = firstbuf;
1104 }
1105 while (buf != curbuf && !bufIsChanged(buf));
1106 }
1107 if (!bufIsChanged(buf))
1108 {
1109 EMSG(_("E84: No modified buffer found"));
1110 return FAIL;
1111 }
1112 }
1113 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1114 {
1115 while (buf != NULL && buf->b_fnum != count)
1116 buf = buf->b_next;
1117 }
1118 else
1119 {
1120 bp = NULL;
1121 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1122 {
1123 /* remember the buffer where we start, we come back there when all
1124 * buffers are unlisted. */
1125 if (bp == NULL)
1126 bp = buf;
1127 if (dir == FORWARD)
1128 {
1129 buf = buf->b_next;
1130 if (buf == NULL)
1131 buf = firstbuf;
1132 }
1133 else
1134 {
1135 buf = buf->b_prev;
1136 if (buf == NULL)
1137 buf = lastbuf;
1138 }
1139 /* don't count unlisted buffers */
1140 if (unload || buf->b_p_bl)
1141 {
1142 --count;
1143 bp = NULL; /* use this buffer as new starting point */
1144 }
1145 if (bp == buf)
1146 {
1147 /* back where we started, didn't find anything. */
1148 EMSG(_("E85: There is no listed buffer"));
1149 return FAIL;
1150 }
1151 }
1152 }
1153
1154 if (buf == NULL) /* could not find it */
1155 {
1156 if (start == DOBUF_FIRST)
1157 {
1158 /* don't warn when deleting */
1159 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001160 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162 else if (dir == FORWARD)
1163 EMSG(_("E87: Cannot go beyond last buffer"));
1164 else
1165 EMSG(_("E88: Cannot go before first buffer"));
1166 return FAIL;
1167 }
1168
1169#ifdef FEAT_GUI
1170 need_mouse_correct = TRUE;
1171#endif
1172
1173#ifdef FEAT_LISTCMDS
1174 /*
1175 * delete buffer buf from memory and/or the list
1176 */
1177 if (unload)
1178 {
1179 int forward;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
1181 /* When unloading or deleting a buffer that's already unloaded and
1182 * unlisted: fail silently. */
1183 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1184 return FAIL;
1185
1186 if (!forceit && bufIsChanged(buf))
1187 {
1188#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1189 if ((p_confirm || cmdmod.confirm) && p_write)
1190 {
1191 dialog_changed(buf, FALSE);
1192# ifdef FEAT_AUTOCMD
1193 if (!buf_valid(buf))
1194 /* Autocommand deleted buffer, oops! It's not changed
1195 * now. */
1196 return FAIL;
1197# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001198 /* If it's still changed fail silently, the dialog already
1199 * mentioned why it fails. */
1200 if (bufIsChanged(buf))
1201 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001203 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204#endif
1205 {
1206 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1207 buf->b_fnum);
1208 return FAIL;
1209 }
1210 }
1211
1212 /*
1213 * If deleting the last (listed) buffer, make it empty.
1214 * The last (listed) buffer cannot be unloaded.
1215 */
1216 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1217 if (bp->b_p_bl && bp != buf)
1218 break;
1219 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001220 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
1222#ifdef FEAT_WINDOWS
1223 /*
1224 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001225 * (unless it's the only window). Repeat this so long as we end up in
1226 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001228 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001229# ifdef FEAT_AUTOCMD
1230 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1231# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001232 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001233 {
1234 if (win_close(curwin, FALSE) == FAIL)
1235 break;
1236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237#endif
1238
1239 /*
1240 * If the buffer to be deleted is not the current one, delete it here.
1241 */
1242 if (buf != curbuf)
1243 {
1244#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001245 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#endif
1247 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001248 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 return OK;
1250 }
1251
1252 /*
1253 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001254 * There should be another, otherwise it would have been handled
1255 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 * First use au_new_curbuf, if it is valid.
1257 * Then prefer the buffer we most recently visited.
1258 * Else try to find one that is loaded, after the current buffer,
1259 * then before the current buffer.
1260 * Finally use any buffer.
1261 */
1262 buf = NULL; /* selected buffer */
1263 bp = NULL; /* used when no loaded buffer found */
1264#ifdef FEAT_AUTOCMD
1265 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1266 buf = au_new_curbuf;
1267# ifdef FEAT_JUMPLIST
1268 else
1269# endif
1270#endif
1271#ifdef FEAT_JUMPLIST
1272 if (curwin->w_jumplistlen > 0)
1273 {
1274 int jumpidx;
1275
1276 jumpidx = curwin->w_jumplistidx - 1;
1277 if (jumpidx < 0)
1278 jumpidx = curwin->w_jumplistlen - 1;
1279
1280 forward = jumpidx;
1281 while (jumpidx != curwin->w_jumplistidx)
1282 {
1283 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1284 if (buf != NULL)
1285 {
1286 if (buf == curbuf || !buf->b_p_bl)
1287 buf = NULL; /* skip current and unlisted bufs */
1288 else if (buf->b_ml.ml_mfp == NULL)
1289 {
1290 /* skip unloaded buf, but may keep it for later */
1291 if (bp == NULL)
1292 bp = buf;
1293 buf = NULL;
1294 }
1295 }
1296 if (buf != NULL) /* found a valid buffer: stop searching */
1297 break;
1298 /* advance to older entry in jump list */
1299 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1300 break;
1301 if (--jumpidx < 0)
1302 jumpidx = curwin->w_jumplistlen - 1;
1303 if (jumpidx == forward) /* List exhausted for sure */
1304 break;
1305 }
1306 }
1307#endif
1308
1309 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1310 {
1311 forward = TRUE;
1312 buf = curbuf->b_next;
1313 for (;;)
1314 {
1315 if (buf == NULL)
1316 {
1317 if (!forward) /* tried both directions */
1318 break;
1319 buf = curbuf->b_prev;
1320 forward = FALSE;
1321 continue;
1322 }
1323 /* in non-help buffer, try to skip help buffers, and vv */
1324 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1325 {
1326 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1327 break;
1328 if (bp == NULL) /* remember unloaded buf for later */
1329 bp = buf;
1330 }
1331 if (forward)
1332 buf = buf->b_next;
1333 else
1334 buf = buf->b_prev;
1335 }
1336 }
1337 if (buf == NULL) /* No loaded buffer, use unloaded one */
1338 buf = bp;
1339 if (buf == NULL) /* No loaded buffer, find listed one */
1340 {
1341 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1342 if (buf->b_p_bl && buf != curbuf)
1343 break;
1344 }
1345 if (buf == NULL) /* Still no buffer, just take one */
1346 {
1347 if (curbuf->b_next != NULL)
1348 buf = curbuf->b_next;
1349 else
1350 buf = curbuf->b_prev;
1351 }
1352 }
1353
Bram Moolenaara02471e2014-01-10 16:43:14 +01001354 if (buf == NULL)
1355 {
1356 /* Autocommands must have wiped out all other buffers. Only option
1357 * now is to make the current buffer empty. */
1358 return empty_curbuf(FALSE, forceit, action);
1359 }
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 /*
1362 * make buf current buffer
1363 */
1364 if (action == DOBUF_SPLIT) /* split window first */
1365 {
1366# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001367 /* If 'switchbuf' contains "useopen": jump to first window containing
1368 * "buf" if one exists */
1369 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001370 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001371 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001372 * page containing "buf" if one exists */
1373 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 return OK;
1375 if (win_split(0, 0) == FAIL)
1376# endif
1377 return FAIL;
1378 }
1379#endif
1380
1381 /* go to current buffer - nothing to do */
1382 if (buf == curbuf)
1383 return OK;
1384
1385 /*
1386 * Check if the current buffer may be abandoned.
1387 */
1388 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1389 {
1390#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1391 if ((p_confirm || cmdmod.confirm) && p_write)
1392 {
1393 dialog_changed(curbuf, FALSE);
1394# ifdef FEAT_AUTOCMD
1395 if (!buf_valid(buf))
1396 /* Autocommand deleted buffer, oops! */
1397 return FAIL;
1398# endif
1399 }
1400 if (bufIsChanged(curbuf))
1401#endif
1402 {
1403 EMSG(_(e_nowrtmsg));
1404 return FAIL;
1405 }
1406 }
1407
1408 /* Go to the other buffer. */
1409 set_curbuf(buf, action);
1410
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001411#if defined(FEAT_LISTCMDS) \
1412 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001414 {
1415 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417#endif
1418
1419#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1420 if (aborting()) /* autocmds may abort script processing */
1421 return FAIL;
1422#endif
1423
1424 return OK;
1425}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428/*
1429 * Set current buffer to "buf". Executes autocommands and closes current
1430 * buffer. "action" tells how to close the current buffer:
1431 * DOBUF_GOTO free or hide it
1432 * DOBUF_SPLIT nothing
1433 * DOBUF_UNLOAD unload it
1434 * DOBUF_DEL delete it
1435 * DOBUF_WIPE wipe it out
1436 */
1437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001438set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
1440 buf_T *prevbuf;
1441 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1442 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001443#ifdef FEAT_SYN_HL
1444 long old_tw = curbuf->b_p_tw;
1445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
1447 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001448 if (!cmdmod.keepalt)
1449 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001450 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 /* Don't restart Select mode after switching to another buffer. */
1453 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
1455 /* close_windows() or apply_autocmds() may change curbuf */
1456 prevbuf = curbuf;
1457
1458#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001459 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460# ifdef FEAT_EVAL
Bram Moolenaar82404332016-07-10 17:00:38 +02001461 || (buf_valid(prevbuf) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462# else
Bram Moolenaar82404332016-07-10 17:00:38 +02001463 || buf_valid(prevbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464# endif
1465#endif
1466 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001467#ifdef FEAT_SYN_HL
1468 if (prevbuf == curwin->w_buffer)
1469 reset_synblock(curwin);
1470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471#ifdef FEAT_WINDOWS
1472 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001473 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#endif
1475#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1476 if (buf_valid(prevbuf) && !aborting())
1477#else
1478 if (buf_valid(prevbuf))
1479#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001480 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001481#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001482 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001483#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001484 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001485 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1487 unload ? action : (action == DOBUF_GOTO
1488 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001489 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001490#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001491 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001492 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001493 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001494#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 }
1497#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001498 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001499 * it did ":bunload") or aborted the script processing!
1500 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1501 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001502# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001503 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001504# endif
1505# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001506 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001507# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001508 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001512#ifdef FEAT_SYN_HL
1513 if (old_tw != curbuf->b_p_tw)
1514 check_colorcolumn(curwin);
1515#endif
1516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517}
1518
1519/*
1520 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001521 * Old curbuf must have been abandoned already! This also means "curbuf" may
1522 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 */
1524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001525enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 /* Copy buffer and window local option values. Not for a help buffer. */
1528 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1529 if (!buf->b_help)
1530 get_winopts(buf);
1531#ifdef FEAT_FOLDING
1532 else
1533 /* Remove all folds in the window. */
1534 clearFolding(curwin);
1535 foldUpdateAll(curwin); /* update folds (later). */
1536#endif
1537
1538 /* Get the buffer in the current window. */
1539 curwin->w_buffer = buf;
1540 curbuf = buf;
1541 ++curbuf->b_nwindows;
1542
1543#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001544 if (curwin->w_p_diff)
1545 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#endif
1547
Bram Moolenaara971b822011-09-14 14:43:25 +02001548#ifdef FEAT_SYN_HL
1549 curwin->w_s = &(buf->b_s);
1550#endif
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 /* Cursor on first line by default. */
1553 curwin->w_cursor.lnum = 1;
1554 curwin->w_cursor.col = 0;
1555#ifdef FEAT_VIRTUALEDIT
1556 curwin->w_cursor.coladd = 0;
1557#endif
1558 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001559#ifdef FEAT_AUTOCMD
1560 curwin->w_topline_was_set = FALSE;
1561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001563 /* mark cursor position as being invalid */
1564 curwin->w_valid = 0;
1565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 /* Make sure the buffer is loaded. */
1567 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001568 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001569#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001570 /* If there is no filetype, allow for detecting one. Esp. useful for
1571 * ":ball" used in a autocommand. If there already is a filetype we
1572 * might prefer to keep it. */
1573 if (*curbuf->b_p_ft == NUL)
1574 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001575#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001576
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001577 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 else
1580 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001581 if (!msg_silent)
1582 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1584#ifdef FEAT_AUTOCMD
1585 curwin->w_topline = 1;
1586# ifdef FEAT_DIFF
1587 curwin->w_topfill = 0;
1588# endif
1589 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1590 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1591#endif
1592 }
1593
1594 /* If autocommands did not change the cursor position, restore cursor lnum
1595 * and possibly cursor col. */
1596 if (curwin->w_cursor.lnum == 1 && inindent(0))
1597 buflist_getfpos();
1598
1599 check_arg_idx(curwin); /* check for valid arg_idx */
1600#ifdef FEAT_TITLE
1601 maketitle();
1602#endif
1603#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001604 /* when autocmds didn't change it */
1605 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606#endif
1607 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1608
Bram Moolenaar009b2592004-10-24 19:18:58 +00001609#ifdef FEAT_NETBEANS_INTG
1610 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001611 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001612#endif
1613
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001614 /* Change directories when the 'acd' option is set. */
1615 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617#ifdef FEAT_KEYMAP
1618 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001619 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001621#ifdef FEAT_SPELL
1622 /* May need to set the spell language. Can only do this after the buffer
1623 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001624 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1625 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001626#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001627#ifdef FEAT_VIMINFO
1628 curbuf->b_last_used = vim_time();
1629#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 redraw_later(NOT_VALID);
1632}
1633
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001634#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1635/*
1636 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001637 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001638 */
1639 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001640do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001641{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001642 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001643 && curbuf->b_ffname != NULL
1644 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001645 shorten_fnames(TRUE);
1646}
1647#endif
1648
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649/*
1650 * functions for dealing with the buffer list
1651 */
1652
1653/*
1654 * Add a file name to the buffer list. Return a pointer to the buffer.
1655 * If the same file name already exists return a pointer to that buffer.
1656 * If it does not exist, or if fname == NULL, a new entry is created.
1657 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1658 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1659 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001660 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001661 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1662 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 * This is the ONLY way to create a new buffer.
1664 */
1665static int top_file_num = 1; /* highest file number */
1666
1667 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001668buflist_new(
1669 char_u *ffname, /* full path of fname or relative */
1670 char_u *sfname, /* short fname or NULL */
1671 linenr_T lnum, /* preferred cursor line */
1672 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674 buf_T *buf;
1675#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001676 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#endif
1678
1679 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1680
1681 /*
1682 * If file name already exists in the list, update the entry.
1683 */
1684#ifdef UNIX
1685 /* On Unix we can use inode numbers when the file exists. Works better
1686 * for hard links. */
1687 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1688 st.st_dev = (dev_T)-1;
1689#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001690 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691#ifdef UNIX
1692 buflist_findname_stat(ffname, &st)
1693#else
1694 buflist_findname(ffname)
1695#endif
1696 ) != NULL)
1697 {
1698 vim_free(ffname);
1699 if (lnum != 0)
1700 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001701
1702 if ((flags & BLN_NOOPT) == 0)
1703 /* copy the options now, if 'cpo' doesn't have 's' and not done
1704 * already */
1705 buf_copy_options(buf, 0);
1706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1708 {
1709 buf->b_p_bl = TRUE;
1710#ifdef FEAT_AUTOCMD
1711 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001712 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001713 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
1714 && !buf_valid(buf))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001715 return NULL;
1716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717#endif
1718 }
1719 return buf;
1720 }
1721
1722 /*
1723 * If the current buffer has no name and no contents, use the current
1724 * buffer. Otherwise: Need to allocate a new buffer structure.
1725 *
1726 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001727 * (A spell file buffer is allocated in spell.c, but that's not a normal
1728 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 */
1730 buf = NULL;
1731 if ((flags & BLN_CURBUF)
1732 && curbuf != NULL
1733 && curbuf->b_ffname == NULL
1734 && curbuf->b_nwindows <= 1
1735 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1736 {
1737 buf = curbuf;
1738#ifdef FEAT_AUTOCMD
1739 /* It's like this buffer is deleted. Watch out for autocommands that
1740 * change curbuf! If that happens, allocate a new buffer anyway. */
1741 if (curbuf->b_p_bl)
1742 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1743 if (buf == curbuf)
1744 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1745# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001746 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 return NULL;
1748# endif
1749#endif
1750#ifdef FEAT_QUICKFIX
1751# ifdef FEAT_AUTOCMD
1752 if (buf == curbuf)
1753# endif
1754 {
1755 /* Make sure 'bufhidden' and 'buftype' are empty */
1756 clear_string_option(&buf->b_p_bh);
1757 clear_string_option(&buf->b_p_bt);
1758 }
1759#endif
1760 }
1761 if (buf != curbuf || curbuf == NULL)
1762 {
1763 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1764 if (buf == NULL)
1765 {
1766 vim_free(ffname);
1767 return NULL;
1768 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001769#ifdef FEAT_EVAL
1770 /* init b: variables */
1771 buf->b_vars = dict_alloc();
1772 if (buf->b_vars == NULL)
1773 {
1774 vim_free(ffname);
1775 vim_free(buf);
1776 return NULL;
1777 }
1778 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 }
1781
1782 if (ffname != NULL)
1783 {
1784 buf->b_ffname = ffname;
1785 buf->b_sfname = vim_strsave(sfname);
1786 }
1787
1788 clear_wininfo(buf);
1789 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1790
1791 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1792 || buf->b_wininfo == NULL)
1793 {
1794 vim_free(buf->b_ffname);
1795 buf->b_ffname = NULL;
1796 vim_free(buf->b_sfname);
1797 buf->b_sfname = NULL;
1798 if (buf != curbuf)
1799 free_buffer(buf);
1800 return NULL;
1801 }
1802
1803 if (buf == curbuf)
1804 {
1805 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001806 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (buf != curbuf) /* autocommands deleted the buffer! */
1808 return NULL;
1809#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001810 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 return NULL;
1812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001814
1815 /* Init the options. */
1816 buf->b_p_initialized = FALSE;
1817 buf_copy_options(buf, BCO_ENTER);
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819#ifdef FEAT_KEYMAP
1820 /* need to reload lmaps and set b:keymap_name */
1821 curbuf->b_kmap_state |= KEYMAP_INIT;
1822#endif
1823 }
1824 else
1825 {
1826 /*
1827 * put new buffer at the end of the buffer list
1828 */
1829 buf->b_next = NULL;
1830 if (firstbuf == NULL) /* buffer list is empty */
1831 {
1832 buf->b_prev = NULL;
1833 firstbuf = buf;
1834 }
1835 else /* append new buffer at end of list */
1836 {
1837 lastbuf->b_next = buf;
1838 buf->b_prev = lastbuf;
1839 }
1840 lastbuf = buf;
1841
1842 buf->b_fnum = top_file_num++;
1843 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1844 {
1845 EMSG(_("W14: Warning: List of file names overflow"));
1846 if (emsg_silent == 0)
1847 {
1848 out_flush();
1849 ui_delay(3000L, TRUE); /* make sure it is noticed */
1850 }
1851 top_file_num = 1;
1852 }
1853
1854 /*
1855 * Always copy the options from the current buffer.
1856 */
1857 buf_copy_options(buf, BCO_ALWAYS);
1858 }
1859
1860 buf->b_wininfo->wi_fpos.lnum = lnum;
1861 buf->b_wininfo->wi_win = curwin;
1862
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001863#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001864 hash_init(&buf->b_s.b_keywtab);
1865 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866#endif
1867
1868 buf->b_fname = buf->b_sfname;
1869#ifdef UNIX
1870 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001871 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 else
1873 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001874 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 buf->b_dev = st.st_dev;
1876 buf->b_ino = st.st_ino;
1877 }
1878#endif
1879 buf->b_u_synced = TRUE;
1880 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001881 if (flags & BLN_DUMMY)
1882 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 buf_clear_file(buf);
1884 clrallmarks(buf); /* clear marks */
1885 fmarks_check_names(buf); /* check file marks for this file */
1886 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1887#ifdef FEAT_AUTOCMD
1888 if (!(flags & BLN_DUMMY))
1889 {
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001890 /* Tricky: these autocommands may change the buffer list. They could
1891 * also split the window with re-using the one empty buffer. This may
1892 * result in unexpectedly losing the empty buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001893 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
1894 && !buf_valid(buf))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001895 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001897 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001898 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
1899 && !buf_valid(buf))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001900 return NULL;
1901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001903 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 return NULL;
1905# endif
1906 }
1907#endif
1908
1909 return buf;
1910}
1911
1912/*
1913 * Free the memory for the options of a buffer.
1914 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1915 * 'fileencoding'.
1916 */
1917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001918free_buf_options(
1919 buf_T *buf,
1920 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921{
1922 if (free_p_ff)
1923 {
1924#ifdef FEAT_MBYTE
1925 clear_string_option(&buf->b_p_fenc);
1926#endif
1927 clear_string_option(&buf->b_p_ff);
1928#ifdef FEAT_QUICKFIX
1929 clear_string_option(&buf->b_p_bh);
1930 clear_string_option(&buf->b_p_bt);
1931#endif
1932 }
1933#ifdef FEAT_FIND_ID
1934 clear_string_option(&buf->b_p_def);
1935 clear_string_option(&buf->b_p_inc);
1936# ifdef FEAT_EVAL
1937 clear_string_option(&buf->b_p_inex);
1938# endif
1939#endif
1940#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1941 clear_string_option(&buf->b_p_inde);
1942 clear_string_option(&buf->b_p_indk);
1943#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001944#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1945 clear_string_option(&buf->b_p_bexpr);
1946#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001947#if defined(FEAT_CRYPT)
1948 clear_string_option(&buf->b_p_cm);
1949#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001950#if defined(FEAT_EVAL)
1951 clear_string_option(&buf->b_p_fex);
1952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953#ifdef FEAT_CRYPT
1954 clear_string_option(&buf->b_p_key);
1955#endif
1956 clear_string_option(&buf->b_p_kp);
1957 clear_string_option(&buf->b_p_mps);
1958 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001959 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 clear_string_option(&buf->b_p_isk);
1961#ifdef FEAT_KEYMAP
1962 clear_string_option(&buf->b_p_keymap);
1963 ga_clear(&buf->b_kmap_ga);
1964#endif
1965#ifdef FEAT_COMMENTS
1966 clear_string_option(&buf->b_p_com);
1967#endif
1968#ifdef FEAT_FOLDING
1969 clear_string_option(&buf->b_p_cms);
1970#endif
1971 clear_string_option(&buf->b_p_nf);
1972#ifdef FEAT_SYN_HL
1973 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001974 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001975#endif
1976#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001977 clear_string_option(&buf->b_s.b_p_spc);
1978 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001979 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001980 buf->b_s.b_cap_prog = NULL;
1981 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982#endif
1983#ifdef FEAT_SEARCHPATH
1984 clear_string_option(&buf->b_p_sua);
1985#endif
1986#ifdef FEAT_AUTOCMD
1987 clear_string_option(&buf->b_p_ft);
1988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989#ifdef FEAT_CINDENT
1990 clear_string_option(&buf->b_p_cink);
1991 clear_string_option(&buf->b_p_cino);
1992#endif
1993#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1994 clear_string_option(&buf->b_p_cinw);
1995#endif
1996#ifdef FEAT_INS_EXPAND
1997 clear_string_option(&buf->b_p_cpt);
1998#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001999#ifdef FEAT_COMPL_FUNC
2000 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002001 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#ifdef FEAT_QUICKFIX
2004 clear_string_option(&buf->b_p_gp);
2005 clear_string_option(&buf->b_p_mp);
2006 clear_string_option(&buf->b_p_efm);
2007#endif
2008 clear_string_option(&buf->b_p_ep);
2009 clear_string_option(&buf->b_p_path);
2010 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002011 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012#ifdef FEAT_INS_EXPAND
2013 clear_string_option(&buf->b_p_dict);
2014 clear_string_option(&buf->b_p_tsr);
2015#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002016#ifdef FEAT_TEXTOBJ
2017 clear_string_option(&buf->b_p_qe);
2018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002020 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002021#ifdef FEAT_LISP
2022 clear_string_option(&buf->b_p_lw);
2023#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002024 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025}
2026
2027/*
2028 * get alternate file n
2029 * set linenr to lnum or altfpos.lnum if lnum == 0
2030 * also set cursor column to altfpos.col if 'startofline' is not set.
2031 * if (options & GETF_SETMARK) call setpcmark()
2032 * if (options & GETF_ALT) we are jumping to an alternate file.
2033 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2034 *
2035 * return FAIL for failure, OK for success
2036 */
2037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002038buflist_getfile(
2039 int n,
2040 linenr_T lnum,
2041 int options,
2042 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044 buf_T *buf;
2045#ifdef FEAT_WINDOWS
2046 win_T *wp = NULL;
2047#endif
2048 pos_T *fpos;
2049 colnr_T col;
2050
2051 buf = buflist_findnr(n);
2052 if (buf == NULL)
2053 {
2054 if ((options & GETF_ALT) && n == 0)
2055 EMSG(_(e_noalt));
2056 else
2057 EMSGN(_("E92: Buffer %ld not found"), n);
2058 return FAIL;
2059 }
2060
2061 /* if alternate file is the current buffer, nothing to do */
2062 if (buf == curbuf)
2063 return OK;
2064
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002065 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002066 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002067 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002069 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002070#ifdef FEAT_AUTOCMD
2071 if (curbuf_locked())
2072 return FAIL;
2073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
2075 /* altfpos may be changed by getfile(), get it now */
2076 if (lnum == 0)
2077 {
2078 fpos = buflist_findfpos(buf);
2079 lnum = fpos->lnum;
2080 col = fpos->col;
2081 }
2082 else
2083 col = 0;
2084
2085#ifdef FEAT_WINDOWS
2086 if (options & GETF_SWITCH)
2087 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002088 /* If 'switchbuf' contains "useopen": jump to first window containing
2089 * "buf" if one exists */
2090 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002092
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002093 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002094 * page containing "buf" if one exists */
2095 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002096 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002097
2098 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2099 * current buffer isn't empty: open new tab or window */
2100 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2101 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002103 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002104 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002105 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2106 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002108 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110 }
2111#endif
2112
2113 ++RedrawingDisabled;
2114 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2115 lnum, forceit) <= 0)
2116 {
2117 --RedrawingDisabled;
2118
2119 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2120 if (!p_sol && col != 0)
2121 {
2122 curwin->w_cursor.col = col;
2123 check_cursor_col();
2124#ifdef FEAT_VIRTUALEDIT
2125 curwin->w_cursor.coladd = 0;
2126#endif
2127 curwin->w_set_curswant = TRUE;
2128 }
2129 return OK;
2130 }
2131 --RedrawingDisabled;
2132 return FAIL;
2133}
2134
2135/*
2136 * go to the last know line number for the current buffer
2137 */
2138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002139buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 pos_T *fpos;
2142
2143 fpos = buflist_findfpos(curbuf);
2144
2145 curwin->w_cursor.lnum = fpos->lnum;
2146 check_cursor_lnum();
2147
2148 if (p_sol)
2149 curwin->w_cursor.col = 0;
2150 else
2151 {
2152 curwin->w_cursor.col = fpos->col;
2153 check_cursor_col();
2154#ifdef FEAT_VIRTUALEDIT
2155 curwin->w_cursor.coladd = 0;
2156#endif
2157 curwin->w_set_curswant = TRUE;
2158 }
2159}
2160
Bram Moolenaar81695252004-12-29 20:58:21 +00002161#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2162/*
2163 * Find file in buffer list by name (it has to be for the current window).
2164 * Returns NULL if not found.
2165 */
2166 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002167buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002168{
2169 char_u *ffname;
2170 buf_T *buf = NULL;
2171
2172 /* First make the name into a full path name */
2173 ffname = FullName_save(fname,
2174#ifdef UNIX
2175 TRUE /* force expansion, get rid of symbolic links */
2176#else
2177 FALSE
2178#endif
2179 );
2180 if (ffname != NULL)
2181 {
2182 buf = buflist_findname(ffname);
2183 vim_free(ffname);
2184 }
2185 return buf;
2186}
2187#endif
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189/*
2190 * Find file in buffer list by name (it has to be for the current window).
2191 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002192 * Skips dummy buffers.
2193 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 */
2195 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002196buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002199 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201 if (mch_stat((char *)ffname, &st) < 0)
2202 st.st_dev = (dev_T)-1;
2203 return buflist_findname_stat(ffname, &st);
2204}
2205
2206/*
2207 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2208 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002209 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 */
2211 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002212buflist_findname_stat(
2213 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002214 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216#endif
2217 buf_T *buf;
2218
2219 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002220 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221#ifdef UNIX
2222 , stp
2223#endif
2224 ))
2225 return buf;
2226 return NULL;
2227}
2228
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002229#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2230 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231/*
2232 * Find file in buffer list by a regexp pattern.
2233 * Return fnum of the found buffer.
2234 * Return < 0 for error.
2235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237buflist_findpat(
2238 char_u *pattern,
2239 char_u *pattern_end, /* pointer to first char after pattern */
2240 int unlisted, /* find unlisted buffers */
2241 int diffmode UNUSED, /* find diff-mode buffers only */
2242 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
2244 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 int match = -1;
2246 int find_listed;
2247 char_u *pat;
2248 char_u *patend;
2249 int attempt;
2250 char_u *p;
2251 int toggledollar;
2252
2253 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2254 {
2255 if (*pattern == '%')
2256 match = curbuf->b_fnum;
2257 else
2258 match = curwin->w_alt_fnum;
2259#ifdef FEAT_DIFF
2260 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2261 match = -1;
2262#endif
2263 }
2264
2265 /*
2266 * Try four ways of matching a listed buffer:
2267 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002268 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 * attempt == 2: with '$' at end (only match at end)
2270 * attempt == 3: with '^' at start and '$' at end (only full match)
2271 * Repeat this for finding an unlisted buffer if there was no matching
2272 * listed buffer.
2273 */
2274 else
2275 {
2276 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2277 if (pat == NULL)
2278 return -1;
2279 patend = pat + STRLEN(pat) - 1;
2280 toggledollar = (patend > pat && *patend == '$');
2281
2282 /* First try finding a listed buffer. If not found and "unlisted"
2283 * is TRUE, try finding an unlisted buffer. */
2284 find_listed = TRUE;
2285 for (;;)
2286 {
2287 for (attempt = 0; attempt <= 3; ++attempt)
2288 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002289 regmatch_T regmatch;
2290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 /* may add '^' and '$' */
2292 if (toggledollar)
2293 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2294 p = pat;
2295 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2296 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002297 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2298 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 vim_free(pat);
2301 return -1;
2302 }
2303
2304 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2305 if (buf->b_p_bl == find_listed
2306#ifdef FEAT_DIFF
2307 && (!diffmode || diff_mode_buf(buf))
2308#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002309 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002311 if (curtab_only)
2312 {
2313 /* Ignore the match if the buffer is not open in
2314 * the current tab. */
2315#ifdef FEAT_WINDOWS
2316 win_T *wp;
2317
2318 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2319 if (wp->w_buffer == buf)
2320 break;
2321 if (wp == NULL)
2322 continue;
2323#else
2324 if (curwin->w_buffer != buf)
2325 continue;
2326#endif
2327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (match >= 0) /* already found a match */
2329 {
2330 match = -2;
2331 break;
2332 }
2333 match = buf->b_fnum; /* remember first match */
2334 }
2335
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002336 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if (match >= 0) /* found one match */
2338 break;
2339 }
2340
2341 /* Only search for unlisted buffers if there was no match with
2342 * a listed buffer. */
2343 if (!unlisted || !find_listed || match != -1)
2344 break;
2345 find_listed = FALSE;
2346 }
2347
2348 vim_free(pat);
2349 }
2350
2351 if (match == -2)
2352 EMSG2(_("E93: More than one match for %s"), pattern);
2353 else if (match < 0)
2354 EMSG2(_("E94: No matching buffer for %s"), pattern);
2355 return match;
2356}
2357#endif
2358
2359#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2360
2361/*
2362 * Find all buffer names that match.
2363 * For command line expansion of ":buf" and ":sbuf".
2364 * Return OK if matches found, FAIL otherwise.
2365 */
2366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002367ExpandBufnames(
2368 char_u *pat,
2369 int *num_file,
2370 char_u ***file,
2371 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 int count = 0;
2374 buf_T *buf;
2375 int round;
2376 char_u *p;
2377 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002378 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 *num_file = 0; /* return values in case of FAIL */
2381 *file = NULL;
2382
Bram Moolenaar05159a02005-02-26 23:04:13 +00002383 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2384 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002386 patc = alloc((unsigned)STRLEN(pat) + 11);
2387 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002389 STRCPY(patc, "\\(^\\|[\\/]\\)");
2390 STRCPY(patc + 11, pat + 1);
2391 }
2392 else
2393 patc = pat;
2394
2395 /*
2396 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002397 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002398 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002399 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002400 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002401 regmatch_T regmatch;
2402
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002403 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002404 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002405 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2406 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002407 {
2408 if (patc != pat)
2409 vim_free(patc);
2410 return FAIL;
2411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413 /*
2414 * round == 1: Count the matches.
2415 * round == 2: Build the array to keep the matches.
2416 */
2417 for (round = 1; round <= 2; ++round)
2418 {
2419 count = 0;
2420 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2421 {
2422 if (!buf->b_p_bl) /* skip unlisted buffers */
2423 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002424 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 if (p != NULL)
2426 {
2427 if (round == 1)
2428 ++count;
2429 else
2430 {
2431 if (options & WILD_HOME_REPLACE)
2432 p = home_replace_save(buf, p);
2433 else
2434 p = vim_strsave(p);
2435 (*file)[count++] = p;
2436 }
2437 }
2438 }
2439 if (count == 0) /* no match found, break here */
2440 break;
2441 if (round == 1)
2442 {
2443 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2444 if (*file == NULL)
2445 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002446 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002447 if (patc != pat)
2448 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 return FAIL;
2450 }
2451 }
2452 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002453 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 if (count) /* match(es) found, break here */
2455 break;
2456 }
2457
Bram Moolenaar05159a02005-02-26 23:04:13 +00002458 if (patc != pat)
2459 vim_free(patc);
2460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 *num_file = count;
2462 return (count == 0 ? FAIL : OK);
2463}
2464
2465#endif /* FEAT_CMDL_COMPL */
2466
2467#ifdef HAVE_BUFLIST_MATCH
2468/*
2469 * Check for a match on the file name for buffer "buf" with regprog "prog".
2470 */
2471 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002472buflist_match(
2473 regmatch_T *rmp,
2474 buf_T *buf,
2475 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
2477 char_u *match;
2478
2479 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002480 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002482 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484 return match;
2485}
2486
2487/*
2488 * Try matching the regexp in "prog" with file name "name".
2489 * Return "name" when there is a match, NULL when not.
2490 */
2491 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002492fname_match(
2493 regmatch_T *rmp,
2494 char_u *name,
2495 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 char_u *match = NULL;
2498 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 if (name != NULL)
2501 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002502 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002503 rmp->rm_ic = p_fic || ignore_case;
2504 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 match = name;
2506 else
2507 {
2508 /* Replace $(HOME) with '~' and try matching again. */
2509 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002510 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 match = name;
2512 vim_free(p);
2513 }
2514 }
2515
2516 return match;
2517}
2518#endif
2519
2520/*
2521 * find file in buffer list by number
2522 */
2523 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002524buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
2526 buf_T *buf;
2527
2528 if (nr == 0)
2529 nr = curwin->w_alt_fnum;
2530 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2531 if (buf->b_fnum == nr)
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002532 return buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 return NULL;
2534}
2535
2536/*
2537 * Get name of file 'n' in the buffer list.
2538 * When the file has no name an empty string is returned.
2539 * home_replace() is used to shorten the file name (used for marks).
2540 * Returns a pointer to allocated memory, of NULL when failed.
2541 */
2542 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002543buflist_nr2name(
2544 int n,
2545 int fullname,
2546 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 buf_T *buf;
2549
2550 buf = buflist_findnr(n);
2551 if (buf == NULL)
2552 return NULL;
2553 return home_replace_save(helptail ? buf : NULL,
2554 fullname ? buf->b_ffname : buf->b_fname);
2555}
2556
2557/*
2558 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2559 * When "copy_options" is TRUE save the local window option values.
2560 * When "lnum" is 0 only do the options.
2561 */
2562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002563buflist_setfpos(
2564 buf_T *buf,
2565 win_T *win,
2566 linenr_T lnum,
2567 colnr_T col,
2568 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570 wininfo_T *wip;
2571
2572 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2573 if (wip->wi_win == win)
2574 break;
2575 if (wip == NULL)
2576 {
2577 /* allocate a new entry */
2578 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2579 if (wip == NULL)
2580 return;
2581 wip->wi_win = win;
2582 if (lnum == 0) /* set lnum even when it's 0 */
2583 lnum = 1;
2584 }
2585 else
2586 {
2587 /* remove the entry from the list */
2588 if (wip->wi_prev)
2589 wip->wi_prev->wi_next = wip->wi_next;
2590 else
2591 buf->b_wininfo = wip->wi_next;
2592 if (wip->wi_next)
2593 wip->wi_next->wi_prev = wip->wi_prev;
2594 if (copy_options && wip->wi_optset)
2595 {
2596 clear_winopt(&wip->wi_opt);
2597#ifdef FEAT_FOLDING
2598 deleteFoldRecurse(&wip->wi_folds);
2599#endif
2600 }
2601 }
2602 if (lnum != 0)
2603 {
2604 wip->wi_fpos.lnum = lnum;
2605 wip->wi_fpos.col = col;
2606 }
2607 if (copy_options)
2608 {
2609 /* Save the window-specific option values. */
2610 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2611#ifdef FEAT_FOLDING
2612 wip->wi_fold_manual = win->w_fold_manual;
2613 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2614#endif
2615 wip->wi_optset = TRUE;
2616 }
2617
2618 /* insert the entry in front of the list */
2619 wip->wi_next = buf->b_wininfo;
2620 buf->b_wininfo = wip;
2621 wip->wi_prev = NULL;
2622 if (wip->wi_next)
2623 wip->wi_next->wi_prev = wip;
2624
2625 return;
2626}
2627
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002628#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002629static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002630
2631/*
2632 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2633 * page. That's because a diff is local to a tab page.
2634 */
2635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002636wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002637{
2638 win_T *wp;
2639
2640 if (wip->wi_opt.wo_diff)
2641 {
2642 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2643 /* return FALSE when it's a window in the current tab page, thus
2644 * the buffer was in diff mode here */
2645 if (wip->wi_win == wp)
2646 return FALSE;
2647 return TRUE;
2648 }
2649 return FALSE;
2650}
2651#endif
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653/*
2654 * Find info for the current window in buffer "buf".
2655 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002656 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2657 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 * Returns NULL when there isn't any info.
2659 */
2660 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002661find_wininfo(
2662 buf_T *buf,
2663 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665 wininfo_T *wip;
2666
2667 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002668 if (wip->wi_win == curwin
2669#ifdef FEAT_DIFF
2670 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2671#endif
2672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002674
2675 /* If no wininfo for curwin, use the first in the list (that doesn't have
2676 * 'diff' set and is in another tab page). */
2677 if (wip == NULL)
2678 {
2679#ifdef FEAT_DIFF
2680 if (skip_diff_buffer)
2681 {
2682 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2683 if (!wininfo_other_tab_diff(wip))
2684 break;
2685 }
2686 else
2687#endif
2688 wip = buf->b_wininfo;
2689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 return wip;
2691}
2692
2693/*
2694 * Reset the local window options to the values last used in this window.
2695 * If the buffer wasn't used in this window before, use the values from
2696 * the most recently used window. If the values were never set, use the
2697 * global values for the window.
2698 */
2699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002700get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 wininfo_T *wip;
2703
2704 clear_winopt(&curwin->w_onebuf_opt);
2705#ifdef FEAT_FOLDING
2706 clearFolding(curwin);
2707#endif
2708
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002709 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (wip != NULL && wip->wi_optset)
2711 {
2712 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2713#ifdef FEAT_FOLDING
2714 curwin->w_fold_manual = wip->wi_fold_manual;
2715 curwin->w_foldinvalid = TRUE;
2716 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2717#endif
2718 }
2719 else
2720 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2721
2722#ifdef FEAT_FOLDING
2723 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2724 if (p_fdls >= 0)
2725 curwin->w_p_fdl = p_fdls;
2726#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002727#ifdef FEAT_SYN_HL
2728 check_colorcolumn(curwin);
2729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730}
2731
2732/*
2733 * Find the position (lnum and col) for the buffer 'buf' for the current
2734 * window.
2735 * Returns a pointer to no_position if no position is found.
2736 */
2737 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002738buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002741 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002743 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (wip != NULL)
2745 return &(wip->wi_fpos);
2746 else
2747 return &no_position;
2748}
2749
2750/*
2751 * Find the lnum for the buffer 'buf' for the current window.
2752 */
2753 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002754buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 return buflist_findfpos(buf)->lnum;
2757}
2758
2759#if defined(FEAT_LISTCMDS) || defined(PROTO)
2760/*
2761 * List all know file names (for :files and :buffers command).
2762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002764buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766 buf_T *buf;
2767 int len;
2768 int i;
2769
2770 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2771 {
2772 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002773 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2774 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2775 || (vim_strchr(eap->arg, '+')
2776 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2777 || (vim_strchr(eap->arg, 'a')
2778 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2779 || (vim_strchr(eap->arg, 'h')
2780 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2781 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2782 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2783 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2784 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2785 || (vim_strchr(eap->arg, '#')
2786 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 continue;
2788 msg_putchar('\n');
2789 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002790 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else
2792 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2793
Bram Moolenaar50cde822005-06-05 21:54:54 +00002794 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 buf->b_fnum,
2796 buf->b_p_bl ? ' ' : 'u',
2797 buf == curbuf ? '%' :
2798 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2799 buf->b_ml.ml_mfp == NULL ? ' ' :
2800 (buf->b_nwindows == 0 ? 'h' : 'a'),
2801 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2802 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002803 : (bufIsChanged(buf) ? '+' : ' '),
2804 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002805 if (len > IOSIZE - 20)
2806 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 i = 40 - vim_strsize(IObuff);
2810 do
2811 {
2812 IObuff[len++] = ' ';
2813 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002814 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2815 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002816 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 msg_outtrans(IObuff);
2818 out_flush(); /* output one line at a time */
2819 ui_breakcheck();
2820 }
2821}
2822#endif
2823
2824/*
2825 * Get file name and line number for file 'fnum'.
2826 * Used by DoOneCmd() for translating '%' and '#'.
2827 * Used by insert_reg() and cmdline_paste() for '#' register.
2828 * Return FAIL if not found, OK for success.
2829 */
2830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002831buflist_name_nr(
2832 int fnum,
2833 char_u **fname,
2834 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
2836 buf_T *buf;
2837
2838 buf = buflist_findnr(fnum);
2839 if (buf == NULL || buf->b_fname == NULL)
2840 return FAIL;
2841
2842 *fname = buf->b_fname;
2843 *lnum = buflist_findlnum(buf);
2844
2845 return OK;
2846}
2847
2848/*
2849 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2850 * The file name with the full path is also remembered, for when :cd is used.
2851 * Returns FAIL for failure (file name already in use by other buffer)
2852 * OK otherwise.
2853 */
2854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002855setfname(
2856 buf_T *buf,
2857 char_u *ffname,
2858 char_u *sfname,
2859 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860{
Bram Moolenaar81695252004-12-29 20:58:21 +00002861 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002863 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#endif
2865
2866 if (ffname == NULL || *ffname == NUL)
2867 {
2868 /* Removing the name. */
2869 vim_free(buf->b_ffname);
2870 vim_free(buf->b_sfname);
2871 buf->b_ffname = NULL;
2872 buf->b_sfname = NULL;
2873#ifdef UNIX
2874 st.st_dev = (dev_T)-1;
2875#endif
2876 }
2877 else
2878 {
2879 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2880 if (ffname == NULL) /* out of memory */
2881 return FAIL;
2882
2883 /*
2884 * if the file name is already used in another buffer:
2885 * - if the buffer is loaded, fail
2886 * - if the buffer is not loaded, delete it from the list
2887 */
2888#ifdef UNIX
2889 if (mch_stat((char *)ffname, &st) < 0)
2890 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002891#endif
2892 if (!(buf->b_flags & BF_DUMMY))
2893#ifdef UNIX
2894 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002896 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#endif
2898 if (obuf != NULL && obuf != buf)
2899 {
2900 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2901 {
2902 if (message)
2903 EMSG(_("E95: Buffer with this name already exists"));
2904 vim_free(ffname);
2905 return FAIL;
2906 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002907 /* delete from the list */
2908 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
2910 sfname = vim_strsave(sfname);
2911 if (ffname == NULL || sfname == NULL)
2912 {
2913 vim_free(sfname);
2914 vim_free(ffname);
2915 return FAIL;
2916 }
2917#ifdef USE_FNAME_CASE
2918# ifdef USE_LONG_FNAME
2919 if (USE_LONG_FNAME)
2920# endif
2921 fname_case(sfname, 0); /* set correct case for short file name */
2922#endif
2923 vim_free(buf->b_ffname);
2924 vim_free(buf->b_sfname);
2925 buf->b_ffname = ffname;
2926 buf->b_sfname = sfname;
2927 }
2928 buf->b_fname = buf->b_sfname;
2929#ifdef UNIX
2930 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002931 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 else
2933 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002934 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 buf->b_dev = st.st_dev;
2936 buf->b_ino = st.st_ino;
2937 }
2938#endif
2939
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942 buf_name_changed(buf);
2943 return OK;
2944}
2945
2946/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002947 * Crude way of changing the name of a buffer. Use with care!
2948 * The name should be relative to the current directory.
2949 */
2950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002951buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00002952{
2953 buf_T *buf;
2954
2955 buf = buflist_findnr(fnum);
2956 if (buf != NULL)
2957 {
2958 vim_free(buf->b_sfname);
2959 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002960 buf->b_ffname = vim_strsave(name);
2961 buf->b_sfname = NULL;
2962 /* Allocate ffname and expand into full path. Also resolves .lnk
2963 * files on Win32. */
2964 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002965 buf->b_fname = buf->b_sfname;
2966 }
2967}
2968
2969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 * Take care of what needs to be done when the name of buffer "buf" has
2971 * changed.
2972 */
2973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002974buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 /*
2977 * If the file name changed, also change the name of the swapfile
2978 */
2979 if (buf->b_ml.ml_mfp != NULL)
2980 ml_setname(buf);
2981
2982 if (curwin->w_buffer == buf)
2983 check_arg_idx(curwin); /* check file name for arg list */
2984#ifdef FEAT_TITLE
2985 maketitle(); /* set window title */
2986#endif
2987#ifdef FEAT_WINDOWS
2988 status_redraw_all(); /* status lines need to be redrawn */
2989#endif
2990 fmarks_check_names(buf); /* check named file marks */
2991 ml_timestamp(buf); /* reset timestamp */
2992}
2993
2994/*
2995 * set alternate file name for current window
2996 *
2997 * Used by do_one_cmd(), do_write() and do_ecmd().
2998 * Return the buffer.
2999 */
3000 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003001setaltfname(
3002 char_u *ffname,
3003 char_u *sfname,
3004 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 buf_T *buf;
3007
3008 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3009 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003010 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 curwin->w_alt_fnum = buf->b_fnum;
3012 return buf;
3013}
3014
3015/*
3016 * Get alternate file name for current window.
3017 * Return NULL if there isn't any, and give error message if requested.
3018 */
3019 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003020getaltfname(
3021 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 char_u *fname;
3024 linenr_T dummy;
3025
3026 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3027 {
3028 if (errmsg)
3029 EMSG(_(e_noalt));
3030 return NULL;
3031 }
3032 return fname;
3033}
3034
3035/*
3036 * Add a file name to the buflist and return its number.
3037 * Uses same flags as buflist_new(), except BLN_DUMMY.
3038 *
3039 * used by qf_init(), main() and doarglist()
3040 */
3041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003042buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
3044 buf_T *buf;
3045
3046 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3047 if (buf != NULL)
3048 return buf->b_fnum;
3049 return 0;
3050}
3051
3052#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3053/*
3054 * Adjust slashes in file names. Called after 'shellslash' was set.
3055 */
3056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003057buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 buf_T *bp;
3060
3061 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3062 {
3063 if (bp->b_ffname != NULL)
3064 slash_adjust(bp->b_ffname);
3065 if (bp->b_sfname != NULL)
3066 slash_adjust(bp->b_sfname);
3067 }
3068}
3069#endif
3070
3071/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003072 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 * Also save the local window option values.
3074 */
3075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003076buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003078 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079}
3080
3081/*
3082 * Return TRUE if 'ffname' is not the same file as current file.
3083 * Fname must have a full path (expanded by mch_FullName()).
3084 */
3085 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 return otherfile_buf(curbuf, ffname
3089#ifdef UNIX
3090 , NULL
3091#endif
3092 );
3093}
3094
3095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003096otherfile_buf(
3097 buf_T *buf,
3098 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003100 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003102 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 /* no name is different */
3105 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3106 return TRUE;
3107 if (fnamecmp(ffname, buf->b_ffname) == 0)
3108 return FALSE;
3109#ifdef UNIX
3110 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003111 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112
Bram Moolenaar8767f522016-07-01 17:17:39 +02003113 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 if (stp == NULL)
3115 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003116 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 st.st_dev = (dev_T)-1;
3118 stp = &st;
3119 }
3120 /* Use dev/ino to check if the files are the same, even when the names
3121 * are different (possible with links). Still need to compare the
3122 * name above, for when the file doesn't exist yet.
3123 * Problem: The dev/ino changes when a file is deleted (and created
3124 * again) and remains the same when renamed/moved. We don't want to
3125 * mch_stat() each buffer each time, that would be too slow. Get the
3126 * dev/ino again when they appear to match, but not when they appear
3127 * to be different: Could skip a buffer when it's actually the same
3128 * file. */
3129 if (buf_same_ino(buf, stp))
3130 {
3131 buf_setino(buf);
3132 if (buf_same_ino(buf, stp))
3133 return FALSE;
3134 }
3135 }
3136#endif
3137 return TRUE;
3138}
3139
3140#if defined(UNIX) || defined(PROTO)
3141/*
3142 * Set inode and device number for a buffer.
3143 * Must always be called when b_fname is changed!.
3144 */
3145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003146buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003148 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3151 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003152 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 buf->b_dev = st.st_dev;
3154 buf->b_ino = st.st_ino;
3155 }
3156 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003157 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158}
3159
3160/*
3161 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3162 */
3163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003164buf_same_ino(
3165 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003166 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003168 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 && stp->st_dev == buf->b_dev
3170 && stp->st_ino == buf->b_ino);
3171}
3172#endif
3173
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003174/*
3175 * Print info about the current buffer.
3176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003178fileinfo(
3179 int fullname, /* when non-zero print full path */
3180 int shorthelp,
3181 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 char_u *name;
3184 int n;
3185 char_u *p;
3186 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003187 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 buffer = alloc(IOSIZE);
3190 if (buffer == NULL)
3191 return;
3192
3193 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3194 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003195 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 p = buffer + STRLEN(buffer);
3197 }
3198 else
3199 p = buffer;
3200
3201 *p++ = '"';
3202 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003203 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 else
3205 {
3206 if (!fullname && curbuf->b_fname != NULL)
3207 name = curbuf->b_fname;
3208 else
3209 name = curbuf->b_ffname;
3210 home_replace(shorthelp ? curbuf : NULL, name, p,
3211 (int)(IOSIZE - (p - buffer)), TRUE);
3212 }
3213
Bram Moolenaara800b422010-06-27 01:15:55 +02003214 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 curbufIsChanged() ? (shortmess(SHM_MOD)
3216 ? " [+]" : _(" [Modified]")) : " ",
3217 (curbuf->b_flags & BF_NOTEDITED)
3218#ifdef FEAT_QUICKFIX
3219 && !bt_dontwrite(curbuf)
3220#endif
3221 ? _("[Not edited]") : "",
3222 (curbuf->b_flags & BF_NEW)
3223#ifdef FEAT_QUICKFIX
3224 && !bt_dontwrite(curbuf)
3225#endif
3226 ? _("[New file]") : "",
3227 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003228 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 : _("[readonly]")) : "",
3230 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3231 || curbuf->b_p_ro) ?
3232 " " : "");
3233 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3234 * causes an overflow, thus for large numbers divide instead. */
3235 if (curwin->w_cursor.lnum > 1000000L)
3236 n = (int)(((long)curwin->w_cursor.lnum) /
3237 ((long)curbuf->b_ml.ml_line_count / 100L));
3238 else
3239 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3240 (long)curbuf->b_ml.ml_line_count);
3241 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3242 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003243 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
3245#ifdef FEAT_CMDL_INFO
3246 else if (p_ru)
3247 {
3248 /* Current line and column are already on the screen -- webb */
3249 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003250 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003252 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 (long)curbuf->b_ml.ml_line_count, n);
3254 }
3255#endif
3256 else
3257 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003258 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 _("line %ld of %ld --%d%%-- col "),
3260 (long)curwin->w_cursor.lnum,
3261 (long)curbuf->b_ml.ml_line_count,
3262 n);
3263 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003264 len = STRLEN(buffer);
3265 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3267 }
3268
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003269 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
3271 if (dont_truncate)
3272 {
3273 /* Temporarily set msg_scroll to avoid the message being truncated.
3274 * First call msg_start() to get the message in the right place. */
3275 msg_start();
3276 n = msg_scroll;
3277 msg_scroll = TRUE;
3278 msg(buffer);
3279 msg_scroll = n;
3280 }
3281 else
3282 {
3283 p = msg_trunc_attr(buffer, FALSE, 0);
3284 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 /* Need to repeat the message after redrawing when:
3286 * - When restart_edit is set (otherwise there will be a delay
3287 * before redrawing).
3288 * - When the screen was scrolled but there is no wait-return
3289 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003290 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
3292
3293 vim_free(buffer);
3294}
3295
3296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003297col_print(
3298 char_u *buf,
3299 size_t buflen,
3300 int col,
3301 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
3303 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003304 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003306 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307}
3308
3309#if defined(FEAT_TITLE) || defined(PROTO)
3310/*
3311 * put file name in title bar of window and in icon title
3312 */
3313
3314static char_u *lasttitle = NULL;
3315static char_u *lasticon = NULL;
3316
3317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003318maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 char_u *p;
3321 char_u *t_str = NULL;
3322 char_u *i_name;
3323 char_u *i_str = NULL;
3324 int maxlen = 0;
3325 int len;
3326 int mustset;
3327 char_u buf[IOSIZE];
3328 int off;
3329
3330 if (!redrawing())
3331 {
3332 /* Postpone updating the title when 'lazyredraw' is set. */
3333 need_maketitle = TRUE;
3334 return;
3335 }
3336
3337 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003338 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 return;
3340
3341 if (p_title)
3342 {
3343 if (p_titlelen > 0)
3344 {
3345 maxlen = p_titlelen * Columns / 100;
3346 if (maxlen < 10)
3347 maxlen = 10;
3348 }
3349
3350 t_str = buf;
3351 if (*p_titlestring != NUL)
3352 {
3353#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003354 if (stl_syntax & STL_IN_TITLE)
3355 {
3356 int use_sandbox = FALSE;
3357 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003358
3359# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003360 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003361# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003362 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003364 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003365 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003366 if (called_emsg)
3367 set_string_option_direct((char_u *)"titlestring", -1,
3368 (char_u *)"", OPT_FREE, SID_ERROR);
3369 called_emsg |= save_called_emsg;
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else
3372#endif
3373 t_str = p_titlestring;
3374 }
3375 else
3376 {
3377 /* format: "fname + (path) (1 of 2) - VIM" */
3378
Bram Moolenaar2c666692012-09-05 13:30:40 +02003379#define SPACE_FOR_FNAME (IOSIZE - 100)
3380#define SPACE_FOR_DIR (IOSIZE - 20)
3381#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003383 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 else
3385 {
3386 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003387 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390
3391 switch (bufIsChanged(curbuf)
3392 + (curbuf->b_p_ro * 2)
3393 + (!curbuf->b_p_ma * 4))
3394 {
3395 case 1: STRCAT(buf, " +"); break;
3396 case 2: STRCAT(buf, " ="); break;
3397 case 3: STRCAT(buf, " =+"); break;
3398 case 4:
3399 case 6: STRCAT(buf, " -"); break;
3400 case 5:
3401 case 7: STRCAT(buf, " -+"); break;
3402 }
3403
3404 if (curbuf->b_fname != NULL)
3405 {
3406 /* Get path of file, replace home dir with ~ */
3407 off = (int)STRLEN(buf);
3408 buf[off++] = ' ';
3409 buf[off++] = '(';
3410 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003411 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#ifdef BACKSLASH_IN_FILENAME
3413 /* avoid "c:/name" to be reduced to "c" */
3414 if (isalpha(buf[off]) && buf[off + 1] == ':')
3415 off += 2;
3416#endif
3417 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003418 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003421 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003422 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003425
Bram Moolenaar2c666692012-09-05 13:30:40 +02003426 /* Translate unprintable chars and concatenate. Keep some
3427 * room for the server name. When there is no room (very long
3428 * file name) use (...). */
3429 if (off < SPACE_FOR_DIR)
3430 {
3431 p = transstr(buf + off);
3432 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3433 vim_free(p);
3434 }
3435 else
3436 {
3437 vim_strncpy(buf + off, (char_u *)"...",
3438 (size_t)(SPACE_FOR_ARGNR - off));
3439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 STRCAT(buf, ")");
3441 }
3442
Bram Moolenaar2c666692012-09-05 13:30:40 +02003443 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445#if defined(FEAT_CLIENTSERVER)
3446 if (serverName != NULL)
3447 {
3448 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003449 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
3451 else
3452#endif
3453 STRCAT(buf, " - VIM");
3454
3455 if (maxlen > 0)
3456 {
3457 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003458 if (vim_strsize(buf) > maxlen)
3459 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
3461 }
3462 }
3463 mustset = ti_change(t_str, &lasttitle);
3464
3465 if (p_icon)
3466 {
3467 i_str = buf;
3468 if (*p_iconstring != NUL)
3469 {
3470#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003471 if (stl_syntax & STL_IN_ICON)
3472 {
3473 int use_sandbox = FALSE;
3474 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003475
3476# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003477 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003478# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003479 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003481 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003482 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003483 if (called_emsg)
3484 set_string_option_direct((char_u *)"iconstring", -1,
3485 (char_u *)"", OPT_FREE, SID_ERROR);
3486 called_emsg |= save_called_emsg;
3487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 else
3489#endif
3490 i_str = p_iconstring;
3491 }
3492 else
3493 {
3494 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003495 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 else /* use file name only in icon */
3497 i_name = gettail(curbuf->b_ffname);
3498 *i_str = NUL;
3499 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003500 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (len > 100)
3502 {
3503 len -= 100;
3504#ifdef FEAT_MBYTE
3505 if (has_mbyte)
3506 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3507#endif
3508 i_name += len;
3509 }
3510 STRCPY(i_str, i_name);
3511 trans_characters(i_str, IOSIZE);
3512 }
3513 }
3514
3515 mustset |= ti_change(i_str, &lasticon);
3516
3517 if (mustset)
3518 resettitle();
3519}
3520
3521/*
3522 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3523 * from "str" if it does.
3524 * Return TRUE when "*last" changed.
3525 */
3526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003527ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
3529 if ((str == NULL) != (*last == NULL)
3530 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3531 {
3532 vim_free(*last);
3533 if (str == NULL)
3534 *last = NULL;
3535 else
3536 *last = vim_strsave(str);
3537 return TRUE;
3538 }
3539 return FALSE;
3540}
3541
3542/*
3543 * Put current window title back (used after calling a shell)
3544 */
3545 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003546resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
3548 mch_settitle(lasttitle, lasticon);
3549}
Bram Moolenaarea408852005-06-25 22:49:46 +00003550
3551# if defined(EXITFREE) || defined(PROTO)
3552 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003554{
3555 vim_free(lasttitle);
3556 vim_free(lasticon);
3557}
3558# endif
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560#endif /* FEAT_TITLE */
3561
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003562#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003564 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 * Return length of string in screen cells.
3566 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003567 * Normally works for window "wp", except when working for 'tabline' then it
3568 * is "curwin".
3569 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 * Items are drawn interspersed with the text that surrounds it
3571 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3572 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3573 *
3574 * If maxwidth is not zero, the string will be filled at any middle marker
3575 * or truncated if too long, fillchar is used for all whitespace.
3576 */
3577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003578build_stl_str_hl(
3579 win_T *wp,
3580 char_u *out, /* buffer to write into != NameBuff */
3581 size_t outlen, /* length of out[] */
3582 char_u *fmt,
3583 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3584 int fillchar,
3585 int maxwidth,
3586 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3587 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
3589 char_u *p;
3590 char_u *s;
3591 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003592 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593#ifdef FEAT_EVAL
3594 win_T *o_curwin;
3595 buf_T *o_curbuf;
3596#endif
3597 int empty_line;
3598 colnr_T virtcol;
3599 long l;
3600 long n;
3601 int prevchar_isflag;
3602 int prevchar_isitem;
3603 int itemisflag;
3604 int fillable;
3605 char_u *str;
3606 long num;
3607 int width;
3608 int itemcnt;
3609 int curitem;
3610 int groupitem[STL_MAX_ITEM];
3611 int groupdepth;
3612 struct stl_item
3613 {
3614 char_u *start;
3615 int minwid;
3616 int maxwid;
3617 enum
3618 {
3619 Normal,
3620 Empty,
3621 Group,
3622 Middle,
3623 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003624 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 Trunc
3626 } type;
3627 } item[STL_MAX_ITEM];
3628 int minwid;
3629 int maxwid;
3630 int zeropad;
3631 char_u base;
3632 char_u opt;
3633#define TMPLEN 70
3634 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003635 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003636 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003637
3638#ifdef FEAT_EVAL
3639 /*
3640 * When the format starts with "%!" then evaluate it as an expression and
3641 * use the result as the actual format string.
3642 */
3643 if (fmt[0] == '%' && fmt[1] == '!')
3644 {
3645 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3646 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003647 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003648 }
3649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 if (fillchar == 0)
3652 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003653#ifdef FEAT_MBYTE
3654 /* Can't handle a multi-byte fill character yet. */
3655 else if (mb_char2len(fillchar) > 1)
3656 fillchar = '-';
3657#endif
3658
Bram Moolenaar567199b2013-04-24 16:52:36 +02003659 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3660 * p will become invalid when getting another buffer line. */
3661 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3662 empty_line = (*p == NUL);
3663
3664 /* Get the byte value now, in case we need it below. This is more
3665 * efficient than making a copy of the line. */
3666 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3667 byteval = 0;
3668 else
3669#ifdef FEAT_MBYTE
3670 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3671#else
3672 byteval = p[wp->w_cursor.col];
3673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
3675 groupdepth = 0;
3676 p = out;
3677 curitem = 0;
3678 prevchar_isflag = TRUE;
3679 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003680 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003682 if (curitem == STL_MAX_ITEM)
3683 {
3684 /* There are too many items. Add the error code to the statusline
3685 * to give the user a hint about what went wrong. */
3686 if (p + 6 < out + outlen)
3687 {
3688 mch_memmove(p, " E541", (size_t)5);
3689 p += 5;
3690 }
3691 break;
3692 }
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 if (*s != NUL && *s != '%')
3695 prevchar_isflag = prevchar_isitem = FALSE;
3696
3697 /*
3698 * Handle up to the next '%' or the end.
3699 */
3700 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3701 *p++ = *s++;
3702 if (*s == NUL || p + 1 >= out + outlen)
3703 break;
3704
3705 /*
3706 * Handle one '%' item.
3707 */
3708 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003709 if (*s == NUL) /* ignore trailing % */
3710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 if (*s == '%')
3712 {
3713 if (p + 1 >= out + outlen)
3714 break;
3715 *p++ = *s++;
3716 prevchar_isflag = prevchar_isitem = FALSE;
3717 continue;
3718 }
3719 if (*s == STL_MIDDLEMARK)
3720 {
3721 s++;
3722 if (groupdepth > 0)
3723 continue;
3724 item[curitem].type = Middle;
3725 item[curitem++].start = p;
3726 continue;
3727 }
3728 if (*s == STL_TRUNCMARK)
3729 {
3730 s++;
3731 item[curitem].type = Trunc;
3732 item[curitem++].start = p;
3733 continue;
3734 }
3735 if (*s == ')')
3736 {
3737 s++;
3738 if (groupdepth < 1)
3739 continue;
3740 groupdepth--;
3741
3742 t = item[groupitem[groupdepth]].start;
3743 *p = NUL;
3744 l = vim_strsize(t);
3745 if (curitem > groupitem[groupdepth] + 1
3746 && item[groupitem[groupdepth]].minwid == 0)
3747 {
3748 /* remove group if all items are empty */
3749 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003750 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 break;
3752 if (n == curitem)
3753 {
3754 p = t;
3755 l = 0;
3756 }
3757 }
3758 if (l > item[groupitem[groupdepth]].maxwid)
3759 {
3760 /* truncate, remove n bytes of text at the start */
3761#ifdef FEAT_MBYTE
3762 if (has_mbyte)
3763 {
3764 /* Find the first character that should be included. */
3765 n = 0;
3766 while (l >= item[groupitem[groupdepth]].maxwid)
3767 {
3768 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003769 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771 }
3772 else
3773#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003774 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
3776 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003777 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 p = p - n + 1;
3779#ifdef FEAT_MBYTE
3780 /* Fill up space left over by half a double-wide char. */
3781 while (++l < item[groupitem[groupdepth]].minwid)
3782 *p++ = fillchar;
3783#endif
3784
3785 /* correct the start of the items for the truncation */
3786 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3787 {
3788 item[l].start -= n;
3789 if (item[l].start < t)
3790 item[l].start = t;
3791 }
3792 }
3793 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3794 {
3795 /* fill */
3796 n = item[groupitem[groupdepth]].minwid;
3797 if (n < 0)
3798 {
3799 /* fill by appending characters */
3800 n = 0 - n;
3801 while (l++ < n && p + 1 < out + outlen)
3802 *p++ = fillchar;
3803 }
3804 else
3805 {
3806 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003807 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 l = n - l;
3809 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003810 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 p += l;
3812 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3813 item[n].start += l;
3814 for ( ; l > 0; l--)
3815 *t++ = fillchar;
3816 }
3817 }
3818 continue;
3819 }
3820 minwid = 0;
3821 maxwid = 9999;
3822 zeropad = FALSE;
3823 l = 1;
3824 if (*s == '0')
3825 {
3826 s++;
3827 zeropad = TRUE;
3828 }
3829 if (*s == '-')
3830 {
3831 s++;
3832 l = -1;
3833 }
3834 if (VIM_ISDIGIT(*s))
3835 {
3836 minwid = (int)getdigits(&s);
3837 if (minwid < 0) /* overflow */
3838 minwid = 0;
3839 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003840 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
3842 item[curitem].type = Highlight;
3843 item[curitem].start = p;
3844 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3845 s++;
3846 curitem++;
3847 continue;
3848 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003849 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3850 {
3851 if (*s == STL_TABCLOSENR)
3852 {
3853 if (minwid == 0)
3854 {
3855 /* %X ends the close label, go back to the previously
3856 * define tab label nr. */
3857 for (n = curitem - 1; n >= 0; --n)
3858 if (item[n].type == TabPage && item[n].minwid >= 0)
3859 {
3860 minwid = item[n].minwid;
3861 break;
3862 }
3863 }
3864 else
3865 /* close nrs are stored as negative values */
3866 minwid = - minwid;
3867 }
3868 item[curitem].type = TabPage;
3869 item[curitem].start = p;
3870 item[curitem].minwid = minwid;
3871 s++;
3872 curitem++;
3873 continue;
3874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (*s == '.')
3876 {
3877 s++;
3878 if (VIM_ISDIGIT(*s))
3879 {
3880 maxwid = (int)getdigits(&s);
3881 if (maxwid <= 0) /* overflow */
3882 maxwid = 50;
3883 }
3884 }
3885 minwid = (minwid > 50 ? 50 : minwid) * l;
3886 if (*s == '(')
3887 {
3888 groupitem[groupdepth++] = curitem;
3889 item[curitem].type = Group;
3890 item[curitem].start = p;
3891 item[curitem].minwid = minwid;
3892 item[curitem].maxwid = maxwid;
3893 s++;
3894 curitem++;
3895 continue;
3896 }
3897 if (vim_strchr(STL_ALL, *s) == NULL)
3898 {
3899 s++;
3900 continue;
3901 }
3902 opt = *s++;
3903
3904 /* OK - now for the real work */
3905 base = 'D';
3906 itemisflag = FALSE;
3907 fillable = TRUE;
3908 num = -1;
3909 str = NULL;
3910 switch (opt)
3911 {
3912 case STL_FILEPATH:
3913 case STL_FULLPATH:
3914 case STL_FILENAME:
3915 fillable = FALSE; /* don't change ' ' to fillchar */
3916 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003917 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 else
3919 {
3920 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003921 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3923 }
3924 trans_characters(NameBuff, MAXPATHL);
3925 if (opt != STL_FILENAME)
3926 str = NameBuff;
3927 else
3928 str = gettail(NameBuff);
3929 break;
3930
3931 case STL_VIM_EXPR: /* '{' */
3932 itemisflag = TRUE;
3933 t = p;
3934 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3935 *p++ = *s++;
3936 if (*s != '}') /* missing '}' or out of space */
3937 break;
3938 s++;
3939 *p = 0;
3940 p = t;
3941
3942#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003943 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3945
3946 o_curbuf = curbuf;
3947 o_curwin = curwin;
3948 curwin = wp;
3949 curbuf = wp->w_buffer;
3950
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003951 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953 curwin = o_curwin;
3954 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003955 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
3957 if (str != NULL && *str != 0)
3958 {
3959 if (*skipdigits(str) == NUL)
3960 {
3961 num = atoi((char *)str);
3962 vim_free(str);
3963 str = NULL;
3964 itemisflag = FALSE;
3965 }
3966 }
3967#endif
3968 break;
3969
3970 case STL_LINE:
3971 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3972 ? 0L : (long)(wp->w_cursor.lnum);
3973 break;
3974
3975 case STL_NUMLINES:
3976 num = wp->w_buffer->b_ml.ml_line_count;
3977 break;
3978
3979 case STL_COLUMN:
3980 num = !(State & INSERT) && empty_line
3981 ? 0 : (int)wp->w_cursor.col + 1;
3982 break;
3983
3984 case STL_VIRTCOL:
3985 case STL_VIRTCOL_ALT:
3986 /* In list mode virtcol needs to be recomputed */
3987 virtcol = wp->w_virtcol;
3988 if (wp->w_p_list && lcs_tab1 == NUL)
3989 {
3990 wp->w_p_list = FALSE;
3991 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3992 wp->w_p_list = TRUE;
3993 }
3994 ++virtcol;
3995 /* Don't display %V if it's the same as %c. */
3996 if (opt == STL_VIRTCOL_ALT
3997 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3998 ? 0 : (int)wp->w_cursor.col + 1)))
3999 break;
4000 num = (long)virtcol;
4001 break;
4002
4003 case STL_PERCENTAGE:
4004 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4005 (long)wp->w_buffer->b_ml.ml_line_count);
4006 break;
4007
4008 case STL_ALTPERCENT:
4009 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004010 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 break;
4012
4013 case STL_ARGLISTSTAT:
4014 fillable = FALSE;
4015 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004016 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 str = tmp;
4018 break;
4019
4020 case STL_KEYMAP:
4021 fillable = FALSE;
4022 if (get_keymap_str(wp, tmp, TMPLEN))
4023 str = tmp;
4024 break;
4025 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004026#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004027 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028#else
4029 num = 0;
4030#endif
4031 break;
4032
4033 case STL_BUFNO:
4034 num = wp->w_buffer->b_fnum;
4035 break;
4036
4037 case STL_OFFSET_X:
4038 base = 'X';
4039 case STL_OFFSET:
4040#ifdef FEAT_BYTEOFF
4041 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4042 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4043 0L : l + 1 + (!(State & INSERT) && empty_line ?
4044 0 : (int)wp->w_cursor.col);
4045#endif
4046 break;
4047
4048 case STL_BYTEVAL_X:
4049 base = 'X';
4050 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004051 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 if (num == NL)
4053 num = 0;
4054 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4055 num = NL;
4056 break;
4057
4058 case STL_ROFLAG:
4059 case STL_ROFLAG_ALT:
4060 itemisflag = TRUE;
4061 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004062 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 break;
4064
4065 case STL_HELPFLAG:
4066 case STL_HELPFLAG_ALT:
4067 itemisflag = TRUE;
4068 if (wp->w_buffer->b_help)
4069 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004070 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 break;
4072
4073#ifdef FEAT_AUTOCMD
4074 case STL_FILETYPE:
4075 if (*wp->w_buffer->b_p_ft != NUL
4076 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4077 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004078 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4079 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 str = tmp;
4081 }
4082 break;
4083
4084 case STL_FILETYPE_ALT:
4085 itemisflag = TRUE;
4086 if (*wp->w_buffer->b_p_ft != NUL
4087 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4088 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004089 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4090 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 for (t = tmp; *t != 0; t++)
4092 *t = TOUPPER_LOC(*t);
4093 str = tmp;
4094 }
4095 break;
4096#endif
4097
4098#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4099 case STL_PREVIEWFLAG:
4100 case STL_PREVIEWFLAG_ALT:
4101 itemisflag = TRUE;
4102 if (wp->w_p_pvw)
4103 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4104 : _("[Preview]"));
4105 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004106
4107 case STL_QUICKFIX:
4108 if (bt_quickfix(wp->w_buffer))
4109 str = (char_u *)(wp->w_llist_ref
4110 ? _(msg_loclist)
4111 : _(msg_qflist));
4112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#endif
4114
4115 case STL_MODIFIED:
4116 case STL_MODIFIED_ALT:
4117 itemisflag = TRUE;
4118 switch ((opt == STL_MODIFIED_ALT)
4119 + bufIsChanged(wp->w_buffer) * 2
4120 + (!wp->w_buffer->b_p_ma) * 4)
4121 {
4122 case 2: str = (char_u *)"[+]"; break;
4123 case 3: str = (char_u *)",+"; break;
4124 case 4: str = (char_u *)"[-]"; break;
4125 case 5: str = (char_u *)",-"; break;
4126 case 6: str = (char_u *)"[+-]"; break;
4127 case 7: str = (char_u *)",+-"; break;
4128 }
4129 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004130
4131 case STL_HIGHLIGHT:
4132 t = s;
4133 while (*s != '#' && *s != NUL)
4134 ++s;
4135 if (*s == '#')
4136 {
4137 item[curitem].type = Highlight;
4138 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004139 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004140 curitem++;
4141 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004142 if (*s != NUL)
4143 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004144 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146
4147 item[curitem].start = p;
4148 item[curitem].type = Normal;
4149 if (str != NULL && *str)
4150 {
4151 t = str;
4152 if (itemisflag)
4153 {
4154 if ((t[0] && t[1])
4155 && ((!prevchar_isitem && *t == ',')
4156 || (prevchar_isflag && *t == ' ')))
4157 t++;
4158 prevchar_isflag = TRUE;
4159 }
4160 l = vim_strsize(t);
4161 if (l > 0)
4162 prevchar_isitem = TRUE;
4163 if (l > maxwid)
4164 {
4165 while (l >= maxwid)
4166#ifdef FEAT_MBYTE
4167 if (has_mbyte)
4168 {
4169 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004170 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 else
4173#endif
4174 l -= byte2cells(*t++);
4175 if (p + 1 >= out + outlen)
4176 break;
4177 *p++ = '<';
4178 }
4179 if (minwid > 0)
4180 {
4181 for (; l < minwid && p + 1 < out + outlen; l++)
4182 {
4183 /* Don't put a "-" in front of a digit. */
4184 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4185 *p++ = ' ';
4186 else
4187 *p++ = fillchar;
4188 }
4189 minwid = 0;
4190 }
4191 else
4192 minwid *= -1;
4193 while (*t && p + 1 < out + outlen)
4194 {
4195 *p++ = *t++;
4196 /* Change a space by fillchar, unless fillchar is '-' and a
4197 * digit follows. */
4198 if (fillable && p[-1] == ' '
4199 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4200 p[-1] = fillchar;
4201 }
4202 for (; l < minwid && p + 1 < out + outlen; l++)
4203 *p++ = fillchar;
4204 }
4205 else if (num >= 0)
4206 {
4207 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4208 char_u nstr[20];
4209
4210 if (p + 20 >= out + outlen)
4211 break; /* not sufficient space */
4212 prevchar_isitem = TRUE;
4213 t = nstr;
4214 if (opt == STL_VIRTCOL_ALT)
4215 {
4216 *t++ = '-';
4217 minwid--;
4218 }
4219 *t++ = '%';
4220 if (zeropad)
4221 *t++ = '0';
4222 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004223 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 *t = 0;
4225
4226 for (n = num, l = 1; n >= nbase; n /= nbase)
4227 l++;
4228 if (opt == STL_VIRTCOL_ALT)
4229 l++;
4230 if (l > maxwid)
4231 {
4232 l += 2;
4233 n = l - maxwid;
4234 while (l-- > maxwid)
4235 num /= nbase;
4236 *t++ = '>';
4237 *t++ = '%';
4238 *t = t[-3];
4239 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004240 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4241 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004244 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4245 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 p += STRLEN(p);
4247 }
4248 else
4249 item[curitem].type = Empty;
4250
4251 if (opt == STL_VIM_EXPR)
4252 vim_free(str);
4253
4254 if (num >= 0 || (!itemisflag && str && *str))
4255 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4256 curitem++;
4257 }
4258 *p = NUL;
4259 itemcnt = curitem;
4260
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004261#ifdef FEAT_EVAL
4262 if (usefmt != fmt)
4263 vim_free(usefmt);
4264#endif
4265
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 width = vim_strsize(out);
4267 if (maxwidth > 0 && width > maxwidth)
4268 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004269 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 l = 0;
4271 if (itemcnt == 0)
4272 s = out;
4273 else
4274 {
4275 for ( ; l < itemcnt; l++)
4276 if (item[l].type == Trunc)
4277 {
4278 /* Truncate at %< item. */
4279 s = item[l].start;
4280 break;
4281 }
4282 if (l == itemcnt)
4283 {
4284 /* No %< item, truncate first item. */
4285 s = item[0].start;
4286 l = 0;
4287 }
4288 }
4289
4290 if (width - vim_strsize(s) >= maxwidth)
4291 {
4292 /* Truncation mark is beyond max length */
4293#ifdef FEAT_MBYTE
4294 if (has_mbyte)
4295 {
4296 s = out;
4297 width = 0;
4298 for (;;)
4299 {
4300 width += ptr2cells(s);
4301 if (width >= maxwidth)
4302 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004303 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 /* Fill up for half a double-wide character. */
4306 while (++width < maxwidth)
4307 *s++ = fillchar;
4308 }
4309 else
4310#endif
4311 s = out + maxwidth - 1;
4312 for (l = 0; l < itemcnt; l++)
4313 if (item[l].start > s)
4314 break;
4315 itemcnt = l;
4316 *s++ = '>';
4317 *s = 0;
4318 }
4319 else
4320 {
4321#ifdef FEAT_MBYTE
4322 if (has_mbyte)
4323 {
4324 n = 0;
4325 while (width >= maxwidth)
4326 {
4327 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004328 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330 }
4331 else
4332#endif
4333 n = width - maxwidth + 1;
4334 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004335 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 *s = '<';
4337
4338 /* Fill up for half a double-wide character. */
4339 while (++width < maxwidth)
4340 {
4341 s = s + STRLEN(s);
4342 *s++ = fillchar;
4343 *s = NUL;
4344 }
4345
4346 --n; /* count the '<' */
4347 for (; l < itemcnt; l++)
4348 {
4349 if (item[l].start - n >= s)
4350 item[l].start -= n;
4351 else
4352 item[l].start = s;
4353 }
4354 }
4355 width = maxwidth;
4356 }
4357 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4358 {
4359 /* Apply STL_MIDDLE if any */
4360 for (l = 0; l < itemcnt; l++)
4361 if (item[l].type == Middle)
4362 break;
4363 if (l < itemcnt)
4364 {
4365 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004366 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 for (s = item[l].start; s < p; s++)
4368 *s = fillchar;
4369 for (l++; l < itemcnt; l++)
4370 item[l].start += maxwidth - width;
4371 width = maxwidth;
4372 }
4373 }
4374
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004375 /* Store the info about highlighting. */
4376 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004378 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 for (l = 0; l < itemcnt; l++)
4380 {
4381 if (item[l].type == Highlight)
4382 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004383 sp->start = item[l].start;
4384 sp->userhl = item[l].minwid;
4385 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
4387 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004388 sp->start = NULL;
4389 sp->userhl = 0;
4390 }
4391
4392 /* Store the info about tab pages labels. */
4393 if (tabtab != NULL)
4394 {
4395 sp = tabtab;
4396 for (l = 0; l < itemcnt; l++)
4397 {
4398 if (item[l].type == TabPage)
4399 {
4400 sp->start = item[l].start;
4401 sp->userhl = item[l].minwid;
4402 sp++;
4403 }
4404 }
4405 sp->start = NULL;
4406 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
4408
4409 return width;
4410}
4411#endif /* FEAT_STL_OPT */
4412
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004413#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4414 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004416 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4417 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 */
4419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004420get_rel_pos(
4421 win_T *wp,
4422 char_u *buf,
4423 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 long above; /* number of lines above window */
4426 long below; /* number of lines below window */
4427
Bram Moolenaar0027c212015-01-07 13:31:52 +01004428 if (buflen < 3) /* need at least 3 chars for writing */
4429 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 above = wp->w_topline - 1;
4431#ifdef FEAT_DIFF
4432 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004433 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4434 above = 0; /* All buffer lines are displayed and there is an
4435 * indication of filler lines, that can be considered
4436 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437#endif
4438 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4439 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004440 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4441 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004443 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004445 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 ? (int)(above / ((above + below) / 100L))
4447 : (int)(above * 100L / (above + below)));
4448}
4449#endif
4450
4451/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004452 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 * Return TRUE if it was appended.
4454 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004456append_arg_number(
4457 win_T *wp,
4458 char_u *buf,
4459 int buflen,
4460 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461{
4462 char_u *p;
4463
4464 if (ARGCOUNT <= 1) /* nothing to do */
4465 return FALSE;
4466
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004467 p = buf + STRLEN(buf); /* go to the end of the buffer */
4468 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 return FALSE;
4470 *p++ = ' ';
4471 *p++ = '(';
4472 if (add_file)
4473 {
4474 STRCPY(p, "file ");
4475 p += 5;
4476 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004477 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4478 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4480 return TRUE;
4481}
4482
4483/*
4484 * If fname is not a full path, make it a full path.
4485 * Returns pointer to allocated memory (NULL for failure).
4486 */
4487 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004488fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489{
4490 /*
4491 * Force expanding the path always for Unix, because symbolic links may
4492 * mess up the full path name, even though it starts with a '/'.
4493 * Also expand when there is ".." in the file name, try to remove it,
4494 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004495 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 * For MS-Windows also expand names like "longna~1" to "longname".
4497 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004498#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 return FullName_save(fname, TRUE);
4500#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004501 if (!vim_isAbsName(fname)
4502 || strstr((char *)fname, "..") != NULL
4503 || strstr((char *)fname, "//") != NULL
4504# ifdef BACKSLASH_IN_FILENAME
4505 || strstr((char *)fname, "\\\\") != NULL
4506# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004507# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 )
4511 return FullName_save(fname, FALSE);
4512
4513 fname = vim_strsave(fname);
4514
Bram Moolenaar9b942202007-10-03 12:31:33 +00004515# ifdef USE_FNAME_CASE
4516# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
4520 if (fname != NULL)
4521 fname_case(fname, 0); /* set correct case for file name */
4522 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
4525 return fname;
4526#endif
4527}
4528
4529/*
4530 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4531 * "ffname" becomes a pointer to allocated memory (or NULL).
4532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004534fname_expand(
4535 buf_T *buf UNUSED,
4536 char_u **ffname,
4537 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538{
4539 if (*ffname == NULL) /* if no file name given, nothing to do */
4540 return;
4541 if (*sfname == NULL) /* if no short file name given, use ffname */
4542 *sfname = *ffname;
4543 *ffname = fix_fname(*ffname); /* expand to full path */
4544
4545#ifdef FEAT_SHORTCUT
4546 if (!buf->b_p_bin)
4547 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004548 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 /* If the file name is a shortcut file, use the file it links to. */
4551 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004552 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
4554 vim_free(*ffname);
4555 *ffname = rfname;
4556 *sfname = rfname;
4557 }
4558 }
4559#endif
4560}
4561
4562/*
4563 * Get the file name for an argument list entry.
4564 */
4565 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004566alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567{
4568 buf_T *bp;
4569
4570 /* Use the name from the associated buffer if it exists. */
4571 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004572 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 return aep->ae_fname;
4574 return bp->b_fname;
4575}
4576
4577#if defined(FEAT_WINDOWS) || defined(PROTO)
4578/*
4579 * do_arg_all(): Open up to 'count' windows, one for each argument.
4580 */
4581 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004582do_arg_all(
4583 int count,
4584 int forceit, /* hide buffers in current windows */
4585 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586{
4587 int i;
4588 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004589 char_u *opened; /* Array of weight for which args are open:
4590 * 0: not opened
4591 * 1: opened in other tab
4592 * 2: opened in curtab
4593 * 3: opened in curtab and curwin
4594 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004595 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 int use_firstwin = FALSE; /* use first window for arglist */
4597 int split_ret = OK;
4598 int p_ea_save;
4599 alist_T *alist; /* argument list to be used */
4600 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004601 tabpage_T *tpnext;
4602 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004603 win_T *old_curwin, *last_curwin;
4604 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004605 win_T *new_curwin = NULL;
4606 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
4608 if (ARGCOUNT <= 0)
4609 {
4610 /* Don't give an error message. We don't want it when the ":all"
4611 * command is in the .vimrc. */
4612 return;
4613 }
4614 setpcmark();
4615
4616 opened_len = ARGCOUNT;
4617 opened = alloc_clear((unsigned)opened_len);
4618 if (opened == NULL)
4619 return;
4620
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004621 /* Autocommands may do anything to the argument list. Make sure it's not
4622 * freed while we are working here by "locking" it. We still have to
4623 * watch out for its size to be changed. */
4624 alist = curwin->w_alist;
4625 ++alist->al_refcount;
4626
4627 old_curwin = curwin;
4628 old_curtab = curtab;
4629
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004630# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
4634 /*
4635 * Try closing all windows that are not in the argument list.
4636 * Also close windows that are not full width;
4637 * When 'hidden' or "forceit" set the buffer becomes hidden.
4638 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004639 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004641 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004642 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004643 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004645 tpnext = curtab->tp_next;
4646 for (wp = firstwin; wp != NULL; wp = wpnext)
4647 {
4648 wpnext = wp->w_next;
4649 buf = wp->w_buffer;
4650 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004651 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004652 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004653 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004656 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004657 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004659 if (i < alist->al_ga.ga_len
4660 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4661 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4662 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004664 int weight = 1;
4665
4666 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004667 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004668 ++weight;
4669 if (old_curwin == wp)
4670 ++weight;
4671 }
4672
4673 if (weight > (int)opened[i])
4674 {
4675 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004676 if (i == 0)
4677 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004678 if (new_curwin != NULL)
4679 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004680 new_curwin = wp;
4681 new_curtab = curtab;
4682 }
4683 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004684 else if (keep_tabs)
4685 i = opened_len;
4686
4687 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004688 {
4689 /* Use the current argument list for all windows
4690 * containing a file from it. */
4691 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004692 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004693 ++wp->w_alist->al_refcount;
4694 }
4695 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004699 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004701 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004703 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004704 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004706 /* If the buffer was changed, and we would like to hide it,
4707 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004708 if (!P_HID(buf) && buf->b_nwindows <= 1
4709 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004711 (void)autowrite(buf, FALSE);
4712#ifdef FEAT_AUTOCMD
4713 /* check if autocommands removed the window */
4714 if (!win_valid(wp) || !buf_valid(buf))
4715 {
4716 wpnext = firstwin; /* start all over... */
4717 continue;
4718 }
4719#endif
4720 }
4721#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004722 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004723 if (firstwin == lastwin
4724 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004725#endif
4726 use_firstwin = TRUE;
4727#ifdef FEAT_WINDOWS
4728 else
4729 {
4730 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4731# ifdef FEAT_AUTOCMD
4732 /* check if autocommands removed the next window */
4733 if (!win_valid(wpnext))
4734 wpnext = firstwin; /* start all over... */
4735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737#endif
4738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
4740 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004741
4742 /* Without the ":tab" modifier only do the current tab page. */
4743 if (had_tab == 0 || tpnext == NULL)
4744 break;
4745
4746# ifdef FEAT_AUTOCMD
4747 /* check if autocommands removed the next tab page */
4748 if (!valid_tabpage(tpnext))
4749 tpnext = first_tabpage; /* start all over...*/
4750# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004751 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
4753
4754 /*
4755 * Open a window for files in the argument list that don't have one.
4756 * ARGCOUNT may change while doing this, because of autocommands.
4757 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004758 if (count > opened_len || count <= 0)
4759 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761#ifdef FEAT_AUTOCMD
4762 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4763 ++autocmd_no_enter;
4764 ++autocmd_no_leave;
4765#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004766 last_curwin = curwin;
4767 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004769#ifdef FEAT_WINDOWS
4770 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4771 * leaving an empty tab page when executed locally. */
4772 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4773 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4774 use_firstwin = TRUE;
4775#endif
4776
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004777 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
4779 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4780 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004781 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 {
4783 /* Move the already present window to below the current window */
4784 if (curwin->w_arg_idx != i)
4785 {
4786 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4787 {
4788 if (wpnext->w_arg_idx == i)
4789 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004790 if (keep_tabs)
4791 {
4792 new_curwin = wpnext;
4793 new_curtab = curtab;
4794 }
4795 else
4796 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 break;
4798 }
4799 }
4800 }
4801 }
4802 else if (split_ret == OK)
4803 {
4804 if (!use_firstwin) /* split current window */
4805 {
4806 p_ea_save = p_ea;
4807 p_ea = TRUE; /* use space from all windows */
4808 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4809 p_ea = p_ea_save;
4810 if (split_ret == FAIL)
4811 continue;
4812 }
4813#ifdef FEAT_AUTOCMD
4814 else /* first window: do autocmd for leaving this buffer */
4815 --autocmd_no_leave;
4816#endif
4817
4818 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004819 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 */
4821 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004822 if (i == 0)
4823 {
4824 new_curwin = curwin;
4825 new_curtab = curtab;
4826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4828 ECMD_ONE,
4829 ((P_HID(curwin->w_buffer)
4830 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004831 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832#ifdef FEAT_AUTOCMD
4833 if (use_firstwin)
4834 ++autocmd_no_leave;
4835#endif
4836 use_firstwin = FALSE;
4837 }
4838 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004839
4840 /* When ":tab" was used open a new tab for a new window repeatedly. */
4841 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4842 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844
4845 /* Remove the "lock" on the argument list. */
4846 alist_unlink(alist);
4847
4848#ifdef FEAT_AUTOCMD
4849 --autocmd_no_enter;
4850#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004851 /* restore last referenced tabpage's curwin */
4852 if (last_curtab != new_curtab)
4853 {
4854 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004855 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004856 if (win_valid(last_curwin))
4857 win_enter(last_curwin, FALSE);
4858 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004859 /* to window with first arg */
4860 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004861 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004862 if (win_valid(new_curwin))
4863 win_enter(new_curwin, FALSE);
4864
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865#ifdef FEAT_AUTOCMD
4866 --autocmd_no_leave;
4867#endif
4868 vim_free(opened);
4869}
4870
4871# if defined(FEAT_LISTCMDS) || defined(PROTO)
4872/*
4873 * Open a window for a number of buffers.
4874 */
4875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004876ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
4878 buf_T *buf;
4879 win_T *wp, *wpnext;
4880 int split_ret = OK;
4881 int p_ea_save;
4882 int open_wins = 0;
4883 int r;
4884 int count; /* Maximum number of windows to open. */
4885 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004886#ifdef FEAT_WINDOWS
4887 int had_tab = cmdmod.tab;
4888 tabpage_T *tpnext;
4889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
4891 if (eap->addr_count == 0) /* make as many windows as possible */
4892 count = 9999;
4893 else
4894 count = eap->line2; /* make as many windows as specified */
4895 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4896 all = FALSE;
4897 else
4898 all = TRUE;
4899
4900 setpcmark();
4901
4902#ifdef FEAT_GUI
4903 need_mouse_correct = TRUE;
4904#endif
4905
4906 /*
4907 * Close superfluous windows (two windows for the same buffer).
4908 * Also close windows that are not full-width.
4909 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004910#ifdef FEAT_WINDOWS
4911 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004912 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004913 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004916 tpnext = curtab->tp_next;
4917 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004919 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004920 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004921#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004922 || ((cmdmod.split & WSP_VERT)
4923 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004924 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004925 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004926 || (had_tab > 0 && wp != firstwin)
4927#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004928 ) && firstwin != lastwin
4929#ifdef FEAT_AUTOCMD
4930 && !(wp->w_closing || wp->w_buffer->b_closing)
4931#endif
4932 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004933 {
4934 win_close(wp, FALSE);
4935#ifdef FEAT_AUTOCMD
4936 wpnext = firstwin; /* just in case an autocommand does
4937 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004938 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004939 open_wins = 0;
4940#endif
4941 }
4942 else
4943 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004945
4946#ifdef FEAT_WINDOWS
4947 /* Without the ":tab" modifier only do the current tab page. */
4948 if (had_tab == 0 || tpnext == NULL)
4949 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004950 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /*
4955 * Go through the buffer list. When a buffer doesn't have a window yet,
4956 * open one. Otherwise move the window to the right position.
4957 * Watch out for autocommands that delete buffers or windows!
4958 */
4959#ifdef FEAT_AUTOCMD
4960 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4961 ++autocmd_no_enter;
4962#endif
4963 win_enter(lastwin, FALSE);
4964#ifdef FEAT_AUTOCMD
4965 ++autocmd_no_leave;
4966#endif
4967 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4968 {
4969 /* Check if this buffer needs a window */
4970 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4971 continue;
4972
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004973#ifdef FEAT_WINDOWS
4974 if (had_tab != 0)
4975 {
4976 /* With the ":tab" modifier don't move the window. */
4977 if (buf->b_nwindows > 0)
4978 wp = lastwin; /* buffer has a window, skip it */
4979 else
4980 wp = NULL;
4981 }
4982 else
4983#endif
4984 {
4985 /* Check if this buffer already has a window */
4986 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4987 if (wp->w_buffer == buf)
4988 break;
4989 /* If the buffer already has a window, move it */
4990 if (wp != NULL)
4991 win_move_after(wp, curwin);
4992 }
4993
4994 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
4996 /* Split the window and put the buffer in it */
4997 p_ea_save = p_ea;
4998 p_ea = TRUE; /* use space from all windows */
4999 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5000 ++open_wins;
5001 p_ea = p_ea_save;
5002 if (split_ret == FAIL)
5003 continue;
5004
5005 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005006#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 swap_exists_action = SEA_DIALOG;
5008#endif
5009 set_curbuf(buf, DOBUF_GOTO);
5010#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00005013#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 break;
5017 }
5018#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005019#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (swap_exists_action == SEA_QUIT)
5021 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005022# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5023 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005024
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005025 /* Reset the error/interrupt/exception state here so that
5026 * aborting() returns FALSE when closing a window. */
5027 enter_cleanup(&cs);
5028# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005029
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005030 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 win_close(curwin, TRUE);
5032 --open_wins;
5033 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005034 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005035
5036# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5037 /* Restore the error/interrupt/exception state if not
5038 * discarded by a new aborting error, interrupt, or uncaught
5039 * exception. */
5040 leave_cleanup(&cs);
5041# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
5043 else
5044 handle_swap_exists(NULL);
5045#endif
5046 }
5047
5048 ui_breakcheck();
5049 if (got_int)
5050 {
5051 (void)vgetc(); /* only break the file loading, not the rest */
5052 break;
5053 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005054#ifdef FEAT_EVAL
5055 /* Autocommands deleted the buffer or aborted script processing!!! */
5056 if (aborting())
5057 break;
5058#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005059#ifdef FEAT_WINDOWS
5060 /* When ":tab" was used open a new tab for a new window repeatedly. */
5061 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5062 cmdmod.tab = 9999;
5063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065#ifdef FEAT_AUTOCMD
5066 --autocmd_no_enter;
5067#endif
5068 win_enter(firstwin, FALSE); /* back to first window */
5069#ifdef FEAT_AUTOCMD
5070 --autocmd_no_leave;
5071#endif
5072
5073 /*
5074 * Close superfluous windows.
5075 */
5076 for (wp = lastwin; open_wins > count; )
5077 {
5078 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5079 || autowrite(wp->w_buffer, FALSE) == OK);
5080#ifdef FEAT_AUTOCMD
5081 if (!win_valid(wp))
5082 {
5083 /* BufWrite Autocommands made the window invalid, start over */
5084 wp = lastwin;
5085 }
5086 else
5087#endif
5088 if (r)
5089 {
5090 win_close(wp, !P_HID(wp->w_buffer));
5091 --open_wins;
5092 wp = lastwin;
5093 }
5094 else
5095 {
5096 wp = wp->w_prev;
5097 if (wp == NULL)
5098 break;
5099 }
5100 }
5101}
5102# endif /* FEAT_LISTCMDS */
5103
5104#endif /* FEAT_WINDOWS */
5105
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005106static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108/*
5109 * do_modelines() - process mode lines for the current file
5110 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005111 * "flags" can be:
5112 * OPT_WINONLY only set options local to window
5113 * OPT_NOWIN don't set options local to window
5114 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 * Returns immediately if the "ml" option isn't set.
5116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005118do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005120 linenr_T lnum;
5121 int nmlines;
5122 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5125 return;
5126
5127 /* Disallow recursive entry here. Can happen when executing a modeline
5128 * triggers an autocommand, which reloads modelines with a ":do". */
5129 if (entered)
5130 return;
5131
5132 ++entered;
5133 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5134 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005135 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 nmlines = 0;
5137
5138 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5139 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005140 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 nmlines = 0;
5142 --entered;
5143}
5144
5145#include "version.h" /* for version number */
5146
5147/*
5148 * chk_modeline() - check a single line for a mode string
5149 * Return FAIL if an error encountered.
5150 */
5151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005152chk_modeline(
5153 linenr_T lnum,
5154 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155{
5156 char_u *s;
5157 char_u *e;
5158 char_u *linecopy; /* local copy of any modeline found */
5159 int prev;
5160 int vers;
5161 int end;
5162 int retval = OK;
5163 char_u *save_sourcing_name;
5164 linenr_T save_sourcing_lnum;
5165#ifdef FEAT_EVAL
5166 scid_T save_SID;
5167#endif
5168
5169 prev = -1;
5170 for (s = ml_get(lnum); *s != NUL; ++s)
5171 {
5172 if (prev == -1 || vim_isspace(prev))
5173 {
5174 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5175 || STRNCMP(s, "vi:", (size_t)3) == 0)
5176 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005177 /* Accept both "vim" and "Vim". */
5178 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
5180 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5181 e = s + 4;
5182 else
5183 e = s + 3;
5184 vers = getdigits(&e);
5185 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005186 && (s[0] != 'V'
5187 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 && (s[3] == ':'
5189 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5190 || (VIM_VERSION_100 < vers && s[3] == '<')
5191 || (VIM_VERSION_100 > vers && s[3] == '>')
5192 || (VIM_VERSION_100 == vers && s[3] == '=')))
5193 break;
5194 }
5195 }
5196 prev = *s;
5197 }
5198
5199 if (*s)
5200 {
5201 do /* skip over "ex:", "vi:" or "vim:" */
5202 ++s;
5203 while (s[-1] != ':');
5204
5205 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5206 if (linecopy == NULL)
5207 return FAIL;
5208
5209 save_sourcing_lnum = sourcing_lnum;
5210 save_sourcing_name = sourcing_name;
5211 sourcing_lnum = lnum; /* prepare for emsg() */
5212 sourcing_name = (char_u *)"modelines";
5213
5214 end = FALSE;
5215 while (end == FALSE)
5216 {
5217 s = skipwhite(s);
5218 if (*s == NUL)
5219 break;
5220
5221 /*
5222 * Find end of set command: ':' or end of line.
5223 * Skip over "\:", replacing it with ":".
5224 */
5225 for (e = s; *e != ':' && *e != NUL; ++e)
5226 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005227 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (*e == NUL)
5229 end = TRUE;
5230
5231 /*
5232 * If there is a "set" command, require a terminating ':' and
5233 * ignore the stuff after the ':'.
5234 * "vi:set opt opt opt: foo" -- foo not interpreted
5235 * "vi:opt opt opt: foo" -- foo interpreted
5236 * Accept "se" for compatibility with Elvis.
5237 */
5238 if (STRNCMP(s, "set ", (size_t)4) == 0
5239 || STRNCMP(s, "se ", (size_t)3) == 0)
5240 {
5241 if (*e != ':') /* no terminating ':'? */
5242 break;
5243 end = TRUE;
5244 s = vim_strchr(s, ' ') + 1;
5245 }
5246 *e = NUL; /* truncate the set command */
5247
5248 if (*s != NUL) /* skip over an empty "::" */
5249 {
5250#ifdef FEAT_EVAL
5251 save_SID = current_SID;
5252 current_SID = SID_MODELINE;
5253#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005254 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255#ifdef FEAT_EVAL
5256 current_SID = save_SID;
5257#endif
5258 if (retval == FAIL) /* stop if error found */
5259 break;
5260 }
5261 s = e + 1; /* advance to next part */
5262 }
5263
5264 sourcing_lnum = save_sourcing_lnum;
5265 sourcing_name = save_sourcing_name;
5266
5267 vim_free(linecopy);
5268 }
5269 return retval;
5270}
5271
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005272#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005274read_viminfo_bufferlist(
5275 vir_T *virp,
5276 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 char_u *tab;
5279 linenr_T lnum;
5280 colnr_T col;
5281 buf_T *buf;
5282 char_u *sfname;
5283 char_u *xline;
5284
5285 /* Handle long line and escaped characters. */
5286 xline = viminfo_readstring(virp, 1, FALSE);
5287
5288 /* don't read in if there are files on the command-line or if writing: */
5289 if (xline != NULL && !writing && ARGCOUNT == 0
5290 && find_viminfo_parameter('%') != NULL)
5291 {
5292 /* Format is: <fname> Tab <lnum> Tab <col>.
5293 * Watch out for a Tab in the file name, work from the end. */
5294 lnum = 0;
5295 col = 0;
5296 tab = vim_strrchr(xline, '\t');
5297 if (tab != NULL)
5298 {
5299 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005300 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 tab = vim_strrchr(xline, '\t');
5302 if (tab != NULL)
5303 {
5304 *tab++ = '\0';
5305 lnum = atol((char *)tab);
5306 }
5307 }
5308
5309 /* Expand "~/" in the file name at "line + 1" to a full path.
5310 * Then try shortening it by comparing with the current directory */
5311 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005312 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5315 if (buf != NULL) /* just in case... */
5316 {
5317 buf->b_last_cursor.lnum = lnum;
5318 buf->b_last_cursor.col = col;
5319 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5320 }
5321 }
5322 vim_free(xline);
5323
5324 return viminfo_readline(virp);
5325}
5326
5327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005328write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
5330 buf_T *buf;
5331#ifdef FEAT_WINDOWS
5332 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005333 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334#endif
5335 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005336 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
5338 if (find_viminfo_parameter('%') == NULL)
5339 return;
5340
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005341 /* Without a number -1 is returned: do all buffers. */
5342 max_buffers = get_viminfo_parameter('%');
5343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005345#define LINE_BUF_LEN (MAXPATHL + 40)
5346 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 if (line == NULL)
5348 return;
5349
5350#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005351 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 set_last_cursor(win);
5353#else
5354 set_last_cursor(curwin);
5355#endif
5356
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005357 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5359 {
5360 if (buf->b_fname == NULL
5361 || !buf->b_p_bl
5362#ifdef FEAT_QUICKFIX
5363 || bt_quickfix(buf)
5364#endif
5365 || removable(buf->b_ffname))
5366 continue;
5367
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005368 if (max_buffers-- == 0)
5369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 putc('%', fp);
5371 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005372 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 (long)buf->b_last_cursor.lnum,
5374 buf->b_last_cursor.col);
5375 viminfo_writestring(fp, line);
5376 }
5377 vim_free(line);
5378}
5379#endif
5380
5381
5382/*
5383 * Return special buffer name.
5384 * Returns NULL when the buffer has a normal file name.
5385 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005386 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005387buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5390 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005391 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005392 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005393 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005394
5395 /*
5396 * For location list window, w_llist_ref points to the location list.
5397 * For quickfix window, w_llist_ref is NULL.
5398 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005399 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005400 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005401 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005402 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404#endif
5405#ifdef FEAT_QUICKFIX
5406 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5407 * contains the name as specified by the user */
5408 if (bt_nofile(buf))
5409 {
5410 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005411 return buf->b_sfname;
5412 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 }
5414#endif
5415 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005416 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 return NULL;
5418}
5419
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005420#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5421 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5422 || defined(PROTO)
5423/*
5424 * Find a window for buffer "buf".
5425 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5426 * If not found FAIL is returned.
5427 */
5428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005429find_win_for_buf(
5430 buf_T *buf,
5431 win_T **wp,
5432 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005433{
5434 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5435 if ((*wp)->w_buffer == buf)
5436 goto win_found;
5437 return FAIL;
5438win_found:
5439 return OK;
5440}
5441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442
5443#if defined(FEAT_SIGNS) || defined(PROTO)
5444/*
5445 * Insert the sign into the signlist.
5446 */
5447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005448insert_sign(
5449 buf_T *buf, /* buffer to store sign in */
5450 signlist_T *prev, /* previous sign entry */
5451 signlist_T *next, /* next sign entry */
5452 int id, /* sign ID */
5453 linenr_T lnum, /* line number which gets the mark */
5454 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 signlist_T *newsign;
5457
5458 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5459 if (newsign != NULL)
5460 {
5461 newsign->id = id;
5462 newsign->lnum = lnum;
5463 newsign->typenr = typenr;
5464 newsign->next = next;
5465#ifdef FEAT_NETBEANS_INTG
5466 newsign->prev = prev;
5467 if (next != NULL)
5468 next->prev = newsign;
5469#endif
5470
5471 if (prev == NULL)
5472 {
5473 /* When adding first sign need to redraw the windows to create the
5474 * column for signs. */
5475 if (buf->b_signlist == NULL)
5476 {
5477 redraw_buf_later(buf, NOT_VALID);
5478 changed_cline_bef_curs();
5479 }
5480
5481 /* first sign in signlist */
5482 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005483#ifdef FEAT_NETBEANS_INTG
5484 if (netbeans_active())
5485 buf->b_has_sign_column = TRUE;
5486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 else
5489 prev->next = newsign;
5490 }
5491}
5492
5493/*
5494 * Add the sign into the signlist. Find the right spot to do it though.
5495 */
5496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005497buf_addsign(
5498 buf_T *buf, /* buffer to store sign in */
5499 int id, /* sign ID */
5500 linenr_T lnum, /* line number which gets the mark */
5501 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
5503 signlist_T *sign; /* a sign in the signlist */
5504 signlist_T *prev; /* the previous sign */
5505
5506 prev = NULL;
5507 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5508 {
5509 if (lnum == sign->lnum && id == sign->id)
5510 {
5511 sign->typenr = typenr;
5512 return;
5513 }
5514 else if (
5515#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5516 id < 0 &&
5517#endif
5518 lnum < sign->lnum)
5519 {
5520#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5521 /* XXX - GRP: Is this because of sign slide problem? Or is it
5522 * really needed? Or is it because we allow multiple signs per
5523 * line? If so, should I add that feature to FEAT_SIGNS?
5524 */
5525 while (prev != NULL && prev->lnum == lnum)
5526 prev = prev->prev;
5527 if (prev == NULL)
5528 sign = buf->b_signlist;
5529 else
5530 sign = prev->next;
5531#endif
5532 insert_sign(buf, prev, sign, id, lnum, typenr);
5533 return;
5534 }
5535 prev = sign;
5536 }
5537#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5538 /* XXX - GRP: See previous comment */
5539 while (prev != NULL && prev->lnum == lnum)
5540 prev = prev->prev;
5541 if (prev == NULL)
5542 sign = buf->b_signlist;
5543 else
5544 sign = prev->next;
5545#endif
5546 insert_sign(buf, prev, sign, id, lnum, typenr);
5547
5548 return;
5549}
5550
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005551/*
5552 * For an existing, placed sign "markId" change the type to "typenr".
5553 * Returns the line number of the sign, or zero if the sign is not found.
5554 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005555 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005556buf_change_sign_type(
5557 buf_T *buf, /* buffer to store sign in */
5558 int markId, /* sign ID */
5559 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 signlist_T *sign; /* a sign in the signlist */
5562
5563 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5564 {
5565 if (sign->id == markId)
5566 {
5567 sign->typenr = typenr;
5568 return sign->lnum;
5569 }
5570 }
5571
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005572 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573}
5574
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005575 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005576buf_getsigntype(
5577 buf_T *buf,
5578 linenr_T lnum,
5579 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 signlist_T *sign; /* a sign in a b_signlist */
5582
5583 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5584 if (sign->lnum == lnum
5585 && (type == SIGN_ANY
5586# ifdef FEAT_SIGN_ICONS
5587 || (type == SIGN_ICON
5588 && sign_get_image(sign->typenr) != NULL)
5589# endif
5590 || (type == SIGN_TEXT
5591 && sign_get_text(sign->typenr) != NULL)
5592 || (type == SIGN_LINEHL
5593 && sign_get_attr(sign->typenr, TRUE) != 0)))
5594 return sign->typenr;
5595 return 0;
5596}
5597
5598
5599 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005600buf_delsign(
5601 buf_T *buf, /* buffer sign is stored in */
5602 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
5604 signlist_T **lastp; /* pointer to pointer to current sign */
5605 signlist_T *sign; /* a sign in a b_signlist */
5606 signlist_T *next; /* the next sign in a b_signlist */
5607 linenr_T lnum; /* line number whose sign was deleted */
5608
5609 lastp = &buf->b_signlist;
5610 lnum = 0;
5611 for (sign = buf->b_signlist; sign != NULL; sign = next)
5612 {
5613 next = sign->next;
5614 if (sign->id == id)
5615 {
5616 *lastp = next;
5617#ifdef FEAT_NETBEANS_INTG
5618 if (next != NULL)
5619 next->prev = sign->prev;
5620#endif
5621 lnum = sign->lnum;
5622 vim_free(sign);
5623 break;
5624 }
5625 else
5626 lastp = &sign->next;
5627 }
5628
5629 /* When deleted the last sign need to redraw the windows to remove the
5630 * sign column. */
5631 if (buf->b_signlist == NULL)
5632 {
5633 redraw_buf_later(buf, NOT_VALID);
5634 changed_cline_bef_curs();
5635 }
5636
5637 return lnum;
5638}
5639
5640
5641/*
5642 * Find the line number of the sign with the requested id. If the sign does
5643 * not exist, return 0 as the line number. This will still let the correct file
5644 * get loaded.
5645 */
5646 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005647buf_findsign(
5648 buf_T *buf, /* buffer to store sign in */
5649 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
5651 signlist_T *sign; /* a sign in the signlist */
5652
5653 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5654 if (sign->id == id)
5655 return sign->lnum;
5656
5657 return 0;
5658}
5659
5660 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005661buf_findsign_id(
5662 buf_T *buf, /* buffer whose sign we are searching for */
5663 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664{
5665 signlist_T *sign; /* a sign in the signlist */
5666
5667 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5668 if (sign->lnum == lnum)
5669 return sign->id;
5670
5671 return 0;
5672}
5673
5674
5675# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5676/* see if a given type of sign exists on a specific line */
5677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005678buf_findsigntype_id(
5679 buf_T *buf, /* buffer whose sign we are searching for */
5680 linenr_T lnum, /* line number of sign */
5681 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 signlist_T *sign; /* a sign in the signlist */
5684
5685 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5686 if (sign->lnum == lnum && sign->typenr == typenr)
5687 return sign->id;
5688
5689 return 0;
5690}
5691
5692
5693# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5694/* return the number of icons on the given line */
5695 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005696buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698 signlist_T *sign; /* a sign in the signlist */
5699 int count = 0;
5700
5701 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5702 if (sign->lnum == lnum)
5703 if (sign_get_image(sign->typenr) != NULL)
5704 count++;
5705
5706 return count;
5707}
5708# endif /* FEAT_SIGN_ICONS */
5709# endif /* FEAT_NETBEANS_INTG */
5710
5711
5712/*
5713 * Delete signs in buffer "buf".
5714 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005715 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005716buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 signlist_T *next;
5719
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005720 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005721 * sign column. Not when curwin is NULL (this means we're exiting). */
5722 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005723 {
5724 redraw_buf_later(buf, NOT_VALID);
5725 changed_cline_bef_curs();
5726 }
5727
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 while (buf->b_signlist != NULL)
5729 {
5730 next = buf->b_signlist->next;
5731 vim_free(buf->b_signlist);
5732 buf->b_signlist = next;
5733 }
5734}
5735
5736/*
5737 * Delete all signs in all buffers.
5738 */
5739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005740buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 buf_T *buf; /* buffer we are checking for signs */
5743
5744 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5745 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747}
5748
5749/*
5750 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5751 */
5752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005753sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
5755 buf_T *buf;
5756 signlist_T *p;
5757 char lbuf[BUFSIZ];
5758
5759 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5760 msg_putchar('\n');
5761 if (rbuf == NULL)
5762 buf = firstbuf;
5763 else
5764 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005765 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
5767 if (buf->b_signlist != NULL)
5768 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005769 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5771 msg_putchar('\n');
5772 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005773 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005775 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5777 MSG_PUTS(lbuf);
5778 msg_putchar('\n');
5779 }
5780 if (rbuf != NULL)
5781 break;
5782 buf = buf->b_next;
5783 }
5784}
5785
5786/*
5787 * Adjust a placed sign for inserted/deleted lines.
5788 */
5789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005790sign_mark_adjust(
5791 linenr_T line1,
5792 linenr_T line2,
5793 long amount,
5794 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796 signlist_T *sign; /* a sign in a b_signlist */
5797
5798 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5799 {
5800 if (sign->lnum >= line1 && sign->lnum <= line2)
5801 {
5802 if (amount == MAXLNUM)
5803 sign->lnum = line1;
5804 else
5805 sign->lnum += amount;
5806 }
5807 else if (sign->lnum > line2)
5808 sign->lnum += amount_after;
5809 }
5810}
5811#endif /* FEAT_SIGNS */
5812
5813/*
5814 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5815 */
5816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005817set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
5819 if (on != curbuf->b_p_bl)
5820 {
5821 curbuf->b_p_bl = on;
5822#ifdef FEAT_AUTOCMD
5823 if (on)
5824 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5825 else
5826 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5827#endif
5828 }
5829}
5830
5831/*
5832 * Read the file for "buf" again and check if the contents changed.
5833 * Return TRUE if it changed or this could not be checked.
5834 */
5835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005836buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
5838 buf_T *newbuf;
5839 int differ = TRUE;
5840 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 exarg_T ea;
5843
5844 /* Allocate a buffer without putting it in the buffer list. */
5845 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5846 if (newbuf == NULL)
5847 return TRUE;
5848
5849 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5850 if (prep_exarg(&ea, buf) == FAIL)
5851 {
5852 wipe_buffer(newbuf, FALSE);
5853 return TRUE;
5854 }
5855
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 /* set curwin/curbuf to buf and save a few things */
5857 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
Bram Moolenaar4770d092006-01-12 23:22:24 +00005859 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 && readfile(buf->b_ffname, buf->b_fname,
5861 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5862 &ea, READ_NEW | READ_DUMMY) == OK)
5863 {
5864 /* compare the two files line by line */
5865 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5866 {
5867 differ = FALSE;
5868 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5869 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5870 {
5871 differ = TRUE;
5872 break;
5873 }
5874 }
5875 }
5876 vim_free(ea.cmd);
5877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 /* restore curwin/curbuf and a few other things */
5879 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
5881 if (curbuf != newbuf) /* safety check */
5882 wipe_buffer(newbuf, FALSE);
5883
5884 return differ;
5885}
5886
5887/*
5888 * Wipe out a buffer and decrement the last buffer number if it was used for
5889 * this buffer. Call this to wipe out a temp buffer that does not contain any
5890 * marks.
5891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005893wipe_buffer(
5894 buf_T *buf,
5895 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 if (buf->b_fnum == top_file_num - 1)
5898 --top_file_num;
5899
5900#ifdef FEAT_AUTOCMD
5901 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005902 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005904 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905#ifdef FEAT_AUTOCMD
5906 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005907 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#endif
5909}