blob: 65ab22fc83437f556600da75e8ecf2e3524eb2d2 [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 Moolenaarb25f9a92016-07-10 18:21:50 +020070/* Number of times free_buffer() was called. */
71static int buf_free_count = 0;
72
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020074 * Open current buffer, that is: open the memfile and read the file into
75 * memory.
76 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 */
78 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010079open_buffer(
80 int read_stdin, /* read file from stdin */
81 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
82 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 int retval = OK;
85#ifdef FEAT_AUTOCMD
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020086 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +010088#ifdef FEAT_SYN_HL
89 long old_tw = curbuf->b_p_tw;
90#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92 /*
93 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
94 * When re-entering the same buffer, it should not change, because the
95 * user may have reset the flag by hand.
96 */
97 if (readonlymode && curbuf->b_ffname != NULL
98 && (curbuf->b_flags & BF_NEVERLOADED))
99 curbuf->b_p_ro = TRUE;
100
Bram Moolenaar4770d092006-01-12 23:22:24 +0000101 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 {
103 /*
104 * There MUST be a memfile, otherwise we can't do anything
105 * If we can't create one for the current buffer, take another buffer
106 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100107 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
109 if (curbuf->b_ml.ml_mfp != NULL)
110 break;
111 /*
112 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000113 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
115 if (curbuf == NULL)
116 {
117 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
118 getout(2);
119 }
120 EMSG(_("E83: Cannot allocate buffer, using other one..."));
121 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100122#ifdef FEAT_SYN_HL
123 if (old_tw != curbuf->b_p_tw)
124 check_colorcolumn(curwin);
125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 return FAIL;
127 }
128
129#ifdef FEAT_AUTOCMD
130 /* The autocommands in readfile() may change the buffer, but only AFTER
131 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200132 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 modified_was_set = FALSE;
134#endif
135
136 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100137 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139 if (curbuf->b_ffname != NULL
140#ifdef FEAT_NETBEANS_INTG
141 && netbeansReadFile
142#endif
143 )
144 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100145 int old_msg_silent = msg_silent;
146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_NETBEANS_INTG
148 int oldFire = netbeansFireChanges;
149
150 netbeansFireChanges = 0;
151#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100152 if (shortmess(SHM_FILEINFO))
153 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200155 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
156 flags | READ_NEW);
Bram Moolenaar426dd022016-03-15 15:09:29 +0100157 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158#ifdef FEAT_NETBEANS_INTG
159 netbeansFireChanges = oldFire;
160#endif
161 /* Help buffer is filtered. */
162 if (curbuf->b_help)
163 fix_help_buffer();
164 }
165 else if (read_stdin)
166 {
167 int save_bin = curbuf->b_p_bin;
168 linenr_T line_count;
169
170 /*
171 * First read the text in binary mode into the buffer.
172 * Then read from that same buffer and append at the end. This makes
173 * it possible to retry when 'fileformat' or 'fileencoding' was
174 * guessed wrong.
175 */
176 curbuf->b_p_bin = TRUE;
177 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200178 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
179 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 curbuf->b_p_bin = save_bin;
181 if (retval == OK)
182 {
183 line_count = curbuf->b_ml.ml_line_count;
184 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200185 (linenr_T)0, (linenr_T)MAXLNUM, eap,
186 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 if (retval == OK)
188 {
189 /* Delete the binary lines. */
190 while (--line_count >= 0)
191 ml_delete((linenr_T)1, FALSE);
192 }
193 else
194 {
195 /* Delete the converted lines. */
196 while (curbuf->b_ml.ml_line_count > line_count)
197 ml_delete(line_count, FALSE);
198 }
199 /* Put the cursor on the first line. */
200 curwin->w_cursor.lnum = 1;
201 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000202
203 /* Set or reset 'modified' before executing autocommands, so that
204 * it can be changed there. */
205 if (!readonlymode && !bufempty())
206 changed();
207 else if (retval != FAIL)
208 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209#ifdef FEAT_AUTOCMD
210# ifdef FEAT_EVAL
211 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
212 curbuf, &retval);
213# else
214 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
215# endif
216#endif
217 }
218 }
219
220 /* if first time loading this buffer, init b_chartab[] */
221 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100224#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100225 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100226#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229 /*
230 * Set/reset the Changed flag first, autocmds may change the buffer.
231 * Apply the automatic commands, before processing the modelines.
232 * So the modelines have priority over auto commands.
233 */
234 /* When reading stdin, the buffer contents always needs writing, so set
235 * the changed flag. Unless in readonly mode: "ls | gview -".
236 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000237 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#ifdef FEAT_AUTOCMD
239 || modified_was_set /* ":set modified" used in autocmd */
240# ifdef FEAT_EVAL
241 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
242# endif
243#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000244 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000246 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 unchanged(curbuf, FALSE);
248 save_file_ff(curbuf); /* keep this fileformat */
249
250 /* require "!" to overwrite the file, because it wasn't read completely */
251#ifdef FEAT_EVAL
252 if (aborting())
253#else
254 if (got_int)
255#endif
256 curbuf->b_flags |= BF_READERR;
257
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000258#ifdef FEAT_FOLDING
259 /* Need to update automatic folding. Do this before the autocommands,
260 * they may use the fold info. */
261 foldUpdateAll(curwin);
262#endif
263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifdef FEAT_AUTOCMD
265 /* need to set w_topline, unless some autocommand already did that. */
266 if (!(curwin->w_valid & VALID_TOPLINE))
267 {
268 curwin->w_topline = 1;
269# ifdef FEAT_DIFF
270 curwin->w_topfill = 0;
271# endif
272 }
273# ifdef FEAT_EVAL
274 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
275# else
276 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
277# endif
278#endif
279
280 if (retval != FAIL)
281 {
282#ifdef FEAT_AUTOCMD
283 /*
284 * The autocommands may have changed the current buffer. Apply the
285 * modelines to the correct buffer, if it still exists and is loaded.
286 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200287 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 {
289 aco_save_T aco;
290
291 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200292 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000294 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
296
297#ifdef FEAT_AUTOCMD
298# ifdef FEAT_EVAL
299 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
300 &retval);
301# else
302 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
303# endif
304
305 /* restore curwin/curbuf and a few other things */
306 aucmd_restbuf(&aco);
307 }
308#endif
309 }
310
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 return retval;
312}
313
314/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200315 * Store "buf" in "bufref" and set the free count.
316 */
317 void
318set_bufref(bufref_T *bufref, buf_T *buf)
319{
320 bufref->br_buf = buf;
321 bufref->br_buf_free_count = buf_free_count;
322}
323
324/*
325 * Return TRUE if "bufref->br_buf" points to a valid buffer.
326 * Only goes through the buffer list if buf_free_count changed.
327 */
328 int
329bufref_valid(bufref_T *bufref)
330{
331 return bufref->br_buf_free_count == buf_free_count
332 ? TRUE : buf_valid(bufref->br_buf);
333}
334
335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200337 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 */
339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341{
342 buf_T *bp;
343
Bram Moolenaar82404332016-07-10 17:00:38 +0200344 /* Assume that we more often have a recent buffer, start with the last
345 * one. */
346 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (bp == buf)
348 return TRUE;
349 return FALSE;
350}
351
352/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200353 * A hash table used to quickly lookup a buffer by its number.
354 */
355static hashtab_T buf_hashtab;
356
357 static void
358buf_hashtab_add(buf_T *buf)
359{
360 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
361 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
362 EMSG(_("E931: Buffer cannot be registered"));
363}
364
365 static void
366buf_hashtab_remove(buf_T *buf)
367{
368 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
369
370 if (!HASHITEM_EMPTY(hi))
371 hash_remove(&buf_hashtab, hi);
372}
373
374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 * Close the link to a buffer.
376 * "action" is used when there is no longer a window for the buffer.
377 * It can be:
378 * 0 buffer becomes hidden
379 * DOBUF_UNLOAD buffer is unloaded
380 * DOBUF_DELETE buffer is unloaded and removed from buffer list
381 * DOBUF_WIPE buffer is unloaded and really deleted
382 * When doing all but the first one on the current buffer, the caller should
383 * get a new buffer very soon!
384 *
385 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100386 *
387 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
388 * cause there to be only one window with this buffer. e.g. when ":quit" is
389 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 */
391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100392close_buffer(
393 win_T *win, /* if not NULL, set b_last_cursor */
394 buf_T *buf,
395 int action,
396 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
398#ifdef FEAT_AUTOCMD
399 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100400 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200401 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#endif
403 int unload_buf = (action != 0);
404 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
405 int wipe_buf = (action == DOBUF_WIPE);
406
407#ifdef FEAT_QUICKFIX
408 /*
409 * Force unloading or deleting when 'bufhidden' says so.
410 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
411 * "hide" (otherwise we could never free or delete a buffer).
412 */
413 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
414 {
415 del_buf = TRUE;
416 unload_buf = TRUE;
417 }
418 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
419 {
420 del_buf = TRUE;
421 unload_buf = TRUE;
422 wipe_buf = TRUE;
423 }
424 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
425 unload_buf = TRUE;
426#endif
427
Bram Moolenaar3be85852014-06-12 14:01:31 +0200428 if (win != NULL
429#ifdef FEAT_WINDOWS
430 && win_valid(win) /* in case autocommands closed the window */
431#endif
432 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {
434 /* Set b_last_cursor when closing the last window for the buffer.
435 * Remember the last cursor position and window options of the buffer.
436 * This used to be only for the current window, but then options like
437 * 'foldmethod' may be lost with a ":only" command. */
438 if (buf->b_nwindows == 1)
439 set_last_cursor(win);
440 buflist_setfpos(buf, win,
441 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
442 win->w_cursor.col, TRUE);
443 }
444
445#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200446 set_bufref(&bufref, buf);
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 /* When the buffer is no longer in a window, trigger BufWinLeave */
449 if (buf->b_nwindows == 1)
450 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200451 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200452 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
453 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200454 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100455 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200456 /* Autocommands deleted the buffer. */
457aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100458 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100460 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200461 buf->b_closing = FALSE;
462 if (abort_if_last && one_window())
463 /* Autocommands made this the only window. */
464 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466 /* When the buffer becomes hidden, but is not unloaded, trigger
467 * BufHidden */
468 if (!unload_buf)
469 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200470 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200471 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
472 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200473 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200474 /* Autocommands deleted the buffer. */
475 goto aucmd_abort;
476 buf->b_closing = FALSE;
477 if (abort_if_last && one_window())
478 /* Autocommands made this the only window. */
479 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 }
481# ifdef FEAT_EVAL
482 if (aborting()) /* autocmds may abort script processing */
483 return;
484# endif
485 }
486 nwindows = buf->b_nwindows;
487#endif
488
489 /* decrease the link count from windows (unless not in any window) */
490 if (buf->b_nwindows > 0)
491 --buf->b_nwindows;
492
493 /* Return when a window is displaying the buffer or when it's not
494 * unloaded. */
495 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
498 /* Always remove the buffer when there is no file name. */
499 if (buf->b_ffname == NULL)
500 del_buf = TRUE;
501
502 /*
503 * Free all things allocated for this buffer.
504 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
505 */
506#ifdef FEAT_AUTOCMD
507 /* Remember if we are closing the current buffer. Restore the number of
508 * windows, so that autocommands in buf_freeall() don't get confused. */
509 is_curbuf = (buf == curbuf);
510 buf->b_nwindows = nwindows;
511#endif
512
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200513 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
515#ifdef FEAT_AUTOCMD
516 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200517 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 return;
519# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000520 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 return;
522# endif
523
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 /*
525 * It's possible that autocommands change curbuf to the one being deleted.
526 * This might cause the previous curbuf to be deleted unexpectedly. But
527 * in some cases it's OK to delete the curbuf, because a new one is
528 * obtained anyway. Therefore only return if curbuf changed to the
529 * deleted buffer.
530 */
531 if (buf == curbuf && !is_curbuf)
532 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200533
534 if (
535#ifdef FEAT_WINDOWS
536 win_valid(win) &&
537#else
538 win != NULL &&
539#endif
540 win->w_buffer == buf)
541 win->w_buffer = NULL; /* make sure we don't use the buffer now */
542
543 /* Autocommands may have opened or closed windows for this buffer.
544 * Decrement the count for the close we do here. */
545 if (buf->b_nwindows > 0)
546 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547#endif
548
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000549 /* Change directories when the 'acd' option is set. */
550 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552 /*
553 * Remove the buffer from the list.
554 */
555 if (wipe_buf)
556 {
557#ifdef FEAT_SUN_WORKSHOP
558 if (usingSunWorkShop)
559 workshop_file_closed_lineno((char *)buf->b_ffname,
560 (int)buf->b_last_cursor.lnum);
561#endif
562 vim_free(buf->b_ffname);
563 vim_free(buf->b_sfname);
564 if (buf->b_prev == NULL)
565 firstbuf = buf->b_next;
566 else
567 buf->b_prev->b_next = buf->b_next;
568 if (buf->b_next == NULL)
569 lastbuf = buf->b_prev;
570 else
571 buf->b_next->b_prev = buf->b_prev;
572 free_buffer(buf);
573 }
574 else
575 {
576 if (del_buf)
577 {
578 /* Free all internal variables and reset option values, to make
579 * ":bdel" compatible with Vim 5.7. */
580 free_buffer_stuff(buf, TRUE);
581
582 /* Make it look like a new buffer. */
583 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
584
585 /* Init the options when loaded again. */
586 buf->b_p_initialized = FALSE;
587 }
588 buf_clear_file(buf);
589 if (del_buf)
590 buf->b_p_bl = FALSE;
591 }
592}
593
594/*
595 * Make buffer not contain a file.
596 */
597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100598buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 buf->b_ml.ml_line_count = 1;
601 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 buf->b_p_eol = TRUE;
604 buf->b_start_eol = TRUE;
605#ifdef FEAT_MBYTE
606 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000607 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#endif
609 buf->b_ml.ml_mfp = NULL;
610 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
611#ifdef FEAT_NETBEANS_INTG
612 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#endif
614}
615
616/*
617 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200618 * the file. flags:
619 * BFA_DEL buffer is going to be deleted
620 * BFA_WIPE buffer is going to be wiped out
621 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100624buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626#ifdef FEAT_AUTOCMD
627 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200628 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
Bram Moolenaar362ce482012-06-06 19:02:45 +0200630 buf->b_closing = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200631 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200632 if (buf->b_ml.ml_mfp != NULL)
633 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200634 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
635 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200636 && !bufref_valid(&bufref))
637 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200638 return;
639 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200640 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200642 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
643 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200644 && !bufref_valid(&bufref))
645 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 return;
647 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200648 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200650 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
651 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200652 && !bufref_valid(&bufref))
653 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 return;
655 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200656 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000658 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return;
660# endif
661
662 /*
663 * It's possible that autocommands change curbuf to the one being deleted.
664 * This might cause curbuf to be deleted unexpectedly. But in some cases
665 * it's OK to delete the curbuf, because a new one is obtained anyway.
666 * Therefore only return if curbuf changed to the deleted buffer.
667 */
668 if (buf == curbuf && !is_curbuf)
669 return;
670#endif
671#ifdef FEAT_DIFF
672 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
673#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200674#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100675 /* Remove any ownsyntax, unless exiting. */
676 if (firstwin != NULL && curwin->w_buffer == buf)
677 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200678#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000679
680#ifdef FEAT_FOLDING
681 /* No folds in an empty buffer. */
682# ifdef FEAT_WINDOWS
683 {
684 win_T *win;
685 tabpage_T *tp;
686
687 FOR_ALL_TAB_WINDOWS(tp, win)
688 if (win->w_buffer == buf)
689 clearFolding(win);
690 }
691# else
692 if (curwin->w_buffer == buf)
693 clearFolding(curwin);
694# endif
695#endif
696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#ifdef FEAT_TCL
698 tcl_buffer_free(buf);
699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ml_close(buf, TRUE); /* close and delete the memline/memfile */
701 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200702 if ((flags & BFA_KEEP_UNDO) == 0)
703 {
704 u_blockfree(buf); /* free the memory allocated for undo */
705 u_clearall(buf); /* reset all undo information */
706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200708 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000710 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711}
712
713/*
714 * Free a buffer structure and the things it contains related to the buffer
715 * itself (not the file, that must have been done already).
716 */
717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100718free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200720 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200722#ifdef FEAT_EVAL
723 unref_var_dict(buf->b_vars);
724#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200725#ifdef FEAT_LUA
726 lua_buffer_free(buf);
727#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000728#ifdef FEAT_MZSCHEME
729 mzscheme_buffer_free(buf);
730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731#ifdef FEAT_PERL
732 perl_buf_free(buf);
733#endif
734#ifdef FEAT_PYTHON
735 python_buffer_free(buf);
736#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200737#ifdef FEAT_PYTHON3
738 python3_buffer_free(buf);
739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740#ifdef FEAT_RUBY
741 ruby_buffer_free(buf);
742#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200743#ifdef FEAT_JOB_CHANNEL
744 channel_buffer_free(buf);
745#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000746#ifdef FEAT_AUTOCMD
747 aubuflocal_remove(buf);
Bram Moolenaar480778b2016-07-14 22:09:39 +0200748
749 buf_hashtab_remove(buf);
750
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200751 if (autocmd_busy)
752 {
753 /* Do not free the buffer structure while autocommands are executing,
754 * it's still needed. Free it when autocmd_busy is reset. */
755 buf->b_next = au_pending_free_buf;
756 au_pending_free_buf = buf;
757 }
758 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000759#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200760 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761}
762
763/*
764 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
765 */
766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100767free_buffer_stuff(
768 buf_T *buf,
769 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 if (free_options)
772 {
773 clear_wininfo(buf); /* including window-local options */
774 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200775#ifdef FEAT_SPELL
776 ga_clear(&buf->b_s.b_langp);
777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
779#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200780 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
781 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783#ifdef FEAT_USR_CMDS
784 uc_clear(&buf->b_ucmds); /* clear local user commands */
785#endif
786#ifdef FEAT_SIGNS
787 buf_delete_signs(buf); /* delete any signs */
788#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000789#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200790 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#ifdef FEAT_LOCALMAP
793 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
794 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
795#endif
796#ifdef FEAT_MBYTE
797 vim_free(buf->b_start_fenc);
798 buf->b_start_fenc = NULL;
799#endif
800}
801
802/*
803 * Free the b_wininfo list for buffer "buf".
804 */
805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100806clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 wininfo_T *wip;
809
810 while (buf->b_wininfo != NULL)
811 {
812 wip = buf->b_wininfo;
813 buf->b_wininfo = wip->wi_next;
814 if (wip->wi_optset)
815 {
816 clear_winopt(&wip->wi_opt);
817#ifdef FEAT_FOLDING
818 deleteFoldRecurse(&wip->wi_folds);
819#endif
820 }
821 vim_free(wip);
822 }
823}
824
825#if defined(FEAT_LISTCMDS) || defined(PROTO)
826/*
827 * Go to another buffer. Handles the result of the ATTENTION dialog.
828 */
829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100830goto_buffer(
831 exarg_T *eap,
832 int start,
833 int dir,
834 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000836# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200837 bufref_T old_curbuf;
838
839 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
841 swap_exists_action = SEA_DIALOG;
842# endif
843 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
844 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000845# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
847 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000848# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
849 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000850
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000851 /* Reset the error/interrupt/exception state here so that
852 * aborting() returns FALSE when closing a window. */
853 enter_cleanup(&cs);
854# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000855
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000856 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 win_close(curwin, TRUE);
858 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000859 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000860
861# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
862 /* Restore the error/interrupt/exception state if not discarded by a
863 * new aborting error, interrupt, or uncaught exception. */
864 leave_cleanup(&cs);
865# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200868 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869# endif
870}
871#endif
872
Bram Moolenaare64ac772005-12-07 20:54:59 +0000873#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874/*
875 * Handle the situation of swap_exists_action being set.
876 * It is allowed for "old_curbuf" to be NULL or invalid.
877 */
878 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200879handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000881# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
882 cleanup_T cs;
883# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100884#ifdef FEAT_SYN_HL
885 long old_tw = curbuf->b_p_tw;
886#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200887 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000888
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (swap_exists_action == SEA_QUIT)
890 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000891# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
892 /* Reset the error/interrupt/exception state here so that
893 * aborting() returns FALSE when closing a buffer. */
894 enter_cleanup(&cs);
895# endif
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 /* User selected Quit at ATTENTION prompt. Go back to previous
898 * buffer. If that buffer is gone or the same as the current one,
899 * open a new, empty buffer. */
900 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000901 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100902 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200903 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
904 || old_curbuf->br_buf == curbuf)
905 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
906 else
907 buf = old_curbuf->br_buf;
908 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100909 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200910 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100911#ifdef FEAT_SYN_HL
912 if (old_tw != curbuf->b_p_tw)
913 check_colorcolumn(curwin);
914#endif
915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000917
918# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
919 /* Restore the error/interrupt/exception state if not discarded by a
920 * new aborting error, interrupt, or uncaught exception. */
921 leave_cleanup(&cs);
922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924 else if (swap_exists_action == SEA_RECOVER)
925 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000926# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
927 /* Reset the error/interrupt/exception state here so that
928 * aborting() returns FALSE when closing a buffer. */
929 enter_cleanup(&cs);
930# endif
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 /* User selected Recover at ATTENTION prompt. */
933 msg_scroll = TRUE;
934 ml_recover();
935 MSG_PUTS("\n"); /* don't overwrite the last message */
936 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000937 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000938
939# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
940 /* Restore the error/interrupt/exception state if not discarded by a
941 * new aborting error, interrupt, or uncaught exception. */
942 leave_cleanup(&cs);
943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945 swap_exists_action = SEA_NONE;
946}
947#endif
948
949#if defined(FEAT_LISTCMDS) || defined(PROTO)
950/*
951 * do_bufdel() - delete or unload buffer(s)
952 *
953 * addr_count == 0: ":bdel" - delete current buffer
954 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
955 * buffer "end_bnr", then any other arguments.
956 * addr_count == 2: ":N,N bdel" - delete buffers in range
957 *
958 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
959 * DOBUF_DEL (":bdel")
960 *
961 * Returns error message or NULL
962 */
963 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100964do_bufdel(
965 int command,
966 char_u *arg, /* pointer to extra arguments */
967 int addr_count,
968 int start_bnr, /* first buffer number in a range */
969 int end_bnr, /* buffer nr or last buffer nr in a range */
970 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971{
972 int do_current = 0; /* delete current buffer? */
973 int deleted = 0; /* number of buffers deleted */
974 char_u *errormsg = NULL; /* return value */
975 int bnr; /* buffer number */
976 char_u *p;
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (addr_count == 0)
979 {
980 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
981 }
982 else
983 {
984 if (addr_count == 2)
985 {
986 if (*arg) /* both range and argument is not allowed */
987 return (char_u *)_(e_trailing);
988 bnr = start_bnr;
989 }
990 else /* addr_count == 1 */
991 bnr = end_bnr;
992
993 for ( ;!got_int; ui_breakcheck())
994 {
995 /*
996 * delete the current buffer last, otherwise when the
997 * current buffer is deleted, the next buffer becomes
998 * the current one and will be loaded, which may then
999 * also be deleted, etc.
1000 */
1001 if (bnr == curbuf->b_fnum)
1002 do_current = bnr;
1003 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1004 forceit) == OK)
1005 ++deleted;
1006
1007 /*
1008 * find next buffer number to delete/unload
1009 */
1010 if (addr_count == 2)
1011 {
1012 if (++bnr > end_bnr)
1013 break;
1014 }
1015 else /* addr_count == 1 */
1016 {
1017 arg = skipwhite(arg);
1018 if (*arg == NUL)
1019 break;
1020 if (!VIM_ISDIGIT(*arg))
1021 {
1022 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001023 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1024 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (bnr < 0) /* failed */
1026 break;
1027 arg = p;
1028 }
1029 else
1030 bnr = getdigits(&arg);
1031 }
1032 }
1033 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1034 FORWARD, do_current, forceit) == OK)
1035 ++deleted;
1036
1037 if (deleted == 0)
1038 {
1039 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001040 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001044 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 errormsg = IObuff;
1046 }
1047 else if (deleted >= p_report)
1048 {
1049 if (command == DOBUF_UNLOAD)
1050 {
1051 if (deleted == 1)
1052 MSG(_("1 buffer unloaded"));
1053 else
1054 smsg((char_u *)_("%d buffers unloaded"), deleted);
1055 }
1056 else if (command == DOBUF_DEL)
1057 {
1058 if (deleted == 1)
1059 MSG(_("1 buffer deleted"));
1060 else
1061 smsg((char_u *)_("%d buffers deleted"), deleted);
1062 }
1063 else
1064 {
1065 if (deleted == 1)
1066 MSG(_("1 buffer wiped out"));
1067 else
1068 smsg((char_u *)_("%d buffers wiped out"), deleted);
1069 }
1070 }
1071 }
1072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
1074 return errormsg;
1075}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001076#endif /* FEAT_LISTCMDS */
1077
1078#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1079 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001081static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001082
1083/*
1084 * Make the current buffer empty.
1085 * Used when it is wiped out and it's the last buffer.
1086 */
1087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001088empty_curbuf(
1089 int close_others,
1090 int forceit,
1091 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001092{
1093 int retval;
1094 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001095 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001096
1097 if (action == DOBUF_UNLOAD)
1098 {
1099 EMSG(_("E90: Cannot unload last buffer"));
1100 return FAIL;
1101 }
1102
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001103 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001104#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001105 if (close_others)
1106 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001107 close_windows(buf, TRUE);
1108#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001109
1110 setpcmark();
1111 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1112 forceit ? ECMD_FORCEIT : 0, curwin);
1113
1114 /*
1115 * do_ecmd() may create a new buffer, then we have to delete
1116 * the old one. But do_ecmd() may have done that already, check
1117 * if the buffer still exists.
1118 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001119 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001120 close_buffer(NULL, buf, action, FALSE);
1121 if (!close_others)
1122 need_fileinfo = FALSE;
1123 return retval;
1124}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125/*
1126 * Implementation of the commands for the buffer list.
1127 *
1128 * action == DOBUF_GOTO go to specified buffer
1129 * action == DOBUF_SPLIT split window and go to specified buffer
1130 * action == DOBUF_UNLOAD unload specified buffer(s)
1131 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1132 * action == DOBUF_WIPE delete specified buffer(s) really
1133 *
1134 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1135 * start == DOBUF_FIRST go to "count" buffer from first buffer
1136 * start == DOBUF_LAST go to "count" buffer from last buffer
1137 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1138 *
1139 * Return FAIL or OK.
1140 */
1141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001142do_buffer(
1143 int action,
1144 int start,
1145 int dir, /* FORWARD or BACKWARD */
1146 int count, /* buffer number or number of buffers */
1147 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148{
1149 buf_T *buf;
1150 buf_T *bp;
1151 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1152 || action == DOBUF_WIPE);
1153
1154 switch (start)
1155 {
1156 case DOBUF_FIRST: buf = firstbuf; break;
1157 case DOBUF_LAST: buf = lastbuf; break;
1158 default: buf = curbuf; break;
1159 }
1160 if (start == DOBUF_MOD) /* find next modified buffer */
1161 {
1162 while (count-- > 0)
1163 {
1164 do
1165 {
1166 buf = buf->b_next;
1167 if (buf == NULL)
1168 buf = firstbuf;
1169 }
1170 while (buf != curbuf && !bufIsChanged(buf));
1171 }
1172 if (!bufIsChanged(buf))
1173 {
1174 EMSG(_("E84: No modified buffer found"));
1175 return FAIL;
1176 }
1177 }
1178 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1179 {
1180 while (buf != NULL && buf->b_fnum != count)
1181 buf = buf->b_next;
1182 }
1183 else
1184 {
1185 bp = NULL;
1186 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1187 {
1188 /* remember the buffer where we start, we come back there when all
1189 * buffers are unlisted. */
1190 if (bp == NULL)
1191 bp = buf;
1192 if (dir == FORWARD)
1193 {
1194 buf = buf->b_next;
1195 if (buf == NULL)
1196 buf = firstbuf;
1197 }
1198 else
1199 {
1200 buf = buf->b_prev;
1201 if (buf == NULL)
1202 buf = lastbuf;
1203 }
1204 /* don't count unlisted buffers */
1205 if (unload || buf->b_p_bl)
1206 {
1207 --count;
1208 bp = NULL; /* use this buffer as new starting point */
1209 }
1210 if (bp == buf)
1211 {
1212 /* back where we started, didn't find anything. */
1213 EMSG(_("E85: There is no listed buffer"));
1214 return FAIL;
1215 }
1216 }
1217 }
1218
1219 if (buf == NULL) /* could not find it */
1220 {
1221 if (start == DOBUF_FIRST)
1222 {
1223 /* don't warn when deleting */
1224 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001225 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 else if (dir == FORWARD)
1228 EMSG(_("E87: Cannot go beyond last buffer"));
1229 else
1230 EMSG(_("E88: Cannot go before first buffer"));
1231 return FAIL;
1232 }
1233
1234#ifdef FEAT_GUI
1235 need_mouse_correct = TRUE;
1236#endif
1237
1238#ifdef FEAT_LISTCMDS
1239 /*
1240 * delete buffer buf from memory and/or the list
1241 */
1242 if (unload)
1243 {
1244 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001245# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1246 bufref_T bufref;
1247
1248 set_bufref(&bufref, buf);
1249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251 /* When unloading or deleting a buffer that's already unloaded and
1252 * unlisted: fail silently. */
1253 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1254 return FAIL;
1255
1256 if (!forceit && bufIsChanged(buf))
1257 {
1258#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1259 if ((p_confirm || cmdmod.confirm) && p_write)
1260 {
1261 dialog_changed(buf, FALSE);
1262# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001263 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /* Autocommand deleted buffer, oops! It's not changed
1265 * now. */
1266 return FAIL;
1267# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001268 /* If it's still changed fail silently, the dialog already
1269 * mentioned why it fails. */
1270 if (bufIsChanged(buf))
1271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274#endif
1275 {
1276 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1277 buf->b_fnum);
1278 return FAIL;
1279 }
1280 }
1281
1282 /*
1283 * If deleting the last (listed) buffer, make it empty.
1284 * The last (listed) buffer cannot be unloaded.
1285 */
1286 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1287 if (bp->b_p_bl && bp != buf)
1288 break;
1289 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001290 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
1292#ifdef FEAT_WINDOWS
1293 /*
1294 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001295 * (unless it's the only window). Repeat this so long as we end up in
1296 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001298 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001299# ifdef FEAT_AUTOCMD
1300 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1301# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001302 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001303 {
1304 if (win_close(curwin, FALSE) == FAIL)
1305 break;
1306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307#endif
1308
1309 /*
1310 * If the buffer to be deleted is not the current one, delete it here.
1311 */
1312 if (buf != curbuf)
1313 {
1314#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001315 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001316 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001318 if (buf->b_nwindows <= 0)
1319 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 return OK;
1321 }
1322
1323 /*
1324 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001325 * There should be another, otherwise it would have been handled
1326 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001327 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * Then prefer the buffer we most recently visited.
1329 * Else try to find one that is loaded, after the current buffer,
1330 * then before the current buffer.
1331 * Finally use any buffer.
1332 */
1333 buf = NULL; /* selected buffer */
1334 bp = NULL; /* used when no loaded buffer found */
1335#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001336 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1337 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338# ifdef FEAT_JUMPLIST
1339 else
1340# endif
1341#endif
1342#ifdef FEAT_JUMPLIST
1343 if (curwin->w_jumplistlen > 0)
1344 {
1345 int jumpidx;
1346
1347 jumpidx = curwin->w_jumplistidx - 1;
1348 if (jumpidx < 0)
1349 jumpidx = curwin->w_jumplistlen - 1;
1350
1351 forward = jumpidx;
1352 while (jumpidx != curwin->w_jumplistidx)
1353 {
1354 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1355 if (buf != NULL)
1356 {
1357 if (buf == curbuf || !buf->b_p_bl)
1358 buf = NULL; /* skip current and unlisted bufs */
1359 else if (buf->b_ml.ml_mfp == NULL)
1360 {
1361 /* skip unloaded buf, but may keep it for later */
1362 if (bp == NULL)
1363 bp = buf;
1364 buf = NULL;
1365 }
1366 }
1367 if (buf != NULL) /* found a valid buffer: stop searching */
1368 break;
1369 /* advance to older entry in jump list */
1370 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1371 break;
1372 if (--jumpidx < 0)
1373 jumpidx = curwin->w_jumplistlen - 1;
1374 if (jumpidx == forward) /* List exhausted for sure */
1375 break;
1376 }
1377 }
1378#endif
1379
1380 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1381 {
1382 forward = TRUE;
1383 buf = curbuf->b_next;
1384 for (;;)
1385 {
1386 if (buf == NULL)
1387 {
1388 if (!forward) /* tried both directions */
1389 break;
1390 buf = curbuf->b_prev;
1391 forward = FALSE;
1392 continue;
1393 }
1394 /* in non-help buffer, try to skip help buffers, and vv */
1395 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1396 {
1397 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1398 break;
1399 if (bp == NULL) /* remember unloaded buf for later */
1400 bp = buf;
1401 }
1402 if (forward)
1403 buf = buf->b_next;
1404 else
1405 buf = buf->b_prev;
1406 }
1407 }
1408 if (buf == NULL) /* No loaded buffer, use unloaded one */
1409 buf = bp;
1410 if (buf == NULL) /* No loaded buffer, find listed one */
1411 {
1412 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1413 if (buf->b_p_bl && buf != curbuf)
1414 break;
1415 }
1416 if (buf == NULL) /* Still no buffer, just take one */
1417 {
1418 if (curbuf->b_next != NULL)
1419 buf = curbuf->b_next;
1420 else
1421 buf = curbuf->b_prev;
1422 }
1423 }
1424
Bram Moolenaara02471e2014-01-10 16:43:14 +01001425 if (buf == NULL)
1426 {
1427 /* Autocommands must have wiped out all other buffers. Only option
1428 * now is to make the current buffer empty. */
1429 return empty_curbuf(FALSE, forceit, action);
1430 }
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 /*
1433 * make buf current buffer
1434 */
1435 if (action == DOBUF_SPLIT) /* split window first */
1436 {
1437# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001438 /* If 'switchbuf' contains "useopen": jump to first window containing
1439 * "buf" if one exists */
1440 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001441 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001442 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001443 * page containing "buf" if one exists */
1444 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 return OK;
1446 if (win_split(0, 0) == FAIL)
1447# endif
1448 return FAIL;
1449 }
1450#endif
1451
1452 /* go to current buffer - nothing to do */
1453 if (buf == curbuf)
1454 return OK;
1455
1456 /*
1457 * Check if the current buffer may be abandoned.
1458 */
1459 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1460 {
1461#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1462 if ((p_confirm || cmdmod.confirm) && p_write)
1463 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001464# ifdef FEAT_AUTOCMD
1465 bufref_T bufref;
1466
1467 set_bufref(&bufref, buf);
1468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 dialog_changed(curbuf, FALSE);
1470# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001471 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 /* Autocommand deleted buffer, oops! */
1473 return FAIL;
1474# endif
1475 }
1476 if (bufIsChanged(curbuf))
1477#endif
1478 {
1479 EMSG(_(e_nowrtmsg));
1480 return FAIL;
1481 }
1482 }
1483
1484 /* Go to the other buffer. */
1485 set_curbuf(buf, action);
1486
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001487#if defined(FEAT_LISTCMDS) \
1488 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001490 {
1491 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493#endif
1494
1495#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1496 if (aborting()) /* autocmds may abort script processing */
1497 return FAIL;
1498#endif
1499
1500 return OK;
1501}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
1504/*
1505 * Set current buffer to "buf". Executes autocommands and closes current
1506 * buffer. "action" tells how to close the current buffer:
1507 * DOBUF_GOTO free or hide it
1508 * DOBUF_SPLIT nothing
1509 * DOBUF_UNLOAD unload it
1510 * DOBUF_DEL delete it
1511 * DOBUF_WIPE wipe it out
1512 */
1513 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001514set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 buf_T *prevbuf;
1517 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1518 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001519#ifdef FEAT_SYN_HL
1520 long old_tw = curbuf->b_p_tw;
1521#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001522 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001525 if (!cmdmod.keepalt)
1526 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001527 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 /* Don't restart Select mode after switching to another buffer. */
1530 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
1532 /* close_windows() or apply_autocmds() may change curbuf */
1533 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001534 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
1536#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001537 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538# ifdef FEAT_EVAL
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001539 || (bufref_valid(&bufref) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540# else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001541 || bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542# endif
1543#endif
1544 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001545#ifdef FEAT_SYN_HL
1546 if (prevbuf == curwin->w_buffer)
1547 reset_synblock(curwin);
1548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#ifdef FEAT_WINDOWS
1550 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001551 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552#endif
1553#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001554 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001556 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001558 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001559#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001560 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001561#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001562 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001563 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1565 unload ? action : (action == DOBUF_GOTO
1566 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001567 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001568#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001569 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001570 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001571 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001572#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
1575#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001576 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001577 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001578 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1579 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001580# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001581 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001582# endif
1583# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001584 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001585# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001586 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001590#ifdef FEAT_SYN_HL
1591 if (old_tw != curbuf->b_p_tw)
1592 check_colorcolumn(curwin);
1593#endif
1594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595}
1596
1597/*
1598 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001599 * Old curbuf must have been abandoned already! This also means "curbuf" may
1600 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 */
1602 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001603enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
1605 /* Copy buffer and window local option values. Not for a help buffer. */
1606 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1607 if (!buf->b_help)
1608 get_winopts(buf);
1609#ifdef FEAT_FOLDING
1610 else
1611 /* Remove all folds in the window. */
1612 clearFolding(curwin);
1613 foldUpdateAll(curwin); /* update folds (later). */
1614#endif
1615
1616 /* Get the buffer in the current window. */
1617 curwin->w_buffer = buf;
1618 curbuf = buf;
1619 ++curbuf->b_nwindows;
1620
1621#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001622 if (curwin->w_p_diff)
1623 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624#endif
1625
Bram Moolenaara971b822011-09-14 14:43:25 +02001626#ifdef FEAT_SYN_HL
1627 curwin->w_s = &(buf->b_s);
1628#endif
1629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 /* Cursor on first line by default. */
1631 curwin->w_cursor.lnum = 1;
1632 curwin->w_cursor.col = 0;
1633#ifdef FEAT_VIRTUALEDIT
1634 curwin->w_cursor.coladd = 0;
1635#endif
1636 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001637#ifdef FEAT_AUTOCMD
1638 curwin->w_topline_was_set = FALSE;
1639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001641 /* mark cursor position as being invalid */
1642 curwin->w_valid = 0;
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 /* Make sure the buffer is loaded. */
1645 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001646 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001647#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001648 /* If there is no filetype, allow for detecting one. Esp. useful for
1649 * ":ball" used in a autocommand. If there already is a filetype we
1650 * might prefer to keep it. */
1651 if (*curbuf->b_p_ft == NUL)
1652 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001653#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001654
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001655 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 else
1658 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001659 if (!msg_silent)
1660 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1662#ifdef FEAT_AUTOCMD
1663 curwin->w_topline = 1;
1664# ifdef FEAT_DIFF
1665 curwin->w_topfill = 0;
1666# endif
1667 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1668 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1669#endif
1670 }
1671
1672 /* If autocommands did not change the cursor position, restore cursor lnum
1673 * and possibly cursor col. */
1674 if (curwin->w_cursor.lnum == 1 && inindent(0))
1675 buflist_getfpos();
1676
1677 check_arg_idx(curwin); /* check for valid arg_idx */
1678#ifdef FEAT_TITLE
1679 maketitle();
1680#endif
1681#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001682 /* when autocmds didn't change it */
1683 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#endif
1685 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1686
Bram Moolenaar009b2592004-10-24 19:18:58 +00001687#ifdef FEAT_NETBEANS_INTG
1688 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001689 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001690#endif
1691
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001692 /* Change directories when the 'acd' option is set. */
1693 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
1695#ifdef FEAT_KEYMAP
1696 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001697 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001699#ifdef FEAT_SPELL
1700 /* May need to set the spell language. Can only do this after the buffer
1701 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001702 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1703 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001704#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001705#ifdef FEAT_VIMINFO
1706 curbuf->b_last_used = vim_time();
1707#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 redraw_later(NOT_VALID);
1710}
1711
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001712#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1713/*
1714 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001715 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001716 */
1717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001718do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001719{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001720 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001721 && curbuf->b_ffname != NULL
1722 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001723 shorten_fnames(TRUE);
1724}
1725#endif
1726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727/*
1728 * functions for dealing with the buffer list
1729 */
1730
Bram Moolenaar480778b2016-07-14 22:09:39 +02001731static int top_file_num = 1; /* highest file number */
1732
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733/*
1734 * Add a file name to the buffer list. Return a pointer to the buffer.
1735 * If the same file name already exists return a pointer to that buffer.
1736 * If it does not exist, or if fname == NULL, a new entry is created.
1737 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1738 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1739 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001740 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001741 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1742 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 * This is the ONLY way to create a new buffer.
1744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001746buflist_new(
1747 char_u *ffname, /* full path of fname or relative */
1748 char_u *sfname, /* short fname or NULL */
1749 linenr_T lnum, /* preferred cursor line */
1750 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 buf_T *buf;
1753#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001754 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755#endif
1756
Bram Moolenaar480778b2016-07-14 22:09:39 +02001757 if (top_file_num == 1)
1758 hash_init(&buf_hashtab);
1759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1761
1762 /*
1763 * If file name already exists in the list, update the entry.
1764 */
1765#ifdef UNIX
1766 /* On Unix we can use inode numbers when the file exists. Works better
1767 * for hard links. */
1768 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1769 st.st_dev = (dev_T)-1;
1770#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001771 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef UNIX
1773 buflist_findname_stat(ffname, &st)
1774#else
1775 buflist_findname(ffname)
1776#endif
1777 ) != NULL)
1778 {
1779 vim_free(ffname);
1780 if (lnum != 0)
1781 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001782
1783 if ((flags & BLN_NOOPT) == 0)
1784 /* copy the options now, if 'cpo' doesn't have 's' and not done
1785 * already */
1786 buf_copy_options(buf, 0);
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1789 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001790#ifdef FEAT_AUTOCMD
1791 bufref_T bufref;
1792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 buf->b_p_bl = TRUE;
1794#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001795 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001797 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001798 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001799 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001800 return NULL;
1801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802#endif
1803 }
1804 return buf;
1805 }
1806
1807 /*
1808 * If the current buffer has no name and no contents, use the current
1809 * buffer. Otherwise: Need to allocate a new buffer structure.
1810 *
1811 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001812 * (A spell file buffer is allocated in spell.c, but that's not a normal
1813 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 */
1815 buf = NULL;
1816 if ((flags & BLN_CURBUF)
1817 && curbuf != NULL
1818 && curbuf->b_ffname == NULL
1819 && curbuf->b_nwindows <= 1
1820 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1821 {
1822 buf = curbuf;
1823#ifdef FEAT_AUTOCMD
1824 /* It's like this buffer is deleted. Watch out for autocommands that
1825 * change curbuf! If that happens, allocate a new buffer anyway. */
1826 if (curbuf->b_p_bl)
1827 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1828 if (buf == curbuf)
1829 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1830# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001831 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 return NULL;
1833# endif
1834#endif
1835#ifdef FEAT_QUICKFIX
1836# ifdef FEAT_AUTOCMD
1837 if (buf == curbuf)
1838# endif
1839 {
1840 /* Make sure 'bufhidden' and 'buftype' are empty */
1841 clear_string_option(&buf->b_p_bh);
1842 clear_string_option(&buf->b_p_bt);
1843 }
1844#endif
1845 }
1846 if (buf != curbuf || curbuf == NULL)
1847 {
1848 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1849 if (buf == NULL)
1850 {
1851 vim_free(ffname);
1852 return NULL;
1853 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001854#ifdef FEAT_EVAL
1855 /* init b: variables */
1856 buf->b_vars = dict_alloc();
1857 if (buf->b_vars == NULL)
1858 {
1859 vim_free(ffname);
1860 vim_free(buf);
1861 return NULL;
1862 }
1863 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866
1867 if (ffname != NULL)
1868 {
1869 buf->b_ffname = ffname;
1870 buf->b_sfname = vim_strsave(sfname);
1871 }
1872
1873 clear_wininfo(buf);
1874 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1875
1876 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1877 || buf->b_wininfo == NULL)
1878 {
1879 vim_free(buf->b_ffname);
1880 buf->b_ffname = NULL;
1881 vim_free(buf->b_sfname);
1882 buf->b_sfname = NULL;
1883 if (buf != curbuf)
1884 free_buffer(buf);
1885 return NULL;
1886 }
1887
1888 if (buf == curbuf)
1889 {
1890 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001891 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (buf != curbuf) /* autocommands deleted the buffer! */
1893 return NULL;
1894#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001895 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 return NULL;
1897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001899
1900 /* Init the options. */
1901 buf->b_p_initialized = FALSE;
1902 buf_copy_options(buf, BCO_ENTER);
1903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904#ifdef FEAT_KEYMAP
1905 /* need to reload lmaps and set b:keymap_name */
1906 curbuf->b_kmap_state |= KEYMAP_INIT;
1907#endif
1908 }
1909 else
1910 {
1911 /*
1912 * put new buffer at the end of the buffer list
1913 */
1914 buf->b_next = NULL;
1915 if (firstbuf == NULL) /* buffer list is empty */
1916 {
1917 buf->b_prev = NULL;
1918 firstbuf = buf;
1919 }
1920 else /* append new buffer at end of list */
1921 {
1922 lastbuf->b_next = buf;
1923 buf->b_prev = lastbuf;
1924 }
1925 lastbuf = buf;
1926
1927 buf->b_fnum = top_file_num++;
1928 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1929 {
1930 EMSG(_("W14: Warning: List of file names overflow"));
1931 if (emsg_silent == 0)
1932 {
1933 out_flush();
1934 ui_delay(3000L, TRUE); /* make sure it is noticed */
1935 }
1936 top_file_num = 1;
1937 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02001938 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
1940 /*
1941 * Always copy the options from the current buffer.
1942 */
1943 buf_copy_options(buf, BCO_ALWAYS);
1944 }
1945
1946 buf->b_wininfo->wi_fpos.lnum = lnum;
1947 buf->b_wininfo->wi_win = curwin;
1948
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001949#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001950 hash_init(&buf->b_s.b_keywtab);
1951 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952#endif
1953
1954 buf->b_fname = buf->b_sfname;
1955#ifdef UNIX
1956 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001957 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 else
1959 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001960 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 buf->b_dev = st.st_dev;
1962 buf->b_ino = st.st_ino;
1963 }
1964#endif
1965 buf->b_u_synced = TRUE;
1966 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001967 if (flags & BLN_DUMMY)
1968 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 buf_clear_file(buf);
1970 clrallmarks(buf); /* clear marks */
1971 fmarks_check_names(buf); /* check file marks for this file */
1972 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1973#ifdef FEAT_AUTOCMD
1974 if (!(flags & BLN_DUMMY))
1975 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001976 bufref_T bufref;
1977
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001978 /* Tricky: these autocommands may change the buffer list. They could
1979 * also split the window with re-using the one empty buffer. This may
1980 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001981 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001982 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001983 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001984 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001986 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001987 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001988 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001989 return NULL;
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001992 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 return NULL;
1994# endif
1995 }
1996#endif
1997
1998 return buf;
1999}
2000
2001/*
2002 * Free the memory for the options of a buffer.
2003 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2004 * 'fileencoding'.
2005 */
2006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002007free_buf_options(
2008 buf_T *buf,
2009 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 if (free_p_ff)
2012 {
2013#ifdef FEAT_MBYTE
2014 clear_string_option(&buf->b_p_fenc);
2015#endif
2016 clear_string_option(&buf->b_p_ff);
2017#ifdef FEAT_QUICKFIX
2018 clear_string_option(&buf->b_p_bh);
2019 clear_string_option(&buf->b_p_bt);
2020#endif
2021 }
2022#ifdef FEAT_FIND_ID
2023 clear_string_option(&buf->b_p_def);
2024 clear_string_option(&buf->b_p_inc);
2025# ifdef FEAT_EVAL
2026 clear_string_option(&buf->b_p_inex);
2027# endif
2028#endif
2029#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2030 clear_string_option(&buf->b_p_inde);
2031 clear_string_option(&buf->b_p_indk);
2032#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002033#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2034 clear_string_option(&buf->b_p_bexpr);
2035#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002036#if defined(FEAT_CRYPT)
2037 clear_string_option(&buf->b_p_cm);
2038#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002039#if defined(FEAT_EVAL)
2040 clear_string_option(&buf->b_p_fex);
2041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042#ifdef FEAT_CRYPT
2043 clear_string_option(&buf->b_p_key);
2044#endif
2045 clear_string_option(&buf->b_p_kp);
2046 clear_string_option(&buf->b_p_mps);
2047 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002048 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 clear_string_option(&buf->b_p_isk);
2050#ifdef FEAT_KEYMAP
2051 clear_string_option(&buf->b_p_keymap);
2052 ga_clear(&buf->b_kmap_ga);
2053#endif
2054#ifdef FEAT_COMMENTS
2055 clear_string_option(&buf->b_p_com);
2056#endif
2057#ifdef FEAT_FOLDING
2058 clear_string_option(&buf->b_p_cms);
2059#endif
2060 clear_string_option(&buf->b_p_nf);
2061#ifdef FEAT_SYN_HL
2062 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002063 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002064#endif
2065#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002066 clear_string_option(&buf->b_s.b_p_spc);
2067 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002068 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002069 buf->b_s.b_cap_prog = NULL;
2070 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#endif
2072#ifdef FEAT_SEARCHPATH
2073 clear_string_option(&buf->b_p_sua);
2074#endif
2075#ifdef FEAT_AUTOCMD
2076 clear_string_option(&buf->b_p_ft);
2077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078#ifdef FEAT_CINDENT
2079 clear_string_option(&buf->b_p_cink);
2080 clear_string_option(&buf->b_p_cino);
2081#endif
2082#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2083 clear_string_option(&buf->b_p_cinw);
2084#endif
2085#ifdef FEAT_INS_EXPAND
2086 clear_string_option(&buf->b_p_cpt);
2087#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002088#ifdef FEAT_COMPL_FUNC
2089 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002090 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092#ifdef FEAT_QUICKFIX
2093 clear_string_option(&buf->b_p_gp);
2094 clear_string_option(&buf->b_p_mp);
2095 clear_string_option(&buf->b_p_efm);
2096#endif
2097 clear_string_option(&buf->b_p_ep);
2098 clear_string_option(&buf->b_p_path);
2099 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002100 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101#ifdef FEAT_INS_EXPAND
2102 clear_string_option(&buf->b_p_dict);
2103 clear_string_option(&buf->b_p_tsr);
2104#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002105#ifdef FEAT_TEXTOBJ
2106 clear_string_option(&buf->b_p_qe);
2107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002109 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002110#ifdef FEAT_LISP
2111 clear_string_option(&buf->b_p_lw);
2112#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002113 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114}
2115
2116/*
2117 * get alternate file n
2118 * set linenr to lnum or altfpos.lnum if lnum == 0
2119 * also set cursor column to altfpos.col if 'startofline' is not set.
2120 * if (options & GETF_SETMARK) call setpcmark()
2121 * if (options & GETF_ALT) we are jumping to an alternate file.
2122 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2123 *
2124 * return FAIL for failure, OK for success
2125 */
2126 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002127buflist_getfile(
2128 int n,
2129 linenr_T lnum,
2130 int options,
2131 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 buf_T *buf;
2134#ifdef FEAT_WINDOWS
2135 win_T *wp = NULL;
2136#endif
2137 pos_T *fpos;
2138 colnr_T col;
2139
2140 buf = buflist_findnr(n);
2141 if (buf == NULL)
2142 {
2143 if ((options & GETF_ALT) && n == 0)
2144 EMSG(_(e_noalt));
2145 else
2146 EMSGN(_("E92: Buffer %ld not found"), n);
2147 return FAIL;
2148 }
2149
2150 /* if alternate file is the current buffer, nothing to do */
2151 if (buf == curbuf)
2152 return OK;
2153
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002154 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002155 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002156 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002158 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159#ifdef FEAT_AUTOCMD
2160 if (curbuf_locked())
2161 return FAIL;
2162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
2164 /* altfpos may be changed by getfile(), get it now */
2165 if (lnum == 0)
2166 {
2167 fpos = buflist_findfpos(buf);
2168 lnum = fpos->lnum;
2169 col = fpos->col;
2170 }
2171 else
2172 col = 0;
2173
2174#ifdef FEAT_WINDOWS
2175 if (options & GETF_SWITCH)
2176 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002177 /* If 'switchbuf' contains "useopen": jump to first window containing
2178 * "buf" if one exists */
2179 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002181
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002182 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002183 * page containing "buf" if one exists */
2184 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002185 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002186
2187 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2188 * current buffer isn't empty: open new tab or window */
2189 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2190 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002192 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002193 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002194 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2195 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002197 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 }
2199 }
2200#endif
2201
2202 ++RedrawingDisabled;
2203 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2204 lnum, forceit) <= 0)
2205 {
2206 --RedrawingDisabled;
2207
2208 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2209 if (!p_sol && col != 0)
2210 {
2211 curwin->w_cursor.col = col;
2212 check_cursor_col();
2213#ifdef FEAT_VIRTUALEDIT
2214 curwin->w_cursor.coladd = 0;
2215#endif
2216 curwin->w_set_curswant = TRUE;
2217 }
2218 return OK;
2219 }
2220 --RedrawingDisabled;
2221 return FAIL;
2222}
2223
2224/*
2225 * go to the last know line number for the current buffer
2226 */
2227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
2230 pos_T *fpos;
2231
2232 fpos = buflist_findfpos(curbuf);
2233
2234 curwin->w_cursor.lnum = fpos->lnum;
2235 check_cursor_lnum();
2236
2237 if (p_sol)
2238 curwin->w_cursor.col = 0;
2239 else
2240 {
2241 curwin->w_cursor.col = fpos->col;
2242 check_cursor_col();
2243#ifdef FEAT_VIRTUALEDIT
2244 curwin->w_cursor.coladd = 0;
2245#endif
2246 curwin->w_set_curswant = TRUE;
2247 }
2248}
2249
Bram Moolenaar81695252004-12-29 20:58:21 +00002250#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2251/*
2252 * Find file in buffer list by name (it has to be for the current window).
2253 * Returns NULL if not found.
2254 */
2255 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002256buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002257{
2258 char_u *ffname;
2259 buf_T *buf = NULL;
2260
2261 /* First make the name into a full path name */
2262 ffname = FullName_save(fname,
2263#ifdef UNIX
2264 TRUE /* force expansion, get rid of symbolic links */
2265#else
2266 FALSE
2267#endif
2268 );
2269 if (ffname != NULL)
2270 {
2271 buf = buflist_findname(ffname);
2272 vim_free(ffname);
2273 }
2274 return buf;
2275}
2276#endif
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278/*
2279 * Find file in buffer list by name (it has to be for the current window).
2280 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002281 * Skips dummy buffers.
2282 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
2284 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002285buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
2287#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002288 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 if (mch_stat((char *)ffname, &st) < 0)
2291 st.st_dev = (dev_T)-1;
2292 return buflist_findname_stat(ffname, &st);
2293}
2294
2295/*
2296 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2297 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002298 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 */
2300 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002301buflist_findname_stat(
2302 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002303 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305#endif
2306 buf_T *buf;
2307
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002308 /* Start at the last buffer, expect to find a match sooner. */
2309 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002310 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311#ifdef UNIX
2312 , stp
2313#endif
2314 ))
2315 return buf;
2316 return NULL;
2317}
2318
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002319#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2320 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321/*
2322 * Find file in buffer list by a regexp pattern.
2323 * Return fnum of the found buffer.
2324 * Return < 0 for error.
2325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002327buflist_findpat(
2328 char_u *pattern,
2329 char_u *pattern_end, /* pointer to first char after pattern */
2330 int unlisted, /* find unlisted buffers */
2331 int diffmode UNUSED, /* find diff-mode buffers only */
2332 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 int match = -1;
2336 int find_listed;
2337 char_u *pat;
2338 char_u *patend;
2339 int attempt;
2340 char_u *p;
2341 int toggledollar;
2342
2343 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2344 {
2345 if (*pattern == '%')
2346 match = curbuf->b_fnum;
2347 else
2348 match = curwin->w_alt_fnum;
2349#ifdef FEAT_DIFF
2350 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2351 match = -1;
2352#endif
2353 }
2354
2355 /*
2356 * Try four ways of matching a listed buffer:
2357 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002358 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 * attempt == 2: with '$' at end (only match at end)
2360 * attempt == 3: with '^' at start and '$' at end (only full match)
2361 * Repeat this for finding an unlisted buffer if there was no matching
2362 * listed buffer.
2363 */
2364 else
2365 {
2366 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2367 if (pat == NULL)
2368 return -1;
2369 patend = pat + STRLEN(pat) - 1;
2370 toggledollar = (patend > pat && *patend == '$');
2371
2372 /* First try finding a listed buffer. If not found and "unlisted"
2373 * is TRUE, try finding an unlisted buffer. */
2374 find_listed = TRUE;
2375 for (;;)
2376 {
2377 for (attempt = 0; attempt <= 3; ++attempt)
2378 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002379 regmatch_T regmatch;
2380
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 /* may add '^' and '$' */
2382 if (toggledollar)
2383 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2384 p = pat;
2385 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2386 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002387 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2388 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
2390 vim_free(pat);
2391 return -1;
2392 }
2393
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002394 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 if (buf->b_p_bl == find_listed
2396#ifdef FEAT_DIFF
2397 && (!diffmode || diff_mode_buf(buf))
2398#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002399 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002401 if (curtab_only)
2402 {
2403 /* Ignore the match if the buffer is not open in
2404 * the current tab. */
2405#ifdef FEAT_WINDOWS
2406 win_T *wp;
2407
2408 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2409 if (wp->w_buffer == buf)
2410 break;
2411 if (wp == NULL)
2412 continue;
2413#else
2414 if (curwin->w_buffer != buf)
2415 continue;
2416#endif
2417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 if (match >= 0) /* already found a match */
2419 {
2420 match = -2;
2421 break;
2422 }
2423 match = buf->b_fnum; /* remember first match */
2424 }
2425
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002426 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (match >= 0) /* found one match */
2428 break;
2429 }
2430
2431 /* Only search for unlisted buffers if there was no match with
2432 * a listed buffer. */
2433 if (!unlisted || !find_listed || match != -1)
2434 break;
2435 find_listed = FALSE;
2436 }
2437
2438 vim_free(pat);
2439 }
2440
2441 if (match == -2)
2442 EMSG2(_("E93: More than one match for %s"), pattern);
2443 else if (match < 0)
2444 EMSG2(_("E94: No matching buffer for %s"), pattern);
2445 return match;
2446}
2447#endif
2448
2449#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2450
2451/*
2452 * Find all buffer names that match.
2453 * For command line expansion of ":buf" and ":sbuf".
2454 * Return OK if matches found, FAIL otherwise.
2455 */
2456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002457ExpandBufnames(
2458 char_u *pat,
2459 int *num_file,
2460 char_u ***file,
2461 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 int count = 0;
2464 buf_T *buf;
2465 int round;
2466 char_u *p;
2467 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002468 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470 *num_file = 0; /* return values in case of FAIL */
2471 *file = NULL;
2472
Bram Moolenaar05159a02005-02-26 23:04:13 +00002473 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2474 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002476 patc = alloc((unsigned)STRLEN(pat) + 11);
2477 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002479 STRCPY(patc, "\\(^\\|[\\/]\\)");
2480 STRCPY(patc + 11, pat + 1);
2481 }
2482 else
2483 patc = pat;
2484
2485 /*
2486 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002487 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002488 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002489 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002490 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002491 regmatch_T regmatch;
2492
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002493 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002494 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002495 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2496 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002497 {
2498 if (patc != pat)
2499 vim_free(patc);
2500 return FAIL;
2501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 /*
2504 * round == 1: Count the matches.
2505 * round == 2: Build the array to keep the matches.
2506 */
2507 for (round = 1; round <= 2; ++round)
2508 {
2509 count = 0;
2510 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2511 {
2512 if (!buf->b_p_bl) /* skip unlisted buffers */
2513 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002514 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (p != NULL)
2516 {
2517 if (round == 1)
2518 ++count;
2519 else
2520 {
2521 if (options & WILD_HOME_REPLACE)
2522 p = home_replace_save(buf, p);
2523 else
2524 p = vim_strsave(p);
2525 (*file)[count++] = p;
2526 }
2527 }
2528 }
2529 if (count == 0) /* no match found, break here */
2530 break;
2531 if (round == 1)
2532 {
2533 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2534 if (*file == NULL)
2535 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002536 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002537 if (patc != pat)
2538 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 return FAIL;
2540 }
2541 }
2542 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002543 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (count) /* match(es) found, break here */
2545 break;
2546 }
2547
Bram Moolenaar05159a02005-02-26 23:04:13 +00002548 if (patc != pat)
2549 vim_free(patc);
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 *num_file = count;
2552 return (count == 0 ? FAIL : OK);
2553}
2554
2555#endif /* FEAT_CMDL_COMPL */
2556
2557#ifdef HAVE_BUFLIST_MATCH
2558/*
2559 * Check for a match on the file name for buffer "buf" with regprog "prog".
2560 */
2561 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002562buflist_match(
2563 regmatch_T *rmp,
2564 buf_T *buf,
2565 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
2567 char_u *match;
2568
2569 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002570 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002572 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574 return match;
2575}
2576
2577/*
2578 * Try matching the regexp in "prog" with file name "name".
2579 * Return "name" when there is a match, NULL when not.
2580 */
2581 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002582fname_match(
2583 regmatch_T *rmp,
2584 char_u *name,
2585 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 char_u *match = NULL;
2588 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590 if (name != NULL)
2591 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002592 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002593 rmp->rm_ic = p_fic || ignore_case;
2594 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 match = name;
2596 else
2597 {
2598 /* Replace $(HOME) with '~' and try matching again. */
2599 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002600 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 match = name;
2602 vim_free(p);
2603 }
2604 }
2605
2606 return match;
2607}
2608#endif
2609
2610/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002611 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
2613 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002614buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002616 char_u key[VIM_SIZEOF_INT * 2 + 1];
2617 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 if (nr == 0)
2620 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002621 sprintf((char *)key, "%x", nr);
2622 hi = hash_find(&buf_hashtab, key);
2623
2624 if (!HASHITEM_EMPTY(hi))
2625 return (buf_T *)(hi->hi_key
2626 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 return NULL;
2628}
2629
2630/*
2631 * Get name of file 'n' in the buffer list.
2632 * When the file has no name an empty string is returned.
2633 * home_replace() is used to shorten the file name (used for marks).
2634 * Returns a pointer to allocated memory, of NULL when failed.
2635 */
2636 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002637buflist_nr2name(
2638 int n,
2639 int fullname,
2640 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
2642 buf_T *buf;
2643
2644 buf = buflist_findnr(n);
2645 if (buf == NULL)
2646 return NULL;
2647 return home_replace_save(helptail ? buf : NULL,
2648 fullname ? buf->b_ffname : buf->b_fname);
2649}
2650
2651/*
2652 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2653 * When "copy_options" is TRUE save the local window option values.
2654 * When "lnum" is 0 only do the options.
2655 */
2656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002657buflist_setfpos(
2658 buf_T *buf,
2659 win_T *win,
2660 linenr_T lnum,
2661 colnr_T col,
2662 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
2664 wininfo_T *wip;
2665
2666 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2667 if (wip->wi_win == win)
2668 break;
2669 if (wip == NULL)
2670 {
2671 /* allocate a new entry */
2672 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2673 if (wip == NULL)
2674 return;
2675 wip->wi_win = win;
2676 if (lnum == 0) /* set lnum even when it's 0 */
2677 lnum = 1;
2678 }
2679 else
2680 {
2681 /* remove the entry from the list */
2682 if (wip->wi_prev)
2683 wip->wi_prev->wi_next = wip->wi_next;
2684 else
2685 buf->b_wininfo = wip->wi_next;
2686 if (wip->wi_next)
2687 wip->wi_next->wi_prev = wip->wi_prev;
2688 if (copy_options && wip->wi_optset)
2689 {
2690 clear_winopt(&wip->wi_opt);
2691#ifdef FEAT_FOLDING
2692 deleteFoldRecurse(&wip->wi_folds);
2693#endif
2694 }
2695 }
2696 if (lnum != 0)
2697 {
2698 wip->wi_fpos.lnum = lnum;
2699 wip->wi_fpos.col = col;
2700 }
2701 if (copy_options)
2702 {
2703 /* Save the window-specific option values. */
2704 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2705#ifdef FEAT_FOLDING
2706 wip->wi_fold_manual = win->w_fold_manual;
2707 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2708#endif
2709 wip->wi_optset = TRUE;
2710 }
2711
2712 /* insert the entry in front of the list */
2713 wip->wi_next = buf->b_wininfo;
2714 buf->b_wininfo = wip;
2715 wip->wi_prev = NULL;
2716 if (wip->wi_next)
2717 wip->wi_next->wi_prev = wip;
2718
2719 return;
2720}
2721
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002722#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002723static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002724
2725/*
2726 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2727 * page. That's because a diff is local to a tab page.
2728 */
2729 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002730wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002731{
2732 win_T *wp;
2733
2734 if (wip->wi_opt.wo_diff)
2735 {
2736 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2737 /* return FALSE when it's a window in the current tab page, thus
2738 * the buffer was in diff mode here */
2739 if (wip->wi_win == wp)
2740 return FALSE;
2741 return TRUE;
2742 }
2743 return FALSE;
2744}
2745#endif
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747/*
2748 * Find info for the current window in buffer "buf".
2749 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002750 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2751 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 * Returns NULL when there isn't any info.
2753 */
2754 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002755find_wininfo(
2756 buf_T *buf,
2757 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
2759 wininfo_T *wip;
2760
2761 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002762 if (wip->wi_win == curwin
2763#ifdef FEAT_DIFF
2764 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2765#endif
2766 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002768
2769 /* If no wininfo for curwin, use the first in the list (that doesn't have
2770 * 'diff' set and is in another tab page). */
2771 if (wip == NULL)
2772 {
2773#ifdef FEAT_DIFF
2774 if (skip_diff_buffer)
2775 {
2776 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2777 if (!wininfo_other_tab_diff(wip))
2778 break;
2779 }
2780 else
2781#endif
2782 wip = buf->b_wininfo;
2783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 return wip;
2785}
2786
2787/*
2788 * Reset the local window options to the values last used in this window.
2789 * If the buffer wasn't used in this window before, use the values from
2790 * the most recently used window. If the values were never set, use the
2791 * global values for the window.
2792 */
2793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 wininfo_T *wip;
2797
2798 clear_winopt(&curwin->w_onebuf_opt);
2799#ifdef FEAT_FOLDING
2800 clearFolding(curwin);
2801#endif
2802
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002803 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (wip != NULL && wip->wi_optset)
2805 {
2806 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2807#ifdef FEAT_FOLDING
2808 curwin->w_fold_manual = wip->wi_fold_manual;
2809 curwin->w_foldinvalid = TRUE;
2810 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2811#endif
2812 }
2813 else
2814 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2815
2816#ifdef FEAT_FOLDING
2817 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2818 if (p_fdls >= 0)
2819 curwin->w_p_fdl = p_fdls;
2820#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002821#ifdef FEAT_SYN_HL
2822 check_colorcolumn(curwin);
2823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824}
2825
2826/*
2827 * Find the position (lnum and col) for the buffer 'buf' for the current
2828 * window.
2829 * Returns a pointer to no_position if no position is found.
2830 */
2831 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002832buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
2834 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002835 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002837 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 if (wip != NULL)
2839 return &(wip->wi_fpos);
2840 else
2841 return &no_position;
2842}
2843
2844/*
2845 * Find the lnum for the buffer 'buf' for the current window.
2846 */
2847 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002848buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 return buflist_findfpos(buf)->lnum;
2851}
2852
2853#if defined(FEAT_LISTCMDS) || defined(PROTO)
2854/*
2855 * List all know file names (for :files and :buffers command).
2856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002858buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860 buf_T *buf;
2861 int len;
2862 int i;
2863
2864 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2865 {
2866 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002867 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2868 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2869 || (vim_strchr(eap->arg, '+')
2870 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2871 || (vim_strchr(eap->arg, 'a')
2872 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2873 || (vim_strchr(eap->arg, 'h')
2874 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2875 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2876 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2877 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2878 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2879 || (vim_strchr(eap->arg, '#')
2880 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 continue;
2882 msg_putchar('\n');
2883 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002884 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else
2886 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2887
Bram Moolenaar50cde822005-06-05 21:54:54 +00002888 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 buf->b_fnum,
2890 buf->b_p_bl ? ' ' : 'u',
2891 buf == curbuf ? '%' :
2892 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2893 buf->b_ml.ml_mfp == NULL ? ' ' :
2894 (buf->b_nwindows == 0 ? 'h' : 'a'),
2895 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2896 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002897 : (bufIsChanged(buf) ? '+' : ' '),
2898 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002899 if (len > IOSIZE - 20)
2900 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
2902 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 i = 40 - vim_strsize(IObuff);
2904 do
2905 {
2906 IObuff[len++] = ' ';
2907 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002908 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2909 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002910 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 msg_outtrans(IObuff);
2912 out_flush(); /* output one line at a time */
2913 ui_breakcheck();
2914 }
2915}
2916#endif
2917
2918/*
2919 * Get file name and line number for file 'fnum'.
2920 * Used by DoOneCmd() for translating '%' and '#'.
2921 * Used by insert_reg() and cmdline_paste() for '#' register.
2922 * Return FAIL if not found, OK for success.
2923 */
2924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925buflist_name_nr(
2926 int fnum,
2927 char_u **fname,
2928 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
2930 buf_T *buf;
2931
2932 buf = buflist_findnr(fnum);
2933 if (buf == NULL || buf->b_fname == NULL)
2934 return FAIL;
2935
2936 *fname = buf->b_fname;
2937 *lnum = buflist_findlnum(buf);
2938
2939 return OK;
2940}
2941
2942/*
2943 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2944 * The file name with the full path is also remembered, for when :cd is used.
2945 * Returns FAIL for failure (file name already in use by other buffer)
2946 * OK otherwise.
2947 */
2948 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002949setfname(
2950 buf_T *buf,
2951 char_u *ffname,
2952 char_u *sfname,
2953 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Bram Moolenaar81695252004-12-29 20:58:21 +00002955 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002957 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#endif
2959
2960 if (ffname == NULL || *ffname == NUL)
2961 {
2962 /* Removing the name. */
2963 vim_free(buf->b_ffname);
2964 vim_free(buf->b_sfname);
2965 buf->b_ffname = NULL;
2966 buf->b_sfname = NULL;
2967#ifdef UNIX
2968 st.st_dev = (dev_T)-1;
2969#endif
2970 }
2971 else
2972 {
2973 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2974 if (ffname == NULL) /* out of memory */
2975 return FAIL;
2976
2977 /*
2978 * if the file name is already used in another buffer:
2979 * - if the buffer is loaded, fail
2980 * - if the buffer is not loaded, delete it from the list
2981 */
2982#ifdef UNIX
2983 if (mch_stat((char *)ffname, &st) < 0)
2984 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002985#endif
2986 if (!(buf->b_flags & BF_DUMMY))
2987#ifdef UNIX
2988 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002990 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#endif
2992 if (obuf != NULL && obuf != buf)
2993 {
2994 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2995 {
2996 if (message)
2997 EMSG(_("E95: Buffer with this name already exists"));
2998 vim_free(ffname);
2999 return FAIL;
3000 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003001 /* delete from the list */
3002 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 sfname = vim_strsave(sfname);
3005 if (ffname == NULL || sfname == NULL)
3006 {
3007 vim_free(sfname);
3008 vim_free(ffname);
3009 return FAIL;
3010 }
3011#ifdef USE_FNAME_CASE
3012# ifdef USE_LONG_FNAME
3013 if (USE_LONG_FNAME)
3014# endif
3015 fname_case(sfname, 0); /* set correct case for short file name */
3016#endif
3017 vim_free(buf->b_ffname);
3018 vim_free(buf->b_sfname);
3019 buf->b_ffname = ffname;
3020 buf->b_sfname = sfname;
3021 }
3022 buf->b_fname = buf->b_sfname;
3023#ifdef UNIX
3024 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003025 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 else
3027 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003028 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 buf->b_dev = st.st_dev;
3030 buf->b_ino = st.st_ino;
3031 }
3032#endif
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
3036 buf_name_changed(buf);
3037 return OK;
3038}
3039
3040/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003041 * Crude way of changing the name of a buffer. Use with care!
3042 * The name should be relative to the current directory.
3043 */
3044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003045buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003046{
3047 buf_T *buf;
3048
3049 buf = buflist_findnr(fnum);
3050 if (buf != NULL)
3051 {
3052 vim_free(buf->b_sfname);
3053 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003054 buf->b_ffname = vim_strsave(name);
3055 buf->b_sfname = NULL;
3056 /* Allocate ffname and expand into full path. Also resolves .lnk
3057 * files on Win32. */
3058 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003059 buf->b_fname = buf->b_sfname;
3060 }
3061}
3062
3063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 * Take care of what needs to be done when the name of buffer "buf" has
3065 * changed.
3066 */
3067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003068buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 /*
3071 * If the file name changed, also change the name of the swapfile
3072 */
3073 if (buf->b_ml.ml_mfp != NULL)
3074 ml_setname(buf);
3075
3076 if (curwin->w_buffer == buf)
3077 check_arg_idx(curwin); /* check file name for arg list */
3078#ifdef FEAT_TITLE
3079 maketitle(); /* set window title */
3080#endif
3081#ifdef FEAT_WINDOWS
3082 status_redraw_all(); /* status lines need to be redrawn */
3083#endif
3084 fmarks_check_names(buf); /* check named file marks */
3085 ml_timestamp(buf); /* reset timestamp */
3086}
3087
3088/*
3089 * set alternate file name for current window
3090 *
3091 * Used by do_one_cmd(), do_write() and do_ecmd().
3092 * Return the buffer.
3093 */
3094 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003095setaltfname(
3096 char_u *ffname,
3097 char_u *sfname,
3098 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 buf_T *buf;
3101
3102 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3103 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003104 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 curwin->w_alt_fnum = buf->b_fnum;
3106 return buf;
3107}
3108
3109/*
3110 * Get alternate file name for current window.
3111 * Return NULL if there isn't any, and give error message if requested.
3112 */
3113 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003114getaltfname(
3115 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 char_u *fname;
3118 linenr_T dummy;
3119
3120 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3121 {
3122 if (errmsg)
3123 EMSG(_(e_noalt));
3124 return NULL;
3125 }
3126 return fname;
3127}
3128
3129/*
3130 * Add a file name to the buflist and return its number.
3131 * Uses same flags as buflist_new(), except BLN_DUMMY.
3132 *
3133 * used by qf_init(), main() and doarglist()
3134 */
3135 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003136buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
3138 buf_T *buf;
3139
3140 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3141 if (buf != NULL)
3142 return buf->b_fnum;
3143 return 0;
3144}
3145
3146#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3147/*
3148 * Adjust slashes in file names. Called after 'shellslash' was set.
3149 */
3150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003151buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 buf_T *bp;
3154
3155 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3156 {
3157 if (bp->b_ffname != NULL)
3158 slash_adjust(bp->b_ffname);
3159 if (bp->b_sfname != NULL)
3160 slash_adjust(bp->b_sfname);
3161 }
3162}
3163#endif
3164
3165/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003166 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 * Also save the local window option values.
3168 */
3169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003170buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003172 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173}
3174
3175/*
3176 * Return TRUE if 'ffname' is not the same file as current file.
3177 * Fname must have a full path (expanded by mch_FullName()).
3178 */
3179 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003180otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 return otherfile_buf(curbuf, ffname
3183#ifdef UNIX
3184 , NULL
3185#endif
3186 );
3187}
3188
3189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003190otherfile_buf(
3191 buf_T *buf,
3192 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003194 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003196 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
3198 /* no name is different */
3199 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3200 return TRUE;
3201 if (fnamecmp(ffname, buf->b_ffname) == 0)
3202 return FALSE;
3203#ifdef UNIX
3204 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003205 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaar8767f522016-07-01 17:17:39 +02003207 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (stp == NULL)
3209 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003210 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 st.st_dev = (dev_T)-1;
3212 stp = &st;
3213 }
3214 /* Use dev/ino to check if the files are the same, even when the names
3215 * are different (possible with links). Still need to compare the
3216 * name above, for when the file doesn't exist yet.
3217 * Problem: The dev/ino changes when a file is deleted (and created
3218 * again) and remains the same when renamed/moved. We don't want to
3219 * mch_stat() each buffer each time, that would be too slow. Get the
3220 * dev/ino again when they appear to match, but not when they appear
3221 * to be different: Could skip a buffer when it's actually the same
3222 * file. */
3223 if (buf_same_ino(buf, stp))
3224 {
3225 buf_setino(buf);
3226 if (buf_same_ino(buf, stp))
3227 return FALSE;
3228 }
3229 }
3230#endif
3231 return TRUE;
3232}
3233
3234#if defined(UNIX) || defined(PROTO)
3235/*
3236 * Set inode and device number for a buffer.
3237 * Must always be called when b_fname is changed!.
3238 */
3239 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003242 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3245 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003246 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 buf->b_dev = st.st_dev;
3248 buf->b_ino = st.st_ino;
3249 }
3250 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003251 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252}
3253
3254/*
3255 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3256 */
3257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003258buf_same_ino(
3259 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003260 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003262 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 && stp->st_dev == buf->b_dev
3264 && stp->st_ino == buf->b_ino);
3265}
3266#endif
3267
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003268/*
3269 * Print info about the current buffer.
3270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003272fileinfo(
3273 int fullname, /* when non-zero print full path */
3274 int shorthelp,
3275 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 char_u *name;
3278 int n;
3279 char_u *p;
3280 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003281 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
3283 buffer = alloc(IOSIZE);
3284 if (buffer == NULL)
3285 return;
3286
3287 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3288 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003289 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 p = buffer + STRLEN(buffer);
3291 }
3292 else
3293 p = buffer;
3294
3295 *p++ = '"';
3296 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003297 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 else
3299 {
3300 if (!fullname && curbuf->b_fname != NULL)
3301 name = curbuf->b_fname;
3302 else
3303 name = curbuf->b_ffname;
3304 home_replace(shorthelp ? curbuf : NULL, name, p,
3305 (int)(IOSIZE - (p - buffer)), TRUE);
3306 }
3307
Bram Moolenaara800b422010-06-27 01:15:55 +02003308 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 curbufIsChanged() ? (shortmess(SHM_MOD)
3310 ? " [+]" : _(" [Modified]")) : " ",
3311 (curbuf->b_flags & BF_NOTEDITED)
3312#ifdef FEAT_QUICKFIX
3313 && !bt_dontwrite(curbuf)
3314#endif
3315 ? _("[Not edited]") : "",
3316 (curbuf->b_flags & BF_NEW)
3317#ifdef FEAT_QUICKFIX
3318 && !bt_dontwrite(curbuf)
3319#endif
3320 ? _("[New file]") : "",
3321 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003322 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 : _("[readonly]")) : "",
3324 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3325 || curbuf->b_p_ro) ?
3326 " " : "");
3327 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3328 * causes an overflow, thus for large numbers divide instead. */
3329 if (curwin->w_cursor.lnum > 1000000L)
3330 n = (int)(((long)curwin->w_cursor.lnum) /
3331 ((long)curbuf->b_ml.ml_line_count / 100L));
3332 else
3333 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3334 (long)curbuf->b_ml.ml_line_count);
3335 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3336 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003337 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339#ifdef FEAT_CMDL_INFO
3340 else if (p_ru)
3341 {
3342 /* Current line and column are already on the screen -- webb */
3343 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003344 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003346 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 (long)curbuf->b_ml.ml_line_count, n);
3348 }
3349#endif
3350 else
3351 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003352 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 _("line %ld of %ld --%d%%-- col "),
3354 (long)curwin->w_cursor.lnum,
3355 (long)curbuf->b_ml.ml_line_count,
3356 n);
3357 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003358 len = STRLEN(buffer);
3359 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3361 }
3362
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003363 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365 if (dont_truncate)
3366 {
3367 /* Temporarily set msg_scroll to avoid the message being truncated.
3368 * First call msg_start() to get the message in the right place. */
3369 msg_start();
3370 n = msg_scroll;
3371 msg_scroll = TRUE;
3372 msg(buffer);
3373 msg_scroll = n;
3374 }
3375 else
3376 {
3377 p = msg_trunc_attr(buffer, FALSE, 0);
3378 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 /* Need to repeat the message after redrawing when:
3380 * - When restart_edit is set (otherwise there will be a delay
3381 * before redrawing).
3382 * - When the screen was scrolled but there is no wait-return
3383 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003384 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 }
3386
3387 vim_free(buffer);
3388}
3389
3390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391col_print(
3392 char_u *buf,
3393 size_t buflen,
3394 int col,
3395 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
3397 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003398 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003400 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401}
3402
3403#if defined(FEAT_TITLE) || defined(PROTO)
3404/*
3405 * put file name in title bar of window and in icon title
3406 */
3407
3408static char_u *lasttitle = NULL;
3409static char_u *lasticon = NULL;
3410
3411 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003412maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 char_u *p;
3415 char_u *t_str = NULL;
3416 char_u *i_name;
3417 char_u *i_str = NULL;
3418 int maxlen = 0;
3419 int len;
3420 int mustset;
3421 char_u buf[IOSIZE];
3422 int off;
3423
3424 if (!redrawing())
3425 {
3426 /* Postpone updating the title when 'lazyredraw' is set. */
3427 need_maketitle = TRUE;
3428 return;
3429 }
3430
3431 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003432 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 return;
3434
3435 if (p_title)
3436 {
3437 if (p_titlelen > 0)
3438 {
3439 maxlen = p_titlelen * Columns / 100;
3440 if (maxlen < 10)
3441 maxlen = 10;
3442 }
3443
3444 t_str = buf;
3445 if (*p_titlestring != NUL)
3446 {
3447#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003448 if (stl_syntax & STL_IN_TITLE)
3449 {
3450 int use_sandbox = FALSE;
3451 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003452
3453# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003454 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003455# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003456 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003458 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003459 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003460 if (called_emsg)
3461 set_string_option_direct((char_u *)"titlestring", -1,
3462 (char_u *)"", OPT_FREE, SID_ERROR);
3463 called_emsg |= save_called_emsg;
3464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 else
3466#endif
3467 t_str = p_titlestring;
3468 }
3469 else
3470 {
3471 /* format: "fname + (path) (1 of 2) - VIM" */
3472
Bram Moolenaar2c666692012-09-05 13:30:40 +02003473#define SPACE_FOR_FNAME (IOSIZE - 100)
3474#define SPACE_FOR_DIR (IOSIZE - 20)
3475#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003477 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 else
3479 {
3480 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003481 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
3484
3485 switch (bufIsChanged(curbuf)
3486 + (curbuf->b_p_ro * 2)
3487 + (!curbuf->b_p_ma * 4))
3488 {
3489 case 1: STRCAT(buf, " +"); break;
3490 case 2: STRCAT(buf, " ="); break;
3491 case 3: STRCAT(buf, " =+"); break;
3492 case 4:
3493 case 6: STRCAT(buf, " -"); break;
3494 case 5:
3495 case 7: STRCAT(buf, " -+"); break;
3496 }
3497
3498 if (curbuf->b_fname != NULL)
3499 {
3500 /* Get path of file, replace home dir with ~ */
3501 off = (int)STRLEN(buf);
3502 buf[off++] = ' ';
3503 buf[off++] = '(';
3504 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003505 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506#ifdef BACKSLASH_IN_FILENAME
3507 /* avoid "c:/name" to be reduced to "c" */
3508 if (isalpha(buf[off]) && buf[off + 1] == ':')
3509 off += 2;
3510#endif
3511 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003512 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003515 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003516 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003519
Bram Moolenaar2c666692012-09-05 13:30:40 +02003520 /* Translate unprintable chars and concatenate. Keep some
3521 * room for the server name. When there is no room (very long
3522 * file name) use (...). */
3523 if (off < SPACE_FOR_DIR)
3524 {
3525 p = transstr(buf + off);
3526 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3527 vim_free(p);
3528 }
3529 else
3530 {
3531 vim_strncpy(buf + off, (char_u *)"...",
3532 (size_t)(SPACE_FOR_ARGNR - off));
3533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 STRCAT(buf, ")");
3535 }
3536
Bram Moolenaar2c666692012-09-05 13:30:40 +02003537 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
3539#if defined(FEAT_CLIENTSERVER)
3540 if (serverName != NULL)
3541 {
3542 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003543 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545 else
3546#endif
3547 STRCAT(buf, " - VIM");
3548
3549 if (maxlen > 0)
3550 {
3551 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003552 if (vim_strsize(buf) > maxlen)
3553 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555 }
3556 }
3557 mustset = ti_change(t_str, &lasttitle);
3558
3559 if (p_icon)
3560 {
3561 i_str = buf;
3562 if (*p_iconstring != NUL)
3563 {
3564#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003565 if (stl_syntax & STL_IN_ICON)
3566 {
3567 int use_sandbox = FALSE;
3568 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003569
3570# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003571 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003572# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003573 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003575 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003576 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003577 if (called_emsg)
3578 set_string_option_direct((char_u *)"iconstring", -1,
3579 (char_u *)"", OPT_FREE, SID_ERROR);
3580 called_emsg |= save_called_emsg;
3581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 else
3583#endif
3584 i_str = p_iconstring;
3585 }
3586 else
3587 {
3588 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003589 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 else /* use file name only in icon */
3591 i_name = gettail(curbuf->b_ffname);
3592 *i_str = NUL;
3593 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (len > 100)
3596 {
3597 len -= 100;
3598#ifdef FEAT_MBYTE
3599 if (has_mbyte)
3600 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3601#endif
3602 i_name += len;
3603 }
3604 STRCPY(i_str, i_name);
3605 trans_characters(i_str, IOSIZE);
3606 }
3607 }
3608
3609 mustset |= ti_change(i_str, &lasticon);
3610
3611 if (mustset)
3612 resettitle();
3613}
3614
3615/*
3616 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3617 * from "str" if it does.
3618 * Return TRUE when "*last" changed.
3619 */
3620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003621ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
3623 if ((str == NULL) != (*last == NULL)
3624 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3625 {
3626 vim_free(*last);
3627 if (str == NULL)
3628 *last = NULL;
3629 else
3630 *last = vim_strsave(str);
3631 return TRUE;
3632 }
3633 return FALSE;
3634}
3635
3636/*
3637 * Put current window title back (used after calling a shell)
3638 */
3639 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003640resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 mch_settitle(lasttitle, lasticon);
3643}
Bram Moolenaarea408852005-06-25 22:49:46 +00003644
3645# if defined(EXITFREE) || defined(PROTO)
3646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003647free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003648{
3649 vim_free(lasttitle);
3650 vim_free(lasticon);
3651}
3652# endif
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654#endif /* FEAT_TITLE */
3655
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003656#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003658 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 * Return length of string in screen cells.
3660 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003661 * Normally works for window "wp", except when working for 'tabline' then it
3662 * is "curwin".
3663 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 * Items are drawn interspersed with the text that surrounds it
3665 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3666 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3667 *
3668 * If maxwidth is not zero, the string will be filled at any middle marker
3669 * or truncated if too long, fillchar is used for all whitespace.
3670 */
3671 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672build_stl_str_hl(
3673 win_T *wp,
3674 char_u *out, /* buffer to write into != NameBuff */
3675 size_t outlen, /* length of out[] */
3676 char_u *fmt,
3677 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3678 int fillchar,
3679 int maxwidth,
3680 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3681 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 char_u *p;
3684 char_u *s;
3685 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003686 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687#ifdef FEAT_EVAL
3688 win_T *o_curwin;
3689 buf_T *o_curbuf;
3690#endif
3691 int empty_line;
3692 colnr_T virtcol;
3693 long l;
3694 long n;
3695 int prevchar_isflag;
3696 int prevchar_isitem;
3697 int itemisflag;
3698 int fillable;
3699 char_u *str;
3700 long num;
3701 int width;
3702 int itemcnt;
3703 int curitem;
3704 int groupitem[STL_MAX_ITEM];
3705 int groupdepth;
3706 struct stl_item
3707 {
3708 char_u *start;
3709 int minwid;
3710 int maxwid;
3711 enum
3712 {
3713 Normal,
3714 Empty,
3715 Group,
3716 Middle,
3717 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003718 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 Trunc
3720 } type;
3721 } item[STL_MAX_ITEM];
3722 int minwid;
3723 int maxwid;
3724 int zeropad;
3725 char_u base;
3726 char_u opt;
3727#define TMPLEN 70
3728 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003729 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003730 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003731
3732#ifdef FEAT_EVAL
3733 /*
3734 * When the format starts with "%!" then evaluate it as an expression and
3735 * use the result as the actual format string.
3736 */
3737 if (fmt[0] == '%' && fmt[1] == '!')
3738 {
3739 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3740 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003741 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003742 }
3743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 if (fillchar == 0)
3746 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003747#ifdef FEAT_MBYTE
3748 /* Can't handle a multi-byte fill character yet. */
3749 else if (mb_char2len(fillchar) > 1)
3750 fillchar = '-';
3751#endif
3752
Bram Moolenaar567199b2013-04-24 16:52:36 +02003753 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3754 * p will become invalid when getting another buffer line. */
3755 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3756 empty_line = (*p == NUL);
3757
3758 /* Get the byte value now, in case we need it below. This is more
3759 * efficient than making a copy of the line. */
3760 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3761 byteval = 0;
3762 else
3763#ifdef FEAT_MBYTE
3764 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3765#else
3766 byteval = p[wp->w_cursor.col];
3767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769 groupdepth = 0;
3770 p = out;
3771 curitem = 0;
3772 prevchar_isflag = TRUE;
3773 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003774 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003776 if (curitem == STL_MAX_ITEM)
3777 {
3778 /* There are too many items. Add the error code to the statusline
3779 * to give the user a hint about what went wrong. */
3780 if (p + 6 < out + outlen)
3781 {
3782 mch_memmove(p, " E541", (size_t)5);
3783 p += 5;
3784 }
3785 break;
3786 }
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (*s != NUL && *s != '%')
3789 prevchar_isflag = prevchar_isitem = FALSE;
3790
3791 /*
3792 * Handle up to the next '%' or the end.
3793 */
3794 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3795 *p++ = *s++;
3796 if (*s == NUL || p + 1 >= out + outlen)
3797 break;
3798
3799 /*
3800 * Handle one '%' item.
3801 */
3802 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003803 if (*s == NUL) /* ignore trailing % */
3804 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (*s == '%')
3806 {
3807 if (p + 1 >= out + outlen)
3808 break;
3809 *p++ = *s++;
3810 prevchar_isflag = prevchar_isitem = FALSE;
3811 continue;
3812 }
3813 if (*s == STL_MIDDLEMARK)
3814 {
3815 s++;
3816 if (groupdepth > 0)
3817 continue;
3818 item[curitem].type = Middle;
3819 item[curitem++].start = p;
3820 continue;
3821 }
3822 if (*s == STL_TRUNCMARK)
3823 {
3824 s++;
3825 item[curitem].type = Trunc;
3826 item[curitem++].start = p;
3827 continue;
3828 }
3829 if (*s == ')')
3830 {
3831 s++;
3832 if (groupdepth < 1)
3833 continue;
3834 groupdepth--;
3835
3836 t = item[groupitem[groupdepth]].start;
3837 *p = NUL;
3838 l = vim_strsize(t);
3839 if (curitem > groupitem[groupdepth] + 1
3840 && item[groupitem[groupdepth]].minwid == 0)
3841 {
3842 /* remove group if all items are empty */
3843 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003844 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 break;
3846 if (n == curitem)
3847 {
3848 p = t;
3849 l = 0;
3850 }
3851 }
3852 if (l > item[groupitem[groupdepth]].maxwid)
3853 {
3854 /* truncate, remove n bytes of text at the start */
3855#ifdef FEAT_MBYTE
3856 if (has_mbyte)
3857 {
3858 /* Find the first character that should be included. */
3859 n = 0;
3860 while (l >= item[groupitem[groupdepth]].maxwid)
3861 {
3862 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003863 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865 }
3866 else
3867#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003868 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
3870 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003871 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 p = p - n + 1;
3873#ifdef FEAT_MBYTE
3874 /* Fill up space left over by half a double-wide char. */
3875 while (++l < item[groupitem[groupdepth]].minwid)
3876 *p++ = fillchar;
3877#endif
3878
3879 /* correct the start of the items for the truncation */
3880 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3881 {
3882 item[l].start -= n;
3883 if (item[l].start < t)
3884 item[l].start = t;
3885 }
3886 }
3887 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3888 {
3889 /* fill */
3890 n = item[groupitem[groupdepth]].minwid;
3891 if (n < 0)
3892 {
3893 /* fill by appending characters */
3894 n = 0 - n;
3895 while (l++ < n && p + 1 < out + outlen)
3896 *p++ = fillchar;
3897 }
3898 else
3899 {
3900 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003901 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 l = n - l;
3903 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003904 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 p += l;
3906 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3907 item[n].start += l;
3908 for ( ; l > 0; l--)
3909 *t++ = fillchar;
3910 }
3911 }
3912 continue;
3913 }
3914 minwid = 0;
3915 maxwid = 9999;
3916 zeropad = FALSE;
3917 l = 1;
3918 if (*s == '0')
3919 {
3920 s++;
3921 zeropad = TRUE;
3922 }
3923 if (*s == '-')
3924 {
3925 s++;
3926 l = -1;
3927 }
3928 if (VIM_ISDIGIT(*s))
3929 {
3930 minwid = (int)getdigits(&s);
3931 if (minwid < 0) /* overflow */
3932 minwid = 0;
3933 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003934 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 item[curitem].type = Highlight;
3937 item[curitem].start = p;
3938 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3939 s++;
3940 curitem++;
3941 continue;
3942 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003943 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3944 {
3945 if (*s == STL_TABCLOSENR)
3946 {
3947 if (minwid == 0)
3948 {
3949 /* %X ends the close label, go back to the previously
3950 * define tab label nr. */
3951 for (n = curitem - 1; n >= 0; --n)
3952 if (item[n].type == TabPage && item[n].minwid >= 0)
3953 {
3954 minwid = item[n].minwid;
3955 break;
3956 }
3957 }
3958 else
3959 /* close nrs are stored as negative values */
3960 minwid = - minwid;
3961 }
3962 item[curitem].type = TabPage;
3963 item[curitem].start = p;
3964 item[curitem].minwid = minwid;
3965 s++;
3966 curitem++;
3967 continue;
3968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (*s == '.')
3970 {
3971 s++;
3972 if (VIM_ISDIGIT(*s))
3973 {
3974 maxwid = (int)getdigits(&s);
3975 if (maxwid <= 0) /* overflow */
3976 maxwid = 50;
3977 }
3978 }
3979 minwid = (minwid > 50 ? 50 : minwid) * l;
3980 if (*s == '(')
3981 {
3982 groupitem[groupdepth++] = curitem;
3983 item[curitem].type = Group;
3984 item[curitem].start = p;
3985 item[curitem].minwid = minwid;
3986 item[curitem].maxwid = maxwid;
3987 s++;
3988 curitem++;
3989 continue;
3990 }
3991 if (vim_strchr(STL_ALL, *s) == NULL)
3992 {
3993 s++;
3994 continue;
3995 }
3996 opt = *s++;
3997
3998 /* OK - now for the real work */
3999 base = 'D';
4000 itemisflag = FALSE;
4001 fillable = TRUE;
4002 num = -1;
4003 str = NULL;
4004 switch (opt)
4005 {
4006 case STL_FILEPATH:
4007 case STL_FULLPATH:
4008 case STL_FILENAME:
4009 fillable = FALSE; /* don't change ' ' to fillchar */
4010 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004011 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 else
4013 {
4014 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004015 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4017 }
4018 trans_characters(NameBuff, MAXPATHL);
4019 if (opt != STL_FILENAME)
4020 str = NameBuff;
4021 else
4022 str = gettail(NameBuff);
4023 break;
4024
4025 case STL_VIM_EXPR: /* '{' */
4026 itemisflag = TRUE;
4027 t = p;
4028 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4029 *p++ = *s++;
4030 if (*s != '}') /* missing '}' or out of space */
4031 break;
4032 s++;
4033 *p = 0;
4034 p = t;
4035
4036#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004037 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4039
4040 o_curbuf = curbuf;
4041 o_curwin = curwin;
4042 curwin = wp;
4043 curbuf = wp->w_buffer;
4044
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004045 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047 curwin = o_curwin;
4048 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004049 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051 if (str != NULL && *str != 0)
4052 {
4053 if (*skipdigits(str) == NUL)
4054 {
4055 num = atoi((char *)str);
4056 vim_free(str);
4057 str = NULL;
4058 itemisflag = FALSE;
4059 }
4060 }
4061#endif
4062 break;
4063
4064 case STL_LINE:
4065 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4066 ? 0L : (long)(wp->w_cursor.lnum);
4067 break;
4068
4069 case STL_NUMLINES:
4070 num = wp->w_buffer->b_ml.ml_line_count;
4071 break;
4072
4073 case STL_COLUMN:
4074 num = !(State & INSERT) && empty_line
4075 ? 0 : (int)wp->w_cursor.col + 1;
4076 break;
4077
4078 case STL_VIRTCOL:
4079 case STL_VIRTCOL_ALT:
4080 /* In list mode virtcol needs to be recomputed */
4081 virtcol = wp->w_virtcol;
4082 if (wp->w_p_list && lcs_tab1 == NUL)
4083 {
4084 wp->w_p_list = FALSE;
4085 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4086 wp->w_p_list = TRUE;
4087 }
4088 ++virtcol;
4089 /* Don't display %V if it's the same as %c. */
4090 if (opt == STL_VIRTCOL_ALT
4091 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4092 ? 0 : (int)wp->w_cursor.col + 1)))
4093 break;
4094 num = (long)virtcol;
4095 break;
4096
4097 case STL_PERCENTAGE:
4098 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4099 (long)wp->w_buffer->b_ml.ml_line_count);
4100 break;
4101
4102 case STL_ALTPERCENT:
4103 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004104 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 break;
4106
4107 case STL_ARGLISTSTAT:
4108 fillable = FALSE;
4109 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004110 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 str = tmp;
4112 break;
4113
4114 case STL_KEYMAP:
4115 fillable = FALSE;
4116 if (get_keymap_str(wp, tmp, TMPLEN))
4117 str = tmp;
4118 break;
4119 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004120#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004121 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122#else
4123 num = 0;
4124#endif
4125 break;
4126
4127 case STL_BUFNO:
4128 num = wp->w_buffer->b_fnum;
4129 break;
4130
4131 case STL_OFFSET_X:
4132 base = 'X';
4133 case STL_OFFSET:
4134#ifdef FEAT_BYTEOFF
4135 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4136 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4137 0L : l + 1 + (!(State & INSERT) && empty_line ?
4138 0 : (int)wp->w_cursor.col);
4139#endif
4140 break;
4141
4142 case STL_BYTEVAL_X:
4143 base = 'X';
4144 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004145 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 if (num == NL)
4147 num = 0;
4148 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4149 num = NL;
4150 break;
4151
4152 case STL_ROFLAG:
4153 case STL_ROFLAG_ALT:
4154 itemisflag = TRUE;
4155 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004156 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 break;
4158
4159 case STL_HELPFLAG:
4160 case STL_HELPFLAG_ALT:
4161 itemisflag = TRUE;
4162 if (wp->w_buffer->b_help)
4163 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004164 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 break;
4166
4167#ifdef FEAT_AUTOCMD
4168 case STL_FILETYPE:
4169 if (*wp->w_buffer->b_p_ft != NUL
4170 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4171 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004172 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4173 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 str = tmp;
4175 }
4176 break;
4177
4178 case STL_FILETYPE_ALT:
4179 itemisflag = TRUE;
4180 if (*wp->w_buffer->b_p_ft != NUL
4181 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4182 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004183 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4184 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 for (t = tmp; *t != 0; t++)
4186 *t = TOUPPER_LOC(*t);
4187 str = tmp;
4188 }
4189 break;
4190#endif
4191
4192#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4193 case STL_PREVIEWFLAG:
4194 case STL_PREVIEWFLAG_ALT:
4195 itemisflag = TRUE;
4196 if (wp->w_p_pvw)
4197 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4198 : _("[Preview]"));
4199 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004200
4201 case STL_QUICKFIX:
4202 if (bt_quickfix(wp->w_buffer))
4203 str = (char_u *)(wp->w_llist_ref
4204 ? _(msg_loclist)
4205 : _(msg_qflist));
4206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207#endif
4208
4209 case STL_MODIFIED:
4210 case STL_MODIFIED_ALT:
4211 itemisflag = TRUE;
4212 switch ((opt == STL_MODIFIED_ALT)
4213 + bufIsChanged(wp->w_buffer) * 2
4214 + (!wp->w_buffer->b_p_ma) * 4)
4215 {
4216 case 2: str = (char_u *)"[+]"; break;
4217 case 3: str = (char_u *)",+"; break;
4218 case 4: str = (char_u *)"[-]"; break;
4219 case 5: str = (char_u *)",-"; break;
4220 case 6: str = (char_u *)"[+-]"; break;
4221 case 7: str = (char_u *)",+-"; break;
4222 }
4223 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004224
4225 case STL_HIGHLIGHT:
4226 t = s;
4227 while (*s != '#' && *s != NUL)
4228 ++s;
4229 if (*s == '#')
4230 {
4231 item[curitem].type = Highlight;
4232 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004233 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004234 curitem++;
4235 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004236 if (*s != NUL)
4237 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004238 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
4240
4241 item[curitem].start = p;
4242 item[curitem].type = Normal;
4243 if (str != NULL && *str)
4244 {
4245 t = str;
4246 if (itemisflag)
4247 {
4248 if ((t[0] && t[1])
4249 && ((!prevchar_isitem && *t == ',')
4250 || (prevchar_isflag && *t == ' ')))
4251 t++;
4252 prevchar_isflag = TRUE;
4253 }
4254 l = vim_strsize(t);
4255 if (l > 0)
4256 prevchar_isitem = TRUE;
4257 if (l > maxwid)
4258 {
4259 while (l >= maxwid)
4260#ifdef FEAT_MBYTE
4261 if (has_mbyte)
4262 {
4263 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004264 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 else
4267#endif
4268 l -= byte2cells(*t++);
4269 if (p + 1 >= out + outlen)
4270 break;
4271 *p++ = '<';
4272 }
4273 if (minwid > 0)
4274 {
4275 for (; l < minwid && p + 1 < out + outlen; l++)
4276 {
4277 /* Don't put a "-" in front of a digit. */
4278 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4279 *p++ = ' ';
4280 else
4281 *p++ = fillchar;
4282 }
4283 minwid = 0;
4284 }
4285 else
4286 minwid *= -1;
4287 while (*t && p + 1 < out + outlen)
4288 {
4289 *p++ = *t++;
4290 /* Change a space by fillchar, unless fillchar is '-' and a
4291 * digit follows. */
4292 if (fillable && p[-1] == ' '
4293 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4294 p[-1] = fillchar;
4295 }
4296 for (; l < minwid && p + 1 < out + outlen; l++)
4297 *p++ = fillchar;
4298 }
4299 else if (num >= 0)
4300 {
4301 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4302 char_u nstr[20];
4303
4304 if (p + 20 >= out + outlen)
4305 break; /* not sufficient space */
4306 prevchar_isitem = TRUE;
4307 t = nstr;
4308 if (opt == STL_VIRTCOL_ALT)
4309 {
4310 *t++ = '-';
4311 minwid--;
4312 }
4313 *t++ = '%';
4314 if (zeropad)
4315 *t++ = '0';
4316 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004317 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 *t = 0;
4319
4320 for (n = num, l = 1; n >= nbase; n /= nbase)
4321 l++;
4322 if (opt == STL_VIRTCOL_ALT)
4323 l++;
4324 if (l > maxwid)
4325 {
4326 l += 2;
4327 n = l - maxwid;
4328 while (l-- > maxwid)
4329 num /= nbase;
4330 *t++ = '>';
4331 *t++ = '%';
4332 *t = t[-3];
4333 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004334 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4335 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004338 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4339 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 p += STRLEN(p);
4341 }
4342 else
4343 item[curitem].type = Empty;
4344
4345 if (opt == STL_VIM_EXPR)
4346 vim_free(str);
4347
4348 if (num >= 0 || (!itemisflag && str && *str))
4349 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4350 curitem++;
4351 }
4352 *p = NUL;
4353 itemcnt = curitem;
4354
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004355#ifdef FEAT_EVAL
4356 if (usefmt != fmt)
4357 vim_free(usefmt);
4358#endif
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 width = vim_strsize(out);
4361 if (maxwidth > 0 && width > maxwidth)
4362 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004363 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 l = 0;
4365 if (itemcnt == 0)
4366 s = out;
4367 else
4368 {
4369 for ( ; l < itemcnt; l++)
4370 if (item[l].type == Trunc)
4371 {
4372 /* Truncate at %< item. */
4373 s = item[l].start;
4374 break;
4375 }
4376 if (l == itemcnt)
4377 {
4378 /* No %< item, truncate first item. */
4379 s = item[0].start;
4380 l = 0;
4381 }
4382 }
4383
4384 if (width - vim_strsize(s) >= maxwidth)
4385 {
4386 /* Truncation mark is beyond max length */
4387#ifdef FEAT_MBYTE
4388 if (has_mbyte)
4389 {
4390 s = out;
4391 width = 0;
4392 for (;;)
4393 {
4394 width += ptr2cells(s);
4395 if (width >= maxwidth)
4396 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004397 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
4399 /* Fill up for half a double-wide character. */
4400 while (++width < maxwidth)
4401 *s++ = fillchar;
4402 }
4403 else
4404#endif
4405 s = out + maxwidth - 1;
4406 for (l = 0; l < itemcnt; l++)
4407 if (item[l].start > s)
4408 break;
4409 itemcnt = l;
4410 *s++ = '>';
4411 *s = 0;
4412 }
4413 else
4414 {
4415#ifdef FEAT_MBYTE
4416 if (has_mbyte)
4417 {
4418 n = 0;
4419 while (width >= maxwidth)
4420 {
4421 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004422 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424 }
4425 else
4426#endif
4427 n = width - maxwidth + 1;
4428 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004429 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 *s = '<';
4431
4432 /* Fill up for half a double-wide character. */
4433 while (++width < maxwidth)
4434 {
4435 s = s + STRLEN(s);
4436 *s++ = fillchar;
4437 *s = NUL;
4438 }
4439
4440 --n; /* count the '<' */
4441 for (; l < itemcnt; l++)
4442 {
4443 if (item[l].start - n >= s)
4444 item[l].start -= n;
4445 else
4446 item[l].start = s;
4447 }
4448 }
4449 width = maxwidth;
4450 }
4451 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4452 {
4453 /* Apply STL_MIDDLE if any */
4454 for (l = 0; l < itemcnt; l++)
4455 if (item[l].type == Middle)
4456 break;
4457 if (l < itemcnt)
4458 {
4459 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004460 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 for (s = item[l].start; s < p; s++)
4462 *s = fillchar;
4463 for (l++; l < itemcnt; l++)
4464 item[l].start += maxwidth - width;
4465 width = maxwidth;
4466 }
4467 }
4468
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004469 /* Store the info about highlighting. */
4470 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004472 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 for (l = 0; l < itemcnt; l++)
4474 {
4475 if (item[l].type == Highlight)
4476 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004477 sp->start = item[l].start;
4478 sp->userhl = item[l].minwid;
4479 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004482 sp->start = NULL;
4483 sp->userhl = 0;
4484 }
4485
4486 /* Store the info about tab pages labels. */
4487 if (tabtab != NULL)
4488 {
4489 sp = tabtab;
4490 for (l = 0; l < itemcnt; l++)
4491 {
4492 if (item[l].type == TabPage)
4493 {
4494 sp->start = item[l].start;
4495 sp->userhl = item[l].minwid;
4496 sp++;
4497 }
4498 }
4499 sp->start = NULL;
4500 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502
4503 return width;
4504}
4505#endif /* FEAT_STL_OPT */
4506
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004507#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4508 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004510 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4511 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 */
4513 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004514get_rel_pos(
4515 win_T *wp,
4516 char_u *buf,
4517 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518{
4519 long above; /* number of lines above window */
4520 long below; /* number of lines below window */
4521
Bram Moolenaar0027c212015-01-07 13:31:52 +01004522 if (buflen < 3) /* need at least 3 chars for writing */
4523 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 above = wp->w_topline - 1;
4525#ifdef FEAT_DIFF
4526 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004527 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4528 above = 0; /* All buffer lines are displayed and there is an
4529 * indication of filler lines, that can be considered
4530 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531#endif
4532 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4533 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004534 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4535 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004537 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004539 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 ? (int)(above / ((above + below) / 100L))
4541 : (int)(above * 100L / (above + below)));
4542}
4543#endif
4544
4545/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004546 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 * Return TRUE if it was appended.
4548 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004549 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004550append_arg_number(
4551 win_T *wp,
4552 char_u *buf,
4553 int buflen,
4554 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555{
4556 char_u *p;
4557
4558 if (ARGCOUNT <= 1) /* nothing to do */
4559 return FALSE;
4560
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004561 p = buf + STRLEN(buf); /* go to the end of the buffer */
4562 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 return FALSE;
4564 *p++ = ' ';
4565 *p++ = '(';
4566 if (add_file)
4567 {
4568 STRCPY(p, "file ");
4569 p += 5;
4570 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004571 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4572 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4574 return TRUE;
4575}
4576
4577/*
4578 * If fname is not a full path, make it a full path.
4579 * Returns pointer to allocated memory (NULL for failure).
4580 */
4581 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004582fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583{
4584 /*
4585 * Force expanding the path always for Unix, because symbolic links may
4586 * mess up the full path name, even though it starts with a '/'.
4587 * Also expand when there is ".." in the file name, try to remove it,
4588 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004589 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 * For MS-Windows also expand names like "longna~1" to "longname".
4591 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004592#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 return FullName_save(fname, TRUE);
4594#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004595 if (!vim_isAbsName(fname)
4596 || strstr((char *)fname, "..") != NULL
4597 || strstr((char *)fname, "//") != NULL
4598# ifdef BACKSLASH_IN_FILENAME
4599 || strstr((char *)fname, "\\\\") != NULL
4600# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004601# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 )
4605 return FullName_save(fname, FALSE);
4606
4607 fname = vim_strsave(fname);
4608
Bram Moolenaar9b942202007-10-03 12:31:33 +00004609# ifdef USE_FNAME_CASE
4610# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
4614 if (fname != NULL)
4615 fname_case(fname, 0); /* set correct case for file name */
4616 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004617# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 return fname;
4620#endif
4621}
4622
4623/*
4624 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4625 * "ffname" becomes a pointer to allocated memory (or NULL).
4626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004628fname_expand(
4629 buf_T *buf UNUSED,
4630 char_u **ffname,
4631 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 if (*ffname == NULL) /* if no file name given, nothing to do */
4634 return;
4635 if (*sfname == NULL) /* if no short file name given, use ffname */
4636 *sfname = *ffname;
4637 *ffname = fix_fname(*ffname); /* expand to full path */
4638
4639#ifdef FEAT_SHORTCUT
4640 if (!buf->b_p_bin)
4641 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004642 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 /* If the file name is a shortcut file, use the file it links to. */
4645 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004646 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
4648 vim_free(*ffname);
4649 *ffname = rfname;
4650 *sfname = rfname;
4651 }
4652 }
4653#endif
4654}
4655
4656/*
4657 * Get the file name for an argument list entry.
4658 */
4659 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004660alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
4662 buf_T *bp;
4663
4664 /* Use the name from the associated buffer if it exists. */
4665 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004666 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return aep->ae_fname;
4668 return bp->b_fname;
4669}
4670
4671#if defined(FEAT_WINDOWS) || defined(PROTO)
4672/*
4673 * do_arg_all(): Open up to 'count' windows, one for each argument.
4674 */
4675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004676do_arg_all(
4677 int count,
4678 int forceit, /* hide buffers in current windows */
4679 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680{
4681 int i;
4682 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004683 char_u *opened; /* Array of weight for which args are open:
4684 * 0: not opened
4685 * 1: opened in other tab
4686 * 2: opened in curtab
4687 * 3: opened in curtab and curwin
4688 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004689 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 int use_firstwin = FALSE; /* use first window for arglist */
4691 int split_ret = OK;
4692 int p_ea_save;
4693 alist_T *alist; /* argument list to be used */
4694 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004695 tabpage_T *tpnext;
4696 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004697 win_T *old_curwin, *last_curwin;
4698 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004699 win_T *new_curwin = NULL;
4700 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
4702 if (ARGCOUNT <= 0)
4703 {
4704 /* Don't give an error message. We don't want it when the ":all"
4705 * command is in the .vimrc. */
4706 return;
4707 }
4708 setpcmark();
4709
4710 opened_len = ARGCOUNT;
4711 opened = alloc_clear((unsigned)opened_len);
4712 if (opened == NULL)
4713 return;
4714
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004715 /* Autocommands may do anything to the argument list. Make sure it's not
4716 * freed while we are working here by "locking" it. We still have to
4717 * watch out for its size to be changed. */
4718 alist = curwin->w_alist;
4719 ++alist->al_refcount;
4720
4721 old_curwin = curwin;
4722 old_curtab = curtab;
4723
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004724# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004726# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
4728 /*
4729 * Try closing all windows that are not in the argument list.
4730 * Also close windows that are not full width;
4731 * When 'hidden' or "forceit" set the buffer becomes hidden.
4732 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004733 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004735 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004736 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004737 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004739 tpnext = curtab->tp_next;
4740 for (wp = firstwin; wp != NULL; wp = wpnext)
4741 {
4742 wpnext = wp->w_next;
4743 buf = wp->w_buffer;
4744 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004745 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004746 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004747 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004748 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004750 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004751 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004753 if (i < alist->al_ga.ga_len
4754 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4755 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4756 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004758 int weight = 1;
4759
4760 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004761 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004762 ++weight;
4763 if (old_curwin == wp)
4764 ++weight;
4765 }
4766
4767 if (weight > (int)opened[i])
4768 {
4769 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004770 if (i == 0)
4771 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004772 if (new_curwin != NULL)
4773 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004774 new_curwin = wp;
4775 new_curtab = curtab;
4776 }
4777 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004778 else if (keep_tabs)
4779 i = opened_len;
4780
4781 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004782 {
4783 /* Use the current argument list for all windows
4784 * containing a file from it. */
4785 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004786 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004787 ++wp->w_alist->al_refcount;
4788 }
4789 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004793 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004795 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004797 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004798 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004800 /* If the buffer was changed, and we would like to hide it,
4801 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004802 if (!P_HID(buf) && buf->b_nwindows <= 1
4803 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004805#ifdef FEAT_AUTOCMD
4806 bufref_T bufref;
4807
4808 set_bufref(&bufref, buf);
4809#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004810 (void)autowrite(buf, FALSE);
4811#ifdef FEAT_AUTOCMD
4812 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004813 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004814 {
4815 wpnext = firstwin; /* start all over... */
4816 continue;
4817 }
4818#endif
4819 }
4820#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004821 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004822 if (firstwin == lastwin
4823 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004824#endif
4825 use_firstwin = TRUE;
4826#ifdef FEAT_WINDOWS
4827 else
4828 {
4829 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4830# ifdef FEAT_AUTOCMD
4831 /* check if autocommands removed the next window */
4832 if (!win_valid(wpnext))
4833 wpnext = firstwin; /* start all over... */
4834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836#endif
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
4839 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004840
4841 /* Without the ":tab" modifier only do the current tab page. */
4842 if (had_tab == 0 || tpnext == NULL)
4843 break;
4844
4845# ifdef FEAT_AUTOCMD
4846 /* check if autocommands removed the next tab page */
4847 if (!valid_tabpage(tpnext))
4848 tpnext = first_tabpage; /* start all over...*/
4849# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004850 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852
4853 /*
4854 * Open a window for files in the argument list that don't have one.
4855 * ARGCOUNT may change while doing this, because of autocommands.
4856 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004857 if (count > opened_len || count <= 0)
4858 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
4860#ifdef FEAT_AUTOCMD
4861 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4862 ++autocmd_no_enter;
4863 ++autocmd_no_leave;
4864#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004865 last_curwin = curwin;
4866 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004868#ifdef FEAT_WINDOWS
4869 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4870 * leaving an empty tab page when executed locally. */
4871 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4872 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4873 use_firstwin = TRUE;
4874#endif
4875
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004876 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 {
4878 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4879 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004880 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
4882 /* Move the already present window to below the current window */
4883 if (curwin->w_arg_idx != i)
4884 {
4885 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4886 {
4887 if (wpnext->w_arg_idx == i)
4888 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004889 if (keep_tabs)
4890 {
4891 new_curwin = wpnext;
4892 new_curtab = curtab;
4893 }
4894 else
4895 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 break;
4897 }
4898 }
4899 }
4900 }
4901 else if (split_ret == OK)
4902 {
4903 if (!use_firstwin) /* split current window */
4904 {
4905 p_ea_save = p_ea;
4906 p_ea = TRUE; /* use space from all windows */
4907 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4908 p_ea = p_ea_save;
4909 if (split_ret == FAIL)
4910 continue;
4911 }
4912#ifdef FEAT_AUTOCMD
4913 else /* first window: do autocmd for leaving this buffer */
4914 --autocmd_no_leave;
4915#endif
4916
4917 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004918 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 */
4920 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004921 if (i == 0)
4922 {
4923 new_curwin = curwin;
4924 new_curtab = curtab;
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4927 ECMD_ONE,
4928 ((P_HID(curwin->w_buffer)
4929 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004930 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931#ifdef FEAT_AUTOCMD
4932 if (use_firstwin)
4933 ++autocmd_no_leave;
4934#endif
4935 use_firstwin = FALSE;
4936 }
4937 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004938
4939 /* When ":tab" was used open a new tab for a new window repeatedly. */
4940 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4941 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943
4944 /* Remove the "lock" on the argument list. */
4945 alist_unlink(alist);
4946
4947#ifdef FEAT_AUTOCMD
4948 --autocmd_no_enter;
4949#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004950 /* restore last referenced tabpage's curwin */
4951 if (last_curtab != new_curtab)
4952 {
4953 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004954 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004955 if (win_valid(last_curwin))
4956 win_enter(last_curwin, FALSE);
4957 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004958 /* to window with first arg */
4959 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004960 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004961 if (win_valid(new_curwin))
4962 win_enter(new_curwin, FALSE);
4963
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964#ifdef FEAT_AUTOCMD
4965 --autocmd_no_leave;
4966#endif
4967 vim_free(opened);
4968}
4969
4970# if defined(FEAT_LISTCMDS) || defined(PROTO)
4971/*
4972 * Open a window for a number of buffers.
4973 */
4974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004975ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
4977 buf_T *buf;
4978 win_T *wp, *wpnext;
4979 int split_ret = OK;
4980 int p_ea_save;
4981 int open_wins = 0;
4982 int r;
4983 int count; /* Maximum number of windows to open. */
4984 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004985#ifdef FEAT_WINDOWS
4986 int had_tab = cmdmod.tab;
4987 tabpage_T *tpnext;
4988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
4990 if (eap->addr_count == 0) /* make as many windows as possible */
4991 count = 9999;
4992 else
4993 count = eap->line2; /* make as many windows as specified */
4994 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4995 all = FALSE;
4996 else
4997 all = TRUE;
4998
4999 setpcmark();
5000
5001#ifdef FEAT_GUI
5002 need_mouse_correct = TRUE;
5003#endif
5004
5005 /*
5006 * Close superfluous windows (two windows for the same buffer).
5007 * Also close windows that are not full-width.
5008 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005009#ifdef FEAT_WINDOWS
5010 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005011 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005012 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005015 tpnext = curtab->tp_next;
5016 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005018 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005019 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005020#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005021 || ((cmdmod.split & WSP_VERT)
5022 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005023 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005024 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005025 || (had_tab > 0 && wp != firstwin)
5026#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02005027 ) && firstwin != lastwin
5028#ifdef FEAT_AUTOCMD
5029 && !(wp->w_closing || wp->w_buffer->b_closing)
5030#endif
5031 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005032 {
5033 win_close(wp, FALSE);
5034#ifdef FEAT_AUTOCMD
5035 wpnext = firstwin; /* just in case an autocommand does
5036 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005037 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005038 open_wins = 0;
5039#endif
5040 }
5041 else
5042 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005044
5045#ifdef FEAT_WINDOWS
5046 /* Without the ":tab" modifier only do the current tab page. */
5047 if (had_tab == 0 || tpnext == NULL)
5048 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005049 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
5053 /*
5054 * Go through the buffer list. When a buffer doesn't have a window yet,
5055 * open one. Otherwise move the window to the right position.
5056 * Watch out for autocommands that delete buffers or windows!
5057 */
5058#ifdef FEAT_AUTOCMD
5059 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5060 ++autocmd_no_enter;
5061#endif
5062 win_enter(lastwin, FALSE);
5063#ifdef FEAT_AUTOCMD
5064 ++autocmd_no_leave;
5065#endif
5066 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5067 {
5068 /* Check if this buffer needs a window */
5069 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5070 continue;
5071
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005072#ifdef FEAT_WINDOWS
5073 if (had_tab != 0)
5074 {
5075 /* With the ":tab" modifier don't move the window. */
5076 if (buf->b_nwindows > 0)
5077 wp = lastwin; /* buffer has a window, skip it */
5078 else
5079 wp = NULL;
5080 }
5081 else
5082#endif
5083 {
5084 /* Check if this buffer already has a window */
5085 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5086 if (wp->w_buffer == buf)
5087 break;
5088 /* If the buffer already has a window, move it */
5089 if (wp != NULL)
5090 win_move_after(wp, curwin);
5091 }
5092
5093 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005095#ifdef FEAT_AUTOCMD
5096 bufref_T bufref;
5097
5098 set_bufref(&bufref, buf);
5099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 /* Split the window and put the buffer in it */
5101 p_ea_save = p_ea;
5102 p_ea = TRUE; /* use space from all windows */
5103 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5104 ++open_wins;
5105 p_ea = p_ea_save;
5106 if (split_ret == FAIL)
5107 continue;
5108
5109 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005110#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 swap_exists_action = SEA_DIALOG;
5112#endif
5113 set_curbuf(buf, DOBUF_GOTO);
5114#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005115 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005117 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005118#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005120# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 break;
5122 }
5123#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005124#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (swap_exists_action == SEA_QUIT)
5126 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005127# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5128 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005129
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005130 /* Reset the error/interrupt/exception state here so that
5131 * aborting() returns FALSE when closing a window. */
5132 enter_cleanup(&cs);
5133# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005134
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005135 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 win_close(curwin, TRUE);
5137 --open_wins;
5138 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005139 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005140
5141# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5142 /* Restore the error/interrupt/exception state if not
5143 * discarded by a new aborting error, interrupt, or uncaught
5144 * exception. */
5145 leave_cleanup(&cs);
5146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 }
5148 else
5149 handle_swap_exists(NULL);
5150#endif
5151 }
5152
5153 ui_breakcheck();
5154 if (got_int)
5155 {
5156 (void)vgetc(); /* only break the file loading, not the rest */
5157 break;
5158 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005159#ifdef FEAT_EVAL
5160 /* Autocommands deleted the buffer or aborted script processing!!! */
5161 if (aborting())
5162 break;
5163#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005164#ifdef FEAT_WINDOWS
5165 /* When ":tab" was used open a new tab for a new window repeatedly. */
5166 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5167 cmdmod.tab = 9999;
5168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170#ifdef FEAT_AUTOCMD
5171 --autocmd_no_enter;
5172#endif
5173 win_enter(firstwin, FALSE); /* back to first window */
5174#ifdef FEAT_AUTOCMD
5175 --autocmd_no_leave;
5176#endif
5177
5178 /*
5179 * Close superfluous windows.
5180 */
5181 for (wp = lastwin; open_wins > count; )
5182 {
5183 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5184 || autowrite(wp->w_buffer, FALSE) == OK);
5185#ifdef FEAT_AUTOCMD
5186 if (!win_valid(wp))
5187 {
5188 /* BufWrite Autocommands made the window invalid, start over */
5189 wp = lastwin;
5190 }
5191 else
5192#endif
5193 if (r)
5194 {
5195 win_close(wp, !P_HID(wp->w_buffer));
5196 --open_wins;
5197 wp = lastwin;
5198 }
5199 else
5200 {
5201 wp = wp->w_prev;
5202 if (wp == NULL)
5203 break;
5204 }
5205 }
5206}
5207# endif /* FEAT_LISTCMDS */
5208
5209#endif /* FEAT_WINDOWS */
5210
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005211static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213/*
5214 * do_modelines() - process mode lines for the current file
5215 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005216 * "flags" can be:
5217 * OPT_WINONLY only set options local to window
5218 * OPT_NOWIN don't set options local to window
5219 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 * Returns immediately if the "ml" option isn't set.
5221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005223do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005225 linenr_T lnum;
5226 int nmlines;
5227 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
5229 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5230 return;
5231
5232 /* Disallow recursive entry here. Can happen when executing a modeline
5233 * triggers an autocommand, which reloads modelines with a ":do". */
5234 if (entered)
5235 return;
5236
5237 ++entered;
5238 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5239 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005240 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 nmlines = 0;
5242
5243 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5244 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005245 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 nmlines = 0;
5247 --entered;
5248}
5249
5250#include "version.h" /* for version number */
5251
5252/*
5253 * chk_modeline() - check a single line for a mode string
5254 * Return FAIL if an error encountered.
5255 */
5256 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005257chk_modeline(
5258 linenr_T lnum,
5259 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260{
5261 char_u *s;
5262 char_u *e;
5263 char_u *linecopy; /* local copy of any modeline found */
5264 int prev;
5265 int vers;
5266 int end;
5267 int retval = OK;
5268 char_u *save_sourcing_name;
5269 linenr_T save_sourcing_lnum;
5270#ifdef FEAT_EVAL
5271 scid_T save_SID;
5272#endif
5273
5274 prev = -1;
5275 for (s = ml_get(lnum); *s != NUL; ++s)
5276 {
5277 if (prev == -1 || vim_isspace(prev))
5278 {
5279 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5280 || STRNCMP(s, "vi:", (size_t)3) == 0)
5281 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005282 /* Accept both "vim" and "Vim". */
5283 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
5285 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5286 e = s + 4;
5287 else
5288 e = s + 3;
5289 vers = getdigits(&e);
5290 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005291 && (s[0] != 'V'
5292 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 && (s[3] == ':'
5294 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5295 || (VIM_VERSION_100 < vers && s[3] == '<')
5296 || (VIM_VERSION_100 > vers && s[3] == '>')
5297 || (VIM_VERSION_100 == vers && s[3] == '=')))
5298 break;
5299 }
5300 }
5301 prev = *s;
5302 }
5303
5304 if (*s)
5305 {
5306 do /* skip over "ex:", "vi:" or "vim:" */
5307 ++s;
5308 while (s[-1] != ':');
5309
5310 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5311 if (linecopy == NULL)
5312 return FAIL;
5313
5314 save_sourcing_lnum = sourcing_lnum;
5315 save_sourcing_name = sourcing_name;
5316 sourcing_lnum = lnum; /* prepare for emsg() */
5317 sourcing_name = (char_u *)"modelines";
5318
5319 end = FALSE;
5320 while (end == FALSE)
5321 {
5322 s = skipwhite(s);
5323 if (*s == NUL)
5324 break;
5325
5326 /*
5327 * Find end of set command: ':' or end of line.
5328 * Skip over "\:", replacing it with ":".
5329 */
5330 for (e = s; *e != ':' && *e != NUL; ++e)
5331 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005332 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (*e == NUL)
5334 end = TRUE;
5335
5336 /*
5337 * If there is a "set" command, require a terminating ':' and
5338 * ignore the stuff after the ':'.
5339 * "vi:set opt opt opt: foo" -- foo not interpreted
5340 * "vi:opt opt opt: foo" -- foo interpreted
5341 * Accept "se" for compatibility with Elvis.
5342 */
5343 if (STRNCMP(s, "set ", (size_t)4) == 0
5344 || STRNCMP(s, "se ", (size_t)3) == 0)
5345 {
5346 if (*e != ':') /* no terminating ':'? */
5347 break;
5348 end = TRUE;
5349 s = vim_strchr(s, ' ') + 1;
5350 }
5351 *e = NUL; /* truncate the set command */
5352
5353 if (*s != NUL) /* skip over an empty "::" */
5354 {
5355#ifdef FEAT_EVAL
5356 save_SID = current_SID;
5357 current_SID = SID_MODELINE;
5358#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005359 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360#ifdef FEAT_EVAL
5361 current_SID = save_SID;
5362#endif
5363 if (retval == FAIL) /* stop if error found */
5364 break;
5365 }
5366 s = e + 1; /* advance to next part */
5367 }
5368
5369 sourcing_lnum = save_sourcing_lnum;
5370 sourcing_name = save_sourcing_name;
5371
5372 vim_free(linecopy);
5373 }
5374 return retval;
5375}
5376
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005377#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005379read_viminfo_bufferlist(
5380 vir_T *virp,
5381 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383 char_u *tab;
5384 linenr_T lnum;
5385 colnr_T col;
5386 buf_T *buf;
5387 char_u *sfname;
5388 char_u *xline;
5389
5390 /* Handle long line and escaped characters. */
5391 xline = viminfo_readstring(virp, 1, FALSE);
5392
5393 /* don't read in if there are files on the command-line or if writing: */
5394 if (xline != NULL && !writing && ARGCOUNT == 0
5395 && find_viminfo_parameter('%') != NULL)
5396 {
5397 /* Format is: <fname> Tab <lnum> Tab <col>.
5398 * Watch out for a Tab in the file name, work from the end. */
5399 lnum = 0;
5400 col = 0;
5401 tab = vim_strrchr(xline, '\t');
5402 if (tab != NULL)
5403 {
5404 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005405 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 tab = vim_strrchr(xline, '\t');
5407 if (tab != NULL)
5408 {
5409 *tab++ = '\0';
5410 lnum = atol((char *)tab);
5411 }
5412 }
5413
5414 /* Expand "~/" in the file name at "line + 1" to a full path.
5415 * Then try shortening it by comparing with the current directory */
5416 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005417 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
5419 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5420 if (buf != NULL) /* just in case... */
5421 {
5422 buf->b_last_cursor.lnum = lnum;
5423 buf->b_last_cursor.col = col;
5424 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5425 }
5426 }
5427 vim_free(xline);
5428
5429 return viminfo_readline(virp);
5430}
5431
5432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005433write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434{
5435 buf_T *buf;
5436#ifdef FEAT_WINDOWS
5437 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005438 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#endif
5440 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005441 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442
5443 if (find_viminfo_parameter('%') == NULL)
5444 return;
5445
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005446 /* Without a number -1 is returned: do all buffers. */
5447 max_buffers = get_viminfo_parameter('%');
5448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005450#define LINE_BUF_LEN (MAXPATHL + 40)
5451 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 if (line == NULL)
5453 return;
5454
5455#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005456 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 set_last_cursor(win);
5458#else
5459 set_last_cursor(curwin);
5460#endif
5461
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005462 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5464 {
5465 if (buf->b_fname == NULL
5466 || !buf->b_p_bl
5467#ifdef FEAT_QUICKFIX
5468 || bt_quickfix(buf)
5469#endif
5470 || removable(buf->b_ffname))
5471 continue;
5472
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005473 if (max_buffers-- == 0)
5474 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 putc('%', fp);
5476 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005477 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 (long)buf->b_last_cursor.lnum,
5479 buf->b_last_cursor.col);
5480 viminfo_writestring(fp, line);
5481 }
5482 vim_free(line);
5483}
5484#endif
5485
5486
5487/*
5488 * Return special buffer name.
5489 * Returns NULL when the buffer has a normal file name.
5490 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005491 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005492buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5495 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005496 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005497 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005498 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005499
5500 /*
5501 * For location list window, w_llist_ref points to the location list.
5502 * For quickfix window, w_llist_ref is NULL.
5503 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005504 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005505 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005506 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005507 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509#endif
5510#ifdef FEAT_QUICKFIX
5511 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5512 * contains the name as specified by the user */
5513 if (bt_nofile(buf))
5514 {
5515 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005516 return buf->b_sfname;
5517 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
5519#endif
5520 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005521 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 return NULL;
5523}
5524
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005525#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5526 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5527 || defined(PROTO)
5528/*
5529 * Find a window for buffer "buf".
5530 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5531 * If not found FAIL is returned.
5532 */
5533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005534find_win_for_buf(
5535 buf_T *buf,
5536 win_T **wp,
5537 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005538{
5539 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5540 if ((*wp)->w_buffer == buf)
5541 goto win_found;
5542 return FAIL;
5543win_found:
5544 return OK;
5545}
5546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548#if defined(FEAT_SIGNS) || defined(PROTO)
5549/*
5550 * Insert the sign into the signlist.
5551 */
5552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005553insert_sign(
5554 buf_T *buf, /* buffer to store sign in */
5555 signlist_T *prev, /* previous sign entry */
5556 signlist_T *next, /* next sign entry */
5557 int id, /* sign ID */
5558 linenr_T lnum, /* line number which gets the mark */
5559 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 signlist_T *newsign;
5562
5563 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5564 if (newsign != NULL)
5565 {
5566 newsign->id = id;
5567 newsign->lnum = lnum;
5568 newsign->typenr = typenr;
5569 newsign->next = next;
5570#ifdef FEAT_NETBEANS_INTG
5571 newsign->prev = prev;
5572 if (next != NULL)
5573 next->prev = newsign;
5574#endif
5575
5576 if (prev == NULL)
5577 {
5578 /* When adding first sign need to redraw the windows to create the
5579 * column for signs. */
5580 if (buf->b_signlist == NULL)
5581 {
5582 redraw_buf_later(buf, NOT_VALID);
5583 changed_cline_bef_curs();
5584 }
5585
5586 /* first sign in signlist */
5587 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005588#ifdef FEAT_NETBEANS_INTG
5589 if (netbeans_active())
5590 buf->b_has_sign_column = TRUE;
5591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
5593 else
5594 prev->next = newsign;
5595 }
5596}
5597
5598/*
5599 * Add the sign into the signlist. Find the right spot to do it though.
5600 */
5601 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005602buf_addsign(
5603 buf_T *buf, /* buffer to store sign in */
5604 int id, /* sign ID */
5605 linenr_T lnum, /* line number which gets the mark */
5606 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 signlist_T *sign; /* a sign in the signlist */
5609 signlist_T *prev; /* the previous sign */
5610
5611 prev = NULL;
5612 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5613 {
5614 if (lnum == sign->lnum && id == sign->id)
5615 {
5616 sign->typenr = typenr;
5617 return;
5618 }
5619 else if (
5620#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5621 id < 0 &&
5622#endif
5623 lnum < sign->lnum)
5624 {
5625#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5626 /* XXX - GRP: Is this because of sign slide problem? Or is it
5627 * really needed? Or is it because we allow multiple signs per
5628 * line? If so, should I add that feature to FEAT_SIGNS?
5629 */
5630 while (prev != NULL && prev->lnum == lnum)
5631 prev = prev->prev;
5632 if (prev == NULL)
5633 sign = buf->b_signlist;
5634 else
5635 sign = prev->next;
5636#endif
5637 insert_sign(buf, prev, sign, id, lnum, typenr);
5638 return;
5639 }
5640 prev = sign;
5641 }
5642#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5643 /* XXX - GRP: See previous comment */
5644 while (prev != NULL && prev->lnum == lnum)
5645 prev = prev->prev;
5646 if (prev == NULL)
5647 sign = buf->b_signlist;
5648 else
5649 sign = prev->next;
5650#endif
5651 insert_sign(buf, prev, sign, id, lnum, typenr);
5652
5653 return;
5654}
5655
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005656/*
5657 * For an existing, placed sign "markId" change the type to "typenr".
5658 * Returns the line number of the sign, or zero if the sign is not found.
5659 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005660 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005661buf_change_sign_type(
5662 buf_T *buf, /* buffer to store sign in */
5663 int markId, /* sign ID */
5664 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 signlist_T *sign; /* a sign in the signlist */
5667
5668 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5669 {
5670 if (sign->id == markId)
5671 {
5672 sign->typenr = typenr;
5673 return sign->lnum;
5674 }
5675 }
5676
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005677 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678}
5679
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005680 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005681buf_getsigntype(
5682 buf_T *buf,
5683 linenr_T lnum,
5684 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
5686 signlist_T *sign; /* a sign in a b_signlist */
5687
5688 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5689 if (sign->lnum == lnum
5690 && (type == SIGN_ANY
5691# ifdef FEAT_SIGN_ICONS
5692 || (type == SIGN_ICON
5693 && sign_get_image(sign->typenr) != NULL)
5694# endif
5695 || (type == SIGN_TEXT
5696 && sign_get_text(sign->typenr) != NULL)
5697 || (type == SIGN_LINEHL
5698 && sign_get_attr(sign->typenr, TRUE) != 0)))
5699 return sign->typenr;
5700 return 0;
5701}
5702
5703
5704 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705buf_delsign(
5706 buf_T *buf, /* buffer sign is stored in */
5707 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 signlist_T **lastp; /* pointer to pointer to current sign */
5710 signlist_T *sign; /* a sign in a b_signlist */
5711 signlist_T *next; /* the next sign in a b_signlist */
5712 linenr_T lnum; /* line number whose sign was deleted */
5713
5714 lastp = &buf->b_signlist;
5715 lnum = 0;
5716 for (sign = buf->b_signlist; sign != NULL; sign = next)
5717 {
5718 next = sign->next;
5719 if (sign->id == id)
5720 {
5721 *lastp = next;
5722#ifdef FEAT_NETBEANS_INTG
5723 if (next != NULL)
5724 next->prev = sign->prev;
5725#endif
5726 lnum = sign->lnum;
5727 vim_free(sign);
5728 break;
5729 }
5730 else
5731 lastp = &sign->next;
5732 }
5733
5734 /* When deleted the last sign need to redraw the windows to remove the
5735 * sign column. */
5736 if (buf->b_signlist == NULL)
5737 {
5738 redraw_buf_later(buf, NOT_VALID);
5739 changed_cline_bef_curs();
5740 }
5741
5742 return lnum;
5743}
5744
5745
5746/*
5747 * Find the line number of the sign with the requested id. If the sign does
5748 * not exist, return 0 as the line number. This will still let the correct file
5749 * get loaded.
5750 */
5751 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005752buf_findsign(
5753 buf_T *buf, /* buffer to store sign in */
5754 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 signlist_T *sign; /* a sign in the signlist */
5757
5758 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5759 if (sign->id == id)
5760 return sign->lnum;
5761
5762 return 0;
5763}
5764
5765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005766buf_findsign_id(
5767 buf_T *buf, /* buffer whose sign we are searching for */
5768 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 signlist_T *sign; /* a sign in the signlist */
5771
5772 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5773 if (sign->lnum == lnum)
5774 return sign->id;
5775
5776 return 0;
5777}
5778
5779
5780# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5781/* see if a given type of sign exists on a specific line */
5782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005783buf_findsigntype_id(
5784 buf_T *buf, /* buffer whose sign we are searching for */
5785 linenr_T lnum, /* line number of sign */
5786 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 signlist_T *sign; /* a sign in the signlist */
5789
5790 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5791 if (sign->lnum == lnum && sign->typenr == typenr)
5792 return sign->id;
5793
5794 return 0;
5795}
5796
5797
5798# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5799/* return the number of icons on the given line */
5800 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005801buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
5803 signlist_T *sign; /* a sign in the signlist */
5804 int count = 0;
5805
5806 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5807 if (sign->lnum == lnum)
5808 if (sign_get_image(sign->typenr) != NULL)
5809 count++;
5810
5811 return count;
5812}
5813# endif /* FEAT_SIGN_ICONS */
5814# endif /* FEAT_NETBEANS_INTG */
5815
5816
5817/*
5818 * Delete signs in buffer "buf".
5819 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005821buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822{
5823 signlist_T *next;
5824
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005825 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005826 * sign column. Not when curwin is NULL (this means we're exiting). */
5827 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005828 {
5829 redraw_buf_later(buf, NOT_VALID);
5830 changed_cline_bef_curs();
5831 }
5832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 while (buf->b_signlist != NULL)
5834 {
5835 next = buf->b_signlist->next;
5836 vim_free(buf->b_signlist);
5837 buf->b_signlist = next;
5838 }
5839}
5840
5841/*
5842 * Delete all signs in all buffers.
5843 */
5844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005845buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846{
5847 buf_T *buf; /* buffer we are checking for signs */
5848
5849 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5850 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852}
5853
5854/*
5855 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5856 */
5857 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005858sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 buf_T *buf;
5861 signlist_T *p;
5862 char lbuf[BUFSIZ];
5863
5864 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5865 msg_putchar('\n');
5866 if (rbuf == NULL)
5867 buf = firstbuf;
5868 else
5869 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005870 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 {
5872 if (buf->b_signlist != NULL)
5873 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005874 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5876 msg_putchar('\n');
5877 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005878 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005880 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5882 MSG_PUTS(lbuf);
5883 msg_putchar('\n');
5884 }
5885 if (rbuf != NULL)
5886 break;
5887 buf = buf->b_next;
5888 }
5889}
5890
5891/*
5892 * Adjust a placed sign for inserted/deleted lines.
5893 */
5894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005895sign_mark_adjust(
5896 linenr_T line1,
5897 linenr_T line2,
5898 long amount,
5899 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 signlist_T *sign; /* a sign in a b_signlist */
5902
5903 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5904 {
5905 if (sign->lnum >= line1 && sign->lnum <= line2)
5906 {
5907 if (amount == MAXLNUM)
5908 sign->lnum = line1;
5909 else
5910 sign->lnum += amount;
5911 }
5912 else if (sign->lnum > line2)
5913 sign->lnum += amount_after;
5914 }
5915}
5916#endif /* FEAT_SIGNS */
5917
5918/*
5919 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5920 */
5921 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005922set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 if (on != curbuf->b_p_bl)
5925 {
5926 curbuf->b_p_bl = on;
5927#ifdef FEAT_AUTOCMD
5928 if (on)
5929 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5930 else
5931 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5932#endif
5933 }
5934}
5935
5936/*
5937 * Read the file for "buf" again and check if the contents changed.
5938 * Return TRUE if it changed or this could not be checked.
5939 */
5940 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005941buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
5943 buf_T *newbuf;
5944 int differ = TRUE;
5945 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 exarg_T ea;
5948
5949 /* Allocate a buffer without putting it in the buffer list. */
5950 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5951 if (newbuf == NULL)
5952 return TRUE;
5953
5954 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5955 if (prep_exarg(&ea, buf) == FAIL)
5956 {
5957 wipe_buffer(newbuf, FALSE);
5958 return TRUE;
5959 }
5960
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 /* set curwin/curbuf to buf and save a few things */
5962 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963
Bram Moolenaar4770d092006-01-12 23:22:24 +00005964 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 && readfile(buf->b_ffname, buf->b_fname,
5966 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5967 &ea, READ_NEW | READ_DUMMY) == OK)
5968 {
5969 /* compare the two files line by line */
5970 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5971 {
5972 differ = FALSE;
5973 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5974 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5975 {
5976 differ = TRUE;
5977 break;
5978 }
5979 }
5980 }
5981 vim_free(ea.cmd);
5982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 /* restore curwin/curbuf and a few other things */
5984 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
5986 if (curbuf != newbuf) /* safety check */
5987 wipe_buffer(newbuf, FALSE);
5988
5989 return differ;
5990}
5991
5992/*
5993 * Wipe out a buffer and decrement the last buffer number if it was used for
5994 * this buffer. Call this to wipe out a temp buffer that does not contain any
5995 * marks.
5996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998wipe_buffer(
5999 buf_T *buf,
6000 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002 if (buf->b_fnum == top_file_num - 1)
6003 --top_file_num;
6004
6005#ifdef FEAT_AUTOCMD
6006 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006007 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006009 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010#ifdef FEAT_AUTOCMD
6011 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006012 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013#endif
6014}