blob: c703465e3f89ae5347e0ff452ee227c6e1744f19 [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)
31static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
32# define HAVE_BUFLIST_MATCH
33static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
34#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
374 if (win != NULL)
375 {
376 /* Set b_last_cursor when closing the last window for the buffer.
377 * Remember the last cursor position and window options of the buffer.
378 * This used to be only for the current window, but then options like
379 * 'foldmethod' may be lost with a ":only" command. */
380 if (buf->b_nwindows == 1)
381 set_last_cursor(win);
382 buflist_setfpos(buf, win,
383 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
384 win->w_cursor.col, TRUE);
385 }
386
387#ifdef FEAT_AUTOCMD
388 /* When the buffer is no longer in a window, trigger BufWinLeave */
389 if (buf->b_nwindows == 1)
390 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200391 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
393 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200394 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100395 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200396 /* Autocommands deleted the buffer. */
397aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100398 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100400 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200401 buf->b_closing = FALSE;
402 if (abort_if_last && one_window())
403 /* Autocommands made this the only window. */
404 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405
406 /* When the buffer becomes hidden, but is not unloaded, trigger
407 * BufHidden */
408 if (!unload_buf)
409 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200410 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
412 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200413 if (!buf_valid(buf))
414 /* Autocommands deleted the buffer. */
415 goto aucmd_abort;
416 buf->b_closing = FALSE;
417 if (abort_if_last && one_window())
418 /* Autocommands made this the only window. */
419 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421# ifdef FEAT_EVAL
422 if (aborting()) /* autocmds may abort script processing */
423 return;
424# endif
425 }
426 nwindows = buf->b_nwindows;
427#endif
428
429 /* decrease the link count from windows (unless not in any window) */
430 if (buf->b_nwindows > 0)
431 --buf->b_nwindows;
432
433 /* Return when a window is displaying the buffer or when it's not
434 * unloaded. */
435 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437
438 /* Always remove the buffer when there is no file name. */
439 if (buf->b_ffname == NULL)
440 del_buf = TRUE;
441
442 /*
443 * Free all things allocated for this buffer.
444 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
445 */
446#ifdef FEAT_AUTOCMD
447 /* Remember if we are closing the current buffer. Restore the number of
448 * windows, so that autocommands in buf_freeall() don't get confused. */
449 is_curbuf = (buf == curbuf);
450 buf->b_nwindows = nwindows;
451#endif
452
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200453 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200454 if (
455#ifdef FEAT_WINDOWS
456 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100457#else
458 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200459#endif
460 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200461 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463#ifdef FEAT_AUTOCMD
464 /* Autocommands may have deleted the buffer. */
465 if (!buf_valid(buf))
466 return;
467# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000468 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 return;
470# endif
471
472 /* Autocommands may have opened or closed windows for this buffer.
473 * Decrement the count for the close we do here. */
474 if (buf->b_nwindows > 0)
475 --buf->b_nwindows;
476
477 /*
478 * It's possible that autocommands change curbuf to the one being deleted.
479 * This might cause the previous curbuf to be deleted unexpectedly. But
480 * in some cases it's OK to delete the curbuf, because a new one is
481 * obtained anyway. Therefore only return if curbuf changed to the
482 * deleted buffer.
483 */
484 if (buf == curbuf && !is_curbuf)
485 return;
486#endif
487
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000488 /* Change directories when the 'acd' option is set. */
489 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
491 /*
492 * Remove the buffer from the list.
493 */
494 if (wipe_buf)
495 {
496#ifdef FEAT_SUN_WORKSHOP
497 if (usingSunWorkShop)
498 workshop_file_closed_lineno((char *)buf->b_ffname,
499 (int)buf->b_last_cursor.lnum);
500#endif
501 vim_free(buf->b_ffname);
502 vim_free(buf->b_sfname);
503 if (buf->b_prev == NULL)
504 firstbuf = buf->b_next;
505 else
506 buf->b_prev->b_next = buf->b_next;
507 if (buf->b_next == NULL)
508 lastbuf = buf->b_prev;
509 else
510 buf->b_next->b_prev = buf->b_prev;
511 free_buffer(buf);
512 }
513 else
514 {
515 if (del_buf)
516 {
517 /* Free all internal variables and reset option values, to make
518 * ":bdel" compatible with Vim 5.7. */
519 free_buffer_stuff(buf, TRUE);
520
521 /* Make it look like a new buffer. */
522 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
523
524 /* Init the options when loaded again. */
525 buf->b_p_initialized = FALSE;
526 }
527 buf_clear_file(buf);
528 if (del_buf)
529 buf->b_p_bl = FALSE;
530 }
531}
532
533/*
534 * Make buffer not contain a file.
535 */
536 void
537buf_clear_file(buf)
538 buf_T *buf;
539{
540 buf->b_ml.ml_line_count = 1;
541 unchanged(buf, TRUE);
542#ifndef SHORT_FNAME
543 buf->b_shortname = FALSE;
544#endif
545 buf->b_p_eol = TRUE;
546 buf->b_start_eol = TRUE;
547#ifdef FEAT_MBYTE
548 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000549 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550#endif
551 buf->b_ml.ml_mfp = NULL;
552 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
553#ifdef FEAT_NETBEANS_INTG
554 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555#endif
556}
557
558/*
559 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200560 * the file. flags:
561 * BFA_DEL buffer is going to be deleted
562 * BFA_WIPE buffer is going to be wiped out
563 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200566buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200568 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
570#ifdef FEAT_AUTOCMD
571 int is_curbuf = (buf == curbuf);
572
Bram Moolenaar362ce482012-06-06 19:02:45 +0200573 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
575 if (!buf_valid(buf)) /* autocommands may delete the buffer */
576 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200577 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
580 if (!buf_valid(buf)) /* autocommands may delete the buffer */
581 return;
582 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200583 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {
585 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
586 FALSE, buf);
587 if (!buf_valid(buf)) /* autocommands may delete the buffer */
588 return;
589 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200590 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000592 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 return;
594# endif
595
596 /*
597 * It's possible that autocommands change curbuf to the one being deleted.
598 * This might cause curbuf to be deleted unexpectedly. But in some cases
599 * it's OK to delete the curbuf, because a new one is obtained anyway.
600 * Therefore only return if curbuf changed to the deleted buffer.
601 */
602 if (buf == curbuf && !is_curbuf)
603 return;
604#endif
605#ifdef FEAT_DIFF
606 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
607#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200608#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100609 /* Remove any ownsyntax, unless exiting. */
610 if (firstwin != NULL && curwin->w_buffer == buf)
611 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200612#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000613
614#ifdef FEAT_FOLDING
615 /* No folds in an empty buffer. */
616# ifdef FEAT_WINDOWS
617 {
618 win_T *win;
619 tabpage_T *tp;
620
621 FOR_ALL_TAB_WINDOWS(tp, win)
622 if (win->w_buffer == buf)
623 clearFolding(win);
624 }
625# else
626 if (curwin->w_buffer == buf)
627 clearFolding(curwin);
628# endif
629#endif
630
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#ifdef FEAT_TCL
632 tcl_buffer_free(buf);
633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 ml_close(buf, TRUE); /* close and delete the memline/memfile */
635 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200636 if ((flags & BFA_KEEP_UNDO) == 0)
637 {
638 u_blockfree(buf); /* free the memory allocated for undo */
639 u_clearall(buf); /* reset all undo information */
640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200642 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000644 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645}
646
647/*
648 * Free a buffer structure and the things it contains related to the buffer
649 * itself (not the file, that must have been done already).
650 */
651 static void
652free_buffer(buf)
653 buf_T *buf;
654{
655 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200656#ifdef FEAT_EVAL
657 unref_var_dict(buf->b_vars);
658#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200659#ifdef FEAT_LUA
660 lua_buffer_free(buf);
661#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000662#ifdef FEAT_MZSCHEME
663 mzscheme_buffer_free(buf);
664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665#ifdef FEAT_PERL
666 perl_buf_free(buf);
667#endif
668#ifdef FEAT_PYTHON
669 python_buffer_free(buf);
670#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200671#ifdef FEAT_PYTHON3
672 python3_buffer_free(buf);
673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#ifdef FEAT_RUBY
675 ruby_buffer_free(buf);
676#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000677#ifdef FEAT_AUTOCMD
678 aubuflocal_remove(buf);
679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 vim_free(buf);
681}
682
683/*
684 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
685 */
686 static void
687free_buffer_stuff(buf, free_options)
688 buf_T *buf;
689 int free_options; /* free options as well */
690{
691 if (free_options)
692 {
693 clear_wininfo(buf); /* including window-local options */
694 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200695#ifdef FEAT_SPELL
696 ga_clear(&buf->b_s.b_langp);
697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 }
699#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200700 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
701 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#endif
703#ifdef FEAT_USR_CMDS
704 uc_clear(&buf->b_ucmds); /* clear local user commands */
705#endif
706#ifdef FEAT_SIGNS
707 buf_delete_signs(buf); /* delete any signs */
708#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000709#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200710 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifdef FEAT_LOCALMAP
713 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
714 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
715#endif
716#ifdef FEAT_MBYTE
717 vim_free(buf->b_start_fenc);
718 buf->b_start_fenc = NULL;
719#endif
720}
721
722/*
723 * Free the b_wininfo list for buffer "buf".
724 */
725 static void
726clear_wininfo(buf)
727 buf_T *buf;
728{
729 wininfo_T *wip;
730
731 while (buf->b_wininfo != NULL)
732 {
733 wip = buf->b_wininfo;
734 buf->b_wininfo = wip->wi_next;
735 if (wip->wi_optset)
736 {
737 clear_winopt(&wip->wi_opt);
738#ifdef FEAT_FOLDING
739 deleteFoldRecurse(&wip->wi_folds);
740#endif
741 }
742 vim_free(wip);
743 }
744}
745
746#if defined(FEAT_LISTCMDS) || defined(PROTO)
747/*
748 * Go to another buffer. Handles the result of the ATTENTION dialog.
749 */
750 void
751goto_buffer(eap, start, dir, count)
752 exarg_T *eap;
753 int start;
754 int dir;
755 int count;
756{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000757# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 buf_T *old_curbuf = curbuf;
759
760 swap_exists_action = SEA_DIALOG;
761# endif
762 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
763 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000764# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
766 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000767# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
768 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000769
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000770 /* Reset the error/interrupt/exception state here so that
771 * aborting() returns FALSE when closing a window. */
772 enter_cleanup(&cs);
773# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000774
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000775 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 win_close(curwin, TRUE);
777 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000778 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000779
780# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
781 /* Restore the error/interrupt/exception state if not discarded by a
782 * new aborting error, interrupt, or uncaught exception. */
783 leave_cleanup(&cs);
784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 else
787 handle_swap_exists(old_curbuf);
788# endif
789}
790#endif
791
Bram Moolenaare64ac772005-12-07 20:54:59 +0000792#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
794 * Handle the situation of swap_exists_action being set.
795 * It is allowed for "old_curbuf" to be NULL or invalid.
796 */
797 void
798handle_swap_exists(old_curbuf)
799 buf_T *old_curbuf;
800{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000801# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
802 cleanup_T cs;
803# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100804#ifdef FEAT_SYN_HL
805 long old_tw = curbuf->b_p_tw;
806#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (swap_exists_action == SEA_QUIT)
809 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000810# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
811 /* Reset the error/interrupt/exception state here so that
812 * aborting() returns FALSE when closing a buffer. */
813 enter_cleanup(&cs);
814# endif
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 /* User selected Quit at ATTENTION prompt. Go back to previous
817 * buffer. If that buffer is gone or the same as the current one,
818 * open a new, empty buffer. */
819 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000820 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100821 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000823 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100827#ifdef FEAT_SYN_HL
828 if (old_tw != curbuf->b_p_tw)
829 check_colorcolumn(curwin);
830#endif
831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000833
834# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
835 /* Restore the error/interrupt/exception state if not discarded by a
836 * new aborting error, interrupt, or uncaught exception. */
837 leave_cleanup(&cs);
838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 else if (swap_exists_action == SEA_RECOVER)
841 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000842# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
843 /* Reset the error/interrupt/exception state here so that
844 * aborting() returns FALSE when closing a buffer. */
845 enter_cleanup(&cs);
846# endif
847
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 /* User selected Recover at ATTENTION prompt. */
849 msg_scroll = TRUE;
850 ml_recover();
851 MSG_PUTS("\n"); /* don't overwrite the last message */
852 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000853 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000854
855# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
856 /* Restore the error/interrupt/exception state if not discarded by a
857 * new aborting error, interrupt, or uncaught exception. */
858 leave_cleanup(&cs);
859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 swap_exists_action = SEA_NONE;
862}
863#endif
864
865#if defined(FEAT_LISTCMDS) || defined(PROTO)
866/*
867 * do_bufdel() - delete or unload buffer(s)
868 *
869 * addr_count == 0: ":bdel" - delete current buffer
870 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
871 * buffer "end_bnr", then any other arguments.
872 * addr_count == 2: ":N,N bdel" - delete buffers in range
873 *
874 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
875 * DOBUF_DEL (":bdel")
876 *
877 * Returns error message or NULL
878 */
879 char_u *
880do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
881 int command;
882 char_u *arg; /* pointer to extra arguments */
883 int addr_count;
884 int start_bnr; /* first buffer number in a range */
885 int end_bnr; /* buffer nr or last buffer nr in a range */
886 int forceit;
887{
888 int do_current = 0; /* delete current buffer? */
889 int deleted = 0; /* number of buffers deleted */
890 char_u *errormsg = NULL; /* return value */
891 int bnr; /* buffer number */
892 char_u *p;
893
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (addr_count == 0)
895 {
896 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
897 }
898 else
899 {
900 if (addr_count == 2)
901 {
902 if (*arg) /* both range and argument is not allowed */
903 return (char_u *)_(e_trailing);
904 bnr = start_bnr;
905 }
906 else /* addr_count == 1 */
907 bnr = end_bnr;
908
909 for ( ;!got_int; ui_breakcheck())
910 {
911 /*
912 * delete the current buffer last, otherwise when the
913 * current buffer is deleted, the next buffer becomes
914 * the current one and will be loaded, which may then
915 * also be deleted, etc.
916 */
917 if (bnr == curbuf->b_fnum)
918 do_current = bnr;
919 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
920 forceit) == OK)
921 ++deleted;
922
923 /*
924 * find next buffer number to delete/unload
925 */
926 if (addr_count == 2)
927 {
928 if (++bnr > end_bnr)
929 break;
930 }
931 else /* addr_count == 1 */
932 {
933 arg = skipwhite(arg);
934 if (*arg == NUL)
935 break;
936 if (!VIM_ISDIGIT(*arg))
937 {
938 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100939 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
940 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 if (bnr < 0) /* failed */
942 break;
943 arg = p;
944 }
945 else
946 bnr = getdigits(&arg);
947 }
948 }
949 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
950 FORWARD, do_current, forceit) == OK)
951 ++deleted;
952
953 if (deleted == 0)
954 {
955 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000956 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000958 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000960 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 errormsg = IObuff;
962 }
963 else if (deleted >= p_report)
964 {
965 if (command == DOBUF_UNLOAD)
966 {
967 if (deleted == 1)
968 MSG(_("1 buffer unloaded"));
969 else
970 smsg((char_u *)_("%d buffers unloaded"), deleted);
971 }
972 else if (command == DOBUF_DEL)
973 {
974 if (deleted == 1)
975 MSG(_("1 buffer deleted"));
976 else
977 smsg((char_u *)_("%d buffers deleted"), deleted);
978 }
979 else
980 {
981 if (deleted == 1)
982 MSG(_("1 buffer wiped out"));
983 else
984 smsg((char_u *)_("%d buffers wiped out"), deleted);
985 }
986 }
987 }
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 return errormsg;
991}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200992#endif /* FEAT_LISTCMDS */
993
994#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
995 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
Bram Moolenaara02471e2014-01-10 16:43:14 +0100997static int empty_curbuf __ARGS((int close_others, int forceit, int action));
998
999/*
1000 * Make the current buffer empty.
1001 * Used when it is wiped out and it's the last buffer.
1002 */
1003 static int
1004empty_curbuf(close_others, forceit, action)
1005 int close_others;
1006 int forceit;
1007 int action;
1008{
1009 int retval;
1010 buf_T *buf = curbuf;
1011
1012 if (action == DOBUF_UNLOAD)
1013 {
1014 EMSG(_("E90: Cannot unload last buffer"));
1015 return FAIL;
1016 }
1017
1018 if (close_others)
1019 {
1020 /* Close any other windows on this buffer, then make it empty. */
1021#ifdef FEAT_WINDOWS
1022 close_windows(buf, TRUE);
1023#endif
1024 }
1025
1026 setpcmark();
1027 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1028 forceit ? ECMD_FORCEIT : 0, curwin);
1029
1030 /*
1031 * do_ecmd() may create a new buffer, then we have to delete
1032 * the old one. But do_ecmd() may have done that already, check
1033 * if the buffer still exists.
1034 */
1035 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1036 close_buffer(NULL, buf, action, FALSE);
1037 if (!close_others)
1038 need_fileinfo = FALSE;
1039 return retval;
1040}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041/*
1042 * Implementation of the commands for the buffer list.
1043 *
1044 * action == DOBUF_GOTO go to specified buffer
1045 * action == DOBUF_SPLIT split window and go to specified buffer
1046 * action == DOBUF_UNLOAD unload specified buffer(s)
1047 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1048 * action == DOBUF_WIPE delete specified buffer(s) really
1049 *
1050 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1051 * start == DOBUF_FIRST go to "count" buffer from first buffer
1052 * start == DOBUF_LAST go to "count" buffer from last buffer
1053 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1054 *
1055 * Return FAIL or OK.
1056 */
1057 int
1058do_buffer(action, start, dir, count, forceit)
1059 int action;
1060 int start;
1061 int dir; /* FORWARD or BACKWARD */
1062 int count; /* buffer number or number of buffers */
1063 int forceit; /* TRUE for :...! */
1064{
1065 buf_T *buf;
1066 buf_T *bp;
1067 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1068 || action == DOBUF_WIPE);
1069
1070 switch (start)
1071 {
1072 case DOBUF_FIRST: buf = firstbuf; break;
1073 case DOBUF_LAST: buf = lastbuf; break;
1074 default: buf = curbuf; break;
1075 }
1076 if (start == DOBUF_MOD) /* find next modified buffer */
1077 {
1078 while (count-- > 0)
1079 {
1080 do
1081 {
1082 buf = buf->b_next;
1083 if (buf == NULL)
1084 buf = firstbuf;
1085 }
1086 while (buf != curbuf && !bufIsChanged(buf));
1087 }
1088 if (!bufIsChanged(buf))
1089 {
1090 EMSG(_("E84: No modified buffer found"));
1091 return FAIL;
1092 }
1093 }
1094 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1095 {
1096 while (buf != NULL && buf->b_fnum != count)
1097 buf = buf->b_next;
1098 }
1099 else
1100 {
1101 bp = NULL;
1102 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1103 {
1104 /* remember the buffer where we start, we come back there when all
1105 * buffers are unlisted. */
1106 if (bp == NULL)
1107 bp = buf;
1108 if (dir == FORWARD)
1109 {
1110 buf = buf->b_next;
1111 if (buf == NULL)
1112 buf = firstbuf;
1113 }
1114 else
1115 {
1116 buf = buf->b_prev;
1117 if (buf == NULL)
1118 buf = lastbuf;
1119 }
1120 /* don't count unlisted buffers */
1121 if (unload || buf->b_p_bl)
1122 {
1123 --count;
1124 bp = NULL; /* use this buffer as new starting point */
1125 }
1126 if (bp == buf)
1127 {
1128 /* back where we started, didn't find anything. */
1129 EMSG(_("E85: There is no listed buffer"));
1130 return FAIL;
1131 }
1132 }
1133 }
1134
1135 if (buf == NULL) /* could not find it */
1136 {
1137 if (start == DOBUF_FIRST)
1138 {
1139 /* don't warn when deleting */
1140 if (!unload)
1141 EMSGN(_("E86: Buffer %ld does not exist"), count);
1142 }
1143 else if (dir == FORWARD)
1144 EMSG(_("E87: Cannot go beyond last buffer"));
1145 else
1146 EMSG(_("E88: Cannot go before first buffer"));
1147 return FAIL;
1148 }
1149
1150#ifdef FEAT_GUI
1151 need_mouse_correct = TRUE;
1152#endif
1153
1154#ifdef FEAT_LISTCMDS
1155 /*
1156 * delete buffer buf from memory and/or the list
1157 */
1158 if (unload)
1159 {
1160 int forward;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
1162 /* When unloading or deleting a buffer that's already unloaded and
1163 * unlisted: fail silently. */
1164 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1165 return FAIL;
1166
1167 if (!forceit && bufIsChanged(buf))
1168 {
1169#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1170 if ((p_confirm || cmdmod.confirm) && p_write)
1171 {
1172 dialog_changed(buf, FALSE);
1173# ifdef FEAT_AUTOCMD
1174 if (!buf_valid(buf))
1175 /* Autocommand deleted buffer, oops! It's not changed
1176 * now. */
1177 return FAIL;
1178# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001179 /* If it's still changed fail silently, the dialog already
1180 * mentioned why it fails. */
1181 if (bufIsChanged(buf))
1182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186 {
1187 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1188 buf->b_fnum);
1189 return FAIL;
1190 }
1191 }
1192
1193 /*
1194 * If deleting the last (listed) buffer, make it empty.
1195 * The last (listed) buffer cannot be unloaded.
1196 */
1197 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1198 if (bp->b_p_bl && bp != buf)
1199 break;
1200 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001201 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
1203#ifdef FEAT_WINDOWS
1204 /*
1205 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001206 * (unless it's the only window). Repeat this so long as we end up in
1207 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001209 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001210# ifdef FEAT_AUTOCMD
1211 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1212# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001213 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001214 {
1215 if (win_close(curwin, FALSE) == FAIL)
1216 break;
1217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#endif
1219
1220 /*
1221 * If the buffer to be deleted is not the current one, delete it here.
1222 */
1223 if (buf != curbuf)
1224 {
1225#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001226 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227#endif
1228 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001229 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 return OK;
1231 }
1232
1233 /*
1234 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001235 * There should be another, otherwise it would have been handled
1236 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 * First use au_new_curbuf, if it is valid.
1238 * Then prefer the buffer we most recently visited.
1239 * Else try to find one that is loaded, after the current buffer,
1240 * then before the current buffer.
1241 * Finally use any buffer.
1242 */
1243 buf = NULL; /* selected buffer */
1244 bp = NULL; /* used when no loaded buffer found */
1245#ifdef FEAT_AUTOCMD
1246 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1247 buf = au_new_curbuf;
1248# ifdef FEAT_JUMPLIST
1249 else
1250# endif
1251#endif
1252#ifdef FEAT_JUMPLIST
1253 if (curwin->w_jumplistlen > 0)
1254 {
1255 int jumpidx;
1256
1257 jumpidx = curwin->w_jumplistidx - 1;
1258 if (jumpidx < 0)
1259 jumpidx = curwin->w_jumplistlen - 1;
1260
1261 forward = jumpidx;
1262 while (jumpidx != curwin->w_jumplistidx)
1263 {
1264 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1265 if (buf != NULL)
1266 {
1267 if (buf == curbuf || !buf->b_p_bl)
1268 buf = NULL; /* skip current and unlisted bufs */
1269 else if (buf->b_ml.ml_mfp == NULL)
1270 {
1271 /* skip unloaded buf, but may keep it for later */
1272 if (bp == NULL)
1273 bp = buf;
1274 buf = NULL;
1275 }
1276 }
1277 if (buf != NULL) /* found a valid buffer: stop searching */
1278 break;
1279 /* advance to older entry in jump list */
1280 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1281 break;
1282 if (--jumpidx < 0)
1283 jumpidx = curwin->w_jumplistlen - 1;
1284 if (jumpidx == forward) /* List exhausted for sure */
1285 break;
1286 }
1287 }
1288#endif
1289
1290 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1291 {
1292 forward = TRUE;
1293 buf = curbuf->b_next;
1294 for (;;)
1295 {
1296 if (buf == NULL)
1297 {
1298 if (!forward) /* tried both directions */
1299 break;
1300 buf = curbuf->b_prev;
1301 forward = FALSE;
1302 continue;
1303 }
1304 /* in non-help buffer, try to skip help buffers, and vv */
1305 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1306 {
1307 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1308 break;
1309 if (bp == NULL) /* remember unloaded buf for later */
1310 bp = buf;
1311 }
1312 if (forward)
1313 buf = buf->b_next;
1314 else
1315 buf = buf->b_prev;
1316 }
1317 }
1318 if (buf == NULL) /* No loaded buffer, use unloaded one */
1319 buf = bp;
1320 if (buf == NULL) /* No loaded buffer, find listed one */
1321 {
1322 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1323 if (buf->b_p_bl && buf != curbuf)
1324 break;
1325 }
1326 if (buf == NULL) /* Still no buffer, just take one */
1327 {
1328 if (curbuf->b_next != NULL)
1329 buf = curbuf->b_next;
1330 else
1331 buf = curbuf->b_prev;
1332 }
1333 }
1334
Bram Moolenaara02471e2014-01-10 16:43:14 +01001335 if (buf == NULL)
1336 {
1337 /* Autocommands must have wiped out all other buffers. Only option
1338 * now is to make the current buffer empty. */
1339 return empty_curbuf(FALSE, forceit, action);
1340 }
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 /*
1343 * make buf current buffer
1344 */
1345 if (action == DOBUF_SPLIT) /* split window first */
1346 {
1347# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001348 /* If 'switchbuf' contains "useopen": jump to first window containing
1349 * "buf" if one exists */
1350 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001351 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001352 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001353 * page containing "buf" if one exists */
1354 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 return OK;
1356 if (win_split(0, 0) == FAIL)
1357# endif
1358 return FAIL;
1359 }
1360#endif
1361
1362 /* go to current buffer - nothing to do */
1363 if (buf == curbuf)
1364 return OK;
1365
1366 /*
1367 * Check if the current buffer may be abandoned.
1368 */
1369 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1370 {
1371#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1372 if ((p_confirm || cmdmod.confirm) && p_write)
1373 {
1374 dialog_changed(curbuf, FALSE);
1375# ifdef FEAT_AUTOCMD
1376 if (!buf_valid(buf))
1377 /* Autocommand deleted buffer, oops! */
1378 return FAIL;
1379# endif
1380 }
1381 if (bufIsChanged(curbuf))
1382#endif
1383 {
1384 EMSG(_(e_nowrtmsg));
1385 return FAIL;
1386 }
1387 }
1388
1389 /* Go to the other buffer. */
1390 set_curbuf(buf, action);
1391
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001392#if defined(FEAT_LISTCMDS) \
1393 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001395 {
1396 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#endif
1399
1400#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1401 if (aborting()) /* autocmds may abort script processing */
1402 return FAIL;
1403#endif
1404
1405 return OK;
1406}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409/*
1410 * Set current buffer to "buf". Executes autocommands and closes current
1411 * buffer. "action" tells how to close the current buffer:
1412 * DOBUF_GOTO free or hide it
1413 * DOBUF_SPLIT nothing
1414 * DOBUF_UNLOAD unload it
1415 * DOBUF_DEL delete it
1416 * DOBUF_WIPE wipe it out
1417 */
1418 void
1419set_curbuf(buf, action)
1420 buf_T *buf;
1421 int action;
1422{
1423 buf_T *prevbuf;
1424 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1425 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001426#ifdef FEAT_SYN_HL
1427 long old_tw = curbuf->b_p_tw;
1428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001431 if (!cmdmod.keepalt)
1432 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001433 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 /* Don't restart Select mode after switching to another buffer. */
1436 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 /* close_windows() or apply_autocmds() may change curbuf */
1439 prevbuf = curbuf;
1440
1441#ifdef FEAT_AUTOCMD
1442 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1443# ifdef FEAT_EVAL
1444 if (buf_valid(prevbuf) && !aborting())
1445# else
1446 if (buf_valid(prevbuf))
1447# endif
1448#endif
1449 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001450#ifdef FEAT_SYN_HL
1451 if (prevbuf == curwin->w_buffer)
1452 reset_synblock(curwin);
1453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454#ifdef FEAT_WINDOWS
1455 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001456 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457#endif
1458#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1459 if (buf_valid(prevbuf) && !aborting())
1460#else
1461 if (buf_valid(prevbuf))
1462#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001463 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001464#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001465 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001466#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001467 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001468 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1470 unload ? action : (action == DOBUF_GOTO
1471 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001472 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001473#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001474 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001475 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001476 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001477#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001481 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001482 * it did ":bunload") or aborted the script processing!
1483 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1484 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001485# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001486 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001487# endif
1488# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001489 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001490# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001491 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001495#ifdef FEAT_SYN_HL
1496 if (old_tw != curbuf->b_p_tw)
1497 check_colorcolumn(curwin);
1498#endif
1499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500}
1501
1502/*
1503 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001504 * Old curbuf must have been abandoned already! This also means "curbuf" may
1505 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 */
1507 void
1508enter_buffer(buf)
1509 buf_T *buf;
1510{
1511 /* Copy buffer and window local option values. Not for a help buffer. */
1512 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1513 if (!buf->b_help)
1514 get_winopts(buf);
1515#ifdef FEAT_FOLDING
1516 else
1517 /* Remove all folds in the window. */
1518 clearFolding(curwin);
1519 foldUpdateAll(curwin); /* update folds (later). */
1520#endif
1521
1522 /* Get the buffer in the current window. */
1523 curwin->w_buffer = buf;
1524 curbuf = buf;
1525 ++curbuf->b_nwindows;
1526
1527#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001528 if (curwin->w_p_diff)
1529 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#endif
1531
Bram Moolenaara971b822011-09-14 14:43:25 +02001532#ifdef FEAT_SYN_HL
1533 curwin->w_s = &(buf->b_s);
1534#endif
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 /* Cursor on first line by default. */
1537 curwin->w_cursor.lnum = 1;
1538 curwin->w_cursor.col = 0;
1539#ifdef FEAT_VIRTUALEDIT
1540 curwin->w_cursor.coladd = 0;
1541#endif
1542 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001543#ifdef FEAT_AUTOCMD
1544 curwin->w_topline_was_set = FALSE;
1545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001547 /* mark cursor position as being invalid */
1548 curwin->w_valid = 0;
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 /* Make sure the buffer is loaded. */
1551 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001552 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001553#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001554 /* If there is no filetype, allow for detecting one. Esp. useful for
1555 * ":ball" used in a autocommand. If there already is a filetype we
1556 * might prefer to keep it. */
1557 if (*curbuf->b_p_ft == NUL)
1558 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001559#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001560
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001561 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 else
1564 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001565 if (!msg_silent)
1566 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1568#ifdef FEAT_AUTOCMD
1569 curwin->w_topline = 1;
1570# ifdef FEAT_DIFF
1571 curwin->w_topfill = 0;
1572# endif
1573 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1574 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1575#endif
1576 }
1577
1578 /* If autocommands did not change the cursor position, restore cursor lnum
1579 * and possibly cursor col. */
1580 if (curwin->w_cursor.lnum == 1 && inindent(0))
1581 buflist_getfpos();
1582
1583 check_arg_idx(curwin); /* check for valid arg_idx */
1584#ifdef FEAT_TITLE
1585 maketitle();
1586#endif
1587#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001588 /* when autocmds didn't change it */
1589 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590#endif
1591 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1592
Bram Moolenaar009b2592004-10-24 19:18:58 +00001593#ifdef FEAT_NETBEANS_INTG
1594 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001595 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001596#endif
1597
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001598 /* Change directories when the 'acd' option is set. */
1599 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601#ifdef FEAT_KEYMAP
1602 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001603 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001605#ifdef FEAT_SPELL
1606 /* May need to set the spell language. Can only do this after the buffer
1607 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001608 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1609 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001610#endif
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 redraw_later(NOT_VALID);
1613}
1614
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001615#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1616/*
1617 * Change to the directory of the current buffer.
1618 */
1619 void
1620do_autochdir()
1621{
1622 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1623 shorten_fnames(TRUE);
1624}
1625#endif
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627/*
1628 * functions for dealing with the buffer list
1629 */
1630
1631/*
1632 * Add a file name to the buffer list. Return a pointer to the buffer.
1633 * If the same file name already exists return a pointer to that buffer.
1634 * If it does not exist, or if fname == NULL, a new entry is created.
1635 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1636 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1637 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 * This is the ONLY way to create a new buffer.
1639 */
1640static int top_file_num = 1; /* highest file number */
1641
1642 buf_T *
1643buflist_new(ffname, sfname, lnum, flags)
1644 char_u *ffname; /* full path of fname or relative */
1645 char_u *sfname; /* short fname or NULL */
1646 linenr_T lnum; /* preferred cursor line */
1647 int flags; /* BLN_ defines */
1648{
1649 buf_T *buf;
1650#ifdef UNIX
1651 struct stat st;
1652#endif
1653
1654 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1655
1656 /*
1657 * If file name already exists in the list, update the entry.
1658 */
1659#ifdef UNIX
1660 /* On Unix we can use inode numbers when the file exists. Works better
1661 * for hard links. */
1662 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1663 st.st_dev = (dev_T)-1;
1664#endif
1665 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1666#ifdef UNIX
1667 buflist_findname_stat(ffname, &st)
1668#else
1669 buflist_findname(ffname)
1670#endif
1671 ) != NULL)
1672 {
1673 vim_free(ffname);
1674 if (lnum != 0)
1675 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1676 /* copy the options now, if 'cpo' doesn't have 's' and not done
1677 * already */
1678 buf_copy_options(buf, 0);
1679 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1680 {
1681 buf->b_p_bl = TRUE;
1682#ifdef FEAT_AUTOCMD
1683 if (!(flags & BLN_DUMMY))
1684 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1685#endif
1686 }
1687 return buf;
1688 }
1689
1690 /*
1691 * If the current buffer has no name and no contents, use the current
1692 * buffer. Otherwise: Need to allocate a new buffer structure.
1693 *
1694 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001695 * (A spell file buffer is allocated in spell.c, but that's not a normal
1696 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 */
1698 buf = NULL;
1699 if ((flags & BLN_CURBUF)
1700 && curbuf != NULL
1701 && curbuf->b_ffname == NULL
1702 && curbuf->b_nwindows <= 1
1703 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1704 {
1705 buf = curbuf;
1706#ifdef FEAT_AUTOCMD
1707 /* It's like this buffer is deleted. Watch out for autocommands that
1708 * change curbuf! If that happens, allocate a new buffer anyway. */
1709 if (curbuf->b_p_bl)
1710 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1711 if (buf == curbuf)
1712 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1713# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001714 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return NULL;
1716# endif
1717#endif
1718#ifdef FEAT_QUICKFIX
1719# ifdef FEAT_AUTOCMD
1720 if (buf == curbuf)
1721# endif
1722 {
1723 /* Make sure 'bufhidden' and 'buftype' are empty */
1724 clear_string_option(&buf->b_p_bh);
1725 clear_string_option(&buf->b_p_bt);
1726 }
1727#endif
1728 }
1729 if (buf != curbuf || curbuf == NULL)
1730 {
1731 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1732 if (buf == NULL)
1733 {
1734 vim_free(ffname);
1735 return NULL;
1736 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001737#ifdef FEAT_EVAL
1738 /* init b: variables */
1739 buf->b_vars = dict_alloc();
1740 if (buf->b_vars == NULL)
1741 {
1742 vim_free(ffname);
1743 vim_free(buf);
1744 return NULL;
1745 }
1746 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749
1750 if (ffname != NULL)
1751 {
1752 buf->b_ffname = ffname;
1753 buf->b_sfname = vim_strsave(sfname);
1754 }
1755
1756 clear_wininfo(buf);
1757 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1758
1759 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1760 || buf->b_wininfo == NULL)
1761 {
1762 vim_free(buf->b_ffname);
1763 buf->b_ffname = NULL;
1764 vim_free(buf->b_sfname);
1765 buf->b_sfname = NULL;
1766 if (buf != curbuf)
1767 free_buffer(buf);
1768 return NULL;
1769 }
1770
1771 if (buf == curbuf)
1772 {
1773 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001774 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (buf != curbuf) /* autocommands deleted the buffer! */
1776 return NULL;
1777#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001778 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 return NULL;
1780#endif
1781 /* buf->b_nwindows = 0; why was this here? */
1782 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001783
1784 /* Init the options. */
1785 buf->b_p_initialized = FALSE;
1786 buf_copy_options(buf, BCO_ENTER);
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#ifdef FEAT_KEYMAP
1789 /* need to reload lmaps and set b:keymap_name */
1790 curbuf->b_kmap_state |= KEYMAP_INIT;
1791#endif
1792 }
1793 else
1794 {
1795 /*
1796 * put new buffer at the end of the buffer list
1797 */
1798 buf->b_next = NULL;
1799 if (firstbuf == NULL) /* buffer list is empty */
1800 {
1801 buf->b_prev = NULL;
1802 firstbuf = buf;
1803 }
1804 else /* append new buffer at end of list */
1805 {
1806 lastbuf->b_next = buf;
1807 buf->b_prev = lastbuf;
1808 }
1809 lastbuf = buf;
1810
1811 buf->b_fnum = top_file_num++;
1812 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1813 {
1814 EMSG(_("W14: Warning: List of file names overflow"));
1815 if (emsg_silent == 0)
1816 {
1817 out_flush();
1818 ui_delay(3000L, TRUE); /* make sure it is noticed */
1819 }
1820 top_file_num = 1;
1821 }
1822
1823 /*
1824 * Always copy the options from the current buffer.
1825 */
1826 buf_copy_options(buf, BCO_ALWAYS);
1827 }
1828
1829 buf->b_wininfo->wi_fpos.lnum = lnum;
1830 buf->b_wininfo->wi_win = curwin;
1831
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001832#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001833 hash_init(&buf->b_s.b_keywtab);
1834 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#endif
1836
1837 buf->b_fname = buf->b_sfname;
1838#ifdef UNIX
1839 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001840 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 else
1842 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001843 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 buf->b_dev = st.st_dev;
1845 buf->b_ino = st.st_ino;
1846 }
1847#endif
1848 buf->b_u_synced = TRUE;
1849 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001850 if (flags & BLN_DUMMY)
1851 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 buf_clear_file(buf);
1853 clrallmarks(buf); /* clear marks */
1854 fmarks_check_names(buf); /* check file marks for this file */
1855 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1856#ifdef FEAT_AUTOCMD
1857 if (!(flags & BLN_DUMMY))
1858 {
1859 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1860 if (flags & BLN_LISTED)
1861 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1862# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001863 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 return NULL;
1865# endif
1866 }
1867#endif
1868
1869 return buf;
1870}
1871
1872/*
1873 * Free the memory for the options of a buffer.
1874 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1875 * 'fileencoding'.
1876 */
1877 void
1878free_buf_options(buf, free_p_ff)
1879 buf_T *buf;
1880 int free_p_ff;
1881{
1882 if (free_p_ff)
1883 {
1884#ifdef FEAT_MBYTE
1885 clear_string_option(&buf->b_p_fenc);
1886#endif
1887 clear_string_option(&buf->b_p_ff);
1888#ifdef FEAT_QUICKFIX
1889 clear_string_option(&buf->b_p_bh);
1890 clear_string_option(&buf->b_p_bt);
1891#endif
1892 }
1893#ifdef FEAT_FIND_ID
1894 clear_string_option(&buf->b_p_def);
1895 clear_string_option(&buf->b_p_inc);
1896# ifdef FEAT_EVAL
1897 clear_string_option(&buf->b_p_inex);
1898# endif
1899#endif
1900#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1901 clear_string_option(&buf->b_p_inde);
1902 clear_string_option(&buf->b_p_indk);
1903#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001904#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1905 clear_string_option(&buf->b_p_bexpr);
1906#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001907#if defined(FEAT_CRYPT)
1908 clear_string_option(&buf->b_p_cm);
1909#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001910#if defined(FEAT_EVAL)
1911 clear_string_option(&buf->b_p_fex);
1912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913#ifdef FEAT_CRYPT
1914 clear_string_option(&buf->b_p_key);
1915#endif
1916 clear_string_option(&buf->b_p_kp);
1917 clear_string_option(&buf->b_p_mps);
1918 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001919 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 clear_string_option(&buf->b_p_isk);
1921#ifdef FEAT_KEYMAP
1922 clear_string_option(&buf->b_p_keymap);
1923 ga_clear(&buf->b_kmap_ga);
1924#endif
1925#ifdef FEAT_COMMENTS
1926 clear_string_option(&buf->b_p_com);
1927#endif
1928#ifdef FEAT_FOLDING
1929 clear_string_option(&buf->b_p_cms);
1930#endif
1931 clear_string_option(&buf->b_p_nf);
1932#ifdef FEAT_SYN_HL
1933 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001934#endif
1935#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001936 clear_string_option(&buf->b_s.b_p_spc);
1937 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001938 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001939 buf->b_s.b_cap_prog = NULL;
1940 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941#endif
1942#ifdef FEAT_SEARCHPATH
1943 clear_string_option(&buf->b_p_sua);
1944#endif
1945#ifdef FEAT_AUTOCMD
1946 clear_string_option(&buf->b_p_ft);
1947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948#ifdef FEAT_CINDENT
1949 clear_string_option(&buf->b_p_cink);
1950 clear_string_option(&buf->b_p_cino);
1951#endif
1952#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1953 clear_string_option(&buf->b_p_cinw);
1954#endif
1955#ifdef FEAT_INS_EXPAND
1956 clear_string_option(&buf->b_p_cpt);
1957#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001958#ifdef FEAT_COMPL_FUNC
1959 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001960 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962#ifdef FEAT_QUICKFIX
1963 clear_string_option(&buf->b_p_gp);
1964 clear_string_option(&buf->b_p_mp);
1965 clear_string_option(&buf->b_p_efm);
1966#endif
1967 clear_string_option(&buf->b_p_ep);
1968 clear_string_option(&buf->b_p_path);
1969 clear_string_option(&buf->b_p_tags);
1970#ifdef FEAT_INS_EXPAND
1971 clear_string_option(&buf->b_p_dict);
1972 clear_string_option(&buf->b_p_tsr);
1973#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001974#ifdef FEAT_TEXTOBJ
1975 clear_string_option(&buf->b_p_qe);
1976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001978 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01001979#ifdef FEAT_LISP
1980 clear_string_option(&buf->b_p_lw);
1981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982}
1983
1984/*
1985 * get alternate file n
1986 * set linenr to lnum or altfpos.lnum if lnum == 0
1987 * also set cursor column to altfpos.col if 'startofline' is not set.
1988 * if (options & GETF_SETMARK) call setpcmark()
1989 * if (options & GETF_ALT) we are jumping to an alternate file.
1990 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1991 *
1992 * return FAIL for failure, OK for success
1993 */
1994 int
1995buflist_getfile(n, lnum, options, forceit)
1996 int n;
1997 linenr_T lnum;
1998 int options;
1999 int forceit;
2000{
2001 buf_T *buf;
2002#ifdef FEAT_WINDOWS
2003 win_T *wp = NULL;
2004#endif
2005 pos_T *fpos;
2006 colnr_T col;
2007
2008 buf = buflist_findnr(n);
2009 if (buf == NULL)
2010 {
2011 if ((options & GETF_ALT) && n == 0)
2012 EMSG(_(e_noalt));
2013 else
2014 EMSGN(_("E92: Buffer %ld not found"), n);
2015 return FAIL;
2016 }
2017
2018 /* if alternate file is the current buffer, nothing to do */
2019 if (buf == curbuf)
2020 return OK;
2021
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002022 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002023 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002024 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002026 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002027#ifdef FEAT_AUTOCMD
2028 if (curbuf_locked())
2029 return FAIL;
2030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
2032 /* altfpos may be changed by getfile(), get it now */
2033 if (lnum == 0)
2034 {
2035 fpos = buflist_findfpos(buf);
2036 lnum = fpos->lnum;
2037 col = fpos->col;
2038 }
2039 else
2040 col = 0;
2041
2042#ifdef FEAT_WINDOWS
2043 if (options & GETF_SWITCH)
2044 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002045 /* If 'switchbuf' contains "useopen": jump to first window containing
2046 * "buf" if one exists */
2047 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 wp = buf_jump_open_win(buf);
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002049 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002050 * page containing "buf" if one exists */
2051 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002052 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00002053 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
2054 * isn't empty: open new window */
2055 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002057 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
2058 tabpage_new();
2059 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002061 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
2063 }
2064#endif
2065
2066 ++RedrawingDisabled;
2067 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2068 lnum, forceit) <= 0)
2069 {
2070 --RedrawingDisabled;
2071
2072 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2073 if (!p_sol && col != 0)
2074 {
2075 curwin->w_cursor.col = col;
2076 check_cursor_col();
2077#ifdef FEAT_VIRTUALEDIT
2078 curwin->w_cursor.coladd = 0;
2079#endif
2080 curwin->w_set_curswant = TRUE;
2081 }
2082 return OK;
2083 }
2084 --RedrawingDisabled;
2085 return FAIL;
2086}
2087
2088/*
2089 * go to the last know line number for the current buffer
2090 */
2091 void
2092buflist_getfpos()
2093{
2094 pos_T *fpos;
2095
2096 fpos = buflist_findfpos(curbuf);
2097
2098 curwin->w_cursor.lnum = fpos->lnum;
2099 check_cursor_lnum();
2100
2101 if (p_sol)
2102 curwin->w_cursor.col = 0;
2103 else
2104 {
2105 curwin->w_cursor.col = fpos->col;
2106 check_cursor_col();
2107#ifdef FEAT_VIRTUALEDIT
2108 curwin->w_cursor.coladd = 0;
2109#endif
2110 curwin->w_set_curswant = TRUE;
2111 }
2112}
2113
Bram Moolenaar81695252004-12-29 20:58:21 +00002114#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2115/*
2116 * Find file in buffer list by name (it has to be for the current window).
2117 * Returns NULL if not found.
2118 */
2119 buf_T *
2120buflist_findname_exp(fname)
2121 char_u *fname;
2122{
2123 char_u *ffname;
2124 buf_T *buf = NULL;
2125
2126 /* First make the name into a full path name */
2127 ffname = FullName_save(fname,
2128#ifdef UNIX
2129 TRUE /* force expansion, get rid of symbolic links */
2130#else
2131 FALSE
2132#endif
2133 );
2134 if (ffname != NULL)
2135 {
2136 buf = buflist_findname(ffname);
2137 vim_free(ffname);
2138 }
2139 return buf;
2140}
2141#endif
2142
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143/*
2144 * Find file in buffer list by name (it has to be for the current window).
2145 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002146 * Skips dummy buffers.
2147 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 */
2149 buf_T *
2150buflist_findname(ffname)
2151 char_u *ffname;
2152{
2153#ifdef UNIX
2154 struct stat st;
2155
2156 if (mch_stat((char *)ffname, &st) < 0)
2157 st.st_dev = (dev_T)-1;
2158 return buflist_findname_stat(ffname, &st);
2159}
2160
2161/*
2162 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2163 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002164 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 */
2166 static buf_T *
2167buflist_findname_stat(ffname, stp)
2168 char_u *ffname;
2169 struct stat *stp;
2170{
2171#endif
2172 buf_T *buf;
2173
2174 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002175 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176#ifdef UNIX
2177 , stp
2178#endif
2179 ))
2180 return buf;
2181 return NULL;
2182}
2183
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002184#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2185 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186/*
2187 * Find file in buffer list by a regexp pattern.
2188 * Return fnum of the found buffer.
2189 * Return < 0 for error.
2190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 int
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002192buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 char_u *pattern;
2194 char_u *pattern_end; /* pointer to first char after pattern */
2195 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002196 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002197 int curtab_only; /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 buf_T *buf;
2200 regprog_T *prog;
2201 int match = -1;
2202 int find_listed;
2203 char_u *pat;
2204 char_u *patend;
2205 int attempt;
2206 char_u *p;
2207 int toggledollar;
2208
2209 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2210 {
2211 if (*pattern == '%')
2212 match = curbuf->b_fnum;
2213 else
2214 match = curwin->w_alt_fnum;
2215#ifdef FEAT_DIFF
2216 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2217 match = -1;
2218#endif
2219 }
2220
2221 /*
2222 * Try four ways of matching a listed buffer:
2223 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002224 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 * attempt == 2: with '$' at end (only match at end)
2226 * attempt == 3: with '^' at start and '$' at end (only full match)
2227 * Repeat this for finding an unlisted buffer if there was no matching
2228 * listed buffer.
2229 */
2230 else
2231 {
2232 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2233 if (pat == NULL)
2234 return -1;
2235 patend = pat + STRLEN(pat) - 1;
2236 toggledollar = (patend > pat && *patend == '$');
2237
2238 /* First try finding a listed buffer. If not found and "unlisted"
2239 * is TRUE, try finding an unlisted buffer. */
2240 find_listed = TRUE;
2241 for (;;)
2242 {
2243 for (attempt = 0; attempt <= 3; ++attempt)
2244 {
2245 /* may add '^' and '$' */
2246 if (toggledollar)
2247 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2248 p = pat;
2249 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2250 ++p;
2251 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2252 if (prog == NULL)
2253 {
2254 vim_free(pat);
2255 return -1;
2256 }
2257
2258 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2259 if (buf->b_p_bl == find_listed
2260#ifdef FEAT_DIFF
2261 && (!diffmode || diff_mode_buf(buf))
2262#endif
2263 && buflist_match(prog, buf) != NULL)
2264 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002265 if (curtab_only)
2266 {
2267 /* Ignore the match if the buffer is not open in
2268 * the current tab. */
2269#ifdef FEAT_WINDOWS
2270 win_T *wp;
2271
2272 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2273 if (wp->w_buffer == buf)
2274 break;
2275 if (wp == NULL)
2276 continue;
2277#else
2278 if (curwin->w_buffer != buf)
2279 continue;
2280#endif
2281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (match >= 0) /* already found a match */
2283 {
2284 match = -2;
2285 break;
2286 }
2287 match = buf->b_fnum; /* remember first match */
2288 }
2289
Bram Moolenaar473de612013-06-08 18:19:48 +02002290 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (match >= 0) /* found one match */
2292 break;
2293 }
2294
2295 /* Only search for unlisted buffers if there was no match with
2296 * a listed buffer. */
2297 if (!unlisted || !find_listed || match != -1)
2298 break;
2299 find_listed = FALSE;
2300 }
2301
2302 vim_free(pat);
2303 }
2304
2305 if (match == -2)
2306 EMSG2(_("E93: More than one match for %s"), pattern);
2307 else if (match < 0)
2308 EMSG2(_("E94: No matching buffer for %s"), pattern);
2309 return match;
2310}
2311#endif
2312
2313#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2314
2315/*
2316 * Find all buffer names that match.
2317 * For command line expansion of ":buf" and ":sbuf".
2318 * Return OK if matches found, FAIL otherwise.
2319 */
2320 int
2321ExpandBufnames(pat, num_file, file, options)
2322 char_u *pat;
2323 int *num_file;
2324 char_u ***file;
2325 int options;
2326{
2327 int count = 0;
2328 buf_T *buf;
2329 int round;
2330 char_u *p;
2331 int attempt;
2332 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002333 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
2335 *num_file = 0; /* return values in case of FAIL */
2336 *file = NULL;
2337
Bram Moolenaar05159a02005-02-26 23:04:13 +00002338 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2339 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002341 patc = alloc((unsigned)STRLEN(pat) + 11);
2342 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002344 STRCPY(patc, "\\(^\\|[\\/]\\)");
2345 STRCPY(patc + 11, pat + 1);
2346 }
2347 else
2348 patc = pat;
2349
2350 /*
2351 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002352 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002353 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002354 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002355 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002356 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002357 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002358 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002359 if (prog == NULL)
2360 {
2361 if (patc != pat)
2362 vim_free(patc);
2363 return FAIL;
2364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366 /*
2367 * round == 1: Count the matches.
2368 * round == 2: Build the array to keep the matches.
2369 */
2370 for (round = 1; round <= 2; ++round)
2371 {
2372 count = 0;
2373 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2374 {
2375 if (!buf->b_p_bl) /* skip unlisted buffers */
2376 continue;
2377 p = buflist_match(prog, buf);
2378 if (p != NULL)
2379 {
2380 if (round == 1)
2381 ++count;
2382 else
2383 {
2384 if (options & WILD_HOME_REPLACE)
2385 p = home_replace_save(buf, p);
2386 else
2387 p = vim_strsave(p);
2388 (*file)[count++] = p;
2389 }
2390 }
2391 }
2392 if (count == 0) /* no match found, break here */
2393 break;
2394 if (round == 1)
2395 {
2396 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2397 if (*file == NULL)
2398 {
Bram Moolenaar473de612013-06-08 18:19:48 +02002399 vim_regfree(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002400 if (patc != pat)
2401 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 return FAIL;
2403 }
2404 }
2405 }
Bram Moolenaar473de612013-06-08 18:19:48 +02002406 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (count) /* match(es) found, break here */
2408 break;
2409 }
2410
Bram Moolenaar05159a02005-02-26 23:04:13 +00002411 if (patc != pat)
2412 vim_free(patc);
2413
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 *num_file = count;
2415 return (count == 0 ? FAIL : OK);
2416}
2417
2418#endif /* FEAT_CMDL_COMPL */
2419
2420#ifdef HAVE_BUFLIST_MATCH
2421/*
2422 * Check for a match on the file name for buffer "buf" with regprog "prog".
2423 */
2424 static char_u *
2425buflist_match(prog, buf)
2426 regprog_T *prog;
2427 buf_T *buf;
2428{
2429 char_u *match;
2430
2431 /* First try the short file name, then the long file name. */
2432 match = fname_match(prog, buf->b_sfname);
2433 if (match == NULL)
2434 match = fname_match(prog, buf->b_ffname);
2435
2436 return match;
2437}
2438
2439/*
2440 * Try matching the regexp in "prog" with file name "name".
2441 * Return "name" when there is a match, NULL when not.
2442 */
2443 static char_u *
2444fname_match(prog, name)
2445 regprog_T *prog;
2446 char_u *name;
2447{
2448 char_u *match = NULL;
2449 char_u *p;
2450 regmatch_T regmatch;
2451
2452 if (name != NULL)
2453 {
2454 regmatch.regprog = prog;
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002455 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (vim_regexec(&regmatch, name, (colnr_T)0))
2457 match = name;
2458 else
2459 {
2460 /* Replace $(HOME) with '~' and try matching again. */
2461 p = home_replace_save(NULL, name);
2462 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2463 match = name;
2464 vim_free(p);
2465 }
2466 }
2467
2468 return match;
2469}
2470#endif
2471
2472/*
2473 * find file in buffer list by number
2474 */
2475 buf_T *
2476buflist_findnr(nr)
2477 int nr;
2478{
2479 buf_T *buf;
2480
2481 if (nr == 0)
2482 nr = curwin->w_alt_fnum;
2483 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2484 if (buf->b_fnum == nr)
2485 return (buf);
2486 return NULL;
2487}
2488
2489/*
2490 * Get name of file 'n' in the buffer list.
2491 * When the file has no name an empty string is returned.
2492 * home_replace() is used to shorten the file name (used for marks).
2493 * Returns a pointer to allocated memory, of NULL when failed.
2494 */
2495 char_u *
2496buflist_nr2name(n, fullname, helptail)
2497 int n;
2498 int fullname;
2499 int helptail; /* for help buffers return tail only */
2500{
2501 buf_T *buf;
2502
2503 buf = buflist_findnr(n);
2504 if (buf == NULL)
2505 return NULL;
2506 return home_replace_save(helptail ? buf : NULL,
2507 fullname ? buf->b_ffname : buf->b_fname);
2508}
2509
2510/*
2511 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2512 * When "copy_options" is TRUE save the local window option values.
2513 * When "lnum" is 0 only do the options.
2514 */
2515 static void
2516buflist_setfpos(buf, win, lnum, col, copy_options)
2517 buf_T *buf;
2518 win_T *win;
2519 linenr_T lnum;
2520 colnr_T col;
2521 int copy_options;
2522{
2523 wininfo_T *wip;
2524
2525 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2526 if (wip->wi_win == win)
2527 break;
2528 if (wip == NULL)
2529 {
2530 /* allocate a new entry */
2531 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2532 if (wip == NULL)
2533 return;
2534 wip->wi_win = win;
2535 if (lnum == 0) /* set lnum even when it's 0 */
2536 lnum = 1;
2537 }
2538 else
2539 {
2540 /* remove the entry from the list */
2541 if (wip->wi_prev)
2542 wip->wi_prev->wi_next = wip->wi_next;
2543 else
2544 buf->b_wininfo = wip->wi_next;
2545 if (wip->wi_next)
2546 wip->wi_next->wi_prev = wip->wi_prev;
2547 if (copy_options && wip->wi_optset)
2548 {
2549 clear_winopt(&wip->wi_opt);
2550#ifdef FEAT_FOLDING
2551 deleteFoldRecurse(&wip->wi_folds);
2552#endif
2553 }
2554 }
2555 if (lnum != 0)
2556 {
2557 wip->wi_fpos.lnum = lnum;
2558 wip->wi_fpos.col = col;
2559 }
2560 if (copy_options)
2561 {
2562 /* Save the window-specific option values. */
2563 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2564#ifdef FEAT_FOLDING
2565 wip->wi_fold_manual = win->w_fold_manual;
2566 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2567#endif
2568 wip->wi_optset = TRUE;
2569 }
2570
2571 /* insert the entry in front of the list */
2572 wip->wi_next = buf->b_wininfo;
2573 buf->b_wininfo = wip;
2574 wip->wi_prev = NULL;
2575 if (wip->wi_next)
2576 wip->wi_next->wi_prev = wip;
2577
2578 return;
2579}
2580
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002581#ifdef FEAT_DIFF
2582static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2583
2584/*
2585 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2586 * page. That's because a diff is local to a tab page.
2587 */
2588 static int
2589wininfo_other_tab_diff(wip)
2590 wininfo_T *wip;
2591{
2592 win_T *wp;
2593
2594 if (wip->wi_opt.wo_diff)
2595 {
2596 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2597 /* return FALSE when it's a window in the current tab page, thus
2598 * the buffer was in diff mode here */
2599 if (wip->wi_win == wp)
2600 return FALSE;
2601 return TRUE;
2602 }
2603 return FALSE;
2604}
2605#endif
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607/*
2608 * Find info for the current window in buffer "buf".
2609 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002610 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2611 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 * Returns NULL when there isn't any info.
2613 */
2614 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002615find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002617 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
2619 wininfo_T *wip;
2620
2621 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002622 if (wip->wi_win == curwin
2623#ifdef FEAT_DIFF
2624 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2625#endif
2626 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002628
2629 /* If no wininfo for curwin, use the first in the list (that doesn't have
2630 * 'diff' set and is in another tab page). */
2631 if (wip == NULL)
2632 {
2633#ifdef FEAT_DIFF
2634 if (skip_diff_buffer)
2635 {
2636 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2637 if (!wininfo_other_tab_diff(wip))
2638 break;
2639 }
2640 else
2641#endif
2642 wip = buf->b_wininfo;
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 return wip;
2645}
2646
2647/*
2648 * Reset the local window options to the values last used in this window.
2649 * If the buffer wasn't used in this window before, use the values from
2650 * the most recently used window. If the values were never set, use the
2651 * global values for the window.
2652 */
2653 void
2654get_winopts(buf)
2655 buf_T *buf;
2656{
2657 wininfo_T *wip;
2658
2659 clear_winopt(&curwin->w_onebuf_opt);
2660#ifdef FEAT_FOLDING
2661 clearFolding(curwin);
2662#endif
2663
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002664 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (wip != NULL && wip->wi_optset)
2666 {
2667 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2668#ifdef FEAT_FOLDING
2669 curwin->w_fold_manual = wip->wi_fold_manual;
2670 curwin->w_foldinvalid = TRUE;
2671 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2672#endif
2673 }
2674 else
2675 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2676
2677#ifdef FEAT_FOLDING
2678 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2679 if (p_fdls >= 0)
2680 curwin->w_p_fdl = p_fdls;
2681#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002682#ifdef FEAT_SYN_HL
2683 check_colorcolumn(curwin);
2684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685}
2686
2687/*
2688 * Find the position (lnum and col) for the buffer 'buf' for the current
2689 * window.
2690 * Returns a pointer to no_position if no position is found.
2691 */
2692 pos_T *
2693buflist_findfpos(buf)
2694 buf_T *buf;
2695{
2696 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002697 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002699 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (wip != NULL)
2701 return &(wip->wi_fpos);
2702 else
2703 return &no_position;
2704}
2705
2706/*
2707 * Find the lnum for the buffer 'buf' for the current window.
2708 */
2709 linenr_T
2710buflist_findlnum(buf)
2711 buf_T *buf;
2712{
2713 return buflist_findfpos(buf)->lnum;
2714}
2715
2716#if defined(FEAT_LISTCMDS) || defined(PROTO)
2717/*
2718 * List all know file names (for :files and :buffers command).
2719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 void
2721buflist_list(eap)
2722 exarg_T *eap;
2723{
2724 buf_T *buf;
2725 int len;
2726 int i;
2727
2728 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2729 {
2730 /* skip unlisted buffers, unless ! was used */
2731 if (!buf->b_p_bl && !eap->forceit)
2732 continue;
2733 msg_putchar('\n');
2734 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002735 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else
2737 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2738
Bram Moolenaar50cde822005-06-05 21:54:54 +00002739 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 buf->b_fnum,
2741 buf->b_p_bl ? ' ' : 'u',
2742 buf == curbuf ? '%' :
2743 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2744 buf->b_ml.ml_mfp == NULL ? ' ' :
2745 (buf->b_nwindows == 0 ? 'h' : 'a'),
2746 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2747 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002748 : (bufIsChanged(buf) ? '+' : ' '),
2749 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
2751 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 i = 40 - vim_strsize(IObuff);
2753 do
2754 {
2755 IObuff[len++] = ' ';
2756 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002757 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2758 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002759 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 msg_outtrans(IObuff);
2761 out_flush(); /* output one line at a time */
2762 ui_breakcheck();
2763 }
2764}
2765#endif
2766
2767/*
2768 * Get file name and line number for file 'fnum'.
2769 * Used by DoOneCmd() for translating '%' and '#'.
2770 * Used by insert_reg() and cmdline_paste() for '#' register.
2771 * Return FAIL if not found, OK for success.
2772 */
2773 int
2774buflist_name_nr(fnum, fname, lnum)
2775 int fnum;
2776 char_u **fname;
2777 linenr_T *lnum;
2778{
2779 buf_T *buf;
2780
2781 buf = buflist_findnr(fnum);
2782 if (buf == NULL || buf->b_fname == NULL)
2783 return FAIL;
2784
2785 *fname = buf->b_fname;
2786 *lnum = buflist_findlnum(buf);
2787
2788 return OK;
2789}
2790
2791/*
2792 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2793 * The file name with the full path is also remembered, for when :cd is used.
2794 * Returns FAIL for failure (file name already in use by other buffer)
2795 * OK otherwise.
2796 */
2797 int
2798setfname(buf, ffname, sfname, message)
2799 buf_T *buf;
2800 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002801 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
Bram Moolenaar81695252004-12-29 20:58:21 +00002803 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804#ifdef UNIX
2805 struct stat st;
2806#endif
2807
2808 if (ffname == NULL || *ffname == NUL)
2809 {
2810 /* Removing the name. */
2811 vim_free(buf->b_ffname);
2812 vim_free(buf->b_sfname);
2813 buf->b_ffname = NULL;
2814 buf->b_sfname = NULL;
2815#ifdef UNIX
2816 st.st_dev = (dev_T)-1;
2817#endif
2818 }
2819 else
2820 {
2821 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2822 if (ffname == NULL) /* out of memory */
2823 return FAIL;
2824
2825 /*
2826 * if the file name is already used in another buffer:
2827 * - if the buffer is loaded, fail
2828 * - if the buffer is not loaded, delete it from the list
2829 */
2830#ifdef UNIX
2831 if (mch_stat((char *)ffname, &st) < 0)
2832 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002833#endif
2834 if (!(buf->b_flags & BF_DUMMY))
2835#ifdef UNIX
2836 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002838 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839#endif
2840 if (obuf != NULL && obuf != buf)
2841 {
2842 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2843 {
2844 if (message)
2845 EMSG(_("E95: Buffer with this name already exists"));
2846 vim_free(ffname);
2847 return FAIL;
2848 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002849 /* delete from the list */
2850 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852 sfname = vim_strsave(sfname);
2853 if (ffname == NULL || sfname == NULL)
2854 {
2855 vim_free(sfname);
2856 vim_free(ffname);
2857 return FAIL;
2858 }
2859#ifdef USE_FNAME_CASE
2860# ifdef USE_LONG_FNAME
2861 if (USE_LONG_FNAME)
2862# endif
2863 fname_case(sfname, 0); /* set correct case for short file name */
2864#endif
2865 vim_free(buf->b_ffname);
2866 vim_free(buf->b_sfname);
2867 buf->b_ffname = ffname;
2868 buf->b_sfname = sfname;
2869 }
2870 buf->b_fname = buf->b_sfname;
2871#ifdef UNIX
2872 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002873 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 else
2875 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002876 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 buf->b_dev = st.st_dev;
2878 buf->b_ino = st.st_ino;
2879 }
2880#endif
2881
2882#ifndef SHORT_FNAME
2883 buf->b_shortname = FALSE;
2884#endif
2885
2886 buf_name_changed(buf);
2887 return OK;
2888}
2889
2890/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002891 * Crude way of changing the name of a buffer. Use with care!
2892 * The name should be relative to the current directory.
2893 */
2894 void
2895buf_set_name(fnum, name)
2896 int fnum;
2897 char_u *name;
2898{
2899 buf_T *buf;
2900
2901 buf = buflist_findnr(fnum);
2902 if (buf != NULL)
2903 {
2904 vim_free(buf->b_sfname);
2905 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002906 buf->b_ffname = vim_strsave(name);
2907 buf->b_sfname = NULL;
2908 /* Allocate ffname and expand into full path. Also resolves .lnk
2909 * files on Win32. */
2910 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002911 buf->b_fname = buf->b_sfname;
2912 }
2913}
2914
2915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 * Take care of what needs to be done when the name of buffer "buf" has
2917 * changed.
2918 */
2919 void
2920buf_name_changed(buf)
2921 buf_T *buf;
2922{
2923 /*
2924 * If the file name changed, also change the name of the swapfile
2925 */
2926 if (buf->b_ml.ml_mfp != NULL)
2927 ml_setname(buf);
2928
2929 if (curwin->w_buffer == buf)
2930 check_arg_idx(curwin); /* check file name for arg list */
2931#ifdef FEAT_TITLE
2932 maketitle(); /* set window title */
2933#endif
2934#ifdef FEAT_WINDOWS
2935 status_redraw_all(); /* status lines need to be redrawn */
2936#endif
2937 fmarks_check_names(buf); /* check named file marks */
2938 ml_timestamp(buf); /* reset timestamp */
2939}
2940
2941/*
2942 * set alternate file name for current window
2943 *
2944 * Used by do_one_cmd(), do_write() and do_ecmd().
2945 * Return the buffer.
2946 */
2947 buf_T *
2948setaltfname(ffname, sfname, lnum)
2949 char_u *ffname;
2950 char_u *sfname;
2951 linenr_T lnum;
2952{
2953 buf_T *buf;
2954
2955 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2956 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002957 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 curwin->w_alt_fnum = buf->b_fnum;
2959 return buf;
2960}
2961
2962/*
2963 * Get alternate file name for current window.
2964 * Return NULL if there isn't any, and give error message if requested.
2965 */
2966 char_u *
2967getaltfname(errmsg)
2968 int errmsg; /* give error message */
2969{
2970 char_u *fname;
2971 linenr_T dummy;
2972
2973 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2974 {
2975 if (errmsg)
2976 EMSG(_(e_noalt));
2977 return NULL;
2978 }
2979 return fname;
2980}
2981
2982/*
2983 * Add a file name to the buflist and return its number.
2984 * Uses same flags as buflist_new(), except BLN_DUMMY.
2985 *
2986 * used by qf_init(), main() and doarglist()
2987 */
2988 int
2989buflist_add(fname, flags)
2990 char_u *fname;
2991 int flags;
2992{
2993 buf_T *buf;
2994
2995 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2996 if (buf != NULL)
2997 return buf->b_fnum;
2998 return 0;
2999}
3000
3001#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3002/*
3003 * Adjust slashes in file names. Called after 'shellslash' was set.
3004 */
3005 void
3006buflist_slash_adjust()
3007{
3008 buf_T *bp;
3009
3010 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3011 {
3012 if (bp->b_ffname != NULL)
3013 slash_adjust(bp->b_ffname);
3014 if (bp->b_sfname != NULL)
3015 slash_adjust(bp->b_sfname);
3016 }
3017}
3018#endif
3019
3020/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003021 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 * Also save the local window option values.
3023 */
3024 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003025buflist_altfpos(win)
3026 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003028 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029}
3030
3031/*
3032 * Return TRUE if 'ffname' is not the same file as current file.
3033 * Fname must have a full path (expanded by mch_FullName()).
3034 */
3035 int
3036otherfile(ffname)
3037 char_u *ffname;
3038{
3039 return otherfile_buf(curbuf, ffname
3040#ifdef UNIX
3041 , NULL
3042#endif
3043 );
3044}
3045
3046 static int
3047otherfile_buf(buf, ffname
3048#ifdef UNIX
3049 , stp
3050#endif
3051 )
3052 buf_T *buf;
3053 char_u *ffname;
3054#ifdef UNIX
3055 struct stat *stp;
3056#endif
3057{
3058 /* no name is different */
3059 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3060 return TRUE;
3061 if (fnamecmp(ffname, buf->b_ffname) == 0)
3062 return FALSE;
3063#ifdef UNIX
3064 {
3065 struct stat st;
3066
3067 /* If no struct stat given, get it now */
3068 if (stp == NULL)
3069 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003070 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 st.st_dev = (dev_T)-1;
3072 stp = &st;
3073 }
3074 /* Use dev/ino to check if the files are the same, even when the names
3075 * are different (possible with links). Still need to compare the
3076 * name above, for when the file doesn't exist yet.
3077 * Problem: The dev/ino changes when a file is deleted (and created
3078 * again) and remains the same when renamed/moved. We don't want to
3079 * mch_stat() each buffer each time, that would be too slow. Get the
3080 * dev/ino again when they appear to match, but not when they appear
3081 * to be different: Could skip a buffer when it's actually the same
3082 * file. */
3083 if (buf_same_ino(buf, stp))
3084 {
3085 buf_setino(buf);
3086 if (buf_same_ino(buf, stp))
3087 return FALSE;
3088 }
3089 }
3090#endif
3091 return TRUE;
3092}
3093
3094#if defined(UNIX) || defined(PROTO)
3095/*
3096 * Set inode and device number for a buffer.
3097 * Must always be called when b_fname is changed!.
3098 */
3099 void
3100buf_setino(buf)
3101 buf_T *buf;
3102{
3103 struct stat st;
3104
3105 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3106 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003107 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 buf->b_dev = st.st_dev;
3109 buf->b_ino = st.st_ino;
3110 }
3111 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003112 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113}
3114
3115/*
3116 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3117 */
3118 static int
3119buf_same_ino(buf, stp)
3120 buf_T *buf;
3121 struct stat *stp;
3122{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003123 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 && stp->st_dev == buf->b_dev
3125 && stp->st_ino == buf->b_ino);
3126}
3127#endif
3128
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003129/*
3130 * Print info about the current buffer.
3131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 void
3133fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003134 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 int shorthelp;
3136 int dont_truncate;
3137{
3138 char_u *name;
3139 int n;
3140 char_u *p;
3141 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003142 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
3144 buffer = alloc(IOSIZE);
3145 if (buffer == NULL)
3146 return;
3147
3148 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3149 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003150 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 p = buffer + STRLEN(buffer);
3152 }
3153 else
3154 p = buffer;
3155
3156 *p++ = '"';
3157 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003158 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 else
3160 {
3161 if (!fullname && curbuf->b_fname != NULL)
3162 name = curbuf->b_fname;
3163 else
3164 name = curbuf->b_ffname;
3165 home_replace(shorthelp ? curbuf : NULL, name, p,
3166 (int)(IOSIZE - (p - buffer)), TRUE);
3167 }
3168
Bram Moolenaara800b422010-06-27 01:15:55 +02003169 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 curbufIsChanged() ? (shortmess(SHM_MOD)
3171 ? " [+]" : _(" [Modified]")) : " ",
3172 (curbuf->b_flags & BF_NOTEDITED)
3173#ifdef FEAT_QUICKFIX
3174 && !bt_dontwrite(curbuf)
3175#endif
3176 ? _("[Not edited]") : "",
3177 (curbuf->b_flags & BF_NEW)
3178#ifdef FEAT_QUICKFIX
3179 && !bt_dontwrite(curbuf)
3180#endif
3181 ? _("[New file]") : "",
3182 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003183 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 : _("[readonly]")) : "",
3185 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3186 || curbuf->b_p_ro) ?
3187 " " : "");
3188 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3189 * causes an overflow, thus for large numbers divide instead. */
3190 if (curwin->w_cursor.lnum > 1000000L)
3191 n = (int)(((long)curwin->w_cursor.lnum) /
3192 ((long)curbuf->b_ml.ml_line_count / 100L));
3193 else
3194 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3195 (long)curbuf->b_ml.ml_line_count);
3196 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3197 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003198 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200#ifdef FEAT_CMDL_INFO
3201 else if (p_ru)
3202 {
3203 /* Current line and column are already on the screen -- webb */
3204 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003205 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003207 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 (long)curbuf->b_ml.ml_line_count, n);
3209 }
3210#endif
3211 else
3212 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003213 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 _("line %ld of %ld --%d%%-- col "),
3215 (long)curwin->w_cursor.lnum,
3216 (long)curbuf->b_ml.ml_line_count,
3217 n);
3218 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003219 len = STRLEN(buffer);
3220 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3222 }
3223
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003224 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
3226 if (dont_truncate)
3227 {
3228 /* Temporarily set msg_scroll to avoid the message being truncated.
3229 * First call msg_start() to get the message in the right place. */
3230 msg_start();
3231 n = msg_scroll;
3232 msg_scroll = TRUE;
3233 msg(buffer);
3234 msg_scroll = n;
3235 }
3236 else
3237 {
3238 p = msg_trunc_attr(buffer, FALSE, 0);
3239 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 /* Need to repeat the message after redrawing when:
3241 * - When restart_edit is set (otherwise there will be a delay
3242 * before redrawing).
3243 * - When the screen was scrolled but there is no wait-return
3244 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003245 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247
3248 vim_free(buffer);
3249}
3250
3251 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003252col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003254 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 int col;
3256 int vcol;
3257{
3258 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003259 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003261 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262}
3263
3264#if defined(FEAT_TITLE) || defined(PROTO)
3265/*
3266 * put file name in title bar of window and in icon title
3267 */
3268
3269static char_u *lasttitle = NULL;
3270static char_u *lasticon = NULL;
3271
3272 void
3273maketitle()
3274{
3275 char_u *p;
3276 char_u *t_str = NULL;
3277 char_u *i_name;
3278 char_u *i_str = NULL;
3279 int maxlen = 0;
3280 int len;
3281 int mustset;
3282 char_u buf[IOSIZE];
3283 int off;
3284
3285 if (!redrawing())
3286 {
3287 /* Postpone updating the title when 'lazyredraw' is set. */
3288 need_maketitle = TRUE;
3289 return;
3290 }
3291
3292 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003293 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 return;
3295
3296 if (p_title)
3297 {
3298 if (p_titlelen > 0)
3299 {
3300 maxlen = p_titlelen * Columns / 100;
3301 if (maxlen < 10)
3302 maxlen = 10;
3303 }
3304
3305 t_str = buf;
3306 if (*p_titlestring != NUL)
3307 {
3308#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003309 if (stl_syntax & STL_IN_TITLE)
3310 {
3311 int use_sandbox = FALSE;
3312 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003313
3314# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003315 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003316# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003317 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003319 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003320 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003321 if (called_emsg)
3322 set_string_option_direct((char_u *)"titlestring", -1,
3323 (char_u *)"", OPT_FREE, SID_ERROR);
3324 called_emsg |= save_called_emsg;
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 else
3327#endif
3328 t_str = p_titlestring;
3329 }
3330 else
3331 {
3332 /* format: "fname + (path) (1 of 2) - VIM" */
3333
Bram Moolenaar2c666692012-09-05 13:30:40 +02003334#define SPACE_FOR_FNAME (IOSIZE - 100)
3335#define SPACE_FOR_DIR (IOSIZE - 20)
3336#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003338 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 else
3340 {
3341 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003342 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
3345
3346 switch (bufIsChanged(curbuf)
3347 + (curbuf->b_p_ro * 2)
3348 + (!curbuf->b_p_ma * 4))
3349 {
3350 case 1: STRCAT(buf, " +"); break;
3351 case 2: STRCAT(buf, " ="); break;
3352 case 3: STRCAT(buf, " =+"); break;
3353 case 4:
3354 case 6: STRCAT(buf, " -"); break;
3355 case 5:
3356 case 7: STRCAT(buf, " -+"); break;
3357 }
3358
3359 if (curbuf->b_fname != NULL)
3360 {
3361 /* Get path of file, replace home dir with ~ */
3362 off = (int)STRLEN(buf);
3363 buf[off++] = ' ';
3364 buf[off++] = '(';
3365 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003366 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367#ifdef BACKSLASH_IN_FILENAME
3368 /* avoid "c:/name" to be reduced to "c" */
3369 if (isalpha(buf[off]) && buf[off + 1] == ':')
3370 off += 2;
3371#endif
3372 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003373 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003376 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003377 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003380
Bram Moolenaar2c666692012-09-05 13:30:40 +02003381 /* Translate unprintable chars and concatenate. Keep some
3382 * room for the server name. When there is no room (very long
3383 * file name) use (...). */
3384 if (off < SPACE_FOR_DIR)
3385 {
3386 p = transstr(buf + off);
3387 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3388 vim_free(p);
3389 }
3390 else
3391 {
3392 vim_strncpy(buf + off, (char_u *)"...",
3393 (size_t)(SPACE_FOR_ARGNR - off));
3394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 STRCAT(buf, ")");
3396 }
3397
Bram Moolenaar2c666692012-09-05 13:30:40 +02003398 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
3400#if defined(FEAT_CLIENTSERVER)
3401 if (serverName != NULL)
3402 {
3403 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003404 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 else
3407#endif
3408 STRCAT(buf, " - VIM");
3409
3410 if (maxlen > 0)
3411 {
3412 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003413 if (vim_strsize(buf) > maxlen)
3414 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416 }
3417 }
3418 mustset = ti_change(t_str, &lasttitle);
3419
3420 if (p_icon)
3421 {
3422 i_str = buf;
3423 if (*p_iconstring != NUL)
3424 {
3425#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003426 if (stl_syntax & STL_IN_ICON)
3427 {
3428 int use_sandbox = FALSE;
3429 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003430
3431# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003432 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003433# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003434 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003436 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003437 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003438 if (called_emsg)
3439 set_string_option_direct((char_u *)"iconstring", -1,
3440 (char_u *)"", OPT_FREE, SID_ERROR);
3441 called_emsg |= save_called_emsg;
3442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 else
3444#endif
3445 i_str = p_iconstring;
3446 }
3447 else
3448 {
3449 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003450 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else /* use file name only in icon */
3452 i_name = gettail(curbuf->b_ffname);
3453 *i_str = NUL;
3454 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003455 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if (len > 100)
3457 {
3458 len -= 100;
3459#ifdef FEAT_MBYTE
3460 if (has_mbyte)
3461 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3462#endif
3463 i_name += len;
3464 }
3465 STRCPY(i_str, i_name);
3466 trans_characters(i_str, IOSIZE);
3467 }
3468 }
3469
3470 mustset |= ti_change(i_str, &lasticon);
3471
3472 if (mustset)
3473 resettitle();
3474}
3475
3476/*
3477 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3478 * from "str" if it does.
3479 * Return TRUE when "*last" changed.
3480 */
3481 static int
3482ti_change(str, last)
3483 char_u *str;
3484 char_u **last;
3485{
3486 if ((str == NULL) != (*last == NULL)
3487 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3488 {
3489 vim_free(*last);
3490 if (str == NULL)
3491 *last = NULL;
3492 else
3493 *last = vim_strsave(str);
3494 return TRUE;
3495 }
3496 return FALSE;
3497}
3498
3499/*
3500 * Put current window title back (used after calling a shell)
3501 */
3502 void
3503resettitle()
3504{
3505 mch_settitle(lasttitle, lasticon);
3506}
Bram Moolenaarea408852005-06-25 22:49:46 +00003507
3508# if defined(EXITFREE) || defined(PROTO)
3509 void
3510free_titles()
3511{
3512 vim_free(lasttitle);
3513 vim_free(lasticon);
3514}
3515# endif
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517#endif /* FEAT_TITLE */
3518
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003519#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003521 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 * Return length of string in screen cells.
3523 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003524 * Normally works for window "wp", except when working for 'tabline' then it
3525 * is "curwin".
3526 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 * Items are drawn interspersed with the text that surrounds it
3528 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3529 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3530 *
3531 * If maxwidth is not zero, the string will be filled at any middle marker
3532 * or truncated if too long, fillchar is used for all whitespace.
3533 */
3534 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003535build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3536 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003538 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 size_t outlen; /* length of out[] */
3540 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003541 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 int fillchar;
3543 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003544 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3545 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
3547 char_u *p;
3548 char_u *s;
3549 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003550 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#ifdef FEAT_EVAL
3552 win_T *o_curwin;
3553 buf_T *o_curbuf;
3554#endif
3555 int empty_line;
3556 colnr_T virtcol;
3557 long l;
3558 long n;
3559 int prevchar_isflag;
3560 int prevchar_isitem;
3561 int itemisflag;
3562 int fillable;
3563 char_u *str;
3564 long num;
3565 int width;
3566 int itemcnt;
3567 int curitem;
3568 int groupitem[STL_MAX_ITEM];
3569 int groupdepth;
3570 struct stl_item
3571 {
3572 char_u *start;
3573 int minwid;
3574 int maxwid;
3575 enum
3576 {
3577 Normal,
3578 Empty,
3579 Group,
3580 Middle,
3581 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003582 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 Trunc
3584 } type;
3585 } item[STL_MAX_ITEM];
3586 int minwid;
3587 int maxwid;
3588 int zeropad;
3589 char_u base;
3590 char_u opt;
3591#define TMPLEN 70
3592 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003593 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003594 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003595
3596#ifdef FEAT_EVAL
3597 /*
3598 * When the format starts with "%!" then evaluate it as an expression and
3599 * use the result as the actual format string.
3600 */
3601 if (fmt[0] == '%' && fmt[1] == '!')
3602 {
3603 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3604 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003605 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003606 }
3607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 if (fillchar == 0)
3610 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003611#ifdef FEAT_MBYTE
3612 /* Can't handle a multi-byte fill character yet. */
3613 else if (mb_char2len(fillchar) > 1)
3614 fillchar = '-';
3615#endif
3616
Bram Moolenaar567199b2013-04-24 16:52:36 +02003617 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3618 * p will become invalid when getting another buffer line. */
3619 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3620 empty_line = (*p == NUL);
3621
3622 /* Get the byte value now, in case we need it below. This is more
3623 * efficient than making a copy of the line. */
3624 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3625 byteval = 0;
3626 else
3627#ifdef FEAT_MBYTE
3628 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3629#else
3630 byteval = p[wp->w_cursor.col];
3631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 groupdepth = 0;
3634 p = out;
3635 curitem = 0;
3636 prevchar_isflag = TRUE;
3637 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003638 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003640 if (curitem == STL_MAX_ITEM)
3641 {
3642 /* There are too many items. Add the error code to the statusline
3643 * to give the user a hint about what went wrong. */
3644 if (p + 6 < out + outlen)
3645 {
3646 mch_memmove(p, " E541", (size_t)5);
3647 p += 5;
3648 }
3649 break;
3650 }
3651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (*s != NUL && *s != '%')
3653 prevchar_isflag = prevchar_isitem = FALSE;
3654
3655 /*
3656 * Handle up to the next '%' or the end.
3657 */
3658 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3659 *p++ = *s++;
3660 if (*s == NUL || p + 1 >= out + outlen)
3661 break;
3662
3663 /*
3664 * Handle one '%' item.
3665 */
3666 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003667 if (*s == NUL) /* ignore trailing % */
3668 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (*s == '%')
3670 {
3671 if (p + 1 >= out + outlen)
3672 break;
3673 *p++ = *s++;
3674 prevchar_isflag = prevchar_isitem = FALSE;
3675 continue;
3676 }
3677 if (*s == STL_MIDDLEMARK)
3678 {
3679 s++;
3680 if (groupdepth > 0)
3681 continue;
3682 item[curitem].type = Middle;
3683 item[curitem++].start = p;
3684 continue;
3685 }
3686 if (*s == STL_TRUNCMARK)
3687 {
3688 s++;
3689 item[curitem].type = Trunc;
3690 item[curitem++].start = p;
3691 continue;
3692 }
3693 if (*s == ')')
3694 {
3695 s++;
3696 if (groupdepth < 1)
3697 continue;
3698 groupdepth--;
3699
3700 t = item[groupitem[groupdepth]].start;
3701 *p = NUL;
3702 l = vim_strsize(t);
3703 if (curitem > groupitem[groupdepth] + 1
3704 && item[groupitem[groupdepth]].minwid == 0)
3705 {
3706 /* remove group if all items are empty */
3707 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3708 if (item[n].type == Normal)
3709 break;
3710 if (n == curitem)
3711 {
3712 p = t;
3713 l = 0;
3714 }
3715 }
3716 if (l > item[groupitem[groupdepth]].maxwid)
3717 {
3718 /* truncate, remove n bytes of text at the start */
3719#ifdef FEAT_MBYTE
3720 if (has_mbyte)
3721 {
3722 /* Find the first character that should be included. */
3723 n = 0;
3724 while (l >= item[groupitem[groupdepth]].maxwid)
3725 {
3726 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003727 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729 }
3730 else
3731#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003732 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
3734 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003735 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 p = p - n + 1;
3737#ifdef FEAT_MBYTE
3738 /* Fill up space left over by half a double-wide char. */
3739 while (++l < item[groupitem[groupdepth]].minwid)
3740 *p++ = fillchar;
3741#endif
3742
3743 /* correct the start of the items for the truncation */
3744 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3745 {
3746 item[l].start -= n;
3747 if (item[l].start < t)
3748 item[l].start = t;
3749 }
3750 }
3751 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3752 {
3753 /* fill */
3754 n = item[groupitem[groupdepth]].minwid;
3755 if (n < 0)
3756 {
3757 /* fill by appending characters */
3758 n = 0 - n;
3759 while (l++ < n && p + 1 < out + outlen)
3760 *p++ = fillchar;
3761 }
3762 else
3763 {
3764 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003765 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 l = n - l;
3767 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003768 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 p += l;
3770 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3771 item[n].start += l;
3772 for ( ; l > 0; l--)
3773 *t++ = fillchar;
3774 }
3775 }
3776 continue;
3777 }
3778 minwid = 0;
3779 maxwid = 9999;
3780 zeropad = FALSE;
3781 l = 1;
3782 if (*s == '0')
3783 {
3784 s++;
3785 zeropad = TRUE;
3786 }
3787 if (*s == '-')
3788 {
3789 s++;
3790 l = -1;
3791 }
3792 if (VIM_ISDIGIT(*s))
3793 {
3794 minwid = (int)getdigits(&s);
3795 if (minwid < 0) /* overflow */
3796 minwid = 0;
3797 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003798 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
3800 item[curitem].type = Highlight;
3801 item[curitem].start = p;
3802 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3803 s++;
3804 curitem++;
3805 continue;
3806 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003807 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3808 {
3809 if (*s == STL_TABCLOSENR)
3810 {
3811 if (minwid == 0)
3812 {
3813 /* %X ends the close label, go back to the previously
3814 * define tab label nr. */
3815 for (n = curitem - 1; n >= 0; --n)
3816 if (item[n].type == TabPage && item[n].minwid >= 0)
3817 {
3818 minwid = item[n].minwid;
3819 break;
3820 }
3821 }
3822 else
3823 /* close nrs are stored as negative values */
3824 minwid = - minwid;
3825 }
3826 item[curitem].type = TabPage;
3827 item[curitem].start = p;
3828 item[curitem].minwid = minwid;
3829 s++;
3830 curitem++;
3831 continue;
3832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (*s == '.')
3834 {
3835 s++;
3836 if (VIM_ISDIGIT(*s))
3837 {
3838 maxwid = (int)getdigits(&s);
3839 if (maxwid <= 0) /* overflow */
3840 maxwid = 50;
3841 }
3842 }
3843 minwid = (minwid > 50 ? 50 : minwid) * l;
3844 if (*s == '(')
3845 {
3846 groupitem[groupdepth++] = curitem;
3847 item[curitem].type = Group;
3848 item[curitem].start = p;
3849 item[curitem].minwid = minwid;
3850 item[curitem].maxwid = maxwid;
3851 s++;
3852 curitem++;
3853 continue;
3854 }
3855 if (vim_strchr(STL_ALL, *s) == NULL)
3856 {
3857 s++;
3858 continue;
3859 }
3860 opt = *s++;
3861
3862 /* OK - now for the real work */
3863 base = 'D';
3864 itemisflag = FALSE;
3865 fillable = TRUE;
3866 num = -1;
3867 str = NULL;
3868 switch (opt)
3869 {
3870 case STL_FILEPATH:
3871 case STL_FULLPATH:
3872 case STL_FILENAME:
3873 fillable = FALSE; /* don't change ' ' to fillchar */
3874 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003875 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 else
3877 {
3878 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003879 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3881 }
3882 trans_characters(NameBuff, MAXPATHL);
3883 if (opt != STL_FILENAME)
3884 str = NameBuff;
3885 else
3886 str = gettail(NameBuff);
3887 break;
3888
3889 case STL_VIM_EXPR: /* '{' */
3890 itemisflag = TRUE;
3891 t = p;
3892 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3893 *p++ = *s++;
3894 if (*s != '}') /* missing '}' or out of space */
3895 break;
3896 s++;
3897 *p = 0;
3898 p = t;
3899
3900#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003901 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3903
3904 o_curbuf = curbuf;
3905 o_curwin = curwin;
3906 curwin = wp;
3907 curbuf = wp->w_buffer;
3908
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003909 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 curwin = o_curwin;
3912 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003913 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915 if (str != NULL && *str != 0)
3916 {
3917 if (*skipdigits(str) == NUL)
3918 {
3919 num = atoi((char *)str);
3920 vim_free(str);
3921 str = NULL;
3922 itemisflag = FALSE;
3923 }
3924 }
3925#endif
3926 break;
3927
3928 case STL_LINE:
3929 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3930 ? 0L : (long)(wp->w_cursor.lnum);
3931 break;
3932
3933 case STL_NUMLINES:
3934 num = wp->w_buffer->b_ml.ml_line_count;
3935 break;
3936
3937 case STL_COLUMN:
3938 num = !(State & INSERT) && empty_line
3939 ? 0 : (int)wp->w_cursor.col + 1;
3940 break;
3941
3942 case STL_VIRTCOL:
3943 case STL_VIRTCOL_ALT:
3944 /* In list mode virtcol needs to be recomputed */
3945 virtcol = wp->w_virtcol;
3946 if (wp->w_p_list && lcs_tab1 == NUL)
3947 {
3948 wp->w_p_list = FALSE;
3949 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3950 wp->w_p_list = TRUE;
3951 }
3952 ++virtcol;
3953 /* Don't display %V if it's the same as %c. */
3954 if (opt == STL_VIRTCOL_ALT
3955 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3956 ? 0 : (int)wp->w_cursor.col + 1)))
3957 break;
3958 num = (long)virtcol;
3959 break;
3960
3961 case STL_PERCENTAGE:
3962 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3963 (long)wp->w_buffer->b_ml.ml_line_count);
3964 break;
3965
3966 case STL_ALTPERCENT:
3967 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003968 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 break;
3970
3971 case STL_ARGLISTSTAT:
3972 fillable = FALSE;
3973 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003974 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 str = tmp;
3976 break;
3977
3978 case STL_KEYMAP:
3979 fillable = FALSE;
3980 if (get_keymap_str(wp, tmp, TMPLEN))
3981 str = tmp;
3982 break;
3983 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003984#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003985 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986#else
3987 num = 0;
3988#endif
3989 break;
3990
3991 case STL_BUFNO:
3992 num = wp->w_buffer->b_fnum;
3993 break;
3994
3995 case STL_OFFSET_X:
3996 base = 'X';
3997 case STL_OFFSET:
3998#ifdef FEAT_BYTEOFF
3999 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4000 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4001 0L : l + 1 + (!(State & INSERT) && empty_line ?
4002 0 : (int)wp->w_cursor.col);
4003#endif
4004 break;
4005
4006 case STL_BYTEVAL_X:
4007 base = 'X';
4008 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004009 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (num == NL)
4011 num = 0;
4012 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4013 num = NL;
4014 break;
4015
4016 case STL_ROFLAG:
4017 case STL_ROFLAG_ALT:
4018 itemisflag = TRUE;
4019 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004020 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 break;
4022
4023 case STL_HELPFLAG:
4024 case STL_HELPFLAG_ALT:
4025 itemisflag = TRUE;
4026 if (wp->w_buffer->b_help)
4027 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004028 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 break;
4030
4031#ifdef FEAT_AUTOCMD
4032 case STL_FILETYPE:
4033 if (*wp->w_buffer->b_p_ft != NUL
4034 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4035 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004036 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4037 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 str = tmp;
4039 }
4040 break;
4041
4042 case STL_FILETYPE_ALT:
4043 itemisflag = TRUE;
4044 if (*wp->w_buffer->b_p_ft != NUL
4045 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4046 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004047 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4048 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 for (t = tmp; *t != 0; t++)
4050 *t = TOUPPER_LOC(*t);
4051 str = tmp;
4052 }
4053 break;
4054#endif
4055
4056#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4057 case STL_PREVIEWFLAG:
4058 case STL_PREVIEWFLAG_ALT:
4059 itemisflag = TRUE;
4060 if (wp->w_p_pvw)
4061 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4062 : _("[Preview]"));
4063 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004064
4065 case STL_QUICKFIX:
4066 if (bt_quickfix(wp->w_buffer))
4067 str = (char_u *)(wp->w_llist_ref
4068 ? _(msg_loclist)
4069 : _(msg_qflist));
4070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071#endif
4072
4073 case STL_MODIFIED:
4074 case STL_MODIFIED_ALT:
4075 itemisflag = TRUE;
4076 switch ((opt == STL_MODIFIED_ALT)
4077 + bufIsChanged(wp->w_buffer) * 2
4078 + (!wp->w_buffer->b_p_ma) * 4)
4079 {
4080 case 2: str = (char_u *)"[+]"; break;
4081 case 3: str = (char_u *)",+"; break;
4082 case 4: str = (char_u *)"[-]"; break;
4083 case 5: str = (char_u *)",-"; break;
4084 case 6: str = (char_u *)"[+-]"; break;
4085 case 7: str = (char_u *)",+-"; break;
4086 }
4087 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004088
4089 case STL_HIGHLIGHT:
4090 t = s;
4091 while (*s != '#' && *s != NUL)
4092 ++s;
4093 if (*s == '#')
4094 {
4095 item[curitem].type = Highlight;
4096 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004097 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004098 curitem++;
4099 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004100 if (*s != NUL)
4101 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004102 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104
4105 item[curitem].start = p;
4106 item[curitem].type = Normal;
4107 if (str != NULL && *str)
4108 {
4109 t = str;
4110 if (itemisflag)
4111 {
4112 if ((t[0] && t[1])
4113 && ((!prevchar_isitem && *t == ',')
4114 || (prevchar_isflag && *t == ' ')))
4115 t++;
4116 prevchar_isflag = TRUE;
4117 }
4118 l = vim_strsize(t);
4119 if (l > 0)
4120 prevchar_isitem = TRUE;
4121 if (l > maxwid)
4122 {
4123 while (l >= maxwid)
4124#ifdef FEAT_MBYTE
4125 if (has_mbyte)
4126 {
4127 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004128 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 else
4131#endif
4132 l -= byte2cells(*t++);
4133 if (p + 1 >= out + outlen)
4134 break;
4135 *p++ = '<';
4136 }
4137 if (minwid > 0)
4138 {
4139 for (; l < minwid && p + 1 < out + outlen; l++)
4140 {
4141 /* Don't put a "-" in front of a digit. */
4142 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4143 *p++ = ' ';
4144 else
4145 *p++ = fillchar;
4146 }
4147 minwid = 0;
4148 }
4149 else
4150 minwid *= -1;
4151 while (*t && p + 1 < out + outlen)
4152 {
4153 *p++ = *t++;
4154 /* Change a space by fillchar, unless fillchar is '-' and a
4155 * digit follows. */
4156 if (fillable && p[-1] == ' '
4157 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4158 p[-1] = fillchar;
4159 }
4160 for (; l < minwid && p + 1 < out + outlen; l++)
4161 *p++ = fillchar;
4162 }
4163 else if (num >= 0)
4164 {
4165 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4166 char_u nstr[20];
4167
4168 if (p + 20 >= out + outlen)
4169 break; /* not sufficient space */
4170 prevchar_isitem = TRUE;
4171 t = nstr;
4172 if (opt == STL_VIRTCOL_ALT)
4173 {
4174 *t++ = '-';
4175 minwid--;
4176 }
4177 *t++ = '%';
4178 if (zeropad)
4179 *t++ = '0';
4180 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004181 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 *t = 0;
4183
4184 for (n = num, l = 1; n >= nbase; n /= nbase)
4185 l++;
4186 if (opt == STL_VIRTCOL_ALT)
4187 l++;
4188 if (l > maxwid)
4189 {
4190 l += 2;
4191 n = l - maxwid;
4192 while (l-- > maxwid)
4193 num /= nbase;
4194 *t++ = '>';
4195 *t++ = '%';
4196 *t = t[-3];
4197 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004198 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4199 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004202 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4203 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 p += STRLEN(p);
4205 }
4206 else
4207 item[curitem].type = Empty;
4208
4209 if (opt == STL_VIM_EXPR)
4210 vim_free(str);
4211
4212 if (num >= 0 || (!itemisflag && str && *str))
4213 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4214 curitem++;
4215 }
4216 *p = NUL;
4217 itemcnt = curitem;
4218
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004219#ifdef FEAT_EVAL
4220 if (usefmt != fmt)
4221 vim_free(usefmt);
4222#endif
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 width = vim_strsize(out);
4225 if (maxwidth > 0 && width > maxwidth)
4226 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004227 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 l = 0;
4229 if (itemcnt == 0)
4230 s = out;
4231 else
4232 {
4233 for ( ; l < itemcnt; l++)
4234 if (item[l].type == Trunc)
4235 {
4236 /* Truncate at %< item. */
4237 s = item[l].start;
4238 break;
4239 }
4240 if (l == itemcnt)
4241 {
4242 /* No %< item, truncate first item. */
4243 s = item[0].start;
4244 l = 0;
4245 }
4246 }
4247
4248 if (width - vim_strsize(s) >= maxwidth)
4249 {
4250 /* Truncation mark is beyond max length */
4251#ifdef FEAT_MBYTE
4252 if (has_mbyte)
4253 {
4254 s = out;
4255 width = 0;
4256 for (;;)
4257 {
4258 width += ptr2cells(s);
4259 if (width >= maxwidth)
4260 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004261 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 /* Fill up for half a double-wide character. */
4264 while (++width < maxwidth)
4265 *s++ = fillchar;
4266 }
4267 else
4268#endif
4269 s = out + maxwidth - 1;
4270 for (l = 0; l < itemcnt; l++)
4271 if (item[l].start > s)
4272 break;
4273 itemcnt = l;
4274 *s++ = '>';
4275 *s = 0;
4276 }
4277 else
4278 {
4279#ifdef FEAT_MBYTE
4280 if (has_mbyte)
4281 {
4282 n = 0;
4283 while (width >= maxwidth)
4284 {
4285 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004286 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 }
4289 else
4290#endif
4291 n = width - maxwidth + 1;
4292 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004293 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 *s = '<';
4295
4296 /* Fill up for half a double-wide character. */
4297 while (++width < maxwidth)
4298 {
4299 s = s + STRLEN(s);
4300 *s++ = fillchar;
4301 *s = NUL;
4302 }
4303
4304 --n; /* count the '<' */
4305 for (; l < itemcnt; l++)
4306 {
4307 if (item[l].start - n >= s)
4308 item[l].start -= n;
4309 else
4310 item[l].start = s;
4311 }
4312 }
4313 width = maxwidth;
4314 }
4315 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4316 {
4317 /* Apply STL_MIDDLE if any */
4318 for (l = 0; l < itemcnt; l++)
4319 if (item[l].type == Middle)
4320 break;
4321 if (l < itemcnt)
4322 {
4323 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004324 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 for (s = item[l].start; s < p; s++)
4326 *s = fillchar;
4327 for (l++; l < itemcnt; l++)
4328 item[l].start += maxwidth - width;
4329 width = maxwidth;
4330 }
4331 }
4332
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004333 /* Store the info about highlighting. */
4334 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004336 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 for (l = 0; l < itemcnt; l++)
4338 {
4339 if (item[l].type == Highlight)
4340 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004341 sp->start = item[l].start;
4342 sp->userhl = item[l].minwid;
4343 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004346 sp->start = NULL;
4347 sp->userhl = 0;
4348 }
4349
4350 /* Store the info about tab pages labels. */
4351 if (tabtab != NULL)
4352 {
4353 sp = tabtab;
4354 for (l = 0; l < itemcnt; l++)
4355 {
4356 if (item[l].type == TabPage)
4357 {
4358 sp->start = item[l].start;
4359 sp->userhl = item[l].minwid;
4360 sp++;
4361 }
4362 }
4363 sp->start = NULL;
4364 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366
4367 return width;
4368}
4369#endif /* FEAT_STL_OPT */
4370
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004371#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4372 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004374 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4375 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 */
4377 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004378get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004380 char_u *buf;
4381 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
4383 long above; /* number of lines above window */
4384 long below; /* number of lines below window */
4385
4386 above = wp->w_topline - 1;
4387#ifdef FEAT_DIFF
4388 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4389#endif
4390 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4391 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004392 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4393 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004395 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004397 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 ? (int)(above / ((above + below) / 100L))
4399 : (int)(above * 100L / (above + below)));
4400}
4401#endif
4402
4403/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004404 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 * Return TRUE if it was appended.
4406 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004407 static int
4408append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 win_T *wp;
4410 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004411 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
4414 char_u *p;
4415
4416 if (ARGCOUNT <= 1) /* nothing to do */
4417 return FALSE;
4418
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004419 p = buf + STRLEN(buf); /* go to the end of the buffer */
4420 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 return FALSE;
4422 *p++ = ' ';
4423 *p++ = '(';
4424 if (add_file)
4425 {
4426 STRCPY(p, "file ");
4427 p += 5;
4428 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004429 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4430 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4432 return TRUE;
4433}
4434
4435/*
4436 * If fname is not a full path, make it a full path.
4437 * Returns pointer to allocated memory (NULL for failure).
4438 */
4439 char_u *
4440fix_fname(fname)
4441 char_u *fname;
4442{
4443 /*
4444 * Force expanding the path always for Unix, because symbolic links may
4445 * mess up the full path name, even though it starts with a '/'.
4446 * Also expand when there is ".." in the file name, try to remove it,
4447 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004448 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 * For MS-Windows also expand names like "longna~1" to "longname".
4450 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004451#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 return FullName_save(fname, TRUE);
4453#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004454 if (!vim_isAbsName(fname)
4455 || strstr((char *)fname, "..") != NULL
4456 || strstr((char *)fname, "//") != NULL
4457# ifdef BACKSLASH_IN_FILENAME
4458 || strstr((char *)fname, "\\\\") != NULL
4459# endif
4460# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 )
4464 return FullName_save(fname, FALSE);
4465
4466 fname = vim_strsave(fname);
4467
Bram Moolenaar9b942202007-10-03 12:31:33 +00004468# ifdef USE_FNAME_CASE
4469# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
4473 if (fname != NULL)
4474 fname_case(fname, 0); /* set correct case for file name */
4475 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
4478 return fname;
4479#endif
4480}
4481
4482/*
4483 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4484 * "ffname" becomes a pointer to allocated memory (or NULL).
4485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 void
4487fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004488 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 char_u **ffname;
4490 char_u **sfname;
4491{
4492 if (*ffname == NULL) /* if no file name given, nothing to do */
4493 return;
4494 if (*sfname == NULL) /* if no short file name given, use ffname */
4495 *sfname = *ffname;
4496 *ffname = fix_fname(*ffname); /* expand to full path */
4497
4498#ifdef FEAT_SHORTCUT
4499 if (!buf->b_p_bin)
4500 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004501 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 /* If the file name is a shortcut file, use the file it links to. */
4504 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004505 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
4507 vim_free(*ffname);
4508 *ffname = rfname;
4509 *sfname = rfname;
4510 }
4511 }
4512#endif
4513}
4514
4515/*
4516 * Get the file name for an argument list entry.
4517 */
4518 char_u *
4519alist_name(aep)
4520 aentry_T *aep;
4521{
4522 buf_T *bp;
4523
4524 /* Use the name from the associated buffer if it exists. */
4525 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004526 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 return aep->ae_fname;
4528 return bp->b_fname;
4529}
4530
4531#if defined(FEAT_WINDOWS) || defined(PROTO)
4532/*
4533 * do_arg_all(): Open up to 'count' windows, one for each argument.
4534 */
4535 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004536do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 int count;
4538 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004539 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540{
4541 int i;
4542 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004543 char_u *opened; /* Array of weight for which args are open:
4544 * 0: not opened
4545 * 1: opened in other tab
4546 * 2: opened in curtab
4547 * 3: opened in curtab and curwin
4548 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004549 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 int use_firstwin = FALSE; /* use first window for arglist */
4551 int split_ret = OK;
4552 int p_ea_save;
4553 alist_T *alist; /* argument list to be used */
4554 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004555 tabpage_T *tpnext;
4556 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004557 win_T *old_curwin, *last_curwin;
4558 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004559 win_T *new_curwin = NULL;
4560 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562 if (ARGCOUNT <= 0)
4563 {
4564 /* Don't give an error message. We don't want it when the ":all"
4565 * command is in the .vimrc. */
4566 return;
4567 }
4568 setpcmark();
4569
4570 opened_len = ARGCOUNT;
4571 opened = alloc_clear((unsigned)opened_len);
4572 if (opened == NULL)
4573 return;
4574
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004575 /* Autocommands may do anything to the argument list. Make sure it's not
4576 * freed while we are working here by "locking" it. We still have to
4577 * watch out for its size to be changed. */
4578 alist = curwin->w_alist;
4579 ++alist->al_refcount;
4580
4581 old_curwin = curwin;
4582 old_curtab = curtab;
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#ifdef FEAT_GUI
4585 need_mouse_correct = TRUE;
4586#endif
4587
4588 /*
4589 * Try closing all windows that are not in the argument list.
4590 * Also close windows that are not full width;
4591 * When 'hidden' or "forceit" set the buffer becomes hidden.
4592 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004593 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004595 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004596 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004597 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004599 tpnext = curtab->tp_next;
4600 for (wp = firstwin; wp != NULL; wp = wpnext)
4601 {
4602 wpnext = wp->w_next;
4603 buf = wp->w_buffer;
4604 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004605 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004607 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004609 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004610 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004611 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004613 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004614 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004616 if (i < alist->al_ga.ga_len
4617 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4618 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4619 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004621 int weight = 1;
4622
4623 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004624 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004625 ++weight;
4626 if (old_curwin == wp)
4627 ++weight;
4628 }
4629
4630 if (weight > (int)opened[i])
4631 {
4632 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004633 if (i == 0)
4634 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004635 if (new_curwin != NULL)
4636 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004637 new_curwin = wp;
4638 new_curtab = curtab;
4639 }
4640 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004641 else if (keep_tabs)
4642 i = opened_len;
4643
4644 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004645 {
4646 /* Use the current argument list for all windows
4647 * containing a file from it. */
4648 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004649 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004650 ++wp->w_alist->al_refcount;
4651 }
4652 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004656 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004658 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004660 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004661 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004663 /* If the buffer was changed, and we would like to hide it,
4664 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004665 if (!P_HID(buf) && buf->b_nwindows <= 1
4666 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004668 (void)autowrite(buf, FALSE);
4669#ifdef FEAT_AUTOCMD
4670 /* check if autocommands removed the window */
4671 if (!win_valid(wp) || !buf_valid(buf))
4672 {
4673 wpnext = firstwin; /* start all over... */
4674 continue;
4675 }
4676#endif
4677 }
4678#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004679 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004680 if (firstwin == lastwin
4681 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004682#endif
4683 use_firstwin = TRUE;
4684#ifdef FEAT_WINDOWS
4685 else
4686 {
4687 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4688# ifdef FEAT_AUTOCMD
4689 /* check if autocommands removed the next window */
4690 if (!win_valid(wpnext))
4691 wpnext = firstwin; /* start all over... */
4692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694#endif
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
4697 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004698
4699 /* Without the ":tab" modifier only do the current tab page. */
4700 if (had_tab == 0 || tpnext == NULL)
4701 break;
4702
4703# ifdef FEAT_AUTOCMD
4704 /* check if autocommands removed the next tab page */
4705 if (!valid_tabpage(tpnext))
4706 tpnext = first_tabpage; /* start all over...*/
4707# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004708 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710
4711 /*
4712 * Open a window for files in the argument list that don't have one.
4713 * ARGCOUNT may change while doing this, because of autocommands.
4714 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004715 if (count > opened_len || count <= 0)
4716 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717
4718#ifdef FEAT_AUTOCMD
4719 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4720 ++autocmd_no_enter;
4721 ++autocmd_no_leave;
4722#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004723 last_curwin = curwin;
4724 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004726#ifdef FEAT_WINDOWS
4727 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4728 * leaving an empty tab page when executed locally. */
4729 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4730 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4731 use_firstwin = TRUE;
4732#endif
4733
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004734 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
4736 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4737 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004738 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740 /* Move the already present window to below the current window */
4741 if (curwin->w_arg_idx != i)
4742 {
4743 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4744 {
4745 if (wpnext->w_arg_idx == i)
4746 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004747 if (keep_tabs)
4748 {
4749 new_curwin = wpnext;
4750 new_curtab = curtab;
4751 }
4752 else
4753 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 break;
4755 }
4756 }
4757 }
4758 }
4759 else if (split_ret == OK)
4760 {
4761 if (!use_firstwin) /* split current window */
4762 {
4763 p_ea_save = p_ea;
4764 p_ea = TRUE; /* use space from all windows */
4765 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4766 p_ea = p_ea_save;
4767 if (split_ret == FAIL)
4768 continue;
4769 }
4770#ifdef FEAT_AUTOCMD
4771 else /* first window: do autocmd for leaving this buffer */
4772 --autocmd_no_leave;
4773#endif
4774
4775 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004776 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 */
4778 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004779 if (i == 0)
4780 {
4781 new_curwin = curwin;
4782 new_curtab = curtab;
4783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4785 ECMD_ONE,
4786 ((P_HID(curwin->w_buffer)
4787 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004788 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789#ifdef FEAT_AUTOCMD
4790 if (use_firstwin)
4791 ++autocmd_no_leave;
4792#endif
4793 use_firstwin = FALSE;
4794 }
4795 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004796
4797 /* When ":tab" was used open a new tab for a new window repeatedly. */
4798 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4799 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
4801
4802 /* Remove the "lock" on the argument list. */
4803 alist_unlink(alist);
4804
4805#ifdef FEAT_AUTOCMD
4806 --autocmd_no_enter;
4807#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004808 /* restore last referenced tabpage's curwin */
4809 if (last_curtab != new_curtab)
4810 {
4811 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004812 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004813 if (win_valid(last_curwin))
4814 win_enter(last_curwin, FALSE);
4815 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004816 /* to window with first arg */
4817 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004818 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004819 if (win_valid(new_curwin))
4820 win_enter(new_curwin, FALSE);
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822#ifdef FEAT_AUTOCMD
4823 --autocmd_no_leave;
4824#endif
4825 vim_free(opened);
4826}
4827
4828# if defined(FEAT_LISTCMDS) || defined(PROTO)
4829/*
4830 * Open a window for a number of buffers.
4831 */
4832 void
4833ex_buffer_all(eap)
4834 exarg_T *eap;
4835{
4836 buf_T *buf;
4837 win_T *wp, *wpnext;
4838 int split_ret = OK;
4839 int p_ea_save;
4840 int open_wins = 0;
4841 int r;
4842 int count; /* Maximum number of windows to open. */
4843 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004844#ifdef FEAT_WINDOWS
4845 int had_tab = cmdmod.tab;
4846 tabpage_T *tpnext;
4847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
4849 if (eap->addr_count == 0) /* make as many windows as possible */
4850 count = 9999;
4851 else
4852 count = eap->line2; /* make as many windows as specified */
4853 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4854 all = FALSE;
4855 else
4856 all = TRUE;
4857
4858 setpcmark();
4859
4860#ifdef FEAT_GUI
4861 need_mouse_correct = TRUE;
4862#endif
4863
4864 /*
4865 * Close superfluous windows (two windows for the same buffer).
4866 * Also close windows that are not full-width.
4867 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004868#ifdef FEAT_WINDOWS
4869 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004870 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004871 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004874 tpnext = curtab->tp_next;
4875 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004877 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004878 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004879#ifdef FEAT_VERTSPLIT
4880 || ((cmdmod.split & WSP_VERT)
4881 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004882 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004883 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004885#ifdef FEAT_WINDOWS
4886 || (had_tab > 0 && wp != firstwin)
4887#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004888 ) && firstwin != lastwin
4889#ifdef FEAT_AUTOCMD
4890 && !(wp->w_closing || wp->w_buffer->b_closing)
4891#endif
4892 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004893 {
4894 win_close(wp, FALSE);
4895#ifdef FEAT_AUTOCMD
4896 wpnext = firstwin; /* just in case an autocommand does
4897 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004898 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004899 open_wins = 0;
4900#endif
4901 }
4902 else
4903 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004905
4906#ifdef FEAT_WINDOWS
4907 /* Without the ":tab" modifier only do the current tab page. */
4908 if (had_tab == 0 || tpnext == NULL)
4909 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004910 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914 /*
4915 * Go through the buffer list. When a buffer doesn't have a window yet,
4916 * open one. Otherwise move the window to the right position.
4917 * Watch out for autocommands that delete buffers or windows!
4918 */
4919#ifdef FEAT_AUTOCMD
4920 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4921 ++autocmd_no_enter;
4922#endif
4923 win_enter(lastwin, FALSE);
4924#ifdef FEAT_AUTOCMD
4925 ++autocmd_no_leave;
4926#endif
4927 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4928 {
4929 /* Check if this buffer needs a window */
4930 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4931 continue;
4932
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004933#ifdef FEAT_WINDOWS
4934 if (had_tab != 0)
4935 {
4936 /* With the ":tab" modifier don't move the window. */
4937 if (buf->b_nwindows > 0)
4938 wp = lastwin; /* buffer has a window, skip it */
4939 else
4940 wp = NULL;
4941 }
4942 else
4943#endif
4944 {
4945 /* Check if this buffer already has a window */
4946 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4947 if (wp->w_buffer == buf)
4948 break;
4949 /* If the buffer already has a window, move it */
4950 if (wp != NULL)
4951 win_move_after(wp, curwin);
4952 }
4953
4954 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
4956 /* Split the window and put the buffer in it */
4957 p_ea_save = p_ea;
4958 p_ea = TRUE; /* use space from all windows */
4959 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4960 ++open_wins;
4961 p_ea = p_ea_save;
4962 if (split_ret == FAIL)
4963 continue;
4964
4965 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004966#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 swap_exists_action = SEA_DIALOG;
4968#endif
4969 set_curbuf(buf, DOBUF_GOTO);
4970#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004973#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 break;
4977 }
4978#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004979#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (swap_exists_action == SEA_QUIT)
4981 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004982# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4983 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004984
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004985 /* Reset the error/interrupt/exception state here so that
4986 * aborting() returns FALSE when closing a window. */
4987 enter_cleanup(&cs);
4988# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004989
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004990 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 win_close(curwin, TRUE);
4992 --open_wins;
4993 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004994 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004995
4996# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4997 /* Restore the error/interrupt/exception state if not
4998 * discarded by a new aborting error, interrupt, or uncaught
4999 * exception. */
5000 leave_cleanup(&cs);
5001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003 else
5004 handle_swap_exists(NULL);
5005#endif
5006 }
5007
5008 ui_breakcheck();
5009 if (got_int)
5010 {
5011 (void)vgetc(); /* only break the file loading, not the rest */
5012 break;
5013 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005014#ifdef FEAT_EVAL
5015 /* Autocommands deleted the buffer or aborted script processing!!! */
5016 if (aborting())
5017 break;
5018#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005019#ifdef FEAT_WINDOWS
5020 /* When ":tab" was used open a new tab for a new window repeatedly. */
5021 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5022 cmdmod.tab = 9999;
5023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025#ifdef FEAT_AUTOCMD
5026 --autocmd_no_enter;
5027#endif
5028 win_enter(firstwin, FALSE); /* back to first window */
5029#ifdef FEAT_AUTOCMD
5030 --autocmd_no_leave;
5031#endif
5032
5033 /*
5034 * Close superfluous windows.
5035 */
5036 for (wp = lastwin; open_wins > count; )
5037 {
5038 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5039 || autowrite(wp->w_buffer, FALSE) == OK);
5040#ifdef FEAT_AUTOCMD
5041 if (!win_valid(wp))
5042 {
5043 /* BufWrite Autocommands made the window invalid, start over */
5044 wp = lastwin;
5045 }
5046 else
5047#endif
5048 if (r)
5049 {
5050 win_close(wp, !P_HID(wp->w_buffer));
5051 --open_wins;
5052 wp = lastwin;
5053 }
5054 else
5055 {
5056 wp = wp->w_prev;
5057 if (wp == NULL)
5058 break;
5059 }
5060 }
5061}
5062# endif /* FEAT_LISTCMDS */
5063
5064#endif /* FEAT_WINDOWS */
5065
Bram Moolenaara3227e22006-03-08 21:32:40 +00005066static int chk_modeline __ARGS((linenr_T, int));
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068/*
5069 * do_modelines() - process mode lines for the current file
5070 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005071 * "flags" can be:
5072 * OPT_WINONLY only set options local to window
5073 * OPT_NOWIN don't set options local to window
5074 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 * Returns immediately if the "ml" option isn't set.
5076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00005078do_modelines(flags)
5079 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005081 linenr_T lnum;
5082 int nmlines;
5083 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5086 return;
5087
5088 /* Disallow recursive entry here. Can happen when executing a modeline
5089 * triggers an autocommand, which reloads modelines with a ":do". */
5090 if (entered)
5091 return;
5092
5093 ++entered;
5094 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5095 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005096 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 nmlines = 0;
5098
5099 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5100 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005101 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 nmlines = 0;
5103 --entered;
5104}
5105
5106#include "version.h" /* for version number */
5107
5108/*
5109 * chk_modeline() - check a single line for a mode string
5110 * Return FAIL if an error encountered.
5111 */
5112 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005113chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005115 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116{
5117 char_u *s;
5118 char_u *e;
5119 char_u *linecopy; /* local copy of any modeline found */
5120 int prev;
5121 int vers;
5122 int end;
5123 int retval = OK;
5124 char_u *save_sourcing_name;
5125 linenr_T save_sourcing_lnum;
5126#ifdef FEAT_EVAL
5127 scid_T save_SID;
5128#endif
5129
5130 prev = -1;
5131 for (s = ml_get(lnum); *s != NUL; ++s)
5132 {
5133 if (prev == -1 || vim_isspace(prev))
5134 {
5135 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5136 || STRNCMP(s, "vi:", (size_t)3) == 0)
5137 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005138 /* Accept both "vim" and "Vim". */
5139 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5142 e = s + 4;
5143 else
5144 e = s + 3;
5145 vers = getdigits(&e);
5146 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005147 && (s[0] != 'V'
5148 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 && (s[3] == ':'
5150 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5151 || (VIM_VERSION_100 < vers && s[3] == '<')
5152 || (VIM_VERSION_100 > vers && s[3] == '>')
5153 || (VIM_VERSION_100 == vers && s[3] == '=')))
5154 break;
5155 }
5156 }
5157 prev = *s;
5158 }
5159
5160 if (*s)
5161 {
5162 do /* skip over "ex:", "vi:" or "vim:" */
5163 ++s;
5164 while (s[-1] != ':');
5165
5166 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5167 if (linecopy == NULL)
5168 return FAIL;
5169
5170 save_sourcing_lnum = sourcing_lnum;
5171 save_sourcing_name = sourcing_name;
5172 sourcing_lnum = lnum; /* prepare for emsg() */
5173 sourcing_name = (char_u *)"modelines";
5174
5175 end = FALSE;
5176 while (end == FALSE)
5177 {
5178 s = skipwhite(s);
5179 if (*s == NUL)
5180 break;
5181
5182 /*
5183 * Find end of set command: ':' or end of line.
5184 * Skip over "\:", replacing it with ":".
5185 */
5186 for (e = s; *e != ':' && *e != NUL; ++e)
5187 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005188 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 if (*e == NUL)
5190 end = TRUE;
5191
5192 /*
5193 * If there is a "set" command, require a terminating ':' and
5194 * ignore the stuff after the ':'.
5195 * "vi:set opt opt opt: foo" -- foo not interpreted
5196 * "vi:opt opt opt: foo" -- foo interpreted
5197 * Accept "se" for compatibility with Elvis.
5198 */
5199 if (STRNCMP(s, "set ", (size_t)4) == 0
5200 || STRNCMP(s, "se ", (size_t)3) == 0)
5201 {
5202 if (*e != ':') /* no terminating ':'? */
5203 break;
5204 end = TRUE;
5205 s = vim_strchr(s, ' ') + 1;
5206 }
5207 *e = NUL; /* truncate the set command */
5208
5209 if (*s != NUL) /* skip over an empty "::" */
5210 {
5211#ifdef FEAT_EVAL
5212 save_SID = current_SID;
5213 current_SID = SID_MODELINE;
5214#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005215 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216#ifdef FEAT_EVAL
5217 current_SID = save_SID;
5218#endif
5219 if (retval == FAIL) /* stop if error found */
5220 break;
5221 }
5222 s = e + 1; /* advance to next part */
5223 }
5224
5225 sourcing_lnum = save_sourcing_lnum;
5226 sourcing_name = save_sourcing_name;
5227
5228 vim_free(linecopy);
5229 }
5230 return retval;
5231}
5232
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005233#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 int
5235read_viminfo_bufferlist(virp, writing)
5236 vir_T *virp;
5237 int writing;
5238{
5239 char_u *tab;
5240 linenr_T lnum;
5241 colnr_T col;
5242 buf_T *buf;
5243 char_u *sfname;
5244 char_u *xline;
5245
5246 /* Handle long line and escaped characters. */
5247 xline = viminfo_readstring(virp, 1, FALSE);
5248
5249 /* don't read in if there are files on the command-line or if writing: */
5250 if (xline != NULL && !writing && ARGCOUNT == 0
5251 && find_viminfo_parameter('%') != NULL)
5252 {
5253 /* Format is: <fname> Tab <lnum> Tab <col>.
5254 * Watch out for a Tab in the file name, work from the end. */
5255 lnum = 0;
5256 col = 0;
5257 tab = vim_strrchr(xline, '\t');
5258 if (tab != NULL)
5259 {
5260 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005261 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 tab = vim_strrchr(xline, '\t');
5263 if (tab != NULL)
5264 {
5265 *tab++ = '\0';
5266 lnum = atol((char *)tab);
5267 }
5268 }
5269
5270 /* Expand "~/" in the file name at "line + 1" to a full path.
5271 * Then try shortening it by comparing with the current directory */
5272 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005273 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5276 if (buf != NULL) /* just in case... */
5277 {
5278 buf->b_last_cursor.lnum = lnum;
5279 buf->b_last_cursor.col = col;
5280 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5281 }
5282 }
5283 vim_free(xline);
5284
5285 return viminfo_readline(virp);
5286}
5287
5288 void
5289write_viminfo_bufferlist(fp)
5290 FILE *fp;
5291{
5292 buf_T *buf;
5293#ifdef FEAT_WINDOWS
5294 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005295 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296#endif
5297 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005298 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
5300 if (find_viminfo_parameter('%') == NULL)
5301 return;
5302
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005303 /* Without a number -1 is returned: do all buffers. */
5304 max_buffers = get_viminfo_parameter('%');
5305
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005307#define LINE_BUF_LEN (MAXPATHL + 40)
5308 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 if (line == NULL)
5310 return;
5311
5312#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005313 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 set_last_cursor(win);
5315#else
5316 set_last_cursor(curwin);
5317#endif
5318
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005319 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5321 {
5322 if (buf->b_fname == NULL
5323 || !buf->b_p_bl
5324#ifdef FEAT_QUICKFIX
5325 || bt_quickfix(buf)
5326#endif
5327 || removable(buf->b_ffname))
5328 continue;
5329
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005330 if (max_buffers-- == 0)
5331 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 putc('%', fp);
5333 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005334 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 (long)buf->b_last_cursor.lnum,
5336 buf->b_last_cursor.col);
5337 viminfo_writestring(fp, line);
5338 }
5339 vim_free(line);
5340}
5341#endif
5342
5343
5344/*
5345 * Return special buffer name.
5346 * Returns NULL when the buffer has a normal file name.
5347 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005348 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349buf_spname(buf)
5350 buf_T *buf;
5351{
5352#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5353 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005354 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005355 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005356 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005357
5358 /*
5359 * For location list window, w_llist_ref points to the location list.
5360 * For quickfix window, w_llist_ref is NULL.
5361 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005362 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005363 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005364 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005365 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367#endif
5368#ifdef FEAT_QUICKFIX
5369 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5370 * contains the name as specified by the user */
5371 if (bt_nofile(buf))
5372 {
5373 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005374 return buf->b_sfname;
5375 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377#endif
5378 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005379 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 return NULL;
5381}
5382
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005383#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5384 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5385 || defined(PROTO)
5386/*
5387 * Find a window for buffer "buf".
5388 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5389 * If not found FAIL is returned.
5390 */
5391 int
5392find_win_for_buf(buf, wp, tp)
5393 buf_T *buf;
5394 win_T **wp;
5395 tabpage_T **tp;
5396{
5397 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5398 if ((*wp)->w_buffer == buf)
5399 goto win_found;
5400 return FAIL;
5401win_found:
5402 return OK;
5403}
5404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
5406#if defined(FEAT_SIGNS) || defined(PROTO)
5407/*
5408 * Insert the sign into the signlist.
5409 */
5410 static void
5411insert_sign(buf, prev, next, id, lnum, typenr)
5412 buf_T *buf; /* buffer to store sign in */
5413 signlist_T *prev; /* previous sign entry */
5414 signlist_T *next; /* next sign entry */
5415 int id; /* sign ID */
5416 linenr_T lnum; /* line number which gets the mark */
5417 int typenr; /* typenr of sign we are adding */
5418{
5419 signlist_T *newsign;
5420
5421 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5422 if (newsign != NULL)
5423 {
5424 newsign->id = id;
5425 newsign->lnum = lnum;
5426 newsign->typenr = typenr;
5427 newsign->next = next;
5428#ifdef FEAT_NETBEANS_INTG
5429 newsign->prev = prev;
5430 if (next != NULL)
5431 next->prev = newsign;
5432#endif
5433
5434 if (prev == NULL)
5435 {
5436 /* When adding first sign need to redraw the windows to create the
5437 * column for signs. */
5438 if (buf->b_signlist == NULL)
5439 {
5440 redraw_buf_later(buf, NOT_VALID);
5441 changed_cline_bef_curs();
5442 }
5443
5444 /* first sign in signlist */
5445 buf->b_signlist = newsign;
5446 }
5447 else
5448 prev->next = newsign;
5449 }
5450}
5451
5452/*
5453 * Add the sign into the signlist. Find the right spot to do it though.
5454 */
5455 void
5456buf_addsign(buf, id, lnum, typenr)
5457 buf_T *buf; /* buffer to store sign in */
5458 int id; /* sign ID */
5459 linenr_T lnum; /* line number which gets the mark */
5460 int typenr; /* typenr of sign we are adding */
5461{
5462 signlist_T *sign; /* a sign in the signlist */
5463 signlist_T *prev; /* the previous sign */
5464
5465 prev = NULL;
5466 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5467 {
5468 if (lnum == sign->lnum && id == sign->id)
5469 {
5470 sign->typenr = typenr;
5471 return;
5472 }
5473 else if (
5474#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5475 id < 0 &&
5476#endif
5477 lnum < sign->lnum)
5478 {
5479#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5480 /* XXX - GRP: Is this because of sign slide problem? Or is it
5481 * really needed? Or is it because we allow multiple signs per
5482 * line? If so, should I add that feature to FEAT_SIGNS?
5483 */
5484 while (prev != NULL && prev->lnum == lnum)
5485 prev = prev->prev;
5486 if (prev == NULL)
5487 sign = buf->b_signlist;
5488 else
5489 sign = prev->next;
5490#endif
5491 insert_sign(buf, prev, sign, id, lnum, typenr);
5492 return;
5493 }
5494 prev = sign;
5495 }
5496#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5497 /* XXX - GRP: See previous comment */
5498 while (prev != NULL && prev->lnum == lnum)
5499 prev = prev->prev;
5500 if (prev == NULL)
5501 sign = buf->b_signlist;
5502 else
5503 sign = prev->next;
5504#endif
5505 insert_sign(buf, prev, sign, id, lnum, typenr);
5506
5507 return;
5508}
5509
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005510 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511buf_change_sign_type(buf, markId, typenr)
5512 buf_T *buf; /* buffer to store sign in */
5513 int markId; /* sign ID */
5514 int typenr; /* typenr of sign we are adding */
5515{
5516 signlist_T *sign; /* a sign in the signlist */
5517
5518 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5519 {
5520 if (sign->id == markId)
5521 {
5522 sign->typenr = typenr;
5523 return sign->lnum;
5524 }
5525 }
5526
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005527 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528}
5529
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005530 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531buf_getsigntype(buf, lnum, type)
5532 buf_T *buf;
5533 linenr_T lnum;
5534 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5535{
5536 signlist_T *sign; /* a sign in a b_signlist */
5537
5538 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5539 if (sign->lnum == lnum
5540 && (type == SIGN_ANY
5541# ifdef FEAT_SIGN_ICONS
5542 || (type == SIGN_ICON
5543 && sign_get_image(sign->typenr) != NULL)
5544# endif
5545 || (type == SIGN_TEXT
5546 && sign_get_text(sign->typenr) != NULL)
5547 || (type == SIGN_LINEHL
5548 && sign_get_attr(sign->typenr, TRUE) != 0)))
5549 return sign->typenr;
5550 return 0;
5551}
5552
5553
5554 linenr_T
5555buf_delsign(buf, id)
5556 buf_T *buf; /* buffer sign is stored in */
5557 int id; /* sign id */
5558{
5559 signlist_T **lastp; /* pointer to pointer to current sign */
5560 signlist_T *sign; /* a sign in a b_signlist */
5561 signlist_T *next; /* the next sign in a b_signlist */
5562 linenr_T lnum; /* line number whose sign was deleted */
5563
5564 lastp = &buf->b_signlist;
5565 lnum = 0;
5566 for (sign = buf->b_signlist; sign != NULL; sign = next)
5567 {
5568 next = sign->next;
5569 if (sign->id == id)
5570 {
5571 *lastp = next;
5572#ifdef FEAT_NETBEANS_INTG
5573 if (next != NULL)
5574 next->prev = sign->prev;
5575#endif
5576 lnum = sign->lnum;
5577 vim_free(sign);
5578 break;
5579 }
5580 else
5581 lastp = &sign->next;
5582 }
5583
5584 /* When deleted the last sign need to redraw the windows to remove the
5585 * sign column. */
5586 if (buf->b_signlist == NULL)
5587 {
5588 redraw_buf_later(buf, NOT_VALID);
5589 changed_cline_bef_curs();
5590 }
5591
5592 return lnum;
5593}
5594
5595
5596/*
5597 * Find the line number of the sign with the requested id. If the sign does
5598 * not exist, return 0 as the line number. This will still let the correct file
5599 * get loaded.
5600 */
5601 int
5602buf_findsign(buf, id)
5603 buf_T *buf; /* buffer to store sign in */
5604 int id; /* sign ID */
5605{
5606 signlist_T *sign; /* a sign in the signlist */
5607
5608 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5609 if (sign->id == id)
5610 return sign->lnum;
5611
5612 return 0;
5613}
5614
5615 int
5616buf_findsign_id(buf, lnum)
5617 buf_T *buf; /* buffer whose sign we are searching for */
5618 linenr_T lnum; /* line number of sign */
5619{
5620 signlist_T *sign; /* a sign in the signlist */
5621
5622 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5623 if (sign->lnum == lnum)
5624 return sign->id;
5625
5626 return 0;
5627}
5628
5629
5630# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5631/* see if a given type of sign exists on a specific line */
5632 int
5633buf_findsigntype_id(buf, lnum, typenr)
5634 buf_T *buf; /* buffer whose sign we are searching for */
5635 linenr_T lnum; /* line number of sign */
5636 int typenr; /* sign type number */
5637{
5638 signlist_T *sign; /* a sign in the signlist */
5639
5640 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5641 if (sign->lnum == lnum && sign->typenr == typenr)
5642 return sign->id;
5643
5644 return 0;
5645}
5646
5647
5648# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5649/* return the number of icons on the given line */
5650 int
5651buf_signcount(buf, lnum)
5652 buf_T *buf;
5653 linenr_T lnum;
5654{
5655 signlist_T *sign; /* a sign in the signlist */
5656 int count = 0;
5657
5658 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5659 if (sign->lnum == lnum)
5660 if (sign_get_image(sign->typenr) != NULL)
5661 count++;
5662
5663 return count;
5664}
5665# endif /* FEAT_SIGN_ICONS */
5666# endif /* FEAT_NETBEANS_INTG */
5667
5668
5669/*
5670 * Delete signs in buffer "buf".
5671 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005672 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673buf_delete_signs(buf)
5674 buf_T *buf;
5675{
5676 signlist_T *next;
5677
5678 while (buf->b_signlist != NULL)
5679 {
5680 next = buf->b_signlist->next;
5681 vim_free(buf->b_signlist);
5682 buf->b_signlist = next;
5683 }
5684}
5685
5686/*
5687 * Delete all signs in all buffers.
5688 */
5689 void
5690buf_delete_all_signs()
5691{
5692 buf_T *buf; /* buffer we are checking for signs */
5693
5694 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5695 if (buf->b_signlist != NULL)
5696 {
5697 /* Need to redraw the windows to remove the sign column. */
5698 redraw_buf_later(buf, NOT_VALID);
5699 buf_delete_signs(buf);
5700 }
5701}
5702
5703/*
5704 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5705 */
5706 void
5707sign_list_placed(rbuf)
5708 buf_T *rbuf;
5709{
5710 buf_T *buf;
5711 signlist_T *p;
5712 char lbuf[BUFSIZ];
5713
5714 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5715 msg_putchar('\n');
5716 if (rbuf == NULL)
5717 buf = firstbuf;
5718 else
5719 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005720 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 if (buf->b_signlist != NULL)
5723 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005724 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5726 msg_putchar('\n');
5727 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005728 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005730 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5732 MSG_PUTS(lbuf);
5733 msg_putchar('\n');
5734 }
5735 if (rbuf != NULL)
5736 break;
5737 buf = buf->b_next;
5738 }
5739}
5740
5741/*
5742 * Adjust a placed sign for inserted/deleted lines.
5743 */
5744 void
5745sign_mark_adjust(line1, line2, amount, amount_after)
5746 linenr_T line1;
5747 linenr_T line2;
5748 long amount;
5749 long amount_after;
5750{
5751 signlist_T *sign; /* a sign in a b_signlist */
5752
5753 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5754 {
5755 if (sign->lnum >= line1 && sign->lnum <= line2)
5756 {
5757 if (amount == MAXLNUM)
5758 sign->lnum = line1;
5759 else
5760 sign->lnum += amount;
5761 }
5762 else if (sign->lnum > line2)
5763 sign->lnum += amount_after;
5764 }
5765}
5766#endif /* FEAT_SIGNS */
5767
5768/*
5769 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5770 */
5771 void
5772set_buflisted(on)
5773 int on;
5774{
5775 if (on != curbuf->b_p_bl)
5776 {
5777 curbuf->b_p_bl = on;
5778#ifdef FEAT_AUTOCMD
5779 if (on)
5780 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5781 else
5782 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5783#endif
5784 }
5785}
5786
5787/*
5788 * Read the file for "buf" again and check if the contents changed.
5789 * Return TRUE if it changed or this could not be checked.
5790 */
5791 int
5792buf_contents_changed(buf)
5793 buf_T *buf;
5794{
5795 buf_T *newbuf;
5796 int differ = TRUE;
5797 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 exarg_T ea;
5800
5801 /* Allocate a buffer without putting it in the buffer list. */
5802 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5803 if (newbuf == NULL)
5804 return TRUE;
5805
5806 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5807 if (prep_exarg(&ea, buf) == FAIL)
5808 {
5809 wipe_buffer(newbuf, FALSE);
5810 return TRUE;
5811 }
5812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 /* set curwin/curbuf to buf and save a few things */
5814 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815
Bram Moolenaar4770d092006-01-12 23:22:24 +00005816 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 && readfile(buf->b_ffname, buf->b_fname,
5818 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5819 &ea, READ_NEW | READ_DUMMY) == OK)
5820 {
5821 /* compare the two files line by line */
5822 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5823 {
5824 differ = FALSE;
5825 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5826 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5827 {
5828 differ = TRUE;
5829 break;
5830 }
5831 }
5832 }
5833 vim_free(ea.cmd);
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 /* restore curwin/curbuf and a few other things */
5836 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
5838 if (curbuf != newbuf) /* safety check */
5839 wipe_buffer(newbuf, FALSE);
5840
5841 return differ;
5842}
5843
5844/*
5845 * Wipe out a buffer and decrement the last buffer number if it was used for
5846 * this buffer. Call this to wipe out a temp buffer that does not contain any
5847 * marks.
5848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 void
5850wipe_buffer(buf, aucmd)
5851 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005852 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853{
5854 if (buf->b_fnum == top_file_num - 1)
5855 --top_file_num;
5856
5857#ifdef FEAT_AUTOCMD
5858 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005859 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005861 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862#ifdef FEAT_AUTOCMD
5863 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005864 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865#endif
5866}