blob: 3507f94ca855de7e056ae48498824c58a8ea88e3 [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
86 buf_T *old_curbuf;
87#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. */
132 old_curbuf = curbuf;
133 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 */
287 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
288 {
289 aco_save_T aco;
290
291 /* Go to the buffer that was opened. */
292 aucmd_prepbuf(&aco, old_curbuf);
293#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/*
353 * Close the link to a buffer.
354 * "action" is used when there is no longer a window for the buffer.
355 * It can be:
356 * 0 buffer becomes hidden
357 * DOBUF_UNLOAD buffer is unloaded
358 * DOBUF_DELETE buffer is unloaded and removed from buffer list
359 * DOBUF_WIPE buffer is unloaded and really deleted
360 * When doing all but the first one on the current buffer, the caller should
361 * get a new buffer very soon!
362 *
363 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100364 *
365 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
366 * cause there to be only one window with this buffer. e.g. when ":quit" is
367 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 */
369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100370close_buffer(
371 win_T *win, /* if not NULL, set b_last_cursor */
372 buf_T *buf,
373 int action,
374 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375{
376#ifdef FEAT_AUTOCMD
377 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100378 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200379 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#endif
381 int unload_buf = (action != 0);
382 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
383 int wipe_buf = (action == DOBUF_WIPE);
384
385#ifdef FEAT_QUICKFIX
386 /*
387 * Force unloading or deleting when 'bufhidden' says so.
388 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
389 * "hide" (otherwise we could never free or delete a buffer).
390 */
391 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
392 {
393 del_buf = TRUE;
394 unload_buf = TRUE;
395 }
396 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
397 {
398 del_buf = TRUE;
399 unload_buf = TRUE;
400 wipe_buf = TRUE;
401 }
402 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
403 unload_buf = TRUE;
404#endif
405
Bram Moolenaar3be85852014-06-12 14:01:31 +0200406 if (win != NULL
407#ifdef FEAT_WINDOWS
408 && win_valid(win) /* in case autocommands closed the window */
409#endif
410 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 {
412 /* Set b_last_cursor when closing the last window for the buffer.
413 * Remember the last cursor position and window options of the buffer.
414 * This used to be only for the current window, but then options like
415 * 'foldmethod' may be lost with a ":only" command. */
416 if (buf->b_nwindows == 1)
417 set_last_cursor(win);
418 buflist_setfpos(buf, win,
419 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
420 win->w_cursor.col, TRUE);
421 }
422
423#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200424 set_bufref(&bufref, buf);
425
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 /* When the buffer is no longer in a window, trigger BufWinLeave */
427 if (buf->b_nwindows == 1)
428 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200429 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200430 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
431 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200432 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100433 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200434 /* Autocommands deleted the buffer. */
435aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100436 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100438 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200439 buf->b_closing = FALSE;
440 if (abort_if_last && one_window())
441 /* Autocommands made this the only window. */
442 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
444 /* When the buffer becomes hidden, but is not unloaded, trigger
445 * BufHidden */
446 if (!unload_buf)
447 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200448 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200449 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
450 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200451 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200452 /* Autocommands deleted the buffer. */
453 goto aucmd_abort;
454 buf->b_closing = FALSE;
455 if (abort_if_last && one_window())
456 /* Autocommands made this the only window. */
457 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 }
459# ifdef FEAT_EVAL
460 if (aborting()) /* autocmds may abort script processing */
461 return;
462# endif
463 }
464 nwindows = buf->b_nwindows;
465#endif
466
467 /* decrease the link count from windows (unless not in any window) */
468 if (buf->b_nwindows > 0)
469 --buf->b_nwindows;
470
471 /* Return when a window is displaying the buffer or when it's not
472 * unloaded. */
473 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475
476 /* Always remove the buffer when there is no file name. */
477 if (buf->b_ffname == NULL)
478 del_buf = TRUE;
479
480 /*
481 * Free all things allocated for this buffer.
482 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
483 */
484#ifdef FEAT_AUTOCMD
485 /* Remember if we are closing the current buffer. Restore the number of
486 * windows, so that autocommands in buf_freeall() don't get confused. */
487 is_curbuf = (buf == curbuf);
488 buf->b_nwindows = nwindows;
489#endif
490
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200491 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493#ifdef FEAT_AUTOCMD
494 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200495 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 return;
497# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000498 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 return;
500# endif
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 /*
503 * It's possible that autocommands change curbuf to the one being deleted.
504 * This might cause the previous curbuf to be deleted unexpectedly. But
505 * in some cases it's OK to delete the curbuf, because a new one is
506 * obtained anyway. Therefore only return if curbuf changed to the
507 * deleted buffer.
508 */
509 if (buf == curbuf && !is_curbuf)
510 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200511
512 if (
513#ifdef FEAT_WINDOWS
514 win_valid(win) &&
515#else
516 win != NULL &&
517#endif
518 win->w_buffer == buf)
519 win->w_buffer = NULL; /* make sure we don't use the buffer now */
520
521 /* Autocommands may have opened or closed windows for this buffer.
522 * Decrement the count for the close we do here. */
523 if (buf->b_nwindows > 0)
524 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#endif
526
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000527 /* Change directories when the 'acd' option is set. */
528 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
530 /*
531 * Remove the buffer from the list.
532 */
533 if (wipe_buf)
534 {
535#ifdef FEAT_SUN_WORKSHOP
536 if (usingSunWorkShop)
537 workshop_file_closed_lineno((char *)buf->b_ffname,
538 (int)buf->b_last_cursor.lnum);
539#endif
540 vim_free(buf->b_ffname);
541 vim_free(buf->b_sfname);
542 if (buf->b_prev == NULL)
543 firstbuf = buf->b_next;
544 else
545 buf->b_prev->b_next = buf->b_next;
546 if (buf->b_next == NULL)
547 lastbuf = buf->b_prev;
548 else
549 buf->b_next->b_prev = buf->b_prev;
550 free_buffer(buf);
551 }
552 else
553 {
554 if (del_buf)
555 {
556 /* Free all internal variables and reset option values, to make
557 * ":bdel" compatible with Vim 5.7. */
558 free_buffer_stuff(buf, TRUE);
559
560 /* Make it look like a new buffer. */
561 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
562
563 /* Init the options when loaded again. */
564 buf->b_p_initialized = FALSE;
565 }
566 buf_clear_file(buf);
567 if (del_buf)
568 buf->b_p_bl = FALSE;
569 }
570}
571
572/*
573 * Make buffer not contain a file.
574 */
575 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100576buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577{
578 buf->b_ml.ml_line_count = 1;
579 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 buf->b_p_eol = TRUE;
582 buf->b_start_eol = TRUE;
583#ifdef FEAT_MBYTE
584 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000585 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586#endif
587 buf->b_ml.ml_mfp = NULL;
588 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
589#ifdef FEAT_NETBEANS_INTG
590 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#endif
592}
593
594/*
595 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200596 * the file. flags:
597 * BFA_DEL buffer is going to be deleted
598 * BFA_WIPE buffer is going to be wiped out
599 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100602buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603{
604#ifdef FEAT_AUTOCMD
605 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200606 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar362ce482012-06-06 19:02:45 +0200608 buf->b_closing = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200609 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200610 if (buf->b_ml.ml_mfp != NULL)
611 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200612 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
613 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200614 && !bufref_valid(&bufref))
615 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200616 return;
617 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200618 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200620 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
621 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200622 && !bufref_valid(&bufref))
623 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 return;
625 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200626 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200628 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
629 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200630 && !bufref_valid(&bufref))
631 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 return;
633 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200634 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000636 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 return;
638# endif
639
640 /*
641 * It's possible that autocommands change curbuf to the one being deleted.
642 * This might cause curbuf to be deleted unexpectedly. But in some cases
643 * it's OK to delete the curbuf, because a new one is obtained anyway.
644 * Therefore only return if curbuf changed to the deleted buffer.
645 */
646 if (buf == curbuf && !is_curbuf)
647 return;
648#endif
649#ifdef FEAT_DIFF
650 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
651#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200652#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100653 /* Remove any ownsyntax, unless exiting. */
654 if (firstwin != NULL && curwin->w_buffer == buf)
655 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200656#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000657
658#ifdef FEAT_FOLDING
659 /* No folds in an empty buffer. */
660# ifdef FEAT_WINDOWS
661 {
662 win_T *win;
663 tabpage_T *tp;
664
665 FOR_ALL_TAB_WINDOWS(tp, win)
666 if (win->w_buffer == buf)
667 clearFolding(win);
668 }
669# else
670 if (curwin->w_buffer == buf)
671 clearFolding(curwin);
672# endif
673#endif
674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_TCL
676 tcl_buffer_free(buf);
677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 ml_close(buf, TRUE); /* close and delete the memline/memfile */
679 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200680 if ((flags & BFA_KEEP_UNDO) == 0)
681 {
682 u_blockfree(buf); /* free the memory allocated for undo */
683 u_clearall(buf); /* reset all undo information */
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200686 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000688 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689}
690
691/*
692 * Free a buffer structure and the things it contains related to the buffer
693 * itself (not the file, that must have been done already).
694 */
695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100696free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200698 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200700#ifdef FEAT_EVAL
701 unref_var_dict(buf->b_vars);
702#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200703#ifdef FEAT_LUA
704 lua_buffer_free(buf);
705#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000706#ifdef FEAT_MZSCHEME
707 mzscheme_buffer_free(buf);
708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#ifdef FEAT_PERL
710 perl_buf_free(buf);
711#endif
712#ifdef FEAT_PYTHON
713 python_buffer_free(buf);
714#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200715#ifdef FEAT_PYTHON3
716 python3_buffer_free(buf);
717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#ifdef FEAT_RUBY
719 ruby_buffer_free(buf);
720#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200721#ifdef FEAT_JOB_CHANNEL
722 channel_buffer_free(buf);
723#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000724#ifdef FEAT_AUTOCMD
725 aubuflocal_remove(buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200726 if (autocmd_busy)
727 {
728 /* Do not free the buffer structure while autocommands are executing,
729 * it's still needed. Free it when autocmd_busy is reset. */
730 buf->b_next = au_pending_free_buf;
731 au_pending_free_buf = buf;
732 }
733 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000734#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200735 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736}
737
738/*
739 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
740 */
741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100742free_buffer_stuff(
743 buf_T *buf,
744 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746 if (free_options)
747 {
748 clear_wininfo(buf); /* including window-local options */
749 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200750#ifdef FEAT_SPELL
751 ga_clear(&buf->b_s.b_langp);
752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200755 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
756 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#endif
758#ifdef FEAT_USR_CMDS
759 uc_clear(&buf->b_ucmds); /* clear local user commands */
760#endif
761#ifdef FEAT_SIGNS
762 buf_delete_signs(buf); /* delete any signs */
763#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000764#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200765 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767#ifdef FEAT_LOCALMAP
768 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
769 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
770#endif
771#ifdef FEAT_MBYTE
772 vim_free(buf->b_start_fenc);
773 buf->b_start_fenc = NULL;
774#endif
775}
776
777/*
778 * Free the b_wininfo list for buffer "buf".
779 */
780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100781clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 wininfo_T *wip;
784
785 while (buf->b_wininfo != NULL)
786 {
787 wip = buf->b_wininfo;
788 buf->b_wininfo = wip->wi_next;
789 if (wip->wi_optset)
790 {
791 clear_winopt(&wip->wi_opt);
792#ifdef FEAT_FOLDING
793 deleteFoldRecurse(&wip->wi_folds);
794#endif
795 }
796 vim_free(wip);
797 }
798}
799
800#if defined(FEAT_LISTCMDS) || defined(PROTO)
801/*
802 * Go to another buffer. Handles the result of the ATTENTION dialog.
803 */
804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100805goto_buffer(
806 exarg_T *eap,
807 int start,
808 int dir,
809 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000811# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 buf_T *old_curbuf = curbuf;
813
814 swap_exists_action = SEA_DIALOG;
815# endif
816 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
817 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000818# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
820 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000821# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
822 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000823
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000824 /* Reset the error/interrupt/exception state here so that
825 * aborting() returns FALSE when closing a window. */
826 enter_cleanup(&cs);
827# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000828
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000829 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 win_close(curwin, TRUE);
831 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000832 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000833
834# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
835 /* Restore the error/interrupt/exception state if not discarded by a
836 * new aborting error, interrupt, or uncaught exception. */
837 leave_cleanup(&cs);
838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 else
841 handle_swap_exists(old_curbuf);
842# endif
843}
844#endif
845
Bram Moolenaare64ac772005-12-07 20:54:59 +0000846#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847/*
848 * Handle the situation of swap_exists_action being set.
849 * It is allowed for "old_curbuf" to be NULL or invalid.
850 */
851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100852handle_swap_exists(buf_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000854# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
855 cleanup_T cs;
856# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100857#ifdef FEAT_SYN_HL
858 long old_tw = curbuf->b_p_tw;
859#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000860
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (swap_exists_action == SEA_QUIT)
862 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000863# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
864 /* Reset the error/interrupt/exception state here so that
865 * aborting() returns FALSE when closing a buffer. */
866 enter_cleanup(&cs);
867# endif
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 /* User selected Quit at ATTENTION prompt. Go back to previous
870 * buffer. If that buffer is gone or the same as the current one,
871 * open a new, empty buffer. */
872 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000873 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100874 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000876 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100880#ifdef FEAT_SYN_HL
881 if (old_tw != curbuf->b_p_tw)
882 check_colorcolumn(curwin);
883#endif
884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000886
887# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
888 /* Restore the error/interrupt/exception state if not discarded by a
889 * new aborting error, interrupt, or uncaught exception. */
890 leave_cleanup(&cs);
891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 }
893 else if (swap_exists_action == SEA_RECOVER)
894 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000895# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
896 /* Reset the error/interrupt/exception state here so that
897 * aborting() returns FALSE when closing a buffer. */
898 enter_cleanup(&cs);
899# endif
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 /* User selected Recover at ATTENTION prompt. */
902 msg_scroll = TRUE;
903 ml_recover();
904 MSG_PUTS("\n"); /* don't overwrite the last message */
905 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000906 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000907
908# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
909 /* Restore the error/interrupt/exception state if not discarded by a
910 * new aborting error, interrupt, or uncaught exception. */
911 leave_cleanup(&cs);
912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
914 swap_exists_action = SEA_NONE;
915}
916#endif
917
918#if defined(FEAT_LISTCMDS) || defined(PROTO)
919/*
920 * do_bufdel() - delete or unload buffer(s)
921 *
922 * addr_count == 0: ":bdel" - delete current buffer
923 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
924 * buffer "end_bnr", then any other arguments.
925 * addr_count == 2: ":N,N bdel" - delete buffers in range
926 *
927 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
928 * DOBUF_DEL (":bdel")
929 *
930 * Returns error message or NULL
931 */
932 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100933do_bufdel(
934 int command,
935 char_u *arg, /* pointer to extra arguments */
936 int addr_count,
937 int start_bnr, /* first buffer number in a range */
938 int end_bnr, /* buffer nr or last buffer nr in a range */
939 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
941 int do_current = 0; /* delete current buffer? */
942 int deleted = 0; /* number of buffers deleted */
943 char_u *errormsg = NULL; /* return value */
944 int bnr; /* buffer number */
945 char_u *p;
946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (addr_count == 0)
948 {
949 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
950 }
951 else
952 {
953 if (addr_count == 2)
954 {
955 if (*arg) /* both range and argument is not allowed */
956 return (char_u *)_(e_trailing);
957 bnr = start_bnr;
958 }
959 else /* addr_count == 1 */
960 bnr = end_bnr;
961
962 for ( ;!got_int; ui_breakcheck())
963 {
964 /*
965 * delete the current buffer last, otherwise when the
966 * current buffer is deleted, the next buffer becomes
967 * the current one and will be loaded, which may then
968 * also be deleted, etc.
969 */
970 if (bnr == curbuf->b_fnum)
971 do_current = bnr;
972 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
973 forceit) == OK)
974 ++deleted;
975
976 /*
977 * find next buffer number to delete/unload
978 */
979 if (addr_count == 2)
980 {
981 if (++bnr > end_bnr)
982 break;
983 }
984 else /* addr_count == 1 */
985 {
986 arg = skipwhite(arg);
987 if (*arg == NUL)
988 break;
989 if (!VIM_ISDIGIT(*arg))
990 {
991 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100992 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
993 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (bnr < 0) /* failed */
995 break;
996 arg = p;
997 }
998 else
999 bnr = getdigits(&arg);
1000 }
1001 }
1002 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1003 FORWARD, do_current, forceit) == OK)
1004 ++deleted;
1005
1006 if (deleted == 0)
1007 {
1008 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001009 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001011 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001013 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 errormsg = IObuff;
1015 }
1016 else if (deleted >= p_report)
1017 {
1018 if (command == DOBUF_UNLOAD)
1019 {
1020 if (deleted == 1)
1021 MSG(_("1 buffer unloaded"));
1022 else
1023 smsg((char_u *)_("%d buffers unloaded"), deleted);
1024 }
1025 else if (command == DOBUF_DEL)
1026 {
1027 if (deleted == 1)
1028 MSG(_("1 buffer deleted"));
1029 else
1030 smsg((char_u *)_("%d buffers deleted"), deleted);
1031 }
1032 else
1033 {
1034 if (deleted == 1)
1035 MSG(_("1 buffer wiped out"));
1036 else
1037 smsg((char_u *)_("%d buffers wiped out"), deleted);
1038 }
1039 }
1040 }
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 return errormsg;
1044}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001045#endif /* FEAT_LISTCMDS */
1046
1047#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1048 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001050static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001051
1052/*
1053 * Make the current buffer empty.
1054 * Used when it is wiped out and it's the last buffer.
1055 */
1056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001057empty_curbuf(
1058 int close_others,
1059 int forceit,
1060 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001061{
1062 int retval;
1063 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001064 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001065
1066 if (action == DOBUF_UNLOAD)
1067 {
1068 EMSG(_("E90: Cannot unload last buffer"));
1069 return FAIL;
1070 }
1071
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001072 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001073#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001074 if (close_others)
1075 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001076 close_windows(buf, TRUE);
1077#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001078
1079 setpcmark();
1080 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1081 forceit ? ECMD_FORCEIT : 0, curwin);
1082
1083 /*
1084 * do_ecmd() may create a new buffer, then we have to delete
1085 * the old one. But do_ecmd() may have done that already, check
1086 * if the buffer still exists.
1087 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001088 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001089 close_buffer(NULL, buf, action, FALSE);
1090 if (!close_others)
1091 need_fileinfo = FALSE;
1092 return retval;
1093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094/*
1095 * Implementation of the commands for the buffer list.
1096 *
1097 * action == DOBUF_GOTO go to specified buffer
1098 * action == DOBUF_SPLIT split window and go to specified buffer
1099 * action == DOBUF_UNLOAD unload specified buffer(s)
1100 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1101 * action == DOBUF_WIPE delete specified buffer(s) really
1102 *
1103 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1104 * start == DOBUF_FIRST go to "count" buffer from first buffer
1105 * start == DOBUF_LAST go to "count" buffer from last buffer
1106 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1107 *
1108 * Return FAIL or OK.
1109 */
1110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001111do_buffer(
1112 int action,
1113 int start,
1114 int dir, /* FORWARD or BACKWARD */
1115 int count, /* buffer number or number of buffers */
1116 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 buf_T *buf;
1119 buf_T *bp;
1120 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1121 || action == DOBUF_WIPE);
1122
1123 switch (start)
1124 {
1125 case DOBUF_FIRST: buf = firstbuf; break;
1126 case DOBUF_LAST: buf = lastbuf; break;
1127 default: buf = curbuf; break;
1128 }
1129 if (start == DOBUF_MOD) /* find next modified buffer */
1130 {
1131 while (count-- > 0)
1132 {
1133 do
1134 {
1135 buf = buf->b_next;
1136 if (buf == NULL)
1137 buf = firstbuf;
1138 }
1139 while (buf != curbuf && !bufIsChanged(buf));
1140 }
1141 if (!bufIsChanged(buf))
1142 {
1143 EMSG(_("E84: No modified buffer found"));
1144 return FAIL;
1145 }
1146 }
1147 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1148 {
1149 while (buf != NULL && buf->b_fnum != count)
1150 buf = buf->b_next;
1151 }
1152 else
1153 {
1154 bp = NULL;
1155 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1156 {
1157 /* remember the buffer where we start, we come back there when all
1158 * buffers are unlisted. */
1159 if (bp == NULL)
1160 bp = buf;
1161 if (dir == FORWARD)
1162 {
1163 buf = buf->b_next;
1164 if (buf == NULL)
1165 buf = firstbuf;
1166 }
1167 else
1168 {
1169 buf = buf->b_prev;
1170 if (buf == NULL)
1171 buf = lastbuf;
1172 }
1173 /* don't count unlisted buffers */
1174 if (unload || buf->b_p_bl)
1175 {
1176 --count;
1177 bp = NULL; /* use this buffer as new starting point */
1178 }
1179 if (bp == buf)
1180 {
1181 /* back where we started, didn't find anything. */
1182 EMSG(_("E85: There is no listed buffer"));
1183 return FAIL;
1184 }
1185 }
1186 }
1187
1188 if (buf == NULL) /* could not find it */
1189 {
1190 if (start == DOBUF_FIRST)
1191 {
1192 /* don't warn when deleting */
1193 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001194 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 }
1196 else if (dir == FORWARD)
1197 EMSG(_("E87: Cannot go beyond last buffer"));
1198 else
1199 EMSG(_("E88: Cannot go before first buffer"));
1200 return FAIL;
1201 }
1202
1203#ifdef FEAT_GUI
1204 need_mouse_correct = TRUE;
1205#endif
1206
1207#ifdef FEAT_LISTCMDS
1208 /*
1209 * delete buffer buf from memory and/or the list
1210 */
1211 if (unload)
1212 {
1213 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001214# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1215 bufref_T bufref;
1216
1217 set_bufref(&bufref, buf);
1218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220 /* When unloading or deleting a buffer that's already unloaded and
1221 * unlisted: fail silently. */
1222 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1223 return FAIL;
1224
1225 if (!forceit && bufIsChanged(buf))
1226 {
1227#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1228 if ((p_confirm || cmdmod.confirm) && p_write)
1229 {
1230 dialog_changed(buf, FALSE);
1231# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001232 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 /* Autocommand deleted buffer, oops! It's not changed
1234 * now. */
1235 return FAIL;
1236# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001237 /* If it's still changed fail silently, the dialog already
1238 * mentioned why it fails. */
1239 if (bufIsChanged(buf))
1240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#endif
1244 {
1245 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1246 buf->b_fnum);
1247 return FAIL;
1248 }
1249 }
1250
1251 /*
1252 * If deleting the last (listed) buffer, make it empty.
1253 * The last (listed) buffer cannot be unloaded.
1254 */
1255 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1256 if (bp->b_p_bl && bp != buf)
1257 break;
1258 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001259 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
1261#ifdef FEAT_WINDOWS
1262 /*
1263 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001264 * (unless it's the only window). Repeat this so long as we end up in
1265 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001267 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001268# ifdef FEAT_AUTOCMD
1269 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1270# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001271 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001272 {
1273 if (win_close(curwin, FALSE) == FAIL)
1274 break;
1275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276#endif
1277
1278 /*
1279 * If the buffer to be deleted is not the current one, delete it here.
1280 */
1281 if (buf != curbuf)
1282 {
1283#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001284 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001285 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001287 if (buf->b_nwindows <= 0)
1288 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 return OK;
1290 }
1291
1292 /*
1293 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001294 * There should be another, otherwise it would have been handled
1295 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 * First use au_new_curbuf, if it is valid.
1297 * Then prefer the buffer we most recently visited.
1298 * Else try to find one that is loaded, after the current buffer,
1299 * then before the current buffer.
1300 * Finally use any buffer.
1301 */
1302 buf = NULL; /* selected buffer */
1303 bp = NULL; /* used when no loaded buffer found */
1304#ifdef FEAT_AUTOCMD
1305 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1306 buf = au_new_curbuf;
1307# ifdef FEAT_JUMPLIST
1308 else
1309# endif
1310#endif
1311#ifdef FEAT_JUMPLIST
1312 if (curwin->w_jumplistlen > 0)
1313 {
1314 int jumpidx;
1315
1316 jumpidx = curwin->w_jumplistidx - 1;
1317 if (jumpidx < 0)
1318 jumpidx = curwin->w_jumplistlen - 1;
1319
1320 forward = jumpidx;
1321 while (jumpidx != curwin->w_jumplistidx)
1322 {
1323 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1324 if (buf != NULL)
1325 {
1326 if (buf == curbuf || !buf->b_p_bl)
1327 buf = NULL; /* skip current and unlisted bufs */
1328 else if (buf->b_ml.ml_mfp == NULL)
1329 {
1330 /* skip unloaded buf, but may keep it for later */
1331 if (bp == NULL)
1332 bp = buf;
1333 buf = NULL;
1334 }
1335 }
1336 if (buf != NULL) /* found a valid buffer: stop searching */
1337 break;
1338 /* advance to older entry in jump list */
1339 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1340 break;
1341 if (--jumpidx < 0)
1342 jumpidx = curwin->w_jumplistlen - 1;
1343 if (jumpidx == forward) /* List exhausted for sure */
1344 break;
1345 }
1346 }
1347#endif
1348
1349 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1350 {
1351 forward = TRUE;
1352 buf = curbuf->b_next;
1353 for (;;)
1354 {
1355 if (buf == NULL)
1356 {
1357 if (!forward) /* tried both directions */
1358 break;
1359 buf = curbuf->b_prev;
1360 forward = FALSE;
1361 continue;
1362 }
1363 /* in non-help buffer, try to skip help buffers, and vv */
1364 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1365 {
1366 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1367 break;
1368 if (bp == NULL) /* remember unloaded buf for later */
1369 bp = buf;
1370 }
1371 if (forward)
1372 buf = buf->b_next;
1373 else
1374 buf = buf->b_prev;
1375 }
1376 }
1377 if (buf == NULL) /* No loaded buffer, use unloaded one */
1378 buf = bp;
1379 if (buf == NULL) /* No loaded buffer, find listed one */
1380 {
1381 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1382 if (buf->b_p_bl && buf != curbuf)
1383 break;
1384 }
1385 if (buf == NULL) /* Still no buffer, just take one */
1386 {
1387 if (curbuf->b_next != NULL)
1388 buf = curbuf->b_next;
1389 else
1390 buf = curbuf->b_prev;
1391 }
1392 }
1393
Bram Moolenaara02471e2014-01-10 16:43:14 +01001394 if (buf == NULL)
1395 {
1396 /* Autocommands must have wiped out all other buffers. Only option
1397 * now is to make the current buffer empty. */
1398 return empty_curbuf(FALSE, forceit, action);
1399 }
1400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 /*
1402 * make buf current buffer
1403 */
1404 if (action == DOBUF_SPLIT) /* split window first */
1405 {
1406# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001407 /* If 'switchbuf' contains "useopen": jump to first window containing
1408 * "buf" if one exists */
1409 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001410 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001411 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001412 * page containing "buf" if one exists */
1413 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 return OK;
1415 if (win_split(0, 0) == FAIL)
1416# endif
1417 return FAIL;
1418 }
1419#endif
1420
1421 /* go to current buffer - nothing to do */
1422 if (buf == curbuf)
1423 return OK;
1424
1425 /*
1426 * Check if the current buffer may be abandoned.
1427 */
1428 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1429 {
1430#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1431 if ((p_confirm || cmdmod.confirm) && p_write)
1432 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001433# ifdef FEAT_AUTOCMD
1434 bufref_T bufref;
1435
1436 set_bufref(&bufref, buf);
1437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 dialog_changed(curbuf, FALSE);
1439# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001440 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 /* Autocommand deleted buffer, oops! */
1442 return FAIL;
1443# endif
1444 }
1445 if (bufIsChanged(curbuf))
1446#endif
1447 {
1448 EMSG(_(e_nowrtmsg));
1449 return FAIL;
1450 }
1451 }
1452
1453 /* Go to the other buffer. */
1454 set_curbuf(buf, action);
1455
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001456#if defined(FEAT_LISTCMDS) \
1457 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001459 {
1460 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462#endif
1463
1464#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1465 if (aborting()) /* autocmds may abort script processing */
1466 return FAIL;
1467#endif
1468
1469 return OK;
1470}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
1473/*
1474 * Set current buffer to "buf". Executes autocommands and closes current
1475 * buffer. "action" tells how to close the current buffer:
1476 * DOBUF_GOTO free or hide it
1477 * DOBUF_SPLIT nothing
1478 * DOBUF_UNLOAD unload it
1479 * DOBUF_DEL delete it
1480 * DOBUF_WIPE wipe it out
1481 */
1482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 buf_T *prevbuf;
1486 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1487 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001488#ifdef FEAT_SYN_HL
1489 long old_tw = curbuf->b_p_tw;
1490#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001491 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001494 if (!cmdmod.keepalt)
1495 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001496 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 /* Don't restart Select mode after switching to another buffer. */
1499 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
1501 /* close_windows() or apply_autocmds() may change curbuf */
1502 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001503 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001506 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507# ifdef FEAT_EVAL
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001508 || (bufref_valid(&bufref) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509# else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001510 || bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511# endif
1512#endif
1513 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001514#ifdef FEAT_SYN_HL
1515 if (prevbuf == curwin->w_buffer)
1516 reset_synblock(curwin);
1517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518#ifdef FEAT_WINDOWS
1519 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001520 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521#endif
1522#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001523 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001525 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001527 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001528#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001529 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001530#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001531 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001532 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1534 unload ? action : (action == DOBUF_GOTO
1535 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001536 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001537#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001538 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001539 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001540 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001541#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
1544#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001545 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001546 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001547 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1548 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001549# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001550 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001551# endif
1552# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001553 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001554# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001555 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001559#ifdef FEAT_SYN_HL
1560 if (old_tw != curbuf->b_p_tw)
1561 check_colorcolumn(curwin);
1562#endif
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564}
1565
1566/*
1567 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001568 * Old curbuf must have been abandoned already! This also means "curbuf" may
1569 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 */
1571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
1574 /* Copy buffer and window local option values. Not for a help buffer. */
1575 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1576 if (!buf->b_help)
1577 get_winopts(buf);
1578#ifdef FEAT_FOLDING
1579 else
1580 /* Remove all folds in the window. */
1581 clearFolding(curwin);
1582 foldUpdateAll(curwin); /* update folds (later). */
1583#endif
1584
1585 /* Get the buffer in the current window. */
1586 curwin->w_buffer = buf;
1587 curbuf = buf;
1588 ++curbuf->b_nwindows;
1589
1590#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001591 if (curwin->w_p_diff)
1592 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#endif
1594
Bram Moolenaara971b822011-09-14 14:43:25 +02001595#ifdef FEAT_SYN_HL
1596 curwin->w_s = &(buf->b_s);
1597#endif
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 /* Cursor on first line by default. */
1600 curwin->w_cursor.lnum = 1;
1601 curwin->w_cursor.col = 0;
1602#ifdef FEAT_VIRTUALEDIT
1603 curwin->w_cursor.coladd = 0;
1604#endif
1605 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001606#ifdef FEAT_AUTOCMD
1607 curwin->w_topline_was_set = FALSE;
1608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001610 /* mark cursor position as being invalid */
1611 curwin->w_valid = 0;
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 /* Make sure the buffer is loaded. */
1614 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001615 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001616#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001617 /* If there is no filetype, allow for detecting one. Esp. useful for
1618 * ":ball" used in a autocommand. If there already is a filetype we
1619 * might prefer to keep it. */
1620 if (*curbuf->b_p_ft == NUL)
1621 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001622#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001623
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001624 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 else
1627 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001628 if (!msg_silent)
1629 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1631#ifdef FEAT_AUTOCMD
1632 curwin->w_topline = 1;
1633# ifdef FEAT_DIFF
1634 curwin->w_topfill = 0;
1635# endif
1636 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1637 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1638#endif
1639 }
1640
1641 /* If autocommands did not change the cursor position, restore cursor lnum
1642 * and possibly cursor col. */
1643 if (curwin->w_cursor.lnum == 1 && inindent(0))
1644 buflist_getfpos();
1645
1646 check_arg_idx(curwin); /* check for valid arg_idx */
1647#ifdef FEAT_TITLE
1648 maketitle();
1649#endif
1650#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001651 /* when autocmds didn't change it */
1652 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653#endif
1654 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1655
Bram Moolenaar009b2592004-10-24 19:18:58 +00001656#ifdef FEAT_NETBEANS_INTG
1657 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001658 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001659#endif
1660
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001661 /* Change directories when the 'acd' option is set. */
1662 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
1664#ifdef FEAT_KEYMAP
1665 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001666 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001668#ifdef FEAT_SPELL
1669 /* May need to set the spell language. Can only do this after the buffer
1670 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001671 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1672 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001673#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001674#ifdef FEAT_VIMINFO
1675 curbuf->b_last_used = vim_time();
1676#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001677
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 redraw_later(NOT_VALID);
1679}
1680
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001681#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1682/*
1683 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001684 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001685 */
1686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001687do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001688{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001689 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001690 && curbuf->b_ffname != NULL
1691 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001692 shorten_fnames(TRUE);
1693}
1694#endif
1695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * functions for dealing with the buffer list
1698 */
1699
1700/*
1701 * Add a file name to the buffer list. Return a pointer to the buffer.
1702 * If the same file name already exists return a pointer to that buffer.
1703 * If it does not exist, or if fname == NULL, a new entry is created.
1704 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1705 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1706 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001707 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001708 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1709 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 * This is the ONLY way to create a new buffer.
1711 */
1712static int top_file_num = 1; /* highest file number */
1713
1714 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001715buflist_new(
1716 char_u *ffname, /* full path of fname or relative */
1717 char_u *sfname, /* short fname or NULL */
1718 linenr_T lnum, /* preferred cursor line */
1719 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 buf_T *buf;
1722#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001723 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
1725
1726 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1727
1728 /*
1729 * If file name already exists in the list, update the entry.
1730 */
1731#ifdef UNIX
1732 /* On Unix we can use inode numbers when the file exists. Works better
1733 * for hard links. */
1734 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1735 st.st_dev = (dev_T)-1;
1736#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001737 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#ifdef UNIX
1739 buflist_findname_stat(ffname, &st)
1740#else
1741 buflist_findname(ffname)
1742#endif
1743 ) != NULL)
1744 {
1745 vim_free(ffname);
1746 if (lnum != 0)
1747 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001748
1749 if ((flags & BLN_NOOPT) == 0)
1750 /* copy the options now, if 'cpo' doesn't have 's' and not done
1751 * already */
1752 buf_copy_options(buf, 0);
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1755 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001756#ifdef FEAT_AUTOCMD
1757 bufref_T bufref;
1758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 buf->b_p_bl = TRUE;
1760#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001761 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001763 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001764 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001765 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001766 return NULL;
1767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
1769 }
1770 return buf;
1771 }
1772
1773 /*
1774 * If the current buffer has no name and no contents, use the current
1775 * buffer. Otherwise: Need to allocate a new buffer structure.
1776 *
1777 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001778 * (A spell file buffer is allocated in spell.c, but that's not a normal
1779 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 */
1781 buf = NULL;
1782 if ((flags & BLN_CURBUF)
1783 && curbuf != NULL
1784 && curbuf->b_ffname == NULL
1785 && curbuf->b_nwindows <= 1
1786 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1787 {
1788 buf = curbuf;
1789#ifdef FEAT_AUTOCMD
1790 /* It's like this buffer is deleted. Watch out for autocommands that
1791 * change curbuf! If that happens, allocate a new buffer anyway. */
1792 if (curbuf->b_p_bl)
1793 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1794 if (buf == curbuf)
1795 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1796# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001797 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 return NULL;
1799# endif
1800#endif
1801#ifdef FEAT_QUICKFIX
1802# ifdef FEAT_AUTOCMD
1803 if (buf == curbuf)
1804# endif
1805 {
1806 /* Make sure 'bufhidden' and 'buftype' are empty */
1807 clear_string_option(&buf->b_p_bh);
1808 clear_string_option(&buf->b_p_bt);
1809 }
1810#endif
1811 }
1812 if (buf != curbuf || curbuf == NULL)
1813 {
1814 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1815 if (buf == NULL)
1816 {
1817 vim_free(ffname);
1818 return NULL;
1819 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001820#ifdef FEAT_EVAL
1821 /* init b: variables */
1822 buf->b_vars = dict_alloc();
1823 if (buf->b_vars == NULL)
1824 {
1825 vim_free(ffname);
1826 vim_free(buf);
1827 return NULL;
1828 }
1829 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832
1833 if (ffname != NULL)
1834 {
1835 buf->b_ffname = ffname;
1836 buf->b_sfname = vim_strsave(sfname);
1837 }
1838
1839 clear_wininfo(buf);
1840 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1841
1842 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1843 || buf->b_wininfo == NULL)
1844 {
1845 vim_free(buf->b_ffname);
1846 buf->b_ffname = NULL;
1847 vim_free(buf->b_sfname);
1848 buf->b_sfname = NULL;
1849 if (buf != curbuf)
1850 free_buffer(buf);
1851 return NULL;
1852 }
1853
1854 if (buf == curbuf)
1855 {
1856 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001857 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 if (buf != curbuf) /* autocommands deleted the buffer! */
1859 return NULL;
1860#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001861 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return NULL;
1863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001865
1866 /* Init the options. */
1867 buf->b_p_initialized = FALSE;
1868 buf_copy_options(buf, BCO_ENTER);
1869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#ifdef FEAT_KEYMAP
1871 /* need to reload lmaps and set b:keymap_name */
1872 curbuf->b_kmap_state |= KEYMAP_INIT;
1873#endif
1874 }
1875 else
1876 {
1877 /*
1878 * put new buffer at the end of the buffer list
1879 */
1880 buf->b_next = NULL;
1881 if (firstbuf == NULL) /* buffer list is empty */
1882 {
1883 buf->b_prev = NULL;
1884 firstbuf = buf;
1885 }
1886 else /* append new buffer at end of list */
1887 {
1888 lastbuf->b_next = buf;
1889 buf->b_prev = lastbuf;
1890 }
1891 lastbuf = buf;
1892
1893 buf->b_fnum = top_file_num++;
1894 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1895 {
1896 EMSG(_("W14: Warning: List of file names overflow"));
1897 if (emsg_silent == 0)
1898 {
1899 out_flush();
1900 ui_delay(3000L, TRUE); /* make sure it is noticed */
1901 }
1902 top_file_num = 1;
1903 }
1904
1905 /*
1906 * Always copy the options from the current buffer.
1907 */
1908 buf_copy_options(buf, BCO_ALWAYS);
1909 }
1910
1911 buf->b_wininfo->wi_fpos.lnum = lnum;
1912 buf->b_wininfo->wi_win = curwin;
1913
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001914#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001915 hash_init(&buf->b_s.b_keywtab);
1916 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917#endif
1918
1919 buf->b_fname = buf->b_sfname;
1920#ifdef UNIX
1921 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001922 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001925 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 buf->b_dev = st.st_dev;
1927 buf->b_ino = st.st_ino;
1928 }
1929#endif
1930 buf->b_u_synced = TRUE;
1931 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001932 if (flags & BLN_DUMMY)
1933 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 buf_clear_file(buf);
1935 clrallmarks(buf); /* clear marks */
1936 fmarks_check_names(buf); /* check file marks for this file */
1937 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1938#ifdef FEAT_AUTOCMD
1939 if (!(flags & BLN_DUMMY))
1940 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001941 bufref_T bufref;
1942
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001943 /* Tricky: these autocommands may change the buffer list. They could
1944 * also split the window with re-using the one empty buffer. This may
1945 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001946 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001947 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001948 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001949 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001951 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001952 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001953 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001954 return NULL;
1955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001957 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 return NULL;
1959# endif
1960 }
1961#endif
1962
1963 return buf;
1964}
1965
1966/*
1967 * Free the memory for the options of a buffer.
1968 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1969 * 'fileencoding'.
1970 */
1971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001972free_buf_options(
1973 buf_T *buf,
1974 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976 if (free_p_ff)
1977 {
1978#ifdef FEAT_MBYTE
1979 clear_string_option(&buf->b_p_fenc);
1980#endif
1981 clear_string_option(&buf->b_p_ff);
1982#ifdef FEAT_QUICKFIX
1983 clear_string_option(&buf->b_p_bh);
1984 clear_string_option(&buf->b_p_bt);
1985#endif
1986 }
1987#ifdef FEAT_FIND_ID
1988 clear_string_option(&buf->b_p_def);
1989 clear_string_option(&buf->b_p_inc);
1990# ifdef FEAT_EVAL
1991 clear_string_option(&buf->b_p_inex);
1992# endif
1993#endif
1994#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1995 clear_string_option(&buf->b_p_inde);
1996 clear_string_option(&buf->b_p_indk);
1997#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001998#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1999 clear_string_option(&buf->b_p_bexpr);
2000#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002001#if defined(FEAT_CRYPT)
2002 clear_string_option(&buf->b_p_cm);
2003#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002004#if defined(FEAT_EVAL)
2005 clear_string_option(&buf->b_p_fex);
2006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007#ifdef FEAT_CRYPT
2008 clear_string_option(&buf->b_p_key);
2009#endif
2010 clear_string_option(&buf->b_p_kp);
2011 clear_string_option(&buf->b_p_mps);
2012 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002013 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 clear_string_option(&buf->b_p_isk);
2015#ifdef FEAT_KEYMAP
2016 clear_string_option(&buf->b_p_keymap);
2017 ga_clear(&buf->b_kmap_ga);
2018#endif
2019#ifdef FEAT_COMMENTS
2020 clear_string_option(&buf->b_p_com);
2021#endif
2022#ifdef FEAT_FOLDING
2023 clear_string_option(&buf->b_p_cms);
2024#endif
2025 clear_string_option(&buf->b_p_nf);
2026#ifdef FEAT_SYN_HL
2027 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002028 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002029#endif
2030#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002031 clear_string_option(&buf->b_s.b_p_spc);
2032 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002033 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002034 buf->b_s.b_cap_prog = NULL;
2035 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036#endif
2037#ifdef FEAT_SEARCHPATH
2038 clear_string_option(&buf->b_p_sua);
2039#endif
2040#ifdef FEAT_AUTOCMD
2041 clear_string_option(&buf->b_p_ft);
2042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043#ifdef FEAT_CINDENT
2044 clear_string_option(&buf->b_p_cink);
2045 clear_string_option(&buf->b_p_cino);
2046#endif
2047#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2048 clear_string_option(&buf->b_p_cinw);
2049#endif
2050#ifdef FEAT_INS_EXPAND
2051 clear_string_option(&buf->b_p_cpt);
2052#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002053#ifdef FEAT_COMPL_FUNC
2054 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002055 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057#ifdef FEAT_QUICKFIX
2058 clear_string_option(&buf->b_p_gp);
2059 clear_string_option(&buf->b_p_mp);
2060 clear_string_option(&buf->b_p_efm);
2061#endif
2062 clear_string_option(&buf->b_p_ep);
2063 clear_string_option(&buf->b_p_path);
2064 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002065 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066#ifdef FEAT_INS_EXPAND
2067 clear_string_option(&buf->b_p_dict);
2068 clear_string_option(&buf->b_p_tsr);
2069#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002070#ifdef FEAT_TEXTOBJ
2071 clear_string_option(&buf->b_p_qe);
2072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002074 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002075#ifdef FEAT_LISP
2076 clear_string_option(&buf->b_p_lw);
2077#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002078 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079}
2080
2081/*
2082 * get alternate file n
2083 * set linenr to lnum or altfpos.lnum if lnum == 0
2084 * also set cursor column to altfpos.col if 'startofline' is not set.
2085 * if (options & GETF_SETMARK) call setpcmark()
2086 * if (options & GETF_ALT) we are jumping to an alternate file.
2087 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2088 *
2089 * return FAIL for failure, OK for success
2090 */
2091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002092buflist_getfile(
2093 int n,
2094 linenr_T lnum,
2095 int options,
2096 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 buf_T *buf;
2099#ifdef FEAT_WINDOWS
2100 win_T *wp = NULL;
2101#endif
2102 pos_T *fpos;
2103 colnr_T col;
2104
2105 buf = buflist_findnr(n);
2106 if (buf == NULL)
2107 {
2108 if ((options & GETF_ALT) && n == 0)
2109 EMSG(_(e_noalt));
2110 else
2111 EMSGN(_("E92: Buffer %ld not found"), n);
2112 return FAIL;
2113 }
2114
2115 /* if alternate file is the current buffer, nothing to do */
2116 if (buf == curbuf)
2117 return OK;
2118
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002119 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002120 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002121 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002123 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002124#ifdef FEAT_AUTOCMD
2125 if (curbuf_locked())
2126 return FAIL;
2127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 /* altfpos may be changed by getfile(), get it now */
2130 if (lnum == 0)
2131 {
2132 fpos = buflist_findfpos(buf);
2133 lnum = fpos->lnum;
2134 col = fpos->col;
2135 }
2136 else
2137 col = 0;
2138
2139#ifdef FEAT_WINDOWS
2140 if (options & GETF_SWITCH)
2141 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002142 /* If 'switchbuf' contains "useopen": jump to first window containing
2143 * "buf" if one exists */
2144 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002146
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002147 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002148 * page containing "buf" if one exists */
2149 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002150 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002151
2152 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2153 * current buffer isn't empty: open new tab or window */
2154 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2155 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002157 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002158 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002159 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2160 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002162 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 }
2165#endif
2166
2167 ++RedrawingDisabled;
2168 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2169 lnum, forceit) <= 0)
2170 {
2171 --RedrawingDisabled;
2172
2173 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2174 if (!p_sol && col != 0)
2175 {
2176 curwin->w_cursor.col = col;
2177 check_cursor_col();
2178#ifdef FEAT_VIRTUALEDIT
2179 curwin->w_cursor.coladd = 0;
2180#endif
2181 curwin->w_set_curswant = TRUE;
2182 }
2183 return OK;
2184 }
2185 --RedrawingDisabled;
2186 return FAIL;
2187}
2188
2189/*
2190 * go to the last know line number for the current buffer
2191 */
2192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002193buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 pos_T *fpos;
2196
2197 fpos = buflist_findfpos(curbuf);
2198
2199 curwin->w_cursor.lnum = fpos->lnum;
2200 check_cursor_lnum();
2201
2202 if (p_sol)
2203 curwin->w_cursor.col = 0;
2204 else
2205 {
2206 curwin->w_cursor.col = fpos->col;
2207 check_cursor_col();
2208#ifdef FEAT_VIRTUALEDIT
2209 curwin->w_cursor.coladd = 0;
2210#endif
2211 curwin->w_set_curswant = TRUE;
2212 }
2213}
2214
Bram Moolenaar81695252004-12-29 20:58:21 +00002215#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2216/*
2217 * Find file in buffer list by name (it has to be for the current window).
2218 * Returns NULL if not found.
2219 */
2220 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002221buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002222{
2223 char_u *ffname;
2224 buf_T *buf = NULL;
2225
2226 /* First make the name into a full path name */
2227 ffname = FullName_save(fname,
2228#ifdef UNIX
2229 TRUE /* force expansion, get rid of symbolic links */
2230#else
2231 FALSE
2232#endif
2233 );
2234 if (ffname != NULL)
2235 {
2236 buf = buflist_findname(ffname);
2237 vim_free(ffname);
2238 }
2239 return buf;
2240}
2241#endif
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243/*
2244 * Find file in buffer list by name (it has to be for the current window).
2245 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002246 * Skips dummy buffers.
2247 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 */
2249 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251{
2252#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002253 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
2255 if (mch_stat((char *)ffname, &st) < 0)
2256 st.st_dev = (dev_T)-1;
2257 return buflist_findname_stat(ffname, &st);
2258}
2259
2260/*
2261 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2262 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002263 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 */
2265 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002266buflist_findname_stat(
2267 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002268 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
2270#endif
2271 buf_T *buf;
2272
2273 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002274 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275#ifdef UNIX
2276 , stp
2277#endif
2278 ))
2279 return buf;
2280 return NULL;
2281}
2282
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002283#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2284 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285/*
2286 * Find file in buffer list by a regexp pattern.
2287 * Return fnum of the found buffer.
2288 * Return < 0 for error.
2289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002291buflist_findpat(
2292 char_u *pattern,
2293 char_u *pattern_end, /* pointer to first char after pattern */
2294 int unlisted, /* find unlisted buffers */
2295 int diffmode UNUSED, /* find diff-mode buffers only */
2296 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 int match = -1;
2300 int find_listed;
2301 char_u *pat;
2302 char_u *patend;
2303 int attempt;
2304 char_u *p;
2305 int toggledollar;
2306
2307 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2308 {
2309 if (*pattern == '%')
2310 match = curbuf->b_fnum;
2311 else
2312 match = curwin->w_alt_fnum;
2313#ifdef FEAT_DIFF
2314 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2315 match = -1;
2316#endif
2317 }
2318
2319 /*
2320 * Try four ways of matching a listed buffer:
2321 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002322 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * attempt == 2: with '$' at end (only match at end)
2324 * attempt == 3: with '^' at start and '$' at end (only full match)
2325 * Repeat this for finding an unlisted buffer if there was no matching
2326 * listed buffer.
2327 */
2328 else
2329 {
2330 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2331 if (pat == NULL)
2332 return -1;
2333 patend = pat + STRLEN(pat) - 1;
2334 toggledollar = (patend > pat && *patend == '$');
2335
2336 /* First try finding a listed buffer. If not found and "unlisted"
2337 * is TRUE, try finding an unlisted buffer. */
2338 find_listed = TRUE;
2339 for (;;)
2340 {
2341 for (attempt = 0; attempt <= 3; ++attempt)
2342 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002343 regmatch_T regmatch;
2344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 /* may add '^' and '$' */
2346 if (toggledollar)
2347 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2348 p = pat;
2349 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2350 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002351 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2352 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 vim_free(pat);
2355 return -1;
2356 }
2357
2358 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2359 if (buf->b_p_bl == find_listed
2360#ifdef FEAT_DIFF
2361 && (!diffmode || diff_mode_buf(buf))
2362#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002363 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002365 if (curtab_only)
2366 {
2367 /* Ignore the match if the buffer is not open in
2368 * the current tab. */
2369#ifdef FEAT_WINDOWS
2370 win_T *wp;
2371
2372 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2373 if (wp->w_buffer == buf)
2374 break;
2375 if (wp == NULL)
2376 continue;
2377#else
2378 if (curwin->w_buffer != buf)
2379 continue;
2380#endif
2381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (match >= 0) /* already found a match */
2383 {
2384 match = -2;
2385 break;
2386 }
2387 match = buf->b_fnum; /* remember first match */
2388 }
2389
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002390 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (match >= 0) /* found one match */
2392 break;
2393 }
2394
2395 /* Only search for unlisted buffers if there was no match with
2396 * a listed buffer. */
2397 if (!unlisted || !find_listed || match != -1)
2398 break;
2399 find_listed = FALSE;
2400 }
2401
2402 vim_free(pat);
2403 }
2404
2405 if (match == -2)
2406 EMSG2(_("E93: More than one match for %s"), pattern);
2407 else if (match < 0)
2408 EMSG2(_("E94: No matching buffer for %s"), pattern);
2409 return match;
2410}
2411#endif
2412
2413#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2414
2415/*
2416 * Find all buffer names that match.
2417 * For command line expansion of ":buf" and ":sbuf".
2418 * Return OK if matches found, FAIL otherwise.
2419 */
2420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002421ExpandBufnames(
2422 char_u *pat,
2423 int *num_file,
2424 char_u ***file,
2425 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427 int count = 0;
2428 buf_T *buf;
2429 int round;
2430 char_u *p;
2431 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002432 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 *num_file = 0; /* return values in case of FAIL */
2435 *file = NULL;
2436
Bram Moolenaar05159a02005-02-26 23:04:13 +00002437 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2438 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002440 patc = alloc((unsigned)STRLEN(pat) + 11);
2441 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002443 STRCPY(patc, "\\(^\\|[\\/]\\)");
2444 STRCPY(patc + 11, pat + 1);
2445 }
2446 else
2447 patc = pat;
2448
2449 /*
2450 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002451 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002452 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002453 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002454 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002455 regmatch_T regmatch;
2456
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002457 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002458 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002459 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2460 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002461 {
2462 if (patc != pat)
2463 vim_free(patc);
2464 return FAIL;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 /*
2468 * round == 1: Count the matches.
2469 * round == 2: Build the array to keep the matches.
2470 */
2471 for (round = 1; round <= 2; ++round)
2472 {
2473 count = 0;
2474 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2475 {
2476 if (!buf->b_p_bl) /* skip unlisted buffers */
2477 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002478 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 if (p != NULL)
2480 {
2481 if (round == 1)
2482 ++count;
2483 else
2484 {
2485 if (options & WILD_HOME_REPLACE)
2486 p = home_replace_save(buf, p);
2487 else
2488 p = vim_strsave(p);
2489 (*file)[count++] = p;
2490 }
2491 }
2492 }
2493 if (count == 0) /* no match found, break here */
2494 break;
2495 if (round == 1)
2496 {
2497 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2498 if (*file == NULL)
2499 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002500 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002501 if (patc != pat)
2502 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 return FAIL;
2504 }
2505 }
2506 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002507 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 if (count) /* match(es) found, break here */
2509 break;
2510 }
2511
Bram Moolenaar05159a02005-02-26 23:04:13 +00002512 if (patc != pat)
2513 vim_free(patc);
2514
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 *num_file = count;
2516 return (count == 0 ? FAIL : OK);
2517}
2518
2519#endif /* FEAT_CMDL_COMPL */
2520
2521#ifdef HAVE_BUFLIST_MATCH
2522/*
2523 * Check for a match on the file name for buffer "buf" with regprog "prog".
2524 */
2525 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002526buflist_match(
2527 regmatch_T *rmp,
2528 buf_T *buf,
2529 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
2531 char_u *match;
2532
2533 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002534 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002536 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
2538 return match;
2539}
2540
2541/*
2542 * Try matching the regexp in "prog" with file name "name".
2543 * Return "name" when there is a match, NULL when not.
2544 */
2545 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002546fname_match(
2547 regmatch_T *rmp,
2548 char_u *name,
2549 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 char_u *match = NULL;
2552 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 if (name != NULL)
2555 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002556 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002557 rmp->rm_ic = p_fic || ignore_case;
2558 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 match = name;
2560 else
2561 {
2562 /* Replace $(HOME) with '~' and try matching again. */
2563 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002564 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 match = name;
2566 vim_free(p);
2567 }
2568 }
2569
2570 return match;
2571}
2572#endif
2573
2574/*
2575 * find file in buffer list by number
2576 */
2577 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002578buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
2580 buf_T *buf;
2581
2582 if (nr == 0)
2583 nr = curwin->w_alt_fnum;
2584 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2585 if (buf->b_fnum == nr)
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002586 return buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 return NULL;
2588}
2589
2590/*
2591 * Get name of file 'n' in the buffer list.
2592 * When the file has no name an empty string is returned.
2593 * home_replace() is used to shorten the file name (used for marks).
2594 * Returns a pointer to allocated memory, of NULL when failed.
2595 */
2596 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002597buflist_nr2name(
2598 int n,
2599 int fullname,
2600 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
2602 buf_T *buf;
2603
2604 buf = buflist_findnr(n);
2605 if (buf == NULL)
2606 return NULL;
2607 return home_replace_save(helptail ? buf : NULL,
2608 fullname ? buf->b_ffname : buf->b_fname);
2609}
2610
2611/*
2612 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2613 * When "copy_options" is TRUE save the local window option values.
2614 * When "lnum" is 0 only do the options.
2615 */
2616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002617buflist_setfpos(
2618 buf_T *buf,
2619 win_T *win,
2620 linenr_T lnum,
2621 colnr_T col,
2622 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
2624 wininfo_T *wip;
2625
2626 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2627 if (wip->wi_win == win)
2628 break;
2629 if (wip == NULL)
2630 {
2631 /* allocate a new entry */
2632 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2633 if (wip == NULL)
2634 return;
2635 wip->wi_win = win;
2636 if (lnum == 0) /* set lnum even when it's 0 */
2637 lnum = 1;
2638 }
2639 else
2640 {
2641 /* remove the entry from the list */
2642 if (wip->wi_prev)
2643 wip->wi_prev->wi_next = wip->wi_next;
2644 else
2645 buf->b_wininfo = wip->wi_next;
2646 if (wip->wi_next)
2647 wip->wi_next->wi_prev = wip->wi_prev;
2648 if (copy_options && wip->wi_optset)
2649 {
2650 clear_winopt(&wip->wi_opt);
2651#ifdef FEAT_FOLDING
2652 deleteFoldRecurse(&wip->wi_folds);
2653#endif
2654 }
2655 }
2656 if (lnum != 0)
2657 {
2658 wip->wi_fpos.lnum = lnum;
2659 wip->wi_fpos.col = col;
2660 }
2661 if (copy_options)
2662 {
2663 /* Save the window-specific option values. */
2664 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2665#ifdef FEAT_FOLDING
2666 wip->wi_fold_manual = win->w_fold_manual;
2667 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2668#endif
2669 wip->wi_optset = TRUE;
2670 }
2671
2672 /* insert the entry in front of the list */
2673 wip->wi_next = buf->b_wininfo;
2674 buf->b_wininfo = wip;
2675 wip->wi_prev = NULL;
2676 if (wip->wi_next)
2677 wip->wi_next->wi_prev = wip;
2678
2679 return;
2680}
2681
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002682#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002683static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002684
2685/*
2686 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2687 * page. That's because a diff is local to a tab page.
2688 */
2689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002690wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002691{
2692 win_T *wp;
2693
2694 if (wip->wi_opt.wo_diff)
2695 {
2696 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2697 /* return FALSE when it's a window in the current tab page, thus
2698 * the buffer was in diff mode here */
2699 if (wip->wi_win == wp)
2700 return FALSE;
2701 return TRUE;
2702 }
2703 return FALSE;
2704}
2705#endif
2706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707/*
2708 * Find info for the current window in buffer "buf".
2709 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002710 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2711 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 * Returns NULL when there isn't any info.
2713 */
2714 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002715find_wininfo(
2716 buf_T *buf,
2717 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 wininfo_T *wip;
2720
2721 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002722 if (wip->wi_win == curwin
2723#ifdef FEAT_DIFF
2724 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2725#endif
2726 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002728
2729 /* If no wininfo for curwin, use the first in the list (that doesn't have
2730 * 'diff' set and is in another tab page). */
2731 if (wip == NULL)
2732 {
2733#ifdef FEAT_DIFF
2734 if (skip_diff_buffer)
2735 {
2736 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2737 if (!wininfo_other_tab_diff(wip))
2738 break;
2739 }
2740 else
2741#endif
2742 wip = buf->b_wininfo;
2743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 return wip;
2745}
2746
2747/*
2748 * Reset the local window options to the values last used in this window.
2749 * If the buffer wasn't used in this window before, use the values from
2750 * the most recently used window. If the values were never set, use the
2751 * global values for the window.
2752 */
2753 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002754get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 wininfo_T *wip;
2757
2758 clear_winopt(&curwin->w_onebuf_opt);
2759#ifdef FEAT_FOLDING
2760 clearFolding(curwin);
2761#endif
2762
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002763 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (wip != NULL && wip->wi_optset)
2765 {
2766 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2767#ifdef FEAT_FOLDING
2768 curwin->w_fold_manual = wip->wi_fold_manual;
2769 curwin->w_foldinvalid = TRUE;
2770 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2771#endif
2772 }
2773 else
2774 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2775
2776#ifdef FEAT_FOLDING
2777 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2778 if (p_fdls >= 0)
2779 curwin->w_p_fdl = p_fdls;
2780#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002781#ifdef FEAT_SYN_HL
2782 check_colorcolumn(curwin);
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784}
2785
2786/*
2787 * Find the position (lnum and col) for the buffer 'buf' for the current
2788 * window.
2789 * Returns a pointer to no_position if no position is found.
2790 */
2791 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002792buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
2794 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002795 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002797 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (wip != NULL)
2799 return &(wip->wi_fpos);
2800 else
2801 return &no_position;
2802}
2803
2804/*
2805 * Find the lnum for the buffer 'buf' for the current window.
2806 */
2807 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002808buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
2810 return buflist_findfpos(buf)->lnum;
2811}
2812
2813#if defined(FEAT_LISTCMDS) || defined(PROTO)
2814/*
2815 * List all know file names (for :files and :buffers command).
2816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002818buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 buf_T *buf;
2821 int len;
2822 int i;
2823
2824 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2825 {
2826 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002827 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2828 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2829 || (vim_strchr(eap->arg, '+')
2830 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2831 || (vim_strchr(eap->arg, 'a')
2832 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2833 || (vim_strchr(eap->arg, 'h')
2834 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2835 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2836 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2837 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2838 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2839 || (vim_strchr(eap->arg, '#')
2840 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 continue;
2842 msg_putchar('\n');
2843 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002844 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 else
2846 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2847
Bram Moolenaar50cde822005-06-05 21:54:54 +00002848 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 buf->b_fnum,
2850 buf->b_p_bl ? ' ' : 'u',
2851 buf == curbuf ? '%' :
2852 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2853 buf->b_ml.ml_mfp == NULL ? ' ' :
2854 (buf->b_nwindows == 0 ? 'h' : 'a'),
2855 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2856 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002857 : (bufIsChanged(buf) ? '+' : ' '),
2858 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002859 if (len > IOSIZE - 20)
2860 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
2862 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 i = 40 - vim_strsize(IObuff);
2864 do
2865 {
2866 IObuff[len++] = ' ';
2867 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002868 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2869 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002870 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 msg_outtrans(IObuff);
2872 out_flush(); /* output one line at a time */
2873 ui_breakcheck();
2874 }
2875}
2876#endif
2877
2878/*
2879 * Get file name and line number for file 'fnum'.
2880 * Used by DoOneCmd() for translating '%' and '#'.
2881 * Used by insert_reg() and cmdline_paste() for '#' register.
2882 * Return FAIL if not found, OK for success.
2883 */
2884 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002885buflist_name_nr(
2886 int fnum,
2887 char_u **fname,
2888 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
2890 buf_T *buf;
2891
2892 buf = buflist_findnr(fnum);
2893 if (buf == NULL || buf->b_fname == NULL)
2894 return FAIL;
2895
2896 *fname = buf->b_fname;
2897 *lnum = buflist_findlnum(buf);
2898
2899 return OK;
2900}
2901
2902/*
2903 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2904 * The file name with the full path is also remembered, for when :cd is used.
2905 * Returns FAIL for failure (file name already in use by other buffer)
2906 * OK otherwise.
2907 */
2908 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002909setfname(
2910 buf_T *buf,
2911 char_u *ffname,
2912 char_u *sfname,
2913 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
Bram Moolenaar81695252004-12-29 20:58:21 +00002915 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002917 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918#endif
2919
2920 if (ffname == NULL || *ffname == NUL)
2921 {
2922 /* Removing the name. */
2923 vim_free(buf->b_ffname);
2924 vim_free(buf->b_sfname);
2925 buf->b_ffname = NULL;
2926 buf->b_sfname = NULL;
2927#ifdef UNIX
2928 st.st_dev = (dev_T)-1;
2929#endif
2930 }
2931 else
2932 {
2933 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2934 if (ffname == NULL) /* out of memory */
2935 return FAIL;
2936
2937 /*
2938 * if the file name is already used in another buffer:
2939 * - if the buffer is loaded, fail
2940 * - if the buffer is not loaded, delete it from the list
2941 */
2942#ifdef UNIX
2943 if (mch_stat((char *)ffname, &st) < 0)
2944 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002945#endif
2946 if (!(buf->b_flags & BF_DUMMY))
2947#ifdef UNIX
2948 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002950 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951#endif
2952 if (obuf != NULL && obuf != buf)
2953 {
2954 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2955 {
2956 if (message)
2957 EMSG(_("E95: Buffer with this name already exists"));
2958 vim_free(ffname);
2959 return FAIL;
2960 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002961 /* delete from the list */
2962 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964 sfname = vim_strsave(sfname);
2965 if (ffname == NULL || sfname == NULL)
2966 {
2967 vim_free(sfname);
2968 vim_free(ffname);
2969 return FAIL;
2970 }
2971#ifdef USE_FNAME_CASE
2972# ifdef USE_LONG_FNAME
2973 if (USE_LONG_FNAME)
2974# endif
2975 fname_case(sfname, 0); /* set correct case for short file name */
2976#endif
2977 vim_free(buf->b_ffname);
2978 vim_free(buf->b_sfname);
2979 buf->b_ffname = ffname;
2980 buf->b_sfname = sfname;
2981 }
2982 buf->b_fname = buf->b_sfname;
2983#ifdef UNIX
2984 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002985 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 else
2987 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002988 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 buf->b_dev = st.st_dev;
2990 buf->b_ino = st.st_ino;
2991 }
2992#endif
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
2996 buf_name_changed(buf);
2997 return OK;
2998}
2999
3000/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003001 * Crude way of changing the name of a buffer. Use with care!
3002 * The name should be relative to the current directory.
3003 */
3004 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003005buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003006{
3007 buf_T *buf;
3008
3009 buf = buflist_findnr(fnum);
3010 if (buf != NULL)
3011 {
3012 vim_free(buf->b_sfname);
3013 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003014 buf->b_ffname = vim_strsave(name);
3015 buf->b_sfname = NULL;
3016 /* Allocate ffname and expand into full path. Also resolves .lnk
3017 * files on Win32. */
3018 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003019 buf->b_fname = buf->b_sfname;
3020 }
3021}
3022
3023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 * Take care of what needs to be done when the name of buffer "buf" has
3025 * changed.
3026 */
3027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003028buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 /*
3031 * If the file name changed, also change the name of the swapfile
3032 */
3033 if (buf->b_ml.ml_mfp != NULL)
3034 ml_setname(buf);
3035
3036 if (curwin->w_buffer == buf)
3037 check_arg_idx(curwin); /* check file name for arg list */
3038#ifdef FEAT_TITLE
3039 maketitle(); /* set window title */
3040#endif
3041#ifdef FEAT_WINDOWS
3042 status_redraw_all(); /* status lines need to be redrawn */
3043#endif
3044 fmarks_check_names(buf); /* check named file marks */
3045 ml_timestamp(buf); /* reset timestamp */
3046}
3047
3048/*
3049 * set alternate file name for current window
3050 *
3051 * Used by do_one_cmd(), do_write() and do_ecmd().
3052 * Return the buffer.
3053 */
3054 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003055setaltfname(
3056 char_u *ffname,
3057 char_u *sfname,
3058 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 buf_T *buf;
3061
3062 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3063 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003064 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 curwin->w_alt_fnum = buf->b_fnum;
3066 return buf;
3067}
3068
3069/*
3070 * Get alternate file name for current window.
3071 * Return NULL if there isn't any, and give error message if requested.
3072 */
3073 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003074getaltfname(
3075 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
3077 char_u *fname;
3078 linenr_T dummy;
3079
3080 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3081 {
3082 if (errmsg)
3083 EMSG(_(e_noalt));
3084 return NULL;
3085 }
3086 return fname;
3087}
3088
3089/*
3090 * Add a file name to the buflist and return its number.
3091 * Uses same flags as buflist_new(), except BLN_DUMMY.
3092 *
3093 * used by qf_init(), main() and doarglist()
3094 */
3095 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003096buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
3098 buf_T *buf;
3099
3100 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3101 if (buf != NULL)
3102 return buf->b_fnum;
3103 return 0;
3104}
3105
3106#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3107/*
3108 * Adjust slashes in file names. Called after 'shellslash' was set.
3109 */
3110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003111buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
3113 buf_T *bp;
3114
3115 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3116 {
3117 if (bp->b_ffname != NULL)
3118 slash_adjust(bp->b_ffname);
3119 if (bp->b_sfname != NULL)
3120 slash_adjust(bp->b_sfname);
3121 }
3122}
3123#endif
3124
3125/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003126 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * Also save the local window option values.
3128 */
3129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003130buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003132 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134
3135/*
3136 * Return TRUE if 'ffname' is not the same file as current file.
3137 * Fname must have a full path (expanded by mch_FullName()).
3138 */
3139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003140otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 return otherfile_buf(curbuf, ffname
3143#ifdef UNIX
3144 , NULL
3145#endif
3146 );
3147}
3148
3149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003150otherfile_buf(
3151 buf_T *buf,
3152 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003154 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003156 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 /* no name is different */
3159 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3160 return TRUE;
3161 if (fnamecmp(ffname, buf->b_ffname) == 0)
3162 return FALSE;
3163#ifdef UNIX
3164 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003165 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
Bram Moolenaar8767f522016-07-01 17:17:39 +02003167 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 if (stp == NULL)
3169 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003170 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 st.st_dev = (dev_T)-1;
3172 stp = &st;
3173 }
3174 /* Use dev/ino to check if the files are the same, even when the names
3175 * are different (possible with links). Still need to compare the
3176 * name above, for when the file doesn't exist yet.
3177 * Problem: The dev/ino changes when a file is deleted (and created
3178 * again) and remains the same when renamed/moved. We don't want to
3179 * mch_stat() each buffer each time, that would be too slow. Get the
3180 * dev/ino again when they appear to match, but not when they appear
3181 * to be different: Could skip a buffer when it's actually the same
3182 * file. */
3183 if (buf_same_ino(buf, stp))
3184 {
3185 buf_setino(buf);
3186 if (buf_same_ino(buf, stp))
3187 return FALSE;
3188 }
3189 }
3190#endif
3191 return TRUE;
3192}
3193
3194#if defined(UNIX) || defined(PROTO)
3195/*
3196 * Set inode and device number for a buffer.
3197 * Must always be called when b_fname is changed!.
3198 */
3199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003200buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003202 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3205 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003206 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 buf->b_dev = st.st_dev;
3208 buf->b_ino = st.st_ino;
3209 }
3210 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003211 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212}
3213
3214/*
3215 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3216 */
3217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218buf_same_ino(
3219 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003220 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003222 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 && stp->st_dev == buf->b_dev
3224 && stp->st_ino == buf->b_ino);
3225}
3226#endif
3227
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003228/*
3229 * Print info about the current buffer.
3230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232fileinfo(
3233 int fullname, /* when non-zero print full path */
3234 int shorthelp,
3235 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 char_u *name;
3238 int n;
3239 char_u *p;
3240 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003241 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
3243 buffer = alloc(IOSIZE);
3244 if (buffer == NULL)
3245 return;
3246
3247 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3248 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003249 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 p = buffer + STRLEN(buffer);
3251 }
3252 else
3253 p = buffer;
3254
3255 *p++ = '"';
3256 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003257 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 else
3259 {
3260 if (!fullname && curbuf->b_fname != NULL)
3261 name = curbuf->b_fname;
3262 else
3263 name = curbuf->b_ffname;
3264 home_replace(shorthelp ? curbuf : NULL, name, p,
3265 (int)(IOSIZE - (p - buffer)), TRUE);
3266 }
3267
Bram Moolenaara800b422010-06-27 01:15:55 +02003268 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 curbufIsChanged() ? (shortmess(SHM_MOD)
3270 ? " [+]" : _(" [Modified]")) : " ",
3271 (curbuf->b_flags & BF_NOTEDITED)
3272#ifdef FEAT_QUICKFIX
3273 && !bt_dontwrite(curbuf)
3274#endif
3275 ? _("[Not edited]") : "",
3276 (curbuf->b_flags & BF_NEW)
3277#ifdef FEAT_QUICKFIX
3278 && !bt_dontwrite(curbuf)
3279#endif
3280 ? _("[New file]") : "",
3281 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003282 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 : _("[readonly]")) : "",
3284 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3285 || curbuf->b_p_ro) ?
3286 " " : "");
3287 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3288 * causes an overflow, thus for large numbers divide instead. */
3289 if (curwin->w_cursor.lnum > 1000000L)
3290 n = (int)(((long)curwin->w_cursor.lnum) /
3291 ((long)curbuf->b_ml.ml_line_count / 100L));
3292 else
3293 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3294 (long)curbuf->b_ml.ml_line_count);
3295 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3296 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003297 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299#ifdef FEAT_CMDL_INFO
3300 else if (p_ru)
3301 {
3302 /* Current line and column are already on the screen -- webb */
3303 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003304 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003306 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 (long)curbuf->b_ml.ml_line_count, n);
3308 }
3309#endif
3310 else
3311 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003312 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 _("line %ld of %ld --%d%%-- col "),
3314 (long)curwin->w_cursor.lnum,
3315 (long)curbuf->b_ml.ml_line_count,
3316 n);
3317 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003318 len = STRLEN(buffer);
3319 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3321 }
3322
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003323 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
3325 if (dont_truncate)
3326 {
3327 /* Temporarily set msg_scroll to avoid the message being truncated.
3328 * First call msg_start() to get the message in the right place. */
3329 msg_start();
3330 n = msg_scroll;
3331 msg_scroll = TRUE;
3332 msg(buffer);
3333 msg_scroll = n;
3334 }
3335 else
3336 {
3337 p = msg_trunc_attr(buffer, FALSE, 0);
3338 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 /* Need to repeat the message after redrawing when:
3340 * - When restart_edit is set (otherwise there will be a delay
3341 * before redrawing).
3342 * - When the screen was scrolled but there is no wait-return
3343 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003344 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346
3347 vim_free(buffer);
3348}
3349
3350 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003351col_print(
3352 char_u *buf,
3353 size_t buflen,
3354 int col,
3355 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003358 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003360 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361}
3362
3363#if defined(FEAT_TITLE) || defined(PROTO)
3364/*
3365 * put file name in title bar of window and in icon title
3366 */
3367
3368static char_u *lasttitle = NULL;
3369static char_u *lasticon = NULL;
3370
3371 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003372maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 char_u *p;
3375 char_u *t_str = NULL;
3376 char_u *i_name;
3377 char_u *i_str = NULL;
3378 int maxlen = 0;
3379 int len;
3380 int mustset;
3381 char_u buf[IOSIZE];
3382 int off;
3383
3384 if (!redrawing())
3385 {
3386 /* Postpone updating the title when 'lazyredraw' is set. */
3387 need_maketitle = TRUE;
3388 return;
3389 }
3390
3391 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003392 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 return;
3394
3395 if (p_title)
3396 {
3397 if (p_titlelen > 0)
3398 {
3399 maxlen = p_titlelen * Columns / 100;
3400 if (maxlen < 10)
3401 maxlen = 10;
3402 }
3403
3404 t_str = buf;
3405 if (*p_titlestring != NUL)
3406 {
3407#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003408 if (stl_syntax & STL_IN_TITLE)
3409 {
3410 int use_sandbox = FALSE;
3411 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003412
3413# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003414 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003415# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003416 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003418 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003419 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003420 if (called_emsg)
3421 set_string_option_direct((char_u *)"titlestring", -1,
3422 (char_u *)"", OPT_FREE, SID_ERROR);
3423 called_emsg |= save_called_emsg;
3424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 else
3426#endif
3427 t_str = p_titlestring;
3428 }
3429 else
3430 {
3431 /* format: "fname + (path) (1 of 2) - VIM" */
3432
Bram Moolenaar2c666692012-09-05 13:30:40 +02003433#define SPACE_FOR_FNAME (IOSIZE - 100)
3434#define SPACE_FOR_DIR (IOSIZE - 20)
3435#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003437 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 else
3439 {
3440 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003441 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444
3445 switch (bufIsChanged(curbuf)
3446 + (curbuf->b_p_ro * 2)
3447 + (!curbuf->b_p_ma * 4))
3448 {
3449 case 1: STRCAT(buf, " +"); break;
3450 case 2: STRCAT(buf, " ="); break;
3451 case 3: STRCAT(buf, " =+"); break;
3452 case 4:
3453 case 6: STRCAT(buf, " -"); break;
3454 case 5:
3455 case 7: STRCAT(buf, " -+"); break;
3456 }
3457
3458 if (curbuf->b_fname != NULL)
3459 {
3460 /* Get path of file, replace home dir with ~ */
3461 off = (int)STRLEN(buf);
3462 buf[off++] = ' ';
3463 buf[off++] = '(';
3464 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003465 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466#ifdef BACKSLASH_IN_FILENAME
3467 /* avoid "c:/name" to be reduced to "c" */
3468 if (isalpha(buf[off]) && buf[off + 1] == ':')
3469 off += 2;
3470#endif
3471 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003472 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003475 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003476 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003479
Bram Moolenaar2c666692012-09-05 13:30:40 +02003480 /* Translate unprintable chars and concatenate. Keep some
3481 * room for the server name. When there is no room (very long
3482 * file name) use (...). */
3483 if (off < SPACE_FOR_DIR)
3484 {
3485 p = transstr(buf + off);
3486 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3487 vim_free(p);
3488 }
3489 else
3490 {
3491 vim_strncpy(buf + off, (char_u *)"...",
3492 (size_t)(SPACE_FOR_ARGNR - off));
3493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 STRCAT(buf, ")");
3495 }
3496
Bram Moolenaar2c666692012-09-05 13:30:40 +02003497 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499#if defined(FEAT_CLIENTSERVER)
3500 if (serverName != NULL)
3501 {
3502 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003503 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505 else
3506#endif
3507 STRCAT(buf, " - VIM");
3508
3509 if (maxlen > 0)
3510 {
3511 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003512 if (vim_strsize(buf) > maxlen)
3513 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515 }
3516 }
3517 mustset = ti_change(t_str, &lasttitle);
3518
3519 if (p_icon)
3520 {
3521 i_str = buf;
3522 if (*p_iconstring != NUL)
3523 {
3524#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003525 if (stl_syntax & STL_IN_ICON)
3526 {
3527 int use_sandbox = FALSE;
3528 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003529
3530# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003531 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003532# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003533 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003535 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003536 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003537 if (called_emsg)
3538 set_string_option_direct((char_u *)"iconstring", -1,
3539 (char_u *)"", OPT_FREE, SID_ERROR);
3540 called_emsg |= save_called_emsg;
3541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 else
3543#endif
3544 i_str = p_iconstring;
3545 }
3546 else
3547 {
3548 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003549 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 else /* use file name only in icon */
3551 i_name = gettail(curbuf->b_ffname);
3552 *i_str = NUL;
3553 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003554 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (len > 100)
3556 {
3557 len -= 100;
3558#ifdef FEAT_MBYTE
3559 if (has_mbyte)
3560 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3561#endif
3562 i_name += len;
3563 }
3564 STRCPY(i_str, i_name);
3565 trans_characters(i_str, IOSIZE);
3566 }
3567 }
3568
3569 mustset |= ti_change(i_str, &lasticon);
3570
3571 if (mustset)
3572 resettitle();
3573}
3574
3575/*
3576 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3577 * from "str" if it does.
3578 * Return TRUE when "*last" changed.
3579 */
3580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003581ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
3583 if ((str == NULL) != (*last == NULL)
3584 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3585 {
3586 vim_free(*last);
3587 if (str == NULL)
3588 *last = NULL;
3589 else
3590 *last = vim_strsave(str);
3591 return TRUE;
3592 }
3593 return FALSE;
3594}
3595
3596/*
3597 * Put current window title back (used after calling a shell)
3598 */
3599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003600resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601{
3602 mch_settitle(lasttitle, lasticon);
3603}
Bram Moolenaarea408852005-06-25 22:49:46 +00003604
3605# if defined(EXITFREE) || defined(PROTO)
3606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003607free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003608{
3609 vim_free(lasttitle);
3610 vim_free(lasticon);
3611}
3612# endif
3613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614#endif /* FEAT_TITLE */
3615
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003616#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003618 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 * Return length of string in screen cells.
3620 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003621 * Normally works for window "wp", except when working for 'tabline' then it
3622 * is "curwin".
3623 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 * Items are drawn interspersed with the text that surrounds it
3625 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3626 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3627 *
3628 * If maxwidth is not zero, the string will be filled at any middle marker
3629 * or truncated if too long, fillchar is used for all whitespace.
3630 */
3631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632build_stl_str_hl(
3633 win_T *wp,
3634 char_u *out, /* buffer to write into != NameBuff */
3635 size_t outlen, /* length of out[] */
3636 char_u *fmt,
3637 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3638 int fillchar,
3639 int maxwidth,
3640 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3641 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 char_u *p;
3644 char_u *s;
3645 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003646 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647#ifdef FEAT_EVAL
3648 win_T *o_curwin;
3649 buf_T *o_curbuf;
3650#endif
3651 int empty_line;
3652 colnr_T virtcol;
3653 long l;
3654 long n;
3655 int prevchar_isflag;
3656 int prevchar_isitem;
3657 int itemisflag;
3658 int fillable;
3659 char_u *str;
3660 long num;
3661 int width;
3662 int itemcnt;
3663 int curitem;
3664 int groupitem[STL_MAX_ITEM];
3665 int groupdepth;
3666 struct stl_item
3667 {
3668 char_u *start;
3669 int minwid;
3670 int maxwid;
3671 enum
3672 {
3673 Normal,
3674 Empty,
3675 Group,
3676 Middle,
3677 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003678 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 Trunc
3680 } type;
3681 } item[STL_MAX_ITEM];
3682 int minwid;
3683 int maxwid;
3684 int zeropad;
3685 char_u base;
3686 char_u opt;
3687#define TMPLEN 70
3688 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003689 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003690 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003691
3692#ifdef FEAT_EVAL
3693 /*
3694 * When the format starts with "%!" then evaluate it as an expression and
3695 * use the result as the actual format string.
3696 */
3697 if (fmt[0] == '%' && fmt[1] == '!')
3698 {
3699 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3700 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003701 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003702 }
3703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 if (fillchar == 0)
3706 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003707#ifdef FEAT_MBYTE
3708 /* Can't handle a multi-byte fill character yet. */
3709 else if (mb_char2len(fillchar) > 1)
3710 fillchar = '-';
3711#endif
3712
Bram Moolenaar567199b2013-04-24 16:52:36 +02003713 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3714 * p will become invalid when getting another buffer line. */
3715 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3716 empty_line = (*p == NUL);
3717
3718 /* Get the byte value now, in case we need it below. This is more
3719 * efficient than making a copy of the line. */
3720 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3721 byteval = 0;
3722 else
3723#ifdef FEAT_MBYTE
3724 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3725#else
3726 byteval = p[wp->w_cursor.col];
3727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 groupdepth = 0;
3730 p = out;
3731 curitem = 0;
3732 prevchar_isflag = TRUE;
3733 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003734 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003736 if (curitem == STL_MAX_ITEM)
3737 {
3738 /* There are too many items. Add the error code to the statusline
3739 * to give the user a hint about what went wrong. */
3740 if (p + 6 < out + outlen)
3741 {
3742 mch_memmove(p, " E541", (size_t)5);
3743 p += 5;
3744 }
3745 break;
3746 }
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (*s != NUL && *s != '%')
3749 prevchar_isflag = prevchar_isitem = FALSE;
3750
3751 /*
3752 * Handle up to the next '%' or the end.
3753 */
3754 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3755 *p++ = *s++;
3756 if (*s == NUL || p + 1 >= out + outlen)
3757 break;
3758
3759 /*
3760 * Handle one '%' item.
3761 */
3762 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003763 if (*s == NUL) /* ignore trailing % */
3764 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (*s == '%')
3766 {
3767 if (p + 1 >= out + outlen)
3768 break;
3769 *p++ = *s++;
3770 prevchar_isflag = prevchar_isitem = FALSE;
3771 continue;
3772 }
3773 if (*s == STL_MIDDLEMARK)
3774 {
3775 s++;
3776 if (groupdepth > 0)
3777 continue;
3778 item[curitem].type = Middle;
3779 item[curitem++].start = p;
3780 continue;
3781 }
3782 if (*s == STL_TRUNCMARK)
3783 {
3784 s++;
3785 item[curitem].type = Trunc;
3786 item[curitem++].start = p;
3787 continue;
3788 }
3789 if (*s == ')')
3790 {
3791 s++;
3792 if (groupdepth < 1)
3793 continue;
3794 groupdepth--;
3795
3796 t = item[groupitem[groupdepth]].start;
3797 *p = NUL;
3798 l = vim_strsize(t);
3799 if (curitem > groupitem[groupdepth] + 1
3800 && item[groupitem[groupdepth]].minwid == 0)
3801 {
3802 /* remove group if all items are empty */
3803 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003804 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 break;
3806 if (n == curitem)
3807 {
3808 p = t;
3809 l = 0;
3810 }
3811 }
3812 if (l > item[groupitem[groupdepth]].maxwid)
3813 {
3814 /* truncate, remove n bytes of text at the start */
3815#ifdef FEAT_MBYTE
3816 if (has_mbyte)
3817 {
3818 /* Find the first character that should be included. */
3819 n = 0;
3820 while (l >= item[groupitem[groupdepth]].maxwid)
3821 {
3822 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003823 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825 }
3826 else
3827#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003828 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003831 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 p = p - n + 1;
3833#ifdef FEAT_MBYTE
3834 /* Fill up space left over by half a double-wide char. */
3835 while (++l < item[groupitem[groupdepth]].minwid)
3836 *p++ = fillchar;
3837#endif
3838
3839 /* correct the start of the items for the truncation */
3840 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3841 {
3842 item[l].start -= n;
3843 if (item[l].start < t)
3844 item[l].start = t;
3845 }
3846 }
3847 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3848 {
3849 /* fill */
3850 n = item[groupitem[groupdepth]].minwid;
3851 if (n < 0)
3852 {
3853 /* fill by appending characters */
3854 n = 0 - n;
3855 while (l++ < n && p + 1 < out + outlen)
3856 *p++ = fillchar;
3857 }
3858 else
3859 {
3860 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003861 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 l = n - l;
3863 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003864 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 p += l;
3866 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3867 item[n].start += l;
3868 for ( ; l > 0; l--)
3869 *t++ = fillchar;
3870 }
3871 }
3872 continue;
3873 }
3874 minwid = 0;
3875 maxwid = 9999;
3876 zeropad = FALSE;
3877 l = 1;
3878 if (*s == '0')
3879 {
3880 s++;
3881 zeropad = TRUE;
3882 }
3883 if (*s == '-')
3884 {
3885 s++;
3886 l = -1;
3887 }
3888 if (VIM_ISDIGIT(*s))
3889 {
3890 minwid = (int)getdigits(&s);
3891 if (minwid < 0) /* overflow */
3892 minwid = 0;
3893 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003894 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
3896 item[curitem].type = Highlight;
3897 item[curitem].start = p;
3898 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3899 s++;
3900 curitem++;
3901 continue;
3902 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003903 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3904 {
3905 if (*s == STL_TABCLOSENR)
3906 {
3907 if (minwid == 0)
3908 {
3909 /* %X ends the close label, go back to the previously
3910 * define tab label nr. */
3911 for (n = curitem - 1; n >= 0; --n)
3912 if (item[n].type == TabPage && item[n].minwid >= 0)
3913 {
3914 minwid = item[n].minwid;
3915 break;
3916 }
3917 }
3918 else
3919 /* close nrs are stored as negative values */
3920 minwid = - minwid;
3921 }
3922 item[curitem].type = TabPage;
3923 item[curitem].start = p;
3924 item[curitem].minwid = minwid;
3925 s++;
3926 curitem++;
3927 continue;
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (*s == '.')
3930 {
3931 s++;
3932 if (VIM_ISDIGIT(*s))
3933 {
3934 maxwid = (int)getdigits(&s);
3935 if (maxwid <= 0) /* overflow */
3936 maxwid = 50;
3937 }
3938 }
3939 minwid = (minwid > 50 ? 50 : minwid) * l;
3940 if (*s == '(')
3941 {
3942 groupitem[groupdepth++] = curitem;
3943 item[curitem].type = Group;
3944 item[curitem].start = p;
3945 item[curitem].minwid = minwid;
3946 item[curitem].maxwid = maxwid;
3947 s++;
3948 curitem++;
3949 continue;
3950 }
3951 if (vim_strchr(STL_ALL, *s) == NULL)
3952 {
3953 s++;
3954 continue;
3955 }
3956 opt = *s++;
3957
3958 /* OK - now for the real work */
3959 base = 'D';
3960 itemisflag = FALSE;
3961 fillable = TRUE;
3962 num = -1;
3963 str = NULL;
3964 switch (opt)
3965 {
3966 case STL_FILEPATH:
3967 case STL_FULLPATH:
3968 case STL_FILENAME:
3969 fillable = FALSE; /* don't change ' ' to fillchar */
3970 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003971 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 else
3973 {
3974 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003975 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3977 }
3978 trans_characters(NameBuff, MAXPATHL);
3979 if (opt != STL_FILENAME)
3980 str = NameBuff;
3981 else
3982 str = gettail(NameBuff);
3983 break;
3984
3985 case STL_VIM_EXPR: /* '{' */
3986 itemisflag = TRUE;
3987 t = p;
3988 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3989 *p++ = *s++;
3990 if (*s != '}') /* missing '}' or out of space */
3991 break;
3992 s++;
3993 *p = 0;
3994 p = t;
3995
3996#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003997 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3999
4000 o_curbuf = curbuf;
4001 o_curwin = curwin;
4002 curwin = wp;
4003 curbuf = wp->w_buffer;
4004
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004005 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007 curwin = o_curwin;
4008 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004009 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011 if (str != NULL && *str != 0)
4012 {
4013 if (*skipdigits(str) == NUL)
4014 {
4015 num = atoi((char *)str);
4016 vim_free(str);
4017 str = NULL;
4018 itemisflag = FALSE;
4019 }
4020 }
4021#endif
4022 break;
4023
4024 case STL_LINE:
4025 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4026 ? 0L : (long)(wp->w_cursor.lnum);
4027 break;
4028
4029 case STL_NUMLINES:
4030 num = wp->w_buffer->b_ml.ml_line_count;
4031 break;
4032
4033 case STL_COLUMN:
4034 num = !(State & INSERT) && empty_line
4035 ? 0 : (int)wp->w_cursor.col + 1;
4036 break;
4037
4038 case STL_VIRTCOL:
4039 case STL_VIRTCOL_ALT:
4040 /* In list mode virtcol needs to be recomputed */
4041 virtcol = wp->w_virtcol;
4042 if (wp->w_p_list && lcs_tab1 == NUL)
4043 {
4044 wp->w_p_list = FALSE;
4045 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4046 wp->w_p_list = TRUE;
4047 }
4048 ++virtcol;
4049 /* Don't display %V if it's the same as %c. */
4050 if (opt == STL_VIRTCOL_ALT
4051 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4052 ? 0 : (int)wp->w_cursor.col + 1)))
4053 break;
4054 num = (long)virtcol;
4055 break;
4056
4057 case STL_PERCENTAGE:
4058 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4059 (long)wp->w_buffer->b_ml.ml_line_count);
4060 break;
4061
4062 case STL_ALTPERCENT:
4063 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004064 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 break;
4066
4067 case STL_ARGLISTSTAT:
4068 fillable = FALSE;
4069 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004070 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 str = tmp;
4072 break;
4073
4074 case STL_KEYMAP:
4075 fillable = FALSE;
4076 if (get_keymap_str(wp, tmp, TMPLEN))
4077 str = tmp;
4078 break;
4079 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004080#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004081 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082#else
4083 num = 0;
4084#endif
4085 break;
4086
4087 case STL_BUFNO:
4088 num = wp->w_buffer->b_fnum;
4089 break;
4090
4091 case STL_OFFSET_X:
4092 base = 'X';
4093 case STL_OFFSET:
4094#ifdef FEAT_BYTEOFF
4095 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4096 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4097 0L : l + 1 + (!(State & INSERT) && empty_line ?
4098 0 : (int)wp->w_cursor.col);
4099#endif
4100 break;
4101
4102 case STL_BYTEVAL_X:
4103 base = 'X';
4104 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004105 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (num == NL)
4107 num = 0;
4108 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4109 num = NL;
4110 break;
4111
4112 case STL_ROFLAG:
4113 case STL_ROFLAG_ALT:
4114 itemisflag = TRUE;
4115 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004116 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 break;
4118
4119 case STL_HELPFLAG:
4120 case STL_HELPFLAG_ALT:
4121 itemisflag = TRUE;
4122 if (wp->w_buffer->b_help)
4123 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004124 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 break;
4126
4127#ifdef FEAT_AUTOCMD
4128 case STL_FILETYPE:
4129 if (*wp->w_buffer->b_p_ft != NUL
4130 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4131 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004132 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4133 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 str = tmp;
4135 }
4136 break;
4137
4138 case STL_FILETYPE_ALT:
4139 itemisflag = TRUE;
4140 if (*wp->w_buffer->b_p_ft != NUL
4141 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4142 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004143 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4144 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 for (t = tmp; *t != 0; t++)
4146 *t = TOUPPER_LOC(*t);
4147 str = tmp;
4148 }
4149 break;
4150#endif
4151
4152#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4153 case STL_PREVIEWFLAG:
4154 case STL_PREVIEWFLAG_ALT:
4155 itemisflag = TRUE;
4156 if (wp->w_p_pvw)
4157 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4158 : _("[Preview]"));
4159 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004160
4161 case STL_QUICKFIX:
4162 if (bt_quickfix(wp->w_buffer))
4163 str = (char_u *)(wp->w_llist_ref
4164 ? _(msg_loclist)
4165 : _(msg_qflist));
4166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167#endif
4168
4169 case STL_MODIFIED:
4170 case STL_MODIFIED_ALT:
4171 itemisflag = TRUE;
4172 switch ((opt == STL_MODIFIED_ALT)
4173 + bufIsChanged(wp->w_buffer) * 2
4174 + (!wp->w_buffer->b_p_ma) * 4)
4175 {
4176 case 2: str = (char_u *)"[+]"; break;
4177 case 3: str = (char_u *)",+"; break;
4178 case 4: str = (char_u *)"[-]"; break;
4179 case 5: str = (char_u *)",-"; break;
4180 case 6: str = (char_u *)"[+-]"; break;
4181 case 7: str = (char_u *)",+-"; break;
4182 }
4183 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004184
4185 case STL_HIGHLIGHT:
4186 t = s;
4187 while (*s != '#' && *s != NUL)
4188 ++s;
4189 if (*s == '#')
4190 {
4191 item[curitem].type = Highlight;
4192 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004193 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004194 curitem++;
4195 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004196 if (*s != NUL)
4197 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004198 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200
4201 item[curitem].start = p;
4202 item[curitem].type = Normal;
4203 if (str != NULL && *str)
4204 {
4205 t = str;
4206 if (itemisflag)
4207 {
4208 if ((t[0] && t[1])
4209 && ((!prevchar_isitem && *t == ',')
4210 || (prevchar_isflag && *t == ' ')))
4211 t++;
4212 prevchar_isflag = TRUE;
4213 }
4214 l = vim_strsize(t);
4215 if (l > 0)
4216 prevchar_isitem = TRUE;
4217 if (l > maxwid)
4218 {
4219 while (l >= maxwid)
4220#ifdef FEAT_MBYTE
4221 if (has_mbyte)
4222 {
4223 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004224 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 else
4227#endif
4228 l -= byte2cells(*t++);
4229 if (p + 1 >= out + outlen)
4230 break;
4231 *p++ = '<';
4232 }
4233 if (minwid > 0)
4234 {
4235 for (; l < minwid && p + 1 < out + outlen; l++)
4236 {
4237 /* Don't put a "-" in front of a digit. */
4238 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4239 *p++ = ' ';
4240 else
4241 *p++ = fillchar;
4242 }
4243 minwid = 0;
4244 }
4245 else
4246 minwid *= -1;
4247 while (*t && p + 1 < out + outlen)
4248 {
4249 *p++ = *t++;
4250 /* Change a space by fillchar, unless fillchar is '-' and a
4251 * digit follows. */
4252 if (fillable && p[-1] == ' '
4253 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4254 p[-1] = fillchar;
4255 }
4256 for (; l < minwid && p + 1 < out + outlen; l++)
4257 *p++ = fillchar;
4258 }
4259 else if (num >= 0)
4260 {
4261 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4262 char_u nstr[20];
4263
4264 if (p + 20 >= out + outlen)
4265 break; /* not sufficient space */
4266 prevchar_isitem = TRUE;
4267 t = nstr;
4268 if (opt == STL_VIRTCOL_ALT)
4269 {
4270 *t++ = '-';
4271 minwid--;
4272 }
4273 *t++ = '%';
4274 if (zeropad)
4275 *t++ = '0';
4276 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004277 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 *t = 0;
4279
4280 for (n = num, l = 1; n >= nbase; n /= nbase)
4281 l++;
4282 if (opt == STL_VIRTCOL_ALT)
4283 l++;
4284 if (l > maxwid)
4285 {
4286 l += 2;
4287 n = l - maxwid;
4288 while (l-- > maxwid)
4289 num /= nbase;
4290 *t++ = '>';
4291 *t++ = '%';
4292 *t = t[-3];
4293 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004294 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4295 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004298 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4299 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 p += STRLEN(p);
4301 }
4302 else
4303 item[curitem].type = Empty;
4304
4305 if (opt == STL_VIM_EXPR)
4306 vim_free(str);
4307
4308 if (num >= 0 || (!itemisflag && str && *str))
4309 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4310 curitem++;
4311 }
4312 *p = NUL;
4313 itemcnt = curitem;
4314
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004315#ifdef FEAT_EVAL
4316 if (usefmt != fmt)
4317 vim_free(usefmt);
4318#endif
4319
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 width = vim_strsize(out);
4321 if (maxwidth > 0 && width > maxwidth)
4322 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004323 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 l = 0;
4325 if (itemcnt == 0)
4326 s = out;
4327 else
4328 {
4329 for ( ; l < itemcnt; l++)
4330 if (item[l].type == Trunc)
4331 {
4332 /* Truncate at %< item. */
4333 s = item[l].start;
4334 break;
4335 }
4336 if (l == itemcnt)
4337 {
4338 /* No %< item, truncate first item. */
4339 s = item[0].start;
4340 l = 0;
4341 }
4342 }
4343
4344 if (width - vim_strsize(s) >= maxwidth)
4345 {
4346 /* Truncation mark is beyond max length */
4347#ifdef FEAT_MBYTE
4348 if (has_mbyte)
4349 {
4350 s = out;
4351 width = 0;
4352 for (;;)
4353 {
4354 width += ptr2cells(s);
4355 if (width >= maxwidth)
4356 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004357 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 /* Fill up for half a double-wide character. */
4360 while (++width < maxwidth)
4361 *s++ = fillchar;
4362 }
4363 else
4364#endif
4365 s = out + maxwidth - 1;
4366 for (l = 0; l < itemcnt; l++)
4367 if (item[l].start > s)
4368 break;
4369 itemcnt = l;
4370 *s++ = '>';
4371 *s = 0;
4372 }
4373 else
4374 {
4375#ifdef FEAT_MBYTE
4376 if (has_mbyte)
4377 {
4378 n = 0;
4379 while (width >= maxwidth)
4380 {
4381 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004382 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 }
4385 else
4386#endif
4387 n = width - maxwidth + 1;
4388 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004389 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 *s = '<';
4391
4392 /* Fill up for half a double-wide character. */
4393 while (++width < maxwidth)
4394 {
4395 s = s + STRLEN(s);
4396 *s++ = fillchar;
4397 *s = NUL;
4398 }
4399
4400 --n; /* count the '<' */
4401 for (; l < itemcnt; l++)
4402 {
4403 if (item[l].start - n >= s)
4404 item[l].start -= n;
4405 else
4406 item[l].start = s;
4407 }
4408 }
4409 width = maxwidth;
4410 }
4411 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4412 {
4413 /* Apply STL_MIDDLE if any */
4414 for (l = 0; l < itemcnt; l++)
4415 if (item[l].type == Middle)
4416 break;
4417 if (l < itemcnt)
4418 {
4419 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004420 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 for (s = item[l].start; s < p; s++)
4422 *s = fillchar;
4423 for (l++; l < itemcnt; l++)
4424 item[l].start += maxwidth - width;
4425 width = maxwidth;
4426 }
4427 }
4428
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004429 /* Store the info about highlighting. */
4430 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004432 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 for (l = 0; l < itemcnt; l++)
4434 {
4435 if (item[l].type == Highlight)
4436 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004437 sp->start = item[l].start;
4438 sp->userhl = item[l].minwid;
4439 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004442 sp->start = NULL;
4443 sp->userhl = 0;
4444 }
4445
4446 /* Store the info about tab pages labels. */
4447 if (tabtab != NULL)
4448 {
4449 sp = tabtab;
4450 for (l = 0; l < itemcnt; l++)
4451 {
4452 if (item[l].type == TabPage)
4453 {
4454 sp->start = item[l].start;
4455 sp->userhl = item[l].minwid;
4456 sp++;
4457 }
4458 }
4459 sp->start = NULL;
4460 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462
4463 return width;
4464}
4465#endif /* FEAT_STL_OPT */
4466
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004467#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4468 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004470 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4471 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 */
4473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004474get_rel_pos(
4475 win_T *wp,
4476 char_u *buf,
4477 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
4479 long above; /* number of lines above window */
4480 long below; /* number of lines below window */
4481
Bram Moolenaar0027c212015-01-07 13:31:52 +01004482 if (buflen < 3) /* need at least 3 chars for writing */
4483 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 above = wp->w_topline - 1;
4485#ifdef FEAT_DIFF
4486 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004487 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4488 above = 0; /* All buffer lines are displayed and there is an
4489 * indication of filler lines, that can be considered
4490 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491#endif
4492 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4493 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004494 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4495 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004497 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004499 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 ? (int)(above / ((above + below) / 100L))
4501 : (int)(above * 100L / (above + below)));
4502}
4503#endif
4504
4505/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004506 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 * Return TRUE if it was appended.
4508 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004509 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004510append_arg_number(
4511 win_T *wp,
4512 char_u *buf,
4513 int buflen,
4514 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
4516 char_u *p;
4517
4518 if (ARGCOUNT <= 1) /* nothing to do */
4519 return FALSE;
4520
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004521 p = buf + STRLEN(buf); /* go to the end of the buffer */
4522 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 return FALSE;
4524 *p++ = ' ';
4525 *p++ = '(';
4526 if (add_file)
4527 {
4528 STRCPY(p, "file ");
4529 p += 5;
4530 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004531 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4532 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4534 return TRUE;
4535}
4536
4537/*
4538 * If fname is not a full path, make it a full path.
4539 * Returns pointer to allocated memory (NULL for failure).
4540 */
4541 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004542fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543{
4544 /*
4545 * Force expanding the path always for Unix, because symbolic links may
4546 * mess up the full path name, even though it starts with a '/'.
4547 * Also expand when there is ".." in the file name, try to remove it,
4548 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004549 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 * For MS-Windows also expand names like "longna~1" to "longname".
4551 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004552#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 return FullName_save(fname, TRUE);
4554#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004555 if (!vim_isAbsName(fname)
4556 || strstr((char *)fname, "..") != NULL
4557 || strstr((char *)fname, "//") != NULL
4558# ifdef BACKSLASH_IN_FILENAME
4559 || strstr((char *)fname, "\\\\") != NULL
4560# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004561# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 )
4565 return FullName_save(fname, FALSE);
4566
4567 fname = vim_strsave(fname);
4568
Bram Moolenaar9b942202007-10-03 12:31:33 +00004569# ifdef USE_FNAME_CASE
4570# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
4574 if (fname != NULL)
4575 fname_case(fname, 0); /* set correct case for file name */
4576 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
4579 return fname;
4580#endif
4581}
4582
4583/*
4584 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4585 * "ffname" becomes a pointer to allocated memory (or NULL).
4586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004588fname_expand(
4589 buf_T *buf UNUSED,
4590 char_u **ffname,
4591 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592{
4593 if (*ffname == NULL) /* if no file name given, nothing to do */
4594 return;
4595 if (*sfname == NULL) /* if no short file name given, use ffname */
4596 *sfname = *ffname;
4597 *ffname = fix_fname(*ffname); /* expand to full path */
4598
4599#ifdef FEAT_SHORTCUT
4600 if (!buf->b_p_bin)
4601 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004602 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604 /* If the file name is a shortcut file, use the file it links to. */
4605 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004606 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 vim_free(*ffname);
4609 *ffname = rfname;
4610 *sfname = rfname;
4611 }
4612 }
4613#endif
4614}
4615
4616/*
4617 * Get the file name for an argument list entry.
4618 */
4619 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004620alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621{
4622 buf_T *bp;
4623
4624 /* Use the name from the associated buffer if it exists. */
4625 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004626 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 return aep->ae_fname;
4628 return bp->b_fname;
4629}
4630
4631#if defined(FEAT_WINDOWS) || defined(PROTO)
4632/*
4633 * do_arg_all(): Open up to 'count' windows, one for each argument.
4634 */
4635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004636do_arg_all(
4637 int count,
4638 int forceit, /* hide buffers in current windows */
4639 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 int i;
4642 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004643 char_u *opened; /* Array of weight for which args are open:
4644 * 0: not opened
4645 * 1: opened in other tab
4646 * 2: opened in curtab
4647 * 3: opened in curtab and curwin
4648 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004649 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 int use_firstwin = FALSE; /* use first window for arglist */
4651 int split_ret = OK;
4652 int p_ea_save;
4653 alist_T *alist; /* argument list to be used */
4654 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004655 tabpage_T *tpnext;
4656 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004657 win_T *old_curwin, *last_curwin;
4658 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004659 win_T *new_curwin = NULL;
4660 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 if (ARGCOUNT <= 0)
4663 {
4664 /* Don't give an error message. We don't want it when the ":all"
4665 * command is in the .vimrc. */
4666 return;
4667 }
4668 setpcmark();
4669
4670 opened_len = ARGCOUNT;
4671 opened = alloc_clear((unsigned)opened_len);
4672 if (opened == NULL)
4673 return;
4674
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004675 /* Autocommands may do anything to the argument list. Make sure it's not
4676 * freed while we are working here by "locking" it. We still have to
4677 * watch out for its size to be changed. */
4678 alist = curwin->w_alist;
4679 ++alist->al_refcount;
4680
4681 old_curwin = curwin;
4682 old_curtab = curtab;
4683
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004684# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688 /*
4689 * Try closing all windows that are not in the argument list.
4690 * Also close windows that are not full width;
4691 * When 'hidden' or "forceit" set the buffer becomes hidden.
4692 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004693 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004695 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004696 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004697 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004699 tpnext = curtab->tp_next;
4700 for (wp = firstwin; wp != NULL; wp = wpnext)
4701 {
4702 wpnext = wp->w_next;
4703 buf = wp->w_buffer;
4704 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004705 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004706 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004707 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004708 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004710 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004711 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004713 if (i < alist->al_ga.ga_len
4714 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4715 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4716 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004718 int weight = 1;
4719
4720 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004721 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004722 ++weight;
4723 if (old_curwin == wp)
4724 ++weight;
4725 }
4726
4727 if (weight > (int)opened[i])
4728 {
4729 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004730 if (i == 0)
4731 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004732 if (new_curwin != NULL)
4733 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004734 new_curwin = wp;
4735 new_curtab = curtab;
4736 }
4737 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004738 else if (keep_tabs)
4739 i = opened_len;
4740
4741 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004742 {
4743 /* Use the current argument list for all windows
4744 * containing a file from it. */
4745 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004746 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004747 ++wp->w_alist->al_refcount;
4748 }
4749 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004753 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004755 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004757 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004758 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004760 /* If the buffer was changed, and we would like to hide it,
4761 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004762 if (!P_HID(buf) && buf->b_nwindows <= 1
4763 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004765#ifdef FEAT_AUTOCMD
4766 bufref_T bufref;
4767
4768 set_bufref(&bufref, buf);
4769#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004770 (void)autowrite(buf, FALSE);
4771#ifdef FEAT_AUTOCMD
4772 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004773 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004774 {
4775 wpnext = firstwin; /* start all over... */
4776 continue;
4777 }
4778#endif
4779 }
4780#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004781 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004782 if (firstwin == lastwin
4783 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004784#endif
4785 use_firstwin = TRUE;
4786#ifdef FEAT_WINDOWS
4787 else
4788 {
4789 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4790# ifdef FEAT_AUTOCMD
4791 /* check if autocommands removed the next window */
4792 if (!win_valid(wpnext))
4793 wpnext = firstwin; /* start all over... */
4794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796#endif
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 }
4799 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004800
4801 /* Without the ":tab" modifier only do the current tab page. */
4802 if (had_tab == 0 || tpnext == NULL)
4803 break;
4804
4805# ifdef FEAT_AUTOCMD
4806 /* check if autocommands removed the next tab page */
4807 if (!valid_tabpage(tpnext))
4808 tpnext = first_tabpage; /* start all over...*/
4809# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004810 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812
4813 /*
4814 * Open a window for files in the argument list that don't have one.
4815 * ARGCOUNT may change while doing this, because of autocommands.
4816 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004817 if (count > opened_len || count <= 0)
4818 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820#ifdef FEAT_AUTOCMD
4821 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4822 ++autocmd_no_enter;
4823 ++autocmd_no_leave;
4824#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004825 last_curwin = curwin;
4826 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004828#ifdef FEAT_WINDOWS
4829 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4830 * leaving an empty tab page when executed locally. */
4831 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4832 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4833 use_firstwin = TRUE;
4834#endif
4835
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004836 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 {
4838 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4839 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004840 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 /* Move the already present window to below the current window */
4843 if (curwin->w_arg_idx != i)
4844 {
4845 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4846 {
4847 if (wpnext->w_arg_idx == i)
4848 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004849 if (keep_tabs)
4850 {
4851 new_curwin = wpnext;
4852 new_curtab = curtab;
4853 }
4854 else
4855 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 break;
4857 }
4858 }
4859 }
4860 }
4861 else if (split_ret == OK)
4862 {
4863 if (!use_firstwin) /* split current window */
4864 {
4865 p_ea_save = p_ea;
4866 p_ea = TRUE; /* use space from all windows */
4867 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4868 p_ea = p_ea_save;
4869 if (split_ret == FAIL)
4870 continue;
4871 }
4872#ifdef FEAT_AUTOCMD
4873 else /* first window: do autocmd for leaving this buffer */
4874 --autocmd_no_leave;
4875#endif
4876
4877 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004878 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 */
4880 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004881 if (i == 0)
4882 {
4883 new_curwin = curwin;
4884 new_curtab = curtab;
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4887 ECMD_ONE,
4888 ((P_HID(curwin->w_buffer)
4889 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004890 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#ifdef FEAT_AUTOCMD
4892 if (use_firstwin)
4893 ++autocmd_no_leave;
4894#endif
4895 use_firstwin = FALSE;
4896 }
4897 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004898
4899 /* When ":tab" was used open a new tab for a new window repeatedly. */
4900 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4901 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903
4904 /* Remove the "lock" on the argument list. */
4905 alist_unlink(alist);
4906
4907#ifdef FEAT_AUTOCMD
4908 --autocmd_no_enter;
4909#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004910 /* restore last referenced tabpage's curwin */
4911 if (last_curtab != new_curtab)
4912 {
4913 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004914 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004915 if (win_valid(last_curwin))
4916 win_enter(last_curwin, FALSE);
4917 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004918 /* to window with first arg */
4919 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004920 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004921 if (win_valid(new_curwin))
4922 win_enter(new_curwin, FALSE);
4923
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924#ifdef FEAT_AUTOCMD
4925 --autocmd_no_leave;
4926#endif
4927 vim_free(opened);
4928}
4929
4930# if defined(FEAT_LISTCMDS) || defined(PROTO)
4931/*
4932 * Open a window for a number of buffers.
4933 */
4934 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004935ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 buf_T *buf;
4938 win_T *wp, *wpnext;
4939 int split_ret = OK;
4940 int p_ea_save;
4941 int open_wins = 0;
4942 int r;
4943 int count; /* Maximum number of windows to open. */
4944 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004945#ifdef FEAT_WINDOWS
4946 int had_tab = cmdmod.tab;
4947 tabpage_T *tpnext;
4948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 if (eap->addr_count == 0) /* make as many windows as possible */
4951 count = 9999;
4952 else
4953 count = eap->line2; /* make as many windows as specified */
4954 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4955 all = FALSE;
4956 else
4957 all = TRUE;
4958
4959 setpcmark();
4960
4961#ifdef FEAT_GUI
4962 need_mouse_correct = TRUE;
4963#endif
4964
4965 /*
4966 * Close superfluous windows (two windows for the same buffer).
4967 * Also close windows that are not full-width.
4968 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004969#ifdef FEAT_WINDOWS
4970 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004971 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004972 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004975 tpnext = curtab->tp_next;
4976 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004978 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004979 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004980#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004981 || ((cmdmod.split & WSP_VERT)
4982 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004983 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004984 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004985 || (had_tab > 0 && wp != firstwin)
4986#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004987 ) && firstwin != lastwin
4988#ifdef FEAT_AUTOCMD
4989 && !(wp->w_closing || wp->w_buffer->b_closing)
4990#endif
4991 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004992 {
4993 win_close(wp, FALSE);
4994#ifdef FEAT_AUTOCMD
4995 wpnext = firstwin; /* just in case an autocommand does
4996 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004997 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004998 open_wins = 0;
4999#endif
5000 }
5001 else
5002 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005004
5005#ifdef FEAT_WINDOWS
5006 /* Without the ":tab" modifier only do the current tab page. */
5007 if (had_tab == 0 || tpnext == NULL)
5008 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005009 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013 /*
5014 * Go through the buffer list. When a buffer doesn't have a window yet,
5015 * open one. Otherwise move the window to the right position.
5016 * Watch out for autocommands that delete buffers or windows!
5017 */
5018#ifdef FEAT_AUTOCMD
5019 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5020 ++autocmd_no_enter;
5021#endif
5022 win_enter(lastwin, FALSE);
5023#ifdef FEAT_AUTOCMD
5024 ++autocmd_no_leave;
5025#endif
5026 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5027 {
5028 /* Check if this buffer needs a window */
5029 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5030 continue;
5031
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005032#ifdef FEAT_WINDOWS
5033 if (had_tab != 0)
5034 {
5035 /* With the ":tab" modifier don't move the window. */
5036 if (buf->b_nwindows > 0)
5037 wp = lastwin; /* buffer has a window, skip it */
5038 else
5039 wp = NULL;
5040 }
5041 else
5042#endif
5043 {
5044 /* Check if this buffer already has a window */
5045 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5046 if (wp->w_buffer == buf)
5047 break;
5048 /* If the buffer already has a window, move it */
5049 if (wp != NULL)
5050 win_move_after(wp, curwin);
5051 }
5052
5053 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005055#ifdef FEAT_AUTOCMD
5056 bufref_T bufref;
5057
5058 set_bufref(&bufref, buf);
5059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 /* Split the window and put the buffer in it */
5061 p_ea_save = p_ea;
5062 p_ea = TRUE; /* use space from all windows */
5063 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5064 ++open_wins;
5065 p_ea = p_ea_save;
5066 if (split_ret == FAIL)
5067 continue;
5068
5069 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005070#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 swap_exists_action = SEA_DIALOG;
5072#endif
5073 set_curbuf(buf, DOBUF_GOTO);
5074#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005075 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005077 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005078#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 break;
5082 }
5083#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005084#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (swap_exists_action == SEA_QUIT)
5086 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005087# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5088 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005089
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005090 /* Reset the error/interrupt/exception state here so that
5091 * aborting() returns FALSE when closing a window. */
5092 enter_cleanup(&cs);
5093# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005094
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005095 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 win_close(curwin, TRUE);
5097 --open_wins;
5098 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005099 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005100
5101# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5102 /* Restore the error/interrupt/exception state if not
5103 * discarded by a new aborting error, interrupt, or uncaught
5104 * exception. */
5105 leave_cleanup(&cs);
5106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 else
5109 handle_swap_exists(NULL);
5110#endif
5111 }
5112
5113 ui_breakcheck();
5114 if (got_int)
5115 {
5116 (void)vgetc(); /* only break the file loading, not the rest */
5117 break;
5118 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005119#ifdef FEAT_EVAL
5120 /* Autocommands deleted the buffer or aborted script processing!!! */
5121 if (aborting())
5122 break;
5123#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005124#ifdef FEAT_WINDOWS
5125 /* When ":tab" was used open a new tab for a new window repeatedly. */
5126 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5127 cmdmod.tab = 9999;
5128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130#ifdef FEAT_AUTOCMD
5131 --autocmd_no_enter;
5132#endif
5133 win_enter(firstwin, FALSE); /* back to first window */
5134#ifdef FEAT_AUTOCMD
5135 --autocmd_no_leave;
5136#endif
5137
5138 /*
5139 * Close superfluous windows.
5140 */
5141 for (wp = lastwin; open_wins > count; )
5142 {
5143 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5144 || autowrite(wp->w_buffer, FALSE) == OK);
5145#ifdef FEAT_AUTOCMD
5146 if (!win_valid(wp))
5147 {
5148 /* BufWrite Autocommands made the window invalid, start over */
5149 wp = lastwin;
5150 }
5151 else
5152#endif
5153 if (r)
5154 {
5155 win_close(wp, !P_HID(wp->w_buffer));
5156 --open_wins;
5157 wp = lastwin;
5158 }
5159 else
5160 {
5161 wp = wp->w_prev;
5162 if (wp == NULL)
5163 break;
5164 }
5165 }
5166}
5167# endif /* FEAT_LISTCMDS */
5168
5169#endif /* FEAT_WINDOWS */
5170
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005171static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005172
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173/*
5174 * do_modelines() - process mode lines for the current file
5175 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005176 * "flags" can be:
5177 * OPT_WINONLY only set options local to window
5178 * OPT_NOWIN don't set options local to window
5179 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 * Returns immediately if the "ml" option isn't set.
5181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005183do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005185 linenr_T lnum;
5186 int nmlines;
5187 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5190 return;
5191
5192 /* Disallow recursive entry here. Can happen when executing a modeline
5193 * triggers an autocommand, which reloads modelines with a ":do". */
5194 if (entered)
5195 return;
5196
5197 ++entered;
5198 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5199 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005200 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 nmlines = 0;
5202
5203 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5204 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005205 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 nmlines = 0;
5207 --entered;
5208}
5209
5210#include "version.h" /* for version number */
5211
5212/*
5213 * chk_modeline() - check a single line for a mode string
5214 * Return FAIL if an error encountered.
5215 */
5216 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005217chk_modeline(
5218 linenr_T lnum,
5219 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220{
5221 char_u *s;
5222 char_u *e;
5223 char_u *linecopy; /* local copy of any modeline found */
5224 int prev;
5225 int vers;
5226 int end;
5227 int retval = OK;
5228 char_u *save_sourcing_name;
5229 linenr_T save_sourcing_lnum;
5230#ifdef FEAT_EVAL
5231 scid_T save_SID;
5232#endif
5233
5234 prev = -1;
5235 for (s = ml_get(lnum); *s != NUL; ++s)
5236 {
5237 if (prev == -1 || vim_isspace(prev))
5238 {
5239 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5240 || STRNCMP(s, "vi:", (size_t)3) == 0)
5241 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005242 /* Accept both "vim" and "Vim". */
5243 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 {
5245 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5246 e = s + 4;
5247 else
5248 e = s + 3;
5249 vers = getdigits(&e);
5250 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005251 && (s[0] != 'V'
5252 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 && (s[3] == ':'
5254 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5255 || (VIM_VERSION_100 < vers && s[3] == '<')
5256 || (VIM_VERSION_100 > vers && s[3] == '>')
5257 || (VIM_VERSION_100 == vers && s[3] == '=')))
5258 break;
5259 }
5260 }
5261 prev = *s;
5262 }
5263
5264 if (*s)
5265 {
5266 do /* skip over "ex:", "vi:" or "vim:" */
5267 ++s;
5268 while (s[-1] != ':');
5269
5270 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5271 if (linecopy == NULL)
5272 return FAIL;
5273
5274 save_sourcing_lnum = sourcing_lnum;
5275 save_sourcing_name = sourcing_name;
5276 sourcing_lnum = lnum; /* prepare for emsg() */
5277 sourcing_name = (char_u *)"modelines";
5278
5279 end = FALSE;
5280 while (end == FALSE)
5281 {
5282 s = skipwhite(s);
5283 if (*s == NUL)
5284 break;
5285
5286 /*
5287 * Find end of set command: ':' or end of line.
5288 * Skip over "\:", replacing it with ":".
5289 */
5290 for (e = s; *e != ':' && *e != NUL; ++e)
5291 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005292 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 if (*e == NUL)
5294 end = TRUE;
5295
5296 /*
5297 * If there is a "set" command, require a terminating ':' and
5298 * ignore the stuff after the ':'.
5299 * "vi:set opt opt opt: foo" -- foo not interpreted
5300 * "vi:opt opt opt: foo" -- foo interpreted
5301 * Accept "se" for compatibility with Elvis.
5302 */
5303 if (STRNCMP(s, "set ", (size_t)4) == 0
5304 || STRNCMP(s, "se ", (size_t)3) == 0)
5305 {
5306 if (*e != ':') /* no terminating ':'? */
5307 break;
5308 end = TRUE;
5309 s = vim_strchr(s, ' ') + 1;
5310 }
5311 *e = NUL; /* truncate the set command */
5312
5313 if (*s != NUL) /* skip over an empty "::" */
5314 {
5315#ifdef FEAT_EVAL
5316 save_SID = current_SID;
5317 current_SID = SID_MODELINE;
5318#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005319 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#ifdef FEAT_EVAL
5321 current_SID = save_SID;
5322#endif
5323 if (retval == FAIL) /* stop if error found */
5324 break;
5325 }
5326 s = e + 1; /* advance to next part */
5327 }
5328
5329 sourcing_lnum = save_sourcing_lnum;
5330 sourcing_name = save_sourcing_name;
5331
5332 vim_free(linecopy);
5333 }
5334 return retval;
5335}
5336
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005337#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005339read_viminfo_bufferlist(
5340 vir_T *virp,
5341 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
5343 char_u *tab;
5344 linenr_T lnum;
5345 colnr_T col;
5346 buf_T *buf;
5347 char_u *sfname;
5348 char_u *xline;
5349
5350 /* Handle long line and escaped characters. */
5351 xline = viminfo_readstring(virp, 1, FALSE);
5352
5353 /* don't read in if there are files on the command-line or if writing: */
5354 if (xline != NULL && !writing && ARGCOUNT == 0
5355 && find_viminfo_parameter('%') != NULL)
5356 {
5357 /* Format is: <fname> Tab <lnum> Tab <col>.
5358 * Watch out for a Tab in the file name, work from the end. */
5359 lnum = 0;
5360 col = 0;
5361 tab = vim_strrchr(xline, '\t');
5362 if (tab != NULL)
5363 {
5364 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005365 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 tab = vim_strrchr(xline, '\t');
5367 if (tab != NULL)
5368 {
5369 *tab++ = '\0';
5370 lnum = atol((char *)tab);
5371 }
5372 }
5373
5374 /* Expand "~/" in the file name at "line + 1" to a full path.
5375 * Then try shortening it by comparing with the current directory */
5376 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005377 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
5379 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5380 if (buf != NULL) /* just in case... */
5381 {
5382 buf->b_last_cursor.lnum = lnum;
5383 buf->b_last_cursor.col = col;
5384 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5385 }
5386 }
5387 vim_free(xline);
5388
5389 return viminfo_readline(virp);
5390}
5391
5392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005393write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
5395 buf_T *buf;
5396#ifdef FEAT_WINDOWS
5397 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005398 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#endif
5400 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005401 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403 if (find_viminfo_parameter('%') == NULL)
5404 return;
5405
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005406 /* Without a number -1 is returned: do all buffers. */
5407 max_buffers = get_viminfo_parameter('%');
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005410#define LINE_BUF_LEN (MAXPATHL + 40)
5411 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (line == NULL)
5413 return;
5414
5415#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005416 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 set_last_cursor(win);
5418#else
5419 set_last_cursor(curwin);
5420#endif
5421
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005422 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5424 {
5425 if (buf->b_fname == NULL
5426 || !buf->b_p_bl
5427#ifdef FEAT_QUICKFIX
5428 || bt_quickfix(buf)
5429#endif
5430 || removable(buf->b_ffname))
5431 continue;
5432
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005433 if (max_buffers-- == 0)
5434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 putc('%', fp);
5436 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005437 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 (long)buf->b_last_cursor.lnum,
5439 buf->b_last_cursor.col);
5440 viminfo_writestring(fp, line);
5441 }
5442 vim_free(line);
5443}
5444#endif
5445
5446
5447/*
5448 * Return special buffer name.
5449 * Returns NULL when the buffer has a normal file name.
5450 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005451 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005452buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
5454#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5455 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005456 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005457 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005458 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005459
5460 /*
5461 * For location list window, w_llist_ref points to the location list.
5462 * For quickfix window, w_llist_ref is NULL.
5463 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005464 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005465 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005466 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005467 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469#endif
5470#ifdef FEAT_QUICKFIX
5471 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5472 * contains the name as specified by the user */
5473 if (bt_nofile(buf))
5474 {
5475 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005476 return buf->b_sfname;
5477 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 }
5479#endif
5480 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005481 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 return NULL;
5483}
5484
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005485#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5486 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5487 || defined(PROTO)
5488/*
5489 * Find a window for buffer "buf".
5490 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5491 * If not found FAIL is returned.
5492 */
5493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005494find_win_for_buf(
5495 buf_T *buf,
5496 win_T **wp,
5497 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005498{
5499 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5500 if ((*wp)->w_buffer == buf)
5501 goto win_found;
5502 return FAIL;
5503win_found:
5504 return OK;
5505}
5506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
5508#if defined(FEAT_SIGNS) || defined(PROTO)
5509/*
5510 * Insert the sign into the signlist.
5511 */
5512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005513insert_sign(
5514 buf_T *buf, /* buffer to store sign in */
5515 signlist_T *prev, /* previous sign entry */
5516 signlist_T *next, /* next sign entry */
5517 int id, /* sign ID */
5518 linenr_T lnum, /* line number which gets the mark */
5519 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 signlist_T *newsign;
5522
5523 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5524 if (newsign != NULL)
5525 {
5526 newsign->id = id;
5527 newsign->lnum = lnum;
5528 newsign->typenr = typenr;
5529 newsign->next = next;
5530#ifdef FEAT_NETBEANS_INTG
5531 newsign->prev = prev;
5532 if (next != NULL)
5533 next->prev = newsign;
5534#endif
5535
5536 if (prev == NULL)
5537 {
5538 /* When adding first sign need to redraw the windows to create the
5539 * column for signs. */
5540 if (buf->b_signlist == NULL)
5541 {
5542 redraw_buf_later(buf, NOT_VALID);
5543 changed_cline_bef_curs();
5544 }
5545
5546 /* first sign in signlist */
5547 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005548#ifdef FEAT_NETBEANS_INTG
5549 if (netbeans_active())
5550 buf->b_has_sign_column = TRUE;
5551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553 else
5554 prev->next = newsign;
5555 }
5556}
5557
5558/*
5559 * Add the sign into the signlist. Find the right spot to do it though.
5560 */
5561 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005562buf_addsign(
5563 buf_T *buf, /* buffer to store sign in */
5564 int id, /* sign ID */
5565 linenr_T lnum, /* line number which gets the mark */
5566 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568 signlist_T *sign; /* a sign in the signlist */
5569 signlist_T *prev; /* the previous sign */
5570
5571 prev = NULL;
5572 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5573 {
5574 if (lnum == sign->lnum && id == sign->id)
5575 {
5576 sign->typenr = typenr;
5577 return;
5578 }
5579 else if (
5580#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5581 id < 0 &&
5582#endif
5583 lnum < sign->lnum)
5584 {
5585#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5586 /* XXX - GRP: Is this because of sign slide problem? Or is it
5587 * really needed? Or is it because we allow multiple signs per
5588 * line? If so, should I add that feature to FEAT_SIGNS?
5589 */
5590 while (prev != NULL && prev->lnum == lnum)
5591 prev = prev->prev;
5592 if (prev == NULL)
5593 sign = buf->b_signlist;
5594 else
5595 sign = prev->next;
5596#endif
5597 insert_sign(buf, prev, sign, id, lnum, typenr);
5598 return;
5599 }
5600 prev = sign;
5601 }
5602#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5603 /* XXX - GRP: See previous comment */
5604 while (prev != NULL && prev->lnum == lnum)
5605 prev = prev->prev;
5606 if (prev == NULL)
5607 sign = buf->b_signlist;
5608 else
5609 sign = prev->next;
5610#endif
5611 insert_sign(buf, prev, sign, id, lnum, typenr);
5612
5613 return;
5614}
5615
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005616/*
5617 * For an existing, placed sign "markId" change the type to "typenr".
5618 * Returns the line number of the sign, or zero if the sign is not found.
5619 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005620 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005621buf_change_sign_type(
5622 buf_T *buf, /* buffer to store sign in */
5623 int markId, /* sign ID */
5624 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 signlist_T *sign; /* a sign in the signlist */
5627
5628 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5629 {
5630 if (sign->id == markId)
5631 {
5632 sign->typenr = typenr;
5633 return sign->lnum;
5634 }
5635 }
5636
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005637 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638}
5639
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005641buf_getsigntype(
5642 buf_T *buf,
5643 linenr_T lnum,
5644 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645{
5646 signlist_T *sign; /* a sign in a b_signlist */
5647
5648 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5649 if (sign->lnum == lnum
5650 && (type == SIGN_ANY
5651# ifdef FEAT_SIGN_ICONS
5652 || (type == SIGN_ICON
5653 && sign_get_image(sign->typenr) != NULL)
5654# endif
5655 || (type == SIGN_TEXT
5656 && sign_get_text(sign->typenr) != NULL)
5657 || (type == SIGN_LINEHL
5658 && sign_get_attr(sign->typenr, TRUE) != 0)))
5659 return sign->typenr;
5660 return 0;
5661}
5662
5663
5664 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005665buf_delsign(
5666 buf_T *buf, /* buffer sign is stored in */
5667 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
5669 signlist_T **lastp; /* pointer to pointer to current sign */
5670 signlist_T *sign; /* a sign in a b_signlist */
5671 signlist_T *next; /* the next sign in a b_signlist */
5672 linenr_T lnum; /* line number whose sign was deleted */
5673
5674 lastp = &buf->b_signlist;
5675 lnum = 0;
5676 for (sign = buf->b_signlist; sign != NULL; sign = next)
5677 {
5678 next = sign->next;
5679 if (sign->id == id)
5680 {
5681 *lastp = next;
5682#ifdef FEAT_NETBEANS_INTG
5683 if (next != NULL)
5684 next->prev = sign->prev;
5685#endif
5686 lnum = sign->lnum;
5687 vim_free(sign);
5688 break;
5689 }
5690 else
5691 lastp = &sign->next;
5692 }
5693
5694 /* When deleted the last sign need to redraw the windows to remove the
5695 * sign column. */
5696 if (buf->b_signlist == NULL)
5697 {
5698 redraw_buf_later(buf, NOT_VALID);
5699 changed_cline_bef_curs();
5700 }
5701
5702 return lnum;
5703}
5704
5705
5706/*
5707 * Find the line number of the sign with the requested id. If the sign does
5708 * not exist, return 0 as the line number. This will still let the correct file
5709 * get loaded.
5710 */
5711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005712buf_findsign(
5713 buf_T *buf, /* buffer to store sign in */
5714 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715{
5716 signlist_T *sign; /* a sign in the signlist */
5717
5718 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5719 if (sign->id == id)
5720 return sign->lnum;
5721
5722 return 0;
5723}
5724
5725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005726buf_findsign_id(
5727 buf_T *buf, /* buffer whose sign we are searching for */
5728 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 signlist_T *sign; /* a sign in the signlist */
5731
5732 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5733 if (sign->lnum == lnum)
5734 return sign->id;
5735
5736 return 0;
5737}
5738
5739
5740# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5741/* see if a given type of sign exists on a specific line */
5742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005743buf_findsigntype_id(
5744 buf_T *buf, /* buffer whose sign we are searching for */
5745 linenr_T lnum, /* line number of sign */
5746 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 signlist_T *sign; /* a sign in the signlist */
5749
5750 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5751 if (sign->lnum == lnum && sign->typenr == typenr)
5752 return sign->id;
5753
5754 return 0;
5755}
5756
5757
5758# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5759/* return the number of icons on the given line */
5760 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005761buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
5763 signlist_T *sign; /* a sign in the signlist */
5764 int count = 0;
5765
5766 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5767 if (sign->lnum == lnum)
5768 if (sign_get_image(sign->typenr) != NULL)
5769 count++;
5770
5771 return count;
5772}
5773# endif /* FEAT_SIGN_ICONS */
5774# endif /* FEAT_NETBEANS_INTG */
5775
5776
5777/*
5778 * Delete signs in buffer "buf".
5779 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005781buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
5783 signlist_T *next;
5784
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005785 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005786 * sign column. Not when curwin is NULL (this means we're exiting). */
5787 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005788 {
5789 redraw_buf_later(buf, NOT_VALID);
5790 changed_cline_bef_curs();
5791 }
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 while (buf->b_signlist != NULL)
5794 {
5795 next = buf->b_signlist->next;
5796 vim_free(buf->b_signlist);
5797 buf->b_signlist = next;
5798 }
5799}
5800
5801/*
5802 * Delete all signs in all buffers.
5803 */
5804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005805buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
5807 buf_T *buf; /* buffer we are checking for signs */
5808
5809 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5810 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812}
5813
5814/*
5815 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5816 */
5817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005818sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
5820 buf_T *buf;
5821 signlist_T *p;
5822 char lbuf[BUFSIZ];
5823
5824 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5825 msg_putchar('\n');
5826 if (rbuf == NULL)
5827 buf = firstbuf;
5828 else
5829 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005830 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
5832 if (buf->b_signlist != NULL)
5833 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005834 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5836 msg_putchar('\n');
5837 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005838 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005840 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5842 MSG_PUTS(lbuf);
5843 msg_putchar('\n');
5844 }
5845 if (rbuf != NULL)
5846 break;
5847 buf = buf->b_next;
5848 }
5849}
5850
5851/*
5852 * Adjust a placed sign for inserted/deleted lines.
5853 */
5854 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005855sign_mark_adjust(
5856 linenr_T line1,
5857 linenr_T line2,
5858 long amount,
5859 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 signlist_T *sign; /* a sign in a b_signlist */
5862
5863 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5864 {
5865 if (sign->lnum >= line1 && sign->lnum <= line2)
5866 {
5867 if (amount == MAXLNUM)
5868 sign->lnum = line1;
5869 else
5870 sign->lnum += amount;
5871 }
5872 else if (sign->lnum > line2)
5873 sign->lnum += amount_after;
5874 }
5875}
5876#endif /* FEAT_SIGNS */
5877
5878/*
5879 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5880 */
5881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005882set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 if (on != curbuf->b_p_bl)
5885 {
5886 curbuf->b_p_bl = on;
5887#ifdef FEAT_AUTOCMD
5888 if (on)
5889 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5890 else
5891 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5892#endif
5893 }
5894}
5895
5896/*
5897 * Read the file for "buf" again and check if the contents changed.
5898 * Return TRUE if it changed or this could not be checked.
5899 */
5900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005901buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 buf_T *newbuf;
5904 int differ = TRUE;
5905 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 exarg_T ea;
5908
5909 /* Allocate a buffer without putting it in the buffer list. */
5910 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5911 if (newbuf == NULL)
5912 return TRUE;
5913
5914 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5915 if (prep_exarg(&ea, buf) == FAIL)
5916 {
5917 wipe_buffer(newbuf, FALSE);
5918 return TRUE;
5919 }
5920
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 /* set curwin/curbuf to buf and save a few things */
5922 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
Bram Moolenaar4770d092006-01-12 23:22:24 +00005924 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 && readfile(buf->b_ffname, buf->b_fname,
5926 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5927 &ea, READ_NEW | READ_DUMMY) == OK)
5928 {
5929 /* compare the two files line by line */
5930 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5931 {
5932 differ = FALSE;
5933 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5934 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5935 {
5936 differ = TRUE;
5937 break;
5938 }
5939 }
5940 }
5941 vim_free(ea.cmd);
5942
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 /* restore curwin/curbuf and a few other things */
5944 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
5946 if (curbuf != newbuf) /* safety check */
5947 wipe_buffer(newbuf, FALSE);
5948
5949 return differ;
5950}
5951
5952/*
5953 * Wipe out a buffer and decrement the last buffer number if it was used for
5954 * this buffer. Call this to wipe out a temp buffer that does not contain any
5955 * marks.
5956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005958wipe_buffer(
5959 buf_T *buf,
5960 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 if (buf->b_fnum == top_file_num - 1)
5963 --top_file_num;
5964
5965#ifdef FEAT_AUTOCMD
5966 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005967 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005969 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970#ifdef FEAT_AUTOCMD
5971 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005972 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#endif
5974}