blob: 8973b42aaedcd59fd22553a1ce8b7b714df71fbc [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 Moolenaar6bcbcc52013-11-05 07:13:41 +0100216 parse_cino(curbuf);
217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219 /*
220 * Set/reset the Changed flag first, autocmds may change the buffer.
221 * Apply the automatic commands, before processing the modelines.
222 * So the modelines have priority over auto commands.
223 */
224 /* When reading stdin, the buffer contents always needs writing, so set
225 * the changed flag. Unless in readonly mode: "ls | gview -".
226 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000227 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#ifdef FEAT_AUTOCMD
229 || modified_was_set /* ":set modified" used in autocmd */
230# ifdef FEAT_EVAL
231 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
232# endif
233#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000234 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000236 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 unchanged(curbuf, FALSE);
238 save_file_ff(curbuf); /* keep this fileformat */
239
240 /* require "!" to overwrite the file, because it wasn't read completely */
241#ifdef FEAT_EVAL
242 if (aborting())
243#else
244 if (got_int)
245#endif
246 curbuf->b_flags |= BF_READERR;
247
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000248#ifdef FEAT_FOLDING
249 /* Need to update automatic folding. Do this before the autocommands,
250 * they may use the fold info. */
251 foldUpdateAll(curwin);
252#endif
253
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#ifdef FEAT_AUTOCMD
255 /* need to set w_topline, unless some autocommand already did that. */
256 if (!(curwin->w_valid & VALID_TOPLINE))
257 {
258 curwin->w_topline = 1;
259# ifdef FEAT_DIFF
260 curwin->w_topfill = 0;
261# endif
262 }
263# ifdef FEAT_EVAL
264 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
265# else
266 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
267# endif
268#endif
269
270 if (retval != FAIL)
271 {
272#ifdef FEAT_AUTOCMD
273 /*
274 * The autocommands may have changed the current buffer. Apply the
275 * modelines to the correct buffer, if it still exists and is loaded.
276 */
277 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
278 {
279 aco_save_T aco;
280
281 /* Go to the buffer that was opened. */
282 aucmd_prepbuf(&aco, old_curbuf);
283#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000284 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
286
287#ifdef FEAT_AUTOCMD
288# ifdef FEAT_EVAL
289 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
290 &retval);
291# else
292 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
293# endif
294
295 /* restore curwin/curbuf and a few other things */
296 aucmd_restbuf(&aco);
297 }
298#endif
299 }
300
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 return retval;
302}
303
304/*
305 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
306 */
307 int
308buf_valid(buf)
309 buf_T *buf;
310{
311 buf_T *bp;
312
313 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
314 if (bp == buf)
315 return TRUE;
316 return FALSE;
317}
318
319/*
320 * Close the link to a buffer.
321 * "action" is used when there is no longer a window for the buffer.
322 * It can be:
323 * 0 buffer becomes hidden
324 * DOBUF_UNLOAD buffer is unloaded
325 * DOBUF_DELETE buffer is unloaded and removed from buffer list
326 * DOBUF_WIPE buffer is unloaded and really deleted
327 * When doing all but the first one on the current buffer, the caller should
328 * get a new buffer very soon!
329 *
330 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100331 *
332 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
333 * cause there to be only one window with this buffer. e.g. when ":quit" is
334 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 */
336 void
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100337close_buffer(win, buf, action, abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 win_T *win; /* if not NULL, set b_last_cursor */
339 buf_T *buf;
340 int action;
Bram Moolenaar5fbe6992012-03-07 22:52:36 +0100341 int abort_if_last UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342{
343#ifdef FEAT_AUTOCMD
344 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100345 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#endif
347 int unload_buf = (action != 0);
348 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
349 int wipe_buf = (action == DOBUF_WIPE);
350
351#ifdef FEAT_QUICKFIX
352 /*
353 * Force unloading or deleting when 'bufhidden' says so.
354 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
355 * "hide" (otherwise we could never free or delete a buffer).
356 */
357 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
358 {
359 del_buf = TRUE;
360 unload_buf = TRUE;
361 }
362 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
363 {
364 del_buf = TRUE;
365 unload_buf = TRUE;
366 wipe_buf = TRUE;
367 }
368 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
369 unload_buf = TRUE;
370#endif
371
372 if (win != NULL)
373 {
374 /* Set b_last_cursor when closing the last window for the buffer.
375 * Remember the last cursor position and window options of the buffer.
376 * This used to be only for the current window, but then options like
377 * 'foldmethod' may be lost with a ":only" command. */
378 if (buf->b_nwindows == 1)
379 set_last_cursor(win);
380 buflist_setfpos(buf, win,
381 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
382 win->w_cursor.col, TRUE);
383 }
384
385#ifdef FEAT_AUTOCMD
386 /* When the buffer is no longer in a window, trigger BufWinLeave */
387 if (buf->b_nwindows == 1)
388 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200389 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
391 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200392 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100393 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200394 /* Autocommands deleted the buffer. */
395aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100396 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100398 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200399 buf->b_closing = FALSE;
400 if (abort_if_last && one_window())
401 /* Autocommands made this the only window. */
402 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
404 /* When the buffer becomes hidden, but is not unloaded, trigger
405 * BufHidden */
406 if (!unload_buf)
407 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200408 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
410 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200411 if (!buf_valid(buf))
412 /* Autocommands deleted the buffer. */
413 goto aucmd_abort;
414 buf->b_closing = FALSE;
415 if (abort_if_last && one_window())
416 /* Autocommands made this the only window. */
417 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 }
419# ifdef FEAT_EVAL
420 if (aborting()) /* autocmds may abort script processing */
421 return;
422# endif
423 }
424 nwindows = buf->b_nwindows;
425#endif
426
427 /* decrease the link count from windows (unless not in any window) */
428 if (buf->b_nwindows > 0)
429 --buf->b_nwindows;
430
431 /* Return when a window is displaying the buffer or when it's not
432 * unloaded. */
433 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435
436 /* Always remove the buffer when there is no file name. */
437 if (buf->b_ffname == NULL)
438 del_buf = TRUE;
439
440 /*
441 * Free all things allocated for this buffer.
442 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
443 */
444#ifdef FEAT_AUTOCMD
445 /* Remember if we are closing the current buffer. Restore the number of
446 * windows, so that autocommands in buf_freeall() don't get confused. */
447 is_curbuf = (buf == curbuf);
448 buf->b_nwindows = nwindows;
449#endif
450
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200451 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200452 if (
453#ifdef FEAT_WINDOWS
454 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100455#else
456 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200457#endif
458 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200459 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
461#ifdef FEAT_AUTOCMD
462 /* Autocommands may have deleted the buffer. */
463 if (!buf_valid(buf))
464 return;
465# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000466 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 return;
468# endif
469
470 /* Autocommands may have opened or closed windows for this buffer.
471 * Decrement the count for the close we do here. */
472 if (buf->b_nwindows > 0)
473 --buf->b_nwindows;
474
475 /*
476 * It's possible that autocommands change curbuf to the one being deleted.
477 * This might cause the previous curbuf to be deleted unexpectedly. But
478 * in some cases it's OK to delete the curbuf, because a new one is
479 * obtained anyway. Therefore only return if curbuf changed to the
480 * deleted buffer.
481 */
482 if (buf == curbuf && !is_curbuf)
483 return;
484#endif
485
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000486 /* Change directories when the 'acd' option is set. */
487 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
489 /*
490 * Remove the buffer from the list.
491 */
492 if (wipe_buf)
493 {
494#ifdef FEAT_SUN_WORKSHOP
495 if (usingSunWorkShop)
496 workshop_file_closed_lineno((char *)buf->b_ffname,
497 (int)buf->b_last_cursor.lnum);
498#endif
499 vim_free(buf->b_ffname);
500 vim_free(buf->b_sfname);
501 if (buf->b_prev == NULL)
502 firstbuf = buf->b_next;
503 else
504 buf->b_prev->b_next = buf->b_next;
505 if (buf->b_next == NULL)
506 lastbuf = buf->b_prev;
507 else
508 buf->b_next->b_prev = buf->b_prev;
509 free_buffer(buf);
510 }
511 else
512 {
513 if (del_buf)
514 {
515 /* Free all internal variables and reset option values, to make
516 * ":bdel" compatible with Vim 5.7. */
517 free_buffer_stuff(buf, TRUE);
518
519 /* Make it look like a new buffer. */
520 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
521
522 /* Init the options when loaded again. */
523 buf->b_p_initialized = FALSE;
524 }
525 buf_clear_file(buf);
526 if (del_buf)
527 buf->b_p_bl = FALSE;
528 }
529}
530
531/*
532 * Make buffer not contain a file.
533 */
534 void
535buf_clear_file(buf)
536 buf_T *buf;
537{
538 buf->b_ml.ml_line_count = 1;
539 unchanged(buf, TRUE);
540#ifndef SHORT_FNAME
541 buf->b_shortname = FALSE;
542#endif
543 buf->b_p_eol = TRUE;
544 buf->b_start_eol = TRUE;
545#ifdef FEAT_MBYTE
546 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000547 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548#endif
549 buf->b_ml.ml_mfp = NULL;
550 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
551#ifdef FEAT_NETBEANS_INTG
552 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#endif
554}
555
556/*
557 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200558 * the file. flags:
559 * BFA_DEL buffer is going to be deleted
560 * BFA_WIPE buffer is going to be wiped out
561 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200564buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200566 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
568#ifdef FEAT_AUTOCMD
569 int is_curbuf = (buf == curbuf);
570
Bram Moolenaar362ce482012-06-06 19:02:45 +0200571 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
573 if (!buf_valid(buf)) /* autocommands may delete the buffer */
574 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200575 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {
577 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
578 if (!buf_valid(buf)) /* autocommands may delete the buffer */
579 return;
580 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200581 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {
583 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
584 FALSE, buf);
585 if (!buf_valid(buf)) /* autocommands may delete the buffer */
586 return;
587 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200588 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000590 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 return;
592# endif
593
594 /*
595 * It's possible that autocommands change curbuf to the one being deleted.
596 * This might cause curbuf to be deleted unexpectedly. But in some cases
597 * it's OK to delete the curbuf, because a new one is obtained anyway.
598 * Therefore only return if curbuf changed to the deleted buffer.
599 */
600 if (buf == curbuf && !is_curbuf)
601 return;
602#endif
603#ifdef FEAT_DIFF
604 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
605#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200606#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100607 /* Remove any ownsyntax, unless exiting. */
608 if (firstwin != NULL && curwin->w_buffer == buf)
609 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200610#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000611
612#ifdef FEAT_FOLDING
613 /* No folds in an empty buffer. */
614# ifdef FEAT_WINDOWS
615 {
616 win_T *win;
617 tabpage_T *tp;
618
619 FOR_ALL_TAB_WINDOWS(tp, win)
620 if (win->w_buffer == buf)
621 clearFolding(win);
622 }
623# else
624 if (curwin->w_buffer == buf)
625 clearFolding(curwin);
626# endif
627#endif
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#ifdef FEAT_TCL
630 tcl_buffer_free(buf);
631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ml_close(buf, TRUE); /* close and delete the memline/memfile */
633 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200634 if ((flags & BFA_KEEP_UNDO) == 0)
635 {
636 u_blockfree(buf); /* free the memory allocated for undo */
637 u_clearall(buf); /* reset all undo information */
638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000642 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643}
644
645/*
646 * Free a buffer structure and the things it contains related to the buffer
647 * itself (not the file, that must have been done already).
648 */
649 static void
650free_buffer(buf)
651 buf_T *buf;
652{
653 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200654#ifdef FEAT_EVAL
655 unref_var_dict(buf->b_vars);
656#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200657#ifdef FEAT_LUA
658 lua_buffer_free(buf);
659#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000660#ifdef FEAT_MZSCHEME
661 mzscheme_buffer_free(buf);
662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663#ifdef FEAT_PERL
664 perl_buf_free(buf);
665#endif
666#ifdef FEAT_PYTHON
667 python_buffer_free(buf);
668#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200669#ifdef FEAT_PYTHON3
670 python3_buffer_free(buf);
671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef FEAT_RUBY
673 ruby_buffer_free(buf);
674#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000675#ifdef FEAT_AUTOCMD
676 aubuflocal_remove(buf);
677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 vim_free(buf);
679}
680
681/*
682 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
683 */
684 static void
685free_buffer_stuff(buf, free_options)
686 buf_T *buf;
687 int free_options; /* free options as well */
688{
689 if (free_options)
690 {
691 clear_wininfo(buf); /* including window-local options */
692 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200693#ifdef FEAT_SPELL
694 ga_clear(&buf->b_s.b_langp);
695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200698 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
699 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700#endif
701#ifdef FEAT_USR_CMDS
702 uc_clear(&buf->b_ucmds); /* clear local user commands */
703#endif
704#ifdef FEAT_SIGNS
705 buf_delete_signs(buf); /* delete any signs */
706#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000707#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200708 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef FEAT_LOCALMAP
711 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
712 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
713#endif
714#ifdef FEAT_MBYTE
715 vim_free(buf->b_start_fenc);
716 buf->b_start_fenc = NULL;
717#endif
718}
719
720/*
721 * Free the b_wininfo list for buffer "buf".
722 */
723 static void
724clear_wininfo(buf)
725 buf_T *buf;
726{
727 wininfo_T *wip;
728
729 while (buf->b_wininfo != NULL)
730 {
731 wip = buf->b_wininfo;
732 buf->b_wininfo = wip->wi_next;
733 if (wip->wi_optset)
734 {
735 clear_winopt(&wip->wi_opt);
736#ifdef FEAT_FOLDING
737 deleteFoldRecurse(&wip->wi_folds);
738#endif
739 }
740 vim_free(wip);
741 }
742}
743
744#if defined(FEAT_LISTCMDS) || defined(PROTO)
745/*
746 * Go to another buffer. Handles the result of the ATTENTION dialog.
747 */
748 void
749goto_buffer(eap, start, dir, count)
750 exarg_T *eap;
751 int start;
752 int dir;
753 int count;
754{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000755# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 buf_T *old_curbuf = curbuf;
757
758 swap_exists_action = SEA_DIALOG;
759# endif
760 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
761 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000762# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
764 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000765# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
766 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000767
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000768 /* Reset the error/interrupt/exception state here so that
769 * aborting() returns FALSE when closing a window. */
770 enter_cleanup(&cs);
771# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000772
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000773 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 win_close(curwin, TRUE);
775 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000776 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000777
778# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
779 /* Restore the error/interrupt/exception state if not discarded by a
780 * new aborting error, interrupt, or uncaught exception. */
781 leave_cleanup(&cs);
782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 else
785 handle_swap_exists(old_curbuf);
786# endif
787}
788#endif
789
Bram Moolenaare64ac772005-12-07 20:54:59 +0000790#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791/*
792 * Handle the situation of swap_exists_action being set.
793 * It is allowed for "old_curbuf" to be NULL or invalid.
794 */
795 void
796handle_swap_exists(old_curbuf)
797 buf_T *old_curbuf;
798{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000799# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
800 cleanup_T cs;
801# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100802#ifdef FEAT_SYN_HL
803 long old_tw = curbuf->b_p_tw;
804#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (swap_exists_action == SEA_QUIT)
807 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000808# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
809 /* Reset the error/interrupt/exception state here so that
810 * aborting() returns FALSE when closing a buffer. */
811 enter_cleanup(&cs);
812# endif
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 /* User selected Quit at ATTENTION prompt. Go back to previous
815 * buffer. If that buffer is gone or the same as the current one,
816 * open a new, empty buffer. */
817 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000818 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100819 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000821 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100825#ifdef FEAT_SYN_HL
826 if (old_tw != curbuf->b_p_tw)
827 check_colorcolumn(curwin);
828#endif
829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000831
832# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
833 /* Restore the error/interrupt/exception state if not discarded by a
834 * new aborting error, interrupt, or uncaught exception. */
835 leave_cleanup(&cs);
836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838 else if (swap_exists_action == SEA_RECOVER)
839 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000840# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
841 /* Reset the error/interrupt/exception state here so that
842 * aborting() returns FALSE when closing a buffer. */
843 enter_cleanup(&cs);
844# endif
845
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 /* User selected Recover at ATTENTION prompt. */
847 msg_scroll = TRUE;
848 ml_recover();
849 MSG_PUTS("\n"); /* don't overwrite the last message */
850 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000851 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000852
853# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
854 /* Restore the error/interrupt/exception state if not discarded by a
855 * new aborting error, interrupt, or uncaught exception. */
856 leave_cleanup(&cs);
857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 swap_exists_action = SEA_NONE;
860}
861#endif
862
863#if defined(FEAT_LISTCMDS) || defined(PROTO)
864/*
865 * do_bufdel() - delete or unload buffer(s)
866 *
867 * addr_count == 0: ":bdel" - delete current buffer
868 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
869 * buffer "end_bnr", then any other arguments.
870 * addr_count == 2: ":N,N bdel" - delete buffers in range
871 *
872 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
873 * DOBUF_DEL (":bdel")
874 *
875 * Returns error message or NULL
876 */
877 char_u *
878do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
879 int command;
880 char_u *arg; /* pointer to extra arguments */
881 int addr_count;
882 int start_bnr; /* first buffer number in a range */
883 int end_bnr; /* buffer nr or last buffer nr in a range */
884 int forceit;
885{
886 int do_current = 0; /* delete current buffer? */
887 int deleted = 0; /* number of buffers deleted */
888 char_u *errormsg = NULL; /* return value */
889 int bnr; /* buffer number */
890 char_u *p;
891
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (addr_count == 0)
893 {
894 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
895 }
896 else
897 {
898 if (addr_count == 2)
899 {
900 if (*arg) /* both range and argument is not allowed */
901 return (char_u *)_(e_trailing);
902 bnr = start_bnr;
903 }
904 else /* addr_count == 1 */
905 bnr = end_bnr;
906
907 for ( ;!got_int; ui_breakcheck())
908 {
909 /*
910 * delete the current buffer last, otherwise when the
911 * current buffer is deleted, the next buffer becomes
912 * the current one and will be loaded, which may then
913 * also be deleted, etc.
914 */
915 if (bnr == curbuf->b_fnum)
916 do_current = bnr;
917 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
918 forceit) == OK)
919 ++deleted;
920
921 /*
922 * find next buffer number to delete/unload
923 */
924 if (addr_count == 2)
925 {
926 if (++bnr > end_bnr)
927 break;
928 }
929 else /* addr_count == 1 */
930 {
931 arg = skipwhite(arg);
932 if (*arg == NUL)
933 break;
934 if (!VIM_ISDIGIT(*arg))
935 {
936 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100937 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
938 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (bnr < 0) /* failed */
940 break;
941 arg = p;
942 }
943 else
944 bnr = getdigits(&arg);
945 }
946 }
947 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
948 FORWARD, do_current, forceit) == OK)
949 ++deleted;
950
951 if (deleted == 0)
952 {
953 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000954 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000956 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000958 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 errormsg = IObuff;
960 }
961 else if (deleted >= p_report)
962 {
963 if (command == DOBUF_UNLOAD)
964 {
965 if (deleted == 1)
966 MSG(_("1 buffer unloaded"));
967 else
968 smsg((char_u *)_("%d buffers unloaded"), deleted);
969 }
970 else if (command == DOBUF_DEL)
971 {
972 if (deleted == 1)
973 MSG(_("1 buffer deleted"));
974 else
975 smsg((char_u *)_("%d buffers deleted"), deleted);
976 }
977 else
978 {
979 if (deleted == 1)
980 MSG(_("1 buffer wiped out"));
981 else
982 smsg((char_u *)_("%d buffers wiped out"), deleted);
983 }
984 }
985 }
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 return errormsg;
989}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200990#endif /* FEAT_LISTCMDS */
991
992#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
993 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995/*
996 * Implementation of the commands for the buffer list.
997 *
998 * action == DOBUF_GOTO go to specified buffer
999 * action == DOBUF_SPLIT split window and go to specified buffer
1000 * action == DOBUF_UNLOAD unload specified buffer(s)
1001 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1002 * action == DOBUF_WIPE delete specified buffer(s) really
1003 *
1004 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1005 * start == DOBUF_FIRST go to "count" buffer from first buffer
1006 * start == DOBUF_LAST go to "count" buffer from last buffer
1007 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1008 *
1009 * Return FAIL or OK.
1010 */
1011 int
1012do_buffer(action, start, dir, count, forceit)
1013 int action;
1014 int start;
1015 int dir; /* FORWARD or BACKWARD */
1016 int count; /* buffer number or number of buffers */
1017 int forceit; /* TRUE for :...! */
1018{
1019 buf_T *buf;
1020 buf_T *bp;
1021 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1022 || action == DOBUF_WIPE);
1023
1024 switch (start)
1025 {
1026 case DOBUF_FIRST: buf = firstbuf; break;
1027 case DOBUF_LAST: buf = lastbuf; break;
1028 default: buf = curbuf; break;
1029 }
1030 if (start == DOBUF_MOD) /* find next modified buffer */
1031 {
1032 while (count-- > 0)
1033 {
1034 do
1035 {
1036 buf = buf->b_next;
1037 if (buf == NULL)
1038 buf = firstbuf;
1039 }
1040 while (buf != curbuf && !bufIsChanged(buf));
1041 }
1042 if (!bufIsChanged(buf))
1043 {
1044 EMSG(_("E84: No modified buffer found"));
1045 return FAIL;
1046 }
1047 }
1048 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1049 {
1050 while (buf != NULL && buf->b_fnum != count)
1051 buf = buf->b_next;
1052 }
1053 else
1054 {
1055 bp = NULL;
1056 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1057 {
1058 /* remember the buffer where we start, we come back there when all
1059 * buffers are unlisted. */
1060 if (bp == NULL)
1061 bp = buf;
1062 if (dir == FORWARD)
1063 {
1064 buf = buf->b_next;
1065 if (buf == NULL)
1066 buf = firstbuf;
1067 }
1068 else
1069 {
1070 buf = buf->b_prev;
1071 if (buf == NULL)
1072 buf = lastbuf;
1073 }
1074 /* don't count unlisted buffers */
1075 if (unload || buf->b_p_bl)
1076 {
1077 --count;
1078 bp = NULL; /* use this buffer as new starting point */
1079 }
1080 if (bp == buf)
1081 {
1082 /* back where we started, didn't find anything. */
1083 EMSG(_("E85: There is no listed buffer"));
1084 return FAIL;
1085 }
1086 }
1087 }
1088
1089 if (buf == NULL) /* could not find it */
1090 {
1091 if (start == DOBUF_FIRST)
1092 {
1093 /* don't warn when deleting */
1094 if (!unload)
1095 EMSGN(_("E86: Buffer %ld does not exist"), count);
1096 }
1097 else if (dir == FORWARD)
1098 EMSG(_("E87: Cannot go beyond last buffer"));
1099 else
1100 EMSG(_("E88: Cannot go before first buffer"));
1101 return FAIL;
1102 }
1103
1104#ifdef FEAT_GUI
1105 need_mouse_correct = TRUE;
1106#endif
1107
1108#ifdef FEAT_LISTCMDS
1109 /*
1110 * delete buffer buf from memory and/or the list
1111 */
1112 if (unload)
1113 {
1114 int forward;
1115 int retval;
1116
1117 /* When unloading or deleting a buffer that's already unloaded and
1118 * unlisted: fail silently. */
1119 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1120 return FAIL;
1121
1122 if (!forceit && bufIsChanged(buf))
1123 {
1124#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1125 if ((p_confirm || cmdmod.confirm) && p_write)
1126 {
1127 dialog_changed(buf, FALSE);
1128# ifdef FEAT_AUTOCMD
1129 if (!buf_valid(buf))
1130 /* Autocommand deleted buffer, oops! It's not changed
1131 * now. */
1132 return FAIL;
1133# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001134 /* If it's still changed fail silently, the dialog already
1135 * mentioned why it fails. */
1136 if (bufIsChanged(buf))
1137 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140#endif
1141 {
1142 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1143 buf->b_fnum);
1144 return FAIL;
1145 }
1146 }
1147
1148 /*
1149 * If deleting the last (listed) buffer, make it empty.
1150 * The last (listed) buffer cannot be unloaded.
1151 */
1152 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1153 if (bp->b_p_bl && bp != buf)
1154 break;
1155 if (bp == NULL && buf == curbuf)
1156 {
1157 if (action == DOBUF_UNLOAD)
1158 {
1159 EMSG(_("E90: Cannot unload last buffer"));
1160 return FAIL;
1161 }
1162
1163 /* Close any other windows on this buffer, then make it empty. */
1164#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001165 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166#endif
1167 setpcmark();
1168 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001169 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
1171 /*
1172 * do_ecmd() may create a new buffer, then we have to delete
1173 * the old one. But do_ecmd() may have done that already, check
1174 * if the buffer still exists.
1175 */
1176 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001177 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 return retval;
1179 }
1180
1181#ifdef FEAT_WINDOWS
1182 /*
1183 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001184 * (unless it's the only window). Repeat this so long as we end up in
1185 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001187 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001188# ifdef FEAT_AUTOCMD
1189 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1190# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001191 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001192 {
1193 if (win_close(curwin, FALSE) == FAIL)
1194 break;
1195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196#endif
1197
1198 /*
1199 * If the buffer to be deleted is not the current one, delete it here.
1200 */
1201 if (buf != curbuf)
1202 {
1203#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001204 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#endif
1206 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001207 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 return OK;
1209 }
1210
1211 /*
1212 * Deleting the current buffer: Need to find another buffer to go to.
1213 * There must be another, otherwise it would have been handled above.
1214 * First use au_new_curbuf, if it is valid.
1215 * Then prefer the buffer we most recently visited.
1216 * Else try to find one that is loaded, after the current buffer,
1217 * then before the current buffer.
1218 * Finally use any buffer.
1219 */
1220 buf = NULL; /* selected buffer */
1221 bp = NULL; /* used when no loaded buffer found */
1222#ifdef FEAT_AUTOCMD
1223 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1224 buf = au_new_curbuf;
1225# ifdef FEAT_JUMPLIST
1226 else
1227# endif
1228#endif
1229#ifdef FEAT_JUMPLIST
1230 if (curwin->w_jumplistlen > 0)
1231 {
1232 int jumpidx;
1233
1234 jumpidx = curwin->w_jumplistidx - 1;
1235 if (jumpidx < 0)
1236 jumpidx = curwin->w_jumplistlen - 1;
1237
1238 forward = jumpidx;
1239 while (jumpidx != curwin->w_jumplistidx)
1240 {
1241 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1242 if (buf != NULL)
1243 {
1244 if (buf == curbuf || !buf->b_p_bl)
1245 buf = NULL; /* skip current and unlisted bufs */
1246 else if (buf->b_ml.ml_mfp == NULL)
1247 {
1248 /* skip unloaded buf, but may keep it for later */
1249 if (bp == NULL)
1250 bp = buf;
1251 buf = NULL;
1252 }
1253 }
1254 if (buf != NULL) /* found a valid buffer: stop searching */
1255 break;
1256 /* advance to older entry in jump list */
1257 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1258 break;
1259 if (--jumpidx < 0)
1260 jumpidx = curwin->w_jumplistlen - 1;
1261 if (jumpidx == forward) /* List exhausted for sure */
1262 break;
1263 }
1264 }
1265#endif
1266
1267 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1268 {
1269 forward = TRUE;
1270 buf = curbuf->b_next;
1271 for (;;)
1272 {
1273 if (buf == NULL)
1274 {
1275 if (!forward) /* tried both directions */
1276 break;
1277 buf = curbuf->b_prev;
1278 forward = FALSE;
1279 continue;
1280 }
1281 /* in non-help buffer, try to skip help buffers, and vv */
1282 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1283 {
1284 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1285 break;
1286 if (bp == NULL) /* remember unloaded buf for later */
1287 bp = buf;
1288 }
1289 if (forward)
1290 buf = buf->b_next;
1291 else
1292 buf = buf->b_prev;
1293 }
1294 }
1295 if (buf == NULL) /* No loaded buffer, use unloaded one */
1296 buf = bp;
1297 if (buf == NULL) /* No loaded buffer, find listed one */
1298 {
1299 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1300 if (buf->b_p_bl && buf != curbuf)
1301 break;
1302 }
1303 if (buf == NULL) /* Still no buffer, just take one */
1304 {
1305 if (curbuf->b_next != NULL)
1306 buf = curbuf->b_next;
1307 else
1308 buf = curbuf->b_prev;
1309 }
1310 }
1311
1312 /*
1313 * make buf current buffer
1314 */
1315 if (action == DOBUF_SPLIT) /* split window first */
1316 {
1317# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001318 /* If 'switchbuf' contains "useopen": jump to first window containing
1319 * "buf" if one exists */
1320 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001321 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001322 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001323 * page containing "buf" if one exists */
1324 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 return OK;
1326 if (win_split(0, 0) == FAIL)
1327# endif
1328 return FAIL;
1329 }
1330#endif
1331
1332 /* go to current buffer - nothing to do */
1333 if (buf == curbuf)
1334 return OK;
1335
1336 /*
1337 * Check if the current buffer may be abandoned.
1338 */
1339 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1340 {
1341#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1342 if ((p_confirm || cmdmod.confirm) && p_write)
1343 {
1344 dialog_changed(curbuf, FALSE);
1345# ifdef FEAT_AUTOCMD
1346 if (!buf_valid(buf))
1347 /* Autocommand deleted buffer, oops! */
1348 return FAIL;
1349# endif
1350 }
1351 if (bufIsChanged(curbuf))
1352#endif
1353 {
1354 EMSG(_(e_nowrtmsg));
1355 return FAIL;
1356 }
1357 }
1358
1359 /* Go to the other buffer. */
1360 set_curbuf(buf, action);
1361
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001362#if defined(FEAT_LISTCMDS) \
1363 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001365 {
1366 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368#endif
1369
1370#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1371 if (aborting()) /* autocmds may abort script processing */
1372 return FAIL;
1373#endif
1374
1375 return OK;
1376}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379/*
1380 * Set current buffer to "buf". Executes autocommands and closes current
1381 * buffer. "action" tells how to close the current buffer:
1382 * DOBUF_GOTO free or hide it
1383 * DOBUF_SPLIT nothing
1384 * DOBUF_UNLOAD unload it
1385 * DOBUF_DEL delete it
1386 * DOBUF_WIPE wipe it out
1387 */
1388 void
1389set_curbuf(buf, action)
1390 buf_T *buf;
1391 int action;
1392{
1393 buf_T *prevbuf;
1394 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1395 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001396#ifdef FEAT_SYN_HL
1397 long old_tw = curbuf->b_p_tw;
1398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
1400 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001401 if (!cmdmod.keepalt)
1402 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001403 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
1405#ifdef FEAT_VISUAL
1406 /* Don't restart Select mode after switching to another buffer. */
1407 VIsual_reselect = FALSE;
1408#endif
1409
1410 /* close_windows() or apply_autocmds() may change curbuf */
1411 prevbuf = curbuf;
1412
1413#ifdef FEAT_AUTOCMD
1414 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1415# ifdef FEAT_EVAL
1416 if (buf_valid(prevbuf) && !aborting())
1417# else
1418 if (buf_valid(prevbuf))
1419# endif
1420#endif
1421 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001422#ifdef FEAT_SYN_HL
1423 if (prevbuf == curwin->w_buffer)
1424 reset_synblock(curwin);
1425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426#ifdef FEAT_WINDOWS
1427 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001428 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429#endif
1430#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1431 if (buf_valid(prevbuf) && !aborting())
1432#else
1433 if (buf_valid(prevbuf))
1434#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001435 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001436#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001437 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001438#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001439 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001440 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1442 unload ? action : (action == DOBUF_GOTO
1443 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001444 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001445#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001446 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001447 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001448 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001449#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001453 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001454 * it did ":bunload") or aborted the script processing!
1455 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1456 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001457# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001458 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001459# endif
1460# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001461 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001462# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001463 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001467#ifdef FEAT_SYN_HL
1468 if (old_tw != curbuf->b_p_tw)
1469 check_colorcolumn(curwin);
1470#endif
1471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472}
1473
1474/*
1475 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001476 * Old curbuf must have been abandoned already! This also means "curbuf" may
1477 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 */
1479 void
1480enter_buffer(buf)
1481 buf_T *buf;
1482{
1483 /* Copy buffer and window local option values. Not for a help buffer. */
1484 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1485 if (!buf->b_help)
1486 get_winopts(buf);
1487#ifdef FEAT_FOLDING
1488 else
1489 /* Remove all folds in the window. */
1490 clearFolding(curwin);
1491 foldUpdateAll(curwin); /* update folds (later). */
1492#endif
1493
1494 /* Get the buffer in the current window. */
1495 curwin->w_buffer = buf;
1496 curbuf = buf;
1497 ++curbuf->b_nwindows;
1498
1499#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001500 if (curwin->w_p_diff)
1501 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502#endif
1503
Bram Moolenaara971b822011-09-14 14:43:25 +02001504#ifdef FEAT_SYN_HL
1505 curwin->w_s = &(buf->b_s);
1506#endif
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 /* Cursor on first line by default. */
1509 curwin->w_cursor.lnum = 1;
1510 curwin->w_cursor.col = 0;
1511#ifdef FEAT_VIRTUALEDIT
1512 curwin->w_cursor.coladd = 0;
1513#endif
1514 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001515#ifdef FEAT_AUTOCMD
1516 curwin->w_topline_was_set = FALSE;
1517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001519 /* mark cursor position as being invalid */
1520 curwin->w_valid = 0;
1521
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 /* Make sure the buffer is loaded. */
1523 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001524 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001525#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001526 /* If there is no filetype, allow for detecting one. Esp. useful for
1527 * ":ball" used in a autocommand. If there already is a filetype we
1528 * might prefer to keep it. */
1529 if (*curbuf->b_p_ft == NUL)
1530 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001531#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001532
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001533 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 else
1536 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001537 if (!msg_silent)
1538 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1540#ifdef FEAT_AUTOCMD
1541 curwin->w_topline = 1;
1542# ifdef FEAT_DIFF
1543 curwin->w_topfill = 0;
1544# endif
1545 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1546 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1547#endif
1548 }
1549
1550 /* If autocommands did not change the cursor position, restore cursor lnum
1551 * and possibly cursor col. */
1552 if (curwin->w_cursor.lnum == 1 && inindent(0))
1553 buflist_getfpos();
1554
1555 check_arg_idx(curwin); /* check for valid arg_idx */
1556#ifdef FEAT_TITLE
1557 maketitle();
1558#endif
1559#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001560 /* when autocmds didn't change it */
1561 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562#endif
1563 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1564
Bram Moolenaar009b2592004-10-24 19:18:58 +00001565#ifdef FEAT_NETBEANS_INTG
1566 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001567 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001568#endif
1569
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001570 /* Change directories when the 'acd' option is set. */
1571 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
1573#ifdef FEAT_KEYMAP
1574 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001575 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001577#ifdef FEAT_SPELL
1578 /* May need to set the spell language. Can only do this after the buffer
1579 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001580 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1581 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001582#endif
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 redraw_later(NOT_VALID);
1585}
1586
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001587#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1588/*
1589 * Change to the directory of the current buffer.
1590 */
1591 void
1592do_autochdir()
1593{
1594 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1595 shorten_fnames(TRUE);
1596}
1597#endif
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599/*
1600 * functions for dealing with the buffer list
1601 */
1602
1603/*
1604 * Add a file name to the buffer list. Return a pointer to the buffer.
1605 * If the same file name already exists return a pointer to that buffer.
1606 * If it does not exist, or if fname == NULL, a new entry is created.
1607 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1608 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1609 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 * This is the ONLY way to create a new buffer.
1611 */
1612static int top_file_num = 1; /* highest file number */
1613
1614 buf_T *
1615buflist_new(ffname, sfname, lnum, flags)
1616 char_u *ffname; /* full path of fname or relative */
1617 char_u *sfname; /* short fname or NULL */
1618 linenr_T lnum; /* preferred cursor line */
1619 int flags; /* BLN_ defines */
1620{
1621 buf_T *buf;
1622#ifdef UNIX
1623 struct stat st;
1624#endif
1625
1626 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1627
1628 /*
1629 * If file name already exists in the list, update the entry.
1630 */
1631#ifdef UNIX
1632 /* On Unix we can use inode numbers when the file exists. Works better
1633 * for hard links. */
1634 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1635 st.st_dev = (dev_T)-1;
1636#endif
1637 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1638#ifdef UNIX
1639 buflist_findname_stat(ffname, &st)
1640#else
1641 buflist_findname(ffname)
1642#endif
1643 ) != NULL)
1644 {
1645 vim_free(ffname);
1646 if (lnum != 0)
1647 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1648 /* copy the options now, if 'cpo' doesn't have 's' and not done
1649 * already */
1650 buf_copy_options(buf, 0);
1651 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1652 {
1653 buf->b_p_bl = TRUE;
1654#ifdef FEAT_AUTOCMD
1655 if (!(flags & BLN_DUMMY))
1656 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1657#endif
1658 }
1659 return buf;
1660 }
1661
1662 /*
1663 * If the current buffer has no name and no contents, use the current
1664 * buffer. Otherwise: Need to allocate a new buffer structure.
1665 *
1666 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001667 * (A spell file buffer is allocated in spell.c, but that's not a normal
1668 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 */
1670 buf = NULL;
1671 if ((flags & BLN_CURBUF)
1672 && curbuf != NULL
1673 && curbuf->b_ffname == NULL
1674 && curbuf->b_nwindows <= 1
1675 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1676 {
1677 buf = curbuf;
1678#ifdef FEAT_AUTOCMD
1679 /* It's like this buffer is deleted. Watch out for autocommands that
1680 * change curbuf! If that happens, allocate a new buffer anyway. */
1681 if (curbuf->b_p_bl)
1682 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1683 if (buf == curbuf)
1684 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1685# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001686 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 return NULL;
1688# endif
1689#endif
1690#ifdef FEAT_QUICKFIX
1691# ifdef FEAT_AUTOCMD
1692 if (buf == curbuf)
1693# endif
1694 {
1695 /* Make sure 'bufhidden' and 'buftype' are empty */
1696 clear_string_option(&buf->b_p_bh);
1697 clear_string_option(&buf->b_p_bt);
1698 }
1699#endif
1700 }
1701 if (buf != curbuf || curbuf == NULL)
1702 {
1703 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1704 if (buf == NULL)
1705 {
1706 vim_free(ffname);
1707 return NULL;
1708 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001709#ifdef FEAT_EVAL
1710 /* init b: variables */
1711 buf->b_vars = dict_alloc();
1712 if (buf->b_vars == NULL)
1713 {
1714 vim_free(ffname);
1715 vim_free(buf);
1716 return NULL;
1717 }
1718 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
1721
1722 if (ffname != NULL)
1723 {
1724 buf->b_ffname = ffname;
1725 buf->b_sfname = vim_strsave(sfname);
1726 }
1727
1728 clear_wininfo(buf);
1729 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1730
1731 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1732 || buf->b_wininfo == NULL)
1733 {
1734 vim_free(buf->b_ffname);
1735 buf->b_ffname = NULL;
1736 vim_free(buf->b_sfname);
1737 buf->b_sfname = NULL;
1738 if (buf != curbuf)
1739 free_buffer(buf);
1740 return NULL;
1741 }
1742
1743 if (buf == curbuf)
1744 {
1745 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001746 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (buf != curbuf) /* autocommands deleted the buffer! */
1748 return NULL;
1749#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001750 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 return NULL;
1752#endif
1753 /* buf->b_nwindows = 0; why was this here? */
1754 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001755
1756 /* Init the options. */
1757 buf->b_p_initialized = FALSE;
1758 buf_copy_options(buf, BCO_ENTER);
1759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760#ifdef FEAT_KEYMAP
1761 /* need to reload lmaps and set b:keymap_name */
1762 curbuf->b_kmap_state |= KEYMAP_INIT;
1763#endif
1764 }
1765 else
1766 {
1767 /*
1768 * put new buffer at the end of the buffer list
1769 */
1770 buf->b_next = NULL;
1771 if (firstbuf == NULL) /* buffer list is empty */
1772 {
1773 buf->b_prev = NULL;
1774 firstbuf = buf;
1775 }
1776 else /* append new buffer at end of list */
1777 {
1778 lastbuf->b_next = buf;
1779 buf->b_prev = lastbuf;
1780 }
1781 lastbuf = buf;
1782
1783 buf->b_fnum = top_file_num++;
1784 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1785 {
1786 EMSG(_("W14: Warning: List of file names overflow"));
1787 if (emsg_silent == 0)
1788 {
1789 out_flush();
1790 ui_delay(3000L, TRUE); /* make sure it is noticed */
1791 }
1792 top_file_num = 1;
1793 }
1794
1795 /*
1796 * Always copy the options from the current buffer.
1797 */
1798 buf_copy_options(buf, BCO_ALWAYS);
1799 }
1800
1801 buf->b_wininfo->wi_fpos.lnum = lnum;
1802 buf->b_wininfo->wi_win = curwin;
1803
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001804#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001805 hash_init(&buf->b_s.b_keywtab);
1806 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808
1809 buf->b_fname = buf->b_sfname;
1810#ifdef UNIX
1811 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001812 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 else
1814 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001815 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 buf->b_dev = st.st_dev;
1817 buf->b_ino = st.st_ino;
1818 }
1819#endif
1820 buf->b_u_synced = TRUE;
1821 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001822 if (flags & BLN_DUMMY)
1823 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 buf_clear_file(buf);
1825 clrallmarks(buf); /* clear marks */
1826 fmarks_check_names(buf); /* check file marks for this file */
1827 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1828#ifdef FEAT_AUTOCMD
1829 if (!(flags & BLN_DUMMY))
1830 {
1831 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1832 if (flags & BLN_LISTED)
1833 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1834# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001835 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 return NULL;
1837# endif
1838 }
1839#endif
1840
1841 return buf;
1842}
1843
1844/*
1845 * Free the memory for the options of a buffer.
1846 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1847 * 'fileencoding'.
1848 */
1849 void
1850free_buf_options(buf, free_p_ff)
1851 buf_T *buf;
1852 int free_p_ff;
1853{
1854 if (free_p_ff)
1855 {
1856#ifdef FEAT_MBYTE
1857 clear_string_option(&buf->b_p_fenc);
1858#endif
1859 clear_string_option(&buf->b_p_ff);
1860#ifdef FEAT_QUICKFIX
1861 clear_string_option(&buf->b_p_bh);
1862 clear_string_option(&buf->b_p_bt);
1863#endif
1864 }
1865#ifdef FEAT_FIND_ID
1866 clear_string_option(&buf->b_p_def);
1867 clear_string_option(&buf->b_p_inc);
1868# ifdef FEAT_EVAL
1869 clear_string_option(&buf->b_p_inex);
1870# endif
1871#endif
1872#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1873 clear_string_option(&buf->b_p_inde);
1874 clear_string_option(&buf->b_p_indk);
1875#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001876#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1877 clear_string_option(&buf->b_p_bexpr);
1878#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001879#if defined(FEAT_CRYPT)
1880 clear_string_option(&buf->b_p_cm);
1881#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001882#if defined(FEAT_EVAL)
1883 clear_string_option(&buf->b_p_fex);
1884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885#ifdef FEAT_CRYPT
1886 clear_string_option(&buf->b_p_key);
1887#endif
1888 clear_string_option(&buf->b_p_kp);
1889 clear_string_option(&buf->b_p_mps);
1890 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001891 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 clear_string_option(&buf->b_p_isk);
1893#ifdef FEAT_KEYMAP
1894 clear_string_option(&buf->b_p_keymap);
1895 ga_clear(&buf->b_kmap_ga);
1896#endif
1897#ifdef FEAT_COMMENTS
1898 clear_string_option(&buf->b_p_com);
1899#endif
1900#ifdef FEAT_FOLDING
1901 clear_string_option(&buf->b_p_cms);
1902#endif
1903 clear_string_option(&buf->b_p_nf);
1904#ifdef FEAT_SYN_HL
1905 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001906#endif
1907#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001908 clear_string_option(&buf->b_s.b_p_spc);
1909 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001910 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001911 buf->b_s.b_cap_prog = NULL;
1912 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913#endif
1914#ifdef FEAT_SEARCHPATH
1915 clear_string_option(&buf->b_p_sua);
1916#endif
1917#ifdef FEAT_AUTOCMD
1918 clear_string_option(&buf->b_p_ft);
1919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920#ifdef FEAT_CINDENT
1921 clear_string_option(&buf->b_p_cink);
1922 clear_string_option(&buf->b_p_cino);
1923#endif
1924#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1925 clear_string_option(&buf->b_p_cinw);
1926#endif
1927#ifdef FEAT_INS_EXPAND
1928 clear_string_option(&buf->b_p_cpt);
1929#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001930#ifdef FEAT_COMPL_FUNC
1931 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001932 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934#ifdef FEAT_QUICKFIX
1935 clear_string_option(&buf->b_p_gp);
1936 clear_string_option(&buf->b_p_mp);
1937 clear_string_option(&buf->b_p_efm);
1938#endif
1939 clear_string_option(&buf->b_p_ep);
1940 clear_string_option(&buf->b_p_path);
1941 clear_string_option(&buf->b_p_tags);
1942#ifdef FEAT_INS_EXPAND
1943 clear_string_option(&buf->b_p_dict);
1944 clear_string_option(&buf->b_p_tsr);
1945#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001946#ifdef FEAT_TEXTOBJ
1947 clear_string_option(&buf->b_p_qe);
1948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 buf->b_p_ar = -1;
1950}
1951
1952/*
1953 * get alternate file n
1954 * set linenr to lnum or altfpos.lnum if lnum == 0
1955 * also set cursor column to altfpos.col if 'startofline' is not set.
1956 * if (options & GETF_SETMARK) call setpcmark()
1957 * if (options & GETF_ALT) we are jumping to an alternate file.
1958 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1959 *
1960 * return FAIL for failure, OK for success
1961 */
1962 int
1963buflist_getfile(n, lnum, options, forceit)
1964 int n;
1965 linenr_T lnum;
1966 int options;
1967 int forceit;
1968{
1969 buf_T *buf;
1970#ifdef FEAT_WINDOWS
1971 win_T *wp = NULL;
1972#endif
1973 pos_T *fpos;
1974 colnr_T col;
1975
1976 buf = buflist_findnr(n);
1977 if (buf == NULL)
1978 {
1979 if ((options & GETF_ALT) && n == 0)
1980 EMSG(_(e_noalt));
1981 else
1982 EMSGN(_("E92: Buffer %ld not found"), n);
1983 return FAIL;
1984 }
1985
1986 /* if alternate file is the current buffer, nothing to do */
1987 if (buf == curbuf)
1988 return OK;
1989
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001990 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001991 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001992 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001994 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001995#ifdef FEAT_AUTOCMD
1996 if (curbuf_locked())
1997 return FAIL;
1998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 /* altfpos may be changed by getfile(), get it now */
2001 if (lnum == 0)
2002 {
2003 fpos = buflist_findfpos(buf);
2004 lnum = fpos->lnum;
2005 col = fpos->col;
2006 }
2007 else
2008 col = 0;
2009
2010#ifdef FEAT_WINDOWS
2011 if (options & GETF_SWITCH)
2012 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002013 /* If 'switchbuf' contains "useopen": jump to first window containing
2014 * "buf" if one exists */
2015 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 wp = buf_jump_open_win(buf);
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002017 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002018 * page containing "buf" if one exists */
2019 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002020 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00002021 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
2022 * isn't empty: open new window */
2023 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002025 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
2026 tabpage_new();
2027 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002029 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
2031 }
2032#endif
2033
2034 ++RedrawingDisabled;
2035 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2036 lnum, forceit) <= 0)
2037 {
2038 --RedrawingDisabled;
2039
2040 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2041 if (!p_sol && col != 0)
2042 {
2043 curwin->w_cursor.col = col;
2044 check_cursor_col();
2045#ifdef FEAT_VIRTUALEDIT
2046 curwin->w_cursor.coladd = 0;
2047#endif
2048 curwin->w_set_curswant = TRUE;
2049 }
2050 return OK;
2051 }
2052 --RedrawingDisabled;
2053 return FAIL;
2054}
2055
2056/*
2057 * go to the last know line number for the current buffer
2058 */
2059 void
2060buflist_getfpos()
2061{
2062 pos_T *fpos;
2063
2064 fpos = buflist_findfpos(curbuf);
2065
2066 curwin->w_cursor.lnum = fpos->lnum;
2067 check_cursor_lnum();
2068
2069 if (p_sol)
2070 curwin->w_cursor.col = 0;
2071 else
2072 {
2073 curwin->w_cursor.col = fpos->col;
2074 check_cursor_col();
2075#ifdef FEAT_VIRTUALEDIT
2076 curwin->w_cursor.coladd = 0;
2077#endif
2078 curwin->w_set_curswant = TRUE;
2079 }
2080}
2081
Bram Moolenaar81695252004-12-29 20:58:21 +00002082#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2083/*
2084 * Find file in buffer list by name (it has to be for the current window).
2085 * Returns NULL if not found.
2086 */
2087 buf_T *
2088buflist_findname_exp(fname)
2089 char_u *fname;
2090{
2091 char_u *ffname;
2092 buf_T *buf = NULL;
2093
2094 /* First make the name into a full path name */
2095 ffname = FullName_save(fname,
2096#ifdef UNIX
2097 TRUE /* force expansion, get rid of symbolic links */
2098#else
2099 FALSE
2100#endif
2101 );
2102 if (ffname != NULL)
2103 {
2104 buf = buflist_findname(ffname);
2105 vim_free(ffname);
2106 }
2107 return buf;
2108}
2109#endif
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111/*
2112 * Find file in buffer list by name (it has to be for the current window).
2113 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002114 * Skips dummy buffers.
2115 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 */
2117 buf_T *
2118buflist_findname(ffname)
2119 char_u *ffname;
2120{
2121#ifdef UNIX
2122 struct stat st;
2123
2124 if (mch_stat((char *)ffname, &st) < 0)
2125 st.st_dev = (dev_T)-1;
2126 return buflist_findname_stat(ffname, &st);
2127}
2128
2129/*
2130 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2131 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002132 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 */
2134 static buf_T *
2135buflist_findname_stat(ffname, stp)
2136 char_u *ffname;
2137 struct stat *stp;
2138{
2139#endif
2140 buf_T *buf;
2141
2142 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002143 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144#ifdef UNIX
2145 , stp
2146#endif
2147 ))
2148 return buf;
2149 return NULL;
2150}
2151
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002152#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2153 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154/*
2155 * Find file in buffer list by a regexp pattern.
2156 * Return fnum of the found buffer.
2157 * Return < 0 for error.
2158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 int
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002160buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 char_u *pattern;
2162 char_u *pattern_end; /* pointer to first char after pattern */
2163 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002164 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002165 int curtab_only; /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 buf_T *buf;
2168 regprog_T *prog;
2169 int match = -1;
2170 int find_listed;
2171 char_u *pat;
2172 char_u *patend;
2173 int attempt;
2174 char_u *p;
2175 int toggledollar;
2176
2177 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2178 {
2179 if (*pattern == '%')
2180 match = curbuf->b_fnum;
2181 else
2182 match = curwin->w_alt_fnum;
2183#ifdef FEAT_DIFF
2184 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2185 match = -1;
2186#endif
2187 }
2188
2189 /*
2190 * Try four ways of matching a listed buffer:
2191 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002192 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 * attempt == 2: with '$' at end (only match at end)
2194 * attempt == 3: with '^' at start and '$' at end (only full match)
2195 * Repeat this for finding an unlisted buffer if there was no matching
2196 * listed buffer.
2197 */
2198 else
2199 {
2200 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2201 if (pat == NULL)
2202 return -1;
2203 patend = pat + STRLEN(pat) - 1;
2204 toggledollar = (patend > pat && *patend == '$');
2205
2206 /* First try finding a listed buffer. If not found and "unlisted"
2207 * is TRUE, try finding an unlisted buffer. */
2208 find_listed = TRUE;
2209 for (;;)
2210 {
2211 for (attempt = 0; attempt <= 3; ++attempt)
2212 {
2213 /* may add '^' and '$' */
2214 if (toggledollar)
2215 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2216 p = pat;
2217 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2218 ++p;
2219 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2220 if (prog == NULL)
2221 {
2222 vim_free(pat);
2223 return -1;
2224 }
2225
2226 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2227 if (buf->b_p_bl == find_listed
2228#ifdef FEAT_DIFF
2229 && (!diffmode || diff_mode_buf(buf))
2230#endif
2231 && buflist_match(prog, buf) != NULL)
2232 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002233 if (curtab_only)
2234 {
2235 /* Ignore the match if the buffer is not open in
2236 * the current tab. */
2237#ifdef FEAT_WINDOWS
2238 win_T *wp;
2239
2240 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2241 if (wp->w_buffer == buf)
2242 break;
2243 if (wp == NULL)
2244 continue;
2245#else
2246 if (curwin->w_buffer != buf)
2247 continue;
2248#endif
2249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (match >= 0) /* already found a match */
2251 {
2252 match = -2;
2253 break;
2254 }
2255 match = buf->b_fnum; /* remember first match */
2256 }
2257
Bram Moolenaar473de612013-06-08 18:19:48 +02002258 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (match >= 0) /* found one match */
2260 break;
2261 }
2262
2263 /* Only search for unlisted buffers if there was no match with
2264 * a listed buffer. */
2265 if (!unlisted || !find_listed || match != -1)
2266 break;
2267 find_listed = FALSE;
2268 }
2269
2270 vim_free(pat);
2271 }
2272
2273 if (match == -2)
2274 EMSG2(_("E93: More than one match for %s"), pattern);
2275 else if (match < 0)
2276 EMSG2(_("E94: No matching buffer for %s"), pattern);
2277 return match;
2278}
2279#endif
2280
2281#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2282
2283/*
2284 * Find all buffer names that match.
2285 * For command line expansion of ":buf" and ":sbuf".
2286 * Return OK if matches found, FAIL otherwise.
2287 */
2288 int
2289ExpandBufnames(pat, num_file, file, options)
2290 char_u *pat;
2291 int *num_file;
2292 char_u ***file;
2293 int options;
2294{
2295 int count = 0;
2296 buf_T *buf;
2297 int round;
2298 char_u *p;
2299 int attempt;
2300 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002301 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
2303 *num_file = 0; /* return values in case of FAIL */
2304 *file = NULL;
2305
Bram Moolenaar05159a02005-02-26 23:04:13 +00002306 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2307 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002309 patc = alloc((unsigned)STRLEN(pat) + 11);
2310 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002312 STRCPY(patc, "\\(^\\|[\\/]\\)");
2313 STRCPY(patc + 11, pat + 1);
2314 }
2315 else
2316 patc = pat;
2317
2318 /*
2319 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002320 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002321 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002322 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002323 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002324 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002325 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002326 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002327 if (prog == NULL)
2328 {
2329 if (patc != pat)
2330 vim_free(patc);
2331 return FAIL;
2332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
2334 /*
2335 * round == 1: Count the matches.
2336 * round == 2: Build the array to keep the matches.
2337 */
2338 for (round = 1; round <= 2; ++round)
2339 {
2340 count = 0;
2341 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2342 {
2343 if (!buf->b_p_bl) /* skip unlisted buffers */
2344 continue;
2345 p = buflist_match(prog, buf);
2346 if (p != NULL)
2347 {
2348 if (round == 1)
2349 ++count;
2350 else
2351 {
2352 if (options & WILD_HOME_REPLACE)
2353 p = home_replace_save(buf, p);
2354 else
2355 p = vim_strsave(p);
2356 (*file)[count++] = p;
2357 }
2358 }
2359 }
2360 if (count == 0) /* no match found, break here */
2361 break;
2362 if (round == 1)
2363 {
2364 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2365 if (*file == NULL)
2366 {
Bram Moolenaar473de612013-06-08 18:19:48 +02002367 vim_regfree(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002368 if (patc != pat)
2369 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 return FAIL;
2371 }
2372 }
2373 }
Bram Moolenaar473de612013-06-08 18:19:48 +02002374 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 if (count) /* match(es) found, break here */
2376 break;
2377 }
2378
Bram Moolenaar05159a02005-02-26 23:04:13 +00002379 if (patc != pat)
2380 vim_free(patc);
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 *num_file = count;
2383 return (count == 0 ? FAIL : OK);
2384}
2385
2386#endif /* FEAT_CMDL_COMPL */
2387
2388#ifdef HAVE_BUFLIST_MATCH
2389/*
2390 * Check for a match on the file name for buffer "buf" with regprog "prog".
2391 */
2392 static char_u *
2393buflist_match(prog, buf)
2394 regprog_T *prog;
2395 buf_T *buf;
2396{
2397 char_u *match;
2398
2399 /* First try the short file name, then the long file name. */
2400 match = fname_match(prog, buf->b_sfname);
2401 if (match == NULL)
2402 match = fname_match(prog, buf->b_ffname);
2403
2404 return match;
2405}
2406
2407/*
2408 * Try matching the regexp in "prog" with file name "name".
2409 * Return "name" when there is a match, NULL when not.
2410 */
2411 static char_u *
2412fname_match(prog, name)
2413 regprog_T *prog;
2414 char_u *name;
2415{
2416 char_u *match = NULL;
2417 char_u *p;
2418 regmatch_T regmatch;
2419
2420 if (name != NULL)
2421 {
2422 regmatch.regprog = prog;
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002423 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (vim_regexec(&regmatch, name, (colnr_T)0))
2425 match = name;
2426 else
2427 {
2428 /* Replace $(HOME) with '~' and try matching again. */
2429 p = home_replace_save(NULL, name);
2430 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2431 match = name;
2432 vim_free(p);
2433 }
2434 }
2435
2436 return match;
2437}
2438#endif
2439
2440/*
2441 * find file in buffer list by number
2442 */
2443 buf_T *
2444buflist_findnr(nr)
2445 int nr;
2446{
2447 buf_T *buf;
2448
2449 if (nr == 0)
2450 nr = curwin->w_alt_fnum;
2451 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2452 if (buf->b_fnum == nr)
2453 return (buf);
2454 return NULL;
2455}
2456
2457/*
2458 * Get name of file 'n' in the buffer list.
2459 * When the file has no name an empty string is returned.
2460 * home_replace() is used to shorten the file name (used for marks).
2461 * Returns a pointer to allocated memory, of NULL when failed.
2462 */
2463 char_u *
2464buflist_nr2name(n, fullname, helptail)
2465 int n;
2466 int fullname;
2467 int helptail; /* for help buffers return tail only */
2468{
2469 buf_T *buf;
2470
2471 buf = buflist_findnr(n);
2472 if (buf == NULL)
2473 return NULL;
2474 return home_replace_save(helptail ? buf : NULL,
2475 fullname ? buf->b_ffname : buf->b_fname);
2476}
2477
2478/*
2479 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2480 * When "copy_options" is TRUE save the local window option values.
2481 * When "lnum" is 0 only do the options.
2482 */
2483 static void
2484buflist_setfpos(buf, win, lnum, col, copy_options)
2485 buf_T *buf;
2486 win_T *win;
2487 linenr_T lnum;
2488 colnr_T col;
2489 int copy_options;
2490{
2491 wininfo_T *wip;
2492
2493 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2494 if (wip->wi_win == win)
2495 break;
2496 if (wip == NULL)
2497 {
2498 /* allocate a new entry */
2499 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2500 if (wip == NULL)
2501 return;
2502 wip->wi_win = win;
2503 if (lnum == 0) /* set lnum even when it's 0 */
2504 lnum = 1;
2505 }
2506 else
2507 {
2508 /* remove the entry from the list */
2509 if (wip->wi_prev)
2510 wip->wi_prev->wi_next = wip->wi_next;
2511 else
2512 buf->b_wininfo = wip->wi_next;
2513 if (wip->wi_next)
2514 wip->wi_next->wi_prev = wip->wi_prev;
2515 if (copy_options && wip->wi_optset)
2516 {
2517 clear_winopt(&wip->wi_opt);
2518#ifdef FEAT_FOLDING
2519 deleteFoldRecurse(&wip->wi_folds);
2520#endif
2521 }
2522 }
2523 if (lnum != 0)
2524 {
2525 wip->wi_fpos.lnum = lnum;
2526 wip->wi_fpos.col = col;
2527 }
2528 if (copy_options)
2529 {
2530 /* Save the window-specific option values. */
2531 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2532#ifdef FEAT_FOLDING
2533 wip->wi_fold_manual = win->w_fold_manual;
2534 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2535#endif
2536 wip->wi_optset = TRUE;
2537 }
2538
2539 /* insert the entry in front of the list */
2540 wip->wi_next = buf->b_wininfo;
2541 buf->b_wininfo = wip;
2542 wip->wi_prev = NULL;
2543 if (wip->wi_next)
2544 wip->wi_next->wi_prev = wip;
2545
2546 return;
2547}
2548
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002549#ifdef FEAT_DIFF
2550static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2551
2552/*
2553 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2554 * page. That's because a diff is local to a tab page.
2555 */
2556 static int
2557wininfo_other_tab_diff(wip)
2558 wininfo_T *wip;
2559{
2560 win_T *wp;
2561
2562 if (wip->wi_opt.wo_diff)
2563 {
2564 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2565 /* return FALSE when it's a window in the current tab page, thus
2566 * the buffer was in diff mode here */
2567 if (wip->wi_win == wp)
2568 return FALSE;
2569 return TRUE;
2570 }
2571 return FALSE;
2572}
2573#endif
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575/*
2576 * Find info for the current window in buffer "buf".
2577 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002578 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2579 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 * Returns NULL when there isn't any info.
2581 */
2582 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002583find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002585 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 wininfo_T *wip;
2588
2589 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002590 if (wip->wi_win == curwin
2591#ifdef FEAT_DIFF
2592 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2593#endif
2594 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002596
2597 /* If no wininfo for curwin, use the first in the list (that doesn't have
2598 * 'diff' set and is in another tab page). */
2599 if (wip == NULL)
2600 {
2601#ifdef FEAT_DIFF
2602 if (skip_diff_buffer)
2603 {
2604 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2605 if (!wininfo_other_tab_diff(wip))
2606 break;
2607 }
2608 else
2609#endif
2610 wip = buf->b_wininfo;
2611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 return wip;
2613}
2614
2615/*
2616 * Reset the local window options to the values last used in this window.
2617 * If the buffer wasn't used in this window before, use the values from
2618 * the most recently used window. If the values were never set, use the
2619 * global values for the window.
2620 */
2621 void
2622get_winopts(buf)
2623 buf_T *buf;
2624{
2625 wininfo_T *wip;
2626
2627 clear_winopt(&curwin->w_onebuf_opt);
2628#ifdef FEAT_FOLDING
2629 clearFolding(curwin);
2630#endif
2631
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002632 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (wip != NULL && wip->wi_optset)
2634 {
2635 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2636#ifdef FEAT_FOLDING
2637 curwin->w_fold_manual = wip->wi_fold_manual;
2638 curwin->w_foldinvalid = TRUE;
2639 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2640#endif
2641 }
2642 else
2643 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2644
2645#ifdef FEAT_FOLDING
2646 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2647 if (p_fdls >= 0)
2648 curwin->w_p_fdl = p_fdls;
2649#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002650#ifdef FEAT_SYN_HL
2651 check_colorcolumn(curwin);
2652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653}
2654
2655/*
2656 * Find the position (lnum and col) for the buffer 'buf' for the current
2657 * window.
2658 * Returns a pointer to no_position if no position is found.
2659 */
2660 pos_T *
2661buflist_findfpos(buf)
2662 buf_T *buf;
2663{
2664 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002665 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002667 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (wip != NULL)
2669 return &(wip->wi_fpos);
2670 else
2671 return &no_position;
2672}
2673
2674/*
2675 * Find the lnum for the buffer 'buf' for the current window.
2676 */
2677 linenr_T
2678buflist_findlnum(buf)
2679 buf_T *buf;
2680{
2681 return buflist_findfpos(buf)->lnum;
2682}
2683
2684#if defined(FEAT_LISTCMDS) || defined(PROTO)
2685/*
2686 * List all know file names (for :files and :buffers command).
2687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 void
2689buflist_list(eap)
2690 exarg_T *eap;
2691{
2692 buf_T *buf;
2693 int len;
2694 int i;
2695
2696 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2697 {
2698 /* skip unlisted buffers, unless ! was used */
2699 if (!buf->b_p_bl && !eap->forceit)
2700 continue;
2701 msg_putchar('\n');
2702 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002703 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 else
2705 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2706
Bram Moolenaar50cde822005-06-05 21:54:54 +00002707 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 buf->b_fnum,
2709 buf->b_p_bl ? ' ' : 'u',
2710 buf == curbuf ? '%' :
2711 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2712 buf->b_ml.ml_mfp == NULL ? ' ' :
2713 (buf->b_nwindows == 0 ? 'h' : 'a'),
2714 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2715 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002716 : (bufIsChanged(buf) ? '+' : ' '),
2717 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 i = 40 - vim_strsize(IObuff);
2721 do
2722 {
2723 IObuff[len++] = ' ';
2724 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002725 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2726 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002727 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 msg_outtrans(IObuff);
2729 out_flush(); /* output one line at a time */
2730 ui_breakcheck();
2731 }
2732}
2733#endif
2734
2735/*
2736 * Get file name and line number for file 'fnum'.
2737 * Used by DoOneCmd() for translating '%' and '#'.
2738 * Used by insert_reg() and cmdline_paste() for '#' register.
2739 * Return FAIL if not found, OK for success.
2740 */
2741 int
2742buflist_name_nr(fnum, fname, lnum)
2743 int fnum;
2744 char_u **fname;
2745 linenr_T *lnum;
2746{
2747 buf_T *buf;
2748
2749 buf = buflist_findnr(fnum);
2750 if (buf == NULL || buf->b_fname == NULL)
2751 return FAIL;
2752
2753 *fname = buf->b_fname;
2754 *lnum = buflist_findlnum(buf);
2755
2756 return OK;
2757}
2758
2759/*
2760 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2761 * The file name with the full path is also remembered, for when :cd is used.
2762 * Returns FAIL for failure (file name already in use by other buffer)
2763 * OK otherwise.
2764 */
2765 int
2766setfname(buf, ffname, sfname, message)
2767 buf_T *buf;
2768 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002769 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Bram Moolenaar81695252004-12-29 20:58:21 +00002771 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772#ifdef UNIX
2773 struct stat st;
2774#endif
2775
2776 if (ffname == NULL || *ffname == NUL)
2777 {
2778 /* Removing the name. */
2779 vim_free(buf->b_ffname);
2780 vim_free(buf->b_sfname);
2781 buf->b_ffname = NULL;
2782 buf->b_sfname = NULL;
2783#ifdef UNIX
2784 st.st_dev = (dev_T)-1;
2785#endif
2786 }
2787 else
2788 {
2789 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2790 if (ffname == NULL) /* out of memory */
2791 return FAIL;
2792
2793 /*
2794 * if the file name is already used in another buffer:
2795 * - if the buffer is loaded, fail
2796 * - if the buffer is not loaded, delete it from the list
2797 */
2798#ifdef UNIX
2799 if (mch_stat((char *)ffname, &st) < 0)
2800 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002801#endif
2802 if (!(buf->b_flags & BF_DUMMY))
2803#ifdef UNIX
2804 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002806 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807#endif
2808 if (obuf != NULL && obuf != buf)
2809 {
2810 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2811 {
2812 if (message)
2813 EMSG(_("E95: Buffer with this name already exists"));
2814 vim_free(ffname);
2815 return FAIL;
2816 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002817 /* delete from the list */
2818 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820 sfname = vim_strsave(sfname);
2821 if (ffname == NULL || sfname == NULL)
2822 {
2823 vim_free(sfname);
2824 vim_free(ffname);
2825 return FAIL;
2826 }
2827#ifdef USE_FNAME_CASE
2828# ifdef USE_LONG_FNAME
2829 if (USE_LONG_FNAME)
2830# endif
2831 fname_case(sfname, 0); /* set correct case for short file name */
2832#endif
2833 vim_free(buf->b_ffname);
2834 vim_free(buf->b_sfname);
2835 buf->b_ffname = ffname;
2836 buf->b_sfname = sfname;
2837 }
2838 buf->b_fname = buf->b_sfname;
2839#ifdef UNIX
2840 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002841 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 else
2843 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002844 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 buf->b_dev = st.st_dev;
2846 buf->b_ino = st.st_ino;
2847 }
2848#endif
2849
2850#ifndef SHORT_FNAME
2851 buf->b_shortname = FALSE;
2852#endif
2853
2854 buf_name_changed(buf);
2855 return OK;
2856}
2857
2858/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002859 * Crude way of changing the name of a buffer. Use with care!
2860 * The name should be relative to the current directory.
2861 */
2862 void
2863buf_set_name(fnum, name)
2864 int fnum;
2865 char_u *name;
2866{
2867 buf_T *buf;
2868
2869 buf = buflist_findnr(fnum);
2870 if (buf != NULL)
2871 {
2872 vim_free(buf->b_sfname);
2873 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002874 buf->b_ffname = vim_strsave(name);
2875 buf->b_sfname = NULL;
2876 /* Allocate ffname and expand into full path. Also resolves .lnk
2877 * files on Win32. */
2878 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002879 buf->b_fname = buf->b_sfname;
2880 }
2881}
2882
2883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 * Take care of what needs to be done when the name of buffer "buf" has
2885 * changed.
2886 */
2887 void
2888buf_name_changed(buf)
2889 buf_T *buf;
2890{
2891 /*
2892 * If the file name changed, also change the name of the swapfile
2893 */
2894 if (buf->b_ml.ml_mfp != NULL)
2895 ml_setname(buf);
2896
2897 if (curwin->w_buffer == buf)
2898 check_arg_idx(curwin); /* check file name for arg list */
2899#ifdef FEAT_TITLE
2900 maketitle(); /* set window title */
2901#endif
2902#ifdef FEAT_WINDOWS
2903 status_redraw_all(); /* status lines need to be redrawn */
2904#endif
2905 fmarks_check_names(buf); /* check named file marks */
2906 ml_timestamp(buf); /* reset timestamp */
2907}
2908
2909/*
2910 * set alternate file name for current window
2911 *
2912 * Used by do_one_cmd(), do_write() and do_ecmd().
2913 * Return the buffer.
2914 */
2915 buf_T *
2916setaltfname(ffname, sfname, lnum)
2917 char_u *ffname;
2918 char_u *sfname;
2919 linenr_T lnum;
2920{
2921 buf_T *buf;
2922
2923 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2924 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002925 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 curwin->w_alt_fnum = buf->b_fnum;
2927 return buf;
2928}
2929
2930/*
2931 * Get alternate file name for current window.
2932 * Return NULL if there isn't any, and give error message if requested.
2933 */
2934 char_u *
2935getaltfname(errmsg)
2936 int errmsg; /* give error message */
2937{
2938 char_u *fname;
2939 linenr_T dummy;
2940
2941 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2942 {
2943 if (errmsg)
2944 EMSG(_(e_noalt));
2945 return NULL;
2946 }
2947 return fname;
2948}
2949
2950/*
2951 * Add a file name to the buflist and return its number.
2952 * Uses same flags as buflist_new(), except BLN_DUMMY.
2953 *
2954 * used by qf_init(), main() and doarglist()
2955 */
2956 int
2957buflist_add(fname, flags)
2958 char_u *fname;
2959 int flags;
2960{
2961 buf_T *buf;
2962
2963 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2964 if (buf != NULL)
2965 return buf->b_fnum;
2966 return 0;
2967}
2968
2969#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2970/*
2971 * Adjust slashes in file names. Called after 'shellslash' was set.
2972 */
2973 void
2974buflist_slash_adjust()
2975{
2976 buf_T *bp;
2977
2978 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2979 {
2980 if (bp->b_ffname != NULL)
2981 slash_adjust(bp->b_ffname);
2982 if (bp->b_sfname != NULL)
2983 slash_adjust(bp->b_sfname);
2984 }
2985}
2986#endif
2987
2988/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002989 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 * Also save the local window option values.
2991 */
2992 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002993buflist_altfpos(win)
2994 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002996 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997}
2998
2999/*
3000 * Return TRUE if 'ffname' is not the same file as current file.
3001 * Fname must have a full path (expanded by mch_FullName()).
3002 */
3003 int
3004otherfile(ffname)
3005 char_u *ffname;
3006{
3007 return otherfile_buf(curbuf, ffname
3008#ifdef UNIX
3009 , NULL
3010#endif
3011 );
3012}
3013
3014 static int
3015otherfile_buf(buf, ffname
3016#ifdef UNIX
3017 , stp
3018#endif
3019 )
3020 buf_T *buf;
3021 char_u *ffname;
3022#ifdef UNIX
3023 struct stat *stp;
3024#endif
3025{
3026 /* no name is different */
3027 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3028 return TRUE;
3029 if (fnamecmp(ffname, buf->b_ffname) == 0)
3030 return FALSE;
3031#ifdef UNIX
3032 {
3033 struct stat st;
3034
3035 /* If no struct stat given, get it now */
3036 if (stp == NULL)
3037 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003038 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 st.st_dev = (dev_T)-1;
3040 stp = &st;
3041 }
3042 /* Use dev/ino to check if the files are the same, even when the names
3043 * are different (possible with links). Still need to compare the
3044 * name above, for when the file doesn't exist yet.
3045 * Problem: The dev/ino changes when a file is deleted (and created
3046 * again) and remains the same when renamed/moved. We don't want to
3047 * mch_stat() each buffer each time, that would be too slow. Get the
3048 * dev/ino again when they appear to match, but not when they appear
3049 * to be different: Could skip a buffer when it's actually the same
3050 * file. */
3051 if (buf_same_ino(buf, stp))
3052 {
3053 buf_setino(buf);
3054 if (buf_same_ino(buf, stp))
3055 return FALSE;
3056 }
3057 }
3058#endif
3059 return TRUE;
3060}
3061
3062#if defined(UNIX) || defined(PROTO)
3063/*
3064 * Set inode and device number for a buffer.
3065 * Must always be called when b_fname is changed!.
3066 */
3067 void
3068buf_setino(buf)
3069 buf_T *buf;
3070{
3071 struct stat st;
3072
3073 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3074 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003075 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 buf->b_dev = st.st_dev;
3077 buf->b_ino = st.st_ino;
3078 }
3079 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003080 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081}
3082
3083/*
3084 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3085 */
3086 static int
3087buf_same_ino(buf, stp)
3088 buf_T *buf;
3089 struct stat *stp;
3090{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003091 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 && stp->st_dev == buf->b_dev
3093 && stp->st_ino == buf->b_ino);
3094}
3095#endif
3096
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003097/*
3098 * Print info about the current buffer.
3099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 void
3101fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003102 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 int shorthelp;
3104 int dont_truncate;
3105{
3106 char_u *name;
3107 int n;
3108 char_u *p;
3109 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003110 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111
3112 buffer = alloc(IOSIZE);
3113 if (buffer == NULL)
3114 return;
3115
3116 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3117 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003118 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 p = buffer + STRLEN(buffer);
3120 }
3121 else
3122 p = buffer;
3123
3124 *p++ = '"';
3125 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003126 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 else
3128 {
3129 if (!fullname && curbuf->b_fname != NULL)
3130 name = curbuf->b_fname;
3131 else
3132 name = curbuf->b_ffname;
3133 home_replace(shorthelp ? curbuf : NULL, name, p,
3134 (int)(IOSIZE - (p - buffer)), TRUE);
3135 }
3136
Bram Moolenaara800b422010-06-27 01:15:55 +02003137 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 curbufIsChanged() ? (shortmess(SHM_MOD)
3139 ? " [+]" : _(" [Modified]")) : " ",
3140 (curbuf->b_flags & BF_NOTEDITED)
3141#ifdef FEAT_QUICKFIX
3142 && !bt_dontwrite(curbuf)
3143#endif
3144 ? _("[Not edited]") : "",
3145 (curbuf->b_flags & BF_NEW)
3146#ifdef FEAT_QUICKFIX
3147 && !bt_dontwrite(curbuf)
3148#endif
3149 ? _("[New file]") : "",
3150 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003151 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 : _("[readonly]")) : "",
3153 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3154 || curbuf->b_p_ro) ?
3155 " " : "");
3156 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3157 * causes an overflow, thus for large numbers divide instead. */
3158 if (curwin->w_cursor.lnum > 1000000L)
3159 n = (int)(((long)curwin->w_cursor.lnum) /
3160 ((long)curbuf->b_ml.ml_line_count / 100L));
3161 else
3162 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3163 (long)curbuf->b_ml.ml_line_count);
3164 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3165 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003166 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
3168#ifdef FEAT_CMDL_INFO
3169 else if (p_ru)
3170 {
3171 /* Current line and column are already on the screen -- webb */
3172 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003173 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003175 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 (long)curbuf->b_ml.ml_line_count, n);
3177 }
3178#endif
3179 else
3180 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003181 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 _("line %ld of %ld --%d%%-- col "),
3183 (long)curwin->w_cursor.lnum,
3184 (long)curbuf->b_ml.ml_line_count,
3185 n);
3186 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003187 len = STRLEN(buffer);
3188 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3190 }
3191
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003192 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 if (dont_truncate)
3195 {
3196 /* Temporarily set msg_scroll to avoid the message being truncated.
3197 * First call msg_start() to get the message in the right place. */
3198 msg_start();
3199 n = msg_scroll;
3200 msg_scroll = TRUE;
3201 msg(buffer);
3202 msg_scroll = n;
3203 }
3204 else
3205 {
3206 p = msg_trunc_attr(buffer, FALSE, 0);
3207 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 /* Need to repeat the message after redrawing when:
3209 * - When restart_edit is set (otherwise there will be a delay
3210 * before redrawing).
3211 * - When the screen was scrolled but there is no wait-return
3212 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003213 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 }
3215
3216 vim_free(buffer);
3217}
3218
3219 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003220col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003222 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 int col;
3224 int vcol;
3225{
3226 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003227 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003229 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
3231
3232#if defined(FEAT_TITLE) || defined(PROTO)
3233/*
3234 * put file name in title bar of window and in icon title
3235 */
3236
3237static char_u *lasttitle = NULL;
3238static char_u *lasticon = NULL;
3239
3240 void
3241maketitle()
3242{
3243 char_u *p;
3244 char_u *t_str = NULL;
3245 char_u *i_name;
3246 char_u *i_str = NULL;
3247 int maxlen = 0;
3248 int len;
3249 int mustset;
3250 char_u buf[IOSIZE];
3251 int off;
3252
3253 if (!redrawing())
3254 {
3255 /* Postpone updating the title when 'lazyredraw' is set. */
3256 need_maketitle = TRUE;
3257 return;
3258 }
3259
3260 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003261 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 return;
3263
3264 if (p_title)
3265 {
3266 if (p_titlelen > 0)
3267 {
3268 maxlen = p_titlelen * Columns / 100;
3269 if (maxlen < 10)
3270 maxlen = 10;
3271 }
3272
3273 t_str = buf;
3274 if (*p_titlestring != NUL)
3275 {
3276#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003277 if (stl_syntax & STL_IN_TITLE)
3278 {
3279 int use_sandbox = FALSE;
3280 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003281
3282# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003283 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003284# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003285 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003287 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003288 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003289 if (called_emsg)
3290 set_string_option_direct((char_u *)"titlestring", -1,
3291 (char_u *)"", OPT_FREE, SID_ERROR);
3292 called_emsg |= save_called_emsg;
3293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 else
3295#endif
3296 t_str = p_titlestring;
3297 }
3298 else
3299 {
3300 /* format: "fname + (path) (1 of 2) - VIM" */
3301
Bram Moolenaar2c666692012-09-05 13:30:40 +02003302#define SPACE_FOR_FNAME (IOSIZE - 100)
3303#define SPACE_FOR_DIR (IOSIZE - 20)
3304#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003306 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else
3308 {
3309 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003310 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313
3314 switch (bufIsChanged(curbuf)
3315 + (curbuf->b_p_ro * 2)
3316 + (!curbuf->b_p_ma * 4))
3317 {
3318 case 1: STRCAT(buf, " +"); break;
3319 case 2: STRCAT(buf, " ="); break;
3320 case 3: STRCAT(buf, " =+"); break;
3321 case 4:
3322 case 6: STRCAT(buf, " -"); break;
3323 case 5:
3324 case 7: STRCAT(buf, " -+"); break;
3325 }
3326
3327 if (curbuf->b_fname != NULL)
3328 {
3329 /* Get path of file, replace home dir with ~ */
3330 off = (int)STRLEN(buf);
3331 buf[off++] = ' ';
3332 buf[off++] = '(';
3333 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003334 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#ifdef BACKSLASH_IN_FILENAME
3336 /* avoid "c:/name" to be reduced to "c" */
3337 if (isalpha(buf[off]) && buf[off + 1] == ':')
3338 off += 2;
3339#endif
3340 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003341 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003344 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003345 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003348
Bram Moolenaar2c666692012-09-05 13:30:40 +02003349 /* Translate unprintable chars and concatenate. Keep some
3350 * room for the server name. When there is no room (very long
3351 * file name) use (...). */
3352 if (off < SPACE_FOR_DIR)
3353 {
3354 p = transstr(buf + off);
3355 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3356 vim_free(p);
3357 }
3358 else
3359 {
3360 vim_strncpy(buf + off, (char_u *)"...",
3361 (size_t)(SPACE_FOR_ARGNR - off));
3362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 STRCAT(buf, ")");
3364 }
3365
Bram Moolenaar2c666692012-09-05 13:30:40 +02003366 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
3368#if defined(FEAT_CLIENTSERVER)
3369 if (serverName != NULL)
3370 {
3371 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003372 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 else
3375#endif
3376 STRCAT(buf, " - VIM");
3377
3378 if (maxlen > 0)
3379 {
3380 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003381 if (vim_strsize(buf) > maxlen)
3382 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 }
3385 }
3386 mustset = ti_change(t_str, &lasttitle);
3387
3388 if (p_icon)
3389 {
3390 i_str = buf;
3391 if (*p_iconstring != NUL)
3392 {
3393#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003394 if (stl_syntax & STL_IN_ICON)
3395 {
3396 int use_sandbox = FALSE;
3397 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003398
3399# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003400 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003401# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003402 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003404 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003405 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003406 if (called_emsg)
3407 set_string_option_direct((char_u *)"iconstring", -1,
3408 (char_u *)"", OPT_FREE, SID_ERROR);
3409 called_emsg |= save_called_emsg;
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 else
3412#endif
3413 i_str = p_iconstring;
3414 }
3415 else
3416 {
3417 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003418 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 else /* use file name only in icon */
3420 i_name = gettail(curbuf->b_ffname);
3421 *i_str = NUL;
3422 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003423 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 if (len > 100)
3425 {
3426 len -= 100;
3427#ifdef FEAT_MBYTE
3428 if (has_mbyte)
3429 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3430#endif
3431 i_name += len;
3432 }
3433 STRCPY(i_str, i_name);
3434 trans_characters(i_str, IOSIZE);
3435 }
3436 }
3437
3438 mustset |= ti_change(i_str, &lasticon);
3439
3440 if (mustset)
3441 resettitle();
3442}
3443
3444/*
3445 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3446 * from "str" if it does.
3447 * Return TRUE when "*last" changed.
3448 */
3449 static int
3450ti_change(str, last)
3451 char_u *str;
3452 char_u **last;
3453{
3454 if ((str == NULL) != (*last == NULL)
3455 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3456 {
3457 vim_free(*last);
3458 if (str == NULL)
3459 *last = NULL;
3460 else
3461 *last = vim_strsave(str);
3462 return TRUE;
3463 }
3464 return FALSE;
3465}
3466
3467/*
3468 * Put current window title back (used after calling a shell)
3469 */
3470 void
3471resettitle()
3472{
3473 mch_settitle(lasttitle, lasticon);
3474}
Bram Moolenaarea408852005-06-25 22:49:46 +00003475
3476# if defined(EXITFREE) || defined(PROTO)
3477 void
3478free_titles()
3479{
3480 vim_free(lasttitle);
3481 vim_free(lasticon);
3482}
3483# endif
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#endif /* FEAT_TITLE */
3486
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003487#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003489 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 * Return length of string in screen cells.
3491 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003492 * Normally works for window "wp", except when working for 'tabline' then it
3493 * is "curwin".
3494 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 * Items are drawn interspersed with the text that surrounds it
3496 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3497 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3498 *
3499 * If maxwidth is not zero, the string will be filled at any middle marker
3500 * or truncated if too long, fillchar is used for all whitespace.
3501 */
3502 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003503build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3504 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003506 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 size_t outlen; /* length of out[] */
3508 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003509 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 int fillchar;
3511 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003512 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3513 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 char_u *p;
3516 char_u *s;
3517 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003518 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519#ifdef FEAT_EVAL
3520 win_T *o_curwin;
3521 buf_T *o_curbuf;
3522#endif
3523 int empty_line;
3524 colnr_T virtcol;
3525 long l;
3526 long n;
3527 int prevchar_isflag;
3528 int prevchar_isitem;
3529 int itemisflag;
3530 int fillable;
3531 char_u *str;
3532 long num;
3533 int width;
3534 int itemcnt;
3535 int curitem;
3536 int groupitem[STL_MAX_ITEM];
3537 int groupdepth;
3538 struct stl_item
3539 {
3540 char_u *start;
3541 int minwid;
3542 int maxwid;
3543 enum
3544 {
3545 Normal,
3546 Empty,
3547 Group,
3548 Middle,
3549 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003550 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 Trunc
3552 } type;
3553 } item[STL_MAX_ITEM];
3554 int minwid;
3555 int maxwid;
3556 int zeropad;
3557 char_u base;
3558 char_u opt;
3559#define TMPLEN 70
3560 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003561 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003562 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003563
3564#ifdef FEAT_EVAL
3565 /*
3566 * When the format starts with "%!" then evaluate it as an expression and
3567 * use the result as the actual format string.
3568 */
3569 if (fmt[0] == '%' && fmt[1] == '!')
3570 {
3571 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3572 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003573 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003574 }
3575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 if (fillchar == 0)
3578 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003579#ifdef FEAT_MBYTE
3580 /* Can't handle a multi-byte fill character yet. */
3581 else if (mb_char2len(fillchar) > 1)
3582 fillchar = '-';
3583#endif
3584
Bram Moolenaar567199b2013-04-24 16:52:36 +02003585 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3586 * p will become invalid when getting another buffer line. */
3587 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3588 empty_line = (*p == NUL);
3589
3590 /* Get the byte value now, in case we need it below. This is more
3591 * efficient than making a copy of the line. */
3592 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3593 byteval = 0;
3594 else
3595#ifdef FEAT_MBYTE
3596 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3597#else
3598 byteval = p[wp->w_cursor.col];
3599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
3601 groupdepth = 0;
3602 p = out;
3603 curitem = 0;
3604 prevchar_isflag = TRUE;
3605 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003606 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003608 if (curitem == STL_MAX_ITEM)
3609 {
3610 /* There are too many items. Add the error code to the statusline
3611 * to give the user a hint about what went wrong. */
3612 if (p + 6 < out + outlen)
3613 {
3614 mch_memmove(p, " E541", (size_t)5);
3615 p += 5;
3616 }
3617 break;
3618 }
3619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 if (*s != NUL && *s != '%')
3621 prevchar_isflag = prevchar_isitem = FALSE;
3622
3623 /*
3624 * Handle up to the next '%' or the end.
3625 */
3626 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3627 *p++ = *s++;
3628 if (*s == NUL || p + 1 >= out + outlen)
3629 break;
3630
3631 /*
3632 * Handle one '%' item.
3633 */
3634 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003635 if (*s == NUL) /* ignore trailing % */
3636 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (*s == '%')
3638 {
3639 if (p + 1 >= out + outlen)
3640 break;
3641 *p++ = *s++;
3642 prevchar_isflag = prevchar_isitem = FALSE;
3643 continue;
3644 }
3645 if (*s == STL_MIDDLEMARK)
3646 {
3647 s++;
3648 if (groupdepth > 0)
3649 continue;
3650 item[curitem].type = Middle;
3651 item[curitem++].start = p;
3652 continue;
3653 }
3654 if (*s == STL_TRUNCMARK)
3655 {
3656 s++;
3657 item[curitem].type = Trunc;
3658 item[curitem++].start = p;
3659 continue;
3660 }
3661 if (*s == ')')
3662 {
3663 s++;
3664 if (groupdepth < 1)
3665 continue;
3666 groupdepth--;
3667
3668 t = item[groupitem[groupdepth]].start;
3669 *p = NUL;
3670 l = vim_strsize(t);
3671 if (curitem > groupitem[groupdepth] + 1
3672 && item[groupitem[groupdepth]].minwid == 0)
3673 {
3674 /* remove group if all items are empty */
3675 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3676 if (item[n].type == Normal)
3677 break;
3678 if (n == curitem)
3679 {
3680 p = t;
3681 l = 0;
3682 }
3683 }
3684 if (l > item[groupitem[groupdepth]].maxwid)
3685 {
3686 /* truncate, remove n bytes of text at the start */
3687#ifdef FEAT_MBYTE
3688 if (has_mbyte)
3689 {
3690 /* Find the first character that should be included. */
3691 n = 0;
3692 while (l >= item[groupitem[groupdepth]].maxwid)
3693 {
3694 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003695 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697 }
3698 else
3699#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003700 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
3702 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003703 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 p = p - n + 1;
3705#ifdef FEAT_MBYTE
3706 /* Fill up space left over by half a double-wide char. */
3707 while (++l < item[groupitem[groupdepth]].minwid)
3708 *p++ = fillchar;
3709#endif
3710
3711 /* correct the start of the items for the truncation */
3712 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3713 {
3714 item[l].start -= n;
3715 if (item[l].start < t)
3716 item[l].start = t;
3717 }
3718 }
3719 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3720 {
3721 /* fill */
3722 n = item[groupitem[groupdepth]].minwid;
3723 if (n < 0)
3724 {
3725 /* fill by appending characters */
3726 n = 0 - n;
3727 while (l++ < n && p + 1 < out + outlen)
3728 *p++ = fillchar;
3729 }
3730 else
3731 {
3732 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003733 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 l = n - l;
3735 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003736 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 p += l;
3738 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3739 item[n].start += l;
3740 for ( ; l > 0; l--)
3741 *t++ = fillchar;
3742 }
3743 }
3744 continue;
3745 }
3746 minwid = 0;
3747 maxwid = 9999;
3748 zeropad = FALSE;
3749 l = 1;
3750 if (*s == '0')
3751 {
3752 s++;
3753 zeropad = TRUE;
3754 }
3755 if (*s == '-')
3756 {
3757 s++;
3758 l = -1;
3759 }
3760 if (VIM_ISDIGIT(*s))
3761 {
3762 minwid = (int)getdigits(&s);
3763 if (minwid < 0) /* overflow */
3764 minwid = 0;
3765 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003766 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
3768 item[curitem].type = Highlight;
3769 item[curitem].start = p;
3770 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3771 s++;
3772 curitem++;
3773 continue;
3774 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003775 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3776 {
3777 if (*s == STL_TABCLOSENR)
3778 {
3779 if (minwid == 0)
3780 {
3781 /* %X ends the close label, go back to the previously
3782 * define tab label nr. */
3783 for (n = curitem - 1; n >= 0; --n)
3784 if (item[n].type == TabPage && item[n].minwid >= 0)
3785 {
3786 minwid = item[n].minwid;
3787 break;
3788 }
3789 }
3790 else
3791 /* close nrs are stored as negative values */
3792 minwid = - minwid;
3793 }
3794 item[curitem].type = TabPage;
3795 item[curitem].start = p;
3796 item[curitem].minwid = minwid;
3797 s++;
3798 curitem++;
3799 continue;
3800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (*s == '.')
3802 {
3803 s++;
3804 if (VIM_ISDIGIT(*s))
3805 {
3806 maxwid = (int)getdigits(&s);
3807 if (maxwid <= 0) /* overflow */
3808 maxwid = 50;
3809 }
3810 }
3811 minwid = (minwid > 50 ? 50 : minwid) * l;
3812 if (*s == '(')
3813 {
3814 groupitem[groupdepth++] = curitem;
3815 item[curitem].type = Group;
3816 item[curitem].start = p;
3817 item[curitem].minwid = minwid;
3818 item[curitem].maxwid = maxwid;
3819 s++;
3820 curitem++;
3821 continue;
3822 }
3823 if (vim_strchr(STL_ALL, *s) == NULL)
3824 {
3825 s++;
3826 continue;
3827 }
3828 opt = *s++;
3829
3830 /* OK - now for the real work */
3831 base = 'D';
3832 itemisflag = FALSE;
3833 fillable = TRUE;
3834 num = -1;
3835 str = NULL;
3836 switch (opt)
3837 {
3838 case STL_FILEPATH:
3839 case STL_FULLPATH:
3840 case STL_FILENAME:
3841 fillable = FALSE; /* don't change ' ' to fillchar */
3842 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003843 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 else
3845 {
3846 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003847 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3849 }
3850 trans_characters(NameBuff, MAXPATHL);
3851 if (opt != STL_FILENAME)
3852 str = NameBuff;
3853 else
3854 str = gettail(NameBuff);
3855 break;
3856
3857 case STL_VIM_EXPR: /* '{' */
3858 itemisflag = TRUE;
3859 t = p;
3860 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3861 *p++ = *s++;
3862 if (*s != '}') /* missing '}' or out of space */
3863 break;
3864 s++;
3865 *p = 0;
3866 p = t;
3867
3868#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003869 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3871
3872 o_curbuf = curbuf;
3873 o_curwin = curwin;
3874 curwin = wp;
3875 curbuf = wp->w_buffer;
3876
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003877 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
3879 curwin = o_curwin;
3880 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003881 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
3883 if (str != NULL && *str != 0)
3884 {
3885 if (*skipdigits(str) == NUL)
3886 {
3887 num = atoi((char *)str);
3888 vim_free(str);
3889 str = NULL;
3890 itemisflag = FALSE;
3891 }
3892 }
3893#endif
3894 break;
3895
3896 case STL_LINE:
3897 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3898 ? 0L : (long)(wp->w_cursor.lnum);
3899 break;
3900
3901 case STL_NUMLINES:
3902 num = wp->w_buffer->b_ml.ml_line_count;
3903 break;
3904
3905 case STL_COLUMN:
3906 num = !(State & INSERT) && empty_line
3907 ? 0 : (int)wp->w_cursor.col + 1;
3908 break;
3909
3910 case STL_VIRTCOL:
3911 case STL_VIRTCOL_ALT:
3912 /* In list mode virtcol needs to be recomputed */
3913 virtcol = wp->w_virtcol;
3914 if (wp->w_p_list && lcs_tab1 == NUL)
3915 {
3916 wp->w_p_list = FALSE;
3917 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3918 wp->w_p_list = TRUE;
3919 }
3920 ++virtcol;
3921 /* Don't display %V if it's the same as %c. */
3922 if (opt == STL_VIRTCOL_ALT
3923 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3924 ? 0 : (int)wp->w_cursor.col + 1)))
3925 break;
3926 num = (long)virtcol;
3927 break;
3928
3929 case STL_PERCENTAGE:
3930 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3931 (long)wp->w_buffer->b_ml.ml_line_count);
3932 break;
3933
3934 case STL_ALTPERCENT:
3935 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003936 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 break;
3938
3939 case STL_ARGLISTSTAT:
3940 fillable = FALSE;
3941 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003942 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 str = tmp;
3944 break;
3945
3946 case STL_KEYMAP:
3947 fillable = FALSE;
3948 if (get_keymap_str(wp, tmp, TMPLEN))
3949 str = tmp;
3950 break;
3951 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003952#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003953 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954#else
3955 num = 0;
3956#endif
3957 break;
3958
3959 case STL_BUFNO:
3960 num = wp->w_buffer->b_fnum;
3961 break;
3962
3963 case STL_OFFSET_X:
3964 base = 'X';
3965 case STL_OFFSET:
3966#ifdef FEAT_BYTEOFF
3967 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3968 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3969 0L : l + 1 + (!(State & INSERT) && empty_line ?
3970 0 : (int)wp->w_cursor.col);
3971#endif
3972 break;
3973
3974 case STL_BYTEVAL_X:
3975 base = 'X';
3976 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02003977 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (num == NL)
3979 num = 0;
3980 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3981 num = NL;
3982 break;
3983
3984 case STL_ROFLAG:
3985 case STL_ROFLAG_ALT:
3986 itemisflag = TRUE;
3987 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02003988 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 break;
3990
3991 case STL_HELPFLAG:
3992 case STL_HELPFLAG_ALT:
3993 itemisflag = TRUE;
3994 if (wp->w_buffer->b_help)
3995 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003996 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 break;
3998
3999#ifdef FEAT_AUTOCMD
4000 case STL_FILETYPE:
4001 if (*wp->w_buffer->b_p_ft != NUL
4002 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4003 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004004 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4005 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 str = tmp;
4007 }
4008 break;
4009
4010 case STL_FILETYPE_ALT:
4011 itemisflag = TRUE;
4012 if (*wp->w_buffer->b_p_ft != NUL
4013 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4014 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004015 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4016 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 for (t = tmp; *t != 0; t++)
4018 *t = TOUPPER_LOC(*t);
4019 str = tmp;
4020 }
4021 break;
4022#endif
4023
4024#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4025 case STL_PREVIEWFLAG:
4026 case STL_PREVIEWFLAG_ALT:
4027 itemisflag = TRUE;
4028 if (wp->w_p_pvw)
4029 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4030 : _("[Preview]"));
4031 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004032
4033 case STL_QUICKFIX:
4034 if (bt_quickfix(wp->w_buffer))
4035 str = (char_u *)(wp->w_llist_ref
4036 ? _(msg_loclist)
4037 : _(msg_qflist));
4038 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039#endif
4040
4041 case STL_MODIFIED:
4042 case STL_MODIFIED_ALT:
4043 itemisflag = TRUE;
4044 switch ((opt == STL_MODIFIED_ALT)
4045 + bufIsChanged(wp->w_buffer) * 2
4046 + (!wp->w_buffer->b_p_ma) * 4)
4047 {
4048 case 2: str = (char_u *)"[+]"; break;
4049 case 3: str = (char_u *)",+"; break;
4050 case 4: str = (char_u *)"[-]"; break;
4051 case 5: str = (char_u *)",-"; break;
4052 case 6: str = (char_u *)"[+-]"; break;
4053 case 7: str = (char_u *)",+-"; break;
4054 }
4055 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004056
4057 case STL_HIGHLIGHT:
4058 t = s;
4059 while (*s != '#' && *s != NUL)
4060 ++s;
4061 if (*s == '#')
4062 {
4063 item[curitem].type = Highlight;
4064 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004065 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004066 curitem++;
4067 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004068 if (*s != NUL)
4069 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004070 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072
4073 item[curitem].start = p;
4074 item[curitem].type = Normal;
4075 if (str != NULL && *str)
4076 {
4077 t = str;
4078 if (itemisflag)
4079 {
4080 if ((t[0] && t[1])
4081 && ((!prevchar_isitem && *t == ',')
4082 || (prevchar_isflag && *t == ' ')))
4083 t++;
4084 prevchar_isflag = TRUE;
4085 }
4086 l = vim_strsize(t);
4087 if (l > 0)
4088 prevchar_isitem = TRUE;
4089 if (l > maxwid)
4090 {
4091 while (l >= maxwid)
4092#ifdef FEAT_MBYTE
4093 if (has_mbyte)
4094 {
4095 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004096 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098 else
4099#endif
4100 l -= byte2cells(*t++);
4101 if (p + 1 >= out + outlen)
4102 break;
4103 *p++ = '<';
4104 }
4105 if (minwid > 0)
4106 {
4107 for (; l < minwid && p + 1 < out + outlen; l++)
4108 {
4109 /* Don't put a "-" in front of a digit. */
4110 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4111 *p++ = ' ';
4112 else
4113 *p++ = fillchar;
4114 }
4115 minwid = 0;
4116 }
4117 else
4118 minwid *= -1;
4119 while (*t && p + 1 < out + outlen)
4120 {
4121 *p++ = *t++;
4122 /* Change a space by fillchar, unless fillchar is '-' and a
4123 * digit follows. */
4124 if (fillable && p[-1] == ' '
4125 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4126 p[-1] = fillchar;
4127 }
4128 for (; l < minwid && p + 1 < out + outlen; l++)
4129 *p++ = fillchar;
4130 }
4131 else if (num >= 0)
4132 {
4133 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4134 char_u nstr[20];
4135
4136 if (p + 20 >= out + outlen)
4137 break; /* not sufficient space */
4138 prevchar_isitem = TRUE;
4139 t = nstr;
4140 if (opt == STL_VIRTCOL_ALT)
4141 {
4142 *t++ = '-';
4143 minwid--;
4144 }
4145 *t++ = '%';
4146 if (zeropad)
4147 *t++ = '0';
4148 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004149 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 *t = 0;
4151
4152 for (n = num, l = 1; n >= nbase; n /= nbase)
4153 l++;
4154 if (opt == STL_VIRTCOL_ALT)
4155 l++;
4156 if (l > maxwid)
4157 {
4158 l += 2;
4159 n = l - maxwid;
4160 while (l-- > maxwid)
4161 num /= nbase;
4162 *t++ = '>';
4163 *t++ = '%';
4164 *t = t[-3];
4165 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004166 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4167 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 }
4169 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004170 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4171 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 p += STRLEN(p);
4173 }
4174 else
4175 item[curitem].type = Empty;
4176
4177 if (opt == STL_VIM_EXPR)
4178 vim_free(str);
4179
4180 if (num >= 0 || (!itemisflag && str && *str))
4181 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4182 curitem++;
4183 }
4184 *p = NUL;
4185 itemcnt = curitem;
4186
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004187#ifdef FEAT_EVAL
4188 if (usefmt != fmt)
4189 vim_free(usefmt);
4190#endif
4191
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 width = vim_strsize(out);
4193 if (maxwidth > 0 && width > maxwidth)
4194 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004195 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 l = 0;
4197 if (itemcnt == 0)
4198 s = out;
4199 else
4200 {
4201 for ( ; l < itemcnt; l++)
4202 if (item[l].type == Trunc)
4203 {
4204 /* Truncate at %< item. */
4205 s = item[l].start;
4206 break;
4207 }
4208 if (l == itemcnt)
4209 {
4210 /* No %< item, truncate first item. */
4211 s = item[0].start;
4212 l = 0;
4213 }
4214 }
4215
4216 if (width - vim_strsize(s) >= maxwidth)
4217 {
4218 /* Truncation mark is beyond max length */
4219#ifdef FEAT_MBYTE
4220 if (has_mbyte)
4221 {
4222 s = out;
4223 width = 0;
4224 for (;;)
4225 {
4226 width += ptr2cells(s);
4227 if (width >= maxwidth)
4228 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004229 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231 /* Fill up for half a double-wide character. */
4232 while (++width < maxwidth)
4233 *s++ = fillchar;
4234 }
4235 else
4236#endif
4237 s = out + maxwidth - 1;
4238 for (l = 0; l < itemcnt; l++)
4239 if (item[l].start > s)
4240 break;
4241 itemcnt = l;
4242 *s++ = '>';
4243 *s = 0;
4244 }
4245 else
4246 {
4247#ifdef FEAT_MBYTE
4248 if (has_mbyte)
4249 {
4250 n = 0;
4251 while (width >= maxwidth)
4252 {
4253 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004254 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 }
4257 else
4258#endif
4259 n = width - maxwidth + 1;
4260 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004261 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 *s = '<';
4263
4264 /* Fill up for half a double-wide character. */
4265 while (++width < maxwidth)
4266 {
4267 s = s + STRLEN(s);
4268 *s++ = fillchar;
4269 *s = NUL;
4270 }
4271
4272 --n; /* count the '<' */
4273 for (; l < itemcnt; l++)
4274 {
4275 if (item[l].start - n >= s)
4276 item[l].start -= n;
4277 else
4278 item[l].start = s;
4279 }
4280 }
4281 width = maxwidth;
4282 }
4283 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4284 {
4285 /* Apply STL_MIDDLE if any */
4286 for (l = 0; l < itemcnt; l++)
4287 if (item[l].type == Middle)
4288 break;
4289 if (l < itemcnt)
4290 {
4291 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004292 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 for (s = item[l].start; s < p; s++)
4294 *s = fillchar;
4295 for (l++; l < itemcnt; l++)
4296 item[l].start += maxwidth - width;
4297 width = maxwidth;
4298 }
4299 }
4300
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004301 /* Store the info about highlighting. */
4302 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004304 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 for (l = 0; l < itemcnt; l++)
4306 {
4307 if (item[l].type == Highlight)
4308 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004309 sp->start = item[l].start;
4310 sp->userhl = item[l].minwid;
4311 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004314 sp->start = NULL;
4315 sp->userhl = 0;
4316 }
4317
4318 /* Store the info about tab pages labels. */
4319 if (tabtab != NULL)
4320 {
4321 sp = tabtab;
4322 for (l = 0; l < itemcnt; l++)
4323 {
4324 if (item[l].type == TabPage)
4325 {
4326 sp->start = item[l].start;
4327 sp->userhl = item[l].minwid;
4328 sp++;
4329 }
4330 }
4331 sp->start = NULL;
4332 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334
4335 return width;
4336}
4337#endif /* FEAT_STL_OPT */
4338
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004339#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4340 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004342 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4343 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 */
4345 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004346get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004348 char_u *buf;
4349 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
4351 long above; /* number of lines above window */
4352 long below; /* number of lines below window */
4353
4354 above = wp->w_topline - 1;
4355#ifdef FEAT_DIFF
4356 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4357#endif
4358 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4359 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004360 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4361 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004363 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004365 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 ? (int)(above / ((above + below) / 100L))
4367 : (int)(above * 100L / (above + below)));
4368}
4369#endif
4370
4371/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004372 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 * Return TRUE if it was appended.
4374 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004375 static int
4376append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 win_T *wp;
4378 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004379 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
4382 char_u *p;
4383
4384 if (ARGCOUNT <= 1) /* nothing to do */
4385 return FALSE;
4386
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004387 p = buf + STRLEN(buf); /* go to the end of the buffer */
4388 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 return FALSE;
4390 *p++ = ' ';
4391 *p++ = '(';
4392 if (add_file)
4393 {
4394 STRCPY(p, "file ");
4395 p += 5;
4396 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004397 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4398 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4400 return TRUE;
4401}
4402
4403/*
4404 * If fname is not a full path, make it a full path.
4405 * Returns pointer to allocated memory (NULL for failure).
4406 */
4407 char_u *
4408fix_fname(fname)
4409 char_u *fname;
4410{
4411 /*
4412 * Force expanding the path always for Unix, because symbolic links may
4413 * mess up the full path name, even though it starts with a '/'.
4414 * Also expand when there is ".." in the file name, try to remove it,
4415 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004416 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 * For MS-Windows also expand names like "longna~1" to "longname".
4418 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004419#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 return FullName_save(fname, TRUE);
4421#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004422 if (!vim_isAbsName(fname)
4423 || strstr((char *)fname, "..") != NULL
4424 || strstr((char *)fname, "//") != NULL
4425# ifdef BACKSLASH_IN_FILENAME
4426 || strstr((char *)fname, "\\\\") != NULL
4427# endif
4428# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 )
4432 return FullName_save(fname, FALSE);
4433
4434 fname = vim_strsave(fname);
4435
Bram Moolenaar9b942202007-10-03 12:31:33 +00004436# ifdef USE_FNAME_CASE
4437# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
4441 if (fname != NULL)
4442 fname_case(fname, 0); /* set correct case for file name */
4443 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 return fname;
4447#endif
4448}
4449
4450/*
4451 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4452 * "ffname" becomes a pointer to allocated memory (or NULL).
4453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 void
4455fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004456 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 char_u **ffname;
4458 char_u **sfname;
4459{
4460 if (*ffname == NULL) /* if no file name given, nothing to do */
4461 return;
4462 if (*sfname == NULL) /* if no short file name given, use ffname */
4463 *sfname = *ffname;
4464 *ffname = fix_fname(*ffname); /* expand to full path */
4465
4466#ifdef FEAT_SHORTCUT
4467 if (!buf->b_p_bin)
4468 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004469 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /* If the file name is a shortcut file, use the file it links to. */
4472 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004473 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 {
4475 vim_free(*ffname);
4476 *ffname = rfname;
4477 *sfname = rfname;
4478 }
4479 }
4480#endif
4481}
4482
4483/*
4484 * Get the file name for an argument list entry.
4485 */
4486 char_u *
4487alist_name(aep)
4488 aentry_T *aep;
4489{
4490 buf_T *bp;
4491
4492 /* Use the name from the associated buffer if it exists. */
4493 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004494 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 return aep->ae_fname;
4496 return bp->b_fname;
4497}
4498
4499#if defined(FEAT_WINDOWS) || defined(PROTO)
4500/*
4501 * do_arg_all(): Open up to 'count' windows, one for each argument.
4502 */
4503 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004504do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 int count;
4506 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004507 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508{
4509 int i;
4510 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004511 char_u *opened; /* Array of weight for which args are open:
4512 * 0: not opened
4513 * 1: opened in other tab
4514 * 2: opened in curtab
4515 * 3: opened in curtab and curwin
4516 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004517 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 int use_firstwin = FALSE; /* use first window for arglist */
4519 int split_ret = OK;
4520 int p_ea_save;
4521 alist_T *alist; /* argument list to be used */
4522 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004523 tabpage_T *tpnext;
4524 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004525 win_T *old_curwin, *last_curwin;
4526 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004527 win_T *new_curwin = NULL;
4528 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
4530 if (ARGCOUNT <= 0)
4531 {
4532 /* Don't give an error message. We don't want it when the ":all"
4533 * command is in the .vimrc. */
4534 return;
4535 }
4536 setpcmark();
4537
4538 opened_len = ARGCOUNT;
4539 opened = alloc_clear((unsigned)opened_len);
4540 if (opened == NULL)
4541 return;
4542
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004543 /* Autocommands may do anything to the argument list. Make sure it's not
4544 * freed while we are working here by "locking" it. We still have to
4545 * watch out for its size to be changed. */
4546 alist = curwin->w_alist;
4547 ++alist->al_refcount;
4548
4549 old_curwin = curwin;
4550 old_curtab = curtab;
4551
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552#ifdef FEAT_GUI
4553 need_mouse_correct = TRUE;
4554#endif
4555
4556 /*
4557 * Try closing all windows that are not in the argument list.
4558 * Also close windows that are not full width;
4559 * When 'hidden' or "forceit" set the buffer becomes hidden.
4560 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004561 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004563 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004564 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004565 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004567 tpnext = curtab->tp_next;
4568 for (wp = firstwin; wp != NULL; wp = wpnext)
4569 {
4570 wpnext = wp->w_next;
4571 buf = wp->w_buffer;
4572 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004573 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004575 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004577 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004578 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004579 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004581 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004582 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004584 if (i < alist->al_ga.ga_len
4585 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4586 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4587 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004589 int weight = 1;
4590
4591 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004592 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004593 ++weight;
4594 if (old_curwin == wp)
4595 ++weight;
4596 }
4597
4598 if (weight > (int)opened[i])
4599 {
4600 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004601 if (i == 0)
4602 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004603 if (new_curwin != NULL)
4604 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004605 new_curwin = wp;
4606 new_curtab = curtab;
4607 }
4608 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004609 else if (keep_tabs)
4610 i = opened_len;
4611
4612 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004613 {
4614 /* Use the current argument list for all windows
4615 * containing a file from it. */
4616 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004617 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004618 ++wp->w_alist->al_refcount;
4619 }
4620 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
4623 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004624 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004626 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004628 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004629 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004631 /* If the buffer was changed, and we would like to hide it,
4632 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004633 if (!P_HID(buf) && buf->b_nwindows <= 1
4634 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004636 (void)autowrite(buf, FALSE);
4637#ifdef FEAT_AUTOCMD
4638 /* check if autocommands removed the window */
4639 if (!win_valid(wp) || !buf_valid(buf))
4640 {
4641 wpnext = firstwin; /* start all over... */
4642 continue;
4643 }
4644#endif
4645 }
4646#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004647 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004648 if (firstwin == lastwin
4649 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004650#endif
4651 use_firstwin = TRUE;
4652#ifdef FEAT_WINDOWS
4653 else
4654 {
4655 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4656# ifdef FEAT_AUTOCMD
4657 /* check if autocommands removed the next window */
4658 if (!win_valid(wpnext))
4659 wpnext = firstwin; /* start all over... */
4660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 }
4662#endif
4663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004666
4667 /* Without the ":tab" modifier only do the current tab page. */
4668 if (had_tab == 0 || tpnext == NULL)
4669 break;
4670
4671# ifdef FEAT_AUTOCMD
4672 /* check if autocommands removed the next tab page */
4673 if (!valid_tabpage(tpnext))
4674 tpnext = first_tabpage; /* start all over...*/
4675# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004676 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
4678
4679 /*
4680 * Open a window for files in the argument list that don't have one.
4681 * ARGCOUNT may change while doing this, because of autocommands.
4682 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004683 if (count > opened_len || count <= 0)
4684 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
4686#ifdef FEAT_AUTOCMD
4687 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4688 ++autocmd_no_enter;
4689 ++autocmd_no_leave;
4690#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004691 last_curwin = curwin;
4692 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004694#ifdef FEAT_WINDOWS
4695 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4696 * leaving an empty tab page when executed locally. */
4697 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4698 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4699 use_firstwin = TRUE;
4700#endif
4701
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004702 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4705 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004706 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
4708 /* Move the already present window to below the current window */
4709 if (curwin->w_arg_idx != i)
4710 {
4711 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4712 {
4713 if (wpnext->w_arg_idx == i)
4714 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004715 if (keep_tabs)
4716 {
4717 new_curwin = wpnext;
4718 new_curtab = curtab;
4719 }
4720 else
4721 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 break;
4723 }
4724 }
4725 }
4726 }
4727 else if (split_ret == OK)
4728 {
4729 if (!use_firstwin) /* split current window */
4730 {
4731 p_ea_save = p_ea;
4732 p_ea = TRUE; /* use space from all windows */
4733 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4734 p_ea = p_ea_save;
4735 if (split_ret == FAIL)
4736 continue;
4737 }
4738#ifdef FEAT_AUTOCMD
4739 else /* first window: do autocmd for leaving this buffer */
4740 --autocmd_no_leave;
4741#endif
4742
4743 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004744 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 */
4746 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004747 if (i == 0)
4748 {
4749 new_curwin = curwin;
4750 new_curtab = curtab;
4751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4753 ECMD_ONE,
4754 ((P_HID(curwin->w_buffer)
4755 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004756 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757#ifdef FEAT_AUTOCMD
4758 if (use_firstwin)
4759 ++autocmd_no_leave;
4760#endif
4761 use_firstwin = FALSE;
4762 }
4763 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004764
4765 /* When ":tab" was used open a new tab for a new window repeatedly. */
4766 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4767 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769
4770 /* Remove the "lock" on the argument list. */
4771 alist_unlink(alist);
4772
4773#ifdef FEAT_AUTOCMD
4774 --autocmd_no_enter;
4775#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004776 /* restore last referenced tabpage's curwin */
4777 if (last_curtab != new_curtab)
4778 {
4779 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004780 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004781 if (win_valid(last_curwin))
4782 win_enter(last_curwin, FALSE);
4783 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004784 /* to window with first arg */
4785 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004786 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004787 if (win_valid(new_curwin))
4788 win_enter(new_curwin, FALSE);
4789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790#ifdef FEAT_AUTOCMD
4791 --autocmd_no_leave;
4792#endif
4793 vim_free(opened);
4794}
4795
4796# if defined(FEAT_LISTCMDS) || defined(PROTO)
4797/*
4798 * Open a window for a number of buffers.
4799 */
4800 void
4801ex_buffer_all(eap)
4802 exarg_T *eap;
4803{
4804 buf_T *buf;
4805 win_T *wp, *wpnext;
4806 int split_ret = OK;
4807 int p_ea_save;
4808 int open_wins = 0;
4809 int r;
4810 int count; /* Maximum number of windows to open. */
4811 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004812#ifdef FEAT_WINDOWS
4813 int had_tab = cmdmod.tab;
4814 tabpage_T *tpnext;
4815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
4817 if (eap->addr_count == 0) /* make as many windows as possible */
4818 count = 9999;
4819 else
4820 count = eap->line2; /* make as many windows as specified */
4821 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4822 all = FALSE;
4823 else
4824 all = TRUE;
4825
4826 setpcmark();
4827
4828#ifdef FEAT_GUI
4829 need_mouse_correct = TRUE;
4830#endif
4831
4832 /*
4833 * Close superfluous windows (two windows for the same buffer).
4834 * Also close windows that are not full-width.
4835 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004836#ifdef FEAT_WINDOWS
4837 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004838 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004839 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004842 tpnext = curtab->tp_next;
4843 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004845 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004846 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004847#ifdef FEAT_VERTSPLIT
4848 || ((cmdmod.split & WSP_VERT)
4849 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004850 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004851 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004853#ifdef FEAT_WINDOWS
4854 || (had_tab > 0 && wp != firstwin)
4855#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004856 ) && firstwin != lastwin
4857#ifdef FEAT_AUTOCMD
4858 && !(wp->w_closing || wp->w_buffer->b_closing)
4859#endif
4860 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004861 {
4862 win_close(wp, FALSE);
4863#ifdef FEAT_AUTOCMD
4864 wpnext = firstwin; /* just in case an autocommand does
4865 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004866 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004867 open_wins = 0;
4868#endif
4869 }
4870 else
4871 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004873
4874#ifdef FEAT_WINDOWS
4875 /* Without the ":tab" modifier only do the current tab page. */
4876 if (had_tab == 0 || tpnext == NULL)
4877 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004878 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 /*
4883 * Go through the buffer list. When a buffer doesn't have a window yet,
4884 * open one. Otherwise move the window to the right position.
4885 * Watch out for autocommands that delete buffers or windows!
4886 */
4887#ifdef FEAT_AUTOCMD
4888 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4889 ++autocmd_no_enter;
4890#endif
4891 win_enter(lastwin, FALSE);
4892#ifdef FEAT_AUTOCMD
4893 ++autocmd_no_leave;
4894#endif
4895 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4896 {
4897 /* Check if this buffer needs a window */
4898 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4899 continue;
4900
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004901#ifdef FEAT_WINDOWS
4902 if (had_tab != 0)
4903 {
4904 /* With the ":tab" modifier don't move the window. */
4905 if (buf->b_nwindows > 0)
4906 wp = lastwin; /* buffer has a window, skip it */
4907 else
4908 wp = NULL;
4909 }
4910 else
4911#endif
4912 {
4913 /* Check if this buffer already has a window */
4914 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4915 if (wp->w_buffer == buf)
4916 break;
4917 /* If the buffer already has a window, move it */
4918 if (wp != NULL)
4919 win_move_after(wp, curwin);
4920 }
4921
4922 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
4924 /* Split the window and put the buffer in it */
4925 p_ea_save = p_ea;
4926 p_ea = TRUE; /* use space from all windows */
4927 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4928 ++open_wins;
4929 p_ea = p_ea_save;
4930 if (split_ret == FAIL)
4931 continue;
4932
4933 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004934#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 swap_exists_action = SEA_DIALOG;
4936#endif
4937 set_curbuf(buf, DOBUF_GOTO);
4938#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004941#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 break;
4945 }
4946#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004947#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (swap_exists_action == SEA_QUIT)
4949 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004950# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4951 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004952
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004953 /* Reset the error/interrupt/exception state here so that
4954 * aborting() returns FALSE when closing a window. */
4955 enter_cleanup(&cs);
4956# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004957
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004958 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 win_close(curwin, TRUE);
4960 --open_wins;
4961 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004962 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004963
4964# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4965 /* Restore the error/interrupt/exception state if not
4966 * discarded by a new aborting error, interrupt, or uncaught
4967 * exception. */
4968 leave_cleanup(&cs);
4969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
4971 else
4972 handle_swap_exists(NULL);
4973#endif
4974 }
4975
4976 ui_breakcheck();
4977 if (got_int)
4978 {
4979 (void)vgetc(); /* only break the file loading, not the rest */
4980 break;
4981 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004982#ifdef FEAT_EVAL
4983 /* Autocommands deleted the buffer or aborted script processing!!! */
4984 if (aborting())
4985 break;
4986#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004987#ifdef FEAT_WINDOWS
4988 /* When ":tab" was used open a new tab for a new window repeatedly. */
4989 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4990 cmdmod.tab = 9999;
4991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993#ifdef FEAT_AUTOCMD
4994 --autocmd_no_enter;
4995#endif
4996 win_enter(firstwin, FALSE); /* back to first window */
4997#ifdef FEAT_AUTOCMD
4998 --autocmd_no_leave;
4999#endif
5000
5001 /*
5002 * Close superfluous windows.
5003 */
5004 for (wp = lastwin; open_wins > count; )
5005 {
5006 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5007 || autowrite(wp->w_buffer, FALSE) == OK);
5008#ifdef FEAT_AUTOCMD
5009 if (!win_valid(wp))
5010 {
5011 /* BufWrite Autocommands made the window invalid, start over */
5012 wp = lastwin;
5013 }
5014 else
5015#endif
5016 if (r)
5017 {
5018 win_close(wp, !P_HID(wp->w_buffer));
5019 --open_wins;
5020 wp = lastwin;
5021 }
5022 else
5023 {
5024 wp = wp->w_prev;
5025 if (wp == NULL)
5026 break;
5027 }
5028 }
5029}
5030# endif /* FEAT_LISTCMDS */
5031
5032#endif /* FEAT_WINDOWS */
5033
Bram Moolenaara3227e22006-03-08 21:32:40 +00005034static int chk_modeline __ARGS((linenr_T, int));
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036/*
5037 * do_modelines() - process mode lines for the current file
5038 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005039 * "flags" can be:
5040 * OPT_WINONLY only set options local to window
5041 * OPT_NOWIN don't set options local to window
5042 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 * Returns immediately if the "ml" option isn't set.
5044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00005046do_modelines(flags)
5047 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005049 linenr_T lnum;
5050 int nmlines;
5051 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
5053 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5054 return;
5055
5056 /* Disallow recursive entry here. Can happen when executing a modeline
5057 * triggers an autocommand, which reloads modelines with a ":do". */
5058 if (entered)
5059 return;
5060
5061 ++entered;
5062 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5063 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005064 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 nmlines = 0;
5066
5067 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5068 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005069 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 nmlines = 0;
5071 --entered;
5072}
5073
5074#include "version.h" /* for version number */
5075
5076/*
5077 * chk_modeline() - check a single line for a mode string
5078 * Return FAIL if an error encountered.
5079 */
5080 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005081chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005083 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
5085 char_u *s;
5086 char_u *e;
5087 char_u *linecopy; /* local copy of any modeline found */
5088 int prev;
5089 int vers;
5090 int end;
5091 int retval = OK;
5092 char_u *save_sourcing_name;
5093 linenr_T save_sourcing_lnum;
5094#ifdef FEAT_EVAL
5095 scid_T save_SID;
5096#endif
5097
5098 prev = -1;
5099 for (s = ml_get(lnum); *s != NUL; ++s)
5100 {
5101 if (prev == -1 || vim_isspace(prev))
5102 {
5103 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5104 || STRNCMP(s, "vi:", (size_t)3) == 0)
5105 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005106 /* Accept both "vim" and "Vim". */
5107 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
5109 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5110 e = s + 4;
5111 else
5112 e = s + 3;
5113 vers = getdigits(&e);
5114 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005115 && (s[0] != 'V'
5116 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 && (s[3] == ':'
5118 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5119 || (VIM_VERSION_100 < vers && s[3] == '<')
5120 || (VIM_VERSION_100 > vers && s[3] == '>')
5121 || (VIM_VERSION_100 == vers && s[3] == '=')))
5122 break;
5123 }
5124 }
5125 prev = *s;
5126 }
5127
5128 if (*s)
5129 {
5130 do /* skip over "ex:", "vi:" or "vim:" */
5131 ++s;
5132 while (s[-1] != ':');
5133
5134 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5135 if (linecopy == NULL)
5136 return FAIL;
5137
5138 save_sourcing_lnum = sourcing_lnum;
5139 save_sourcing_name = sourcing_name;
5140 sourcing_lnum = lnum; /* prepare for emsg() */
5141 sourcing_name = (char_u *)"modelines";
5142
5143 end = FALSE;
5144 while (end == FALSE)
5145 {
5146 s = skipwhite(s);
5147 if (*s == NUL)
5148 break;
5149
5150 /*
5151 * Find end of set command: ':' or end of line.
5152 * Skip over "\:", replacing it with ":".
5153 */
5154 for (e = s; *e != ':' && *e != NUL; ++e)
5155 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005156 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (*e == NUL)
5158 end = TRUE;
5159
5160 /*
5161 * If there is a "set" command, require a terminating ':' and
5162 * ignore the stuff after the ':'.
5163 * "vi:set opt opt opt: foo" -- foo not interpreted
5164 * "vi:opt opt opt: foo" -- foo interpreted
5165 * Accept "se" for compatibility with Elvis.
5166 */
5167 if (STRNCMP(s, "set ", (size_t)4) == 0
5168 || STRNCMP(s, "se ", (size_t)3) == 0)
5169 {
5170 if (*e != ':') /* no terminating ':'? */
5171 break;
5172 end = TRUE;
5173 s = vim_strchr(s, ' ') + 1;
5174 }
5175 *e = NUL; /* truncate the set command */
5176
5177 if (*s != NUL) /* skip over an empty "::" */
5178 {
5179#ifdef FEAT_EVAL
5180 save_SID = current_SID;
5181 current_SID = SID_MODELINE;
5182#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005183 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184#ifdef FEAT_EVAL
5185 current_SID = save_SID;
5186#endif
5187 if (retval == FAIL) /* stop if error found */
5188 break;
5189 }
5190 s = e + 1; /* advance to next part */
5191 }
5192
5193 sourcing_lnum = save_sourcing_lnum;
5194 sourcing_name = save_sourcing_name;
5195
5196 vim_free(linecopy);
5197 }
5198 return retval;
5199}
5200
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005201#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 int
5203read_viminfo_bufferlist(virp, writing)
5204 vir_T *virp;
5205 int writing;
5206{
5207 char_u *tab;
5208 linenr_T lnum;
5209 colnr_T col;
5210 buf_T *buf;
5211 char_u *sfname;
5212 char_u *xline;
5213
5214 /* Handle long line and escaped characters. */
5215 xline = viminfo_readstring(virp, 1, FALSE);
5216
5217 /* don't read in if there are files on the command-line or if writing: */
5218 if (xline != NULL && !writing && ARGCOUNT == 0
5219 && find_viminfo_parameter('%') != NULL)
5220 {
5221 /* Format is: <fname> Tab <lnum> Tab <col>.
5222 * Watch out for a Tab in the file name, work from the end. */
5223 lnum = 0;
5224 col = 0;
5225 tab = vim_strrchr(xline, '\t');
5226 if (tab != NULL)
5227 {
5228 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005229 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 tab = vim_strrchr(xline, '\t');
5231 if (tab != NULL)
5232 {
5233 *tab++ = '\0';
5234 lnum = atol((char *)tab);
5235 }
5236 }
5237
5238 /* Expand "~/" in the file name at "line + 1" to a full path.
5239 * Then try shortening it by comparing with the current directory */
5240 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005241 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
5243 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5244 if (buf != NULL) /* just in case... */
5245 {
5246 buf->b_last_cursor.lnum = lnum;
5247 buf->b_last_cursor.col = col;
5248 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5249 }
5250 }
5251 vim_free(xline);
5252
5253 return viminfo_readline(virp);
5254}
5255
5256 void
5257write_viminfo_bufferlist(fp)
5258 FILE *fp;
5259{
5260 buf_T *buf;
5261#ifdef FEAT_WINDOWS
5262 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005263 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264#endif
5265 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005266 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
5268 if (find_viminfo_parameter('%') == NULL)
5269 return;
5270
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005271 /* Without a number -1 is returned: do all buffers. */
5272 max_buffers = get_viminfo_parameter('%');
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005275#define LINE_BUF_LEN (MAXPATHL + 40)
5276 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 if (line == NULL)
5278 return;
5279
5280#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005281 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 set_last_cursor(win);
5283#else
5284 set_last_cursor(curwin);
5285#endif
5286
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005287 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5289 {
5290 if (buf->b_fname == NULL
5291 || !buf->b_p_bl
5292#ifdef FEAT_QUICKFIX
5293 || bt_quickfix(buf)
5294#endif
5295 || removable(buf->b_ffname))
5296 continue;
5297
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005298 if (max_buffers-- == 0)
5299 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 putc('%', fp);
5301 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005302 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 (long)buf->b_last_cursor.lnum,
5304 buf->b_last_cursor.col);
5305 viminfo_writestring(fp, line);
5306 }
5307 vim_free(line);
5308}
5309#endif
5310
5311
5312/*
5313 * Return special buffer name.
5314 * Returns NULL when the buffer has a normal file name.
5315 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005316 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317buf_spname(buf)
5318 buf_T *buf;
5319{
5320#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5321 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005322 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005323 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005324 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005325
5326 /*
5327 * For location list window, w_llist_ref points to the location list.
5328 * For quickfix window, w_llist_ref is NULL.
5329 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005330 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005331 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005332 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005333 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335#endif
5336#ifdef FEAT_QUICKFIX
5337 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5338 * contains the name as specified by the user */
5339 if (bt_nofile(buf))
5340 {
5341 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005342 return buf->b_sfname;
5343 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345#endif
5346 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005347 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 return NULL;
5349}
5350
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005351#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5352 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5353 || defined(PROTO)
5354/*
5355 * Find a window for buffer "buf".
5356 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5357 * If not found FAIL is returned.
5358 */
5359 int
5360find_win_for_buf(buf, wp, tp)
5361 buf_T *buf;
5362 win_T **wp;
5363 tabpage_T **tp;
5364{
5365 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5366 if ((*wp)->w_buffer == buf)
5367 goto win_found;
5368 return FAIL;
5369win_found:
5370 return OK;
5371}
5372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
5374#if defined(FEAT_SIGNS) || defined(PROTO)
5375/*
5376 * Insert the sign into the signlist.
5377 */
5378 static void
5379insert_sign(buf, prev, next, id, lnum, typenr)
5380 buf_T *buf; /* buffer to store sign in */
5381 signlist_T *prev; /* previous sign entry */
5382 signlist_T *next; /* next sign entry */
5383 int id; /* sign ID */
5384 linenr_T lnum; /* line number which gets the mark */
5385 int typenr; /* typenr of sign we are adding */
5386{
5387 signlist_T *newsign;
5388
5389 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5390 if (newsign != NULL)
5391 {
5392 newsign->id = id;
5393 newsign->lnum = lnum;
5394 newsign->typenr = typenr;
5395 newsign->next = next;
5396#ifdef FEAT_NETBEANS_INTG
5397 newsign->prev = prev;
5398 if (next != NULL)
5399 next->prev = newsign;
5400#endif
5401
5402 if (prev == NULL)
5403 {
5404 /* When adding first sign need to redraw the windows to create the
5405 * column for signs. */
5406 if (buf->b_signlist == NULL)
5407 {
5408 redraw_buf_later(buf, NOT_VALID);
5409 changed_cline_bef_curs();
5410 }
5411
5412 /* first sign in signlist */
5413 buf->b_signlist = newsign;
5414 }
5415 else
5416 prev->next = newsign;
5417 }
5418}
5419
5420/*
5421 * Add the sign into the signlist. Find the right spot to do it though.
5422 */
5423 void
5424buf_addsign(buf, id, lnum, typenr)
5425 buf_T *buf; /* buffer to store sign in */
5426 int id; /* sign ID */
5427 linenr_T lnum; /* line number which gets the mark */
5428 int typenr; /* typenr of sign we are adding */
5429{
5430 signlist_T *sign; /* a sign in the signlist */
5431 signlist_T *prev; /* the previous sign */
5432
5433 prev = NULL;
5434 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5435 {
5436 if (lnum == sign->lnum && id == sign->id)
5437 {
5438 sign->typenr = typenr;
5439 return;
5440 }
5441 else if (
5442#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5443 id < 0 &&
5444#endif
5445 lnum < sign->lnum)
5446 {
5447#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5448 /* XXX - GRP: Is this because of sign slide problem? Or is it
5449 * really needed? Or is it because we allow multiple signs per
5450 * line? If so, should I add that feature to FEAT_SIGNS?
5451 */
5452 while (prev != NULL && prev->lnum == lnum)
5453 prev = prev->prev;
5454 if (prev == NULL)
5455 sign = buf->b_signlist;
5456 else
5457 sign = prev->next;
5458#endif
5459 insert_sign(buf, prev, sign, id, lnum, typenr);
5460 return;
5461 }
5462 prev = sign;
5463 }
5464#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5465 /* XXX - GRP: See previous comment */
5466 while (prev != NULL && prev->lnum == lnum)
5467 prev = prev->prev;
5468 if (prev == NULL)
5469 sign = buf->b_signlist;
5470 else
5471 sign = prev->next;
5472#endif
5473 insert_sign(buf, prev, sign, id, lnum, typenr);
5474
5475 return;
5476}
5477
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005478 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479buf_change_sign_type(buf, markId, typenr)
5480 buf_T *buf; /* buffer to store sign in */
5481 int markId; /* sign ID */
5482 int typenr; /* typenr of sign we are adding */
5483{
5484 signlist_T *sign; /* a sign in the signlist */
5485
5486 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5487 {
5488 if (sign->id == markId)
5489 {
5490 sign->typenr = typenr;
5491 return sign->lnum;
5492 }
5493 }
5494
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005495 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496}
5497
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005498 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499buf_getsigntype(buf, lnum, type)
5500 buf_T *buf;
5501 linenr_T lnum;
5502 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5503{
5504 signlist_T *sign; /* a sign in a b_signlist */
5505
5506 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5507 if (sign->lnum == lnum
5508 && (type == SIGN_ANY
5509# ifdef FEAT_SIGN_ICONS
5510 || (type == SIGN_ICON
5511 && sign_get_image(sign->typenr) != NULL)
5512# endif
5513 || (type == SIGN_TEXT
5514 && sign_get_text(sign->typenr) != NULL)
5515 || (type == SIGN_LINEHL
5516 && sign_get_attr(sign->typenr, TRUE) != 0)))
5517 return sign->typenr;
5518 return 0;
5519}
5520
5521
5522 linenr_T
5523buf_delsign(buf, id)
5524 buf_T *buf; /* buffer sign is stored in */
5525 int id; /* sign id */
5526{
5527 signlist_T **lastp; /* pointer to pointer to current sign */
5528 signlist_T *sign; /* a sign in a b_signlist */
5529 signlist_T *next; /* the next sign in a b_signlist */
5530 linenr_T lnum; /* line number whose sign was deleted */
5531
5532 lastp = &buf->b_signlist;
5533 lnum = 0;
5534 for (sign = buf->b_signlist; sign != NULL; sign = next)
5535 {
5536 next = sign->next;
5537 if (sign->id == id)
5538 {
5539 *lastp = next;
5540#ifdef FEAT_NETBEANS_INTG
5541 if (next != NULL)
5542 next->prev = sign->prev;
5543#endif
5544 lnum = sign->lnum;
5545 vim_free(sign);
5546 break;
5547 }
5548 else
5549 lastp = &sign->next;
5550 }
5551
5552 /* When deleted the last sign need to redraw the windows to remove the
5553 * sign column. */
5554 if (buf->b_signlist == NULL)
5555 {
5556 redraw_buf_later(buf, NOT_VALID);
5557 changed_cline_bef_curs();
5558 }
5559
5560 return lnum;
5561}
5562
5563
5564/*
5565 * Find the line number of the sign with the requested id. If the sign does
5566 * not exist, return 0 as the line number. This will still let the correct file
5567 * get loaded.
5568 */
5569 int
5570buf_findsign(buf, id)
5571 buf_T *buf; /* buffer to store sign in */
5572 int id; /* sign ID */
5573{
5574 signlist_T *sign; /* a sign in the signlist */
5575
5576 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5577 if (sign->id == id)
5578 return sign->lnum;
5579
5580 return 0;
5581}
5582
5583 int
5584buf_findsign_id(buf, lnum)
5585 buf_T *buf; /* buffer whose sign we are searching for */
5586 linenr_T lnum; /* line number of sign */
5587{
5588 signlist_T *sign; /* a sign in the signlist */
5589
5590 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5591 if (sign->lnum == lnum)
5592 return sign->id;
5593
5594 return 0;
5595}
5596
5597
5598# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5599/* see if a given type of sign exists on a specific line */
5600 int
5601buf_findsigntype_id(buf, lnum, typenr)
5602 buf_T *buf; /* buffer whose sign we are searching for */
5603 linenr_T lnum; /* line number of sign */
5604 int typenr; /* sign type number */
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->lnum == lnum && sign->typenr == typenr)
5610 return sign->id;
5611
5612 return 0;
5613}
5614
5615
5616# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5617/* return the number of icons on the given line */
5618 int
5619buf_signcount(buf, lnum)
5620 buf_T *buf;
5621 linenr_T lnum;
5622{
5623 signlist_T *sign; /* a sign in the signlist */
5624 int count = 0;
5625
5626 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5627 if (sign->lnum == lnum)
5628 if (sign_get_image(sign->typenr) != NULL)
5629 count++;
5630
5631 return count;
5632}
5633# endif /* FEAT_SIGN_ICONS */
5634# endif /* FEAT_NETBEANS_INTG */
5635
5636
5637/*
5638 * Delete signs in buffer "buf".
5639 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005640 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641buf_delete_signs(buf)
5642 buf_T *buf;
5643{
5644 signlist_T *next;
5645
5646 while (buf->b_signlist != NULL)
5647 {
5648 next = buf->b_signlist->next;
5649 vim_free(buf->b_signlist);
5650 buf->b_signlist = next;
5651 }
5652}
5653
5654/*
5655 * Delete all signs in all buffers.
5656 */
5657 void
5658buf_delete_all_signs()
5659{
5660 buf_T *buf; /* buffer we are checking for signs */
5661
5662 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5663 if (buf->b_signlist != NULL)
5664 {
5665 /* Need to redraw the windows to remove the sign column. */
5666 redraw_buf_later(buf, NOT_VALID);
5667 buf_delete_signs(buf);
5668 }
5669}
5670
5671/*
5672 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5673 */
5674 void
5675sign_list_placed(rbuf)
5676 buf_T *rbuf;
5677{
5678 buf_T *buf;
5679 signlist_T *p;
5680 char lbuf[BUFSIZ];
5681
5682 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5683 msg_putchar('\n');
5684 if (rbuf == NULL)
5685 buf = firstbuf;
5686 else
5687 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005688 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
5690 if (buf->b_signlist != NULL)
5691 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005692 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5694 msg_putchar('\n');
5695 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005696 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005698 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5700 MSG_PUTS(lbuf);
5701 msg_putchar('\n');
5702 }
5703 if (rbuf != NULL)
5704 break;
5705 buf = buf->b_next;
5706 }
5707}
5708
5709/*
5710 * Adjust a placed sign for inserted/deleted lines.
5711 */
5712 void
5713sign_mark_adjust(line1, line2, amount, amount_after)
5714 linenr_T line1;
5715 linenr_T line2;
5716 long amount;
5717 long amount_after;
5718{
5719 signlist_T *sign; /* a sign in a b_signlist */
5720
5721 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5722 {
5723 if (sign->lnum >= line1 && sign->lnum <= line2)
5724 {
5725 if (amount == MAXLNUM)
5726 sign->lnum = line1;
5727 else
5728 sign->lnum += amount;
5729 }
5730 else if (sign->lnum > line2)
5731 sign->lnum += amount_after;
5732 }
5733}
5734#endif /* FEAT_SIGNS */
5735
5736/*
5737 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5738 */
5739 void
5740set_buflisted(on)
5741 int on;
5742{
5743 if (on != curbuf->b_p_bl)
5744 {
5745 curbuf->b_p_bl = on;
5746#ifdef FEAT_AUTOCMD
5747 if (on)
5748 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5749 else
5750 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5751#endif
5752 }
5753}
5754
5755/*
5756 * Read the file for "buf" again and check if the contents changed.
5757 * Return TRUE if it changed or this could not be checked.
5758 */
5759 int
5760buf_contents_changed(buf)
5761 buf_T *buf;
5762{
5763 buf_T *newbuf;
5764 int differ = TRUE;
5765 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 exarg_T ea;
5768
5769 /* Allocate a buffer without putting it in the buffer list. */
5770 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5771 if (newbuf == NULL)
5772 return TRUE;
5773
5774 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5775 if (prep_exarg(&ea, buf) == FAIL)
5776 {
5777 wipe_buffer(newbuf, FALSE);
5778 return TRUE;
5779 }
5780
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 /* set curwin/curbuf to buf and save a few things */
5782 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
Bram Moolenaar4770d092006-01-12 23:22:24 +00005784 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 && readfile(buf->b_ffname, buf->b_fname,
5786 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5787 &ea, READ_NEW | READ_DUMMY) == OK)
5788 {
5789 /* compare the two files line by line */
5790 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5791 {
5792 differ = FALSE;
5793 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5794 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5795 {
5796 differ = TRUE;
5797 break;
5798 }
5799 }
5800 }
5801 vim_free(ea.cmd);
5802
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 /* restore curwin/curbuf and a few other things */
5804 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 if (curbuf != newbuf) /* safety check */
5807 wipe_buffer(newbuf, FALSE);
5808
5809 return differ;
5810}
5811
5812/*
5813 * Wipe out a buffer and decrement the last buffer number if it was used for
5814 * this buffer. Call this to wipe out a temp buffer that does not contain any
5815 * marks.
5816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 void
5818wipe_buffer(buf, aucmd)
5819 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005820 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
5822 if (buf->b_fnum == top_file_num - 1)
5823 --top_file_num;
5824
5825#ifdef FEAT_AUTOCMD
5826 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005827 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005829 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#ifdef FEAT_AUTOCMD
5831 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005832 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#endif
5834}