blob: 4fc50321ce6cfe70777c11fbadd573518c611e34 [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 Moolenaar4b9d6372014-09-23 14:24:40 +020031static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf, int ignore_case));
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaar4b9d6372014-09-23 14:24:40 +020033static char_u *fname_match __ARGS((regprog_T *prog, char_u *name, int ignore_case));
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
35static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
Bram Moolenaar701f7af2008-11-15 13:12:07 +000036static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
38static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
39static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
40static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
41#else
42static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
43#endif
44#ifdef FEAT_TITLE
45static int ti_change __ARGS((char_u *str, char_u **last));
46#endif
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000047static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void free_buffer __ARGS((buf_T *));
49static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50static void clear_wininfo __ARGS((buf_T *buf));
51
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
59static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar7fd73202010-07-25 16:58:46 +020062#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
63static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066#ifdef FEAT_AUTOCMD
67static char *e_auabort = N_("E855: Autocommands caused command to abort");
68#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram Moolenaar071d4272004-06-13 20:20:40 +000070/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020071 * Open current buffer, that is: open the memfile and read the file into
72 * memory.
73 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
75 int
Bram Moolenaar59f931e2010-07-24 20:27:03 +020076open_buffer(read_stdin, eap, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 int read_stdin; /* read file from stdin */
78 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
Bram Moolenaar59f931e2010-07-24 20:27:03 +020079 int flags; /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080{
81 int retval = OK;
82#ifdef FEAT_AUTOCMD
83 buf_T *old_curbuf;
84#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +010085#ifdef FEAT_SYN_HL
86 long old_tw = curbuf->b_p_tw;
87#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89 /*
90 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
91 * When re-entering the same buffer, it should not change, because the
92 * user may have reset the flag by hand.
93 */
94 if (readonlymode && curbuf->b_ffname != NULL
95 && (curbuf->b_flags & BF_NEVERLOADED))
96 curbuf->b_p_ro = TRUE;
97
Bram Moolenaar4770d092006-01-12 23:22:24 +000098 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 {
100 /*
101 * There MUST be a memfile, otherwise we can't do anything
102 * If we can't create one for the current buffer, take another buffer
103 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100104 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
106 if (curbuf->b_ml.ml_mfp != NULL)
107 break;
108 /*
109 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000110 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 */
112 if (curbuf == NULL)
113 {
114 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
115 getout(2);
116 }
117 EMSG(_("E83: Cannot allocate buffer, using other one..."));
118 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100119#ifdef FEAT_SYN_HL
120 if (old_tw != curbuf->b_p_tw)
121 check_colorcolumn(curwin);
122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 return FAIL;
124 }
125
126#ifdef FEAT_AUTOCMD
127 /* The autocommands in readfile() may change the buffer, but only AFTER
128 * reading the file. */
129 old_curbuf = curbuf;
130 modified_was_set = FALSE;
131#endif
132
133 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100134 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136 if (curbuf->b_ffname != NULL
137#ifdef FEAT_NETBEANS_INTG
138 && netbeansReadFile
139#endif
140 )
141 {
142#ifdef FEAT_NETBEANS_INTG
143 int oldFire = netbeansFireChanges;
144
145 netbeansFireChanges = 0;
146#endif
147 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200148 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
149 flags | READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#ifdef FEAT_NETBEANS_INTG
151 netbeansFireChanges = oldFire;
152#endif
153 /* Help buffer is filtered. */
154 if (curbuf->b_help)
155 fix_help_buffer();
156 }
157 else if (read_stdin)
158 {
159 int save_bin = curbuf->b_p_bin;
160 linenr_T line_count;
161
162 /*
163 * First read the text in binary mode into the buffer.
164 * Then read from that same buffer and append at the end. This makes
165 * it possible to retry when 'fileformat' or 'fileencoding' was
166 * guessed wrong.
167 */
168 curbuf->b_p_bin = TRUE;
169 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200170 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
171 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 curbuf->b_p_bin = save_bin;
173 if (retval == OK)
174 {
175 line_count = curbuf->b_ml.ml_line_count;
176 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200177 (linenr_T)0, (linenr_T)MAXLNUM, eap,
178 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 if (retval == OK)
180 {
181 /* Delete the binary lines. */
182 while (--line_count >= 0)
183 ml_delete((linenr_T)1, FALSE);
184 }
185 else
186 {
187 /* Delete the converted lines. */
188 while (curbuf->b_ml.ml_line_count > line_count)
189 ml_delete(line_count, FALSE);
190 }
191 /* Put the cursor on the first line. */
192 curwin->w_cursor.lnum = 1;
193 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000194
195 /* Set or reset 'modified' before executing autocommands, so that
196 * it can be changed there. */
197 if (!readonlymode && !bufempty())
198 changed();
199 else if (retval != FAIL)
200 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef FEAT_AUTOCMD
202# ifdef FEAT_EVAL
203 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
204 curbuf, &retval);
205# else
206 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
207# endif
208#endif
209 }
210 }
211
212 /* if first time loading this buffer, init b_chartab[] */
213 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100216#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100217 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100218#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
221 /*
222 * Set/reset the Changed flag first, autocmds may change the buffer.
223 * Apply the automatic commands, before processing the modelines.
224 * So the modelines have priority over auto commands.
225 */
226 /* When reading stdin, the buffer contents always needs writing, so set
227 * the changed flag. Unless in readonly mode: "ls | gview -".
228 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000229 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#ifdef FEAT_AUTOCMD
231 || modified_was_set /* ":set modified" used in autocmd */
232# ifdef FEAT_EVAL
233 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
234# endif
235#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000236 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000238 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 unchanged(curbuf, FALSE);
240 save_file_ff(curbuf); /* keep this fileformat */
241
242 /* require "!" to overwrite the file, because it wasn't read completely */
243#ifdef FEAT_EVAL
244 if (aborting())
245#else
246 if (got_int)
247#endif
248 curbuf->b_flags |= BF_READERR;
249
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000250#ifdef FEAT_FOLDING
251 /* Need to update automatic folding. Do this before the autocommands,
252 * they may use the fold info. */
253 foldUpdateAll(curwin);
254#endif
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifdef FEAT_AUTOCMD
257 /* need to set w_topline, unless some autocommand already did that. */
258 if (!(curwin->w_valid & VALID_TOPLINE))
259 {
260 curwin->w_topline = 1;
261# ifdef FEAT_DIFF
262 curwin->w_topfill = 0;
263# endif
264 }
265# ifdef FEAT_EVAL
266 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
267# else
268 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
269# endif
270#endif
271
272 if (retval != FAIL)
273 {
274#ifdef FEAT_AUTOCMD
275 /*
276 * The autocommands may have changed the current buffer. Apply the
277 * modelines to the correct buffer, if it still exists and is loaded.
278 */
279 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
280 {
281 aco_save_T aco;
282
283 /* Go to the buffer that was opened. */
284 aucmd_prepbuf(&aco, old_curbuf);
285#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000286 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
288
289#ifdef FEAT_AUTOCMD
290# ifdef FEAT_EVAL
291 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
292 &retval);
293# else
294 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
295# endif
296
297 /* restore curwin/curbuf and a few other things */
298 aucmd_restbuf(&aco);
299 }
300#endif
301 }
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 return retval;
304}
305
306/*
307 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
308 */
309 int
310buf_valid(buf)
311 buf_T *buf;
312{
313 buf_T *bp;
314
315 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
316 if (bp == buf)
317 return TRUE;
318 return FALSE;
319}
320
321/*
322 * Close the link to a buffer.
323 * "action" is used when there is no longer a window for the buffer.
324 * It can be:
325 * 0 buffer becomes hidden
326 * DOBUF_UNLOAD buffer is unloaded
327 * DOBUF_DELETE buffer is unloaded and removed from buffer list
328 * DOBUF_WIPE buffer is unloaded and really deleted
329 * When doing all but the first one on the current buffer, the caller should
330 * get a new buffer very soon!
331 *
332 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100333 *
334 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
335 * cause there to be only one window with this buffer. e.g. when ":quit" is
336 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 */
338 void
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100339close_buffer(win, buf, action, abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 win_T *win; /* if not NULL, set b_last_cursor */
341 buf_T *buf;
342 int action;
Bram Moolenaar5fbe6992012-03-07 22:52:36 +0100343 int abort_if_last UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
345#ifdef FEAT_AUTOCMD
346 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100347 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349 int unload_buf = (action != 0);
350 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
351 int wipe_buf = (action == DOBUF_WIPE);
352
353#ifdef FEAT_QUICKFIX
354 /*
355 * Force unloading or deleting when 'bufhidden' says so.
356 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
357 * "hide" (otherwise we could never free or delete a buffer).
358 */
359 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
360 {
361 del_buf = TRUE;
362 unload_buf = TRUE;
363 }
364 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
365 {
366 del_buf = TRUE;
367 unload_buf = TRUE;
368 wipe_buf = TRUE;
369 }
370 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
371 unload_buf = TRUE;
372#endif
373
Bram Moolenaar3be85852014-06-12 14:01:31 +0200374 if (win != NULL
375#ifdef FEAT_WINDOWS
376 && win_valid(win) /* in case autocommands closed the window */
377#endif
378 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {
380 /* Set b_last_cursor when closing the last window for the buffer.
381 * Remember the last cursor position and window options of the buffer.
382 * This used to be only for the current window, but then options like
383 * 'foldmethod' may be lost with a ":only" command. */
384 if (buf->b_nwindows == 1)
385 set_last_cursor(win);
386 buflist_setfpos(buf, win,
387 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
388 win->w_cursor.col, TRUE);
389 }
390
391#ifdef FEAT_AUTOCMD
392 /* When the buffer is no longer in a window, trigger BufWinLeave */
393 if (buf->b_nwindows == 1)
394 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200395 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
397 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200398 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100399 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200400 /* Autocommands deleted the buffer. */
401aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100402 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100404 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200405 buf->b_closing = FALSE;
406 if (abort_if_last && one_window())
407 /* Autocommands made this the only window. */
408 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
410 /* When the buffer becomes hidden, but is not unloaded, trigger
411 * BufHidden */
412 if (!unload_buf)
413 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200414 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
416 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200417 if (!buf_valid(buf))
418 /* Autocommands deleted the buffer. */
419 goto aucmd_abort;
420 buf->b_closing = FALSE;
421 if (abort_if_last && one_window())
422 /* Autocommands made this the only window. */
423 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425# ifdef FEAT_EVAL
426 if (aborting()) /* autocmds may abort script processing */
427 return;
428# endif
429 }
430 nwindows = buf->b_nwindows;
431#endif
432
433 /* decrease the link count from windows (unless not in any window) */
434 if (buf->b_nwindows > 0)
435 --buf->b_nwindows;
436
437 /* Return when a window is displaying the buffer or when it's not
438 * unloaded. */
439 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442 /* Always remove the buffer when there is no file name. */
443 if (buf->b_ffname == NULL)
444 del_buf = TRUE;
445
446 /*
447 * Free all things allocated for this buffer.
448 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
449 */
450#ifdef FEAT_AUTOCMD
451 /* Remember if we are closing the current buffer. Restore the number of
452 * windows, so that autocommands in buf_freeall() don't get confused. */
453 is_curbuf = (buf == curbuf);
454 buf->b_nwindows = nwindows;
455#endif
456
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200457 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200458 if (
459#ifdef FEAT_WINDOWS
460 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100461#else
462 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200463#endif
464 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200465 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
467#ifdef FEAT_AUTOCMD
468 /* Autocommands may have deleted the buffer. */
469 if (!buf_valid(buf))
470 return;
471# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000472 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 return;
474# endif
475
476 /* Autocommands may have opened or closed windows for this buffer.
477 * Decrement the count for the close we do here. */
478 if (buf->b_nwindows > 0)
479 --buf->b_nwindows;
480
481 /*
482 * It's possible that autocommands change curbuf to the one being deleted.
483 * This might cause the previous curbuf to be deleted unexpectedly. But
484 * in some cases it's OK to delete the curbuf, because a new one is
485 * obtained anyway. Therefore only return if curbuf changed to the
486 * deleted buffer.
487 */
488 if (buf == curbuf && !is_curbuf)
489 return;
490#endif
491
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000492 /* Change directories when the 'acd' option is set. */
493 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
495 /*
496 * Remove the buffer from the list.
497 */
498 if (wipe_buf)
499 {
500#ifdef FEAT_SUN_WORKSHOP
501 if (usingSunWorkShop)
502 workshop_file_closed_lineno((char *)buf->b_ffname,
503 (int)buf->b_last_cursor.lnum);
504#endif
505 vim_free(buf->b_ffname);
506 vim_free(buf->b_sfname);
507 if (buf->b_prev == NULL)
508 firstbuf = buf->b_next;
509 else
510 buf->b_prev->b_next = buf->b_next;
511 if (buf->b_next == NULL)
512 lastbuf = buf->b_prev;
513 else
514 buf->b_next->b_prev = buf->b_prev;
515 free_buffer(buf);
516 }
517 else
518 {
519 if (del_buf)
520 {
521 /* Free all internal variables and reset option values, to make
522 * ":bdel" compatible with Vim 5.7. */
523 free_buffer_stuff(buf, TRUE);
524
525 /* Make it look like a new buffer. */
526 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
527
528 /* Init the options when loaded again. */
529 buf->b_p_initialized = FALSE;
530 }
531 buf_clear_file(buf);
532 if (del_buf)
533 buf->b_p_bl = FALSE;
534 }
535}
536
537/*
538 * Make buffer not contain a file.
539 */
540 void
541buf_clear_file(buf)
542 buf_T *buf;
543{
544 buf->b_ml.ml_line_count = 1;
545 unchanged(buf, TRUE);
546#ifndef SHORT_FNAME
547 buf->b_shortname = FALSE;
548#endif
549 buf->b_p_eol = TRUE;
550 buf->b_start_eol = TRUE;
551#ifdef FEAT_MBYTE
552 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000553 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#endif
555 buf->b_ml.ml_mfp = NULL;
556 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
557#ifdef FEAT_NETBEANS_INTG
558 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#endif
560}
561
562/*
563 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200564 * the file. flags:
565 * BFA_DEL buffer is going to be deleted
566 * BFA_WIPE buffer is going to be wiped out
567 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200570buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200572 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
574#ifdef FEAT_AUTOCMD
575 int is_curbuf = (buf == curbuf);
576
Bram Moolenaar362ce482012-06-06 19:02:45 +0200577 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
579 if (!buf_valid(buf)) /* autocommands may delete the buffer */
580 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200581 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {
583 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
584 if (!buf_valid(buf)) /* autocommands may delete the buffer */
585 return;
586 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200587 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {
589 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
590 FALSE, buf);
591 if (!buf_valid(buf)) /* autocommands may delete the buffer */
592 return;
593 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200594 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000596 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 return;
598# endif
599
600 /*
601 * It's possible that autocommands change curbuf to the one being deleted.
602 * This might cause curbuf to be deleted unexpectedly. But in some cases
603 * it's OK to delete the curbuf, because a new one is obtained anyway.
604 * Therefore only return if curbuf changed to the deleted buffer.
605 */
606 if (buf == curbuf && !is_curbuf)
607 return;
608#endif
609#ifdef FEAT_DIFF
610 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
611#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200612#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100613 /* Remove any ownsyntax, unless exiting. */
614 if (firstwin != NULL && curwin->w_buffer == buf)
615 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200616#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000617
618#ifdef FEAT_FOLDING
619 /* No folds in an empty buffer. */
620# ifdef FEAT_WINDOWS
621 {
622 win_T *win;
623 tabpage_T *tp;
624
625 FOR_ALL_TAB_WINDOWS(tp, win)
626 if (win->w_buffer == buf)
627 clearFolding(win);
628 }
629# else
630 if (curwin->w_buffer == buf)
631 clearFolding(curwin);
632# endif
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#ifdef FEAT_TCL
636 tcl_buffer_free(buf);
637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ml_close(buf, TRUE); /* close and delete the memline/memfile */
639 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200640 if ((flags & BFA_KEEP_UNDO) == 0)
641 {
642 u_blockfree(buf); /* free the memory allocated for undo */
643 u_clearall(buf); /* reset all undo information */
644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200646 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000648 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649}
650
651/*
652 * Free a buffer structure and the things it contains related to the buffer
653 * itself (not the file, that must have been done already).
654 */
655 static void
656free_buffer(buf)
657 buf_T *buf;
658{
659 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200660#ifdef FEAT_EVAL
661 unref_var_dict(buf->b_vars);
662#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200663#ifdef FEAT_LUA
664 lua_buffer_free(buf);
665#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000666#ifdef FEAT_MZSCHEME
667 mzscheme_buffer_free(buf);
668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#ifdef FEAT_PERL
670 perl_buf_free(buf);
671#endif
672#ifdef FEAT_PYTHON
673 python_buffer_free(buf);
674#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200675#ifdef FEAT_PYTHON3
676 python3_buffer_free(buf);
677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef FEAT_RUBY
679 ruby_buffer_free(buf);
680#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000681#ifdef FEAT_AUTOCMD
682 aubuflocal_remove(buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200683 if (autocmd_busy)
684 {
685 /* Do not free the buffer structure while autocommands are executing,
686 * it's still needed. Free it when autocmd_busy is reset. */
687 buf->b_next = au_pending_free_buf;
688 au_pending_free_buf = buf;
689 }
690 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000691#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200692 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
695/*
696 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
697 */
698 static void
699free_buffer_stuff(buf, free_options)
700 buf_T *buf;
701 int free_options; /* free options as well */
702{
703 if (free_options)
704 {
705 clear_wininfo(buf); /* including window-local options */
706 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200707#ifdef FEAT_SPELL
708 ga_clear(&buf->b_s.b_langp);
709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200712 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
713 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#endif
715#ifdef FEAT_USR_CMDS
716 uc_clear(&buf->b_ucmds); /* clear local user commands */
717#endif
718#ifdef FEAT_SIGNS
719 buf_delete_signs(buf); /* delete any signs */
720#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000721#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200722 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#ifdef FEAT_LOCALMAP
725 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
726 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
727#endif
728#ifdef FEAT_MBYTE
729 vim_free(buf->b_start_fenc);
730 buf->b_start_fenc = NULL;
731#endif
732}
733
734/*
735 * Free the b_wininfo list for buffer "buf".
736 */
737 static void
738clear_wininfo(buf)
739 buf_T *buf;
740{
741 wininfo_T *wip;
742
743 while (buf->b_wininfo != NULL)
744 {
745 wip = buf->b_wininfo;
746 buf->b_wininfo = wip->wi_next;
747 if (wip->wi_optset)
748 {
749 clear_winopt(&wip->wi_opt);
750#ifdef FEAT_FOLDING
751 deleteFoldRecurse(&wip->wi_folds);
752#endif
753 }
754 vim_free(wip);
755 }
756}
757
758#if defined(FEAT_LISTCMDS) || defined(PROTO)
759/*
760 * Go to another buffer. Handles the result of the ATTENTION dialog.
761 */
762 void
763goto_buffer(eap, start, dir, count)
764 exarg_T *eap;
765 int start;
766 int dir;
767 int count;
768{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000769# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 buf_T *old_curbuf = curbuf;
771
772 swap_exists_action = SEA_DIALOG;
773# endif
774 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
775 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000776# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
778 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000779# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
780 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000781
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000782 /* Reset the error/interrupt/exception state here so that
783 * aborting() returns FALSE when closing a window. */
784 enter_cleanup(&cs);
785# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000786
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000787 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 win_close(curwin, TRUE);
789 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000790 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000791
792# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
793 /* Restore the error/interrupt/exception state if not discarded by a
794 * new aborting error, interrupt, or uncaught exception. */
795 leave_cleanup(&cs);
796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 }
798 else
799 handle_swap_exists(old_curbuf);
800# endif
801}
802#endif
803
Bram Moolenaare64ac772005-12-07 20:54:59 +0000804#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805/*
806 * Handle the situation of swap_exists_action being set.
807 * It is allowed for "old_curbuf" to be NULL or invalid.
808 */
809 void
810handle_swap_exists(old_curbuf)
811 buf_T *old_curbuf;
812{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000813# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
814 cleanup_T cs;
815# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100816#ifdef FEAT_SYN_HL
817 long old_tw = curbuf->b_p_tw;
818#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (swap_exists_action == SEA_QUIT)
821 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000822# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
823 /* Reset the error/interrupt/exception state here so that
824 * aborting() returns FALSE when closing a buffer. */
825 enter_cleanup(&cs);
826# endif
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 /* User selected Quit at ATTENTION prompt. Go back to previous
829 * buffer. If that buffer is gone or the same as the current one,
830 * open a new, empty buffer. */
831 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000832 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100833 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000835 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100839#ifdef FEAT_SYN_HL
840 if (old_tw != curbuf->b_p_tw)
841 check_colorcolumn(curwin);
842#endif
843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000845
846# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
847 /* Restore the error/interrupt/exception state if not discarded by a
848 * new aborting error, interrupt, or uncaught exception. */
849 leave_cleanup(&cs);
850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 else if (swap_exists_action == SEA_RECOVER)
853 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000854# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
855 /* Reset the error/interrupt/exception state here so that
856 * aborting() returns FALSE when closing a buffer. */
857 enter_cleanup(&cs);
858# endif
859
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 /* User selected Recover at ATTENTION prompt. */
861 msg_scroll = TRUE;
862 ml_recover();
863 MSG_PUTS("\n"); /* don't overwrite the last message */
864 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000865 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000866
867# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
868 /* Restore the error/interrupt/exception state if not discarded by a
869 * new aborting error, interrupt, or uncaught exception. */
870 leave_cleanup(&cs);
871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873 swap_exists_action = SEA_NONE;
874}
875#endif
876
877#if defined(FEAT_LISTCMDS) || defined(PROTO)
878/*
879 * do_bufdel() - delete or unload buffer(s)
880 *
881 * addr_count == 0: ":bdel" - delete current buffer
882 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
883 * buffer "end_bnr", then any other arguments.
884 * addr_count == 2: ":N,N bdel" - delete buffers in range
885 *
886 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
887 * DOBUF_DEL (":bdel")
888 *
889 * Returns error message or NULL
890 */
891 char_u *
892do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
893 int command;
894 char_u *arg; /* pointer to extra arguments */
895 int addr_count;
896 int start_bnr; /* first buffer number in a range */
897 int end_bnr; /* buffer nr or last buffer nr in a range */
898 int forceit;
899{
900 int do_current = 0; /* delete current buffer? */
901 int deleted = 0; /* number of buffers deleted */
902 char_u *errormsg = NULL; /* return value */
903 int bnr; /* buffer number */
904 char_u *p;
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (addr_count == 0)
907 {
908 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
909 }
910 else
911 {
912 if (addr_count == 2)
913 {
914 if (*arg) /* both range and argument is not allowed */
915 return (char_u *)_(e_trailing);
916 bnr = start_bnr;
917 }
918 else /* addr_count == 1 */
919 bnr = end_bnr;
920
921 for ( ;!got_int; ui_breakcheck())
922 {
923 /*
924 * delete the current buffer last, otherwise when the
925 * current buffer is deleted, the next buffer becomes
926 * the current one and will be loaded, which may then
927 * also be deleted, etc.
928 */
929 if (bnr == curbuf->b_fnum)
930 do_current = bnr;
931 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
932 forceit) == OK)
933 ++deleted;
934
935 /*
936 * find next buffer number to delete/unload
937 */
938 if (addr_count == 2)
939 {
940 if (++bnr > end_bnr)
941 break;
942 }
943 else /* addr_count == 1 */
944 {
945 arg = skipwhite(arg);
946 if (*arg == NUL)
947 break;
948 if (!VIM_ISDIGIT(*arg))
949 {
950 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100951 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
952 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (bnr < 0) /* failed */
954 break;
955 arg = p;
956 }
957 else
958 bnr = getdigits(&arg);
959 }
960 }
961 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
962 FORWARD, do_current, forceit) == OK)
963 ++deleted;
964
965 if (deleted == 0)
966 {
967 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000968 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000970 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000972 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 errormsg = IObuff;
974 }
975 else if (deleted >= p_report)
976 {
977 if (command == DOBUF_UNLOAD)
978 {
979 if (deleted == 1)
980 MSG(_("1 buffer unloaded"));
981 else
982 smsg((char_u *)_("%d buffers unloaded"), deleted);
983 }
984 else if (command == DOBUF_DEL)
985 {
986 if (deleted == 1)
987 MSG(_("1 buffer deleted"));
988 else
989 smsg((char_u *)_("%d buffers deleted"), deleted);
990 }
991 else
992 {
993 if (deleted == 1)
994 MSG(_("1 buffer wiped out"));
995 else
996 smsg((char_u *)_("%d buffers wiped out"), deleted);
997 }
998 }
999 }
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 return errormsg;
1003}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001004#endif /* FEAT_LISTCMDS */
1005
1006#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1007 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Bram Moolenaara02471e2014-01-10 16:43:14 +01001009static int empty_curbuf __ARGS((int close_others, int forceit, int action));
1010
1011/*
1012 * Make the current buffer empty.
1013 * Used when it is wiped out and it's the last buffer.
1014 */
1015 static int
1016empty_curbuf(close_others, forceit, action)
1017 int close_others;
1018 int forceit;
1019 int action;
1020{
1021 int retval;
1022 buf_T *buf = curbuf;
1023
1024 if (action == DOBUF_UNLOAD)
1025 {
1026 EMSG(_("E90: Cannot unload last buffer"));
1027 return FAIL;
1028 }
1029
1030 if (close_others)
1031 {
1032 /* Close any other windows on this buffer, then make it empty. */
1033#ifdef FEAT_WINDOWS
1034 close_windows(buf, TRUE);
1035#endif
1036 }
1037
1038 setpcmark();
1039 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1040 forceit ? ECMD_FORCEIT : 0, curwin);
1041
1042 /*
1043 * do_ecmd() may create a new buffer, then we have to delete
1044 * the old one. But do_ecmd() may have done that already, check
1045 * if the buffer still exists.
1046 */
1047 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1048 close_buffer(NULL, buf, action, FALSE);
1049 if (!close_others)
1050 need_fileinfo = FALSE;
1051 return retval;
1052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053/*
1054 * Implementation of the commands for the buffer list.
1055 *
1056 * action == DOBUF_GOTO go to specified buffer
1057 * action == DOBUF_SPLIT split window and go to specified buffer
1058 * action == DOBUF_UNLOAD unload specified buffer(s)
1059 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1060 * action == DOBUF_WIPE delete specified buffer(s) really
1061 *
1062 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1063 * start == DOBUF_FIRST go to "count" buffer from first buffer
1064 * start == DOBUF_LAST go to "count" buffer from last buffer
1065 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1066 *
1067 * Return FAIL or OK.
1068 */
1069 int
1070do_buffer(action, start, dir, count, forceit)
1071 int action;
1072 int start;
1073 int dir; /* FORWARD or BACKWARD */
1074 int count; /* buffer number or number of buffers */
1075 int forceit; /* TRUE for :...! */
1076{
1077 buf_T *buf;
1078 buf_T *bp;
1079 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1080 || action == DOBUF_WIPE);
1081
1082 switch (start)
1083 {
1084 case DOBUF_FIRST: buf = firstbuf; break;
1085 case DOBUF_LAST: buf = lastbuf; break;
1086 default: buf = curbuf; break;
1087 }
1088 if (start == DOBUF_MOD) /* find next modified buffer */
1089 {
1090 while (count-- > 0)
1091 {
1092 do
1093 {
1094 buf = buf->b_next;
1095 if (buf == NULL)
1096 buf = firstbuf;
1097 }
1098 while (buf != curbuf && !bufIsChanged(buf));
1099 }
1100 if (!bufIsChanged(buf))
1101 {
1102 EMSG(_("E84: No modified buffer found"));
1103 return FAIL;
1104 }
1105 }
1106 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1107 {
1108 while (buf != NULL && buf->b_fnum != count)
1109 buf = buf->b_next;
1110 }
1111 else
1112 {
1113 bp = NULL;
1114 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1115 {
1116 /* remember the buffer where we start, we come back there when all
1117 * buffers are unlisted. */
1118 if (bp == NULL)
1119 bp = buf;
1120 if (dir == FORWARD)
1121 {
1122 buf = buf->b_next;
1123 if (buf == NULL)
1124 buf = firstbuf;
1125 }
1126 else
1127 {
1128 buf = buf->b_prev;
1129 if (buf == NULL)
1130 buf = lastbuf;
1131 }
1132 /* don't count unlisted buffers */
1133 if (unload || buf->b_p_bl)
1134 {
1135 --count;
1136 bp = NULL; /* use this buffer as new starting point */
1137 }
1138 if (bp == buf)
1139 {
1140 /* back where we started, didn't find anything. */
1141 EMSG(_("E85: There is no listed buffer"));
1142 return FAIL;
1143 }
1144 }
1145 }
1146
1147 if (buf == NULL) /* could not find it */
1148 {
1149 if (start == DOBUF_FIRST)
1150 {
1151 /* don't warn when deleting */
1152 if (!unload)
1153 EMSGN(_("E86: Buffer %ld does not exist"), count);
1154 }
1155 else if (dir == FORWARD)
1156 EMSG(_("E87: Cannot go beyond last buffer"));
1157 else
1158 EMSG(_("E88: Cannot go before first buffer"));
1159 return FAIL;
1160 }
1161
1162#ifdef FEAT_GUI
1163 need_mouse_correct = TRUE;
1164#endif
1165
1166#ifdef FEAT_LISTCMDS
1167 /*
1168 * delete buffer buf from memory and/or the list
1169 */
1170 if (unload)
1171 {
1172 int forward;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174 /* When unloading or deleting a buffer that's already unloaded and
1175 * unlisted: fail silently. */
1176 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1177 return FAIL;
1178
1179 if (!forceit && bufIsChanged(buf))
1180 {
1181#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1182 if ((p_confirm || cmdmod.confirm) && p_write)
1183 {
1184 dialog_changed(buf, FALSE);
1185# ifdef FEAT_AUTOCMD
1186 if (!buf_valid(buf))
1187 /* Autocommand deleted buffer, oops! It's not changed
1188 * now. */
1189 return FAIL;
1190# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001191 /* If it's still changed fail silently, the dialog already
1192 * mentioned why it fails. */
1193 if (bufIsChanged(buf))
1194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#endif
1198 {
1199 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1200 buf->b_fnum);
1201 return FAIL;
1202 }
1203 }
1204
1205 /*
1206 * If deleting the last (listed) buffer, make it empty.
1207 * The last (listed) buffer cannot be unloaded.
1208 */
1209 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1210 if (bp->b_p_bl && bp != buf)
1211 break;
1212 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001213 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215#ifdef FEAT_WINDOWS
1216 /*
1217 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001218 * (unless it's the only window). Repeat this so long as we end up in
1219 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001221 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001222# ifdef FEAT_AUTOCMD
1223 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1224# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001225 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001226 {
1227 if (win_close(curwin, FALSE) == FAIL)
1228 break;
1229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#endif
1231
1232 /*
1233 * If the buffer to be deleted is not the current one, delete it here.
1234 */
1235 if (buf != curbuf)
1236 {
1237#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001238 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#endif
1240 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001241 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 return OK;
1243 }
1244
1245 /*
1246 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001247 * There should be another, otherwise it would have been handled
1248 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 * First use au_new_curbuf, if it is valid.
1250 * Then prefer the buffer we most recently visited.
1251 * Else try to find one that is loaded, after the current buffer,
1252 * then before the current buffer.
1253 * Finally use any buffer.
1254 */
1255 buf = NULL; /* selected buffer */
1256 bp = NULL; /* used when no loaded buffer found */
1257#ifdef FEAT_AUTOCMD
1258 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1259 buf = au_new_curbuf;
1260# ifdef FEAT_JUMPLIST
1261 else
1262# endif
1263#endif
1264#ifdef FEAT_JUMPLIST
1265 if (curwin->w_jumplistlen > 0)
1266 {
1267 int jumpidx;
1268
1269 jumpidx = curwin->w_jumplistidx - 1;
1270 if (jumpidx < 0)
1271 jumpidx = curwin->w_jumplistlen - 1;
1272
1273 forward = jumpidx;
1274 while (jumpidx != curwin->w_jumplistidx)
1275 {
1276 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1277 if (buf != NULL)
1278 {
1279 if (buf == curbuf || !buf->b_p_bl)
1280 buf = NULL; /* skip current and unlisted bufs */
1281 else if (buf->b_ml.ml_mfp == NULL)
1282 {
1283 /* skip unloaded buf, but may keep it for later */
1284 if (bp == NULL)
1285 bp = buf;
1286 buf = NULL;
1287 }
1288 }
1289 if (buf != NULL) /* found a valid buffer: stop searching */
1290 break;
1291 /* advance to older entry in jump list */
1292 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1293 break;
1294 if (--jumpidx < 0)
1295 jumpidx = curwin->w_jumplistlen - 1;
1296 if (jumpidx == forward) /* List exhausted for sure */
1297 break;
1298 }
1299 }
1300#endif
1301
1302 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1303 {
1304 forward = TRUE;
1305 buf = curbuf->b_next;
1306 for (;;)
1307 {
1308 if (buf == NULL)
1309 {
1310 if (!forward) /* tried both directions */
1311 break;
1312 buf = curbuf->b_prev;
1313 forward = FALSE;
1314 continue;
1315 }
1316 /* in non-help buffer, try to skip help buffers, and vv */
1317 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1318 {
1319 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1320 break;
1321 if (bp == NULL) /* remember unloaded buf for later */
1322 bp = buf;
1323 }
1324 if (forward)
1325 buf = buf->b_next;
1326 else
1327 buf = buf->b_prev;
1328 }
1329 }
1330 if (buf == NULL) /* No loaded buffer, use unloaded one */
1331 buf = bp;
1332 if (buf == NULL) /* No loaded buffer, find listed one */
1333 {
1334 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1335 if (buf->b_p_bl && buf != curbuf)
1336 break;
1337 }
1338 if (buf == NULL) /* Still no buffer, just take one */
1339 {
1340 if (curbuf->b_next != NULL)
1341 buf = curbuf->b_next;
1342 else
1343 buf = curbuf->b_prev;
1344 }
1345 }
1346
Bram Moolenaara02471e2014-01-10 16:43:14 +01001347 if (buf == NULL)
1348 {
1349 /* Autocommands must have wiped out all other buffers. Only option
1350 * now is to make the current buffer empty. */
1351 return empty_curbuf(FALSE, forceit, action);
1352 }
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 /*
1355 * make buf current buffer
1356 */
1357 if (action == DOBUF_SPLIT) /* split window first */
1358 {
1359# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001360 /* If 'switchbuf' contains "useopen": jump to first window containing
1361 * "buf" if one exists */
1362 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001363 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001364 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001365 * page containing "buf" if one exists */
1366 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 return OK;
1368 if (win_split(0, 0) == FAIL)
1369# endif
1370 return FAIL;
1371 }
1372#endif
1373
1374 /* go to current buffer - nothing to do */
1375 if (buf == curbuf)
1376 return OK;
1377
1378 /*
1379 * Check if the current buffer may be abandoned.
1380 */
1381 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1382 {
1383#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1384 if ((p_confirm || cmdmod.confirm) && p_write)
1385 {
1386 dialog_changed(curbuf, FALSE);
1387# ifdef FEAT_AUTOCMD
1388 if (!buf_valid(buf))
1389 /* Autocommand deleted buffer, oops! */
1390 return FAIL;
1391# endif
1392 }
1393 if (bufIsChanged(curbuf))
1394#endif
1395 {
1396 EMSG(_(e_nowrtmsg));
1397 return FAIL;
1398 }
1399 }
1400
1401 /* Go to the other buffer. */
1402 set_curbuf(buf, action);
1403
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001404#if defined(FEAT_LISTCMDS) \
1405 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001407 {
1408 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#endif
1411
1412#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1413 if (aborting()) /* autocmds may abort script processing */
1414 return FAIL;
1415#endif
1416
1417 return OK;
1418}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421/*
1422 * Set current buffer to "buf". Executes autocommands and closes current
1423 * buffer. "action" tells how to close the current buffer:
1424 * DOBUF_GOTO free or hide it
1425 * DOBUF_SPLIT nothing
1426 * DOBUF_UNLOAD unload it
1427 * DOBUF_DEL delete it
1428 * DOBUF_WIPE wipe it out
1429 */
1430 void
1431set_curbuf(buf, action)
1432 buf_T *buf;
1433 int action;
1434{
1435 buf_T *prevbuf;
1436 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1437 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001438#ifdef FEAT_SYN_HL
1439 long old_tw = curbuf->b_p_tw;
1440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001443 if (!cmdmod.keepalt)
1444 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001445 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /* Don't restart Select mode after switching to another buffer. */
1448 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450 /* close_windows() or apply_autocmds() may change curbuf */
1451 prevbuf = curbuf;
1452
1453#ifdef FEAT_AUTOCMD
1454 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1455# ifdef FEAT_EVAL
1456 if (buf_valid(prevbuf) && !aborting())
1457# else
1458 if (buf_valid(prevbuf))
1459# endif
1460#endif
1461 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001462#ifdef FEAT_SYN_HL
1463 if (prevbuf == curwin->w_buffer)
1464 reset_synblock(curwin);
1465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#ifdef FEAT_WINDOWS
1467 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001468 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#endif
1470#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1471 if (buf_valid(prevbuf) && !aborting())
1472#else
1473 if (buf_valid(prevbuf))
1474#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001475 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001476#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001477 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001478#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001479 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001480 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1482 unload ? action : (action == DOBUF_GOTO
1483 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001484 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001485#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001486 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001487 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001488 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001489#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001493 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001494 * it did ":bunload") or aborted the script processing!
1495 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1496 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001497# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001498 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001499# endif
1500# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001501 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001502# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001503 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001507#ifdef FEAT_SYN_HL
1508 if (old_tw != curbuf->b_p_tw)
1509 check_colorcolumn(curwin);
1510#endif
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512}
1513
1514/*
1515 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001516 * Old curbuf must have been abandoned already! This also means "curbuf" may
1517 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
1519 void
1520enter_buffer(buf)
1521 buf_T *buf;
1522{
1523 /* Copy buffer and window local option values. Not for a help buffer. */
1524 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1525 if (!buf->b_help)
1526 get_winopts(buf);
1527#ifdef FEAT_FOLDING
1528 else
1529 /* Remove all folds in the window. */
1530 clearFolding(curwin);
1531 foldUpdateAll(curwin); /* update folds (later). */
1532#endif
1533
1534 /* Get the buffer in the current window. */
1535 curwin->w_buffer = buf;
1536 curbuf = buf;
1537 ++curbuf->b_nwindows;
1538
1539#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001540 if (curwin->w_p_diff)
1541 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#endif
1543
Bram Moolenaara971b822011-09-14 14:43:25 +02001544#ifdef FEAT_SYN_HL
1545 curwin->w_s = &(buf->b_s);
1546#endif
1547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 /* Cursor on first line by default. */
1549 curwin->w_cursor.lnum = 1;
1550 curwin->w_cursor.col = 0;
1551#ifdef FEAT_VIRTUALEDIT
1552 curwin->w_cursor.coladd = 0;
1553#endif
1554 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001555#ifdef FEAT_AUTOCMD
1556 curwin->w_topline_was_set = FALSE;
1557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001559 /* mark cursor position as being invalid */
1560 curwin->w_valid = 0;
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 /* Make sure the buffer is loaded. */
1563 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001564 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001565#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001566 /* If there is no filetype, allow for detecting one. Esp. useful for
1567 * ":ball" used in a autocommand. If there already is a filetype we
1568 * might prefer to keep it. */
1569 if (*curbuf->b_p_ft == NUL)
1570 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001571#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001572
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001573 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 else
1576 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001577 if (!msg_silent)
1578 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1580#ifdef FEAT_AUTOCMD
1581 curwin->w_topline = 1;
1582# ifdef FEAT_DIFF
1583 curwin->w_topfill = 0;
1584# endif
1585 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1586 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1587#endif
1588 }
1589
1590 /* If autocommands did not change the cursor position, restore cursor lnum
1591 * and possibly cursor col. */
1592 if (curwin->w_cursor.lnum == 1 && inindent(0))
1593 buflist_getfpos();
1594
1595 check_arg_idx(curwin); /* check for valid arg_idx */
1596#ifdef FEAT_TITLE
1597 maketitle();
1598#endif
1599#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001600 /* when autocmds didn't change it */
1601 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602#endif
1603 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1604
Bram Moolenaar009b2592004-10-24 19:18:58 +00001605#ifdef FEAT_NETBEANS_INTG
1606 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001607 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001608#endif
1609
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001610 /* Change directories when the 'acd' option is set. */
1611 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613#ifdef FEAT_KEYMAP
1614 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001615 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001617#ifdef FEAT_SPELL
1618 /* May need to set the spell language. Can only do this after the buffer
1619 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001620 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1621 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001622#endif
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 redraw_later(NOT_VALID);
1625}
1626
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001627#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1628/*
1629 * Change to the directory of the current buffer.
1630 */
1631 void
1632do_autochdir()
1633{
1634 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1635 shorten_fnames(TRUE);
1636}
1637#endif
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639/*
1640 * functions for dealing with the buffer list
1641 */
1642
1643/*
1644 * Add a file name to the buffer list. Return a pointer to the buffer.
1645 * If the same file name already exists return a pointer to that buffer.
1646 * If it does not exist, or if fname == NULL, a new entry is created.
1647 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1648 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1649 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * This is the ONLY way to create a new buffer.
1651 */
1652static int top_file_num = 1; /* highest file number */
1653
1654 buf_T *
1655buflist_new(ffname, sfname, lnum, flags)
1656 char_u *ffname; /* full path of fname or relative */
1657 char_u *sfname; /* short fname or NULL */
1658 linenr_T lnum; /* preferred cursor line */
1659 int flags; /* BLN_ defines */
1660{
1661 buf_T *buf;
1662#ifdef UNIX
1663 struct stat st;
1664#endif
1665
1666 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1667
1668 /*
1669 * If file name already exists in the list, update the entry.
1670 */
1671#ifdef UNIX
1672 /* On Unix we can use inode numbers when the file exists. Works better
1673 * for hard links. */
1674 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1675 st.st_dev = (dev_T)-1;
1676#endif
1677 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1678#ifdef UNIX
1679 buflist_findname_stat(ffname, &st)
1680#else
1681 buflist_findname(ffname)
1682#endif
1683 ) != NULL)
1684 {
1685 vim_free(ffname);
1686 if (lnum != 0)
1687 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1688 /* copy the options now, if 'cpo' doesn't have 's' and not done
1689 * already */
1690 buf_copy_options(buf, 0);
1691 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1692 {
1693 buf->b_p_bl = TRUE;
1694#ifdef FEAT_AUTOCMD
1695 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001698 if (!buf_valid(buf))
1699 return NULL;
1700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#endif
1702 }
1703 return buf;
1704 }
1705
1706 /*
1707 * If the current buffer has no name and no contents, use the current
1708 * buffer. Otherwise: Need to allocate a new buffer structure.
1709 *
1710 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001711 * (A spell file buffer is allocated in spell.c, but that's not a normal
1712 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 */
1714 buf = NULL;
1715 if ((flags & BLN_CURBUF)
1716 && curbuf != NULL
1717 && curbuf->b_ffname == NULL
1718 && curbuf->b_nwindows <= 1
1719 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1720 {
1721 buf = curbuf;
1722#ifdef FEAT_AUTOCMD
1723 /* It's like this buffer is deleted. Watch out for autocommands that
1724 * change curbuf! If that happens, allocate a new buffer anyway. */
1725 if (curbuf->b_p_bl)
1726 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1727 if (buf == curbuf)
1728 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1729# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001730 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 return NULL;
1732# endif
1733#endif
1734#ifdef FEAT_QUICKFIX
1735# ifdef FEAT_AUTOCMD
1736 if (buf == curbuf)
1737# endif
1738 {
1739 /* Make sure 'bufhidden' and 'buftype' are empty */
1740 clear_string_option(&buf->b_p_bh);
1741 clear_string_option(&buf->b_p_bt);
1742 }
1743#endif
1744 }
1745 if (buf != curbuf || curbuf == NULL)
1746 {
1747 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1748 if (buf == NULL)
1749 {
1750 vim_free(ffname);
1751 return NULL;
1752 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001753#ifdef FEAT_EVAL
1754 /* init b: variables */
1755 buf->b_vars = dict_alloc();
1756 if (buf->b_vars == NULL)
1757 {
1758 vim_free(ffname);
1759 vim_free(buf);
1760 return NULL;
1761 }
1762 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
1765
1766 if (ffname != NULL)
1767 {
1768 buf->b_ffname = ffname;
1769 buf->b_sfname = vim_strsave(sfname);
1770 }
1771
1772 clear_wininfo(buf);
1773 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1774
1775 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1776 || buf->b_wininfo == NULL)
1777 {
1778 vim_free(buf->b_ffname);
1779 buf->b_ffname = NULL;
1780 vim_free(buf->b_sfname);
1781 buf->b_sfname = NULL;
1782 if (buf != curbuf)
1783 free_buffer(buf);
1784 return NULL;
1785 }
1786
1787 if (buf == curbuf)
1788 {
1789 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001790 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (buf != curbuf) /* autocommands deleted the buffer! */
1792 return NULL;
1793#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001794 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 return NULL;
1796#endif
1797 /* buf->b_nwindows = 0; why was this here? */
1798 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001799
1800 /* Init the options. */
1801 buf->b_p_initialized = FALSE;
1802 buf_copy_options(buf, BCO_ENTER);
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#ifdef FEAT_KEYMAP
1805 /* need to reload lmaps and set b:keymap_name */
1806 curbuf->b_kmap_state |= KEYMAP_INIT;
1807#endif
1808 }
1809 else
1810 {
1811 /*
1812 * put new buffer at the end of the buffer list
1813 */
1814 buf->b_next = NULL;
1815 if (firstbuf == NULL) /* buffer list is empty */
1816 {
1817 buf->b_prev = NULL;
1818 firstbuf = buf;
1819 }
1820 else /* append new buffer at end of list */
1821 {
1822 lastbuf->b_next = buf;
1823 buf->b_prev = lastbuf;
1824 }
1825 lastbuf = buf;
1826
1827 buf->b_fnum = top_file_num++;
1828 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1829 {
1830 EMSG(_("W14: Warning: List of file names overflow"));
1831 if (emsg_silent == 0)
1832 {
1833 out_flush();
1834 ui_delay(3000L, TRUE); /* make sure it is noticed */
1835 }
1836 top_file_num = 1;
1837 }
1838
1839 /*
1840 * Always copy the options from the current buffer.
1841 */
1842 buf_copy_options(buf, BCO_ALWAYS);
1843 }
1844
1845 buf->b_wininfo->wi_fpos.lnum = lnum;
1846 buf->b_wininfo->wi_win = curwin;
1847
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001848#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001849 hash_init(&buf->b_s.b_keywtab);
1850 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851#endif
1852
1853 buf->b_fname = buf->b_sfname;
1854#ifdef UNIX
1855 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001856 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 else
1858 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001859 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 buf->b_dev = st.st_dev;
1861 buf->b_ino = st.st_ino;
1862 }
1863#endif
1864 buf->b_u_synced = TRUE;
1865 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001866 if (flags & BLN_DUMMY)
1867 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 buf_clear_file(buf);
1869 clrallmarks(buf); /* clear marks */
1870 fmarks_check_names(buf); /* check file marks for this file */
1871 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1872#ifdef FEAT_AUTOCMD
1873 if (!(flags & BLN_DUMMY))
1874 {
1875 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001876 if (!buf_valid(buf))
1877 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001881 if (!buf_valid(buf))
1882 return NULL;
1883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001885 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 return NULL;
1887# endif
1888 }
1889#endif
1890
1891 return buf;
1892}
1893
1894/*
1895 * Free the memory for the options of a buffer.
1896 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1897 * 'fileencoding'.
1898 */
1899 void
1900free_buf_options(buf, free_p_ff)
1901 buf_T *buf;
1902 int free_p_ff;
1903{
1904 if (free_p_ff)
1905 {
1906#ifdef FEAT_MBYTE
1907 clear_string_option(&buf->b_p_fenc);
1908#endif
1909 clear_string_option(&buf->b_p_ff);
1910#ifdef FEAT_QUICKFIX
1911 clear_string_option(&buf->b_p_bh);
1912 clear_string_option(&buf->b_p_bt);
1913#endif
1914 }
1915#ifdef FEAT_FIND_ID
1916 clear_string_option(&buf->b_p_def);
1917 clear_string_option(&buf->b_p_inc);
1918# ifdef FEAT_EVAL
1919 clear_string_option(&buf->b_p_inex);
1920# endif
1921#endif
1922#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1923 clear_string_option(&buf->b_p_inde);
1924 clear_string_option(&buf->b_p_indk);
1925#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001926#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1927 clear_string_option(&buf->b_p_bexpr);
1928#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001929#if defined(FEAT_CRYPT)
1930 clear_string_option(&buf->b_p_cm);
1931#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001932#if defined(FEAT_EVAL)
1933 clear_string_option(&buf->b_p_fex);
1934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935#ifdef FEAT_CRYPT
1936 clear_string_option(&buf->b_p_key);
1937#endif
1938 clear_string_option(&buf->b_p_kp);
1939 clear_string_option(&buf->b_p_mps);
1940 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001941 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 clear_string_option(&buf->b_p_isk);
1943#ifdef FEAT_KEYMAP
1944 clear_string_option(&buf->b_p_keymap);
1945 ga_clear(&buf->b_kmap_ga);
1946#endif
1947#ifdef FEAT_COMMENTS
1948 clear_string_option(&buf->b_p_com);
1949#endif
1950#ifdef FEAT_FOLDING
1951 clear_string_option(&buf->b_p_cms);
1952#endif
1953 clear_string_option(&buf->b_p_nf);
1954#ifdef FEAT_SYN_HL
1955 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001956#endif
1957#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001958 clear_string_option(&buf->b_s.b_p_spc);
1959 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001960 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001961 buf->b_s.b_cap_prog = NULL;
1962 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963#endif
1964#ifdef FEAT_SEARCHPATH
1965 clear_string_option(&buf->b_p_sua);
1966#endif
1967#ifdef FEAT_AUTOCMD
1968 clear_string_option(&buf->b_p_ft);
1969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970#ifdef FEAT_CINDENT
1971 clear_string_option(&buf->b_p_cink);
1972 clear_string_option(&buf->b_p_cino);
1973#endif
1974#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1975 clear_string_option(&buf->b_p_cinw);
1976#endif
1977#ifdef FEAT_INS_EXPAND
1978 clear_string_option(&buf->b_p_cpt);
1979#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001980#ifdef FEAT_COMPL_FUNC
1981 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001982 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#ifdef FEAT_QUICKFIX
1985 clear_string_option(&buf->b_p_gp);
1986 clear_string_option(&buf->b_p_mp);
1987 clear_string_option(&buf->b_p_efm);
1988#endif
1989 clear_string_option(&buf->b_p_ep);
1990 clear_string_option(&buf->b_p_path);
1991 clear_string_option(&buf->b_p_tags);
1992#ifdef FEAT_INS_EXPAND
1993 clear_string_option(&buf->b_p_dict);
1994 clear_string_option(&buf->b_p_tsr);
1995#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001996#ifdef FEAT_TEXTOBJ
1997 clear_string_option(&buf->b_p_qe);
1998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002000 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002001#ifdef FEAT_LISP
2002 clear_string_option(&buf->b_p_lw);
2003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004}
2005
2006/*
2007 * get alternate file n
2008 * set linenr to lnum or altfpos.lnum if lnum == 0
2009 * also set cursor column to altfpos.col if 'startofline' is not set.
2010 * if (options & GETF_SETMARK) call setpcmark()
2011 * if (options & GETF_ALT) we are jumping to an alternate file.
2012 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2013 *
2014 * return FAIL for failure, OK for success
2015 */
2016 int
2017buflist_getfile(n, lnum, options, forceit)
2018 int n;
2019 linenr_T lnum;
2020 int options;
2021 int forceit;
2022{
2023 buf_T *buf;
2024#ifdef FEAT_WINDOWS
2025 win_T *wp = NULL;
2026#endif
2027 pos_T *fpos;
2028 colnr_T col;
2029
2030 buf = buflist_findnr(n);
2031 if (buf == NULL)
2032 {
2033 if ((options & GETF_ALT) && n == 0)
2034 EMSG(_(e_noalt));
2035 else
2036 EMSGN(_("E92: Buffer %ld not found"), n);
2037 return FAIL;
2038 }
2039
2040 /* if alternate file is the current buffer, nothing to do */
2041 if (buf == curbuf)
2042 return OK;
2043
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002044 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002045 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002046 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002048 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002049#ifdef FEAT_AUTOCMD
2050 if (curbuf_locked())
2051 return FAIL;
2052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 /* altfpos may be changed by getfile(), get it now */
2055 if (lnum == 0)
2056 {
2057 fpos = buflist_findfpos(buf);
2058 lnum = fpos->lnum;
2059 col = fpos->col;
2060 }
2061 else
2062 col = 0;
2063
2064#ifdef FEAT_WINDOWS
2065 if (options & GETF_SWITCH)
2066 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002067 /* If 'switchbuf' contains "useopen": jump to first window containing
2068 * "buf" if one exists */
2069 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 wp = buf_jump_open_win(buf);
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002071 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002072 * page containing "buf" if one exists */
2073 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002074 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00002075 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
2076 * isn't empty: open new window */
2077 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002079 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
2080 tabpage_new();
2081 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002083 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 }
2086#endif
2087
2088 ++RedrawingDisabled;
2089 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2090 lnum, forceit) <= 0)
2091 {
2092 --RedrawingDisabled;
2093
2094 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2095 if (!p_sol && col != 0)
2096 {
2097 curwin->w_cursor.col = col;
2098 check_cursor_col();
2099#ifdef FEAT_VIRTUALEDIT
2100 curwin->w_cursor.coladd = 0;
2101#endif
2102 curwin->w_set_curswant = TRUE;
2103 }
2104 return OK;
2105 }
2106 --RedrawingDisabled;
2107 return FAIL;
2108}
2109
2110/*
2111 * go to the last know line number for the current buffer
2112 */
2113 void
2114buflist_getfpos()
2115{
2116 pos_T *fpos;
2117
2118 fpos = buflist_findfpos(curbuf);
2119
2120 curwin->w_cursor.lnum = fpos->lnum;
2121 check_cursor_lnum();
2122
2123 if (p_sol)
2124 curwin->w_cursor.col = 0;
2125 else
2126 {
2127 curwin->w_cursor.col = fpos->col;
2128 check_cursor_col();
2129#ifdef FEAT_VIRTUALEDIT
2130 curwin->w_cursor.coladd = 0;
2131#endif
2132 curwin->w_set_curswant = TRUE;
2133 }
2134}
2135
Bram Moolenaar81695252004-12-29 20:58:21 +00002136#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2137/*
2138 * Find file in buffer list by name (it has to be for the current window).
2139 * Returns NULL if not found.
2140 */
2141 buf_T *
2142buflist_findname_exp(fname)
2143 char_u *fname;
2144{
2145 char_u *ffname;
2146 buf_T *buf = NULL;
2147
2148 /* First make the name into a full path name */
2149 ffname = FullName_save(fname,
2150#ifdef UNIX
2151 TRUE /* force expansion, get rid of symbolic links */
2152#else
2153 FALSE
2154#endif
2155 );
2156 if (ffname != NULL)
2157 {
2158 buf = buflist_findname(ffname);
2159 vim_free(ffname);
2160 }
2161 return buf;
2162}
2163#endif
2164
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165/*
2166 * Find file in buffer list by name (it has to be for the current window).
2167 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002168 * Skips dummy buffers.
2169 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 */
2171 buf_T *
2172buflist_findname(ffname)
2173 char_u *ffname;
2174{
2175#ifdef UNIX
2176 struct stat st;
2177
2178 if (mch_stat((char *)ffname, &st) < 0)
2179 st.st_dev = (dev_T)-1;
2180 return buflist_findname_stat(ffname, &st);
2181}
2182
2183/*
2184 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2185 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002186 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 */
2188 static buf_T *
2189buflist_findname_stat(ffname, stp)
2190 char_u *ffname;
2191 struct stat *stp;
2192{
2193#endif
2194 buf_T *buf;
2195
2196 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002197 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#ifdef UNIX
2199 , stp
2200#endif
2201 ))
2202 return buf;
2203 return NULL;
2204}
2205
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002206#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2207 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208/*
2209 * Find file in buffer list by a regexp pattern.
2210 * Return fnum of the found buffer.
2211 * Return < 0 for error.
2212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 int
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002214buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 char_u *pattern;
2216 char_u *pattern_end; /* pointer to first char after pattern */
2217 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002218 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002219 int curtab_only; /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 buf_T *buf;
2222 regprog_T *prog;
2223 int match = -1;
2224 int find_listed;
2225 char_u *pat;
2226 char_u *patend;
2227 int attempt;
2228 char_u *p;
2229 int toggledollar;
2230
2231 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2232 {
2233 if (*pattern == '%')
2234 match = curbuf->b_fnum;
2235 else
2236 match = curwin->w_alt_fnum;
2237#ifdef FEAT_DIFF
2238 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2239 match = -1;
2240#endif
2241 }
2242
2243 /*
2244 * Try four ways of matching a listed buffer:
2245 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002246 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 * attempt == 2: with '$' at end (only match at end)
2248 * attempt == 3: with '^' at start and '$' at end (only full match)
2249 * Repeat this for finding an unlisted buffer if there was no matching
2250 * listed buffer.
2251 */
2252 else
2253 {
2254 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2255 if (pat == NULL)
2256 return -1;
2257 patend = pat + STRLEN(pat) - 1;
2258 toggledollar = (patend > pat && *patend == '$');
2259
2260 /* First try finding a listed buffer. If not found and "unlisted"
2261 * is TRUE, try finding an unlisted buffer. */
2262 find_listed = TRUE;
2263 for (;;)
2264 {
2265 for (attempt = 0; attempt <= 3; ++attempt)
2266 {
2267 /* may add '^' and '$' */
2268 if (toggledollar)
2269 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2270 p = pat;
2271 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2272 ++p;
2273 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2274 if (prog == NULL)
2275 {
2276 vim_free(pat);
2277 return -1;
2278 }
2279
2280 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2281 if (buf->b_p_bl == find_listed
2282#ifdef FEAT_DIFF
2283 && (!diffmode || diff_mode_buf(buf))
2284#endif
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002285 && buflist_match(prog, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002287 if (curtab_only)
2288 {
2289 /* Ignore the match if the buffer is not open in
2290 * the current tab. */
2291#ifdef FEAT_WINDOWS
2292 win_T *wp;
2293
2294 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2295 if (wp->w_buffer == buf)
2296 break;
2297 if (wp == NULL)
2298 continue;
2299#else
2300 if (curwin->w_buffer != buf)
2301 continue;
2302#endif
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (match >= 0) /* already found a match */
2305 {
2306 match = -2;
2307 break;
2308 }
2309 match = buf->b_fnum; /* remember first match */
2310 }
2311
Bram Moolenaar473de612013-06-08 18:19:48 +02002312 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (match >= 0) /* found one match */
2314 break;
2315 }
2316
2317 /* Only search for unlisted buffers if there was no match with
2318 * a listed buffer. */
2319 if (!unlisted || !find_listed || match != -1)
2320 break;
2321 find_listed = FALSE;
2322 }
2323
2324 vim_free(pat);
2325 }
2326
2327 if (match == -2)
2328 EMSG2(_("E93: More than one match for %s"), pattern);
2329 else if (match < 0)
2330 EMSG2(_("E94: No matching buffer for %s"), pattern);
2331 return match;
2332}
2333#endif
2334
2335#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2336
2337/*
2338 * Find all buffer names that match.
2339 * For command line expansion of ":buf" and ":sbuf".
2340 * Return OK if matches found, FAIL otherwise.
2341 */
2342 int
2343ExpandBufnames(pat, num_file, file, options)
2344 char_u *pat;
2345 int *num_file;
2346 char_u ***file;
2347 int options;
2348{
2349 int count = 0;
2350 buf_T *buf;
2351 int round;
2352 char_u *p;
2353 int attempt;
2354 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002355 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357 *num_file = 0; /* return values in case of FAIL */
2358 *file = NULL;
2359
Bram Moolenaar05159a02005-02-26 23:04:13 +00002360 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2361 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002363 patc = alloc((unsigned)STRLEN(pat) + 11);
2364 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002366 STRCPY(patc, "\\(^\\|[\\/]\\)");
2367 STRCPY(patc + 11, pat + 1);
2368 }
2369 else
2370 patc = pat;
2371
2372 /*
2373 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002374 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002375 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002376 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002377 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002378 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002379 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002380 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002381 if (prog == NULL)
2382 {
2383 if (patc != pat)
2384 vim_free(patc);
2385 return FAIL;
2386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 /*
2389 * round == 1: Count the matches.
2390 * round == 2: Build the array to keep the matches.
2391 */
2392 for (round = 1; round <= 2; ++round)
2393 {
2394 count = 0;
2395 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2396 {
2397 if (!buf->b_p_bl) /* skip unlisted buffers */
2398 continue;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002399 p = buflist_match(prog, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (p != NULL)
2401 {
2402 if (round == 1)
2403 ++count;
2404 else
2405 {
2406 if (options & WILD_HOME_REPLACE)
2407 p = home_replace_save(buf, p);
2408 else
2409 p = vim_strsave(p);
2410 (*file)[count++] = p;
2411 }
2412 }
2413 }
2414 if (count == 0) /* no match found, break here */
2415 break;
2416 if (round == 1)
2417 {
2418 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2419 if (*file == NULL)
2420 {
Bram Moolenaar473de612013-06-08 18:19:48 +02002421 vim_regfree(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002422 if (patc != pat)
2423 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 return FAIL;
2425 }
2426 }
2427 }
Bram Moolenaar473de612013-06-08 18:19:48 +02002428 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (count) /* match(es) found, break here */
2430 break;
2431 }
2432
Bram Moolenaar05159a02005-02-26 23:04:13 +00002433 if (patc != pat)
2434 vim_free(patc);
2435
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 *num_file = count;
2437 return (count == 0 ? FAIL : OK);
2438}
2439
2440#endif /* FEAT_CMDL_COMPL */
2441
2442#ifdef HAVE_BUFLIST_MATCH
2443/*
2444 * Check for a match on the file name for buffer "buf" with regprog "prog".
2445 */
2446 static char_u *
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002447buflist_match(prog, buf, ignore_case)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 regprog_T *prog;
2449 buf_T *buf;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002450 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 char_u *match;
2453
2454 /* First try the short file name, then the long file name. */
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002455 match = fname_match(prog, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (match == NULL)
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002457 match = fname_match(prog, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 return match;
2460}
2461
2462/*
2463 * Try matching the regexp in "prog" with file name "name".
2464 * Return "name" when there is a match, NULL when not.
2465 */
2466 static char_u *
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002467fname_match(prog, name, ignore_case)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 regprog_T *prog;
2469 char_u *name;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002470 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 char_u *match = NULL;
2473 char_u *p;
2474 regmatch_T regmatch;
2475
2476 if (name != NULL)
2477 {
2478 regmatch.regprog = prog;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002479 /* Ignore case when 'fileignorecase' or the argument is set. */
2480 regmatch.rm_ic = p_fic || ignore_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (vim_regexec(&regmatch, name, (colnr_T)0))
2482 match = name;
2483 else
2484 {
2485 /* Replace $(HOME) with '~' and try matching again. */
2486 p = home_replace_save(NULL, name);
2487 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2488 match = name;
2489 vim_free(p);
2490 }
2491 }
2492
2493 return match;
2494}
2495#endif
2496
2497/*
2498 * find file in buffer list by number
2499 */
2500 buf_T *
2501buflist_findnr(nr)
2502 int nr;
2503{
2504 buf_T *buf;
2505
2506 if (nr == 0)
2507 nr = curwin->w_alt_fnum;
2508 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2509 if (buf->b_fnum == nr)
2510 return (buf);
2511 return NULL;
2512}
2513
2514/*
2515 * Get name of file 'n' in the buffer list.
2516 * When the file has no name an empty string is returned.
2517 * home_replace() is used to shorten the file name (used for marks).
2518 * Returns a pointer to allocated memory, of NULL when failed.
2519 */
2520 char_u *
2521buflist_nr2name(n, fullname, helptail)
2522 int n;
2523 int fullname;
2524 int helptail; /* for help buffers return tail only */
2525{
2526 buf_T *buf;
2527
2528 buf = buflist_findnr(n);
2529 if (buf == NULL)
2530 return NULL;
2531 return home_replace_save(helptail ? buf : NULL,
2532 fullname ? buf->b_ffname : buf->b_fname);
2533}
2534
2535/*
2536 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2537 * When "copy_options" is TRUE save the local window option values.
2538 * When "lnum" is 0 only do the options.
2539 */
2540 static void
2541buflist_setfpos(buf, win, lnum, col, copy_options)
2542 buf_T *buf;
2543 win_T *win;
2544 linenr_T lnum;
2545 colnr_T col;
2546 int copy_options;
2547{
2548 wininfo_T *wip;
2549
2550 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2551 if (wip->wi_win == win)
2552 break;
2553 if (wip == NULL)
2554 {
2555 /* allocate a new entry */
2556 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2557 if (wip == NULL)
2558 return;
2559 wip->wi_win = win;
2560 if (lnum == 0) /* set lnum even when it's 0 */
2561 lnum = 1;
2562 }
2563 else
2564 {
2565 /* remove the entry from the list */
2566 if (wip->wi_prev)
2567 wip->wi_prev->wi_next = wip->wi_next;
2568 else
2569 buf->b_wininfo = wip->wi_next;
2570 if (wip->wi_next)
2571 wip->wi_next->wi_prev = wip->wi_prev;
2572 if (copy_options && wip->wi_optset)
2573 {
2574 clear_winopt(&wip->wi_opt);
2575#ifdef FEAT_FOLDING
2576 deleteFoldRecurse(&wip->wi_folds);
2577#endif
2578 }
2579 }
2580 if (lnum != 0)
2581 {
2582 wip->wi_fpos.lnum = lnum;
2583 wip->wi_fpos.col = col;
2584 }
2585 if (copy_options)
2586 {
2587 /* Save the window-specific option values. */
2588 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2589#ifdef FEAT_FOLDING
2590 wip->wi_fold_manual = win->w_fold_manual;
2591 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2592#endif
2593 wip->wi_optset = TRUE;
2594 }
2595
2596 /* insert the entry in front of the list */
2597 wip->wi_next = buf->b_wininfo;
2598 buf->b_wininfo = wip;
2599 wip->wi_prev = NULL;
2600 if (wip->wi_next)
2601 wip->wi_next->wi_prev = wip;
2602
2603 return;
2604}
2605
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002606#ifdef FEAT_DIFF
2607static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2608
2609/*
2610 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2611 * page. That's because a diff is local to a tab page.
2612 */
2613 static int
2614wininfo_other_tab_diff(wip)
2615 wininfo_T *wip;
2616{
2617 win_T *wp;
2618
2619 if (wip->wi_opt.wo_diff)
2620 {
2621 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2622 /* return FALSE when it's a window in the current tab page, thus
2623 * the buffer was in diff mode here */
2624 if (wip->wi_win == wp)
2625 return FALSE;
2626 return TRUE;
2627 }
2628 return FALSE;
2629}
2630#endif
2631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632/*
2633 * Find info for the current window in buffer "buf".
2634 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002635 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2636 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 * Returns NULL when there isn't any info.
2638 */
2639 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002640find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002642 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 wininfo_T *wip;
2645
2646 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002647 if (wip->wi_win == curwin
2648#ifdef FEAT_DIFF
2649 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2650#endif
2651 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002653
2654 /* If no wininfo for curwin, use the first in the list (that doesn't have
2655 * 'diff' set and is in another tab page). */
2656 if (wip == NULL)
2657 {
2658#ifdef FEAT_DIFF
2659 if (skip_diff_buffer)
2660 {
2661 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2662 if (!wininfo_other_tab_diff(wip))
2663 break;
2664 }
2665 else
2666#endif
2667 wip = buf->b_wininfo;
2668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 return wip;
2670}
2671
2672/*
2673 * Reset the local window options to the values last used in this window.
2674 * If the buffer wasn't used in this window before, use the values from
2675 * the most recently used window. If the values were never set, use the
2676 * global values for the window.
2677 */
2678 void
2679get_winopts(buf)
2680 buf_T *buf;
2681{
2682 wininfo_T *wip;
2683
2684 clear_winopt(&curwin->w_onebuf_opt);
2685#ifdef FEAT_FOLDING
2686 clearFolding(curwin);
2687#endif
2688
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002689 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (wip != NULL && wip->wi_optset)
2691 {
2692 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2693#ifdef FEAT_FOLDING
2694 curwin->w_fold_manual = wip->wi_fold_manual;
2695 curwin->w_foldinvalid = TRUE;
2696 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2697#endif
2698 }
2699 else
2700 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2701
2702#ifdef FEAT_FOLDING
2703 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2704 if (p_fdls >= 0)
2705 curwin->w_p_fdl = p_fdls;
2706#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002707#ifdef FEAT_SYN_HL
2708 check_colorcolumn(curwin);
2709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710}
2711
2712/*
2713 * Find the position (lnum and col) for the buffer 'buf' for the current
2714 * window.
2715 * Returns a pointer to no_position if no position is found.
2716 */
2717 pos_T *
2718buflist_findfpos(buf)
2719 buf_T *buf;
2720{
2721 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002722 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002724 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (wip != NULL)
2726 return &(wip->wi_fpos);
2727 else
2728 return &no_position;
2729}
2730
2731/*
2732 * Find the lnum for the buffer 'buf' for the current window.
2733 */
2734 linenr_T
2735buflist_findlnum(buf)
2736 buf_T *buf;
2737{
2738 return buflist_findfpos(buf)->lnum;
2739}
2740
2741#if defined(FEAT_LISTCMDS) || defined(PROTO)
2742/*
2743 * List all know file names (for :files and :buffers command).
2744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 void
2746buflist_list(eap)
2747 exarg_T *eap;
2748{
2749 buf_T *buf;
2750 int len;
2751 int i;
2752
2753 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2754 {
2755 /* skip unlisted buffers, unless ! was used */
2756 if (!buf->b_p_bl && !eap->forceit)
2757 continue;
2758 msg_putchar('\n');
2759 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002760 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 else
2762 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2763
Bram Moolenaar50cde822005-06-05 21:54:54 +00002764 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 buf->b_fnum,
2766 buf->b_p_bl ? ' ' : 'u',
2767 buf == curbuf ? '%' :
2768 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2769 buf->b_ml.ml_mfp == NULL ? ' ' :
2770 (buf->b_nwindows == 0 ? 'h' : 'a'),
2771 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2772 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002773 : (bufIsChanged(buf) ? '+' : ' '),
2774 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 i = 40 - vim_strsize(IObuff);
2778 do
2779 {
2780 IObuff[len++] = ' ';
2781 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002782 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2783 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002784 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 msg_outtrans(IObuff);
2786 out_flush(); /* output one line at a time */
2787 ui_breakcheck();
2788 }
2789}
2790#endif
2791
2792/*
2793 * Get file name and line number for file 'fnum'.
2794 * Used by DoOneCmd() for translating '%' and '#'.
2795 * Used by insert_reg() and cmdline_paste() for '#' register.
2796 * Return FAIL if not found, OK for success.
2797 */
2798 int
2799buflist_name_nr(fnum, fname, lnum)
2800 int fnum;
2801 char_u **fname;
2802 linenr_T *lnum;
2803{
2804 buf_T *buf;
2805
2806 buf = buflist_findnr(fnum);
2807 if (buf == NULL || buf->b_fname == NULL)
2808 return FAIL;
2809
2810 *fname = buf->b_fname;
2811 *lnum = buflist_findlnum(buf);
2812
2813 return OK;
2814}
2815
2816/*
2817 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2818 * The file name with the full path is also remembered, for when :cd is used.
2819 * Returns FAIL for failure (file name already in use by other buffer)
2820 * OK otherwise.
2821 */
2822 int
2823setfname(buf, ffname, sfname, message)
2824 buf_T *buf;
2825 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002826 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
Bram Moolenaar81695252004-12-29 20:58:21 +00002828 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#ifdef UNIX
2830 struct stat st;
2831#endif
2832
2833 if (ffname == NULL || *ffname == NUL)
2834 {
2835 /* Removing the name. */
2836 vim_free(buf->b_ffname);
2837 vim_free(buf->b_sfname);
2838 buf->b_ffname = NULL;
2839 buf->b_sfname = NULL;
2840#ifdef UNIX
2841 st.st_dev = (dev_T)-1;
2842#endif
2843 }
2844 else
2845 {
2846 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2847 if (ffname == NULL) /* out of memory */
2848 return FAIL;
2849
2850 /*
2851 * if the file name is already used in another buffer:
2852 * - if the buffer is loaded, fail
2853 * - if the buffer is not loaded, delete it from the list
2854 */
2855#ifdef UNIX
2856 if (mch_stat((char *)ffname, &st) < 0)
2857 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002858#endif
2859 if (!(buf->b_flags & BF_DUMMY))
2860#ifdef UNIX
2861 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002863 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#endif
2865 if (obuf != NULL && obuf != buf)
2866 {
2867 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2868 {
2869 if (message)
2870 EMSG(_("E95: Buffer with this name already exists"));
2871 vim_free(ffname);
2872 return FAIL;
2873 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002874 /* delete from the list */
2875 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
2877 sfname = vim_strsave(sfname);
2878 if (ffname == NULL || sfname == NULL)
2879 {
2880 vim_free(sfname);
2881 vim_free(ffname);
2882 return FAIL;
2883 }
2884#ifdef USE_FNAME_CASE
2885# ifdef USE_LONG_FNAME
2886 if (USE_LONG_FNAME)
2887# endif
2888 fname_case(sfname, 0); /* set correct case for short file name */
2889#endif
2890 vim_free(buf->b_ffname);
2891 vim_free(buf->b_sfname);
2892 buf->b_ffname = ffname;
2893 buf->b_sfname = sfname;
2894 }
2895 buf->b_fname = buf->b_sfname;
2896#ifdef UNIX
2897 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002898 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 else
2900 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002901 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 buf->b_dev = st.st_dev;
2903 buf->b_ino = st.st_ino;
2904 }
2905#endif
2906
2907#ifndef SHORT_FNAME
2908 buf->b_shortname = FALSE;
2909#endif
2910
2911 buf_name_changed(buf);
2912 return OK;
2913}
2914
2915/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002916 * Crude way of changing the name of a buffer. Use with care!
2917 * The name should be relative to the current directory.
2918 */
2919 void
2920buf_set_name(fnum, name)
2921 int fnum;
2922 char_u *name;
2923{
2924 buf_T *buf;
2925
2926 buf = buflist_findnr(fnum);
2927 if (buf != NULL)
2928 {
2929 vim_free(buf->b_sfname);
2930 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002931 buf->b_ffname = vim_strsave(name);
2932 buf->b_sfname = NULL;
2933 /* Allocate ffname and expand into full path. Also resolves .lnk
2934 * files on Win32. */
2935 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002936 buf->b_fname = buf->b_sfname;
2937 }
2938}
2939
2940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 * Take care of what needs to be done when the name of buffer "buf" has
2942 * changed.
2943 */
2944 void
2945buf_name_changed(buf)
2946 buf_T *buf;
2947{
2948 /*
2949 * If the file name changed, also change the name of the swapfile
2950 */
2951 if (buf->b_ml.ml_mfp != NULL)
2952 ml_setname(buf);
2953
2954 if (curwin->w_buffer == buf)
2955 check_arg_idx(curwin); /* check file name for arg list */
2956#ifdef FEAT_TITLE
2957 maketitle(); /* set window title */
2958#endif
2959#ifdef FEAT_WINDOWS
2960 status_redraw_all(); /* status lines need to be redrawn */
2961#endif
2962 fmarks_check_names(buf); /* check named file marks */
2963 ml_timestamp(buf); /* reset timestamp */
2964}
2965
2966/*
2967 * set alternate file name for current window
2968 *
2969 * Used by do_one_cmd(), do_write() and do_ecmd().
2970 * Return the buffer.
2971 */
2972 buf_T *
2973setaltfname(ffname, sfname, lnum)
2974 char_u *ffname;
2975 char_u *sfname;
2976 linenr_T lnum;
2977{
2978 buf_T *buf;
2979
2980 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2981 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002982 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 curwin->w_alt_fnum = buf->b_fnum;
2984 return buf;
2985}
2986
2987/*
2988 * Get alternate file name for current window.
2989 * Return NULL if there isn't any, and give error message if requested.
2990 */
2991 char_u *
2992getaltfname(errmsg)
2993 int errmsg; /* give error message */
2994{
2995 char_u *fname;
2996 linenr_T dummy;
2997
2998 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2999 {
3000 if (errmsg)
3001 EMSG(_(e_noalt));
3002 return NULL;
3003 }
3004 return fname;
3005}
3006
3007/*
3008 * Add a file name to the buflist and return its number.
3009 * Uses same flags as buflist_new(), except BLN_DUMMY.
3010 *
3011 * used by qf_init(), main() and doarglist()
3012 */
3013 int
3014buflist_add(fname, flags)
3015 char_u *fname;
3016 int flags;
3017{
3018 buf_T *buf;
3019
3020 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3021 if (buf != NULL)
3022 return buf->b_fnum;
3023 return 0;
3024}
3025
3026#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3027/*
3028 * Adjust slashes in file names. Called after 'shellslash' was set.
3029 */
3030 void
3031buflist_slash_adjust()
3032{
3033 buf_T *bp;
3034
3035 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3036 {
3037 if (bp->b_ffname != NULL)
3038 slash_adjust(bp->b_ffname);
3039 if (bp->b_sfname != NULL)
3040 slash_adjust(bp->b_sfname);
3041 }
3042}
3043#endif
3044
3045/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003046 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 * Also save the local window option values.
3048 */
3049 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003050buflist_altfpos(win)
3051 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003053 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054}
3055
3056/*
3057 * Return TRUE if 'ffname' is not the same file as current file.
3058 * Fname must have a full path (expanded by mch_FullName()).
3059 */
3060 int
3061otherfile(ffname)
3062 char_u *ffname;
3063{
3064 return otherfile_buf(curbuf, ffname
3065#ifdef UNIX
3066 , NULL
3067#endif
3068 );
3069}
3070
3071 static int
3072otherfile_buf(buf, ffname
3073#ifdef UNIX
3074 , stp
3075#endif
3076 )
3077 buf_T *buf;
3078 char_u *ffname;
3079#ifdef UNIX
3080 struct stat *stp;
3081#endif
3082{
3083 /* no name is different */
3084 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3085 return TRUE;
3086 if (fnamecmp(ffname, buf->b_ffname) == 0)
3087 return FALSE;
3088#ifdef UNIX
3089 {
3090 struct stat st;
3091
3092 /* If no struct stat given, get it now */
3093 if (stp == NULL)
3094 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003095 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 st.st_dev = (dev_T)-1;
3097 stp = &st;
3098 }
3099 /* Use dev/ino to check if the files are the same, even when the names
3100 * are different (possible with links). Still need to compare the
3101 * name above, for when the file doesn't exist yet.
3102 * Problem: The dev/ino changes when a file is deleted (and created
3103 * again) and remains the same when renamed/moved. We don't want to
3104 * mch_stat() each buffer each time, that would be too slow. Get the
3105 * dev/ino again when they appear to match, but not when they appear
3106 * to be different: Could skip a buffer when it's actually the same
3107 * file. */
3108 if (buf_same_ino(buf, stp))
3109 {
3110 buf_setino(buf);
3111 if (buf_same_ino(buf, stp))
3112 return FALSE;
3113 }
3114 }
3115#endif
3116 return TRUE;
3117}
3118
3119#if defined(UNIX) || defined(PROTO)
3120/*
3121 * Set inode and device number for a buffer.
3122 * Must always be called when b_fname is changed!.
3123 */
3124 void
3125buf_setino(buf)
3126 buf_T *buf;
3127{
3128 struct stat st;
3129
3130 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3131 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003132 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 buf->b_dev = st.st_dev;
3134 buf->b_ino = st.st_ino;
3135 }
3136 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003137 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138}
3139
3140/*
3141 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3142 */
3143 static int
3144buf_same_ino(buf, stp)
3145 buf_T *buf;
3146 struct stat *stp;
3147{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003148 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 && stp->st_dev == buf->b_dev
3150 && stp->st_ino == buf->b_ino);
3151}
3152#endif
3153
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003154/*
3155 * Print info about the current buffer.
3156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 void
3158fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003159 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 int shorthelp;
3161 int dont_truncate;
3162{
3163 char_u *name;
3164 int n;
3165 char_u *p;
3166 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003167 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
3169 buffer = alloc(IOSIZE);
3170 if (buffer == NULL)
3171 return;
3172
3173 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3174 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003175 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 p = buffer + STRLEN(buffer);
3177 }
3178 else
3179 p = buffer;
3180
3181 *p++ = '"';
3182 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003183 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 else
3185 {
3186 if (!fullname && curbuf->b_fname != NULL)
3187 name = curbuf->b_fname;
3188 else
3189 name = curbuf->b_ffname;
3190 home_replace(shorthelp ? curbuf : NULL, name, p,
3191 (int)(IOSIZE - (p - buffer)), TRUE);
3192 }
3193
Bram Moolenaara800b422010-06-27 01:15:55 +02003194 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 curbufIsChanged() ? (shortmess(SHM_MOD)
3196 ? " [+]" : _(" [Modified]")) : " ",
3197 (curbuf->b_flags & BF_NOTEDITED)
3198#ifdef FEAT_QUICKFIX
3199 && !bt_dontwrite(curbuf)
3200#endif
3201 ? _("[Not edited]") : "",
3202 (curbuf->b_flags & BF_NEW)
3203#ifdef FEAT_QUICKFIX
3204 && !bt_dontwrite(curbuf)
3205#endif
3206 ? _("[New file]") : "",
3207 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003208 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 : _("[readonly]")) : "",
3210 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3211 || curbuf->b_p_ro) ?
3212 " " : "");
3213 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3214 * causes an overflow, thus for large numbers divide instead. */
3215 if (curwin->w_cursor.lnum > 1000000L)
3216 n = (int)(((long)curwin->w_cursor.lnum) /
3217 ((long)curbuf->b_ml.ml_line_count / 100L));
3218 else
3219 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3220 (long)curbuf->b_ml.ml_line_count);
3221 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3222 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003223 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
3225#ifdef FEAT_CMDL_INFO
3226 else if (p_ru)
3227 {
3228 /* Current line and column are already on the screen -- webb */
3229 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003230 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003232 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 (long)curbuf->b_ml.ml_line_count, n);
3234 }
3235#endif
3236 else
3237 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003238 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 _("line %ld of %ld --%d%%-- col "),
3240 (long)curwin->w_cursor.lnum,
3241 (long)curbuf->b_ml.ml_line_count,
3242 n);
3243 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003244 len = STRLEN(buffer);
3245 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3247 }
3248
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003249 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
3251 if (dont_truncate)
3252 {
3253 /* Temporarily set msg_scroll to avoid the message being truncated.
3254 * First call msg_start() to get the message in the right place. */
3255 msg_start();
3256 n = msg_scroll;
3257 msg_scroll = TRUE;
3258 msg(buffer);
3259 msg_scroll = n;
3260 }
3261 else
3262 {
3263 p = msg_trunc_attr(buffer, FALSE, 0);
3264 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 /* Need to repeat the message after redrawing when:
3266 * - When restart_edit is set (otherwise there will be a delay
3267 * before redrawing).
3268 * - When the screen was scrolled but there is no wait-return
3269 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003270 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
3272
3273 vim_free(buffer);
3274}
3275
3276 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003277col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003279 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 int col;
3281 int vcol;
3282{
3283 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003284 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003286 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287}
3288
3289#if defined(FEAT_TITLE) || defined(PROTO)
3290/*
3291 * put file name in title bar of window and in icon title
3292 */
3293
3294static char_u *lasttitle = NULL;
3295static char_u *lasticon = NULL;
3296
3297 void
3298maketitle()
3299{
3300 char_u *p;
3301 char_u *t_str = NULL;
3302 char_u *i_name;
3303 char_u *i_str = NULL;
3304 int maxlen = 0;
3305 int len;
3306 int mustset;
3307 char_u buf[IOSIZE];
3308 int off;
3309
3310 if (!redrawing())
3311 {
3312 /* Postpone updating the title when 'lazyredraw' is set. */
3313 need_maketitle = TRUE;
3314 return;
3315 }
3316
3317 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003318 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 return;
3320
3321 if (p_title)
3322 {
3323 if (p_titlelen > 0)
3324 {
3325 maxlen = p_titlelen * Columns / 100;
3326 if (maxlen < 10)
3327 maxlen = 10;
3328 }
3329
3330 t_str = buf;
3331 if (*p_titlestring != NUL)
3332 {
3333#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003334 if (stl_syntax & STL_IN_TITLE)
3335 {
3336 int use_sandbox = FALSE;
3337 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003338
3339# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003340 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003341# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003342 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003344 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003345 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003346 if (called_emsg)
3347 set_string_option_direct((char_u *)"titlestring", -1,
3348 (char_u *)"", OPT_FREE, SID_ERROR);
3349 called_emsg |= save_called_emsg;
3350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 else
3352#endif
3353 t_str = p_titlestring;
3354 }
3355 else
3356 {
3357 /* format: "fname + (path) (1 of 2) - VIM" */
3358
Bram Moolenaar2c666692012-09-05 13:30:40 +02003359#define SPACE_FOR_FNAME (IOSIZE - 100)
3360#define SPACE_FOR_DIR (IOSIZE - 20)
3361#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003363 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 else
3365 {
3366 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003367 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
3370
3371 switch (bufIsChanged(curbuf)
3372 + (curbuf->b_p_ro * 2)
3373 + (!curbuf->b_p_ma * 4))
3374 {
3375 case 1: STRCAT(buf, " +"); break;
3376 case 2: STRCAT(buf, " ="); break;
3377 case 3: STRCAT(buf, " =+"); break;
3378 case 4:
3379 case 6: STRCAT(buf, " -"); break;
3380 case 5:
3381 case 7: STRCAT(buf, " -+"); break;
3382 }
3383
3384 if (curbuf->b_fname != NULL)
3385 {
3386 /* Get path of file, replace home dir with ~ */
3387 off = (int)STRLEN(buf);
3388 buf[off++] = ' ';
3389 buf[off++] = '(';
3390 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003391 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392#ifdef BACKSLASH_IN_FILENAME
3393 /* avoid "c:/name" to be reduced to "c" */
3394 if (isalpha(buf[off]) && buf[off + 1] == ':')
3395 off += 2;
3396#endif
3397 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003398 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003401 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003402 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003405
Bram Moolenaar2c666692012-09-05 13:30:40 +02003406 /* Translate unprintable chars and concatenate. Keep some
3407 * room for the server name. When there is no room (very long
3408 * file name) use (...). */
3409 if (off < SPACE_FOR_DIR)
3410 {
3411 p = transstr(buf + off);
3412 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3413 vim_free(p);
3414 }
3415 else
3416 {
3417 vim_strncpy(buf + off, (char_u *)"...",
3418 (size_t)(SPACE_FOR_ARGNR - off));
3419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 STRCAT(buf, ")");
3421 }
3422
Bram Moolenaar2c666692012-09-05 13:30:40 +02003423 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425#if defined(FEAT_CLIENTSERVER)
3426 if (serverName != NULL)
3427 {
3428 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003429 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
3431 else
3432#endif
3433 STRCAT(buf, " - VIM");
3434
3435 if (maxlen > 0)
3436 {
3437 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003438 if (vim_strsize(buf) > maxlen)
3439 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441 }
3442 }
3443 mustset = ti_change(t_str, &lasttitle);
3444
3445 if (p_icon)
3446 {
3447 i_str = buf;
3448 if (*p_iconstring != NUL)
3449 {
3450#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003451 if (stl_syntax & STL_IN_ICON)
3452 {
3453 int use_sandbox = FALSE;
3454 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003455
3456# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003457 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003458# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003459 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003461 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003462 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003463 if (called_emsg)
3464 set_string_option_direct((char_u *)"iconstring", -1,
3465 (char_u *)"", OPT_FREE, SID_ERROR);
3466 called_emsg |= save_called_emsg;
3467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 else
3469#endif
3470 i_str = p_iconstring;
3471 }
3472 else
3473 {
3474 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003475 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 else /* use file name only in icon */
3477 i_name = gettail(curbuf->b_ffname);
3478 *i_str = NUL;
3479 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003480 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (len > 100)
3482 {
3483 len -= 100;
3484#ifdef FEAT_MBYTE
3485 if (has_mbyte)
3486 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3487#endif
3488 i_name += len;
3489 }
3490 STRCPY(i_str, i_name);
3491 trans_characters(i_str, IOSIZE);
3492 }
3493 }
3494
3495 mustset |= ti_change(i_str, &lasticon);
3496
3497 if (mustset)
3498 resettitle();
3499}
3500
3501/*
3502 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3503 * from "str" if it does.
3504 * Return TRUE when "*last" changed.
3505 */
3506 static int
3507ti_change(str, last)
3508 char_u *str;
3509 char_u **last;
3510{
3511 if ((str == NULL) != (*last == NULL)
3512 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3513 {
3514 vim_free(*last);
3515 if (str == NULL)
3516 *last = NULL;
3517 else
3518 *last = vim_strsave(str);
3519 return TRUE;
3520 }
3521 return FALSE;
3522}
3523
3524/*
3525 * Put current window title back (used after calling a shell)
3526 */
3527 void
3528resettitle()
3529{
3530 mch_settitle(lasttitle, lasticon);
3531}
Bram Moolenaarea408852005-06-25 22:49:46 +00003532
3533# if defined(EXITFREE) || defined(PROTO)
3534 void
3535free_titles()
3536{
3537 vim_free(lasttitle);
3538 vim_free(lasticon);
3539}
3540# endif
3541
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#endif /* FEAT_TITLE */
3543
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003544#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003546 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 * Return length of string in screen cells.
3548 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003549 * Normally works for window "wp", except when working for 'tabline' then it
3550 * is "curwin".
3551 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 * Items are drawn interspersed with the text that surrounds it
3553 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3554 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3555 *
3556 * If maxwidth is not zero, the string will be filled at any middle marker
3557 * or truncated if too long, fillchar is used for all whitespace.
3558 */
3559 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003560build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3561 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003563 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 size_t outlen; /* length of out[] */
3565 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003566 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 int fillchar;
3568 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003569 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3570 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
3572 char_u *p;
3573 char_u *s;
3574 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003575 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576#ifdef FEAT_EVAL
3577 win_T *o_curwin;
3578 buf_T *o_curbuf;
3579#endif
3580 int empty_line;
3581 colnr_T virtcol;
3582 long l;
3583 long n;
3584 int prevchar_isflag;
3585 int prevchar_isitem;
3586 int itemisflag;
3587 int fillable;
3588 char_u *str;
3589 long num;
3590 int width;
3591 int itemcnt;
3592 int curitem;
3593 int groupitem[STL_MAX_ITEM];
3594 int groupdepth;
3595 struct stl_item
3596 {
3597 char_u *start;
3598 int minwid;
3599 int maxwid;
3600 enum
3601 {
3602 Normal,
3603 Empty,
3604 Group,
3605 Middle,
3606 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003607 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 Trunc
3609 } type;
3610 } item[STL_MAX_ITEM];
3611 int minwid;
3612 int maxwid;
3613 int zeropad;
3614 char_u base;
3615 char_u opt;
3616#define TMPLEN 70
3617 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003618 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003619 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003620
3621#ifdef FEAT_EVAL
3622 /*
3623 * When the format starts with "%!" then evaluate it as an expression and
3624 * use the result as the actual format string.
3625 */
3626 if (fmt[0] == '%' && fmt[1] == '!')
3627 {
3628 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3629 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003630 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003631 }
3632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 if (fillchar == 0)
3635 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003636#ifdef FEAT_MBYTE
3637 /* Can't handle a multi-byte fill character yet. */
3638 else if (mb_char2len(fillchar) > 1)
3639 fillchar = '-';
3640#endif
3641
Bram Moolenaar567199b2013-04-24 16:52:36 +02003642 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3643 * p will become invalid when getting another buffer line. */
3644 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3645 empty_line = (*p == NUL);
3646
3647 /* Get the byte value now, in case we need it below. This is more
3648 * efficient than making a copy of the line. */
3649 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3650 byteval = 0;
3651 else
3652#ifdef FEAT_MBYTE
3653 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3654#else
3655 byteval = p[wp->w_cursor.col];
3656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
3658 groupdepth = 0;
3659 p = out;
3660 curitem = 0;
3661 prevchar_isflag = TRUE;
3662 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003663 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003665 if (curitem == STL_MAX_ITEM)
3666 {
3667 /* There are too many items. Add the error code to the statusline
3668 * to give the user a hint about what went wrong. */
3669 if (p + 6 < out + outlen)
3670 {
3671 mch_memmove(p, " E541", (size_t)5);
3672 p += 5;
3673 }
3674 break;
3675 }
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (*s != NUL && *s != '%')
3678 prevchar_isflag = prevchar_isitem = FALSE;
3679
3680 /*
3681 * Handle up to the next '%' or the end.
3682 */
3683 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3684 *p++ = *s++;
3685 if (*s == NUL || p + 1 >= out + outlen)
3686 break;
3687
3688 /*
3689 * Handle one '%' item.
3690 */
3691 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003692 if (*s == NUL) /* ignore trailing % */
3693 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 if (*s == '%')
3695 {
3696 if (p + 1 >= out + outlen)
3697 break;
3698 *p++ = *s++;
3699 prevchar_isflag = prevchar_isitem = FALSE;
3700 continue;
3701 }
3702 if (*s == STL_MIDDLEMARK)
3703 {
3704 s++;
3705 if (groupdepth > 0)
3706 continue;
3707 item[curitem].type = Middle;
3708 item[curitem++].start = p;
3709 continue;
3710 }
3711 if (*s == STL_TRUNCMARK)
3712 {
3713 s++;
3714 item[curitem].type = Trunc;
3715 item[curitem++].start = p;
3716 continue;
3717 }
3718 if (*s == ')')
3719 {
3720 s++;
3721 if (groupdepth < 1)
3722 continue;
3723 groupdepth--;
3724
3725 t = item[groupitem[groupdepth]].start;
3726 *p = NUL;
3727 l = vim_strsize(t);
3728 if (curitem > groupitem[groupdepth] + 1
3729 && item[groupitem[groupdepth]].minwid == 0)
3730 {
3731 /* remove group if all items are empty */
3732 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3733 if (item[n].type == Normal)
3734 break;
3735 if (n == curitem)
3736 {
3737 p = t;
3738 l = 0;
3739 }
3740 }
3741 if (l > item[groupitem[groupdepth]].maxwid)
3742 {
3743 /* truncate, remove n bytes of text at the start */
3744#ifdef FEAT_MBYTE
3745 if (has_mbyte)
3746 {
3747 /* Find the first character that should be included. */
3748 n = 0;
3749 while (l >= item[groupitem[groupdepth]].maxwid)
3750 {
3751 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003752 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 }
3755 else
3756#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003757 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
3759 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003760 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 p = p - n + 1;
3762#ifdef FEAT_MBYTE
3763 /* Fill up space left over by half a double-wide char. */
3764 while (++l < item[groupitem[groupdepth]].minwid)
3765 *p++ = fillchar;
3766#endif
3767
3768 /* correct the start of the items for the truncation */
3769 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3770 {
3771 item[l].start -= n;
3772 if (item[l].start < t)
3773 item[l].start = t;
3774 }
3775 }
3776 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3777 {
3778 /* fill */
3779 n = item[groupitem[groupdepth]].minwid;
3780 if (n < 0)
3781 {
3782 /* fill by appending characters */
3783 n = 0 - n;
3784 while (l++ < n && p + 1 < out + outlen)
3785 *p++ = fillchar;
3786 }
3787 else
3788 {
3789 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003790 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 l = n - l;
3792 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003793 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 p += l;
3795 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3796 item[n].start += l;
3797 for ( ; l > 0; l--)
3798 *t++ = fillchar;
3799 }
3800 }
3801 continue;
3802 }
3803 minwid = 0;
3804 maxwid = 9999;
3805 zeropad = FALSE;
3806 l = 1;
3807 if (*s == '0')
3808 {
3809 s++;
3810 zeropad = TRUE;
3811 }
3812 if (*s == '-')
3813 {
3814 s++;
3815 l = -1;
3816 }
3817 if (VIM_ISDIGIT(*s))
3818 {
3819 minwid = (int)getdigits(&s);
3820 if (minwid < 0) /* overflow */
3821 minwid = 0;
3822 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003823 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
3825 item[curitem].type = Highlight;
3826 item[curitem].start = p;
3827 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3828 s++;
3829 curitem++;
3830 continue;
3831 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003832 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3833 {
3834 if (*s == STL_TABCLOSENR)
3835 {
3836 if (minwid == 0)
3837 {
3838 /* %X ends the close label, go back to the previously
3839 * define tab label nr. */
3840 for (n = curitem - 1; n >= 0; --n)
3841 if (item[n].type == TabPage && item[n].minwid >= 0)
3842 {
3843 minwid = item[n].minwid;
3844 break;
3845 }
3846 }
3847 else
3848 /* close nrs are stored as negative values */
3849 minwid = - minwid;
3850 }
3851 item[curitem].type = TabPage;
3852 item[curitem].start = p;
3853 item[curitem].minwid = minwid;
3854 s++;
3855 curitem++;
3856 continue;
3857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (*s == '.')
3859 {
3860 s++;
3861 if (VIM_ISDIGIT(*s))
3862 {
3863 maxwid = (int)getdigits(&s);
3864 if (maxwid <= 0) /* overflow */
3865 maxwid = 50;
3866 }
3867 }
3868 minwid = (minwid > 50 ? 50 : minwid) * l;
3869 if (*s == '(')
3870 {
3871 groupitem[groupdepth++] = curitem;
3872 item[curitem].type = Group;
3873 item[curitem].start = p;
3874 item[curitem].minwid = minwid;
3875 item[curitem].maxwid = maxwid;
3876 s++;
3877 curitem++;
3878 continue;
3879 }
3880 if (vim_strchr(STL_ALL, *s) == NULL)
3881 {
3882 s++;
3883 continue;
3884 }
3885 opt = *s++;
3886
3887 /* OK - now for the real work */
3888 base = 'D';
3889 itemisflag = FALSE;
3890 fillable = TRUE;
3891 num = -1;
3892 str = NULL;
3893 switch (opt)
3894 {
3895 case STL_FILEPATH:
3896 case STL_FULLPATH:
3897 case STL_FILENAME:
3898 fillable = FALSE; /* don't change ' ' to fillchar */
3899 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003900 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 else
3902 {
3903 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003904 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3906 }
3907 trans_characters(NameBuff, MAXPATHL);
3908 if (opt != STL_FILENAME)
3909 str = NameBuff;
3910 else
3911 str = gettail(NameBuff);
3912 break;
3913
3914 case STL_VIM_EXPR: /* '{' */
3915 itemisflag = TRUE;
3916 t = p;
3917 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3918 *p++ = *s++;
3919 if (*s != '}') /* missing '}' or out of space */
3920 break;
3921 s++;
3922 *p = 0;
3923 p = t;
3924
3925#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003926 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3928
3929 o_curbuf = curbuf;
3930 o_curwin = curwin;
3931 curwin = wp;
3932 curbuf = wp->w_buffer;
3933
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003934 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
3936 curwin = o_curwin;
3937 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003938 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
3940 if (str != NULL && *str != 0)
3941 {
3942 if (*skipdigits(str) == NUL)
3943 {
3944 num = atoi((char *)str);
3945 vim_free(str);
3946 str = NULL;
3947 itemisflag = FALSE;
3948 }
3949 }
3950#endif
3951 break;
3952
3953 case STL_LINE:
3954 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3955 ? 0L : (long)(wp->w_cursor.lnum);
3956 break;
3957
3958 case STL_NUMLINES:
3959 num = wp->w_buffer->b_ml.ml_line_count;
3960 break;
3961
3962 case STL_COLUMN:
3963 num = !(State & INSERT) && empty_line
3964 ? 0 : (int)wp->w_cursor.col + 1;
3965 break;
3966
3967 case STL_VIRTCOL:
3968 case STL_VIRTCOL_ALT:
3969 /* In list mode virtcol needs to be recomputed */
3970 virtcol = wp->w_virtcol;
3971 if (wp->w_p_list && lcs_tab1 == NUL)
3972 {
3973 wp->w_p_list = FALSE;
3974 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3975 wp->w_p_list = TRUE;
3976 }
3977 ++virtcol;
3978 /* Don't display %V if it's the same as %c. */
3979 if (opt == STL_VIRTCOL_ALT
3980 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3981 ? 0 : (int)wp->w_cursor.col + 1)))
3982 break;
3983 num = (long)virtcol;
3984 break;
3985
3986 case STL_PERCENTAGE:
3987 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3988 (long)wp->w_buffer->b_ml.ml_line_count);
3989 break;
3990
3991 case STL_ALTPERCENT:
3992 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003993 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 break;
3995
3996 case STL_ARGLISTSTAT:
3997 fillable = FALSE;
3998 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003999 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 str = tmp;
4001 break;
4002
4003 case STL_KEYMAP:
4004 fillable = FALSE;
4005 if (get_keymap_str(wp, tmp, TMPLEN))
4006 str = tmp;
4007 break;
4008 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004009#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004010 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#else
4012 num = 0;
4013#endif
4014 break;
4015
4016 case STL_BUFNO:
4017 num = wp->w_buffer->b_fnum;
4018 break;
4019
4020 case STL_OFFSET_X:
4021 base = 'X';
4022 case STL_OFFSET:
4023#ifdef FEAT_BYTEOFF
4024 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4025 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4026 0L : l + 1 + (!(State & INSERT) && empty_line ?
4027 0 : (int)wp->w_cursor.col);
4028#endif
4029 break;
4030
4031 case STL_BYTEVAL_X:
4032 base = 'X';
4033 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004034 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (num == NL)
4036 num = 0;
4037 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4038 num = NL;
4039 break;
4040
4041 case STL_ROFLAG:
4042 case STL_ROFLAG_ALT:
4043 itemisflag = TRUE;
4044 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004045 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 break;
4047
4048 case STL_HELPFLAG:
4049 case STL_HELPFLAG_ALT:
4050 itemisflag = TRUE;
4051 if (wp->w_buffer->b_help)
4052 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004053 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 break;
4055
4056#ifdef FEAT_AUTOCMD
4057 case STL_FILETYPE:
4058 if (*wp->w_buffer->b_p_ft != NUL
4059 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4060 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004061 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4062 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 str = tmp;
4064 }
4065 break;
4066
4067 case STL_FILETYPE_ALT:
4068 itemisflag = TRUE;
4069 if (*wp->w_buffer->b_p_ft != NUL
4070 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4071 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004072 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4073 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 for (t = tmp; *t != 0; t++)
4075 *t = TOUPPER_LOC(*t);
4076 str = tmp;
4077 }
4078 break;
4079#endif
4080
4081#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4082 case STL_PREVIEWFLAG:
4083 case STL_PREVIEWFLAG_ALT:
4084 itemisflag = TRUE;
4085 if (wp->w_p_pvw)
4086 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4087 : _("[Preview]"));
4088 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004089
4090 case STL_QUICKFIX:
4091 if (bt_quickfix(wp->w_buffer))
4092 str = (char_u *)(wp->w_llist_ref
4093 ? _(msg_loclist)
4094 : _(msg_qflist));
4095 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096#endif
4097
4098 case STL_MODIFIED:
4099 case STL_MODIFIED_ALT:
4100 itemisflag = TRUE;
4101 switch ((opt == STL_MODIFIED_ALT)
4102 + bufIsChanged(wp->w_buffer) * 2
4103 + (!wp->w_buffer->b_p_ma) * 4)
4104 {
4105 case 2: str = (char_u *)"[+]"; break;
4106 case 3: str = (char_u *)",+"; break;
4107 case 4: str = (char_u *)"[-]"; break;
4108 case 5: str = (char_u *)",-"; break;
4109 case 6: str = (char_u *)"[+-]"; break;
4110 case 7: str = (char_u *)",+-"; break;
4111 }
4112 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004113
4114 case STL_HIGHLIGHT:
4115 t = s;
4116 while (*s != '#' && *s != NUL)
4117 ++s;
4118 if (*s == '#')
4119 {
4120 item[curitem].type = Highlight;
4121 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004122 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004123 curitem++;
4124 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004125 if (*s != NUL)
4126 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004127 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129
4130 item[curitem].start = p;
4131 item[curitem].type = Normal;
4132 if (str != NULL && *str)
4133 {
4134 t = str;
4135 if (itemisflag)
4136 {
4137 if ((t[0] && t[1])
4138 && ((!prevchar_isitem && *t == ',')
4139 || (prevchar_isflag && *t == ' ')))
4140 t++;
4141 prevchar_isflag = TRUE;
4142 }
4143 l = vim_strsize(t);
4144 if (l > 0)
4145 prevchar_isitem = TRUE;
4146 if (l > maxwid)
4147 {
4148 while (l >= maxwid)
4149#ifdef FEAT_MBYTE
4150 if (has_mbyte)
4151 {
4152 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004153 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
4155 else
4156#endif
4157 l -= byte2cells(*t++);
4158 if (p + 1 >= out + outlen)
4159 break;
4160 *p++ = '<';
4161 }
4162 if (minwid > 0)
4163 {
4164 for (; l < minwid && p + 1 < out + outlen; l++)
4165 {
4166 /* Don't put a "-" in front of a digit. */
4167 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4168 *p++ = ' ';
4169 else
4170 *p++ = fillchar;
4171 }
4172 minwid = 0;
4173 }
4174 else
4175 minwid *= -1;
4176 while (*t && p + 1 < out + outlen)
4177 {
4178 *p++ = *t++;
4179 /* Change a space by fillchar, unless fillchar is '-' and a
4180 * digit follows. */
4181 if (fillable && p[-1] == ' '
4182 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4183 p[-1] = fillchar;
4184 }
4185 for (; l < minwid && p + 1 < out + outlen; l++)
4186 *p++ = fillchar;
4187 }
4188 else if (num >= 0)
4189 {
4190 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4191 char_u nstr[20];
4192
4193 if (p + 20 >= out + outlen)
4194 break; /* not sufficient space */
4195 prevchar_isitem = TRUE;
4196 t = nstr;
4197 if (opt == STL_VIRTCOL_ALT)
4198 {
4199 *t++ = '-';
4200 minwid--;
4201 }
4202 *t++ = '%';
4203 if (zeropad)
4204 *t++ = '0';
4205 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004206 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 *t = 0;
4208
4209 for (n = num, l = 1; n >= nbase; n /= nbase)
4210 l++;
4211 if (opt == STL_VIRTCOL_ALT)
4212 l++;
4213 if (l > maxwid)
4214 {
4215 l += 2;
4216 n = l - maxwid;
4217 while (l-- > maxwid)
4218 num /= nbase;
4219 *t++ = '>';
4220 *t++ = '%';
4221 *t = t[-3];
4222 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004223 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4224 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004227 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4228 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 p += STRLEN(p);
4230 }
4231 else
4232 item[curitem].type = Empty;
4233
4234 if (opt == STL_VIM_EXPR)
4235 vim_free(str);
4236
4237 if (num >= 0 || (!itemisflag && str && *str))
4238 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4239 curitem++;
4240 }
4241 *p = NUL;
4242 itemcnt = curitem;
4243
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004244#ifdef FEAT_EVAL
4245 if (usefmt != fmt)
4246 vim_free(usefmt);
4247#endif
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 width = vim_strsize(out);
4250 if (maxwidth > 0 && width > maxwidth)
4251 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004252 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 l = 0;
4254 if (itemcnt == 0)
4255 s = out;
4256 else
4257 {
4258 for ( ; l < itemcnt; l++)
4259 if (item[l].type == Trunc)
4260 {
4261 /* Truncate at %< item. */
4262 s = item[l].start;
4263 break;
4264 }
4265 if (l == itemcnt)
4266 {
4267 /* No %< item, truncate first item. */
4268 s = item[0].start;
4269 l = 0;
4270 }
4271 }
4272
4273 if (width - vim_strsize(s) >= maxwidth)
4274 {
4275 /* Truncation mark is beyond max length */
4276#ifdef FEAT_MBYTE
4277 if (has_mbyte)
4278 {
4279 s = out;
4280 width = 0;
4281 for (;;)
4282 {
4283 width += ptr2cells(s);
4284 if (width >= maxwidth)
4285 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004286 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 /* Fill up for half a double-wide character. */
4289 while (++width < maxwidth)
4290 *s++ = fillchar;
4291 }
4292 else
4293#endif
4294 s = out + maxwidth - 1;
4295 for (l = 0; l < itemcnt; l++)
4296 if (item[l].start > s)
4297 break;
4298 itemcnt = l;
4299 *s++ = '>';
4300 *s = 0;
4301 }
4302 else
4303 {
4304#ifdef FEAT_MBYTE
4305 if (has_mbyte)
4306 {
4307 n = 0;
4308 while (width >= maxwidth)
4309 {
4310 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004311 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 }
4314 else
4315#endif
4316 n = width - maxwidth + 1;
4317 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004318 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 *s = '<';
4320
4321 /* Fill up for half a double-wide character. */
4322 while (++width < maxwidth)
4323 {
4324 s = s + STRLEN(s);
4325 *s++ = fillchar;
4326 *s = NUL;
4327 }
4328
4329 --n; /* count the '<' */
4330 for (; l < itemcnt; l++)
4331 {
4332 if (item[l].start - n >= s)
4333 item[l].start -= n;
4334 else
4335 item[l].start = s;
4336 }
4337 }
4338 width = maxwidth;
4339 }
4340 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4341 {
4342 /* Apply STL_MIDDLE if any */
4343 for (l = 0; l < itemcnt; l++)
4344 if (item[l].type == Middle)
4345 break;
4346 if (l < itemcnt)
4347 {
4348 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004349 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 for (s = item[l].start; s < p; s++)
4351 *s = fillchar;
4352 for (l++; l < itemcnt; l++)
4353 item[l].start += maxwidth - width;
4354 width = maxwidth;
4355 }
4356 }
4357
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004358 /* Store the info about highlighting. */
4359 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004361 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 for (l = 0; l < itemcnt; l++)
4363 {
4364 if (item[l].type == Highlight)
4365 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004366 sp->start = item[l].start;
4367 sp->userhl = item[l].minwid;
4368 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004371 sp->start = NULL;
4372 sp->userhl = 0;
4373 }
4374
4375 /* Store the info about tab pages labels. */
4376 if (tabtab != NULL)
4377 {
4378 sp = tabtab;
4379 for (l = 0; l < itemcnt; l++)
4380 {
4381 if (item[l].type == TabPage)
4382 {
4383 sp->start = item[l].start;
4384 sp->userhl = item[l].minwid;
4385 sp++;
4386 }
4387 }
4388 sp->start = NULL;
4389 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
4391
4392 return width;
4393}
4394#endif /* FEAT_STL_OPT */
4395
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004396#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4397 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004399 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4400 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 */
4402 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004403get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004405 char_u *buf;
4406 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
4408 long above; /* number of lines above window */
4409 long below; /* number of lines below window */
4410
4411 above = wp->w_topline - 1;
4412#ifdef FEAT_DIFF
4413 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4414#endif
4415 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4416 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004417 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4418 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004420 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004422 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 ? (int)(above / ((above + below) / 100L))
4424 : (int)(above * 100L / (above + below)));
4425}
4426#endif
4427
4428/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004429 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 * Return TRUE if it was appended.
4431 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004432 static int
4433append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 win_T *wp;
4435 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004436 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438{
4439 char_u *p;
4440
4441 if (ARGCOUNT <= 1) /* nothing to do */
4442 return FALSE;
4443
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004444 p = buf + STRLEN(buf); /* go to the end of the buffer */
4445 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 return FALSE;
4447 *p++ = ' ';
4448 *p++ = '(';
4449 if (add_file)
4450 {
4451 STRCPY(p, "file ");
4452 p += 5;
4453 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004454 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4455 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4457 return TRUE;
4458}
4459
4460/*
4461 * If fname is not a full path, make it a full path.
4462 * Returns pointer to allocated memory (NULL for failure).
4463 */
4464 char_u *
4465fix_fname(fname)
4466 char_u *fname;
4467{
4468 /*
4469 * Force expanding the path always for Unix, because symbolic links may
4470 * mess up the full path name, even though it starts with a '/'.
4471 * Also expand when there is ".." in the file name, try to remove it,
4472 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004473 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 * For MS-Windows also expand names like "longna~1" to "longname".
4475 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004476#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 return FullName_save(fname, TRUE);
4478#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004479 if (!vim_isAbsName(fname)
4480 || strstr((char *)fname, "..") != NULL
4481 || strstr((char *)fname, "//") != NULL
4482# ifdef BACKSLASH_IN_FILENAME
4483 || strstr((char *)fname, "\\\\") != NULL
4484# endif
4485# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 )
4489 return FullName_save(fname, FALSE);
4490
4491 fname = vim_strsave(fname);
4492
Bram Moolenaar9b942202007-10-03 12:31:33 +00004493# ifdef USE_FNAME_CASE
4494# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
4498 if (fname != NULL)
4499 fname_case(fname, 0); /* set correct case for file name */
4500 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 return fname;
4504#endif
4505}
4506
4507/*
4508 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4509 * "ffname" becomes a pointer to allocated memory (or NULL).
4510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 void
4512fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004513 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 char_u **ffname;
4515 char_u **sfname;
4516{
4517 if (*ffname == NULL) /* if no file name given, nothing to do */
4518 return;
4519 if (*sfname == NULL) /* if no short file name given, use ffname */
4520 *sfname = *ffname;
4521 *ffname = fix_fname(*ffname); /* expand to full path */
4522
4523#ifdef FEAT_SHORTCUT
4524 if (!buf->b_p_bin)
4525 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004526 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
4528 /* If the file name is a shortcut file, use the file it links to. */
4529 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004530 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
4532 vim_free(*ffname);
4533 *ffname = rfname;
4534 *sfname = rfname;
4535 }
4536 }
4537#endif
4538}
4539
4540/*
4541 * Get the file name for an argument list entry.
4542 */
4543 char_u *
4544alist_name(aep)
4545 aentry_T *aep;
4546{
4547 buf_T *bp;
4548
4549 /* Use the name from the associated buffer if it exists. */
4550 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004551 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 return aep->ae_fname;
4553 return bp->b_fname;
4554}
4555
4556#if defined(FEAT_WINDOWS) || defined(PROTO)
4557/*
4558 * do_arg_all(): Open up to 'count' windows, one for each argument.
4559 */
4560 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004561do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 int count;
4563 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004564 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565{
4566 int i;
4567 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004568 char_u *opened; /* Array of weight for which args are open:
4569 * 0: not opened
4570 * 1: opened in other tab
4571 * 2: opened in curtab
4572 * 3: opened in curtab and curwin
4573 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004574 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 int use_firstwin = FALSE; /* use first window for arglist */
4576 int split_ret = OK;
4577 int p_ea_save;
4578 alist_T *alist; /* argument list to be used */
4579 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004580 tabpage_T *tpnext;
4581 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004582 win_T *old_curwin, *last_curwin;
4583 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004584 win_T *new_curwin = NULL;
4585 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 if (ARGCOUNT <= 0)
4588 {
4589 /* Don't give an error message. We don't want it when the ":all"
4590 * command is in the .vimrc. */
4591 return;
4592 }
4593 setpcmark();
4594
4595 opened_len = ARGCOUNT;
4596 opened = alloc_clear((unsigned)opened_len);
4597 if (opened == NULL)
4598 return;
4599
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004600 /* Autocommands may do anything to the argument list. Make sure it's not
4601 * freed while we are working here by "locking" it. We still have to
4602 * watch out for its size to be changed. */
4603 alist = curwin->w_alist;
4604 ++alist->al_refcount;
4605
4606 old_curwin = curwin;
4607 old_curtab = curtab;
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609#ifdef FEAT_GUI
4610 need_mouse_correct = TRUE;
4611#endif
4612
4613 /*
4614 * Try closing all windows that are not in the argument list.
4615 * Also close windows that are not full width;
4616 * When 'hidden' or "forceit" set the buffer becomes hidden.
4617 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004618 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004620 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004621 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004622 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004624 tpnext = curtab->tp_next;
4625 for (wp = firstwin; wp != NULL; wp = wpnext)
4626 {
4627 wpnext = wp->w_next;
4628 buf = wp->w_buffer;
4629 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004630 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004632 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004634 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004635 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004636 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004638 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004639 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004641 if (i < alist->al_ga.ga_len
4642 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4643 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4644 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004646 int weight = 1;
4647
4648 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004649 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004650 ++weight;
4651 if (old_curwin == wp)
4652 ++weight;
4653 }
4654
4655 if (weight > (int)opened[i])
4656 {
4657 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004658 if (i == 0)
4659 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004660 if (new_curwin != NULL)
4661 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004662 new_curwin = wp;
4663 new_curtab = curtab;
4664 }
4665 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004666 else if (keep_tabs)
4667 i = opened_len;
4668
4669 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004670 {
4671 /* Use the current argument list for all windows
4672 * containing a file from it. */
4673 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004674 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004675 ++wp->w_alist->al_refcount;
4676 }
4677 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004681 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004683 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004685 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004686 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004688 /* If the buffer was changed, and we would like to hide it,
4689 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004690 if (!P_HID(buf) && buf->b_nwindows <= 1
4691 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004693 (void)autowrite(buf, FALSE);
4694#ifdef FEAT_AUTOCMD
4695 /* check if autocommands removed the window */
4696 if (!win_valid(wp) || !buf_valid(buf))
4697 {
4698 wpnext = firstwin; /* start all over... */
4699 continue;
4700 }
4701#endif
4702 }
4703#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004704 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004705 if (firstwin == lastwin
4706 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004707#endif
4708 use_firstwin = TRUE;
4709#ifdef FEAT_WINDOWS
4710 else
4711 {
4712 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4713# ifdef FEAT_AUTOCMD
4714 /* check if autocommands removed the next window */
4715 if (!win_valid(wpnext))
4716 wpnext = firstwin; /* start all over... */
4717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 }
4719#endif
4720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004723
4724 /* Without the ":tab" modifier only do the current tab page. */
4725 if (had_tab == 0 || tpnext == NULL)
4726 break;
4727
4728# ifdef FEAT_AUTOCMD
4729 /* check if autocommands removed the next tab page */
4730 if (!valid_tabpage(tpnext))
4731 tpnext = first_tabpage; /* start all over...*/
4732# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004733 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735
4736 /*
4737 * Open a window for files in the argument list that don't have one.
4738 * ARGCOUNT may change while doing this, because of autocommands.
4739 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004740 if (count > opened_len || count <= 0)
4741 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
4743#ifdef FEAT_AUTOCMD
4744 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4745 ++autocmd_no_enter;
4746 ++autocmd_no_leave;
4747#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004748 last_curwin = curwin;
4749 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004751#ifdef FEAT_WINDOWS
4752 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4753 * leaving an empty tab page when executed locally. */
4754 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4755 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4756 use_firstwin = TRUE;
4757#endif
4758
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004759 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
4761 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4762 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004763 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
4765 /* Move the already present window to below the current window */
4766 if (curwin->w_arg_idx != i)
4767 {
4768 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4769 {
4770 if (wpnext->w_arg_idx == i)
4771 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004772 if (keep_tabs)
4773 {
4774 new_curwin = wpnext;
4775 new_curtab = curtab;
4776 }
4777 else
4778 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 break;
4780 }
4781 }
4782 }
4783 }
4784 else if (split_ret == OK)
4785 {
4786 if (!use_firstwin) /* split current window */
4787 {
4788 p_ea_save = p_ea;
4789 p_ea = TRUE; /* use space from all windows */
4790 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4791 p_ea = p_ea_save;
4792 if (split_ret == FAIL)
4793 continue;
4794 }
4795#ifdef FEAT_AUTOCMD
4796 else /* first window: do autocmd for leaving this buffer */
4797 --autocmd_no_leave;
4798#endif
4799
4800 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004801 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 */
4803 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004804 if (i == 0)
4805 {
4806 new_curwin = curwin;
4807 new_curtab = curtab;
4808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4810 ECMD_ONE,
4811 ((P_HID(curwin->w_buffer)
4812 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004813 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#ifdef FEAT_AUTOCMD
4815 if (use_firstwin)
4816 ++autocmd_no_leave;
4817#endif
4818 use_firstwin = FALSE;
4819 }
4820 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004821
4822 /* When ":tab" was used open a new tab for a new window repeatedly. */
4823 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4824 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826
4827 /* Remove the "lock" on the argument list. */
4828 alist_unlink(alist);
4829
4830#ifdef FEAT_AUTOCMD
4831 --autocmd_no_enter;
4832#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004833 /* restore last referenced tabpage's curwin */
4834 if (last_curtab != new_curtab)
4835 {
4836 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004837 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004838 if (win_valid(last_curwin))
4839 win_enter(last_curwin, FALSE);
4840 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004841 /* to window with first arg */
4842 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004843 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004844 if (win_valid(new_curwin))
4845 win_enter(new_curwin, FALSE);
4846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#ifdef FEAT_AUTOCMD
4848 --autocmd_no_leave;
4849#endif
4850 vim_free(opened);
4851}
4852
4853# if defined(FEAT_LISTCMDS) || defined(PROTO)
4854/*
4855 * Open a window for a number of buffers.
4856 */
4857 void
4858ex_buffer_all(eap)
4859 exarg_T *eap;
4860{
4861 buf_T *buf;
4862 win_T *wp, *wpnext;
4863 int split_ret = OK;
4864 int p_ea_save;
4865 int open_wins = 0;
4866 int r;
4867 int count; /* Maximum number of windows to open. */
4868 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004869#ifdef FEAT_WINDOWS
4870 int had_tab = cmdmod.tab;
4871 tabpage_T *tpnext;
4872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 if (eap->addr_count == 0) /* make as many windows as possible */
4875 count = 9999;
4876 else
4877 count = eap->line2; /* make as many windows as specified */
4878 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4879 all = FALSE;
4880 else
4881 all = TRUE;
4882
4883 setpcmark();
4884
4885#ifdef FEAT_GUI
4886 need_mouse_correct = TRUE;
4887#endif
4888
4889 /*
4890 * Close superfluous windows (two windows for the same buffer).
4891 * Also close windows that are not full-width.
4892 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004893#ifdef FEAT_WINDOWS
4894 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004895 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004896 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004899 tpnext = curtab->tp_next;
4900 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004902 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004903 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004904#ifdef FEAT_VERTSPLIT
4905 || ((cmdmod.split & WSP_VERT)
4906 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004907 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004908 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004910#ifdef FEAT_WINDOWS
4911 || (had_tab > 0 && wp != firstwin)
4912#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004913 ) && firstwin != lastwin
4914#ifdef FEAT_AUTOCMD
4915 && !(wp->w_closing || wp->w_buffer->b_closing)
4916#endif
4917 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004918 {
4919 win_close(wp, FALSE);
4920#ifdef FEAT_AUTOCMD
4921 wpnext = firstwin; /* just in case an autocommand does
4922 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004923 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004924 open_wins = 0;
4925#endif
4926 }
4927 else
4928 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004930
4931#ifdef FEAT_WINDOWS
4932 /* Without the ":tab" modifier only do the current tab page. */
4933 if (had_tab == 0 || tpnext == NULL)
4934 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004935 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 /*
4940 * Go through the buffer list. When a buffer doesn't have a window yet,
4941 * open one. Otherwise move the window to the right position.
4942 * Watch out for autocommands that delete buffers or windows!
4943 */
4944#ifdef FEAT_AUTOCMD
4945 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4946 ++autocmd_no_enter;
4947#endif
4948 win_enter(lastwin, FALSE);
4949#ifdef FEAT_AUTOCMD
4950 ++autocmd_no_leave;
4951#endif
4952 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4953 {
4954 /* Check if this buffer needs a window */
4955 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4956 continue;
4957
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004958#ifdef FEAT_WINDOWS
4959 if (had_tab != 0)
4960 {
4961 /* With the ":tab" modifier don't move the window. */
4962 if (buf->b_nwindows > 0)
4963 wp = lastwin; /* buffer has a window, skip it */
4964 else
4965 wp = NULL;
4966 }
4967 else
4968#endif
4969 {
4970 /* Check if this buffer already has a window */
4971 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4972 if (wp->w_buffer == buf)
4973 break;
4974 /* If the buffer already has a window, move it */
4975 if (wp != NULL)
4976 win_move_after(wp, curwin);
4977 }
4978
4979 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
4981 /* Split the window and put the buffer in it */
4982 p_ea_save = p_ea;
4983 p_ea = TRUE; /* use space from all windows */
4984 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4985 ++open_wins;
4986 p_ea = p_ea_save;
4987 if (split_ret == FAIL)
4988 continue;
4989
4990 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004991#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 swap_exists_action = SEA_DIALOG;
4993#endif
4994 set_curbuf(buf, DOBUF_GOTO);
4995#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004998#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 break;
5002 }
5003#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005004#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 if (swap_exists_action == SEA_QUIT)
5006 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005007# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5008 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005009
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005010 /* Reset the error/interrupt/exception state here so that
5011 * aborting() returns FALSE when closing a window. */
5012 enter_cleanup(&cs);
5013# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005014
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005015 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 win_close(curwin, TRUE);
5017 --open_wins;
5018 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005019 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005020
5021# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5022 /* Restore the error/interrupt/exception state if not
5023 * discarded by a new aborting error, interrupt, or uncaught
5024 * exception. */
5025 leave_cleanup(&cs);
5026# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 else
5029 handle_swap_exists(NULL);
5030#endif
5031 }
5032
5033 ui_breakcheck();
5034 if (got_int)
5035 {
5036 (void)vgetc(); /* only break the file loading, not the rest */
5037 break;
5038 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005039#ifdef FEAT_EVAL
5040 /* Autocommands deleted the buffer or aborted script processing!!! */
5041 if (aborting())
5042 break;
5043#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005044#ifdef FEAT_WINDOWS
5045 /* When ":tab" was used open a new tab for a new window repeatedly. */
5046 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5047 cmdmod.tab = 9999;
5048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050#ifdef FEAT_AUTOCMD
5051 --autocmd_no_enter;
5052#endif
5053 win_enter(firstwin, FALSE); /* back to first window */
5054#ifdef FEAT_AUTOCMD
5055 --autocmd_no_leave;
5056#endif
5057
5058 /*
5059 * Close superfluous windows.
5060 */
5061 for (wp = lastwin; open_wins > count; )
5062 {
5063 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5064 || autowrite(wp->w_buffer, FALSE) == OK);
5065#ifdef FEAT_AUTOCMD
5066 if (!win_valid(wp))
5067 {
5068 /* BufWrite Autocommands made the window invalid, start over */
5069 wp = lastwin;
5070 }
5071 else
5072#endif
5073 if (r)
5074 {
5075 win_close(wp, !P_HID(wp->w_buffer));
5076 --open_wins;
5077 wp = lastwin;
5078 }
5079 else
5080 {
5081 wp = wp->w_prev;
5082 if (wp == NULL)
5083 break;
5084 }
5085 }
5086}
5087# endif /* FEAT_LISTCMDS */
5088
5089#endif /* FEAT_WINDOWS */
5090
Bram Moolenaara3227e22006-03-08 21:32:40 +00005091static int chk_modeline __ARGS((linenr_T, int));
5092
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093/*
5094 * do_modelines() - process mode lines for the current file
5095 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005096 * "flags" can be:
5097 * OPT_WINONLY only set options local to window
5098 * OPT_NOWIN don't set options local to window
5099 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 * Returns immediately if the "ml" option isn't set.
5101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00005103do_modelines(flags)
5104 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005106 linenr_T lnum;
5107 int nmlines;
5108 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
5110 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5111 return;
5112
5113 /* Disallow recursive entry here. Can happen when executing a modeline
5114 * triggers an autocommand, which reloads modelines with a ":do". */
5115 if (entered)
5116 return;
5117
5118 ++entered;
5119 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5120 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005121 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 nmlines = 0;
5123
5124 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5125 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005126 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 nmlines = 0;
5128 --entered;
5129}
5130
5131#include "version.h" /* for version number */
5132
5133/*
5134 * chk_modeline() - check a single line for a mode string
5135 * Return FAIL if an error encountered.
5136 */
5137 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005138chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005140 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 char_u *s;
5143 char_u *e;
5144 char_u *linecopy; /* local copy of any modeline found */
5145 int prev;
5146 int vers;
5147 int end;
5148 int retval = OK;
5149 char_u *save_sourcing_name;
5150 linenr_T save_sourcing_lnum;
5151#ifdef FEAT_EVAL
5152 scid_T save_SID;
5153#endif
5154
5155 prev = -1;
5156 for (s = ml_get(lnum); *s != NUL; ++s)
5157 {
5158 if (prev == -1 || vim_isspace(prev))
5159 {
5160 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5161 || STRNCMP(s, "vi:", (size_t)3) == 0)
5162 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005163 /* Accept both "vim" and "Vim". */
5164 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5167 e = s + 4;
5168 else
5169 e = s + 3;
5170 vers = getdigits(&e);
5171 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005172 && (s[0] != 'V'
5173 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 && (s[3] == ':'
5175 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5176 || (VIM_VERSION_100 < vers && s[3] == '<')
5177 || (VIM_VERSION_100 > vers && s[3] == '>')
5178 || (VIM_VERSION_100 == vers && s[3] == '=')))
5179 break;
5180 }
5181 }
5182 prev = *s;
5183 }
5184
5185 if (*s)
5186 {
5187 do /* skip over "ex:", "vi:" or "vim:" */
5188 ++s;
5189 while (s[-1] != ':');
5190
5191 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5192 if (linecopy == NULL)
5193 return FAIL;
5194
5195 save_sourcing_lnum = sourcing_lnum;
5196 save_sourcing_name = sourcing_name;
5197 sourcing_lnum = lnum; /* prepare for emsg() */
5198 sourcing_name = (char_u *)"modelines";
5199
5200 end = FALSE;
5201 while (end == FALSE)
5202 {
5203 s = skipwhite(s);
5204 if (*s == NUL)
5205 break;
5206
5207 /*
5208 * Find end of set command: ':' or end of line.
5209 * Skip over "\:", replacing it with ":".
5210 */
5211 for (e = s; *e != ':' && *e != NUL; ++e)
5212 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005213 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (*e == NUL)
5215 end = TRUE;
5216
5217 /*
5218 * If there is a "set" command, require a terminating ':' and
5219 * ignore the stuff after the ':'.
5220 * "vi:set opt opt opt: foo" -- foo not interpreted
5221 * "vi:opt opt opt: foo" -- foo interpreted
5222 * Accept "se" for compatibility with Elvis.
5223 */
5224 if (STRNCMP(s, "set ", (size_t)4) == 0
5225 || STRNCMP(s, "se ", (size_t)3) == 0)
5226 {
5227 if (*e != ':') /* no terminating ':'? */
5228 break;
5229 end = TRUE;
5230 s = vim_strchr(s, ' ') + 1;
5231 }
5232 *e = NUL; /* truncate the set command */
5233
5234 if (*s != NUL) /* skip over an empty "::" */
5235 {
5236#ifdef FEAT_EVAL
5237 save_SID = current_SID;
5238 current_SID = SID_MODELINE;
5239#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005240 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#ifdef FEAT_EVAL
5242 current_SID = save_SID;
5243#endif
5244 if (retval == FAIL) /* stop if error found */
5245 break;
5246 }
5247 s = e + 1; /* advance to next part */
5248 }
5249
5250 sourcing_lnum = save_sourcing_lnum;
5251 sourcing_name = save_sourcing_name;
5252
5253 vim_free(linecopy);
5254 }
5255 return retval;
5256}
5257
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005258#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 int
5260read_viminfo_bufferlist(virp, writing)
5261 vir_T *virp;
5262 int writing;
5263{
5264 char_u *tab;
5265 linenr_T lnum;
5266 colnr_T col;
5267 buf_T *buf;
5268 char_u *sfname;
5269 char_u *xline;
5270
5271 /* Handle long line and escaped characters. */
5272 xline = viminfo_readstring(virp, 1, FALSE);
5273
5274 /* don't read in if there are files on the command-line or if writing: */
5275 if (xline != NULL && !writing && ARGCOUNT == 0
5276 && find_viminfo_parameter('%') != NULL)
5277 {
5278 /* Format is: <fname> Tab <lnum> Tab <col>.
5279 * Watch out for a Tab in the file name, work from the end. */
5280 lnum = 0;
5281 col = 0;
5282 tab = vim_strrchr(xline, '\t');
5283 if (tab != NULL)
5284 {
5285 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005286 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 tab = vim_strrchr(xline, '\t');
5288 if (tab != NULL)
5289 {
5290 *tab++ = '\0';
5291 lnum = atol((char *)tab);
5292 }
5293 }
5294
5295 /* Expand "~/" in the file name at "line + 1" to a full path.
5296 * Then try shortening it by comparing with the current directory */
5297 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005298 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
5300 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5301 if (buf != NULL) /* just in case... */
5302 {
5303 buf->b_last_cursor.lnum = lnum;
5304 buf->b_last_cursor.col = col;
5305 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5306 }
5307 }
5308 vim_free(xline);
5309
5310 return viminfo_readline(virp);
5311}
5312
5313 void
5314write_viminfo_bufferlist(fp)
5315 FILE *fp;
5316{
5317 buf_T *buf;
5318#ifdef FEAT_WINDOWS
5319 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005320 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#endif
5322 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005323 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
5325 if (find_viminfo_parameter('%') == NULL)
5326 return;
5327
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005328 /* Without a number -1 is returned: do all buffers. */
5329 max_buffers = get_viminfo_parameter('%');
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005332#define LINE_BUF_LEN (MAXPATHL + 40)
5333 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (line == NULL)
5335 return;
5336
5337#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005338 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 set_last_cursor(win);
5340#else
5341 set_last_cursor(curwin);
5342#endif
5343
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005344 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5346 {
5347 if (buf->b_fname == NULL
5348 || !buf->b_p_bl
5349#ifdef FEAT_QUICKFIX
5350 || bt_quickfix(buf)
5351#endif
5352 || removable(buf->b_ffname))
5353 continue;
5354
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005355 if (max_buffers-- == 0)
5356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 putc('%', fp);
5358 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005359 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 (long)buf->b_last_cursor.lnum,
5361 buf->b_last_cursor.col);
5362 viminfo_writestring(fp, line);
5363 }
5364 vim_free(line);
5365}
5366#endif
5367
5368
5369/*
5370 * Return special buffer name.
5371 * Returns NULL when the buffer has a normal file name.
5372 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005373 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374buf_spname(buf)
5375 buf_T *buf;
5376{
5377#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5378 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005379 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005380 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005381 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005382
5383 /*
5384 * For location list window, w_llist_ref points to the location list.
5385 * For quickfix window, w_llist_ref is NULL.
5386 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005387 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005388 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005389 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005390 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392#endif
5393#ifdef FEAT_QUICKFIX
5394 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5395 * contains the name as specified by the user */
5396 if (bt_nofile(buf))
5397 {
5398 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005399 return buf->b_sfname;
5400 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402#endif
5403 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005404 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 return NULL;
5406}
5407
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005408#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5409 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5410 || defined(PROTO)
5411/*
5412 * Find a window for buffer "buf".
5413 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5414 * If not found FAIL is returned.
5415 */
5416 int
5417find_win_for_buf(buf, wp, tp)
5418 buf_T *buf;
5419 win_T **wp;
5420 tabpage_T **tp;
5421{
5422 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5423 if ((*wp)->w_buffer == buf)
5424 goto win_found;
5425 return FAIL;
5426win_found:
5427 return OK;
5428}
5429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
5431#if defined(FEAT_SIGNS) || defined(PROTO)
5432/*
5433 * Insert the sign into the signlist.
5434 */
5435 static void
5436insert_sign(buf, prev, next, id, lnum, typenr)
5437 buf_T *buf; /* buffer to store sign in */
5438 signlist_T *prev; /* previous sign entry */
5439 signlist_T *next; /* next sign entry */
5440 int id; /* sign ID */
5441 linenr_T lnum; /* line number which gets the mark */
5442 int typenr; /* typenr of sign we are adding */
5443{
5444 signlist_T *newsign;
5445
5446 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5447 if (newsign != NULL)
5448 {
5449 newsign->id = id;
5450 newsign->lnum = lnum;
5451 newsign->typenr = typenr;
5452 newsign->next = next;
5453#ifdef FEAT_NETBEANS_INTG
5454 newsign->prev = prev;
5455 if (next != NULL)
5456 next->prev = newsign;
5457#endif
5458
5459 if (prev == NULL)
5460 {
5461 /* When adding first sign need to redraw the windows to create the
5462 * column for signs. */
5463 if (buf->b_signlist == NULL)
5464 {
5465 redraw_buf_later(buf, NOT_VALID);
5466 changed_cline_bef_curs();
5467 }
5468
5469 /* first sign in signlist */
5470 buf->b_signlist = newsign;
5471 }
5472 else
5473 prev->next = newsign;
5474 }
5475}
5476
5477/*
5478 * Add the sign into the signlist. Find the right spot to do it though.
5479 */
5480 void
5481buf_addsign(buf, id, lnum, typenr)
5482 buf_T *buf; /* buffer to store sign in */
5483 int id; /* sign ID */
5484 linenr_T lnum; /* line number which gets the mark */
5485 int typenr; /* typenr of sign we are adding */
5486{
5487 signlist_T *sign; /* a sign in the signlist */
5488 signlist_T *prev; /* the previous sign */
5489
5490 prev = NULL;
5491 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5492 {
5493 if (lnum == sign->lnum && id == sign->id)
5494 {
5495 sign->typenr = typenr;
5496 return;
5497 }
5498 else if (
5499#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5500 id < 0 &&
5501#endif
5502 lnum < sign->lnum)
5503 {
5504#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5505 /* XXX - GRP: Is this because of sign slide problem? Or is it
5506 * really needed? Or is it because we allow multiple signs per
5507 * line? If so, should I add that feature to FEAT_SIGNS?
5508 */
5509 while (prev != NULL && prev->lnum == lnum)
5510 prev = prev->prev;
5511 if (prev == NULL)
5512 sign = buf->b_signlist;
5513 else
5514 sign = prev->next;
5515#endif
5516 insert_sign(buf, prev, sign, id, lnum, typenr);
5517 return;
5518 }
5519 prev = sign;
5520 }
5521#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5522 /* XXX - GRP: See previous comment */
5523 while (prev != NULL && prev->lnum == lnum)
5524 prev = prev->prev;
5525 if (prev == NULL)
5526 sign = buf->b_signlist;
5527 else
5528 sign = prev->next;
5529#endif
5530 insert_sign(buf, prev, sign, id, lnum, typenr);
5531
5532 return;
5533}
5534
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005535/*
5536 * For an existing, placed sign "markId" change the type to "typenr".
5537 * Returns the line number of the sign, or zero if the sign is not found.
5538 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005539 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540buf_change_sign_type(buf, markId, typenr)
5541 buf_T *buf; /* buffer to store sign in */
5542 int markId; /* sign ID */
5543 int typenr; /* typenr of sign we are adding */
5544{
5545 signlist_T *sign; /* a sign in the signlist */
5546
5547 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5548 {
5549 if (sign->id == markId)
5550 {
5551 sign->typenr = typenr;
5552 return sign->lnum;
5553 }
5554 }
5555
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005556 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557}
5558
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005559 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560buf_getsigntype(buf, lnum, type)
5561 buf_T *buf;
5562 linenr_T lnum;
5563 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5564{
5565 signlist_T *sign; /* a sign in a b_signlist */
5566
5567 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5568 if (sign->lnum == lnum
5569 && (type == SIGN_ANY
5570# ifdef FEAT_SIGN_ICONS
5571 || (type == SIGN_ICON
5572 && sign_get_image(sign->typenr) != NULL)
5573# endif
5574 || (type == SIGN_TEXT
5575 && sign_get_text(sign->typenr) != NULL)
5576 || (type == SIGN_LINEHL
5577 && sign_get_attr(sign->typenr, TRUE) != 0)))
5578 return sign->typenr;
5579 return 0;
5580}
5581
5582
5583 linenr_T
5584buf_delsign(buf, id)
5585 buf_T *buf; /* buffer sign is stored in */
5586 int id; /* sign id */
5587{
5588 signlist_T **lastp; /* pointer to pointer to current sign */
5589 signlist_T *sign; /* a sign in a b_signlist */
5590 signlist_T *next; /* the next sign in a b_signlist */
5591 linenr_T lnum; /* line number whose sign was deleted */
5592
5593 lastp = &buf->b_signlist;
5594 lnum = 0;
5595 for (sign = buf->b_signlist; sign != NULL; sign = next)
5596 {
5597 next = sign->next;
5598 if (sign->id == id)
5599 {
5600 *lastp = next;
5601#ifdef FEAT_NETBEANS_INTG
5602 if (next != NULL)
5603 next->prev = sign->prev;
5604#endif
5605 lnum = sign->lnum;
5606 vim_free(sign);
5607 break;
5608 }
5609 else
5610 lastp = &sign->next;
5611 }
5612
5613 /* When deleted the last sign need to redraw the windows to remove the
5614 * sign column. */
5615 if (buf->b_signlist == NULL)
5616 {
5617 redraw_buf_later(buf, NOT_VALID);
5618 changed_cline_bef_curs();
5619 }
5620
5621 return lnum;
5622}
5623
5624
5625/*
5626 * Find the line number of the sign with the requested id. If the sign does
5627 * not exist, return 0 as the line number. This will still let the correct file
5628 * get loaded.
5629 */
5630 int
5631buf_findsign(buf, id)
5632 buf_T *buf; /* buffer to store sign in */
5633 int id; /* sign ID */
5634{
5635 signlist_T *sign; /* a sign in the signlist */
5636
5637 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5638 if (sign->id == id)
5639 return sign->lnum;
5640
5641 return 0;
5642}
5643
5644 int
5645buf_findsign_id(buf, lnum)
5646 buf_T *buf; /* buffer whose sign we are searching for */
5647 linenr_T lnum; /* line number of sign */
5648{
5649 signlist_T *sign; /* a sign in the signlist */
5650
5651 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5652 if (sign->lnum == lnum)
5653 return sign->id;
5654
5655 return 0;
5656}
5657
5658
5659# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5660/* see if a given type of sign exists on a specific line */
5661 int
5662buf_findsigntype_id(buf, lnum, typenr)
5663 buf_T *buf; /* buffer whose sign we are searching for */
5664 linenr_T lnum; /* line number of sign */
5665 int typenr; /* sign type number */
5666{
5667 signlist_T *sign; /* a sign in the signlist */
5668
5669 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5670 if (sign->lnum == lnum && sign->typenr == typenr)
5671 return sign->id;
5672
5673 return 0;
5674}
5675
5676
5677# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5678/* return the number of icons on the given line */
5679 int
5680buf_signcount(buf, lnum)
5681 buf_T *buf;
5682 linenr_T lnum;
5683{
5684 signlist_T *sign; /* a sign in the signlist */
5685 int count = 0;
5686
5687 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5688 if (sign->lnum == lnum)
5689 if (sign_get_image(sign->typenr) != NULL)
5690 count++;
5691
5692 return count;
5693}
5694# endif /* FEAT_SIGN_ICONS */
5695# endif /* FEAT_NETBEANS_INTG */
5696
5697
5698/*
5699 * Delete signs in buffer "buf".
5700 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005701 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702buf_delete_signs(buf)
5703 buf_T *buf;
5704{
5705 signlist_T *next;
5706
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005707 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005708 * sign column. Not when curwin is NULL (this means we're exiting). */
5709 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005710 {
5711 redraw_buf_later(buf, NOT_VALID);
5712 changed_cline_bef_curs();
5713 }
5714
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 while (buf->b_signlist != NULL)
5716 {
5717 next = buf->b_signlist->next;
5718 vim_free(buf->b_signlist);
5719 buf->b_signlist = next;
5720 }
5721}
5722
5723/*
5724 * Delete all signs in all buffers.
5725 */
5726 void
5727buf_delete_all_signs()
5728{
5729 buf_T *buf; /* buffer we are checking for signs */
5730
5731 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5732 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734}
5735
5736/*
5737 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5738 */
5739 void
5740sign_list_placed(rbuf)
5741 buf_T *rbuf;
5742{
5743 buf_T *buf;
5744 signlist_T *p;
5745 char lbuf[BUFSIZ];
5746
5747 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5748 msg_putchar('\n');
5749 if (rbuf == NULL)
5750 buf = firstbuf;
5751 else
5752 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005753 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755 if (buf->b_signlist != NULL)
5756 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005757 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5759 msg_putchar('\n');
5760 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005761 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005763 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5765 MSG_PUTS(lbuf);
5766 msg_putchar('\n');
5767 }
5768 if (rbuf != NULL)
5769 break;
5770 buf = buf->b_next;
5771 }
5772}
5773
5774/*
5775 * Adjust a placed sign for inserted/deleted lines.
5776 */
5777 void
5778sign_mark_adjust(line1, line2, amount, amount_after)
5779 linenr_T line1;
5780 linenr_T line2;
5781 long amount;
5782 long amount_after;
5783{
5784 signlist_T *sign; /* a sign in a b_signlist */
5785
5786 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5787 {
5788 if (sign->lnum >= line1 && sign->lnum <= line2)
5789 {
5790 if (amount == MAXLNUM)
5791 sign->lnum = line1;
5792 else
5793 sign->lnum += amount;
5794 }
5795 else if (sign->lnum > line2)
5796 sign->lnum += amount_after;
5797 }
5798}
5799#endif /* FEAT_SIGNS */
5800
5801/*
5802 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5803 */
5804 void
5805set_buflisted(on)
5806 int on;
5807{
5808 if (on != curbuf->b_p_bl)
5809 {
5810 curbuf->b_p_bl = on;
5811#ifdef FEAT_AUTOCMD
5812 if (on)
5813 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5814 else
5815 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5816#endif
5817 }
5818}
5819
5820/*
5821 * Read the file for "buf" again and check if the contents changed.
5822 * Return TRUE if it changed or this could not be checked.
5823 */
5824 int
5825buf_contents_changed(buf)
5826 buf_T *buf;
5827{
5828 buf_T *newbuf;
5829 int differ = TRUE;
5830 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 exarg_T ea;
5833
5834 /* Allocate a buffer without putting it in the buffer list. */
5835 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5836 if (newbuf == NULL)
5837 return TRUE;
5838
5839 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5840 if (prep_exarg(&ea, buf) == FAIL)
5841 {
5842 wipe_buffer(newbuf, FALSE);
5843 return TRUE;
5844 }
5845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 /* set curwin/curbuf to buf and save a few things */
5847 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
Bram Moolenaar4770d092006-01-12 23:22:24 +00005849 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 && readfile(buf->b_ffname, buf->b_fname,
5851 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5852 &ea, READ_NEW | READ_DUMMY) == OK)
5853 {
5854 /* compare the two files line by line */
5855 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5856 {
5857 differ = FALSE;
5858 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5859 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5860 {
5861 differ = TRUE;
5862 break;
5863 }
5864 }
5865 }
5866 vim_free(ea.cmd);
5867
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 /* restore curwin/curbuf and a few other things */
5869 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
5871 if (curbuf != newbuf) /* safety check */
5872 wipe_buffer(newbuf, FALSE);
5873
5874 return differ;
5875}
5876
5877/*
5878 * Wipe out a buffer and decrement the last buffer number if it was used for
5879 * this buffer. Call this to wipe out a temp buffer that does not contain any
5880 * marks.
5881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 void
5883wipe_buffer(buf, aucmd)
5884 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005885 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 if (buf->b_fnum == top_file_num - 1)
5888 --top_file_num;
5889
5890#ifdef FEAT_AUTOCMD
5891 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005892 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005894 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#ifdef FEAT_AUTOCMD
5896 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005897 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898#endif
5899}