blob: 5c63899c83009b8937208f87fc83baa3b7603010 [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)
214 (void)buf_init_chartab(curbuf, FALSE);
215
216 /*
217 * Set/reset the Changed flag first, autocmds may change the buffer.
218 * Apply the automatic commands, before processing the modelines.
219 * So the modelines have priority over auto commands.
220 */
221 /* When reading stdin, the buffer contents always needs writing, so set
222 * the changed flag. Unless in readonly mode: "ls | gview -".
223 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000224 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifdef FEAT_AUTOCMD
226 || modified_was_set /* ":set modified" used in autocmd */
227# ifdef FEAT_EVAL
228 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
229# endif
230#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000231 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000233 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 unchanged(curbuf, FALSE);
235 save_file_ff(curbuf); /* keep this fileformat */
236
237 /* require "!" to overwrite the file, because it wasn't read completely */
238#ifdef FEAT_EVAL
239 if (aborting())
240#else
241 if (got_int)
242#endif
243 curbuf->b_flags |= BF_READERR;
244
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000245#ifdef FEAT_FOLDING
246 /* Need to update automatic folding. Do this before the autocommands,
247 * they may use the fold info. */
248 foldUpdateAll(curwin);
249#endif
250
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_AUTOCMD
252 /* need to set w_topline, unless some autocommand already did that. */
253 if (!(curwin->w_valid & VALID_TOPLINE))
254 {
255 curwin->w_topline = 1;
256# ifdef FEAT_DIFF
257 curwin->w_topfill = 0;
258# endif
259 }
260# ifdef FEAT_EVAL
261 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
262# else
263 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
264# endif
265#endif
266
267 if (retval != FAIL)
268 {
269#ifdef FEAT_AUTOCMD
270 /*
271 * The autocommands may have changed the current buffer. Apply the
272 * modelines to the correct buffer, if it still exists and is loaded.
273 */
274 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
275 {
276 aco_save_T aco;
277
278 /* Go to the buffer that was opened. */
279 aucmd_prepbuf(&aco, old_curbuf);
280#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000281 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
283
284#ifdef FEAT_AUTOCMD
285# ifdef FEAT_EVAL
286 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
287 &retval);
288# else
289 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
290# endif
291
292 /* restore curwin/curbuf and a few other things */
293 aucmd_restbuf(&aco);
294 }
295#endif
296 }
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 return retval;
299}
300
301/*
302 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
303 */
304 int
305buf_valid(buf)
306 buf_T *buf;
307{
308 buf_T *bp;
309
310 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
311 if (bp == buf)
312 return TRUE;
313 return FALSE;
314}
315
316/*
317 * Close the link to a buffer.
318 * "action" is used when there is no longer a window for the buffer.
319 * It can be:
320 * 0 buffer becomes hidden
321 * DOBUF_UNLOAD buffer is unloaded
322 * DOBUF_DELETE buffer is unloaded and removed from buffer list
323 * DOBUF_WIPE buffer is unloaded and really deleted
324 * When doing all but the first one on the current buffer, the caller should
325 * get a new buffer very soon!
326 *
327 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100328 *
329 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
330 * cause there to be only one window with this buffer. e.g. when ":quit" is
331 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 */
333 void
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100334close_buffer(win, buf, action, abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 win_T *win; /* if not NULL, set b_last_cursor */
336 buf_T *buf;
337 int action;
Bram Moolenaar5fbe6992012-03-07 22:52:36 +0100338 int abort_if_last UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340#ifdef FEAT_AUTOCMD
341 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100342 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#endif
344 int unload_buf = (action != 0);
345 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
346 int wipe_buf = (action == DOBUF_WIPE);
347
348#ifdef FEAT_QUICKFIX
349 /*
350 * Force unloading or deleting when 'bufhidden' says so.
351 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
352 * "hide" (otherwise we could never free or delete a buffer).
353 */
354 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
355 {
356 del_buf = TRUE;
357 unload_buf = TRUE;
358 }
359 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
360 {
361 del_buf = TRUE;
362 unload_buf = TRUE;
363 wipe_buf = TRUE;
364 }
365 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
366 unload_buf = TRUE;
367#endif
368
369 if (win != NULL)
370 {
371 /* Set b_last_cursor when closing the last window for the buffer.
372 * Remember the last cursor position and window options of the buffer.
373 * This used to be only for the current window, but then options like
374 * 'foldmethod' may be lost with a ":only" command. */
375 if (buf->b_nwindows == 1)
376 set_last_cursor(win);
377 buflist_setfpos(buf, win,
378 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
379 win->w_cursor.col, TRUE);
380 }
381
382#ifdef FEAT_AUTOCMD
383 /* When the buffer is no longer in a window, trigger BufWinLeave */
384 if (buf->b_nwindows == 1)
385 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200386 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
388 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200389 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100390 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200391 /* Autocommands deleted the buffer. */
392aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100393 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100395 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200396 buf->b_closing = FALSE;
397 if (abort_if_last && one_window())
398 /* Autocommands made this the only window. */
399 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400
401 /* When the buffer becomes hidden, but is not unloaded, trigger
402 * BufHidden */
403 if (!unload_buf)
404 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200405 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
407 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200408 if (!buf_valid(buf))
409 /* Autocommands deleted the buffer. */
410 goto aucmd_abort;
411 buf->b_closing = FALSE;
412 if (abort_if_last && one_window())
413 /* Autocommands made this the only window. */
414 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 }
416# ifdef FEAT_EVAL
417 if (aborting()) /* autocmds may abort script processing */
418 return;
419# endif
420 }
421 nwindows = buf->b_nwindows;
422#endif
423
424 /* decrease the link count from windows (unless not in any window) */
425 if (buf->b_nwindows > 0)
426 --buf->b_nwindows;
427
428 /* Return when a window is displaying the buffer or when it's not
429 * unloaded. */
430 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
433 /* Always remove the buffer when there is no file name. */
434 if (buf->b_ffname == NULL)
435 del_buf = TRUE;
436
437 /*
438 * Free all things allocated for this buffer.
439 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
440 */
441#ifdef FEAT_AUTOCMD
442 /* Remember if we are closing the current buffer. Restore the number of
443 * windows, so that autocommands in buf_freeall() don't get confused. */
444 is_curbuf = (buf == curbuf);
445 buf->b_nwindows = nwindows;
446#endif
447
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200448 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200449 if (
450#ifdef FEAT_WINDOWS
451 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100452#else
453 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200454#endif
455 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200456 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457
458#ifdef FEAT_AUTOCMD
459 /* Autocommands may have deleted the buffer. */
460 if (!buf_valid(buf))
461 return;
462# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000463 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 return;
465# endif
466
467 /* Autocommands may have opened or closed windows for this buffer.
468 * Decrement the count for the close we do here. */
469 if (buf->b_nwindows > 0)
470 --buf->b_nwindows;
471
472 /*
473 * It's possible that autocommands change curbuf to the one being deleted.
474 * This might cause the previous curbuf to be deleted unexpectedly. But
475 * in some cases it's OK to delete the curbuf, because a new one is
476 * obtained anyway. Therefore only return if curbuf changed to the
477 * deleted buffer.
478 */
479 if (buf == curbuf && !is_curbuf)
480 return;
481#endif
482
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000483 /* Change directories when the 'acd' option is set. */
484 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485
486 /*
487 * Remove the buffer from the list.
488 */
489 if (wipe_buf)
490 {
491#ifdef FEAT_SUN_WORKSHOP
492 if (usingSunWorkShop)
493 workshop_file_closed_lineno((char *)buf->b_ffname,
494 (int)buf->b_last_cursor.lnum);
495#endif
496 vim_free(buf->b_ffname);
497 vim_free(buf->b_sfname);
498 if (buf->b_prev == NULL)
499 firstbuf = buf->b_next;
500 else
501 buf->b_prev->b_next = buf->b_next;
502 if (buf->b_next == NULL)
503 lastbuf = buf->b_prev;
504 else
505 buf->b_next->b_prev = buf->b_prev;
506 free_buffer(buf);
507 }
508 else
509 {
510 if (del_buf)
511 {
512 /* Free all internal variables and reset option values, to make
513 * ":bdel" compatible with Vim 5.7. */
514 free_buffer_stuff(buf, TRUE);
515
516 /* Make it look like a new buffer. */
517 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
518
519 /* Init the options when loaded again. */
520 buf->b_p_initialized = FALSE;
521 }
522 buf_clear_file(buf);
523 if (del_buf)
524 buf->b_p_bl = FALSE;
525 }
526}
527
528/*
529 * Make buffer not contain a file.
530 */
531 void
532buf_clear_file(buf)
533 buf_T *buf;
534{
535 buf->b_ml.ml_line_count = 1;
536 unchanged(buf, TRUE);
537#ifndef SHORT_FNAME
538 buf->b_shortname = FALSE;
539#endif
540 buf->b_p_eol = TRUE;
541 buf->b_start_eol = TRUE;
542#ifdef FEAT_MBYTE
543 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000544 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545#endif
546 buf->b_ml.ml_mfp = NULL;
547 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
548#ifdef FEAT_NETBEANS_INTG
549 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550#endif
551}
552
553/*
554 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200555 * the file. flags:
556 * BFA_DEL buffer is going to be deleted
557 * BFA_WIPE buffer is going to be wiped out
558 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200561buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200563 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
565#ifdef FEAT_AUTOCMD
566 int is_curbuf = (buf == curbuf);
567
Bram Moolenaar362ce482012-06-06 19:02:45 +0200568 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
570 if (!buf_valid(buf)) /* autocommands may delete the buffer */
571 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200572 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
575 if (!buf_valid(buf)) /* autocommands may delete the buffer */
576 return;
577 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200578 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {
580 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
581 FALSE, buf);
582 if (!buf_valid(buf)) /* autocommands may delete the buffer */
583 return;
584 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200585 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000587 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 return;
589# endif
590
591 /*
592 * It's possible that autocommands change curbuf to the one being deleted.
593 * This might cause curbuf to be deleted unexpectedly. But in some cases
594 * it's OK to delete the curbuf, because a new one is obtained anyway.
595 * Therefore only return if curbuf changed to the deleted buffer.
596 */
597 if (buf == curbuf && !is_curbuf)
598 return;
599#endif
600#ifdef FEAT_DIFF
601 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
602#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200603#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100604 /* Remove any ownsyntax, unless exiting. */
605 if (firstwin != NULL && curwin->w_buffer == buf)
606 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200607#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000608
609#ifdef FEAT_FOLDING
610 /* No folds in an empty buffer. */
611# ifdef FEAT_WINDOWS
612 {
613 win_T *win;
614 tabpage_T *tp;
615
616 FOR_ALL_TAB_WINDOWS(tp, win)
617 if (win->w_buffer == buf)
618 clearFolding(win);
619 }
620# else
621 if (curwin->w_buffer == buf)
622 clearFolding(curwin);
623# endif
624#endif
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626#ifdef FEAT_TCL
627 tcl_buffer_free(buf);
628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 ml_close(buf, TRUE); /* close and delete the memline/memfile */
630 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200631 if ((flags & BFA_KEEP_UNDO) == 0)
632 {
633 u_blockfree(buf); /* free the memory allocated for undo */
634 u_clearall(buf); /* reset all undo information */
635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200637 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000639 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640}
641
642/*
643 * Free a buffer structure and the things it contains related to the buffer
644 * itself (not the file, that must have been done already).
645 */
646 static void
647free_buffer(buf)
648 buf_T *buf;
649{
650 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200651#ifdef FEAT_LUA
652 lua_buffer_free(buf);
653#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000654#ifdef FEAT_MZSCHEME
655 mzscheme_buffer_free(buf);
656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657#ifdef FEAT_PERL
658 perl_buf_free(buf);
659#endif
660#ifdef FEAT_PYTHON
661 python_buffer_free(buf);
662#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200663#ifdef FEAT_PYTHON3
664 python3_buffer_free(buf);
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#ifdef FEAT_RUBY
667 ruby_buffer_free(buf);
668#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000669#ifdef FEAT_AUTOCMD
670 aubuflocal_remove(buf);
671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 vim_free(buf);
673}
674
675/*
676 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
677 */
678 static void
679free_buffer_stuff(buf, free_options)
680 buf_T *buf;
681 int free_options; /* free options as well */
682{
683 if (free_options)
684 {
685 clear_wininfo(buf); /* including window-local options */
686 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200687#ifdef FEAT_SPELL
688 ga_clear(&buf->b_s.b_langp);
689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 }
691#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000692 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
693 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#endif
695#ifdef FEAT_USR_CMDS
696 uc_clear(&buf->b_ucmds); /* clear local user commands */
697#endif
698#ifdef FEAT_SIGNS
699 buf_delete_signs(buf); /* delete any signs */
700#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000701#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200702 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#ifdef FEAT_LOCALMAP
705 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
706 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
707#endif
708#ifdef FEAT_MBYTE
709 vim_free(buf->b_start_fenc);
710 buf->b_start_fenc = NULL;
711#endif
712}
713
714/*
715 * Free the b_wininfo list for buffer "buf".
716 */
717 static void
718clear_wininfo(buf)
719 buf_T *buf;
720{
721 wininfo_T *wip;
722
723 while (buf->b_wininfo != NULL)
724 {
725 wip = buf->b_wininfo;
726 buf->b_wininfo = wip->wi_next;
727 if (wip->wi_optset)
728 {
729 clear_winopt(&wip->wi_opt);
730#ifdef FEAT_FOLDING
731 deleteFoldRecurse(&wip->wi_folds);
732#endif
733 }
734 vim_free(wip);
735 }
736}
737
738#if defined(FEAT_LISTCMDS) || defined(PROTO)
739/*
740 * Go to another buffer. Handles the result of the ATTENTION dialog.
741 */
742 void
743goto_buffer(eap, start, dir, count)
744 exarg_T *eap;
745 int start;
746 int dir;
747 int count;
748{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000749# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 buf_T *old_curbuf = curbuf;
751
752 swap_exists_action = SEA_DIALOG;
753# endif
754 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
755 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000756# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
758 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000759# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
760 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000761
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000762 /* Reset the error/interrupt/exception state here so that
763 * aborting() returns FALSE when closing a window. */
764 enter_cleanup(&cs);
765# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000766
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000767 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 win_close(curwin, TRUE);
769 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000770 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000771
772# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
773 /* Restore the error/interrupt/exception state if not discarded by a
774 * new aborting error, interrupt, or uncaught exception. */
775 leave_cleanup(&cs);
776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778 else
779 handle_swap_exists(old_curbuf);
780# endif
781}
782#endif
783
Bram Moolenaare64ac772005-12-07 20:54:59 +0000784#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785/*
786 * Handle the situation of swap_exists_action being set.
787 * It is allowed for "old_curbuf" to be NULL or invalid.
788 */
789 void
790handle_swap_exists(old_curbuf)
791 buf_T *old_curbuf;
792{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000793# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
794 cleanup_T cs;
795# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100796#ifdef FEAT_SYN_HL
797 long old_tw = curbuf->b_p_tw;
798#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (swap_exists_action == SEA_QUIT)
801 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000802# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
803 /* Reset the error/interrupt/exception state here so that
804 * aborting() returns FALSE when closing a buffer. */
805 enter_cleanup(&cs);
806# endif
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 /* User selected Quit at ATTENTION prompt. Go back to previous
809 * buffer. If that buffer is gone or the same as the current one,
810 * open a new, empty buffer. */
811 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000812 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100813 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000815 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100819#ifdef FEAT_SYN_HL
820 if (old_tw != curbuf->b_p_tw)
821 check_colorcolumn(curwin);
822#endif
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000825
826# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
827 /* Restore the error/interrupt/exception state if not discarded by a
828 * new aborting error, interrupt, or uncaught exception. */
829 leave_cleanup(&cs);
830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832 else if (swap_exists_action == SEA_RECOVER)
833 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000834# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
835 /* Reset the error/interrupt/exception state here so that
836 * aborting() returns FALSE when closing a buffer. */
837 enter_cleanup(&cs);
838# endif
839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 /* User selected Recover at ATTENTION prompt. */
841 msg_scroll = TRUE;
842 ml_recover();
843 MSG_PUTS("\n"); /* don't overwrite the last message */
844 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000845 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000846
847# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
848 /* Restore the error/interrupt/exception state if not discarded by a
849 * new aborting error, interrupt, or uncaught exception. */
850 leave_cleanup(&cs);
851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 swap_exists_action = SEA_NONE;
854}
855#endif
856
857#if defined(FEAT_LISTCMDS) || defined(PROTO)
858/*
859 * do_bufdel() - delete or unload buffer(s)
860 *
861 * addr_count == 0: ":bdel" - delete current buffer
862 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
863 * buffer "end_bnr", then any other arguments.
864 * addr_count == 2: ":N,N bdel" - delete buffers in range
865 *
866 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
867 * DOBUF_DEL (":bdel")
868 *
869 * Returns error message or NULL
870 */
871 char_u *
872do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
873 int command;
874 char_u *arg; /* pointer to extra arguments */
875 int addr_count;
876 int start_bnr; /* first buffer number in a range */
877 int end_bnr; /* buffer nr or last buffer nr in a range */
878 int forceit;
879{
880 int do_current = 0; /* delete current buffer? */
881 int deleted = 0; /* number of buffers deleted */
882 char_u *errormsg = NULL; /* return value */
883 int bnr; /* buffer number */
884 char_u *p;
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 if (addr_count == 0)
887 {
888 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
889 }
890 else
891 {
892 if (addr_count == 2)
893 {
894 if (*arg) /* both range and argument is not allowed */
895 return (char_u *)_(e_trailing);
896 bnr = start_bnr;
897 }
898 else /* addr_count == 1 */
899 bnr = end_bnr;
900
901 for ( ;!got_int; ui_breakcheck())
902 {
903 /*
904 * delete the current buffer last, otherwise when the
905 * current buffer is deleted, the next buffer becomes
906 * the current one and will be loaded, which may then
907 * also be deleted, etc.
908 */
909 if (bnr == curbuf->b_fnum)
910 do_current = bnr;
911 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
912 forceit) == OK)
913 ++deleted;
914
915 /*
916 * find next buffer number to delete/unload
917 */
918 if (addr_count == 2)
919 {
920 if (++bnr > end_bnr)
921 break;
922 }
923 else /* addr_count == 1 */
924 {
925 arg = skipwhite(arg);
926 if (*arg == NUL)
927 break;
928 if (!VIM_ISDIGIT(*arg))
929 {
930 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100931 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
932 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (bnr < 0) /* failed */
934 break;
935 arg = p;
936 }
937 else
938 bnr = getdigits(&arg);
939 }
940 }
941 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
942 FORWARD, do_current, forceit) == OK)
943 ++deleted;
944
945 if (deleted == 0)
946 {
947 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000948 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000950 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000952 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 errormsg = IObuff;
954 }
955 else if (deleted >= p_report)
956 {
957 if (command == DOBUF_UNLOAD)
958 {
959 if (deleted == 1)
960 MSG(_("1 buffer unloaded"));
961 else
962 smsg((char_u *)_("%d buffers unloaded"), deleted);
963 }
964 else if (command == DOBUF_DEL)
965 {
966 if (deleted == 1)
967 MSG(_("1 buffer deleted"));
968 else
969 smsg((char_u *)_("%d buffers deleted"), deleted);
970 }
971 else
972 {
973 if (deleted == 1)
974 MSG(_("1 buffer wiped out"));
975 else
976 smsg((char_u *)_("%d buffers wiped out"), deleted);
977 }
978 }
979 }
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982 return errormsg;
983}
984
985/*
986 * Implementation of the commands for the buffer list.
987 *
988 * action == DOBUF_GOTO go to specified buffer
989 * action == DOBUF_SPLIT split window and go to specified buffer
990 * action == DOBUF_UNLOAD unload specified buffer(s)
991 * action == DOBUF_DEL delete specified buffer(s) from buffer list
992 * action == DOBUF_WIPE delete specified buffer(s) really
993 *
994 * start == DOBUF_CURRENT go to "count" buffer from current buffer
995 * start == DOBUF_FIRST go to "count" buffer from first buffer
996 * start == DOBUF_LAST go to "count" buffer from last buffer
997 * start == DOBUF_MOD go to "count" modified buffer from current buffer
998 *
999 * Return FAIL or OK.
1000 */
1001 int
1002do_buffer(action, start, dir, count, forceit)
1003 int action;
1004 int start;
1005 int dir; /* FORWARD or BACKWARD */
1006 int count; /* buffer number or number of buffers */
1007 int forceit; /* TRUE for :...! */
1008{
1009 buf_T *buf;
1010 buf_T *bp;
1011 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1012 || action == DOBUF_WIPE);
1013
1014 switch (start)
1015 {
1016 case DOBUF_FIRST: buf = firstbuf; break;
1017 case DOBUF_LAST: buf = lastbuf; break;
1018 default: buf = curbuf; break;
1019 }
1020 if (start == DOBUF_MOD) /* find next modified buffer */
1021 {
1022 while (count-- > 0)
1023 {
1024 do
1025 {
1026 buf = buf->b_next;
1027 if (buf == NULL)
1028 buf = firstbuf;
1029 }
1030 while (buf != curbuf && !bufIsChanged(buf));
1031 }
1032 if (!bufIsChanged(buf))
1033 {
1034 EMSG(_("E84: No modified buffer found"));
1035 return FAIL;
1036 }
1037 }
1038 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1039 {
1040 while (buf != NULL && buf->b_fnum != count)
1041 buf = buf->b_next;
1042 }
1043 else
1044 {
1045 bp = NULL;
1046 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1047 {
1048 /* remember the buffer where we start, we come back there when all
1049 * buffers are unlisted. */
1050 if (bp == NULL)
1051 bp = buf;
1052 if (dir == FORWARD)
1053 {
1054 buf = buf->b_next;
1055 if (buf == NULL)
1056 buf = firstbuf;
1057 }
1058 else
1059 {
1060 buf = buf->b_prev;
1061 if (buf == NULL)
1062 buf = lastbuf;
1063 }
1064 /* don't count unlisted buffers */
1065 if (unload || buf->b_p_bl)
1066 {
1067 --count;
1068 bp = NULL; /* use this buffer as new starting point */
1069 }
1070 if (bp == buf)
1071 {
1072 /* back where we started, didn't find anything. */
1073 EMSG(_("E85: There is no listed buffer"));
1074 return FAIL;
1075 }
1076 }
1077 }
1078
1079 if (buf == NULL) /* could not find it */
1080 {
1081 if (start == DOBUF_FIRST)
1082 {
1083 /* don't warn when deleting */
1084 if (!unload)
1085 EMSGN(_("E86: Buffer %ld does not exist"), count);
1086 }
1087 else if (dir == FORWARD)
1088 EMSG(_("E87: Cannot go beyond last buffer"));
1089 else
1090 EMSG(_("E88: Cannot go before first buffer"));
1091 return FAIL;
1092 }
1093
1094#ifdef FEAT_GUI
1095 need_mouse_correct = TRUE;
1096#endif
1097
1098#ifdef FEAT_LISTCMDS
1099 /*
1100 * delete buffer buf from memory and/or the list
1101 */
1102 if (unload)
1103 {
1104 int forward;
1105 int retval;
1106
1107 /* When unloading or deleting a buffer that's already unloaded and
1108 * unlisted: fail silently. */
1109 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1110 return FAIL;
1111
1112 if (!forceit && bufIsChanged(buf))
1113 {
1114#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1115 if ((p_confirm || cmdmod.confirm) && p_write)
1116 {
1117 dialog_changed(buf, FALSE);
1118# ifdef FEAT_AUTOCMD
1119 if (!buf_valid(buf))
1120 /* Autocommand deleted buffer, oops! It's not changed
1121 * now. */
1122 return FAIL;
1123# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001124 /* If it's still changed fail silently, the dialog already
1125 * mentioned why it fails. */
1126 if (bufIsChanged(buf))
1127 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001129 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130#endif
1131 {
1132 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1133 buf->b_fnum);
1134 return FAIL;
1135 }
1136 }
1137
1138 /*
1139 * If deleting the last (listed) buffer, make it empty.
1140 * The last (listed) buffer cannot be unloaded.
1141 */
1142 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1143 if (bp->b_p_bl && bp != buf)
1144 break;
1145 if (bp == NULL && buf == curbuf)
1146 {
1147 if (action == DOBUF_UNLOAD)
1148 {
1149 EMSG(_("E90: Cannot unload last buffer"));
1150 return FAIL;
1151 }
1152
1153 /* Close any other windows on this buffer, then make it empty. */
1154#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001155 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156#endif
1157 setpcmark();
1158 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001159 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 /*
1162 * do_ecmd() may create a new buffer, then we have to delete
1163 * the old one. But do_ecmd() may have done that already, check
1164 * if the buffer still exists.
1165 */
1166 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001167 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 return retval;
1169 }
1170
1171#ifdef FEAT_WINDOWS
1172 /*
1173 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001174 * (unless it's the only window). Repeat this so long as we end up in
1175 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001177 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001178# ifdef FEAT_AUTOCMD
1179 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1180# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001181 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 win_close(curwin, FALSE);
1183#endif
1184
1185 /*
1186 * If the buffer to be deleted is not the current one, delete it here.
1187 */
1188 if (buf != curbuf)
1189 {
1190#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001191 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192#endif
1193 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001194 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 return OK;
1196 }
1197
1198 /*
1199 * Deleting the current buffer: Need to find another buffer to go to.
1200 * There must be another, otherwise it would have been handled above.
1201 * First use au_new_curbuf, if it is valid.
1202 * Then prefer the buffer we most recently visited.
1203 * Else try to find one that is loaded, after the current buffer,
1204 * then before the current buffer.
1205 * Finally use any buffer.
1206 */
1207 buf = NULL; /* selected buffer */
1208 bp = NULL; /* used when no loaded buffer found */
1209#ifdef FEAT_AUTOCMD
1210 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1211 buf = au_new_curbuf;
1212# ifdef FEAT_JUMPLIST
1213 else
1214# endif
1215#endif
1216#ifdef FEAT_JUMPLIST
1217 if (curwin->w_jumplistlen > 0)
1218 {
1219 int jumpidx;
1220
1221 jumpidx = curwin->w_jumplistidx - 1;
1222 if (jumpidx < 0)
1223 jumpidx = curwin->w_jumplistlen - 1;
1224
1225 forward = jumpidx;
1226 while (jumpidx != curwin->w_jumplistidx)
1227 {
1228 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1229 if (buf != NULL)
1230 {
1231 if (buf == curbuf || !buf->b_p_bl)
1232 buf = NULL; /* skip current and unlisted bufs */
1233 else if (buf->b_ml.ml_mfp == NULL)
1234 {
1235 /* skip unloaded buf, but may keep it for later */
1236 if (bp == NULL)
1237 bp = buf;
1238 buf = NULL;
1239 }
1240 }
1241 if (buf != NULL) /* found a valid buffer: stop searching */
1242 break;
1243 /* advance to older entry in jump list */
1244 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1245 break;
1246 if (--jumpidx < 0)
1247 jumpidx = curwin->w_jumplistlen - 1;
1248 if (jumpidx == forward) /* List exhausted for sure */
1249 break;
1250 }
1251 }
1252#endif
1253
1254 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1255 {
1256 forward = TRUE;
1257 buf = curbuf->b_next;
1258 for (;;)
1259 {
1260 if (buf == NULL)
1261 {
1262 if (!forward) /* tried both directions */
1263 break;
1264 buf = curbuf->b_prev;
1265 forward = FALSE;
1266 continue;
1267 }
1268 /* in non-help buffer, try to skip help buffers, and vv */
1269 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1270 {
1271 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1272 break;
1273 if (bp == NULL) /* remember unloaded buf for later */
1274 bp = buf;
1275 }
1276 if (forward)
1277 buf = buf->b_next;
1278 else
1279 buf = buf->b_prev;
1280 }
1281 }
1282 if (buf == NULL) /* No loaded buffer, use unloaded one */
1283 buf = bp;
1284 if (buf == NULL) /* No loaded buffer, find listed one */
1285 {
1286 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1287 if (buf->b_p_bl && buf != curbuf)
1288 break;
1289 }
1290 if (buf == NULL) /* Still no buffer, just take one */
1291 {
1292 if (curbuf->b_next != NULL)
1293 buf = curbuf->b_next;
1294 else
1295 buf = curbuf->b_prev;
1296 }
1297 }
1298
1299 /*
1300 * make buf current buffer
1301 */
1302 if (action == DOBUF_SPLIT) /* split window first */
1303 {
1304# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001305 /* If 'switchbuf' contains "useopen": jump to first window containing
1306 * "buf" if one exists */
1307 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001308 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001309 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001310 * page containing "buf" if one exists */
1311 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 return OK;
1313 if (win_split(0, 0) == FAIL)
1314# endif
1315 return FAIL;
1316 }
1317#endif
1318
1319 /* go to current buffer - nothing to do */
1320 if (buf == curbuf)
1321 return OK;
1322
1323 /*
1324 * Check if the current buffer may be abandoned.
1325 */
1326 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1327 {
1328#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1329 if ((p_confirm || cmdmod.confirm) && p_write)
1330 {
1331 dialog_changed(curbuf, FALSE);
1332# ifdef FEAT_AUTOCMD
1333 if (!buf_valid(buf))
1334 /* Autocommand deleted buffer, oops! */
1335 return FAIL;
1336# endif
1337 }
1338 if (bufIsChanged(curbuf))
1339#endif
1340 {
1341 EMSG(_(e_nowrtmsg));
1342 return FAIL;
1343 }
1344 }
1345
1346 /* Go to the other buffer. */
1347 set_curbuf(buf, action);
1348
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001349#if defined(FEAT_LISTCMDS) \
1350 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001352 {
1353 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355#endif
1356
1357#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1358 if (aborting()) /* autocmds may abort script processing */
1359 return FAIL;
1360#endif
1361
1362 return OK;
1363}
1364
1365#endif /* FEAT_LISTCMDS */
1366
1367/*
1368 * Set current buffer to "buf". Executes autocommands and closes current
1369 * buffer. "action" tells how to close the current buffer:
1370 * DOBUF_GOTO free or hide it
1371 * DOBUF_SPLIT nothing
1372 * DOBUF_UNLOAD unload it
1373 * DOBUF_DEL delete it
1374 * DOBUF_WIPE wipe it out
1375 */
1376 void
1377set_curbuf(buf, action)
1378 buf_T *buf;
1379 int action;
1380{
1381 buf_T *prevbuf;
1382 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1383 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001384#ifdef FEAT_SYN_HL
1385 long old_tw = curbuf->b_p_tw;
1386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001389 if (!cmdmod.keepalt)
1390 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001391 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
1393#ifdef FEAT_VISUAL
1394 /* Don't restart Select mode after switching to another buffer. */
1395 VIsual_reselect = FALSE;
1396#endif
1397
1398 /* close_windows() or apply_autocmds() may change curbuf */
1399 prevbuf = curbuf;
1400
1401#ifdef FEAT_AUTOCMD
1402 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1403# ifdef FEAT_EVAL
1404 if (buf_valid(prevbuf) && !aborting())
1405# else
1406 if (buf_valid(prevbuf))
1407# endif
1408#endif
1409 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001410#ifdef FEAT_SYN_HL
1411 if (prevbuf == curwin->w_buffer)
1412 reset_synblock(curwin);
1413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414#ifdef FEAT_WINDOWS
1415 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417#endif
1418#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1419 if (buf_valid(prevbuf) && !aborting())
1420#else
1421 if (buf_valid(prevbuf))
1422#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001423 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001424#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001425 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001426#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001427 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001428 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1430 unload ? action : (action == DOBUF_GOTO
1431 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001432 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001433#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001434 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001435 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001436 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001437#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 }
1440#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001441 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001442 * it did ":bunload") or aborted the script processing!
1443 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1444 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001445# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001446 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001447# endif
1448# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001449 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001450# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001451 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001455#ifdef FEAT_SYN_HL
1456 if (old_tw != curbuf->b_p_tw)
1457 check_colorcolumn(curwin);
1458#endif
1459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460}
1461
1462/*
1463 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001464 * Old curbuf must have been abandoned already! This also means "curbuf" may
1465 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 */
1467 void
1468enter_buffer(buf)
1469 buf_T *buf;
1470{
1471 /* Copy buffer and window local option values. Not for a help buffer. */
1472 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1473 if (!buf->b_help)
1474 get_winopts(buf);
1475#ifdef FEAT_FOLDING
1476 else
1477 /* Remove all folds in the window. */
1478 clearFolding(curwin);
1479 foldUpdateAll(curwin); /* update folds (later). */
1480#endif
1481
1482 /* Get the buffer in the current window. */
1483 curwin->w_buffer = buf;
1484 curbuf = buf;
1485 ++curbuf->b_nwindows;
1486
1487#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001488 if (curwin->w_p_diff)
1489 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491
Bram Moolenaara971b822011-09-14 14:43:25 +02001492#ifdef FEAT_SYN_HL
1493 curwin->w_s = &(buf->b_s);
1494#endif
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /* Cursor on first line by default. */
1497 curwin->w_cursor.lnum = 1;
1498 curwin->w_cursor.col = 0;
1499#ifdef FEAT_VIRTUALEDIT
1500 curwin->w_cursor.coladd = 0;
1501#endif
1502 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001503#ifdef FEAT_AUTOCMD
1504 curwin->w_topline_was_set = FALSE;
1505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001507 /* mark cursor position as being invalid */
1508 curwin->w_valid = 0;
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 /* Make sure the buffer is loaded. */
1511 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001512 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001513#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001514 /* If there is no filetype, allow for detecting one. Esp. useful for
1515 * ":ball" used in a autocommand. If there already is a filetype we
1516 * might prefer to keep it. */
1517 if (*curbuf->b_p_ft == NUL)
1518 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001519#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001520
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001521 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 else
1524 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001525 if (!msg_silent)
1526 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1528#ifdef FEAT_AUTOCMD
1529 curwin->w_topline = 1;
1530# ifdef FEAT_DIFF
1531 curwin->w_topfill = 0;
1532# endif
1533 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1534 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1535#endif
1536 }
1537
1538 /* If autocommands did not change the cursor position, restore cursor lnum
1539 * and possibly cursor col. */
1540 if (curwin->w_cursor.lnum == 1 && inindent(0))
1541 buflist_getfpos();
1542
1543 check_arg_idx(curwin); /* check for valid arg_idx */
1544#ifdef FEAT_TITLE
1545 maketitle();
1546#endif
1547#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001548 /* when autocmds didn't change it */
1549 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#endif
1551 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1552
Bram Moolenaar009b2592004-10-24 19:18:58 +00001553#ifdef FEAT_NETBEANS_INTG
1554 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001555 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001556#endif
1557
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001558 /* Change directories when the 'acd' option is set. */
1559 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
1561#ifdef FEAT_KEYMAP
1562 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001563 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001565#ifdef FEAT_SPELL
1566 /* May need to set the spell language. Can only do this after the buffer
1567 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001568 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1569 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001570#endif
1571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 redraw_later(NOT_VALID);
1573}
1574
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001575#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1576/*
1577 * Change to the directory of the current buffer.
1578 */
1579 void
1580do_autochdir()
1581{
1582 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1583 shorten_fnames(TRUE);
1584}
1585#endif
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587/*
1588 * functions for dealing with the buffer list
1589 */
1590
1591/*
1592 * Add a file name to the buffer list. Return a pointer to the buffer.
1593 * If the same file name already exists return a pointer to that buffer.
1594 * If it does not exist, or if fname == NULL, a new entry is created.
1595 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1596 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1597 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 * This is the ONLY way to create a new buffer.
1599 */
1600static int top_file_num = 1; /* highest file number */
1601
1602 buf_T *
1603buflist_new(ffname, sfname, lnum, flags)
1604 char_u *ffname; /* full path of fname or relative */
1605 char_u *sfname; /* short fname or NULL */
1606 linenr_T lnum; /* preferred cursor line */
1607 int flags; /* BLN_ defines */
1608{
1609 buf_T *buf;
1610#ifdef UNIX
1611 struct stat st;
1612#endif
1613
1614 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1615
1616 /*
1617 * If file name already exists in the list, update the entry.
1618 */
1619#ifdef UNIX
1620 /* On Unix we can use inode numbers when the file exists. Works better
1621 * for hard links. */
1622 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1623 st.st_dev = (dev_T)-1;
1624#endif
1625 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1626#ifdef UNIX
1627 buflist_findname_stat(ffname, &st)
1628#else
1629 buflist_findname(ffname)
1630#endif
1631 ) != NULL)
1632 {
1633 vim_free(ffname);
1634 if (lnum != 0)
1635 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1636 /* copy the options now, if 'cpo' doesn't have 's' and not done
1637 * already */
1638 buf_copy_options(buf, 0);
1639 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1640 {
1641 buf->b_p_bl = TRUE;
1642#ifdef FEAT_AUTOCMD
1643 if (!(flags & BLN_DUMMY))
1644 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1645#endif
1646 }
1647 return buf;
1648 }
1649
1650 /*
1651 * If the current buffer has no name and no contents, use the current
1652 * buffer. Otherwise: Need to allocate a new buffer structure.
1653 *
1654 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001655 * (A spell file buffer is allocated in spell.c, but that's not a normal
1656 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
1658 buf = NULL;
1659 if ((flags & BLN_CURBUF)
1660 && curbuf != NULL
1661 && curbuf->b_ffname == NULL
1662 && curbuf->b_nwindows <= 1
1663 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1664 {
1665 buf = curbuf;
1666#ifdef FEAT_AUTOCMD
1667 /* It's like this buffer is deleted. Watch out for autocommands that
1668 * change curbuf! If that happens, allocate a new buffer anyway. */
1669 if (curbuf->b_p_bl)
1670 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1671 if (buf == curbuf)
1672 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1673# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001674 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 return NULL;
1676# endif
1677#endif
1678#ifdef FEAT_QUICKFIX
1679# ifdef FEAT_AUTOCMD
1680 if (buf == curbuf)
1681# endif
1682 {
1683 /* Make sure 'bufhidden' and 'buftype' are empty */
1684 clear_string_option(&buf->b_p_bh);
1685 clear_string_option(&buf->b_p_bt);
1686 }
1687#endif
1688 }
1689 if (buf != curbuf || curbuf == NULL)
1690 {
1691 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1692 if (buf == NULL)
1693 {
1694 vim_free(ffname);
1695 return NULL;
1696 }
1697 }
1698
1699 if (ffname != NULL)
1700 {
1701 buf->b_ffname = ffname;
1702 buf->b_sfname = vim_strsave(sfname);
1703 }
1704
1705 clear_wininfo(buf);
1706 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1707
1708 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1709 || buf->b_wininfo == NULL)
1710 {
1711 vim_free(buf->b_ffname);
1712 buf->b_ffname = NULL;
1713 vim_free(buf->b_sfname);
1714 buf->b_sfname = NULL;
1715 if (buf != curbuf)
1716 free_buffer(buf);
1717 return NULL;
1718 }
1719
1720 if (buf == curbuf)
1721 {
1722 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001723 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (buf != curbuf) /* autocommands deleted the buffer! */
1725 return NULL;
1726#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001727 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 return NULL;
1729#endif
1730 /* buf->b_nwindows = 0; why was this here? */
1731 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001732
1733 /* Init the options. */
1734 buf->b_p_initialized = FALSE;
1735 buf_copy_options(buf, BCO_ENTER);
1736
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#ifdef FEAT_KEYMAP
1738 /* need to reload lmaps and set b:keymap_name */
1739 curbuf->b_kmap_state |= KEYMAP_INIT;
1740#endif
1741 }
1742 else
1743 {
1744 /*
1745 * put new buffer at the end of the buffer list
1746 */
1747 buf->b_next = NULL;
1748 if (firstbuf == NULL) /* buffer list is empty */
1749 {
1750 buf->b_prev = NULL;
1751 firstbuf = buf;
1752 }
1753 else /* append new buffer at end of list */
1754 {
1755 lastbuf->b_next = buf;
1756 buf->b_prev = lastbuf;
1757 }
1758 lastbuf = buf;
1759
1760 buf->b_fnum = top_file_num++;
1761 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1762 {
1763 EMSG(_("W14: Warning: List of file names overflow"));
1764 if (emsg_silent == 0)
1765 {
1766 out_flush();
1767 ui_delay(3000L, TRUE); /* make sure it is noticed */
1768 }
1769 top_file_num = 1;
1770 }
1771
1772 /*
1773 * Always copy the options from the current buffer.
1774 */
1775 buf_copy_options(buf, BCO_ALWAYS);
1776 }
1777
1778 buf->b_wininfo->wi_fpos.lnum = lnum;
1779 buf->b_wininfo->wi_win = curwin;
1780
1781#ifdef FEAT_EVAL
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001782 /* init b: variables */
1783 init_var_dict(&buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001784#endif
1785#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001786 hash_init(&buf->b_s.b_keywtab);
1787 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
1789
1790 buf->b_fname = buf->b_sfname;
1791#ifdef UNIX
1792 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001793 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 else
1795 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001796 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 buf->b_dev = st.st_dev;
1798 buf->b_ino = st.st_ino;
1799 }
1800#endif
1801 buf->b_u_synced = TRUE;
1802 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001803 if (flags & BLN_DUMMY)
1804 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 buf_clear_file(buf);
1806 clrallmarks(buf); /* clear marks */
1807 fmarks_check_names(buf); /* check file marks for this file */
1808 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1809#ifdef FEAT_AUTOCMD
1810 if (!(flags & BLN_DUMMY))
1811 {
1812 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1813 if (flags & BLN_LISTED)
1814 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1815# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001816 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 return NULL;
1818# endif
1819 }
1820#endif
1821
1822 return buf;
1823}
1824
1825/*
1826 * Free the memory for the options of a buffer.
1827 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1828 * 'fileencoding'.
1829 */
1830 void
1831free_buf_options(buf, free_p_ff)
1832 buf_T *buf;
1833 int free_p_ff;
1834{
1835 if (free_p_ff)
1836 {
1837#ifdef FEAT_MBYTE
1838 clear_string_option(&buf->b_p_fenc);
1839#endif
1840 clear_string_option(&buf->b_p_ff);
1841#ifdef FEAT_QUICKFIX
1842 clear_string_option(&buf->b_p_bh);
1843 clear_string_option(&buf->b_p_bt);
1844#endif
1845 }
1846#ifdef FEAT_FIND_ID
1847 clear_string_option(&buf->b_p_def);
1848 clear_string_option(&buf->b_p_inc);
1849# ifdef FEAT_EVAL
1850 clear_string_option(&buf->b_p_inex);
1851# endif
1852#endif
1853#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1854 clear_string_option(&buf->b_p_inde);
1855 clear_string_option(&buf->b_p_indk);
1856#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001857#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1858 clear_string_option(&buf->b_p_bexpr);
1859#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001860#if defined(FEAT_CRYPT)
1861 clear_string_option(&buf->b_p_cm);
1862#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001863#if defined(FEAT_EVAL)
1864 clear_string_option(&buf->b_p_fex);
1865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866#ifdef FEAT_CRYPT
1867 clear_string_option(&buf->b_p_key);
1868#endif
1869 clear_string_option(&buf->b_p_kp);
1870 clear_string_option(&buf->b_p_mps);
1871 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001872 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 clear_string_option(&buf->b_p_isk);
1874#ifdef FEAT_KEYMAP
1875 clear_string_option(&buf->b_p_keymap);
1876 ga_clear(&buf->b_kmap_ga);
1877#endif
1878#ifdef FEAT_COMMENTS
1879 clear_string_option(&buf->b_p_com);
1880#endif
1881#ifdef FEAT_FOLDING
1882 clear_string_option(&buf->b_p_cms);
1883#endif
1884 clear_string_option(&buf->b_p_nf);
1885#ifdef FEAT_SYN_HL
1886 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001887#endif
1888#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001889 clear_string_option(&buf->b_s.b_p_spc);
1890 clear_string_option(&buf->b_s.b_p_spf);
1891 vim_free(buf->b_s.b_cap_prog);
1892 buf->b_s.b_cap_prog = NULL;
1893 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894#endif
1895#ifdef FEAT_SEARCHPATH
1896 clear_string_option(&buf->b_p_sua);
1897#endif
1898#ifdef FEAT_AUTOCMD
1899 clear_string_option(&buf->b_p_ft);
1900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#ifdef FEAT_CINDENT
1902 clear_string_option(&buf->b_p_cink);
1903 clear_string_option(&buf->b_p_cino);
1904#endif
1905#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1906 clear_string_option(&buf->b_p_cinw);
1907#endif
1908#ifdef FEAT_INS_EXPAND
1909 clear_string_option(&buf->b_p_cpt);
1910#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001911#ifdef FEAT_COMPL_FUNC
1912 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001913 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915#ifdef FEAT_QUICKFIX
1916 clear_string_option(&buf->b_p_gp);
1917 clear_string_option(&buf->b_p_mp);
1918 clear_string_option(&buf->b_p_efm);
1919#endif
1920 clear_string_option(&buf->b_p_ep);
1921 clear_string_option(&buf->b_p_path);
1922 clear_string_option(&buf->b_p_tags);
1923#ifdef FEAT_INS_EXPAND
1924 clear_string_option(&buf->b_p_dict);
1925 clear_string_option(&buf->b_p_tsr);
1926#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001927#ifdef FEAT_TEXTOBJ
1928 clear_string_option(&buf->b_p_qe);
1929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 buf->b_p_ar = -1;
1931}
1932
1933/*
1934 * get alternate file n
1935 * set linenr to lnum or altfpos.lnum if lnum == 0
1936 * also set cursor column to altfpos.col if 'startofline' is not set.
1937 * if (options & GETF_SETMARK) call setpcmark()
1938 * if (options & GETF_ALT) we are jumping to an alternate file.
1939 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1940 *
1941 * return FAIL for failure, OK for success
1942 */
1943 int
1944buflist_getfile(n, lnum, options, forceit)
1945 int n;
1946 linenr_T lnum;
1947 int options;
1948 int forceit;
1949{
1950 buf_T *buf;
1951#ifdef FEAT_WINDOWS
1952 win_T *wp = NULL;
1953#endif
1954 pos_T *fpos;
1955 colnr_T col;
1956
1957 buf = buflist_findnr(n);
1958 if (buf == NULL)
1959 {
1960 if ((options & GETF_ALT) && n == 0)
1961 EMSG(_(e_noalt));
1962 else
1963 EMSGN(_("E92: Buffer %ld not found"), n);
1964 return FAIL;
1965 }
1966
1967 /* if alternate file is the current buffer, nothing to do */
1968 if (buf == curbuf)
1969 return OK;
1970
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001971 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001972 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001973 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001975 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001976#ifdef FEAT_AUTOCMD
1977 if (curbuf_locked())
1978 return FAIL;
1979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 /* altfpos may be changed by getfile(), get it now */
1982 if (lnum == 0)
1983 {
1984 fpos = buflist_findfpos(buf);
1985 lnum = fpos->lnum;
1986 col = fpos->col;
1987 }
1988 else
1989 col = 0;
1990
1991#ifdef FEAT_WINDOWS
1992 if (options & GETF_SWITCH)
1993 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001994 /* If 'switchbuf' contains "useopen": jump to first window containing
1995 * "buf" if one exists */
1996 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001998 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1999 * page containing "buf" if one exists */
2000 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002001 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00002002 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
2003 * isn't empty: open new window */
2004 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002006 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
2007 tabpage_new();
2008 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002010 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 }
2013#endif
2014
2015 ++RedrawingDisabled;
2016 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2017 lnum, forceit) <= 0)
2018 {
2019 --RedrawingDisabled;
2020
2021 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2022 if (!p_sol && col != 0)
2023 {
2024 curwin->w_cursor.col = col;
2025 check_cursor_col();
2026#ifdef FEAT_VIRTUALEDIT
2027 curwin->w_cursor.coladd = 0;
2028#endif
2029 curwin->w_set_curswant = TRUE;
2030 }
2031 return OK;
2032 }
2033 --RedrawingDisabled;
2034 return FAIL;
2035}
2036
2037/*
2038 * go to the last know line number for the current buffer
2039 */
2040 void
2041buflist_getfpos()
2042{
2043 pos_T *fpos;
2044
2045 fpos = buflist_findfpos(curbuf);
2046
2047 curwin->w_cursor.lnum = fpos->lnum;
2048 check_cursor_lnum();
2049
2050 if (p_sol)
2051 curwin->w_cursor.col = 0;
2052 else
2053 {
2054 curwin->w_cursor.col = fpos->col;
2055 check_cursor_col();
2056#ifdef FEAT_VIRTUALEDIT
2057 curwin->w_cursor.coladd = 0;
2058#endif
2059 curwin->w_set_curswant = TRUE;
2060 }
2061}
2062
Bram Moolenaar81695252004-12-29 20:58:21 +00002063#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2064/*
2065 * Find file in buffer list by name (it has to be for the current window).
2066 * Returns NULL if not found.
2067 */
2068 buf_T *
2069buflist_findname_exp(fname)
2070 char_u *fname;
2071{
2072 char_u *ffname;
2073 buf_T *buf = NULL;
2074
2075 /* First make the name into a full path name */
2076 ffname = FullName_save(fname,
2077#ifdef UNIX
2078 TRUE /* force expansion, get rid of symbolic links */
2079#else
2080 FALSE
2081#endif
2082 );
2083 if (ffname != NULL)
2084 {
2085 buf = buflist_findname(ffname);
2086 vim_free(ffname);
2087 }
2088 return buf;
2089}
2090#endif
2091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092/*
2093 * Find file in buffer list by name (it has to be for the current window).
2094 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002095 * Skips dummy buffers.
2096 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 */
2098 buf_T *
2099buflist_findname(ffname)
2100 char_u *ffname;
2101{
2102#ifdef UNIX
2103 struct stat st;
2104
2105 if (mch_stat((char *)ffname, &st) < 0)
2106 st.st_dev = (dev_T)-1;
2107 return buflist_findname_stat(ffname, &st);
2108}
2109
2110/*
2111 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2112 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002113 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 */
2115 static buf_T *
2116buflist_findname_stat(ffname, stp)
2117 char_u *ffname;
2118 struct stat *stp;
2119{
2120#endif
2121 buf_T *buf;
2122
2123 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002124 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125#ifdef UNIX
2126 , stp
2127#endif
2128 ))
2129 return buf;
2130 return NULL;
2131}
2132
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002133#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2134 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135/*
2136 * Find file in buffer list by a regexp pattern.
2137 * Return fnum of the found buffer.
2138 * Return < 0 for error.
2139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 int
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002141buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 char_u *pattern;
2143 char_u *pattern_end; /* pointer to first char after pattern */
2144 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002145 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002146 int curtab_only; /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
2148 buf_T *buf;
2149 regprog_T *prog;
2150 int match = -1;
2151 int find_listed;
2152 char_u *pat;
2153 char_u *patend;
2154 int attempt;
2155 char_u *p;
2156 int toggledollar;
2157
2158 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2159 {
2160 if (*pattern == '%')
2161 match = curbuf->b_fnum;
2162 else
2163 match = curwin->w_alt_fnum;
2164#ifdef FEAT_DIFF
2165 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2166 match = -1;
2167#endif
2168 }
2169
2170 /*
2171 * Try four ways of matching a listed buffer:
2172 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002173 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 * attempt == 2: with '$' at end (only match at end)
2175 * attempt == 3: with '^' at start and '$' at end (only full match)
2176 * Repeat this for finding an unlisted buffer if there was no matching
2177 * listed buffer.
2178 */
2179 else
2180 {
2181 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2182 if (pat == NULL)
2183 return -1;
2184 patend = pat + STRLEN(pat) - 1;
2185 toggledollar = (patend > pat && *patend == '$');
2186
2187 /* First try finding a listed buffer. If not found and "unlisted"
2188 * is TRUE, try finding an unlisted buffer. */
2189 find_listed = TRUE;
2190 for (;;)
2191 {
2192 for (attempt = 0; attempt <= 3; ++attempt)
2193 {
2194 /* may add '^' and '$' */
2195 if (toggledollar)
2196 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2197 p = pat;
2198 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2199 ++p;
2200 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2201 if (prog == NULL)
2202 {
2203 vim_free(pat);
2204 return -1;
2205 }
2206
2207 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2208 if (buf->b_p_bl == find_listed
2209#ifdef FEAT_DIFF
2210 && (!diffmode || diff_mode_buf(buf))
2211#endif
2212 && buflist_match(prog, buf) != NULL)
2213 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002214 if (curtab_only)
2215 {
2216 /* Ignore the match if the buffer is not open in
2217 * the current tab. */
2218#ifdef FEAT_WINDOWS
2219 win_T *wp;
2220
2221 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2222 if (wp->w_buffer == buf)
2223 break;
2224 if (wp == NULL)
2225 continue;
2226#else
2227 if (curwin->w_buffer != buf)
2228 continue;
2229#endif
2230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 if (match >= 0) /* already found a match */
2232 {
2233 match = -2;
2234 break;
2235 }
2236 match = buf->b_fnum; /* remember first match */
2237 }
2238
2239 vim_free(prog);
2240 if (match >= 0) /* found one match */
2241 break;
2242 }
2243
2244 /* Only search for unlisted buffers if there was no match with
2245 * a listed buffer. */
2246 if (!unlisted || !find_listed || match != -1)
2247 break;
2248 find_listed = FALSE;
2249 }
2250
2251 vim_free(pat);
2252 }
2253
2254 if (match == -2)
2255 EMSG2(_("E93: More than one match for %s"), pattern);
2256 else if (match < 0)
2257 EMSG2(_("E94: No matching buffer for %s"), pattern);
2258 return match;
2259}
2260#endif
2261
2262#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2263
2264/*
2265 * Find all buffer names that match.
2266 * For command line expansion of ":buf" and ":sbuf".
2267 * Return OK if matches found, FAIL otherwise.
2268 */
2269 int
2270ExpandBufnames(pat, num_file, file, options)
2271 char_u *pat;
2272 int *num_file;
2273 char_u ***file;
2274 int options;
2275{
2276 int count = 0;
2277 buf_T *buf;
2278 int round;
2279 char_u *p;
2280 int attempt;
2281 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002282 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284 *num_file = 0; /* return values in case of FAIL */
2285 *file = NULL;
2286
Bram Moolenaar05159a02005-02-26 23:04:13 +00002287 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2288 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002290 patc = alloc((unsigned)STRLEN(pat) + 11);
2291 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002293 STRCPY(patc, "\\(^\\|[\\/]\\)");
2294 STRCPY(patc + 11, pat + 1);
2295 }
2296 else
2297 patc = pat;
2298
2299 /*
2300 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002301 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002302 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002303 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002304 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002305 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002306 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002307 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002308 if (prog == NULL)
2309 {
2310 if (patc != pat)
2311 vim_free(patc);
2312 return FAIL;
2313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
2315 /*
2316 * round == 1: Count the matches.
2317 * round == 2: Build the array to keep the matches.
2318 */
2319 for (round = 1; round <= 2; ++round)
2320 {
2321 count = 0;
2322 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2323 {
2324 if (!buf->b_p_bl) /* skip unlisted buffers */
2325 continue;
2326 p = buflist_match(prog, buf);
2327 if (p != NULL)
2328 {
2329 if (round == 1)
2330 ++count;
2331 else
2332 {
2333 if (options & WILD_HOME_REPLACE)
2334 p = home_replace_save(buf, p);
2335 else
2336 p = vim_strsave(p);
2337 (*file)[count++] = p;
2338 }
2339 }
2340 }
2341 if (count == 0) /* no match found, break here */
2342 break;
2343 if (round == 1)
2344 {
2345 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2346 if (*file == NULL)
2347 {
2348 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002349 if (patc != pat)
2350 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 return FAIL;
2352 }
2353 }
2354 }
2355 vim_free(prog);
2356 if (count) /* match(es) found, break here */
2357 break;
2358 }
2359
Bram Moolenaar05159a02005-02-26 23:04:13 +00002360 if (patc != pat)
2361 vim_free(patc);
2362
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 *num_file = count;
2364 return (count == 0 ? FAIL : OK);
2365}
2366
2367#endif /* FEAT_CMDL_COMPL */
2368
2369#ifdef HAVE_BUFLIST_MATCH
2370/*
2371 * Check for a match on the file name for buffer "buf" with regprog "prog".
2372 */
2373 static char_u *
2374buflist_match(prog, buf)
2375 regprog_T *prog;
2376 buf_T *buf;
2377{
2378 char_u *match;
2379
2380 /* First try the short file name, then the long file name. */
2381 match = fname_match(prog, buf->b_sfname);
2382 if (match == NULL)
2383 match = fname_match(prog, buf->b_ffname);
2384
2385 return match;
2386}
2387
2388/*
2389 * Try matching the regexp in "prog" with file name "name".
2390 * Return "name" when there is a match, NULL when not.
2391 */
2392 static char_u *
2393fname_match(prog, name)
2394 regprog_T *prog;
2395 char_u *name;
2396{
2397 char_u *match = NULL;
2398 char_u *p;
2399 regmatch_T regmatch;
2400
2401 if (name != NULL)
2402 {
2403 regmatch.regprog = prog;
2404#ifdef CASE_INSENSITIVE_FILENAME
2405 regmatch.rm_ic = TRUE; /* Always ignore case */
2406#else
2407 regmatch.rm_ic = FALSE; /* Never ignore case */
2408#endif
2409
2410 if (vim_regexec(&regmatch, name, (colnr_T)0))
2411 match = name;
2412 else
2413 {
2414 /* Replace $(HOME) with '~' and try matching again. */
2415 p = home_replace_save(NULL, name);
2416 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2417 match = name;
2418 vim_free(p);
2419 }
2420 }
2421
2422 return match;
2423}
2424#endif
2425
2426/*
2427 * find file in buffer list by number
2428 */
2429 buf_T *
2430buflist_findnr(nr)
2431 int nr;
2432{
2433 buf_T *buf;
2434
2435 if (nr == 0)
2436 nr = curwin->w_alt_fnum;
2437 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2438 if (buf->b_fnum == nr)
2439 return (buf);
2440 return NULL;
2441}
2442
2443/*
2444 * Get name of file 'n' in the buffer list.
2445 * When the file has no name an empty string is returned.
2446 * home_replace() is used to shorten the file name (used for marks).
2447 * Returns a pointer to allocated memory, of NULL when failed.
2448 */
2449 char_u *
2450buflist_nr2name(n, fullname, helptail)
2451 int n;
2452 int fullname;
2453 int helptail; /* for help buffers return tail only */
2454{
2455 buf_T *buf;
2456
2457 buf = buflist_findnr(n);
2458 if (buf == NULL)
2459 return NULL;
2460 return home_replace_save(helptail ? buf : NULL,
2461 fullname ? buf->b_ffname : buf->b_fname);
2462}
2463
2464/*
2465 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2466 * When "copy_options" is TRUE save the local window option values.
2467 * When "lnum" is 0 only do the options.
2468 */
2469 static void
2470buflist_setfpos(buf, win, lnum, col, copy_options)
2471 buf_T *buf;
2472 win_T *win;
2473 linenr_T lnum;
2474 colnr_T col;
2475 int copy_options;
2476{
2477 wininfo_T *wip;
2478
2479 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2480 if (wip->wi_win == win)
2481 break;
2482 if (wip == NULL)
2483 {
2484 /* allocate a new entry */
2485 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2486 if (wip == NULL)
2487 return;
2488 wip->wi_win = win;
2489 if (lnum == 0) /* set lnum even when it's 0 */
2490 lnum = 1;
2491 }
2492 else
2493 {
2494 /* remove the entry from the list */
2495 if (wip->wi_prev)
2496 wip->wi_prev->wi_next = wip->wi_next;
2497 else
2498 buf->b_wininfo = wip->wi_next;
2499 if (wip->wi_next)
2500 wip->wi_next->wi_prev = wip->wi_prev;
2501 if (copy_options && wip->wi_optset)
2502 {
2503 clear_winopt(&wip->wi_opt);
2504#ifdef FEAT_FOLDING
2505 deleteFoldRecurse(&wip->wi_folds);
2506#endif
2507 }
2508 }
2509 if (lnum != 0)
2510 {
2511 wip->wi_fpos.lnum = lnum;
2512 wip->wi_fpos.col = col;
2513 }
2514 if (copy_options)
2515 {
2516 /* Save the window-specific option values. */
2517 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2518#ifdef FEAT_FOLDING
2519 wip->wi_fold_manual = win->w_fold_manual;
2520 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2521#endif
2522 wip->wi_optset = TRUE;
2523 }
2524
2525 /* insert the entry in front of the list */
2526 wip->wi_next = buf->b_wininfo;
2527 buf->b_wininfo = wip;
2528 wip->wi_prev = NULL;
2529 if (wip->wi_next)
2530 wip->wi_next->wi_prev = wip;
2531
2532 return;
2533}
2534
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002535#ifdef FEAT_DIFF
2536static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2537
2538/*
2539 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2540 * page. That's because a diff is local to a tab page.
2541 */
2542 static int
2543wininfo_other_tab_diff(wip)
2544 wininfo_T *wip;
2545{
2546 win_T *wp;
2547
2548 if (wip->wi_opt.wo_diff)
2549 {
2550 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2551 /* return FALSE when it's a window in the current tab page, thus
2552 * the buffer was in diff mode here */
2553 if (wip->wi_win == wp)
2554 return FALSE;
2555 return TRUE;
2556 }
2557 return FALSE;
2558}
2559#endif
2560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561/*
2562 * Find info for the current window in buffer "buf".
2563 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002564 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2565 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 * Returns NULL when there isn't any info.
2567 */
2568 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002569find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002571 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573 wininfo_T *wip;
2574
2575 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002576 if (wip->wi_win == curwin
2577#ifdef FEAT_DIFF
2578 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2579#endif
2580 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002582
2583 /* If no wininfo for curwin, use the first in the list (that doesn't have
2584 * 'diff' set and is in another tab page). */
2585 if (wip == NULL)
2586 {
2587#ifdef FEAT_DIFF
2588 if (skip_diff_buffer)
2589 {
2590 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2591 if (!wininfo_other_tab_diff(wip))
2592 break;
2593 }
2594 else
2595#endif
2596 wip = buf->b_wininfo;
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 return wip;
2599}
2600
2601/*
2602 * Reset the local window options to the values last used in this window.
2603 * If the buffer wasn't used in this window before, use the values from
2604 * the most recently used window. If the values were never set, use the
2605 * global values for the window.
2606 */
2607 void
2608get_winopts(buf)
2609 buf_T *buf;
2610{
2611 wininfo_T *wip;
2612
2613 clear_winopt(&curwin->w_onebuf_opt);
2614#ifdef FEAT_FOLDING
2615 clearFolding(curwin);
2616#endif
2617
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002618 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 if (wip != NULL && wip->wi_optset)
2620 {
2621 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2622#ifdef FEAT_FOLDING
2623 curwin->w_fold_manual = wip->wi_fold_manual;
2624 curwin->w_foldinvalid = TRUE;
2625 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2626#endif
2627 }
2628 else
2629 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2630
2631#ifdef FEAT_FOLDING
2632 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2633 if (p_fdls >= 0)
2634 curwin->w_p_fdl = p_fdls;
2635#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002636#ifdef FEAT_SYN_HL
2637 check_colorcolumn(curwin);
2638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639}
2640
2641/*
2642 * Find the position (lnum and col) for the buffer 'buf' for the current
2643 * window.
2644 * Returns a pointer to no_position if no position is found.
2645 */
2646 pos_T *
2647buflist_findfpos(buf)
2648 buf_T *buf;
2649{
2650 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002651 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002653 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (wip != NULL)
2655 return &(wip->wi_fpos);
2656 else
2657 return &no_position;
2658}
2659
2660/*
2661 * Find the lnum for the buffer 'buf' for the current window.
2662 */
2663 linenr_T
2664buflist_findlnum(buf)
2665 buf_T *buf;
2666{
2667 return buflist_findfpos(buf)->lnum;
2668}
2669
2670#if defined(FEAT_LISTCMDS) || defined(PROTO)
2671/*
2672 * List all know file names (for :files and :buffers command).
2673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 void
2675buflist_list(eap)
2676 exarg_T *eap;
2677{
2678 buf_T *buf;
2679 int len;
2680 int i;
2681
2682 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2683 {
2684 /* skip unlisted buffers, unless ! was used */
2685 if (!buf->b_p_bl && !eap->forceit)
2686 continue;
2687 msg_putchar('\n');
2688 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002689 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 else
2691 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2692
Bram Moolenaar50cde822005-06-05 21:54:54 +00002693 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 buf->b_fnum,
2695 buf->b_p_bl ? ' ' : 'u',
2696 buf == curbuf ? '%' :
2697 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2698 buf->b_ml.ml_mfp == NULL ? ' ' :
2699 (buf->b_nwindows == 0 ? 'h' : 'a'),
2700 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2701 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002702 : (bufIsChanged(buf) ? '+' : ' '),
2703 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 i = 40 - vim_strsize(IObuff);
2707 do
2708 {
2709 IObuff[len++] = ' ';
2710 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002711 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2712 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002713 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 msg_outtrans(IObuff);
2715 out_flush(); /* output one line at a time */
2716 ui_breakcheck();
2717 }
2718}
2719#endif
2720
2721/*
2722 * Get file name and line number for file 'fnum'.
2723 * Used by DoOneCmd() for translating '%' and '#'.
2724 * Used by insert_reg() and cmdline_paste() for '#' register.
2725 * Return FAIL if not found, OK for success.
2726 */
2727 int
2728buflist_name_nr(fnum, fname, lnum)
2729 int fnum;
2730 char_u **fname;
2731 linenr_T *lnum;
2732{
2733 buf_T *buf;
2734
2735 buf = buflist_findnr(fnum);
2736 if (buf == NULL || buf->b_fname == NULL)
2737 return FAIL;
2738
2739 *fname = buf->b_fname;
2740 *lnum = buflist_findlnum(buf);
2741
2742 return OK;
2743}
2744
2745/*
2746 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2747 * The file name with the full path is also remembered, for when :cd is used.
2748 * Returns FAIL for failure (file name already in use by other buffer)
2749 * OK otherwise.
2750 */
2751 int
2752setfname(buf, ffname, sfname, message)
2753 buf_T *buf;
2754 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002755 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
Bram Moolenaar81695252004-12-29 20:58:21 +00002757 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#ifdef UNIX
2759 struct stat st;
2760#endif
2761
2762 if (ffname == NULL || *ffname == NUL)
2763 {
2764 /* Removing the name. */
2765 vim_free(buf->b_ffname);
2766 vim_free(buf->b_sfname);
2767 buf->b_ffname = NULL;
2768 buf->b_sfname = NULL;
2769#ifdef UNIX
2770 st.st_dev = (dev_T)-1;
2771#endif
2772 }
2773 else
2774 {
2775 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2776 if (ffname == NULL) /* out of memory */
2777 return FAIL;
2778
2779 /*
2780 * if the file name is already used in another buffer:
2781 * - if the buffer is loaded, fail
2782 * - if the buffer is not loaded, delete it from the list
2783 */
2784#ifdef UNIX
2785 if (mch_stat((char *)ffname, &st) < 0)
2786 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002787#endif
2788 if (!(buf->b_flags & BF_DUMMY))
2789#ifdef UNIX
2790 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002792 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793#endif
2794 if (obuf != NULL && obuf != buf)
2795 {
2796 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2797 {
2798 if (message)
2799 EMSG(_("E95: Buffer with this name already exists"));
2800 vim_free(ffname);
2801 return FAIL;
2802 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002803 /* delete from the list */
2804 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806 sfname = vim_strsave(sfname);
2807 if (ffname == NULL || sfname == NULL)
2808 {
2809 vim_free(sfname);
2810 vim_free(ffname);
2811 return FAIL;
2812 }
2813#ifdef USE_FNAME_CASE
2814# ifdef USE_LONG_FNAME
2815 if (USE_LONG_FNAME)
2816# endif
2817 fname_case(sfname, 0); /* set correct case for short file name */
2818#endif
2819 vim_free(buf->b_ffname);
2820 vim_free(buf->b_sfname);
2821 buf->b_ffname = ffname;
2822 buf->b_sfname = sfname;
2823 }
2824 buf->b_fname = buf->b_sfname;
2825#ifdef UNIX
2826 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002827 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 else
2829 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002830 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 buf->b_dev = st.st_dev;
2832 buf->b_ino = st.st_ino;
2833 }
2834#endif
2835
2836#ifndef SHORT_FNAME
2837 buf->b_shortname = FALSE;
2838#endif
2839
2840 buf_name_changed(buf);
2841 return OK;
2842}
2843
2844/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002845 * Crude way of changing the name of a buffer. Use with care!
2846 * The name should be relative to the current directory.
2847 */
2848 void
2849buf_set_name(fnum, name)
2850 int fnum;
2851 char_u *name;
2852{
2853 buf_T *buf;
2854
2855 buf = buflist_findnr(fnum);
2856 if (buf != NULL)
2857 {
2858 vim_free(buf->b_sfname);
2859 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002860 buf->b_ffname = vim_strsave(name);
2861 buf->b_sfname = NULL;
2862 /* Allocate ffname and expand into full path. Also resolves .lnk
2863 * files on Win32. */
2864 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002865 buf->b_fname = buf->b_sfname;
2866 }
2867}
2868
2869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 * Take care of what needs to be done when the name of buffer "buf" has
2871 * changed.
2872 */
2873 void
2874buf_name_changed(buf)
2875 buf_T *buf;
2876{
2877 /*
2878 * If the file name changed, also change the name of the swapfile
2879 */
2880 if (buf->b_ml.ml_mfp != NULL)
2881 ml_setname(buf);
2882
2883 if (curwin->w_buffer == buf)
2884 check_arg_idx(curwin); /* check file name for arg list */
2885#ifdef FEAT_TITLE
2886 maketitle(); /* set window title */
2887#endif
2888#ifdef FEAT_WINDOWS
2889 status_redraw_all(); /* status lines need to be redrawn */
2890#endif
2891 fmarks_check_names(buf); /* check named file marks */
2892 ml_timestamp(buf); /* reset timestamp */
2893}
2894
2895/*
2896 * set alternate file name for current window
2897 *
2898 * Used by do_one_cmd(), do_write() and do_ecmd().
2899 * Return the buffer.
2900 */
2901 buf_T *
2902setaltfname(ffname, sfname, lnum)
2903 char_u *ffname;
2904 char_u *sfname;
2905 linenr_T lnum;
2906{
2907 buf_T *buf;
2908
2909 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2910 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002911 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 curwin->w_alt_fnum = buf->b_fnum;
2913 return buf;
2914}
2915
2916/*
2917 * Get alternate file name for current window.
2918 * Return NULL if there isn't any, and give error message if requested.
2919 */
2920 char_u *
2921getaltfname(errmsg)
2922 int errmsg; /* give error message */
2923{
2924 char_u *fname;
2925 linenr_T dummy;
2926
2927 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2928 {
2929 if (errmsg)
2930 EMSG(_(e_noalt));
2931 return NULL;
2932 }
2933 return fname;
2934}
2935
2936/*
2937 * Add a file name to the buflist and return its number.
2938 * Uses same flags as buflist_new(), except BLN_DUMMY.
2939 *
2940 * used by qf_init(), main() and doarglist()
2941 */
2942 int
2943buflist_add(fname, flags)
2944 char_u *fname;
2945 int flags;
2946{
2947 buf_T *buf;
2948
2949 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2950 if (buf != NULL)
2951 return buf->b_fnum;
2952 return 0;
2953}
2954
2955#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2956/*
2957 * Adjust slashes in file names. Called after 'shellslash' was set.
2958 */
2959 void
2960buflist_slash_adjust()
2961{
2962 buf_T *bp;
2963
2964 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2965 {
2966 if (bp->b_ffname != NULL)
2967 slash_adjust(bp->b_ffname);
2968 if (bp->b_sfname != NULL)
2969 slash_adjust(bp->b_sfname);
2970 }
2971}
2972#endif
2973
2974/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002975 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 * Also save the local window option values.
2977 */
2978 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002979buflist_altfpos(win)
2980 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002982 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983}
2984
2985/*
2986 * Return TRUE if 'ffname' is not the same file as current file.
2987 * Fname must have a full path (expanded by mch_FullName()).
2988 */
2989 int
2990otherfile(ffname)
2991 char_u *ffname;
2992{
2993 return otherfile_buf(curbuf, ffname
2994#ifdef UNIX
2995 , NULL
2996#endif
2997 );
2998}
2999
3000 static int
3001otherfile_buf(buf, ffname
3002#ifdef UNIX
3003 , stp
3004#endif
3005 )
3006 buf_T *buf;
3007 char_u *ffname;
3008#ifdef UNIX
3009 struct stat *stp;
3010#endif
3011{
3012 /* no name is different */
3013 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3014 return TRUE;
3015 if (fnamecmp(ffname, buf->b_ffname) == 0)
3016 return FALSE;
3017#ifdef UNIX
3018 {
3019 struct stat st;
3020
3021 /* If no struct stat given, get it now */
3022 if (stp == NULL)
3023 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003024 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 st.st_dev = (dev_T)-1;
3026 stp = &st;
3027 }
3028 /* Use dev/ino to check if the files are the same, even when the names
3029 * are different (possible with links). Still need to compare the
3030 * name above, for when the file doesn't exist yet.
3031 * Problem: The dev/ino changes when a file is deleted (and created
3032 * again) and remains the same when renamed/moved. We don't want to
3033 * mch_stat() each buffer each time, that would be too slow. Get the
3034 * dev/ino again when they appear to match, but not when they appear
3035 * to be different: Could skip a buffer when it's actually the same
3036 * file. */
3037 if (buf_same_ino(buf, stp))
3038 {
3039 buf_setino(buf);
3040 if (buf_same_ino(buf, stp))
3041 return FALSE;
3042 }
3043 }
3044#endif
3045 return TRUE;
3046}
3047
3048#if defined(UNIX) || defined(PROTO)
3049/*
3050 * Set inode and device number for a buffer.
3051 * Must always be called when b_fname is changed!.
3052 */
3053 void
3054buf_setino(buf)
3055 buf_T *buf;
3056{
3057 struct stat st;
3058
3059 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3060 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003061 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 buf->b_dev = st.st_dev;
3063 buf->b_ino = st.st_ino;
3064 }
3065 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003066 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067}
3068
3069/*
3070 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3071 */
3072 static int
3073buf_same_ino(buf, stp)
3074 buf_T *buf;
3075 struct stat *stp;
3076{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003077 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 && stp->st_dev == buf->b_dev
3079 && stp->st_ino == buf->b_ino);
3080}
3081#endif
3082
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003083/*
3084 * Print info about the current buffer.
3085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 void
3087fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003088 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 int shorthelp;
3090 int dont_truncate;
3091{
3092 char_u *name;
3093 int n;
3094 char_u *p;
3095 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003096 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 buffer = alloc(IOSIZE);
3099 if (buffer == NULL)
3100 return;
3101
3102 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3103 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003104 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 p = buffer + STRLEN(buffer);
3106 }
3107 else
3108 p = buffer;
3109
3110 *p++ = '"';
3111 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003112 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 else
3114 {
3115 if (!fullname && curbuf->b_fname != NULL)
3116 name = curbuf->b_fname;
3117 else
3118 name = curbuf->b_ffname;
3119 home_replace(shorthelp ? curbuf : NULL, name, p,
3120 (int)(IOSIZE - (p - buffer)), TRUE);
3121 }
3122
Bram Moolenaara800b422010-06-27 01:15:55 +02003123 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 curbufIsChanged() ? (shortmess(SHM_MOD)
3125 ? " [+]" : _(" [Modified]")) : " ",
3126 (curbuf->b_flags & BF_NOTEDITED)
3127#ifdef FEAT_QUICKFIX
3128 && !bt_dontwrite(curbuf)
3129#endif
3130 ? _("[Not edited]") : "",
3131 (curbuf->b_flags & BF_NEW)
3132#ifdef FEAT_QUICKFIX
3133 && !bt_dontwrite(curbuf)
3134#endif
3135 ? _("[New file]") : "",
3136 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3137 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3138 : _("[readonly]")) : "",
3139 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3140 || curbuf->b_p_ro) ?
3141 " " : "");
3142 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3143 * causes an overflow, thus for large numbers divide instead. */
3144 if (curwin->w_cursor.lnum > 1000000L)
3145 n = (int)(((long)curwin->w_cursor.lnum) /
3146 ((long)curbuf->b_ml.ml_line_count / 100L));
3147 else
3148 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3149 (long)curbuf->b_ml.ml_line_count);
3150 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3151 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003152 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
3154#ifdef FEAT_CMDL_INFO
3155 else if (p_ru)
3156 {
3157 /* Current line and column are already on the screen -- webb */
3158 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003159 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003161 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 (long)curbuf->b_ml.ml_line_count, n);
3163 }
3164#endif
3165 else
3166 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003167 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 _("line %ld of %ld --%d%%-- col "),
3169 (long)curwin->w_cursor.lnum,
3170 (long)curbuf->b_ml.ml_line_count,
3171 n);
3172 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003173 len = STRLEN(buffer);
3174 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3176 }
3177
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003178 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
3180 if (dont_truncate)
3181 {
3182 /* Temporarily set msg_scroll to avoid the message being truncated.
3183 * First call msg_start() to get the message in the right place. */
3184 msg_start();
3185 n = msg_scroll;
3186 msg_scroll = TRUE;
3187 msg(buffer);
3188 msg_scroll = n;
3189 }
3190 else
3191 {
3192 p = msg_trunc_attr(buffer, FALSE, 0);
3193 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 /* Need to repeat the message after redrawing when:
3195 * - When restart_edit is set (otherwise there will be a delay
3196 * before redrawing).
3197 * - When the screen was scrolled but there is no wait-return
3198 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003199 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201
3202 vim_free(buffer);
3203}
3204
3205 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003206col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003208 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 int col;
3210 int vcol;
3211{
3212 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003213 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003215 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216}
3217
3218#if defined(FEAT_TITLE) || defined(PROTO)
3219/*
3220 * put file name in title bar of window and in icon title
3221 */
3222
3223static char_u *lasttitle = NULL;
3224static char_u *lasticon = NULL;
3225
3226 void
3227maketitle()
3228{
3229 char_u *p;
3230 char_u *t_str = NULL;
3231 char_u *i_name;
3232 char_u *i_str = NULL;
3233 int maxlen = 0;
3234 int len;
3235 int mustset;
3236 char_u buf[IOSIZE];
3237 int off;
3238
3239 if (!redrawing())
3240 {
3241 /* Postpone updating the title when 'lazyredraw' is set. */
3242 need_maketitle = TRUE;
3243 return;
3244 }
3245
3246 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003247 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 return;
3249
3250 if (p_title)
3251 {
3252 if (p_titlelen > 0)
3253 {
3254 maxlen = p_titlelen * Columns / 100;
3255 if (maxlen < 10)
3256 maxlen = 10;
3257 }
3258
3259 t_str = buf;
3260 if (*p_titlestring != NUL)
3261 {
3262#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003263 if (stl_syntax & STL_IN_TITLE)
3264 {
3265 int use_sandbox = FALSE;
3266 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003267
3268# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003269 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003270# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003271 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003273 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003274 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003275 if (called_emsg)
3276 set_string_option_direct((char_u *)"titlestring", -1,
3277 (char_u *)"", OPT_FREE, SID_ERROR);
3278 called_emsg |= save_called_emsg;
3279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 else
3281#endif
3282 t_str = p_titlestring;
3283 }
3284 else
3285 {
3286 /* format: "fname + (path) (1 of 2) - VIM" */
3287
Bram Moolenaar2c666692012-09-05 13:30:40 +02003288#define SPACE_FOR_FNAME (IOSIZE - 100)
3289#define SPACE_FOR_DIR (IOSIZE - 20)
3290#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003292 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 else
3294 {
3295 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003296 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299
3300 switch (bufIsChanged(curbuf)
3301 + (curbuf->b_p_ro * 2)
3302 + (!curbuf->b_p_ma * 4))
3303 {
3304 case 1: STRCAT(buf, " +"); break;
3305 case 2: STRCAT(buf, " ="); break;
3306 case 3: STRCAT(buf, " =+"); break;
3307 case 4:
3308 case 6: STRCAT(buf, " -"); break;
3309 case 5:
3310 case 7: STRCAT(buf, " -+"); break;
3311 }
3312
3313 if (curbuf->b_fname != NULL)
3314 {
3315 /* Get path of file, replace home dir with ~ */
3316 off = (int)STRLEN(buf);
3317 buf[off++] = ' ';
3318 buf[off++] = '(';
3319 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003320 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#ifdef BACKSLASH_IN_FILENAME
3322 /* avoid "c:/name" to be reduced to "c" */
3323 if (isalpha(buf[off]) && buf[off + 1] == ':')
3324 off += 2;
3325#endif
3326 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003327 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003330 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003331 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003334
Bram Moolenaar2c666692012-09-05 13:30:40 +02003335 /* Translate unprintable chars and concatenate. Keep some
3336 * room for the server name. When there is no room (very long
3337 * file name) use (...). */
3338 if (off < SPACE_FOR_DIR)
3339 {
3340 p = transstr(buf + off);
3341 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3342 vim_free(p);
3343 }
3344 else
3345 {
3346 vim_strncpy(buf + off, (char_u *)"...",
3347 (size_t)(SPACE_FOR_ARGNR - off));
3348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 STRCAT(buf, ")");
3350 }
3351
Bram Moolenaar2c666692012-09-05 13:30:40 +02003352 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
3354#if defined(FEAT_CLIENTSERVER)
3355 if (serverName != NULL)
3356 {
3357 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003358 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360 else
3361#endif
3362 STRCAT(buf, " - VIM");
3363
3364 if (maxlen > 0)
3365 {
3366 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003367 if (vim_strsize(buf) > maxlen)
3368 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
3370 }
3371 }
3372 mustset = ti_change(t_str, &lasttitle);
3373
3374 if (p_icon)
3375 {
3376 i_str = buf;
3377 if (*p_iconstring != NUL)
3378 {
3379#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003380 if (stl_syntax & STL_IN_ICON)
3381 {
3382 int use_sandbox = FALSE;
3383 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003384
3385# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003386 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003387# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003388 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003390 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003391 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003392 if (called_emsg)
3393 set_string_option_direct((char_u *)"iconstring", -1,
3394 (char_u *)"", OPT_FREE, SID_ERROR);
3395 called_emsg |= save_called_emsg;
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 else
3398#endif
3399 i_str = p_iconstring;
3400 }
3401 else
3402 {
3403 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003404 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 else /* use file name only in icon */
3406 i_name = gettail(curbuf->b_ffname);
3407 *i_str = NUL;
3408 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003409 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (len > 100)
3411 {
3412 len -= 100;
3413#ifdef FEAT_MBYTE
3414 if (has_mbyte)
3415 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3416#endif
3417 i_name += len;
3418 }
3419 STRCPY(i_str, i_name);
3420 trans_characters(i_str, IOSIZE);
3421 }
3422 }
3423
3424 mustset |= ti_change(i_str, &lasticon);
3425
3426 if (mustset)
3427 resettitle();
3428}
3429
3430/*
3431 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3432 * from "str" if it does.
3433 * Return TRUE when "*last" changed.
3434 */
3435 static int
3436ti_change(str, last)
3437 char_u *str;
3438 char_u **last;
3439{
3440 if ((str == NULL) != (*last == NULL)
3441 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3442 {
3443 vim_free(*last);
3444 if (str == NULL)
3445 *last = NULL;
3446 else
3447 *last = vim_strsave(str);
3448 return TRUE;
3449 }
3450 return FALSE;
3451}
3452
3453/*
3454 * Put current window title back (used after calling a shell)
3455 */
3456 void
3457resettitle()
3458{
3459 mch_settitle(lasttitle, lasticon);
3460}
Bram Moolenaarea408852005-06-25 22:49:46 +00003461
3462# if defined(EXITFREE) || defined(PROTO)
3463 void
3464free_titles()
3465{
3466 vim_free(lasttitle);
3467 vim_free(lasticon);
3468}
3469# endif
3470
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471#endif /* FEAT_TITLE */
3472
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003473#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003475 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 * Return length of string in screen cells.
3477 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003478 * Normally works for window "wp", except when working for 'tabline' then it
3479 * is "curwin".
3480 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 * Items are drawn interspersed with the text that surrounds it
3482 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3483 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3484 *
3485 * If maxwidth is not zero, the string will be filled at any middle marker
3486 * or truncated if too long, fillchar is used for all whitespace.
3487 */
3488 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003489build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3490 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003492 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 size_t outlen; /* length of out[] */
3494 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003495 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 int fillchar;
3497 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003498 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3499 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 char_u *p;
3502 char_u *s;
3503 char_u *t;
3504 char_u *linecont;
3505#ifdef FEAT_EVAL
3506 win_T *o_curwin;
3507 buf_T *o_curbuf;
3508#endif
3509 int empty_line;
3510 colnr_T virtcol;
3511 long l;
3512 long n;
3513 int prevchar_isflag;
3514 int prevchar_isitem;
3515 int itemisflag;
3516 int fillable;
3517 char_u *str;
3518 long num;
3519 int width;
3520 int itemcnt;
3521 int curitem;
3522 int groupitem[STL_MAX_ITEM];
3523 int groupdepth;
3524 struct stl_item
3525 {
3526 char_u *start;
3527 int minwid;
3528 int maxwid;
3529 enum
3530 {
3531 Normal,
3532 Empty,
3533 Group,
3534 Middle,
3535 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003536 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 Trunc
3538 } type;
3539 } item[STL_MAX_ITEM];
3540 int minwid;
3541 int maxwid;
3542 int zeropad;
3543 char_u base;
3544 char_u opt;
3545#define TMPLEN 70
3546 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003547 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003548 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003549
3550#ifdef FEAT_EVAL
3551 /*
3552 * When the format starts with "%!" then evaluate it as an expression and
3553 * use the result as the actual format string.
3554 */
3555 if (fmt[0] == '%' && fmt[1] == '!')
3556 {
3557 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3558 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003559 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003560 }
3561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 if (fillchar == 0)
3564 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003565#ifdef FEAT_MBYTE
3566 /* Can't handle a multi-byte fill character yet. */
3567 else if (mb_char2len(fillchar) > 1)
3568 fillchar = '-';
3569#endif
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 /*
3572 * Get line & check if empty (cursorpos will show "0-1").
3573 * If inversion is possible we use it. Else '=' characters are used.
3574 */
3575 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3576 empty_line = (*linecont == NUL);
3577
3578 groupdepth = 0;
3579 p = out;
3580 curitem = 0;
3581 prevchar_isflag = TRUE;
3582 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003583 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003585 if (curitem == STL_MAX_ITEM)
3586 {
3587 /* There are too many items. Add the error code to the statusline
3588 * to give the user a hint about what went wrong. */
3589 if (p + 6 < out + outlen)
3590 {
3591 mch_memmove(p, " E541", (size_t)5);
3592 p += 5;
3593 }
3594 break;
3595 }
3596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (*s != NUL && *s != '%')
3598 prevchar_isflag = prevchar_isitem = FALSE;
3599
3600 /*
3601 * Handle up to the next '%' or the end.
3602 */
3603 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3604 *p++ = *s++;
3605 if (*s == NUL || p + 1 >= out + outlen)
3606 break;
3607
3608 /*
3609 * Handle one '%' item.
3610 */
3611 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003612 if (*s == NUL) /* ignore trailing % */
3613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (*s == '%')
3615 {
3616 if (p + 1 >= out + outlen)
3617 break;
3618 *p++ = *s++;
3619 prevchar_isflag = prevchar_isitem = FALSE;
3620 continue;
3621 }
3622 if (*s == STL_MIDDLEMARK)
3623 {
3624 s++;
3625 if (groupdepth > 0)
3626 continue;
3627 item[curitem].type = Middle;
3628 item[curitem++].start = p;
3629 continue;
3630 }
3631 if (*s == STL_TRUNCMARK)
3632 {
3633 s++;
3634 item[curitem].type = Trunc;
3635 item[curitem++].start = p;
3636 continue;
3637 }
3638 if (*s == ')')
3639 {
3640 s++;
3641 if (groupdepth < 1)
3642 continue;
3643 groupdepth--;
3644
3645 t = item[groupitem[groupdepth]].start;
3646 *p = NUL;
3647 l = vim_strsize(t);
3648 if (curitem > groupitem[groupdepth] + 1
3649 && item[groupitem[groupdepth]].minwid == 0)
3650 {
3651 /* remove group if all items are empty */
3652 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3653 if (item[n].type == Normal)
3654 break;
3655 if (n == curitem)
3656 {
3657 p = t;
3658 l = 0;
3659 }
3660 }
3661 if (l > item[groupitem[groupdepth]].maxwid)
3662 {
3663 /* truncate, remove n bytes of text at the start */
3664#ifdef FEAT_MBYTE
3665 if (has_mbyte)
3666 {
3667 /* Find the first character that should be included. */
3668 n = 0;
3669 while (l >= item[groupitem[groupdepth]].maxwid)
3670 {
3671 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003672 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
3674 }
3675 else
3676#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003677 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
3679 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003680 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 p = p - n + 1;
3682#ifdef FEAT_MBYTE
3683 /* Fill up space left over by half a double-wide char. */
3684 while (++l < item[groupitem[groupdepth]].minwid)
3685 *p++ = fillchar;
3686#endif
3687
3688 /* correct the start of the items for the truncation */
3689 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3690 {
3691 item[l].start -= n;
3692 if (item[l].start < t)
3693 item[l].start = t;
3694 }
3695 }
3696 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3697 {
3698 /* fill */
3699 n = item[groupitem[groupdepth]].minwid;
3700 if (n < 0)
3701 {
3702 /* fill by appending characters */
3703 n = 0 - n;
3704 while (l++ < n && p + 1 < out + outlen)
3705 *p++ = fillchar;
3706 }
3707 else
3708 {
3709 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003710 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 l = n - l;
3712 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003713 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 p += l;
3715 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3716 item[n].start += l;
3717 for ( ; l > 0; l--)
3718 *t++ = fillchar;
3719 }
3720 }
3721 continue;
3722 }
3723 minwid = 0;
3724 maxwid = 9999;
3725 zeropad = FALSE;
3726 l = 1;
3727 if (*s == '0')
3728 {
3729 s++;
3730 zeropad = TRUE;
3731 }
3732 if (*s == '-')
3733 {
3734 s++;
3735 l = -1;
3736 }
3737 if (VIM_ISDIGIT(*s))
3738 {
3739 minwid = (int)getdigits(&s);
3740 if (minwid < 0) /* overflow */
3741 minwid = 0;
3742 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003743 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
3745 item[curitem].type = Highlight;
3746 item[curitem].start = p;
3747 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3748 s++;
3749 curitem++;
3750 continue;
3751 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003752 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3753 {
3754 if (*s == STL_TABCLOSENR)
3755 {
3756 if (minwid == 0)
3757 {
3758 /* %X ends the close label, go back to the previously
3759 * define tab label nr. */
3760 for (n = curitem - 1; n >= 0; --n)
3761 if (item[n].type == TabPage && item[n].minwid >= 0)
3762 {
3763 minwid = item[n].minwid;
3764 break;
3765 }
3766 }
3767 else
3768 /* close nrs are stored as negative values */
3769 minwid = - minwid;
3770 }
3771 item[curitem].type = TabPage;
3772 item[curitem].start = p;
3773 item[curitem].minwid = minwid;
3774 s++;
3775 curitem++;
3776 continue;
3777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (*s == '.')
3779 {
3780 s++;
3781 if (VIM_ISDIGIT(*s))
3782 {
3783 maxwid = (int)getdigits(&s);
3784 if (maxwid <= 0) /* overflow */
3785 maxwid = 50;
3786 }
3787 }
3788 minwid = (minwid > 50 ? 50 : minwid) * l;
3789 if (*s == '(')
3790 {
3791 groupitem[groupdepth++] = curitem;
3792 item[curitem].type = Group;
3793 item[curitem].start = p;
3794 item[curitem].minwid = minwid;
3795 item[curitem].maxwid = maxwid;
3796 s++;
3797 curitem++;
3798 continue;
3799 }
3800 if (vim_strchr(STL_ALL, *s) == NULL)
3801 {
3802 s++;
3803 continue;
3804 }
3805 opt = *s++;
3806
3807 /* OK - now for the real work */
3808 base = 'D';
3809 itemisflag = FALSE;
3810 fillable = TRUE;
3811 num = -1;
3812 str = NULL;
3813 switch (opt)
3814 {
3815 case STL_FILEPATH:
3816 case STL_FULLPATH:
3817 case STL_FILENAME:
3818 fillable = FALSE; /* don't change ' ' to fillchar */
3819 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003820 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 else
3822 {
3823 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003824 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3826 }
3827 trans_characters(NameBuff, MAXPATHL);
3828 if (opt != STL_FILENAME)
3829 str = NameBuff;
3830 else
3831 str = gettail(NameBuff);
3832 break;
3833
3834 case STL_VIM_EXPR: /* '{' */
3835 itemisflag = TRUE;
3836 t = p;
3837 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3838 *p++ = *s++;
3839 if (*s != '}') /* missing '}' or out of space */
3840 break;
3841 s++;
3842 *p = 0;
3843 p = t;
3844
3845#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003846 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3848
3849 o_curbuf = curbuf;
3850 o_curwin = curwin;
3851 curwin = wp;
3852 curbuf = wp->w_buffer;
3853
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003854 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856 curwin = o_curwin;
3857 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003858 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
3860 if (str != NULL && *str != 0)
3861 {
3862 if (*skipdigits(str) == NUL)
3863 {
3864 num = atoi((char *)str);
3865 vim_free(str);
3866 str = NULL;
3867 itemisflag = FALSE;
3868 }
3869 }
3870#endif
3871 break;
3872
3873 case STL_LINE:
3874 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3875 ? 0L : (long)(wp->w_cursor.lnum);
3876 break;
3877
3878 case STL_NUMLINES:
3879 num = wp->w_buffer->b_ml.ml_line_count;
3880 break;
3881
3882 case STL_COLUMN:
3883 num = !(State & INSERT) && empty_line
3884 ? 0 : (int)wp->w_cursor.col + 1;
3885 break;
3886
3887 case STL_VIRTCOL:
3888 case STL_VIRTCOL_ALT:
3889 /* In list mode virtcol needs to be recomputed */
3890 virtcol = wp->w_virtcol;
3891 if (wp->w_p_list && lcs_tab1 == NUL)
3892 {
3893 wp->w_p_list = FALSE;
3894 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3895 wp->w_p_list = TRUE;
3896 }
3897 ++virtcol;
3898 /* Don't display %V if it's the same as %c. */
3899 if (opt == STL_VIRTCOL_ALT
3900 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3901 ? 0 : (int)wp->w_cursor.col + 1)))
3902 break;
3903 num = (long)virtcol;
3904 break;
3905
3906 case STL_PERCENTAGE:
3907 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3908 (long)wp->w_buffer->b_ml.ml_line_count);
3909 break;
3910
3911 case STL_ALTPERCENT:
3912 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003913 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 break;
3915
3916 case STL_ARGLISTSTAT:
3917 fillable = FALSE;
3918 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003919 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 str = tmp;
3921 break;
3922
3923 case STL_KEYMAP:
3924 fillable = FALSE;
3925 if (get_keymap_str(wp, tmp, TMPLEN))
3926 str = tmp;
3927 break;
3928 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003929#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003930 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#else
3932 num = 0;
3933#endif
3934 break;
3935
3936 case STL_BUFNO:
3937 num = wp->w_buffer->b_fnum;
3938 break;
3939
3940 case STL_OFFSET_X:
3941 base = 'X';
3942 case STL_OFFSET:
3943#ifdef FEAT_BYTEOFF
3944 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3945 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3946 0L : l + 1 + (!(State & INSERT) && empty_line ?
3947 0 : (int)wp->w_cursor.col);
3948#endif
3949 break;
3950
3951 case STL_BYTEVAL_X:
3952 base = 'X';
3953 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003954 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 num = 0;
3956 else
3957 {
3958#ifdef FEAT_MBYTE
3959 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3960#else
3961 num = linecont[wp->w_cursor.col];
3962#endif
3963 }
3964 if (num == NL)
3965 num = 0;
3966 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3967 num = NL;
3968 break;
3969
3970 case STL_ROFLAG:
3971 case STL_ROFLAG_ALT:
3972 itemisflag = TRUE;
3973 if (wp->w_buffer->b_p_ro)
3974 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3975 break;
3976
3977 case STL_HELPFLAG:
3978 case STL_HELPFLAG_ALT:
3979 itemisflag = TRUE;
3980 if (wp->w_buffer->b_help)
3981 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003982 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 break;
3984
3985#ifdef FEAT_AUTOCMD
3986 case STL_FILETYPE:
3987 if (*wp->w_buffer->b_p_ft != NUL
3988 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3989 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003990 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3991 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 str = tmp;
3993 }
3994 break;
3995
3996 case STL_FILETYPE_ALT:
3997 itemisflag = TRUE;
3998 if (*wp->w_buffer->b_p_ft != NUL
3999 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4000 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004001 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4002 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 for (t = tmp; *t != 0; t++)
4004 *t = TOUPPER_LOC(*t);
4005 str = tmp;
4006 }
4007 break;
4008#endif
4009
4010#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4011 case STL_PREVIEWFLAG:
4012 case STL_PREVIEWFLAG_ALT:
4013 itemisflag = TRUE;
4014 if (wp->w_p_pvw)
4015 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4016 : _("[Preview]"));
4017 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004018
4019 case STL_QUICKFIX:
4020 if (bt_quickfix(wp->w_buffer))
4021 str = (char_u *)(wp->w_llist_ref
4022 ? _(msg_loclist)
4023 : _(msg_qflist));
4024 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#endif
4026
4027 case STL_MODIFIED:
4028 case STL_MODIFIED_ALT:
4029 itemisflag = TRUE;
4030 switch ((opt == STL_MODIFIED_ALT)
4031 + bufIsChanged(wp->w_buffer) * 2
4032 + (!wp->w_buffer->b_p_ma) * 4)
4033 {
4034 case 2: str = (char_u *)"[+]"; break;
4035 case 3: str = (char_u *)",+"; break;
4036 case 4: str = (char_u *)"[-]"; break;
4037 case 5: str = (char_u *)",-"; break;
4038 case 6: str = (char_u *)"[+-]"; break;
4039 case 7: str = (char_u *)",+-"; break;
4040 }
4041 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004042
4043 case STL_HIGHLIGHT:
4044 t = s;
4045 while (*s != '#' && *s != NUL)
4046 ++s;
4047 if (*s == '#')
4048 {
4049 item[curitem].type = Highlight;
4050 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004051 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004052 curitem++;
4053 }
4054 ++s;
4055 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 }
4057
4058 item[curitem].start = p;
4059 item[curitem].type = Normal;
4060 if (str != NULL && *str)
4061 {
4062 t = str;
4063 if (itemisflag)
4064 {
4065 if ((t[0] && t[1])
4066 && ((!prevchar_isitem && *t == ',')
4067 || (prevchar_isflag && *t == ' ')))
4068 t++;
4069 prevchar_isflag = TRUE;
4070 }
4071 l = vim_strsize(t);
4072 if (l > 0)
4073 prevchar_isitem = TRUE;
4074 if (l > maxwid)
4075 {
4076 while (l >= maxwid)
4077#ifdef FEAT_MBYTE
4078 if (has_mbyte)
4079 {
4080 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004081 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083 else
4084#endif
4085 l -= byte2cells(*t++);
4086 if (p + 1 >= out + outlen)
4087 break;
4088 *p++ = '<';
4089 }
4090 if (minwid > 0)
4091 {
4092 for (; l < minwid && p + 1 < out + outlen; l++)
4093 {
4094 /* Don't put a "-" in front of a digit. */
4095 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4096 *p++ = ' ';
4097 else
4098 *p++ = fillchar;
4099 }
4100 minwid = 0;
4101 }
4102 else
4103 minwid *= -1;
4104 while (*t && p + 1 < out + outlen)
4105 {
4106 *p++ = *t++;
4107 /* Change a space by fillchar, unless fillchar is '-' and a
4108 * digit follows. */
4109 if (fillable && p[-1] == ' '
4110 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4111 p[-1] = fillchar;
4112 }
4113 for (; l < minwid && p + 1 < out + outlen; l++)
4114 *p++ = fillchar;
4115 }
4116 else if (num >= 0)
4117 {
4118 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4119 char_u nstr[20];
4120
4121 if (p + 20 >= out + outlen)
4122 break; /* not sufficient space */
4123 prevchar_isitem = TRUE;
4124 t = nstr;
4125 if (opt == STL_VIRTCOL_ALT)
4126 {
4127 *t++ = '-';
4128 minwid--;
4129 }
4130 *t++ = '%';
4131 if (zeropad)
4132 *t++ = '0';
4133 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004134 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 *t = 0;
4136
4137 for (n = num, l = 1; n >= nbase; n /= nbase)
4138 l++;
4139 if (opt == STL_VIRTCOL_ALT)
4140 l++;
4141 if (l > maxwid)
4142 {
4143 l += 2;
4144 n = l - maxwid;
4145 while (l-- > maxwid)
4146 num /= nbase;
4147 *t++ = '>';
4148 *t++ = '%';
4149 *t = t[-3];
4150 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004151 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4152 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004155 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4156 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 p += STRLEN(p);
4158 }
4159 else
4160 item[curitem].type = Empty;
4161
4162 if (opt == STL_VIM_EXPR)
4163 vim_free(str);
4164
4165 if (num >= 0 || (!itemisflag && str && *str))
4166 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4167 curitem++;
4168 }
4169 *p = NUL;
4170 itemcnt = curitem;
4171
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004172#ifdef FEAT_EVAL
4173 if (usefmt != fmt)
4174 vim_free(usefmt);
4175#endif
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 width = vim_strsize(out);
4178 if (maxwidth > 0 && width > maxwidth)
4179 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004180 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 l = 0;
4182 if (itemcnt == 0)
4183 s = out;
4184 else
4185 {
4186 for ( ; l < itemcnt; l++)
4187 if (item[l].type == Trunc)
4188 {
4189 /* Truncate at %< item. */
4190 s = item[l].start;
4191 break;
4192 }
4193 if (l == itemcnt)
4194 {
4195 /* No %< item, truncate first item. */
4196 s = item[0].start;
4197 l = 0;
4198 }
4199 }
4200
4201 if (width - vim_strsize(s) >= maxwidth)
4202 {
4203 /* Truncation mark is beyond max length */
4204#ifdef FEAT_MBYTE
4205 if (has_mbyte)
4206 {
4207 s = out;
4208 width = 0;
4209 for (;;)
4210 {
4211 width += ptr2cells(s);
4212 if (width >= maxwidth)
4213 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004214 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216 /* Fill up for half a double-wide character. */
4217 while (++width < maxwidth)
4218 *s++ = fillchar;
4219 }
4220 else
4221#endif
4222 s = out + maxwidth - 1;
4223 for (l = 0; l < itemcnt; l++)
4224 if (item[l].start > s)
4225 break;
4226 itemcnt = l;
4227 *s++ = '>';
4228 *s = 0;
4229 }
4230 else
4231 {
4232#ifdef FEAT_MBYTE
4233 if (has_mbyte)
4234 {
4235 n = 0;
4236 while (width >= maxwidth)
4237 {
4238 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004239 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 }
4242 else
4243#endif
4244 n = width - maxwidth + 1;
4245 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004246 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 *s = '<';
4248
4249 /* Fill up for half a double-wide character. */
4250 while (++width < maxwidth)
4251 {
4252 s = s + STRLEN(s);
4253 *s++ = fillchar;
4254 *s = NUL;
4255 }
4256
4257 --n; /* count the '<' */
4258 for (; l < itemcnt; l++)
4259 {
4260 if (item[l].start - n >= s)
4261 item[l].start -= n;
4262 else
4263 item[l].start = s;
4264 }
4265 }
4266 width = maxwidth;
4267 }
4268 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4269 {
4270 /* Apply STL_MIDDLE if any */
4271 for (l = 0; l < itemcnt; l++)
4272 if (item[l].type == Middle)
4273 break;
4274 if (l < itemcnt)
4275 {
4276 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004277 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 for (s = item[l].start; s < p; s++)
4279 *s = fillchar;
4280 for (l++; l < itemcnt; l++)
4281 item[l].start += maxwidth - width;
4282 width = maxwidth;
4283 }
4284 }
4285
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004286 /* Store the info about highlighting. */
4287 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004289 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 for (l = 0; l < itemcnt; l++)
4291 {
4292 if (item[l].type == Highlight)
4293 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004294 sp->start = item[l].start;
4295 sp->userhl = item[l].minwid;
4296 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004299 sp->start = NULL;
4300 sp->userhl = 0;
4301 }
4302
4303 /* Store the info about tab pages labels. */
4304 if (tabtab != NULL)
4305 {
4306 sp = tabtab;
4307 for (l = 0; l < itemcnt; l++)
4308 {
4309 if (item[l].type == TabPage)
4310 {
4311 sp->start = item[l].start;
4312 sp->userhl = item[l].minwid;
4313 sp++;
4314 }
4315 }
4316 sp->start = NULL;
4317 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319
4320 return width;
4321}
4322#endif /* FEAT_STL_OPT */
4323
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004324#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4325 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004327 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4328 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 */
4330 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004331get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004333 char_u *buf;
4334 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
4336 long above; /* number of lines above window */
4337 long below; /* number of lines below window */
4338
4339 above = wp->w_topline - 1;
4340#ifdef FEAT_DIFF
4341 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4342#endif
4343 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4344 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004345 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4346 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004348 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004350 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 ? (int)(above / ((above + below) / 100L))
4352 : (int)(above * 100L / (above + below)));
4353}
4354#endif
4355
4356/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004357 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 * Return TRUE if it was appended.
4359 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004360 static int
4361append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 win_T *wp;
4363 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004364 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
4367 char_u *p;
4368
4369 if (ARGCOUNT <= 1) /* nothing to do */
4370 return FALSE;
4371
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004372 p = buf + STRLEN(buf); /* go to the end of the buffer */
4373 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 return FALSE;
4375 *p++ = ' ';
4376 *p++ = '(';
4377 if (add_file)
4378 {
4379 STRCPY(p, "file ");
4380 p += 5;
4381 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004382 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4383 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4385 return TRUE;
4386}
4387
4388/*
4389 * If fname is not a full path, make it a full path.
4390 * Returns pointer to allocated memory (NULL for failure).
4391 */
4392 char_u *
4393fix_fname(fname)
4394 char_u *fname;
4395{
4396 /*
4397 * Force expanding the path always for Unix, because symbolic links may
4398 * mess up the full path name, even though it starts with a '/'.
4399 * Also expand when there is ".." in the file name, try to remove it,
4400 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004401 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 * For MS-Windows also expand names like "longna~1" to "longname".
4403 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004404#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return FullName_save(fname, TRUE);
4406#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004407 if (!vim_isAbsName(fname)
4408 || strstr((char *)fname, "..") != NULL
4409 || strstr((char *)fname, "//") != NULL
4410# ifdef BACKSLASH_IN_FILENAME
4411 || strstr((char *)fname, "\\\\") != NULL
4412# endif
4413# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 )
4417 return FullName_save(fname, FALSE);
4418
4419 fname = vim_strsave(fname);
4420
Bram Moolenaar9b942202007-10-03 12:31:33 +00004421# ifdef USE_FNAME_CASE
4422# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004424# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 if (fname != NULL)
4427 fname_case(fname, 0); /* set correct case for file name */
4428 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 return fname;
4432#endif
4433}
4434
4435/*
4436 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4437 * "ffname" becomes a pointer to allocated memory (or NULL).
4438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 void
4440fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004441 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 char_u **ffname;
4443 char_u **sfname;
4444{
4445 if (*ffname == NULL) /* if no file name given, nothing to do */
4446 return;
4447 if (*sfname == NULL) /* if no short file name given, use ffname */
4448 *sfname = *ffname;
4449 *ffname = fix_fname(*ffname); /* expand to full path */
4450
4451#ifdef FEAT_SHORTCUT
4452 if (!buf->b_p_bin)
4453 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004454 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456 /* If the file name is a shortcut file, use the file it links to. */
4457 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004458 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
4460 vim_free(*ffname);
4461 *ffname = rfname;
4462 *sfname = rfname;
4463 }
4464 }
4465#endif
4466}
4467
4468/*
4469 * Get the file name for an argument list entry.
4470 */
4471 char_u *
4472alist_name(aep)
4473 aentry_T *aep;
4474{
4475 buf_T *bp;
4476
4477 /* Use the name from the associated buffer if it exists. */
4478 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004479 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 return aep->ae_fname;
4481 return bp->b_fname;
4482}
4483
4484#if defined(FEAT_WINDOWS) || defined(PROTO)
4485/*
4486 * do_arg_all(): Open up to 'count' windows, one for each argument.
4487 */
4488 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004489do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 int count;
4491 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004492 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
4494 int i;
4495 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004496 char_u *opened; /* Array of weight for which args are open:
4497 * 0: not opened
4498 * 1: opened in other tab
4499 * 2: opened in curtab
4500 * 3: opened in curtab and curwin
4501 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004502 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 int use_firstwin = FALSE; /* use first window for arglist */
4504 int split_ret = OK;
4505 int p_ea_save;
4506 alist_T *alist; /* argument list to be used */
4507 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004508 tabpage_T *tpnext;
4509 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004510 win_T *old_curwin, *last_curwin;
4511 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004512 win_T *new_curwin = NULL;
4513 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
4515 if (ARGCOUNT <= 0)
4516 {
4517 /* Don't give an error message. We don't want it when the ":all"
4518 * command is in the .vimrc. */
4519 return;
4520 }
4521 setpcmark();
4522
4523 opened_len = ARGCOUNT;
4524 opened = alloc_clear((unsigned)opened_len);
4525 if (opened == NULL)
4526 return;
4527
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004528 /* Autocommands may do anything to the argument list. Make sure it's not
4529 * freed while we are working here by "locking" it. We still have to
4530 * watch out for its size to be changed. */
4531 alist = curwin->w_alist;
4532 ++alist->al_refcount;
4533
4534 old_curwin = curwin;
4535 old_curtab = curtab;
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537#ifdef FEAT_GUI
4538 need_mouse_correct = TRUE;
4539#endif
4540
4541 /*
4542 * Try closing all windows that are not in the argument list.
4543 * Also close windows that are not full width;
4544 * When 'hidden' or "forceit" set the buffer becomes hidden.
4545 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004546 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004548 if (had_tab > 0)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004549 goto_tabpage_tp(first_tabpage, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004550 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004552 tpnext = curtab->tp_next;
4553 for (wp = firstwin; wp != NULL; wp = wpnext)
4554 {
4555 wpnext = wp->w_next;
4556 buf = wp->w_buffer;
4557 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004558 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004560 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004562 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004563 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004566 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004567 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004569 if (i < alist->al_ga.ga_len
4570 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4571 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4572 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004574 int weight = 1;
4575
4576 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004577 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004578 ++weight;
4579 if (old_curwin == wp)
4580 ++weight;
4581 }
4582
4583 if (weight > (int)opened[i])
4584 {
4585 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004586 if (i == 0)
4587 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004588 if (new_curwin != NULL)
4589 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004590 new_curwin = wp;
4591 new_curtab = curtab;
4592 }
4593 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004594 else if (keep_tabs)
4595 i = opened_len;
4596
4597 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004598 {
4599 /* Use the current argument list for all windows
4600 * containing a file from it. */
4601 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004602 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004603 ++wp->w_alist->al_refcount;
4604 }
4605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004609 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004611 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004613 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004614 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004616 /* If the buffer was changed, and we would like to hide it,
4617 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004618 if (!P_HID(buf) && buf->b_nwindows <= 1
4619 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004621 (void)autowrite(buf, FALSE);
4622#ifdef FEAT_AUTOCMD
4623 /* check if autocommands removed the window */
4624 if (!win_valid(wp) || !buf_valid(buf))
4625 {
4626 wpnext = firstwin; /* start all over... */
4627 continue;
4628 }
4629#endif
4630 }
4631#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004632 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004633 if (firstwin == lastwin
4634 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004635#endif
4636 use_firstwin = TRUE;
4637#ifdef FEAT_WINDOWS
4638 else
4639 {
4640 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4641# ifdef FEAT_AUTOCMD
4642 /* check if autocommands removed the next window */
4643 if (!win_valid(wpnext))
4644 wpnext = firstwin; /* start all over... */
4645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
4647#endif
4648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
4650 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004651
4652 /* Without the ":tab" modifier only do the current tab page. */
4653 if (had_tab == 0 || tpnext == NULL)
4654 break;
4655
4656# ifdef FEAT_AUTOCMD
4657 /* check if autocommands removed the next tab page */
4658 if (!valid_tabpage(tpnext))
4659 tpnext = first_tabpage; /* start all over...*/
4660# endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02004661 goto_tabpage_tp(tpnext, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 }
4663
4664 /*
4665 * Open a window for files in the argument list that don't have one.
4666 * ARGCOUNT may change while doing this, because of autocommands.
4667 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004668 if (count > opened_len || count <= 0)
4669 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671#ifdef FEAT_AUTOCMD
4672 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4673 ++autocmd_no_enter;
4674 ++autocmd_no_leave;
4675#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004676 last_curwin = curwin;
4677 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004679#ifdef FEAT_WINDOWS
4680 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4681 * leaving an empty tab page when executed locally. */
4682 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4683 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4684 use_firstwin = TRUE;
4685#endif
4686
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004687 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4690 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004691 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693 /* Move the already present window to below the current window */
4694 if (curwin->w_arg_idx != i)
4695 {
4696 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4697 {
4698 if (wpnext->w_arg_idx == i)
4699 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004700 if (keep_tabs)
4701 {
4702 new_curwin = wpnext;
4703 new_curtab = curtab;
4704 }
4705 else
4706 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 break;
4708 }
4709 }
4710 }
4711 }
4712 else if (split_ret == OK)
4713 {
4714 if (!use_firstwin) /* split current window */
4715 {
4716 p_ea_save = p_ea;
4717 p_ea = TRUE; /* use space from all windows */
4718 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4719 p_ea = p_ea_save;
4720 if (split_ret == FAIL)
4721 continue;
4722 }
4723#ifdef FEAT_AUTOCMD
4724 else /* first window: do autocmd for leaving this buffer */
4725 --autocmd_no_leave;
4726#endif
4727
4728 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004729 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 */
4731 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004732 if (i == 0)
4733 {
4734 new_curwin = curwin;
4735 new_curtab = curtab;
4736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4738 ECMD_ONE,
4739 ((P_HID(curwin->w_buffer)
4740 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004741 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742#ifdef FEAT_AUTOCMD
4743 if (use_firstwin)
4744 ++autocmd_no_leave;
4745#endif
4746 use_firstwin = FALSE;
4747 }
4748 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004749
4750 /* When ":tab" was used open a new tab for a new window repeatedly. */
4751 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4752 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754
4755 /* Remove the "lock" on the argument list. */
4756 alist_unlink(alist);
4757
4758#ifdef FEAT_AUTOCMD
4759 --autocmd_no_enter;
4760#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004761 /* restore last referenced tabpage's curwin */
4762 if (last_curtab != new_curtab)
4763 {
4764 if (valid_tabpage(last_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +02004765 goto_tabpage_tp(last_curtab, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004766 if (win_valid(last_curwin))
4767 win_enter(last_curwin, FALSE);
4768 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004769 /* to window with first arg */
4770 if (valid_tabpage(new_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +02004771 goto_tabpage_tp(new_curtab, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004772 if (win_valid(new_curwin))
4773 win_enter(new_curwin, FALSE);
4774
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775#ifdef FEAT_AUTOCMD
4776 --autocmd_no_leave;
4777#endif
4778 vim_free(opened);
4779}
4780
4781# if defined(FEAT_LISTCMDS) || defined(PROTO)
4782/*
4783 * Open a window for a number of buffers.
4784 */
4785 void
4786ex_buffer_all(eap)
4787 exarg_T *eap;
4788{
4789 buf_T *buf;
4790 win_T *wp, *wpnext;
4791 int split_ret = OK;
4792 int p_ea_save;
4793 int open_wins = 0;
4794 int r;
4795 int count; /* Maximum number of windows to open. */
4796 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004797#ifdef FEAT_WINDOWS
4798 int had_tab = cmdmod.tab;
4799 tabpage_T *tpnext;
4800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 if (eap->addr_count == 0) /* make as many windows as possible */
4803 count = 9999;
4804 else
4805 count = eap->line2; /* make as many windows as specified */
4806 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4807 all = FALSE;
4808 else
4809 all = TRUE;
4810
4811 setpcmark();
4812
4813#ifdef FEAT_GUI
4814 need_mouse_correct = TRUE;
4815#endif
4816
4817 /*
4818 * Close superfluous windows (two windows for the same buffer).
4819 * Also close windows that are not full-width.
4820 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004821#ifdef FEAT_WINDOWS
4822 if (had_tab > 0)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004823 goto_tabpage_tp(first_tabpage, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004824 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004827 tpnext = curtab->tp_next;
4828 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004830 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004831 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004832#ifdef FEAT_VERTSPLIT
4833 || ((cmdmod.split & WSP_VERT)
4834 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004835 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004836 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004838#ifdef FEAT_WINDOWS
4839 || (had_tab > 0 && wp != firstwin)
4840#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004841 ) && firstwin != lastwin
4842#ifdef FEAT_AUTOCMD
4843 && !(wp->w_closing || wp->w_buffer->b_closing)
4844#endif
4845 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004846 {
4847 win_close(wp, FALSE);
4848#ifdef FEAT_AUTOCMD
4849 wpnext = firstwin; /* just in case an autocommand does
4850 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004851 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004852 open_wins = 0;
4853#endif
4854 }
4855 else
4856 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004858
4859#ifdef FEAT_WINDOWS
4860 /* Without the ":tab" modifier only do the current tab page. */
4861 if (had_tab == 0 || tpnext == NULL)
4862 break;
Bram Moolenaara8596c42012-06-13 14:28:20 +02004863 goto_tabpage_tp(tpnext, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867 /*
4868 * Go through the buffer list. When a buffer doesn't have a window yet,
4869 * open one. Otherwise move the window to the right position.
4870 * Watch out for autocommands that delete buffers or windows!
4871 */
4872#ifdef FEAT_AUTOCMD
4873 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4874 ++autocmd_no_enter;
4875#endif
4876 win_enter(lastwin, FALSE);
4877#ifdef FEAT_AUTOCMD
4878 ++autocmd_no_leave;
4879#endif
4880 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4881 {
4882 /* Check if this buffer needs a window */
4883 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4884 continue;
4885
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004886#ifdef FEAT_WINDOWS
4887 if (had_tab != 0)
4888 {
4889 /* With the ":tab" modifier don't move the window. */
4890 if (buf->b_nwindows > 0)
4891 wp = lastwin; /* buffer has a window, skip it */
4892 else
4893 wp = NULL;
4894 }
4895 else
4896#endif
4897 {
4898 /* Check if this buffer already has a window */
4899 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4900 if (wp->w_buffer == buf)
4901 break;
4902 /* If the buffer already has a window, move it */
4903 if (wp != NULL)
4904 win_move_after(wp, curwin);
4905 }
4906
4907 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
4909 /* Split the window and put the buffer in it */
4910 p_ea_save = p_ea;
4911 p_ea = TRUE; /* use space from all windows */
4912 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4913 ++open_wins;
4914 p_ea = p_ea_save;
4915 if (split_ret == FAIL)
4916 continue;
4917
4918 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004919#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 swap_exists_action = SEA_DIALOG;
4921#endif
4922 set_curbuf(buf, DOBUF_GOTO);
4923#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004926#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 break;
4930 }
4931#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004932#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (swap_exists_action == SEA_QUIT)
4934 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004935# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4936 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004937
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004938 /* Reset the error/interrupt/exception state here so that
4939 * aborting() returns FALSE when closing a window. */
4940 enter_cleanup(&cs);
4941# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004942
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004943 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 win_close(curwin, TRUE);
4945 --open_wins;
4946 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004947 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004948
4949# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4950 /* Restore the error/interrupt/exception state if not
4951 * discarded by a new aborting error, interrupt, or uncaught
4952 * exception. */
4953 leave_cleanup(&cs);
4954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 else
4957 handle_swap_exists(NULL);
4958#endif
4959 }
4960
4961 ui_breakcheck();
4962 if (got_int)
4963 {
4964 (void)vgetc(); /* only break the file loading, not the rest */
4965 break;
4966 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004967#ifdef FEAT_EVAL
4968 /* Autocommands deleted the buffer or aborted script processing!!! */
4969 if (aborting())
4970 break;
4971#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004972#ifdef FEAT_WINDOWS
4973 /* When ":tab" was used open a new tab for a new window repeatedly. */
4974 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4975 cmdmod.tab = 9999;
4976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978#ifdef FEAT_AUTOCMD
4979 --autocmd_no_enter;
4980#endif
4981 win_enter(firstwin, FALSE); /* back to first window */
4982#ifdef FEAT_AUTOCMD
4983 --autocmd_no_leave;
4984#endif
4985
4986 /*
4987 * Close superfluous windows.
4988 */
4989 for (wp = lastwin; open_wins > count; )
4990 {
4991 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4992 || autowrite(wp->w_buffer, FALSE) == OK);
4993#ifdef FEAT_AUTOCMD
4994 if (!win_valid(wp))
4995 {
4996 /* BufWrite Autocommands made the window invalid, start over */
4997 wp = lastwin;
4998 }
4999 else
5000#endif
5001 if (r)
5002 {
5003 win_close(wp, !P_HID(wp->w_buffer));
5004 --open_wins;
5005 wp = lastwin;
5006 }
5007 else
5008 {
5009 wp = wp->w_prev;
5010 if (wp == NULL)
5011 break;
5012 }
5013 }
5014}
5015# endif /* FEAT_LISTCMDS */
5016
5017#endif /* FEAT_WINDOWS */
5018
Bram Moolenaara3227e22006-03-08 21:32:40 +00005019static int chk_modeline __ARGS((linenr_T, int));
5020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021/*
5022 * do_modelines() - process mode lines for the current file
5023 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005024 * "flags" can be:
5025 * OPT_WINONLY only set options local to window
5026 * OPT_NOWIN don't set options local to window
5027 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 * Returns immediately if the "ml" option isn't set.
5029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00005031do_modelines(flags)
5032 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005034 linenr_T lnum;
5035 int nmlines;
5036 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5039 return;
5040
5041 /* Disallow recursive entry here. Can happen when executing a modeline
5042 * triggers an autocommand, which reloads modelines with a ":do". */
5043 if (entered)
5044 return;
5045
5046 ++entered;
5047 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5048 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005049 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 nmlines = 0;
5051
5052 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5053 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005054 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 nmlines = 0;
5056 --entered;
5057}
5058
5059#include "version.h" /* for version number */
5060
5061/*
5062 * chk_modeline() - check a single line for a mode string
5063 * Return FAIL if an error encountered.
5064 */
5065 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005066chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005068 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
5070 char_u *s;
5071 char_u *e;
5072 char_u *linecopy; /* local copy of any modeline found */
5073 int prev;
5074 int vers;
5075 int end;
5076 int retval = OK;
5077 char_u *save_sourcing_name;
5078 linenr_T save_sourcing_lnum;
5079#ifdef FEAT_EVAL
5080 scid_T save_SID;
5081#endif
5082
5083 prev = -1;
5084 for (s = ml_get(lnum); *s != NUL; ++s)
5085 {
5086 if (prev == -1 || vim_isspace(prev))
5087 {
5088 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5089 || STRNCMP(s, "vi:", (size_t)3) == 0)
5090 break;
5091 if (STRNCMP(s, "vim", 3) == 0)
5092 {
5093 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5094 e = s + 4;
5095 else
5096 e = s + 3;
5097 vers = getdigits(&e);
5098 if (*e == ':'
5099 && (s[3] == ':'
5100 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5101 || (VIM_VERSION_100 < vers && s[3] == '<')
5102 || (VIM_VERSION_100 > vers && s[3] == '>')
5103 || (VIM_VERSION_100 == vers && s[3] == '=')))
5104 break;
5105 }
5106 }
5107 prev = *s;
5108 }
5109
5110 if (*s)
5111 {
5112 do /* skip over "ex:", "vi:" or "vim:" */
5113 ++s;
5114 while (s[-1] != ':');
5115
5116 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5117 if (linecopy == NULL)
5118 return FAIL;
5119
5120 save_sourcing_lnum = sourcing_lnum;
5121 save_sourcing_name = sourcing_name;
5122 sourcing_lnum = lnum; /* prepare for emsg() */
5123 sourcing_name = (char_u *)"modelines";
5124
5125 end = FALSE;
5126 while (end == FALSE)
5127 {
5128 s = skipwhite(s);
5129 if (*s == NUL)
5130 break;
5131
5132 /*
5133 * Find end of set command: ':' or end of line.
5134 * Skip over "\:", replacing it with ":".
5135 */
5136 for (e = s; *e != ':' && *e != NUL; ++e)
5137 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005138 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (*e == NUL)
5140 end = TRUE;
5141
5142 /*
5143 * If there is a "set" command, require a terminating ':' and
5144 * ignore the stuff after the ':'.
5145 * "vi:set opt opt opt: foo" -- foo not interpreted
5146 * "vi:opt opt opt: foo" -- foo interpreted
5147 * Accept "se" for compatibility with Elvis.
5148 */
5149 if (STRNCMP(s, "set ", (size_t)4) == 0
5150 || STRNCMP(s, "se ", (size_t)3) == 0)
5151 {
5152 if (*e != ':') /* no terminating ':'? */
5153 break;
5154 end = TRUE;
5155 s = vim_strchr(s, ' ') + 1;
5156 }
5157 *e = NUL; /* truncate the set command */
5158
5159 if (*s != NUL) /* skip over an empty "::" */
5160 {
5161#ifdef FEAT_EVAL
5162 save_SID = current_SID;
5163 current_SID = SID_MODELINE;
5164#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005165 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166#ifdef FEAT_EVAL
5167 current_SID = save_SID;
5168#endif
5169 if (retval == FAIL) /* stop if error found */
5170 break;
5171 }
5172 s = e + 1; /* advance to next part */
5173 }
5174
5175 sourcing_lnum = save_sourcing_lnum;
5176 sourcing_name = save_sourcing_name;
5177
5178 vim_free(linecopy);
5179 }
5180 return retval;
5181}
5182
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005183#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 int
5185read_viminfo_bufferlist(virp, writing)
5186 vir_T *virp;
5187 int writing;
5188{
5189 char_u *tab;
5190 linenr_T lnum;
5191 colnr_T col;
5192 buf_T *buf;
5193 char_u *sfname;
5194 char_u *xline;
5195
5196 /* Handle long line and escaped characters. */
5197 xline = viminfo_readstring(virp, 1, FALSE);
5198
5199 /* don't read in if there are files on the command-line or if writing: */
5200 if (xline != NULL && !writing && ARGCOUNT == 0
5201 && find_viminfo_parameter('%') != NULL)
5202 {
5203 /* Format is: <fname> Tab <lnum> Tab <col>.
5204 * Watch out for a Tab in the file name, work from the end. */
5205 lnum = 0;
5206 col = 0;
5207 tab = vim_strrchr(xline, '\t');
5208 if (tab != NULL)
5209 {
5210 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005211 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 tab = vim_strrchr(xline, '\t');
5213 if (tab != NULL)
5214 {
5215 *tab++ = '\0';
5216 lnum = atol((char *)tab);
5217 }
5218 }
5219
5220 /* Expand "~/" in the file name at "line + 1" to a full path.
5221 * Then try shortening it by comparing with the current directory */
5222 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005223 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
5225 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5226 if (buf != NULL) /* just in case... */
5227 {
5228 buf->b_last_cursor.lnum = lnum;
5229 buf->b_last_cursor.col = col;
5230 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5231 }
5232 }
5233 vim_free(xline);
5234
5235 return viminfo_readline(virp);
5236}
5237
5238 void
5239write_viminfo_bufferlist(fp)
5240 FILE *fp;
5241{
5242 buf_T *buf;
5243#ifdef FEAT_WINDOWS
5244 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005245 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#endif
5247 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005248 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
5250 if (find_viminfo_parameter('%') == NULL)
5251 return;
5252
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005253 /* Without a number -1 is returned: do all buffers. */
5254 max_buffers = get_viminfo_parameter('%');
5255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005257#define LINE_BUF_LEN (MAXPATHL + 40)
5258 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 if (line == NULL)
5260 return;
5261
5262#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005263 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 set_last_cursor(win);
5265#else
5266 set_last_cursor(curwin);
5267#endif
5268
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005269 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5271 {
5272 if (buf->b_fname == NULL
5273 || !buf->b_p_bl
5274#ifdef FEAT_QUICKFIX
5275 || bt_quickfix(buf)
5276#endif
5277 || removable(buf->b_ffname))
5278 continue;
5279
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005280 if (max_buffers-- == 0)
5281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 putc('%', fp);
5283 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005284 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 (long)buf->b_last_cursor.lnum,
5286 buf->b_last_cursor.col);
5287 viminfo_writestring(fp, line);
5288 }
5289 vim_free(line);
5290}
5291#endif
5292
5293
5294/*
5295 * Return special buffer name.
5296 * Returns NULL when the buffer has a normal file name.
5297 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005298 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299buf_spname(buf)
5300 buf_T *buf;
5301{
5302#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5303 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005304 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005305 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005306 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005307
5308 /*
5309 * For location list window, w_llist_ref points to the location list.
5310 * For quickfix window, w_llist_ref is NULL.
5311 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005312 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005313 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005314 goto win_found;
5315win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005316 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005317 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005318 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005319 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#endif
5322#ifdef FEAT_QUICKFIX
5323 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5324 * contains the name as specified by the user */
5325 if (bt_nofile(buf))
5326 {
5327 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005328 return buf->b_sfname;
5329 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331#endif
5332 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005333 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 return NULL;
5335}
5336
5337
5338#if defined(FEAT_SIGNS) || defined(PROTO)
5339/*
5340 * Insert the sign into the signlist.
5341 */
5342 static void
5343insert_sign(buf, prev, next, id, lnum, typenr)
5344 buf_T *buf; /* buffer to store sign in */
5345 signlist_T *prev; /* previous sign entry */
5346 signlist_T *next; /* next sign entry */
5347 int id; /* sign ID */
5348 linenr_T lnum; /* line number which gets the mark */
5349 int typenr; /* typenr of sign we are adding */
5350{
5351 signlist_T *newsign;
5352
5353 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5354 if (newsign != NULL)
5355 {
5356 newsign->id = id;
5357 newsign->lnum = lnum;
5358 newsign->typenr = typenr;
5359 newsign->next = next;
5360#ifdef FEAT_NETBEANS_INTG
5361 newsign->prev = prev;
5362 if (next != NULL)
5363 next->prev = newsign;
5364#endif
5365
5366 if (prev == NULL)
5367 {
5368 /* When adding first sign need to redraw the windows to create the
5369 * column for signs. */
5370 if (buf->b_signlist == NULL)
5371 {
5372 redraw_buf_later(buf, NOT_VALID);
5373 changed_cline_bef_curs();
5374 }
5375
5376 /* first sign in signlist */
5377 buf->b_signlist = newsign;
5378 }
5379 else
5380 prev->next = newsign;
5381 }
5382}
5383
5384/*
5385 * Add the sign into the signlist. Find the right spot to do it though.
5386 */
5387 void
5388buf_addsign(buf, id, lnum, typenr)
5389 buf_T *buf; /* buffer to store sign in */
5390 int id; /* sign ID */
5391 linenr_T lnum; /* line number which gets the mark */
5392 int typenr; /* typenr of sign we are adding */
5393{
5394 signlist_T *sign; /* a sign in the signlist */
5395 signlist_T *prev; /* the previous sign */
5396
5397 prev = NULL;
5398 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5399 {
5400 if (lnum == sign->lnum && id == sign->id)
5401 {
5402 sign->typenr = typenr;
5403 return;
5404 }
5405 else if (
5406#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5407 id < 0 &&
5408#endif
5409 lnum < sign->lnum)
5410 {
5411#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5412 /* XXX - GRP: Is this because of sign slide problem? Or is it
5413 * really needed? Or is it because we allow multiple signs per
5414 * line? If so, should I add that feature to FEAT_SIGNS?
5415 */
5416 while (prev != NULL && prev->lnum == lnum)
5417 prev = prev->prev;
5418 if (prev == NULL)
5419 sign = buf->b_signlist;
5420 else
5421 sign = prev->next;
5422#endif
5423 insert_sign(buf, prev, sign, id, lnum, typenr);
5424 return;
5425 }
5426 prev = sign;
5427 }
5428#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5429 /* XXX - GRP: See previous comment */
5430 while (prev != NULL && prev->lnum == lnum)
5431 prev = prev->prev;
5432 if (prev == NULL)
5433 sign = buf->b_signlist;
5434 else
5435 sign = prev->next;
5436#endif
5437 insert_sign(buf, prev, sign, id, lnum, typenr);
5438
5439 return;
5440}
5441
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005442 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443buf_change_sign_type(buf, markId, typenr)
5444 buf_T *buf; /* buffer to store sign in */
5445 int markId; /* sign ID */
5446 int typenr; /* typenr of sign we are adding */
5447{
5448 signlist_T *sign; /* a sign in the signlist */
5449
5450 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5451 {
5452 if (sign->id == markId)
5453 {
5454 sign->typenr = typenr;
5455 return sign->lnum;
5456 }
5457 }
5458
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005459 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460}
5461
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005462 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463buf_getsigntype(buf, lnum, type)
5464 buf_T *buf;
5465 linenr_T lnum;
5466 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5467{
5468 signlist_T *sign; /* a sign in a b_signlist */
5469
5470 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5471 if (sign->lnum == lnum
5472 && (type == SIGN_ANY
5473# ifdef FEAT_SIGN_ICONS
5474 || (type == SIGN_ICON
5475 && sign_get_image(sign->typenr) != NULL)
5476# endif
5477 || (type == SIGN_TEXT
5478 && sign_get_text(sign->typenr) != NULL)
5479 || (type == SIGN_LINEHL
5480 && sign_get_attr(sign->typenr, TRUE) != 0)))
5481 return sign->typenr;
5482 return 0;
5483}
5484
5485
5486 linenr_T
5487buf_delsign(buf, id)
5488 buf_T *buf; /* buffer sign is stored in */
5489 int id; /* sign id */
5490{
5491 signlist_T **lastp; /* pointer to pointer to current sign */
5492 signlist_T *sign; /* a sign in a b_signlist */
5493 signlist_T *next; /* the next sign in a b_signlist */
5494 linenr_T lnum; /* line number whose sign was deleted */
5495
5496 lastp = &buf->b_signlist;
5497 lnum = 0;
5498 for (sign = buf->b_signlist; sign != NULL; sign = next)
5499 {
5500 next = sign->next;
5501 if (sign->id == id)
5502 {
5503 *lastp = next;
5504#ifdef FEAT_NETBEANS_INTG
5505 if (next != NULL)
5506 next->prev = sign->prev;
5507#endif
5508 lnum = sign->lnum;
5509 vim_free(sign);
5510 break;
5511 }
5512 else
5513 lastp = &sign->next;
5514 }
5515
5516 /* When deleted the last sign need to redraw the windows to remove the
5517 * sign column. */
5518 if (buf->b_signlist == NULL)
5519 {
5520 redraw_buf_later(buf, NOT_VALID);
5521 changed_cline_bef_curs();
5522 }
5523
5524 return lnum;
5525}
5526
5527
5528/*
5529 * Find the line number of the sign with the requested id. If the sign does
5530 * not exist, return 0 as the line number. This will still let the correct file
5531 * get loaded.
5532 */
5533 int
5534buf_findsign(buf, id)
5535 buf_T *buf; /* buffer to store sign in */
5536 int id; /* sign ID */
5537{
5538 signlist_T *sign; /* a sign in the signlist */
5539
5540 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5541 if (sign->id == id)
5542 return sign->lnum;
5543
5544 return 0;
5545}
5546
5547 int
5548buf_findsign_id(buf, lnum)
5549 buf_T *buf; /* buffer whose sign we are searching for */
5550 linenr_T lnum; /* line number of sign */
5551{
5552 signlist_T *sign; /* a sign in the signlist */
5553
5554 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5555 if (sign->lnum == lnum)
5556 return sign->id;
5557
5558 return 0;
5559}
5560
5561
5562# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5563/* see if a given type of sign exists on a specific line */
5564 int
5565buf_findsigntype_id(buf, lnum, typenr)
5566 buf_T *buf; /* buffer whose sign we are searching for */
5567 linenr_T lnum; /* line number of sign */
5568 int typenr; /* sign type number */
5569{
5570 signlist_T *sign; /* a sign in the signlist */
5571
5572 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5573 if (sign->lnum == lnum && sign->typenr == typenr)
5574 return sign->id;
5575
5576 return 0;
5577}
5578
5579
5580# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5581/* return the number of icons on the given line */
5582 int
5583buf_signcount(buf, lnum)
5584 buf_T *buf;
5585 linenr_T lnum;
5586{
5587 signlist_T *sign; /* a sign in the signlist */
5588 int count = 0;
5589
5590 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5591 if (sign->lnum == lnum)
5592 if (sign_get_image(sign->typenr) != NULL)
5593 count++;
5594
5595 return count;
5596}
5597# endif /* FEAT_SIGN_ICONS */
5598# endif /* FEAT_NETBEANS_INTG */
5599
5600
5601/*
5602 * Delete signs in buffer "buf".
5603 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005604 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605buf_delete_signs(buf)
5606 buf_T *buf;
5607{
5608 signlist_T *next;
5609
5610 while (buf->b_signlist != NULL)
5611 {
5612 next = buf->b_signlist->next;
5613 vim_free(buf->b_signlist);
5614 buf->b_signlist = next;
5615 }
5616}
5617
5618/*
5619 * Delete all signs in all buffers.
5620 */
5621 void
5622buf_delete_all_signs()
5623{
5624 buf_T *buf; /* buffer we are checking for signs */
5625
5626 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5627 if (buf->b_signlist != NULL)
5628 {
5629 /* Need to redraw the windows to remove the sign column. */
5630 redraw_buf_later(buf, NOT_VALID);
5631 buf_delete_signs(buf);
5632 }
5633}
5634
5635/*
5636 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5637 */
5638 void
5639sign_list_placed(rbuf)
5640 buf_T *rbuf;
5641{
5642 buf_T *buf;
5643 signlist_T *p;
5644 char lbuf[BUFSIZ];
5645
5646 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5647 msg_putchar('\n');
5648 if (rbuf == NULL)
5649 buf = firstbuf;
5650 else
5651 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005652 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
5654 if (buf->b_signlist != NULL)
5655 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005656 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5658 msg_putchar('\n');
5659 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005660 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005662 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5664 MSG_PUTS(lbuf);
5665 msg_putchar('\n');
5666 }
5667 if (rbuf != NULL)
5668 break;
5669 buf = buf->b_next;
5670 }
5671}
5672
5673/*
5674 * Adjust a placed sign for inserted/deleted lines.
5675 */
5676 void
5677sign_mark_adjust(line1, line2, amount, amount_after)
5678 linenr_T line1;
5679 linenr_T line2;
5680 long amount;
5681 long amount_after;
5682{
5683 signlist_T *sign; /* a sign in a b_signlist */
5684
5685 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5686 {
5687 if (sign->lnum >= line1 && sign->lnum <= line2)
5688 {
5689 if (amount == MAXLNUM)
5690 sign->lnum = line1;
5691 else
5692 sign->lnum += amount;
5693 }
5694 else if (sign->lnum > line2)
5695 sign->lnum += amount_after;
5696 }
5697}
5698#endif /* FEAT_SIGNS */
5699
5700/*
5701 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5702 */
5703 void
5704set_buflisted(on)
5705 int on;
5706{
5707 if (on != curbuf->b_p_bl)
5708 {
5709 curbuf->b_p_bl = on;
5710#ifdef FEAT_AUTOCMD
5711 if (on)
5712 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5713 else
5714 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5715#endif
5716 }
5717}
5718
5719/*
5720 * Read the file for "buf" again and check if the contents changed.
5721 * Return TRUE if it changed or this could not be checked.
5722 */
5723 int
5724buf_contents_changed(buf)
5725 buf_T *buf;
5726{
5727 buf_T *newbuf;
5728 int differ = TRUE;
5729 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 exarg_T ea;
5732
5733 /* Allocate a buffer without putting it in the buffer list. */
5734 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5735 if (newbuf == NULL)
5736 return TRUE;
5737
5738 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5739 if (prep_exarg(&ea, buf) == FAIL)
5740 {
5741 wipe_buffer(newbuf, FALSE);
5742 return TRUE;
5743 }
5744
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 /* set curwin/curbuf to buf and save a few things */
5746 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaar4770d092006-01-12 23:22:24 +00005748 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 && readfile(buf->b_ffname, buf->b_fname,
5750 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5751 &ea, READ_NEW | READ_DUMMY) == OK)
5752 {
5753 /* compare the two files line by line */
5754 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5755 {
5756 differ = FALSE;
5757 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5758 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5759 {
5760 differ = TRUE;
5761 break;
5762 }
5763 }
5764 }
5765 vim_free(ea.cmd);
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 /* restore curwin/curbuf and a few other things */
5768 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
5770 if (curbuf != newbuf) /* safety check */
5771 wipe_buffer(newbuf, FALSE);
5772
5773 return differ;
5774}
5775
5776/*
5777 * Wipe out a buffer and decrement the last buffer number if it was used for
5778 * this buffer. Call this to wipe out a temp buffer that does not contain any
5779 * marks.
5780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 void
5782wipe_buffer(buf, aucmd)
5783 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005784 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786 if (buf->b_fnum == top_file_num - 1)
5787 --top_file_num;
5788
5789#ifdef FEAT_AUTOCMD
5790 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005791 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005793 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794#ifdef FEAT_AUTOCMD
5795 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005796 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797#endif
5798}